-
Notifications
You must be signed in to change notification settings - Fork 1.3k
write and update have been modified #615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -212,56 +212,71 @@ uint16 EEPROMClass::EE_VerifyPageFullWriteVariable(uint16 Address, uint16 Data) | |
FLASH_Status FlashStatus; | ||
uint32 idx, pageBase, pageEnd, newPage; | ||
uint16 count; | ||
|
||
boolean found = false; | ||
// Get valid Page for write operation | ||
|
||
pageBase = EE_FindValidPage(); | ||
if (pageBase == 0) | ||
return EEPROM_NO_VALID_PAGE; | ||
|
||
// Get the valid Page end Address | ||
pageEnd = pageBase + PageSize; // Set end of page | ||
|
||
for (idx = pageEnd - 2; idx > pageBase; idx -= 4) | ||
|
||
//FLASH_Unlock(); | ||
|
||
for (idx = pageEnd - 2; idx >= pageBase; idx -= 4) | ||
{ | ||
if ((*(__IO uint16*)idx) == Address) // Find last value for address | ||
{ | ||
found = false; | ||
|
||
count = (*(__IO uint16*)(idx - 2)); // Read last data | ||
|
||
if (count == Data) | ||
return EEPROM_OK; | ||
if (count == 0xFFFF) | ||
|
||
if (count == 0xFFFF || Data < count) | ||
{ | ||
FlashStatus = FLASH_ProgramHalfWord(idx - 2, Data); // Set variable data | ||
if (FlashStatus == FLASH_COMPLETE) | ||
return EEPROM_OK; | ||
|
||
FlashStatus = FLASH_ProgramHalfWord(idx - 2, Data); // Set variable data | ||
if (FlashStatus == FLASH_COMPLETE) | ||
return EEPROM_OK; | ||
} | ||
|
||
break; | ||
} else { | ||
found = true; | ||
|
||
} | ||
} | ||
|
||
// Check each active page address starting from begining | ||
for (idx = pageBase + 4; idx < pageEnd; idx += 4) | ||
if ((*(__IO uint32*)idx) == 0xFFFFFFFF) // Verify if element | ||
{ // contents are 0xFFFFFFFF | ||
FlashStatus = FLASH_ProgramHalfWord(idx, Data); // Set variable data | ||
if (FlashStatus != FLASH_COMPLETE) | ||
return FlashStatus; | ||
FlashStatus = FLASH_ProgramHalfWord(idx + 2, Address); // Set variable virtual address | ||
if (FlashStatus != FLASH_COMPLETE) | ||
return FlashStatus; | ||
return EEPROM_OK; | ||
if(found){ | ||
// Check each active page address starting from begining | ||
for (idx = pageBase + 4; idx < pageEnd; idx += 4){ | ||
if ((*(__IO uint32*)idx) == 0xFFFFFFFF) // Verify if element | ||
{ // contents are 0xFFFFFFFF | ||
FlashStatus = FLASH_ProgramHalfWord(idx, Data); // Set variable data | ||
if (FlashStatus != FLASH_COMPLETE) | ||
return FlashStatus; | ||
FlashStatus = FLASH_ProgramHalfWord(idx + 2, Address); // Set variable virtual address | ||
if (FlashStatus != FLASH_COMPLETE) | ||
return FlashStatus; | ||
return EEPROM_OK; | ||
Comment on lines
+260
to
+263
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the indentation of these lines is not correct, please check and correct. |
||
} | ||
} | ||
|
||
|
||
|
||
// Empty slot not found, need page transfer | ||
// Calculate unique variables in page | ||
count = EE_GetVariablesCount(pageBase, Address) + 1; | ||
if (count >= (PageSize / 4 - 1)) | ||
return EEPROM_OUT_SIZE; | ||
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this added here? the "if" statement doesn't have the opening bracket |
||
|
||
if (pageBase == PageBase1) | ||
newPage = PageBase0; // New page address where variable will be moved to | ||
else | ||
newPage = PageBase1; | ||
|
||
// Set the new Page status to RECEIVE_DATA status | ||
FlashStatus = FLASH_ProgramHalfWord(newPage, EEPROM_RECEIVE_DATA); | ||
if (FlashStatus != FLASH_COMPLETE) | ||
|
@@ -277,6 +292,7 @@ uint16 EEPROMClass::EE_VerifyPageFullWriteVariable(uint16 Address, uint16 Data) | |
return FlashStatus; | ||
|
||
return EE_PageTransfer(newPage, pageBase, Address); | ||
|
||
} | ||
|
||
EEPROMClass::EEPROMClass(void) | ||
|
@@ -521,28 +537,6 @@ uint16 EEPROMClass::write(uint16 Address, uint16 Data) | |
return status; | ||
} | ||
|
||
/** | ||
* @brief Writes/upadtes variable data in EEPROM. | ||
The value is written only if differs from the one already saved at the same address. | ||
* @param VirtAddress: Variable virtual address | ||
* @param Data: 16 bit data to be written | ||
* @retval Success or error status: | ||
* - EEPROM_SAME_VALUE: If new Data matches existing EEPROM Data | ||
* - FLASH_COMPLETE: on success | ||
* - EEPROM_BAD_ADDRESS: if address = 0xFFFF | ||
* - EEPROM_PAGE_FULL: if valid page is full | ||
* - EEPROM_NO_VALID_PAGE: if no valid page was found | ||
* - EEPROM_OUT_SIZE: if no empty EEPROM variables | ||
* - Flash error code: on write Flash error | ||
*/ | ||
uint16 EEPROMClass::update(uint16 Address, uint16 Data) | ||
{ | ||
if (read(Address) == Data) | ||
return EEPROM_SAME_VALUE; | ||
else | ||
return write(Address, Data); | ||
} | ||
|
||
Comment on lines
-538
to
-545
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this function removed? |
||
/** | ||
* @brief Return number of variable | ||
* @retval Number of variables | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is incorrect, you can't reprogram 1s in a halfword of flash, only 0s, so comparing Data < count to decide whether the word can be rewritten is invalid and will lead to corruption.
Either leave is as before (comparing to 0xFFFF) or compare to 0 to make sure the new data has all bits 0.
As per the flash programming manual:
So the only valid cases to write to a flash halfword are: The halfword is in erased state (0xFFFF) OR the new data is all 0 (0x0000)
if (count == 0xFFFF || Data = 0)