26 lines
937 B
C
26 lines
937 B
C
#include "config.h"
|
|
#include "ch32v30x_flash.h"
|
|
|
|
PersistentData_t persistent;
|
|
|
|
void loadConfig() {
|
|
memcpy (&persistent, FLASH_USER_PAGE_ADDR, sizeof (persistent));
|
|
uint32_t crcSum = *((uint32_t *)(((uint8_t *)&persistent) + (sizeof (persistent) - 2)));
|
|
memset ((((uint8_t *)&persistent) + (sizeof (persistent) - sizeof(crcSum))), 0, 4);
|
|
CRC_ResetDR();
|
|
uint32_t currentSum = CRC_CalcBlockCRC ((uint32_t *)&persistent, sizeof (persistent) - 2);
|
|
|
|
if (currentSum != crcSum) {
|
|
memset (&persistent, 0, sizeof (persistent));
|
|
}
|
|
}
|
|
|
|
void saveConfig() {
|
|
CRC_ResetDR();
|
|
uint32_t currentSum = CRC_CalcBlockCRC ((uint32_t *)&persistent, sizeof (persistent) - 2);
|
|
memcpy ((((uint8_t *)&persistent) + (sizeof (persistent) - sizeof(currentSum))), (uint8_t *)currentSum, 4);
|
|
FLASH_Unlock();
|
|
FLASH_ErasePage_Fast (1919);
|
|
FLASH_ProgramPage_Fast (1919, (uint32_t *)&persistent);
|
|
FLASH_Lock();
|
|
} |