STM32 内部flash的读写程序
来源:互联网 发布:suse网络不通 编辑:程序博客网 时间:2024/06/05 11:42
/* Base address of the Flash sectors */#define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbytes */#define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbytes */#define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbytes */#define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base @ of Sector 3, 16 Kbytes */#define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base @ of Sector 4, 64 Kbytes *///stm32f205rbt6 max 128Kbyte#define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base @ of Sector 5, 128 Kbytes */#define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base @ of Sector 6, 128 Kbytes */#define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base @ of Sector 7, 128 Kbytes */#define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) /* Base @ of Sector 8, 128 Kbytes */#define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) /* Base @ of Sector 9, 128 Kbytes */#define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbytes */#define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbytes */static uint32_t GetSector(uint32_t Address){ uint32_t sector = 0; if((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0)) { sector = FLASH_SECTOR_0; } else if((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1)) { sector = FLASH_SECTOR_1; } else if((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2)) { sector = FLASH_SECTOR_2; } else if((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3)) { sector = FLASH_SECTOR_3; } else if((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4)) { sector = FLASH_SECTOR_4; } else if((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5)) { sector = FLASH_SECTOR_5; } else if((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6)) { sector = FLASH_SECTOR_6; } else if((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7)) { sector = FLASH_SECTOR_7; } else if((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8)) { sector = FLASH_SECTOR_8; } else if((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9)) { sector = FLASH_SECTOR_9; } else if((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10)) { sector = FLASH_SECTOR_10; } else /* (Address < FLASH_END_ADDR) && (Address >= ADDR_FLASH_SECTOR_11) */ { sector = FLASH_SECTOR_11; } return sector;}
写函数!
/* Get the 1st sector to erase */intwriteFlash(uint32_t startAddr,uint32_t endAddr,uint32_t* data,uint32_t len){/* Private macro -------------------------------------------------------------*//* Private variables ---------------------------------------------------------*/uint32_t FirstSector = 0, NbOfSectors = 0, Address = 0;uint32_t SectorError;/*Variable used for Erase procedure*/FLASH_EraseInitTypeDef EraseInitStruct;/* Unlock the Flash to enable the flash control register access *************/ HAL_FLASH_Unlock();FirstSector = GetSector(startAddr);/* Get the number of sector to erase from 1st sector*/NbOfSectors = GetSector(endAddr) - FirstSector + 1;/* Fill EraseInit structure*/EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;EraseInitStruct.Sector = FirstSector;EraseInitStruct.NbSectors = NbOfSectors;if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK){ /* Error occurred while sector erase. User can add here some code to deal with this error. SectorError will contain the faulty sector and then to know the code error on this sector, user can call function 'HAL_FLASH_GetError()'*/HAL_FLASH_Lock();return -1;}/* Program the user Flash area word by word (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/Address = startAddr;while(len -- ){if(data == NULL) break;if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, *data) == HAL_OK){ Address = Address + 4;}else{ HAL_FLASH_Lock(); return -1;}data++;}/* Lock the Flash to disable the flash control register access (recommendedto protect the FLASH memory against possible unwanted operation) *********/HAL_FLASH_Lock();return 0;}
读取函数!
int readFlash(uint32_t startAddr,uint32_t *pdata,uint32_t len){if(pdata == NULL) return -1;while(len --){if(pdata == NULL) break;*pdata = *(__IO uint32_t*)startAddr;startAddr += 4;pdata++;}return 0;}
STM的内部flash的读取对应做升级非常方便!IAP做U盘读取文件,写入flash,检验写入都很好!
0 0
- STM32 内部flash的读写程序
- stm32 读写内部Flash
- STM32读写内部Flash
- STM32 读写内部Flash
- STM32内部flash读写
- STM32内部FLASH读写-通用
- 读写STM32内部flash读写代码
- 读写STM32内部flash读写代码
- 读写STM32内部flash读写代码
- 读写STM32内部flash读写代码
- STM32的内部Flash
- STM32 对内部FLASH读写接口函数
- STM32 对内部FLASH读写接口函数
- STM32 对内部FLASH读写接口函数
- STM32 对内部FLASH读写接口函数
- STM32学习笔记:读写内部Flash。
- 实现STM32 Flash的读写
- STR710的内部Flash读写
- 师--链表的结点插入
- 使用Gilde加载图片时裁剪大小异常
- JSON与泛型的结合(大幅度减小工作量)-巧用工具类
- 内部类
- 解决ajax的status=0问题:submit和button冲突
- STM32 内部flash的读写程序
- Multilayer Perceptron(MLP)
- [BZOJ Contest-2017省队十连测推广赛1·T2][BZOJ4766][完全二分图的生成树个数]文艺计算姬
- 如何让某软件永不联网?
- zabbix自带触发器介绍
- ABP 框架从源码学习——abp框架启动和结束(1)
- leetcode-7. Reverse Integer
- 函数传参指针和引用的差别
- FastReport编写事件代码控制控件显示隐藏