STM32F1: Fix SD card persistent store API (#11090)

2.0.x
Alexander Amelkin 7 years ago committed by Scott Lahteine
parent 8d8f257384
commit ffdbc1f42c

@ -42,10 +42,12 @@
namespace HAL { namespace HAL {
namespace PersistentStore { namespace PersistentStore {
// Store settings in the last two pages namespace {
// Flash pages must be erased before writing, so keep track. // Store settings in the last two pages
bool firstWrite = false; // Flash pages must be erased before writing, so keep track.
uint32_t pageBase = EEPROM_START_ADDRESS; bool firstWrite = false;
uint32_t pageBase = EEPROM_START_ADDRESS;
}
bool access_start() { bool access_start() {
firstWrite = true; firstWrite = true;
@ -64,9 +66,9 @@ bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
if (firstWrite) { if (firstWrite) {
FLASH_Unlock(); FLASH_Unlock();
status = FLASH_ErasePage(EEPROM_PAGE0_BASE); status = FLASH_ErasePage(EEPROM_PAGE0_BASE);
if (status != FLASH_COMPLETE) return false; if (status != FLASH_COMPLETE) return true;
status = FLASH_ErasePage(EEPROM_PAGE1_BASE); status = FLASH_ErasePage(EEPROM_PAGE1_BASE);
if (status != FLASH_COMPLETE) return false; if (status != FLASH_COMPLETE) return true;
firstWrite = false; firstWrite = false;
} }
@ -76,7 +78,7 @@ bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
uint16_t* wordBuffer = (uint16_t *)value; uint16_t* wordBuffer = (uint16_t *)value;
while (wordsToWrite) { while (wordsToWrite) {
status = FLASH_ProgramHalfWord(pageBase + pos + (i * 2), wordBuffer[i]); status = FLASH_ProgramHalfWord(pageBase + pos + (i * 2), wordBuffer[i]);
if (status != FLASH_COMPLETE) return false; if (status != FLASH_COMPLETE) return true;
wordsToWrite--; wordsToWrite--;
i++; i++;
} }
@ -85,15 +87,15 @@ bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
if (size & 1) { if (size & 1) {
uint16_t temp = value[size - 1]; uint16_t temp = value[size - 1];
status = FLASH_ProgramHalfWord(pageBase + pos + i, temp); status = FLASH_ProgramHalfWord(pageBase + pos + i, temp);
if (status != FLASH_COMPLETE) return false; if (status != FLASH_COMPLETE) return true;
} }
crc16(crc, value, size); crc16(crc, value, size);
pos += ((size + 1) & ~1); pos += ((size + 1) & ~1);
return true; return false;
} }
void read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const bool writing/*=true*/) { bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const bool writing/*=true*/) {
for (uint16_t i = 0; i < size; i++) { for (uint16_t i = 0; i < size; i++) {
byte* accessPoint = (byte*)(pageBase + pos + i); byte* accessPoint = (byte*)(pageBase + pos + i);
uint8_t c = *accessPoint; uint8_t c = *accessPoint;
@ -101,6 +103,7 @@ void read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const boo
crc16(crc, &c, 1); crc16(crc, &c, 1);
} }
pos += ((size + 1) & ~1); pos += ((size + 1) & ~1);
return false;
} }
} // PersistentStore } // PersistentStore

Loading…
Cancel
Save