/* * reads the calibration state from channels variable * reads the WiFi config from configuration variable * then calls HAConfigSave */ void SaveCalibrationStateToFlash() { #if defined(STACK_USE_UART) UARTWrite(1,"Saving Settings\r\n"); UARTWrite(1,"\r\n"); #endif unsigned long int saveAddress = CALIBRATION_SETTING_ADDRESS; SPIFlashEraseSector(CALIBRATION_SETTING_ADDRESS_BLOCK1); SPIFlashEraseSector(CALIBRATION_SETTING_ADDRESS_BLOCK2); SPIFlashBeginWrite(saveAddress); vTaskSuspendAll(); SPIFlashWrite(0x0A); SPIFlashWrite(0x0B); xTaskResumeAll(); saveAddress += 2; int i; for(i=0; i<NUM_OF_CHANNELS; i++) { switch (channels[i].channelType) { case BLINDS: saveAddress = SaveVariableToFlash(saveAddress, (BYTE *) &channels[i].channelStatus.blinds.calibrationStatus, sizeof(BlindsCalibrationStatus)); saveAddress = SaveVariableToFlash(saveAddress, (BYTE *) &channels[i].channelStatus.blinds.channelStatus.buttonUpStatus.buttonSettings, sizeof(ButtonSettings)); saveAddress = SaveVariableToFlash(saveAddress, (BYTE *) &channels[i].channelStatus.blinds.channelStatus.buttonDownStatus.buttonSettings, sizeof(ButtonSettings)); break; case ONOFF_W_KEY: case ONOFF_W_BUTTON: saveAddress = SaveVariableToFlash(saveAddress, (BYTE *) &channels[i].channelStatus.onOff.channelStatus.buttonStatus.buttonSettings, sizeof(ButtonSettings)); break; case ONOFF_BUTTON: saveAddress = SaveVariableToFlash(saveAddress, (BYTE *) &channels[i].channelStatus.onOffButton.buttonStatus.buttonSettings, sizeof(ButtonSettings)); break; default: break; } saveAddress = SaveVariableToFlash(saveAddress, (BYTE *) &channels[i].channelPower, sizeof(unsigned int)); } }
int main() { char c; char str[200]; int i; int address; int data; unsigned int mid, pid; uart0_init(); // 波特率115200,8N1(8个数据位,无校验位,1个停止位) SPIInit(); OLEDInit(); OLEDPrint(0, 0, "Hello World SPI!"); SPIFlashReadID(&mid, &pid); printf("SPI FLASH : Mid = 0x%2x, Pid = 0x%2x\n\r", mid, pid); sprintf(str, "SPI FLASH: %2x,%2x", mid, pid); OLEDPrint(2, 0, str); SPIFlashInit(); SPIFlashEraseSector(4096); SPIFlashEraseSector(1024); SPIFlashProgram(4096, "Happy NEW YEAR !", 20); SPIFlashProgram(1024, "Chinese New Year", 20); SPIFlashRead(1024, str, 18); printf("SPI Flash read from 1024: %s\n\r", str); // OLEDPrint(4, 0, str); SPIFlashRead(4096, str, 18); printf("SPI Flash read from 4096: %s\n\r", str); //OLEDPrint(6, 0, str); Test_Adc(); i2c_init(); at24cxx_write(0,0x11); data = at24cxx_read(0); if (data == 0x11) OLEDPrint(4, 0, "I2C OK!!!!"); else OLEDPrint(4, 0, "I2C ERROR!"); return 0; }
void FOTAEraseFlash() { unsigned long int mem_ind; _dbgwrite("Erasing flash... "); for (mem_ind = FLASH_START_ADD; mem_ind < 0x1FFFFF; mem_ind += PAGE_SIZE) { vTaskSuspendAll(); SPIFlashEraseSector(mem_ind); xTaskResumeAll(); } _dbgwrite("Done!\r\n"); }
void SaveWiFiStateToFlash() { #if defined(STACK_USE_UART) UARTWrite(1,"Saving WiFi State\r\n"); UARTWrite(1,"IP Address: "); UARTWrite(1,config_parms.MyIPAddr); UARTWrite(1,"\r\n"); #endif SPIFlashEraseSector(WIFI_SETTING_ADDRESS); SPIFlashBeginWrite(WIFI_SETTING_ADDRESS); vTaskSuspendAll(); SPIFlashWrite(0x0A); SPIFlashWrite(0x0B); xTaskResumeAll(); SaveVariableToFlash(WIFI_SETTING_ADDRESS + 2, (BYTE *) &config_parms, sizeof(config_parms)); }
void DeleteWiFiState() { WFCustomDelete(); SPIFlashEraseSector(WIFI_SETTING_ADDRESS); }
/***************************************************************************** Function: void SPIFlashWriteArray(BYTE* vData, WORD wLen) Summary: Writes an array of bytes to the SPI Flash part. Description: This function writes an array of bytes to the SPI Flash part. When the address pointer crosses a sector boundary (and has more data to write), the next sector will automatically be erased. If the current address pointer indicates an address that is not a sector boundary and is not already erased, the chip will silently ignore the write command until the next sector boundary is crossed. Precondition: SPIFlashInit and SPIFlashBeginWrite have been called, and the current address is either the front of a sector or has already been erased. Parameters: vData - The array to write to the next memory location wLen - The length of the data to be written Returns: None Remarks: See Remarks in SPIFlashBeginWrite for important information about Flash memory parts. ***************************************************************************/ void SPIFlashWriteArray(BYTE* vData, WORD wLen) { volatile BYTE Dummy; BYTE vSPIONSave; #if defined(__18CXX) BYTE SPICON1Save; #elif defined(__C30__) WORD SPICON1Save; #else DWORD SPICON1Save; #endif BOOL isStarted; BYTE vOpcode; BYTE i; // Do nothing if no data to process if(wLen == 0u) return; // Save SPI state (clock speed) SPICON1Save = SPIFLASH_SPICON1; vSPIONSave = SPI_ON_BIT; // Configure SPI SPI_ON_BIT = 0; SPIFLASH_SPICON1 = PROPER_SPICON1; SPI_ON_BIT = 1; // If starting at an odd address, write a single byte if((dwWriteAddr & 0x01) && wLen) { SPIFlashWrite(*vData); vData++; wLen--; } // Assume we are using AAI Word program mode unless changed later vOpcode = WRITE_WORD_STREAM; isStarted = FALSE; // Loop over all remaining WORDs while(wLen > 1) { // Don't do anything until chip is ready _WaitWhileBusy(); // If address is a sector boundary if((dwWriteAddr & SPI_FLASH_SECTOR_MASK) == 0) SPIFlashEraseSector(dwWriteAddr); // If not yet started, initiate AAI mode if(!isStarted) { // Enable writing _SendCmd(WREN); // Select appropriate programming opcode. The WRITE_WORD_STREAM // mode is the default if neither of these flags are set. if(deviceCaps.bits.bWriteByteStream) vOpcode = WRITE_BYTE_STREAM; else if(deviceCaps.bits.bPageProgram) { // Note: Writing one byte at a time is extremely slow (ex: ~667 // bytes/second write speed on SST SST25VF064C). You can // improve this by over a couple of orders of magnitude by // writing a function to write full pages of up to 256 bytes at // a time. This is implemented this way only because I don't // have an SST25VF064C handy to test with right now. -HS while(wLen--) SPIFlashWrite(*vData++); return; } // Activate the chip select SPIFLASH_CS_IO = 0; ClearSPIDoneFlag(); // Issue WRITE_xxx_STREAM command with address SPIFLASH_SSPBUF = vOpcode; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; SPIFLASH_SSPBUF = ((BYTE*)&dwWriteAddr)[2]; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; SPIFLASH_SSPBUF = ((BYTE*)&dwWriteAddr)[1]; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; SPIFLASH_SSPBUF = ((BYTE*)&dwWriteAddr)[0]; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; isStarted = TRUE; } // Otherwise, just write the AAI command again else { // Assert the chip select pin SPIFLASH_CS_IO = 0; ClearSPIDoneFlag(); // Issue the WRITE_STREAM command for continuation SPIFLASH_SSPBUF = vOpcode; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; } // Write a byte or two for(i = 0; i <= deviceCaps.bits.bWriteWordStream; i++) { SPIFLASH_SSPBUF = *vData++; dwWriteAddr++; wLen--; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; } // Release the chip select to begin the write SPIFLASH_CS_IO = 1; // If a boundary was reached, end the write if((dwWriteAddr & SPI_FLASH_SECTOR_MASK) == 0) { _WaitWhileBusy(); _SendCmd(WRDI); isStarted = FALSE; } } // Wait for write to complete, then exit AAI mode _WaitWhileBusy(); _SendCmd(WRDI); // If a byte remains, write the odd address if(wLen) SPIFlashWrite(*vData); // Restore SPI state SPI_ON_BIT = 0; SPIFLASH_SPICON1 = SPICON1Save; SPI_ON_BIT = vSPIONSave; }
/***************************************************************************** Function: void SPIFlashWrite(BYTE vData) Summary: Writes a byte to the SPI Flash part. Description: This function writes a byte to the SPI Flash part. If the current address pointer indicates the beginning of a 4kB sector, the entire sector will first be erased to allow writes to proceed. If the current address pointer indicates elsewhere, it will be assumed that the sector has already been erased. If this is not true, the chip will silently ignore the write command. Precondition: SPIFlashInit and SPIFlashBeginWrite have been called, and the current address is either the front of a 4kB sector or has already been erased. Parameters: vData - The byte to write to the next memory location. Returns: None Remarks: See Remarks in SPIFlashBeginWrite for important information about Flash memory parts. ***************************************************************************/ void SPIFlashWrite(BYTE vData) { volatile BYTE Dummy; BYTE vSPIONSave; #if defined(__18CXX) BYTE SPICON1Save; #elif defined(__C30__) WORD SPICON1Save; #else DWORD SPICON1Save; #endif // Save SPI state (clock speed) SPICON1Save = SPIFLASH_SPICON1; vSPIONSave = SPI_ON_BIT; // Configure SPI SPI_ON_BIT = 0; SPIFLASH_SPICON1 = PROPER_SPICON1; SPI_ON_BIT = 1; // If address is a boundary, erase a sector first if((dwWriteAddr & SPI_FLASH_SECTOR_MASK) == 0u) SPIFlashEraseSector(dwWriteAddr); // Enable writing _SendCmd(WREN); // Activate the chip select SPIFLASH_CS_IO = 0; ClearSPIDoneFlag(); // Issue WRITE command with address SPIFLASH_SSPBUF = WRITE; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; SPIFLASH_SSPBUF = ((BYTE*)&dwWriteAddr)[2]; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; SPIFLASH_SSPBUF = ((BYTE*)&dwWriteAddr)[1]; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; SPIFLASH_SSPBUF = ((BYTE*)&dwWriteAddr)[0]; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; // Write the byte SPIFLASH_SSPBUF = vData; WaitForDataByte(); Dummy = SPIFLASH_SSPBUF; dwWriteAddr++; // Deactivate chip select and wait for write to complete SPIFLASH_CS_IO = 1; _WaitWhileBusy(); // Restore SPI state SPI_ON_BIT = 0; SPIFLASH_SPICON1 = SPICON1Save; SPI_ON_BIT = vSPIONSave; }