}
// calculate which page is affected (Pagenum1/Pagenum2...PagenumN)
- page = (FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address)) & 0x00000FFF;
-
- if (page % FEE_PAGE_SIZE) page = page + FEE_PAGE_SIZE;
- page = (page / FEE_PAGE_SIZE) - 1;
+ page = FEE_ADDR_OFFSET(Address) / FEE_PAGE_SIZE;
// if current data is 0xFF, the byte is empty, just overwrite with the new one
if ((*(__IO uint16_t*)(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address))) == FEE_EMPTY_WORD) {
FlashStatus = FLASH_ProgramHalfWord(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address), (uint16_t)(0x00FF & DataByte));
- }
- else {
+ } else {
// Copy Page to a buffer
memcpy(DataBuf, (uint8_t*)FEE_PAGE_BASE_ADDRESS + (page * FEE_PAGE_SIZE), FEE_PAGE_SIZE); // !!! Calculate base address for the desired page
}
// manipulate desired data byte in temp data array if new byte is differ to the current
- DataBuf[FEE_ADDR_OFFSET(Address)] = DataByte;
+ DataBuf[FEE_ADDR_OFFSET(Address) % FEE_PAGE_SIZE] = DataByte;
//Erase Page
- FlashStatus = FLASH_ErasePage(FEE_PAGE_BASE_ADDRESS + page);
+ FlashStatus = FLASH_ErasePage(FEE_PAGE_BASE_ADDRESS + (page * FEE_PAGE_SIZE));
- // Write new data (whole page) to flash if data has beed changed
+ // Write new data (whole page) to flash if data has been changed
for(i = 0; i < (FEE_PAGE_SIZE / 2); i++) {
if ((__IO uint16_t)(0xFF00 | DataBuf[FEE_ADDR_OFFSET(i)]) != 0xFFFF) {
FlashStatus = FLASH_ProgramHalfWord((FEE_PAGE_BASE_ADDRESS + (page * FEE_PAGE_SIZE)) + (i * 2), (uint16_t)(0xFF00 | DataBuf[FEE_ADDR_OFFSET(i)]));
}
}
-
}
return FlashStatus;
}
uint32_t eeprom_read_dword (const uint32_t *Address)
{
const uint16_t p = (const uint32_t) Address;
- return EEPROM_ReadDataByte(p) | (EEPROM_ReadDataByte(p+1) << 8)
+ return EEPROM_ReadDataByte(p) | (EEPROM_ReadDataByte(p+1) << 8)
| (EEPROM_ReadDataByte(p+2) << 16) | (EEPROM_ReadDataByte(p+3) << 24);
}
void eeprom_update_dword (uint32_t *Address, uint32_t Value)
{
uint16_t p = (const uint32_t) Address;
- EEPROM_WriteDataByte(p, (uint8_t) Value);
- EEPROM_WriteDataByte(p+1, (uint8_t) (Value >> 8));
- EEPROM_WriteDataByte(p+2, (uint8_t) (Value >> 16));
- EEPROM_WriteDataByte(p+3, (uint8_t) (Value >> 24));
+ uint32_t existingValue = EEPROM_ReadDataByte(p) | (EEPROM_ReadDataByte(p+1) << 8)
+ | (EEPROM_ReadDataByte(p+2) << 16) | (EEPROM_ReadDataByte(p+3) << 24);
+ if(Value != existingValue){
+ EEPROM_WriteDataByte(p, (uint8_t) Value);
+ EEPROM_WriteDataByte(p+1, (uint8_t) (Value >> 8));
+ EEPROM_WriteDataByte(p+2, (uint8_t) (Value >> 16));
+ EEPROM_WriteDataByte(p+3, (uint8_t) (Value >> 24));
+ }
}
void eeprom_read_block(void *buf, const void *addr, uint32_t len) {