micro_climate/App/Src/inflash.c

206 lines
6.9 KiB
C
Raw Normal View History

2024-07-05 03:52:43 +00:00
#include "inflash.h"
#include "stm32l4xx_hal.h"
#include "pdebug.h"
#include "assertions.h"
2024-11-19 09:56:39 +00:00
/* Ĭ<>ϲ<EFBFBD><CFB2><EFBFBD> */
2024-11-27 10:05:09 +00:00
usr_config_info g_usrConfigInfo={
.flag_head = FLAG_SAVE_INFLASH_HEAD,
2024-11-27 10:05:09 +00:00
.addr = 0x30, /* Ĭ<>ϵ<EFBFBD>ַ */
.uart_baud = 9600, /* Ĭ<>ϲ<EFBFBD><CFB2><EFBFBD><EFBFBD><EFBFBD> */
.speed_average_time = 600, /* ƽ<><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC>10<31><30><EFBFBD>Ӹ<EFBFBD><D3B8><EFBFBD> */
.temp_hum_update_time = 600, /* <20><>ʪ<EFBFBD>ȸ<EFBFBD><C8B8><EFBFBD>ʱ<EFBFBD><CAB1> */
2024-11-27 10:05:09 +00:00
.flag_end = FLAG_SAVE_INFLASH_END,
};
/* Ĭ<>ϲ<EFBFBD><CFB2><EFBFBD> */
factory_config_info g_stConfigInfo={
.flag_head = FLAG_SAVE_INFLASH_HEAD,
// .transducer_cfg_1R5 = 15, /* <20><><EFBFBD><EFBFBD><EEBBBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
// .transducer_cfg_4R5 = 45, /* <20><><EFBFBD><EFBFBD><EEBBBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
// .transducer_cfg_8R0 = 80, /* <20><><EFBFBD><EFBFBD><EEBBBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
.transducer_cfg_1R5 = 10, /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
.transducer_cfg_4R5 = 35, /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
.transducer_cfg_8R0 = 65, /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
.RSSI_range = 20, /* RSSI<53><49><EFBFBD><EFBFBD>0.2<EFBFBD><EFBFBD>Ч */
.transducer_distace = 57560, /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̽ͷ<CCBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
2024-11-19 09:56:39 +00:00
.linear_point_1_x = 0, /* <20><><EFBFBD>Բ<EFBFBD>ֵ<EFBFBD><D6B5>1 */
.linear_point_1_y = 0, /* <20><><EFBFBD>Բ<EFBFBD>ֵ<EFBFBD><D6B5>1 */
.linear_point_2_x = 0, /* <20><><EFBFBD>Բ<EFBFBD>ֵ<EFBFBD><D6B5>2 */
.linear_point_2_y = 0, /* <20><><EFBFBD>Բ<EFBFBD>ֵ<EFBFBD><D6B5>2 */
.linear_point_3_x = 0, /* <20><><EFBFBD>Բ<EFBFBD>ֵ<EFBFBD><D6B5>3 */
.linear_point_3_y = 0, /* <20><><EFBFBD>Բ<EFBFBD>ֵ<EFBFBD><D6B5>3 */
.linear_point_4_x = 0, /* <20><><EFBFBD>Բ<EFBFBD>ֵ<EFBFBD><D6B5>4 */
.linear_point_4_y = 0, /* <20><><EFBFBD>Բ<EFBFBD>ֵ<EFBFBD><D6B5>4 */
.linear_point_5_x = 0, /* <20><><EFBFBD>Բ<EFBFBD>ֵ<EFBFBD><D6B5>5 */
.linear_point_5_y = 0, /* <20><><EFBFBD>Բ<EFBFBD>ֵ<EFBFBD><D6B5>5 */
.linear_enable = 0, /* <20><><EFBFBD>Բ<EFBFBD>ֵʹ<D6B5><CAB9> */
.flag_end = FLAG_SAVE_INFLASH_END,
2024-07-05 03:52:43 +00:00
};
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҳflash
* @param
* @retval
*/
void InFlash_Erase_Pages(u_int16_t PageBeginNum, u_int16_t NbPages)
{
FLASH_EraseInitTypeDef EraseInitStruct;
u_int32_t PAGEError = 0;
EraseInitStruct.Banks = FLASH_BANK_2;
2024-07-05 03:52:43 +00:00
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 д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @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!");
2024-07-05 03:52:43 +00:00
return;
}else{
addr += 8;
i++;
}
}
}
/**
2024-11-27 10:05:09 +00:00
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
2024-07-05 03:52:43 +00:00
* @param
* @retval
*/
2024-11-27 10:05:09 +00:00
void save_usr_config_info(usr_config_info save_config_info)
2024-07-05 03:52:43 +00:00
{
HAL_FLASH_Unlock();
2024-11-27 10:05:09 +00:00
InFlash_Write(STM32_INFLASH_SAVE_ADDR_1_BEGIN, (u_int64_t*)&save_config_info, (sizeof(save_config_info)+7)/8);
InFlash_Erase_Pages(STM32_INFLASH_SAVE_PAGE_1, 1);
InFlash_Write(STM32_INFLASH_SAVE_ADDR_1_BEGIN, (u_int64_t*)&save_config_info, (sizeof(save_config_info)+7)/8);
2024-07-05 03:52:43 +00:00
HAL_FLASH_Lock();
}
/**
2024-11-27 10:05:09 +00:00
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
2024-07-05 03:52:43 +00:00
* @param
* @retval
*/
2024-11-27 10:05:09 +00:00
void save_factory_config_info(factory_config_info save_config_info)
{
HAL_FLASH_Unlock();
InFlash_Write(STM32_INFLASH_SAVE_ADDR_2_BEGIN, (u_int64_t*)&save_config_info, (sizeof(save_config_info)+7)/8);
InFlash_Erase_Pages(STM32_INFLASH_SAVE_PAGE_2, 1);
InFlash_Write(STM32_INFLASH_SAVE_ADDR_2_BEGIN, (u_int64_t*)&save_config_info, (sizeof(save_config_info)+7)/8);
HAL_FLASH_Lock();
}
/**
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD>û<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
* @param
* @retval
*/
BOOL read_usr_config_info()
{
usr_config_info temp_config_info={0x00};
memcpy((u_int8_t*)&temp_config_info,(u_int8_t*) STM32_INFLASH_SAVE_ADDR_1_BEGIN, sizeof(usr_config_info));
// term_printf("%d", temp_config_info.addr);
if((temp_config_info.flag_head == FLAG_SAVE_INFLASH_HEAD) && (temp_config_info.flag_end == FLAG_SAVE_INFLASH_END)){
memcpy((u_int8_t*)&g_usrConfigInfo, (u_int8_t*)&temp_config_info, sizeof(usr_config_info));
// term_printf("Addr:%d, speed_average_time:%dmin\r\n", g_usrConfigInfo.addr, g_usrConfigInfo.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_usrConfigInfo.addr, g_usrConfigInfo.speed_average_time);
return FALSE;
}
/**
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
* @param
* @retval
*/
BOOL read_factory_config_info()
2024-07-05 03:52:43 +00:00
{
2024-11-27 10:05:09 +00:00
factory_config_info temp_config_info={0x00};
2024-07-05 03:52:43 +00:00
2024-11-27 10:05:09 +00:00
memcpy((u_int8_t*)&temp_config_info,(u_int8_t*) STM32_INFLASH_SAVE_ADDR_2_BEGIN, sizeof(factory_config_info));
// term_printf("%d", temp_config_info.addr);
2024-07-05 03:52:43 +00:00
if((temp_config_info.flag_head == FLAG_SAVE_INFLASH_HEAD) && (temp_config_info.flag_end == FLAG_SAVE_INFLASH_END)){
2024-11-27 10:05:09 +00:00
memcpy((u_int8_t*)&g_stConfigInfo, (u_int8_t*)&temp_config_info, sizeof(factory_config_info));
// term_printf("Addr:%d, speed_average_time:%dmin\r\n", g_stConfigInfo.addr, g_stConfigInfo.speed_average_time);
2024-07-05 03:52:43 +00:00
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);
2024-07-05 03:52:43 +00:00
return FALSE;
}
/**
2024-11-27 10:05:09 +00:00
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
* @param
* @retval
*/
BOOL read_config_info()
{
if(read_usr_config_info&&read_factory_config_info)
{
return TRUE;
}
return FALSE;
}
/**
* @brief ɾ<EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
* @param
* @retval
*/
void delete_config_info()
{
2024-11-27 10:05:09 +00:00
usr_config_info delete_config_info = {0};
HAL_FLASH_Unlock();
2024-11-27 10:05:09 +00:00
InFlash_Write(STM32_INFLASH_SAVE_ADDR_1_BEGIN, (u_int64_t*)&delete_config_info, (sizeof(delete_config_info)+7)/8);
InFlash_Erase_Pages(STM32_INFLASH_SAVE_PAGE_1, 1);
InFlash_Write(STM32_INFLASH_SAVE_ADDR_1_BEGIN, (u_int64_t*)&delete_config_info, (sizeof(delete_config_info)+7)/8);
HAL_FLASH_Lock();
}
2024-07-05 03:52:43 +00:00
#if 0
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @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