|
初探一周的stm32f429板子,读了固件库的部分demo代码,今天记录下自己学到的吧~闲话少说,正文开始!
stm32f429拥有2M的flash空间,并且通过datasheet可了解其物理地址为0x0800 0000 ~ 0x081F FFFF,如下图
下面我们就扒一扒如何对flash进行操作。- [color=#ff0000]下面文件可从固件库stm32f4xx_flash.h头文件中查找到,这部分预定义的含义就是将flash的地址空间分为24个不同的扇区并进行编号,如扇区0([backcolor=#f7f7f7][color=#000000]FLASH_Sector_0[/color][/backcolor])的编号为 0x0000;[/color]
- #define FLASH_Sector_0 ((uint16_t)0x0000) /*!< Sector Number 0 */
- #define FLASH_Sector_1 ((uint16_t)0x0008) /*!< Sector Number 1 */
- #define FLASH_Sector_2 ((uint16_t)0x0010) /*!< Sector Number 2 */
- #define FLASH_Sector_3 ((uint16_t)0x0018) /*!< Sector Number 3 */
- #define FLASH_Sector_4 ((uint16_t)0x0020) /*!< Sector Number 4 */
- #define FLASH_Sector_5 ((uint16_t)0x0028) /*!< Sector Number 5 */
- #define FLASH_Sector_6 ((uint16_t)0x0030) /*!< Sector Number 6 */
- #define FLASH_Sector_7 ((uint16_t)0x0038) /*!< Sector Number 7 */
- #define FLASH_Sector_8 ((uint16_t)0x0040) /*!< Sector Number 8 */
- #define FLASH_Sector_9 ((uint16_t)0x0048) /*!< Sector Number 9 */
- #define FLASH_Sector_10 ((uint16_t)0x0050) /*!< Sector Number 10 */
- #define FLASH_Sector_11 ((uint16_t)0x0058) /*!< Sector Number 11 */
- #define FLASH_Sector_12 ((uint16_t)0x0080) /*!< Sector Number 12 */
- #define FLASH_Sector_13 ((uint16_t)0x0088) /*!< Sector Number 13 */
- #define FLASH_Sector_14 ((uint16_t)0x0090) /*!< Sector Number 14 */
- #define FLASH_Sector_15 ((uint16_t)0x0098) /*!< Sector Number 15 */
- #define FLASH_Sector_16 ((uint16_t)0x00A0) /*!< Sector Number 16 */
- #define FLASH_Sector_17 ((uint16_t)0x00A8) /*!< Sector Number 17 */
- #define FLASH_Sector_18 ((uint16_t)0x00B0) /*!< Sector Number 18 */
- #define FLASH_Sector_19 ((uint16_t)0x00B8) /*!< Sector Number 19 */
- #define FLASH_Sector_20 ((uint16_t)0x00C0) /*!< Sector Number 20 */
- #define FLASH_Sector_21 ((uint16_t)0x00C8) /*!< Sector Number 21 */
- #define FLASH_Sector_22 ((uint16_t)0x00D0) /*!< Sector Number 22 */
- #define FLASH_Sector_23 ((uint16_t)0x00D8) /*!< Sector Number 23 */
复制代码- [color=#ff0000]并且对于不同扇区的地址与空间大小也不同,具体如下:[/color]
- #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 */
- #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 */
- #define ADDR_FLASH_SECTOR_12 ((uint32_t)0x08100000) /* Base @ of Sector 12, 16 Kbytes */
- #define ADDR_FLASH_SECTOR_13 ((uint32_t)0x08104000) /* Base @ of Sector 13, 16 Kbytes */
- #define ADDR_FLASH_SECTOR_14 ((uint32_t)0x08108000) /* Base @ of Sector 14, 16 Kbytes */
- #define ADDR_FLASH_SECTOR_15 ((uint32_t)0x0810C000) /* Base @ of Sector 15, 16 Kbytes */
- #define ADDR_FLASH_SECTOR_16 ((uint32_t)0x08110000) /* Base @ of Sector 16, 64 Kbytes */
- #define ADDR_FLASH_SECTOR_17 ((uint32_t)0x08120000) /* Base @ of Sector 17, 128 Kbytes */
- #define ADDR_FLASH_SECTOR_18 ((uint32_t)0x08140000) /* Base @ of Sector 18, 128 Kbytes */
- #define ADDR_FLASH_SECTOR_19 ((uint32_t)0x08160000) /* Base @ of Sector 19, 128 Kbytes */
- #define ADDR_FLASH_SECTOR_20 ((uint32_t)0x08180000) /* Base @ of Sector 20, 128 Kbytes */
- #define ADDR_FLASH_SECTOR_21 ((uint32_t)0x081A0000) /* Base @ of Sector 21, 128 Kbytes */
- #define ADDR_FLASH_SECTOR_22 ((uint32_t)0x081C0000) /* Base @ of Sector 22, 128 Kbytes */
- #define ADDR_FLASH_SECTOR_23 ((uint32_t)0x081E0000) /* Base @ of Sector 23, 128 Kbytes */
- [color=#ff0000][b]因此如果相对 flash具体的地址空间操作的话,我们将需要一个转换函数static uint32_t GetSector(uint32_t Address),将地址转换为扇区的对应编号,并在编程中,以扇区的空间编号进行读写及其他操作。[/b][/color]
复制代码 下面的操作既是对flash某块区域进行写入数据操作,直接上代码:- FLASH_Unlock();[color=#ff0000][b][b][color=#ff0000]//[/color][/b]对flash区进行解锁[/b][/color]
- FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);[color=#ff0000][b][b][color=#ff0000]//[/color][/b]清楚标记位[/b][/color]
- [backcolor=#ffffff][color=#008ef1]uwStartSector = GetSector(FLASH_USER_START_ADDR);[/color][/backcolor][b][color=#ff0000][b][color=#ff0000]//[/color][/b]FLASH_USER_START_ADDR为自己定义的 #define FLASH_USER_START_ADDR ADDR_FLASH_SECTOR_4 起始地址[/color][/b]
- uwEndSector = GetSector(FLASH_USER_END_ADDR);[b][color=#ff0000]//[/color][/b][color=#ff0000][b]FLASH_USER_END_ADDR为自己定义的 FLASH_USER_END_ADDR ADDR_FLASH_SECTOR_23 终止地址[/b][/color]
- [color=#ff0000][b]// GetSector是从地址空间与扇区编号的对应函数,上文提及过[/b][/color]
- uwSectorCounter = uwStartSector;
- while (uwSectorCounter <= uwEndSector)
- {
- [color=#ff0000][b]// 施加3v电压,擦除扇区数据[/b][/color]
- if (FLASH_EraseSector(uwSectorCounter, VoltageRange_3) != FLASH_COMPLETE)
- {
- while (1)
- {
- }
- }
- [b][color=#ff0000]// 跳到下一个扇区,通过扇区编号表可了解到从扇区11跳到扇区12相差(0x80-0x58=0x28)[/color][/b]
- if (uwSectorCounter == FLASH_Sector_11)
- {
- uwSectorCounter += 40;
- }
- else
- {
- uwSectorCounter += 8;
- }
- }
- [backcolor=#f7f7f7]uwAddress = FLASH_USER_START_ADDR;[/backcolor]
- while (uwAddress < FLASH_USER_END_ADDR)
- {
- if (FLASH_ProgramWord(uwAddress, DATA_32) == FLASH_COMPLETE) [b][color=#ff0000]//DATA_32为自定义数据,这里我定义为32F429DC[/color][/b]
- {
- uwAddress = uwAddress + 4;
- }
- else
- {
- while (1)
- {
- }
- }
- }
- [b][color=#ff0000]//最后要对flash进行加锁操作[/color][/b]
- FLASH_Lock();
复制代码 至此,对flash指定扇区写入数据操作结束,烧入板子的部分就不做过多介绍。
可同过STM32 ST-Link Utility工具进行查证。
小弟最近了解的就是这些东西了,文章中的不足大神们可以随便喷。
还有,小弟有个问题:stm32系列的芯片上电后,是如何去查找flash的起始地址的,难道是将flash中的数据读到内存sram中进行执行的么,还是就是直接执行flash中的程序的。
希望看到帖子的能给个答复~~~ |
评分
-
查看全部评分
|