STS_E2/AirQuality/Src/user_flash.c

35 lines
853 B
C

#include "main.h"
#include "stm32f0xx_hal.h"
#include "user_flash.h"
uint32_t Save_Config_Flash_Addr = 0x0801F800;
uint8_t save_config(uint16_t config)
{
uint8_t status = HAL_ERROR;
FLASH_EraseInitTypeDef My_Flash;
HAL_FLASH_Unlock();
My_Flash.TypeErase = FLASH_TYPEERASE_PAGES;
My_Flash.PageAddress = Save_Config_Flash_Addr;
My_Flash.NbPages = 1;
uint32_t PageError = 0;
status = HAL_FLASHEx_Erase(&My_Flash, &PageError);
if(status != HAL_OK)
{
return status;
}
uint16_t Write_Flash_Data = config;
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, Save_Config_Flash_Addr, Write_Flash_Data);
HAL_FLASH_Lock();
return status;
}
uint16_t read_config(void)
{
uint16_t data = *(__IO uint16_t*)( Save_Config_Flash_Addr );
return data;
}