#include "inflash.h" #include "stm32l4xx_hal.h" #include "pdebug.h" #include "assertions.h" config_info g_stConfigInfo={ .flag_head = FLAG_SAVE_INFLASH_HEAD, .addr = 0x30, /* 默认地址 */ .uart_baud = 9600, /* 默认波特率 */ .speed_average_time = 10, /* 默认10分钟平均风速 */ .temp_hum_update_time = 10, /* 温湿度更新时间 */ .flag_end = FLAG_SAVE_INFLASH_END, }; /** * @brief 擦除多页flash * @param * @retval */ void InFlash_Erase_Pages(u_int16_t PageBeginNum, u_int16_t NbPages) { FLASH_EraseInitTypeDef EraseInitStruct; u_int32_t PAGEError = 0; EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; EraseInitStruct.Page = PageBeginNum; EraseInitStruct.NbPages = NbPages; if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) { log_error("inflash erase error!"); } } /** * @brief 写数据 * @param * @retval */ void InFlash_Write(u_int32_t addr, u_int64_t *pBuffer,u_int32_t NumToWrite) { int i = 0; while( i < NumToWrite){ if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, addr, pBuffer[i]) != HAL_OK){ log_error("inflash write error!"); return; }else{ addr += 8; i++; } } } /** * @brief 存入配置信息 * @param * @retval */ void save_config_info(config_info save_config_info) { HAL_FLASH_Unlock(); InFlash_Erase_Pages(STM32_INFLASH_SAVE_PAGE, 1); InFlash_Write(STM32_INFLASH_SAVE_ADDR_BEGIN, (u_int64_t*)&save_config_info, (sizeof(save_config_info)+7)/8); HAL_FLASH_Lock(); } /** * @brief 读取配置信息 * @param * @retval */ BOOL read_config_info() { config_info temp_config_info={0x00}; memcpy((u_int8_t*)&temp_config_info,(u_int8_t*) STM32_INFLASH_SAVE_ADDR_BEGIN, sizeof(config_info)); if((temp_config_info.flag_head == FLAG_SAVE_INFLASH_HEAD) && (temp_config_info.flag_end == FLAG_SAVE_INFLASH_END)){ memcpy((u_int8_t*)&g_stConfigInfo, (u_int8_t*)&temp_config_info, sizeof(config_info)); term_printf("Addr:%d, speed_average_time:%dmin\r\n", g_stConfigInfo.addr, g_stConfigInfo.speed_average_time); return TRUE; } term_printf("!get config info from inflash error,use default parameter.\r\n"); term_printf("Addr:%d, speed_average_time:%dmin\r\n", g_stConfigInfo.addr, g_stConfigInfo.speed_average_time); return FALSE; } #if 0 /** * @brief 测试 * @param * @retval */ u_int8_t buff[100]={0x00}; void TEST_config_info() { g_stConfigInfo.x_angle_adjust = 2; g_stConfigInfo.y_angle_adjust =3; save_config_info(g_stConfigInfo); memcpy(buff,(u_int8_t*) STM32_INFLASH_SAVE_ADDR_BEGIN, sizeof(g_stConfigInfo)); } #endif