23
23
Page erase takes 15ms
24
24
Writing a byte takes 19ms
25
25
Writing a float across two words takes 19ms
26
- Update (no write) takes 1ms
26
+ Update (no write) takes ~ 1ms
27
27
28
28
Development environment specifics:
29
29
Arduino IDE 1.8.x
@@ -62,9 +62,10 @@ void EEPROMClass::erase()
62
62
}
63
63
64
64
// 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
68
69
void writeBlockToEEPROM (uint16_t eepromLocation, const uint8_t *dataToWrite, uint16_t blockSize)
69
70
{
70
71
// Error check
@@ -73,8 +74,14 @@ void writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dataToWrite, uin
73
74
blockSize = AP3_FLASH_EEPROM_SIZE - eepromLocation;
74
75
}
75
76
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;
78
85
for (uint16_t x = 0 ; x < AP3_FLASH_EEPROM_SIZE; x++)
79
86
{
80
87
eepromContents[x] = *(uint8_t *)(AP3_FLASH_EEPROM_START + x);
@@ -86,8 +93,8 @@ void writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dataToWrite, uin
86
93
eepromContents[eepromLocation + x] = dataToWrite[x];
87
94
}
88
95
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.
91
98
bool theSame = true ;
92
99
for (uint16_t x = 0 ; x < AP3_FLASH_EEPROM_SIZE; x++)
93
100
{
@@ -105,19 +112,6 @@ void writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dataToWrite, uin
105
112
AM_HAL_FLASH_ADDR2INST (AP3_FLASH_EEPROM_START + eepromLocation),
106
113
AM_HAL_FLASH_ADDR2PAGE (AP3_FLASH_EEPROM_START + eepromLocation));
107
114
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
-
121
115
// Then we write the contents of the array back
122
116
am_hal_flash_program_main (AM_HAL_FLASH_PROGRAM_KEY,
123
117
flashContent,
0 commit comments