#include "main.h" #include "stm32f4xx_hal.h" #include "user_flash.h" uint32_t Save_Config_Flash_Addr = 0x0801F800; uint8_t save_config(uint8_t config) { uint8_t status = HAL_ERROR; FLASH_EraseInitTypeDef My_Flash; // __set_PRIMASK(1); 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(); // __set_PRIMASK(0); return status; } uint8_t read_config(void) { uint8_t data = *(__IO uint8_t*)( Save_Config_Flash_Addr ); return data; }