@@ -269,51 +269,36 @@ void ap3_EEPROM::writeWordToFlash(uint32_t flashLocation, uint32_t dataToWrite)
269
269
// Zero out this word(s)
270
270
uint8_t byteOffset = (flashLocation % 4 );
271
271
uint16_t wordLocation = (flashLocation - FLASH_EEPROM_START) / 4 ;
272
+
273
+ // Mask in the new data into the array
272
274
if (byteOffset == 0 )
273
275
{
274
- // Easy - reset this word to 1s
275
- tempContents[wordLocation] = 0xFFFFFFFF ;
276
+ // Easy - update this word with new word
277
+ tempContents[wordLocation] = dataToWrite ;
276
278
}
277
279
else
278
280
{
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 );
281
289
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;
284
295
}
285
296
286
297
// Then we write the contents of the array back
287
298
am_hal_flash_program_main (AM_HAL_FLASH_PROGRAM_KEY,
288
299
tempContents,
289
300
(uint32_t *)FLASH_EEPROM_START,
290
301
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
- }
317
302
}
318
303
319
304
ap3_EEPROM EEPROM;
0 commit comments