当前位置:网站首页>STM32内部Flash的操作函数
STM32内部Flash的操作函数
2022-07-15 15:23:00 【夏夜晚风_】
IAP远程更新系列文章目录如下:
1. IAP远程更新机制说明
STM32 操作内部Flash的函数接口
#include "inFlash.h"
#include "stm32f10x_flash.h"
/*! * 功 能: 不检查的写入内部Flash * param1: 起始地址 * param2: 要写入的数据指针 * param3: 要写入的数据的个数 * return: 无返回值 */
void InFlashWriteNoCheck(uint32_t writeAddr, uint16_t *pBuffer, uint16_t numToWrite)
{
uint16_t i;
for(i=0; i<numToWrite; i++)
{
FLASH_ProgramHalfWord(writeAddr, pBuffer[i]); //在指定的地址写入半字
writeAddr += 2; //地址增加2.
}
}
/*! * 功 能: 从指定地址开始写入指定长度的数据 * param1: 起始地址(此地址必须为2的倍数!!) * param2: 要写入的数据指针 * param3: 半字(16位)数 (就是要写入的16位数据的个数.) * return: 成功返回 0 * 失败返回 -1 */
int InFlashWrite(uint32_t writeAddr, uint8_t *pBuffer, uint16_t numToWrite)
{
uint16_t i, temp = 0;
// 先判断地址是否合法
if(writeAddr<INFLASH_START_ADDR || (writeAddr>=(INFLASH_START_ADDR + INFLASH_TOTAL_SIZE * 1024)))
{
return -1;
}
FLASH_Unlock(); // 解锁
FLASH_SetLatency(FLASH_Latency_2); // 因为系统时钟为72M所以要设置两个时钟周期的延时
FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR | FLASH_FLAG_EOP);
// 写入页之前要擦除整页,并判断是否擦除成功
while(FLASH_ErasePage(writeAddr) != FLASH_COMPLETE);
// 写入数据,小端存储,数据的高字节保存在高地址
if(numToWrite == 1)
{
temp |= pBuffer[0];
while(FLASH_ProgramHalfWord(writeAddr, temp) != FLASH_COMPLETE); //判断是否写入成功
}
else
{
for(i=0; i<numToWrite; i++) // 按照半字写入
{
temp = (pBuffer[2*i+1]<<8) | pBuffer[2*i]; //2个字节整合为1个半字
while(FLASH_ProgramHalfWord(writeAddr, temp) != FLASH_COMPLETE); //判断是否写入成功
writeAddr += 2; // 地址要加2,因为每次写入的是2个字节(1个半字)
}
}
FLASH_Lock(); // 上锁
return 0;
}
/*! * 功 能: 读取指定地址的半字(16位数据) * param1: 读地址(此地址必须为2的倍数!!) * return: 读取到的数据 */
uint16_t InFlashReadHalfWord(uint32_t faddr)
{
return *(uint16_t*)faddr;
}
/*! * 功 能: 从指定地址开始读出指定长度的数据 * param1: 起始地址(此地址必须为2的倍数!!) * param2: 要保存的缓冲区的地址 * param3: 要读取的半字(16位)数 (就是要读取的16位数据的个数.) * return: 无返回值 */
void InFlashRead(uint32_t readAddr, uint16_t *pBuffer, uint16_t numToRead)
{
uint16_t i;
for(i = 0; i < numToRead; i++)
{
pBuffer[i] = InFlashReadHalfWord(readAddr); //读取2个字节.
readAddr += 2; //偏移2个字节.
}
}
/*! * 功 能: 测试函数,往内部Flash写入一个字节的数据 * param1: 起始地址 * param2: 要写入的数据 * return: 成功返回0 */
int InFlashWriteChar(uint32_t writeAddr, uint8_t writeData)
{
return InFlashWrite(writeAddr, &writeData, 1); //写入一个字
}
边栏推荐
猜你喜欢

Puge - basic configuration usage of winscp and xshell of Honghu team on the code

Core examination sites for information system project managers (VI) layering of OSI protocol, mapping + real questions

电商平台后台管理系统--->项目前期准备(需求分析、系统设计、环境搭建与配置文件)

12306 ticket grabbing tools shock and attack

Dynamic memory functions and common dynamic memory errors

Experience first! What kind of experience is it to write fluent in the browser?

Information system project managers must recite the core examination points (I) six elements of the national information system

Dynamic programming | 0-1 knapsack problem

H5 cloud image background reading and writing CAD files - Online CAD, web CAD, web browsing and editing CAD

CRMEB Pro v1.4,让用户体验更出彩!
随机推荐
uni-app表单数据不复现
Mobile Robotics (I) fundamentals of Mathematics
Niuke 2021 summer training 4-e-tree XOR
牛客2021暑期训练3-J-Counting Triangles
Niuke 2021 summer training 3-j-counting triangles
电商平台后台管理系统--->操作方法说明
MySQL数据库忘记密码怎么办???
科科过信管】信管论文写作要求-不合格论文
LSM存储模型
Redis03: five common data types of redis
信息系统项目管理师必背核心考点(三)UML的14种图形工具
Dynamic programming | 0-1 knapsack problem
Kekeguo information management] information management paper writing requirements - unqualified papers
动作捕捉协助中国电力科学研究院建立边云协同电力自主巡检系统
The distant savior obeys the objective law (I) -- what can be done to Wangmiao village
Power change in dispute: a limited game for head players
牛客2021暑期训练4-E-Tree Xor
Background management system of e-commerce platform -- > preliminary preparation of the project (demand analysis, system design, environment construction and configuration files)
Realize the side-by-side display of two elements (including the explanation of some flex attributes)
ERROR 1366 (HY000): Incorrect string value: ‘\xE8\xB5\xB5\xE9\x9B\xB7‘ for column ‘s_name‘ at row 1