STM32 flash擦除读写
记录一下flash的擦除和读写操作,第一次玩嵌入式的板子
void flash_thread_entry(void *parameter)
{
uint16_t my_add = 0x0011;
uint32_t SectorError = 0;
FLASH_EraseInitTypeDef My_Flash;
HAL_FLASH_Unlock();
My_Flash.TypeErase = FLASH_TYPEERASE_SECTORS;
My_Flash.VoltageRange = FLASH_VOLTAGE_RANGE_3;
My_Flash.Sector = 5;
My_Flash.NbSectors = 6;
if (HAL_FLASHEx_Erase(&My_Flash, &SectorError) != HAL_OK)
{
rt_kprintf("Erase error\r\n");
}
uint16_t Write_Flash_Data = my_add;
uint32_t addr = ((uint32_t)0x08020000);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, addr, Write_Flash_Data);
HAL_FLASH_Lock();
rt_kprintf("at address:0x%x, read value:0x%x\r\n", addr, *(__IO uint8_t*)addr);
rt_thread_mdelay(500);
for( ; ; ) {
rt_thread_mdelay(500);
}
}
环境是用的HAL库,STM32F2的板子
|