/** * Reads TWO Bytes from the SPI Interface and returns them as * a 16 Bit value with first byte read in the upper 8 bits. */ uint16_t readWordSPI(void) { uint16_t data = 0; data = readSPI() << 8; data |= readSPI(); return data; }
int readSECTOR( LBA a, char *p) // a LBA (logic block address) of sector requested // p ponter to sector buffer // returns TRUE if succesful { int r, i; // 1. send READ command sendSDCmd(READ_SINGLE, (a <<9)); if ( r == 0) //check if command was received { // 2. wait for a response for( i=0; i<R_TIMEOUT; i++) { r = readSPI(); if ( r == DATA_START) break; } // 3. if it did not timeout, read 512 byte of data if ( i != R_TIMEOUT) { i = 512 do { *p++ = readSPI(); } while (---i>0); // 4. ignore CRC readSPI(); readSPI(); } // data arrived } // command accepted
// a LBA of sector requested // p pointer to sector buffer // returns TRUE if successful int writeSECTOR(LBA a, char *p) { unsigned r, i; // 0. check Write Protect if (getWP()) return FAIL; // 1. send WRITE command r = sendSDCmd(WRITE_SINGLE, (a << 9)); if (r == 0) // check if command was accepted { // 2. send data writeSPI(DATA_START); // send 512 bytes of data for(i=0; i<512; i++) writeSPI(*p++); // 3. send dummy CRC clockSPI(); clockSPI(); // 4. check if data accepted r = readSPI(); if ((r & 0xf) == DATA_ACCEPT) { #ifdef WRITE_LED digitalwrite(WRITE_LED, 0); #endif // 5. wait for write completion for(i=0; i<W_TIMEOUT; i++) { r = readSPI(); if (r != 0 ) break; } #ifdef WRITE_LED digitalwrite(WRITE_LED, 1); #endif } // accepted else { r = FAIL; } } // command accepted // 6. disable the card disableSD(); return (r); // return TRUE if successful } // writeSECTOR
int sendSDCmd ( unsigned char c, unsigned a) // c command code // a bte address of data block { int i, r; //enable SD card enableSD(); // send a command packet (6 bytes) writeSPI( c | 0x40); //send command writeSPI( a>>24); //msb of the address writeSPI( a>>16); writeSPI( a>>8); writeSPI( a); //lsb writeSPI(0x95); //send CMD0 CRC // now wait for a response, allow to 8 bytes delay for( i=0; i<8; i++) { r=readSPI(); if ( r != 0xFF) break; } return ( r); // NOTE SDCS is still low! } // sendSDCmd
// a LBA of sector requested // p pointer to sector buffer // returns TRUE if successful int readSECTOR(LBA a, char *p) { int r, i; #ifdef READ_LED digitalwrite(READ_LED, 0); #endif // 1. send READ command r = sendSDCmd(READ_SINGLE, (a << 9)); if (r == 0) // check if command was accepted { // 2. wait for a response for(i=0; i<R_TIMEOUT; i++) { r = readSPI(); if (r == DATA_START) break; } // 3. if it did not timeout, read 512 byte of data if (i != R_TIMEOUT) { i = 512; do{ *p++ = readSPI(); } while (--i>0); // 4. ignore CRC readSPI(); readSPI(); } // data arrived } // command accepted // 5. remember to disable the card disableSD(); #ifdef READLED digital(READ_LED, 1); #endif return (r == DATA_START); // return TRUE if successful } // readSECTOR
// c command code // a byte address of data block int sendSDCmd(unsigned char c, unsigned a) { int i, r; // enable SD card // CS low enableSD(); // send a comand packet (6 bytes) writeSPI(c | 0x40); // send command writeSPI(a>>24); // msb of the address writeSPI(a>>16); writeSPI(a>>8); writeSPI(a); // lsb writeSPI(0x95); // send CMD0 CRC // now wait for a response, allow for up to 8 bytes delay for(i=0; i<8; i++) { r = readSPI(); if (r != 0xFF) break; } return (r); /* return response FF - timeout 00 - command accepted 01 - command received, card in idle state after RESET other codes: bit 0 = Idle state bit 1 = Erase Reset bit 2 = Illegal command bit 3 = Communication CRC error bit 4 = Erase sequence error bit 5 = Address error bit 6 = Parameter error bit 7 = Always 0 */ // NOTE CSCD is still low! } // sendSDCmd
float readADC(){ float vref = 3.3; float voltage; unsigned int hex; // read the ADC hex = readSPI(SPI_MODE_ADC_32, ADC_CONFIG_BITS, ADC); // read the ADC and change ADC chip select //------------------------------------------------- //xil_printf("\n\nADC Value = %x", hex); // convert hex to voltage hex &= 0x00000FFF; // clear configByte and other garbage //xil_printf("\nValue = %x", hex); //xil_printf("\nValue = %d", hex); voltage = vref * hex / (4096.0); //------------------------------------------------- // scale voltage from 0V --- +3.3V to -10V --- +10V voltage -= 1.65; // offset voltage; voltage *= 6.0; // gain of op amp //------------------------------------------------- return voltage; }
int main (void) { //Set Pin Modes // pinMode(PIN_RaspiReset, OUTPUT); // pinMode(PIN_IHTempSensor, INPUT/*_ANALOG*/); // pinMode(PIN_MotorPosSensor, INPUT_PULLUP); pinMode(PIN_Ventil, OUTPUT); // pinMode(PIN_SafetyChain, OUTPUT); pinMode(PIN_PusherLocked, INPUT_PULLUP); pinMode(PIN_LidLocked, INPUT_PULLUP); pinMode(PIN_LidClosed, INPUT_PULLUP); // pinMode(PIN_SimulateButton0, OUTPUT); // pinMode(PIN_IHOff, OUTPUT); // pinMode(PIN_isIHOn, INPUT_PULLUP); // pinMode(PIN_IHOn, OUTPUT); // pinMode(PIN_MotorPWM, OUTPUT/*_PWM*/); // pinMode(PIN_IHPowerPWM, OUTPUT/*_PWM*/); // pinMode(PIN_IHFanPWM, OUTPUT/*_PWM*/); Motor_init(); Heating_init(); initTime(); //Init LoL-Shield // #ifdef GRAYSCALE LedSign_Init(DOUBLE_BUFFER | GRAYSCALE); //Initializes the screen // #else // LedSign_Init(DOUBLE_BUFFER); //Initializes the screen // #endif LedSign_Clear(0); LedSign_Flip(false); //initialize time messurement //initTime(); //Init SPI SPI_init(wdtRestart); //Disable JTAG interface MCUCR = _BV(JTD); MCUCR = _BV(JTD); //Need to write twice to disable it (Atmega_644.pdf, page 267, 23.8.1) #ifdef DEBUG_MODE if (isExternalReset){ wdtRestartCount = wdtRestartCount + 1; } //TODO remove, test was WTD reset if (wdtRestart && wdtRestartLast){ wdtRestart = false; } wdtRestartLast = wdtRestart; digitalWrite(PIN_Ventil, wdtRestart); wdtRestart = false; char valueAsString[10]; sprintf(valueAsString, "R%X", wdtRestartCount); //HEX //sprintf(valueAsString, "%d", wdtRestartCount); //DEC DisplayHandler_setText2(valueAsString); DisplayHandler_displayText(false); _delay_ms(50); #endif LedSign_Clear(PIXEL_HALF); LedSign_Flip(true); _delay_ms(100); LedSign_Clear(PIXEL_OFF); LedSign_Flip(true); /* digitalWrite(PIN_Ventil, HIGH); _delay_ms(5000); digitalWrite(PIN_Ventil, LOW); */ if (isStartup){ //Was power On reset LedSign_Clear(PIXEL_OFF); LedSign_Flip(true); DisplayHandler_setText("EveryCook is starting"); currentMode = SPI_MODE_DISPLAY_TEXT; } else { //is other than power on reset DisplayHandler_setPicture(&picture_hi[0]); DisplayHandler_DisplayBitMap(); } initWatchDog(); while(1){ Motor_motorControl(); Heating_heatControl(); Heating_controlIHTemp(); checkLocks(); if (!LidClosed) Motor_setMotor(0); if (availableSPI() > 0) { triggerWatchDog(true); uint8_t data; uint8_t newMode = readSPI(true); char* newText; uint8_t readAmount = 0; boolean setOn = false; boolean setOff = false; switch(newMode){ case SPI_MODE_IDLE: //DisplayHandler_setPicture(&picture_0[0]); //DisplayHandler_DisplayBitMap(); break; case SPI_MODE_GET_STATUS: nextResponse = StatusByte; //DisplayHandler_setPicture(&picture_1[0]); //DisplayHandler_DisplayBitMap(); break; case SPI_MODE_DISPLAY_CLEAR: LedSign_Clear(0); LedSign_Flip(true); currentMode = 0; nextResponse = SPI_CommandOK; break; case SPI_MODE_MOTOR: data = readSPI(true); wdt_reset(); if(LidClosed){ Motor_setMotor(data); }else{ Motor_setMotor(0); } nextResponse = SPI_CommandOK; //DisplayHandler_setPicture(&picture_2[0]); //DisplayHandler_DisplayBitMap(); break; case SPI_MODE_HEATING: data = readSPI(true); wdt_reset(); Heating_setHeating(data); nextResponse = SPI_CommandOK; //DisplayHandler_setPicture(&picture_3[0]); //DisplayHandler_DisplayBitMap(); break; case SPI_MODE_GET_DEBUG: nextResponse = Vdebug; //DisplayHandler_setPicture(&picture_18[0]); //DisplayHandler_DisplayBitMap(); break; case SPI_MODE_VENTIL: VentilState = readSPI(true); wdt_reset(); digitalWrite(PIN_Ventil, VentilState); nextResponse = SPI_CommandOK; //DisplayHandler_setPicture(&picture_10[0]); //DisplayHandler_DisplayBitMap(); break; case SPI_MODE_DISPLAY_PERCENT: case SPI_MODE_DISPLAY_PERCENT_TEXT: data = readSPI(true); wdt_reset(); DisplayHandler_displayProgress(data, newMode == SPI_MODE_DISPLAY_PERCENT_TEXT); lastPercentValue = data; if (newMode != SPI_MODE_DISPLAY_PERCENT_TEXT){ nextResponse = SPI_CommandOK; currentMode = newMode; break; } case SPI_MODE_DISPLAY_TEXT: case SPI_MODE_DISPLAY_TEXT_SMALL: if (DisplayHandler_readText()) {//[01]<textlen 1 Byte><text>[00<control byte>] if (newMode == SPI_MODE_DISPLAY_TEXT){ DisplayHandler_displayText(false); } else { DisplayHandler_displayText(true); } lastTextUpdate = millis(); if (newMode == SPI_MODE_DISPLAY_PERCENT_TEXT){ updatePercentAgain = true; } nextResponse = SPI_CommandOK; currentMode = newMode; } else { nextResponse = SPI_Error_Text_Invalid; } break; case SPI_MODE_DISPLAY_PICTURE: readAmount=0; while (readAmount < 9){ if (availableSPI() >= 2) { data = readSPI(true); picture[readAmount] = data << 8; data = readSPI(true); picture[readAmount] |= data; readAmount++; wdt_reset(); } else { _delay_ms(1); } } if (availableSPI() == 0) { _delay_ms(10); if (availableSPI() == 0) { _delay_ms(20); } } if (availableSPI() > 0 && peekSPI() == 0x00) { readSPI(false); DisplayHandler_setPicture(&picture[0]); DisplayHandler_DisplayBitMap(); currentMode = newMode; nextResponse = SPI_CommandOK; } else { DisplayHandler_setPicture(&picture[0]); DisplayHandler_DisplayBitMap(); currentMode = newMode; nextResponse = SPI_Error_Picture_Invalid; } break; case SPI_MODE_MAINTENANCE: data = readSPI(true); if (data == 0x99) { setOn = true; } else if (data == 0x22){ setOff = true; } //be sure its change to maintenance, it has the be send 2 times data = readSPI(true); if (data == SPI_MODE_MAINTENANCE) { data = readSPI(true); if (setOn && data != 0x99) { setOn = false; } else if (setOff && data != 0x22){ setOff = false; } data = readSPI(true); //00 as command end mark if (data == 0x00){ if (setOn){ isMaintenanceMode = true; nextResponse = SPI_CommandOK; } else if (setOff){ isMaintenanceMode = false; nextResponse = SPI_CommandOK; } else { nextResponse = SPI_Error_Unknown_Command; } } else { nextResponse = SPI_Error_Unknown_Command; } } else { nextResponse = SPI_Error_Unknown_Command; } break; case SPI_MODE_GET_MOTOR_SPEED: nextResponse = outputValueMotor; //DisplayHandler_setPicture(&picture_12[0]); //DisplayHandler_DisplayBitMap(); break; case SPI_MODE_GET_IGBT_TEMP: nextResponse = ihTemp8bit; //DisplayHandler_setPicture(&picture_hi[0]); //DisplayHandler_DisplayBitMap(); break; case SPI_MODE_GET_FAN_PWM: nextResponse = lastIHFanPWM; //DisplayHandler_setPicture(&picture_17[0]); //DisplayHandler_DisplayBitMap(); break; case SPI_MODE_GET_HEATING_OUTPUT_LEVEL: nextResponse = 0; //nextResponse = Heating_getLastOnPWM(); //DisplayHandler_setPicture(&picture_14[0]); //DisplayHandler_DisplayBitMap(); break; case SPI_MODE_GET_MOTOR_POS_SENSOR: nextResponse = lastSensorValue; //DisplayHandler_setPicture(&picture_15[0]); //DisplayHandler_DisplayBitMap(); break; case SPI_MODE_GET_MOTOR_RPM: nextResponse = rpm; //DisplayHandler_setPicture(&picture_16[0]); //DisplayHandler_DisplayBitMap(); break; default: nextResponse = SPI_Error_Unknown_Command; } } else { triggerWatchDog(false); switch(currentMode){ case SPI_MODE_IDLE: break; case SPI_MODE_DISPLAY_PERCENT_TEXT: if (updatePercentAgain) { DisplayHandler_displayProgress(lastPercentValue, true); updatePercentAgain = false; } case SPI_MODE_DISPLAY_TEXT: case SPI_MODE_DISPLAY_TEXT_SMALL: if ((millis() - lastTextUpdate) > TEXT_UPDATE_TIMEOUT){ lastTextUpdate = millis(); if(!DisplayHandler_displayText(currentMode != SPI_MODE_DISPLAY_TEXT)){ currentMode = 0; } } break; } //DisplayHandler_setPicture(&picture_0[0]); //DisplayHandler_DisplayBitMap(); } } return 0; }
unsigned char transferSPI(unsigned char data) { writeSPI(data); //write data and wait and of transmit return readSPI(); //get the data from SPI }