写一份基于STM32G0 系列芯片的 Flash 读写函数
时间: 2024-05-01 16:18:19 浏览: 109
好的,请稍等,我在查找资料。
以下是基于STM32G0系列芯片的Flash读写函数:
```c
#include "stm32g0xx_hal.h"
void WriteFlash(uint32_t address, uint32_t data) {
HAL_FLASH_Unlock(); // Unlock the Flash memory
FLASH_EraseInitTypeDef eraseInitStruct;
uint32_t pageError;
uint32_t pageAddress = address & ~(FLASH_PAGE_SIZE - 1); // Get the start address of the Flash page
eraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
eraseInitStruct.PageAddress = pageAddress;
eraseInitStruct.NbPages = 1;
HAL_FLASHEx_Erase(&eraseInitStruct, &pageError); // Erase the Flash page
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data); // Program the data into the Flash memory
HAL_FLASH_Lock(); // Lock the Flash memory
}
uint32_t ReadFlash(uint32_t address) {
return (*(__IO uint32_t *)(address)); // Read the data from the Flash memory
}
```
这是一个基本的Flash读写函数,WriteFlash函数可以将一个32位的数据写入指定地址的Flash中,ReadFlash函数可以从指定地址的Flash中读取一段32位的数据。
注意:具体使用时需要根据自己的芯片和Flash布局进行调整。
阅读全文
相关推荐















