2121 Flash is 0x00 to 0xFF000. EEPROM writes will start at 0xFF000 - 8192 = 0xF2000.
2222
2323 Page erase takes 15ms
24- Writing a byte takes 30ms
25- Writing a float across two words takes 30ms
24+ Writing a byte takes 19ms
25+ Writing a float across two words takes 19ms
2626 Update (no write) takes 1ms
2727
2828 Development environment specifics:
4040#include " EEPROM.h"
4141#include " Arduino.h"
4242
43- // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
44-
4543// Write a byte to a given "EEPROM" location
46- // Automatically masks user's byte into flash without
47- // affecting other bytes in this flash word
4844void write (uint16_t eepromLocation, uint8_t dataToWrite)
4945{
5046 EEPROM.writeBlockToEEPROM (eepromLocation, &dataToWrite, 1 );
@@ -57,17 +53,6 @@ uint8_t read(uint16_t eepromLocation)
5753 return (*(uint8_t *)flashLocation);
5854}
5955
60- // Write a new byte to a given location in "EEROM" only if new data is there
61- void update (uint16_t eepromLocation, uint8_t dataToWrite)
62- {
63- if (read (eepromLocation) != dataToWrite)
64- {
65- write (eepromLocation, dataToWrite);
66- }
67- }
68-
69- // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
70-
7156// Erase 8k page encapsulating the EEPROM section
7257void EEPROMClass::erase ()
7358{
@@ -76,82 +61,10 @@ void EEPROMClass::erase()
7661 AM_HAL_FLASH_ADDR2PAGE (AP3_FLASH_EEPROM_START));
7762}
7863
79- // This is the main helper function
80- // Reprogram a given location with 32-bits
81- // Flash is written to in words at locations that are %4=0
82- // Span words if necessary
8364// 1) Make copy of current flash contents into SRAM
84- // 2) Turn user's requested spot into 0xFFs
65+ // 2) Record user data into SRAM. Check if new data is different from flash.
8566// 3) Erase flash page (8k)
8667// 4) Write SRAM back into flash
87- // 5) Write user's data onto the spot with recently created 0xFFs
88- // Note - this code assumes EEPROM temp space is contained in one page
89- void writeWordToFlash (uint32_t flashLocation, uint32_t dataToWrite)
90- {
91- // Error check
92- if (flashLocation >= AP3_FLASH_EEPROM_START + AP3_FLASH_EEPROM_SIZE)
93- {
94- return ;
95- }
96- if (flashLocation < AP3_FLASH_EEPROM_START)
97- {
98- return ;
99- }
100-
101- // Check to see if location needs updating
102- if (*(uint32_t *)(flashLocation) == dataToWrite)
103- {
104- return ;
105- }
106-
107- // First we have to read the contents of current "EEPROM" to SRAM
108- uint32_t tempContents[AP3_FLASH_EEPROM_SIZE / 4 ];
109- uint16_t spot = 0 ;
110- for (uint16_t x = 0 ; x < AP3_FLASH_EEPROM_SIZE; x += 4 )
111- {
112- tempContents[spot++] = *(uint32_t *)(AP3_FLASH_EEPROM_START + x);
113- }
114-
115- // Then we erase an 8K page
116- am_hal_flash_page_erase (AM_HAL_FLASH_PROGRAM_KEY,
117- AM_HAL_FLASH_ADDR2INST (flashLocation),
118- AM_HAL_FLASH_ADDR2PAGE (flashLocation));
119-
120- // Zero out this word(s)
121- uint8_t byteOffset = (flashLocation % 4 );
122- uint16_t wordLocation = (flashLocation - AP3_FLASH_EEPROM_START) / 4 ;
123-
124- // Mask in the new data into the array
125- if (byteOffset == 0 )
126- {
127- // Easy - update this word with new word
128- tempContents[wordLocation] = dataToWrite;
129- }
130- else
131- {
132- // Clear the upper bytes of the first word to 0s
133- tempContents[wordLocation] &= ~(0xFFFFFFFF << (byteOffset * 8 ));
134-
135- // Clear the lower bytes of the second word to 0s
136- tempContents[wordLocation + 1 ] &= ~(0xFFFFFFFF >> ((4 - byteOffset) * 8 ));
137-
138- // OR in upper bytes of this word with new data
139- uint32_t dataToWriteFirstWord = dataToWrite << (byteOffset * 8 );
140-
141- // OR in the lower bytes of the following word with new data
142- uint32_t dataToWriteSecondWord = dataToWrite >> ((4 - byteOffset) * 8 );
143-
144- tempContents[wordLocation] |= dataToWriteFirstWord;
145- tempContents[wordLocation + 1 ] |= dataToWriteSecondWord;
146- }
147-
148- // Then we write the contents of the array back
149- am_hal_flash_program_main (AM_HAL_FLASH_PROGRAM_KEY,
150- tempContents,
151- (uint32_t *)AP3_FLASH_EEPROM_START,
152- AP3_FLASH_EEPROM_SIZE);
153- }
154-
15568void EEPROMClass::writeBlockToEEPROM (uint16_t eepromLocation, const uint8_t *dataToWrite, uint16_t blockSize)
15669{
15770 // Error check
@@ -168,7 +81,7 @@ void EEPROMClass::writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dat
16881 }
16982
17083 // Write the caller's new data into the byte array
171- for (int x = 0 ; x < blockSize; x++)
84+ for (uint16_t x = 0 ; x < blockSize; x++)
17285 {
17386 eepromContents[eepromLocation + x] = dataToWrite[x];
17487 }
@@ -192,7 +105,7 @@ void EEPROMClass::writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dat
192105 AM_HAL_FLASH_ADDR2INST (AP3_FLASH_EEPROM_START + eepromLocation),
193106 AM_HAL_FLASH_ADDR2PAGE (AP3_FLASH_EEPROM_START + eepromLocation));
194107
195- // Flash is written in 32bit words so we split the byte array into 4 byte chunks
108+ // Flash is written in 32-bit words so we split the byte array into 4 byte chunks
196109 uint32_t flashContent[AP3_FLASH_EEPROM_SIZE / 4 ];
197110 uint16_t spot = 0 ;
198111 for (uint16_t x = 0 ; x < AP3_FLASH_EEPROM_SIZE; x += 4 )
0 commit comments