micro_climate/App/Src/inflash.c

127 lines
3.6 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"
config_info g_stConfigInfo={
.flag_head = FLAG_SAVE_INFLASH_HEAD,
.addr = 0x30, /* Ĭ<>ϵ<EFBFBD>ַ */
.uart_baud = 9600, /* Ĭ<>ϲ<EFBFBD><CFB2><EFBFBD><EFBFBD><EFBFBD> */
.speed_average_time = 10, /* Ĭ<><C4AC>10<31><30><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
2024-07-09 08:58:37 +00:00
.temp_hum_update_time = 10, /* <20><>ʪ<EFBFBD>ȸ<EFBFBD><C8B8><EFBFBD>ʱ<EFBFBD><CAB1> */
.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_1;
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++;
}
}
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
* @param
* @retval
*/
void save_config_info(config_info save_config_info)
{
HAL_FLASH_Unlock();
InFlash_Write(STM32_INFLASH_SAVE_ADDR_BEGIN, (u_int64_t*)&save_config_info, (sizeof(save_config_info)+7)/8);
2024-07-05 03:52:43 +00:00
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 <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
* @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));
// 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)){
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);
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;
}
/**
* @brief ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
* @param
* @retval
*/
void delete_config_info()
{
config_info delete_config_info = {0};
HAL_FLASH_Unlock();
InFlash_Write(STM32_INFLASH_SAVE_ADDR_BEGIN, (u_int64_t*)&delete_config_info, (sizeof(delete_config_info)+7)/8);
InFlash_Erase_Pages(STM32_INFLASH_SAVE_PAGE, 1);
InFlash_Write(STM32_INFLASH_SAVE_ADDR_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