@@ -269,51 +269,36 @@ void ap3_EEPROM::writeWordToFlash(uint32_t flashLocation, uint32_t dataToWrite)
269269 // Zero out this word(s)
270270 uint8_t byteOffset = (flashLocation % 4 );
271271 uint16_t wordLocation = (flashLocation - FLASH_EEPROM_START) / 4 ;
272+
273+ // Mask in the new data into the array
272274 if (byteOffset == 0 )
273275 {
274- // Easy - reset this word to 1s
275- tempContents[wordLocation] = 0xFFFFFFFF ;
276+ // Easy - update this word with new word
277+ tempContents[wordLocation] = dataToWrite ;
276278 }
277279 else
278280 {
279- // Reset the upper bytes of the first word to 1s
280- tempContents[wordLocation] |= 0xFFFFFFFF << (byteOffset * 8 );
281+ // Clear the upper bytes of the first word to 0s
282+ tempContents[wordLocation] &= ~(0xFFFFFFFF << (byteOffset * 8 ));
283+
284+ // Clear the lower bytes of the second word to 0s
285+ tempContents[wordLocation + 1 ] &= ~(0xFFFFFFFF >> ((4 - byteOffset) * 8 ));
286+
287+ // OR in upper bytes of this word with new data
288+ uint32_t dataToWriteFirstWord = dataToWrite << (byteOffset * 8 );
281289
282- // Reset the lower bytes of the second word to 1s
283- tempContents[wordLocation + 1 ] |= 0xFFFFFFFF >> ((4 - byteOffset) * 8 );
290+ // OR in the lower bytes of the following word with new data
291+ uint32_t dataToWriteSecondWord = dataToWrite >> ((4 - byteOffset) * 8 );
292+
293+ tempContents[wordLocation] |= dataToWriteFirstWord;
294+ tempContents[wordLocation + 1 ] |= dataToWriteSecondWord;
284295 }
285296
286297 // Then we write the contents of the array back
287298 am_hal_flash_program_main (AM_HAL_FLASH_PROGRAM_KEY,
288299 tempContents,
289300 (uint32_t *)FLASH_EEPROM_START,
290301 FLASH_EEPROM_SIZE);
291-
292- if (byteOffset == 0 )
293- {
294- // Easy - update this word with new word
295- am_hal_flash_reprogram_ui32 (AM_HAL_FLASH_PROGRAM_KEY,
296- dataToWrite,
297- (uint32_t *)flashLocation);
298- }
299- else
300- {
301- // Update the upper bytes of this word with new data
302- uint32_t dataToWriteFirstWord = dataToWrite << (byteOffset * 8 );
303- dataToWriteFirstWord |= 0xFFFFFFFF >> ((4 - byteOffset) * 8 );
304-
305- // Update the lower bytes of the following word with new data
306- uint32_t dataToWriteSecondWord = dataToWrite >> ((4 - byteOffset) * 8 );
307- dataToWriteSecondWord |= 0xFFFFFFFF << (byteOffset * 8 );
308-
309- am_hal_flash_reprogram_ui32 (AM_HAL_FLASH_PROGRAM_KEY,
310- dataToWriteFirstWord,
311- (uint32_t *)(flashLocation - byteOffset));
312-
313- am_hal_flash_reprogram_ui32 (AM_HAL_FLASH_PROGRAM_KEY,
314- dataToWriteSecondWord,
315- (uint32_t *)(flashLocation + (4 - byteOffset)));
316- }
317302}
318303
319304ap3_EEPROM EEPROM;
0 commit comments