#include "param.h" #include "W25QXX/w25qxx.h" sysParamDef _gtSystemParam; sysOtpInfoDef _gtOtpInfo; sysParamDef *gptSysParam = &_gtSystemParam; sysOtpInfoDef *gptOtpInfo = &_gtOtpInfo; int loadSysParam(void) { debug("load param"); W25QXX_Read((uint8_t *)gptSysParam, FLASH_PART_PARAM_START, sizeof(sysParamDef)); dump((uint8_t *)(gptSysParam), sizeof(sysParamDef)); return 0; } void saveSysParam(void) { debug("save param"); dump((uint8_t *)(gptSysParam), sizeof(sysParamDef)); W25QXX_Erase_Sector(FLASH_PART_PARAM_START); W25QXX_Page_Program((uint8_t *)(gptSysParam), FLASH_PART_PARAM_START, sizeof(sysParamDef)); } const sysOtpInfoDef *loadOtpInfo(void) { debug("load otp info"); W25QXX_Read((uint8_t *)gptOtpInfo, FLASH_PART_OTP_START, sizeof(sysOtpInfoDef)); dump((uint8_t *)(gptOtpInfo), sizeof(sysOtpInfoDef)); return gptOtpInfo; } int saveOtpInfo(const sysOtpInfoDef *otpInfo) { debug("save otp info"); dump((uint8_t *)(otpInfo), sizeof(sysOtpInfoDef)); if (otpInfo == NULL) { return -1; } loadOtpInfo(); for (int i = 0; i < sizeof(sysOtpInfoDef); i++) { if (((uint8_t *)gptOtpInfo)[i] != 0xFF) { debug("write failed, otp info is not empty"); return -2; } } W25QXX_Page_Program((uint8_t *)(otpInfo), FLASH_PART_OTP_START, sizeof(sysOtpInfoDef)); loadOtpInfo(); if (memcmp((uint8_t *)gptOtpInfo, (uint8_t *)otpInfo, sizeof(sysOtpInfoDef)) != 0) { debug("write failed, otp info is not equal"); return -3; } return 0; } void systemResetFactory(void) { debug("reset factory"); for (uint32_t i = FLASH_PART_PARAM_START; i < FLASH_PART_PARAM_END; i += W25QXX_SECTOR_SIZE) { W25QXX_Erase_Sector(i); } for (uint32_t i = FLASH_PART_OTP_START; i < FLASH_PART_OTP_END; i += W25QXX_SECTOR_SIZE) { W25QXX_Erase_Sector(i); } }