123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- #include "parameters.h"
- #include "global_def.h"
- #include "usart.h"
- #include "stm32l4xx_hal_flash.h"
- #include "stm32l4xx_hal_flash_ex.h"
- #include <string.h>
- #include <stdio.h>
- static uint64_t reflectionBuff[(sizeof(Config_type)>>3)+1];
- Config_type *config = (Config_type *)reflectionBuff;
- static uint32_t GetPage(uint32_t Addr)
- {
- uint32_t page = 0;
-
- if (Addr < (EFLASH_BASE + EFLASH_BANK_SIZE))
- {
- /* Bank 1 */
- page = (Addr - EFLASH_BASE) / EFLASH_PAGE_SIZE;
- }
- else
- {
- /* Bank 2 */
- page = (Addr - (EFLASH_BASE + EFLASH_BANK_SIZE)) / EFLASH_PAGE_SIZE;
- }
-
- return page;
- }
-
- /**
- * @brief Gets the bank of a given address
- * @param Addr: Address of the FLASH Memory
- * @retval The bank of a given address
- */
- static uint32_t GetBank(uint32_t Addr)
- {
- return FLASH_BANK_1;
- }
- static void Factory_reset(void)
- {
- config->magic = CONFIG_MAGIC;
- config->addr = DEFAULT_ADDR;
- config->br_index = BaudRate_9600;
-
-
- //config->App2Size = 0;
- //config->App2Crc = 0;
- //config->IapFlag = 0;
- }
- int Config_Init(void)
- {
- //EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;
- memcpy(reflectionBuff, (void *)CONFIG_ADDRESS, sizeof(Config_type));
- // for(int i=0; i<(sizeof(Config_type)>>3)+1; i++){
- // reflectionBuff[i] = *(__IO uint64_t *)(CONFIG_ADDRESS+i*8);
- // }
-
- if (config->magic != CONFIG_MAGIC)
- {/* initiliaze config */
- memset(reflectionBuff, 0xFF, sizeof(Config_type));
- //config->magic = CONFIG_MAGIC;
-
-
- Factory_reset();
-
- //
- config->IapFlag = Startup_Normal;
- config->hw_version = 0x0000;
- config->devicetype = 0x0000;
- config->deviceid = 0x00;
-
- SaveConfig();
-
- }
-
- printf("config->IapFlag: %x \r\n", config->IapFlag);
- return 0;
- }
- int SaveConfig(void)
- {
- HAL_StatusTypeDef ret = HAL_OK;
- FLASH_EraseInitTypeDef pEraseInit;
- uint32_t pageError = 0;
-
- HAL_FLASH_Unlock();
-
- pEraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
- pEraseInit.Page = GetPage(CONFIG_ADDRESS);
- pEraseInit.Banks = GetBank(CONFIG_ADDRESS);
- pEraseInit.NbPages = 1;
-
- ret = HAL_FLASHEx_Erase(&pEraseInit, &pageError);
- //if (ret != HAL_OK) return -1;
-
- for(int i=0; i<((sizeof(Config_type)>>3)+1); i++){
- ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, CONFIG_ADDRESS+i*8, reflectionBuff[i]);
- }
-
-
- HAL_FLASH_Lock();
-
- return ret;
- }
- int ResetConfig(void)
- {
- Factory_reset();
- //SaveConfig();
-
- return 0;
- }
|