2323 Page erase takes 15ms
2424 Writing a byte takes 19ms
2525 Writing a float across two words takes 19ms
26- Update (no write) takes 1ms
26+ Update (no write) takes ~ 1ms
2727
2828 Development environment specifics:
2929 Arduino IDE 1.8.x
@@ -62,9 +62,10 @@ void EEPROMClass::erase()
6262}
6363
6464// 1) Make copy of current flash contents into SRAM
65- // 2) Record user data into SRAM. Check if new data is different from flash.
66- // 3) Erase flash page (8k)
67- // 4) Write SRAM back into flash
65+ // 2) Record user data into SRAM.
66+ // 3) Check if new data is different from flash.
67+ // 4) Erase flash page (8k)
68+ // 5) Write SRAM back into flash
6869void writeBlockToEEPROM (uint16_t eepromLocation, const uint8_t *dataToWrite, uint16_t blockSize)
6970{
7071 // Error check
@@ -73,8 +74,14 @@ void writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dataToWrite, uin
7374 blockSize = AP3_FLASH_EEPROM_SIZE - eepromLocation;
7475 }
7576
76- // First we have to read the contents of current "EEPROM" to SRAM, one byte at a time
77- uint8_t eepromContents[AP3_FLASH_EEPROM_SIZE];
77+ // Read the contents of current "EEPROM" to SRAM
78+ // Flash is written in 32-bit words but user passes in array of bytes
79+ // Create an array of 32-bit words but reference it a byte at a time
80+ uint32_t flashContent[AP3_FLASH_EEPROM_SIZE / 4 ];
81+
82+ // We can't read 32bits at a time because the way flash is oriented (little endian)
83+ // So we read a byte at a time
84+ uint8_t *eepromContents = (uint8_t *)flashContent;
7885 for (uint16_t x = 0 ; x < AP3_FLASH_EEPROM_SIZE; x++)
7986 {
8087 eepromContents[x] = *(uint8_t *)(AP3_FLASH_EEPROM_START + x);
@@ -86,8 +93,8 @@ void writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dataToWrite, uin
8693 eepromContents[eepromLocation + x] = dataToWrite[x];
8794 }
8895
89- // Only update flash with new data.
90- // Run a check here to see if the new data is the same as what's in flash. If it's the same , don't erase flash.
96+ // Run a check here to see if the new data is the same as what's in flash. If it's the same,
97+ // just return , don't erase flash.
9198 bool theSame = true ;
9299 for (uint16_t x = 0 ; x < AP3_FLASH_EEPROM_SIZE; x++)
93100 {
@@ -105,19 +112,6 @@ void writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dataToWrite, uin
105112 AM_HAL_FLASH_ADDR2INST (AP3_FLASH_EEPROM_START + eepromLocation),
106113 AM_HAL_FLASH_ADDR2PAGE (AP3_FLASH_EEPROM_START + eepromLocation));
107114
108- // Flash is written in 32-bit words so we split the byte array into 4 byte chunks
109- uint32_t flashContent[AP3_FLASH_EEPROM_SIZE / 4 ];
110- uint16_t spot = 0 ;
111- for (uint16_t x = 0 ; x < AP3_FLASH_EEPROM_SIZE; x += 4 )
112- {
113- flashContent[spot] = (uint32_t )eepromContents[x + 3 ] << (8 * 3 );
114- flashContent[spot] |= (uint32_t )eepromContents[x + 2 ] << (8 * 2 );
115- flashContent[spot] |= (uint32_t )eepromContents[x + 1 ] << (8 * 1 );
116- flashContent[spot] |= (uint32_t )eepromContents[x + 0 ] << (8 * 0 );
117-
118- spot++;
119- }
120-
121115 // Then we write the contents of the array back
122116 am_hal_flash_program_main (AM_HAL_FLASH_PROGRAM_KEY,
123117 flashContent,
0 commit comments