5

Flash Read Write

 2 years ago
source link: https://gist.github.com/tzechienchu/125e3c80c1ee4091e8b04ba1f9935617
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Flash Read Write · GitHub

Instantly share code, notes, and snippets.

Flash Read Write

#include "stm32f0xx_hal.h"

void save_data(uint32_t Address,uint32_t data){

HAL_FLASH_Unlock(); FLASH_EraseInitTypeDef EraseInitStruct; EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; EraseInitStruct.PageAddress = Address; EraseInitStruct.NbPages = 1;

uint32_t PageError; if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK) //Erase the Page Before a Write Operation return HAL_ERROR;

HAL_Delay(50); HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,Address,(uint64_t)data); HAL_Delay(50); HAL_FLASH_Lock();

}

uint32_t read_data(uint32_t Address){

__IO uint32_t read_data = *(__IO uint32_t *)Address; return (uint32_t)read_data; }


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK