硬汉嵌入式论坛

 找回密码
 立即注册
查看: 4349|回复: 5
收起左侧

STM32F429--有关Flash的理解

[复制链接]

4

主题

4

回帖

4

积分

新手上路

积分
4
发表于 2015-4-10 15:04:36 | 显示全部楼层 |阅读模式
初探一周的stm32f429板子,读了固件库的部分demo代码,今天记录下自己学到的吧~闲话少说,正文开始!
stm32f429拥有2M的flash空间,并且通过datasheet可了解其物理地址为0x0800 0000 ~ 0x081F FFFF,如下图
无标题.png

下面我们就扒一扒如何对flash进行操作。
  1. [color=#ff0000]下面文件可从固件库stm32f4xx_flash.h头文件中查找到,这部分预定义的含义就是将flash的地址空间分为24个不同的扇区并进行编号,如扇区0([backcolor=#f7f7f7][color=#000000]FLASH_Sector_0[/color][/backcolor])的编号为 0x0000;[/color]
  2. #define FLASH_Sector_0     ((uint16_t)0x0000) /*!< Sector Number 0   */
  3. #define FLASH_Sector_1     ((uint16_t)0x0008) /*!< Sector Number 1   */
  4. #define FLASH_Sector_2     ((uint16_t)0x0010) /*!< Sector Number 2   */
  5. #define FLASH_Sector_3     ((uint16_t)0x0018) /*!< Sector Number 3   */
  6. #define FLASH_Sector_4     ((uint16_t)0x0020) /*!< Sector Number 4   */
  7. #define FLASH_Sector_5     ((uint16_t)0x0028) /*!< Sector Number 5   */
  8. #define FLASH_Sector_6     ((uint16_t)0x0030) /*!< Sector Number 6   */
  9. #define FLASH_Sector_7     ((uint16_t)0x0038) /*!< Sector Number 7   */
  10. #define FLASH_Sector_8     ((uint16_t)0x0040) /*!< Sector Number 8   */
  11. #define FLASH_Sector_9     ((uint16_t)0x0048) /*!< Sector Number 9   */
  12. #define FLASH_Sector_10    ((uint16_t)0x0050) /*!< Sector Number 10  */
  13. #define FLASH_Sector_11    ((uint16_t)0x0058) /*!< Sector Number 11  */
  14. #define FLASH_Sector_12    ((uint16_t)0x0080) /*!< Sector Number 12  */
  15. #define FLASH_Sector_13    ((uint16_t)0x0088) /*!< Sector Number 13  */
  16. #define FLASH_Sector_14    ((uint16_t)0x0090) /*!< Sector Number 14  */
  17. #define FLASH_Sector_15    ((uint16_t)0x0098) /*!< Sector Number 15  */
  18. #define FLASH_Sector_16    ((uint16_t)0x00A0) /*!< Sector Number 16  */
  19. #define FLASH_Sector_17    ((uint16_t)0x00A8) /*!< Sector Number 17  */
  20. #define FLASH_Sector_18    ((uint16_t)0x00B0) /*!< Sector Number 18  */
  21. #define FLASH_Sector_19    ((uint16_t)0x00B8) /*!< Sector Number 19  */
  22. #define FLASH_Sector_20    ((uint16_t)0x00C0) /*!< Sector Number 20  */
  23. #define FLASH_Sector_21    ((uint16_t)0x00C8) /*!< Sector Number 21  */
  24. #define FLASH_Sector_22    ((uint16_t)0x00D0) /*!< Sector Number 22  */
  25. #define FLASH_Sector_23    ((uint16_t)0x00D8) /*!< Sector Number 23  */
复制代码
  1. [color=#ff0000]并且对于不同扇区的地址与空间大小也不同,具体如下:[/color]
  2. #define ADDR_FLASH_SECTOR_0      ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbytes   */
  3. #define ADDR_FLASH_SECTOR_1      ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbytes   */
  4. #define ADDR_FLASH_SECTOR_2      ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbytes   */
  5. #define ADDR_FLASH_SECTOR_3      ((uint32_t)0x0800C000) /* Base @ of Sector 3, 16 Kbytes   */
  6. #define ADDR_FLASH_SECTOR_4      ((uint32_t)0x08010000) /* Base @ of Sector 4, 64 Kbytes   */
  7. #define ADDR_FLASH_SECTOR_5      ((uint32_t)0x08020000) /* Base @ of Sector 5, 128 Kbytes  */
  8. #define ADDR_FLASH_SECTOR_6      ((uint32_t)0x08040000) /* Base @ of Sector 6, 128 Kbytes  */
  9. #define ADDR_FLASH_SECTOR_7      ((uint32_t)0x08060000) /* Base @ of Sector 7, 128 Kbytes  */
  10. #define ADDR_FLASH_SECTOR_8      ((uint32_t)0x08080000) /* Base @ of Sector 8, 128 Kbytes  */
  11. #define ADDR_FLASH_SECTOR_9      ((uint32_t)0x080A0000) /* Base @ of Sector 9, 128 Kbytes  */
  12. #define ADDR_FLASH_SECTOR_10     ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbytes */
  13. #define ADDR_FLASH_SECTOR_11     ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbytes */
  14. #define ADDR_FLASH_SECTOR_12     ((uint32_t)0x08100000) /* Base @ of Sector 12, 16 Kbytes  */
  15. #define ADDR_FLASH_SECTOR_13     ((uint32_t)0x08104000) /* Base @ of Sector 13, 16 Kbytes  */
  16. #define ADDR_FLASH_SECTOR_14     ((uint32_t)0x08108000) /* Base @ of Sector 14, 16 Kbytes  */
  17. #define ADDR_FLASH_SECTOR_15     ((uint32_t)0x0810C000) /* Base @ of Sector 15, 16 Kbytes  */
  18. #define ADDR_FLASH_SECTOR_16     ((uint32_t)0x08110000) /* Base @ of Sector 16, 64 Kbytes  */
  19. #define ADDR_FLASH_SECTOR_17     ((uint32_t)0x08120000) /* Base @ of Sector 17, 128 Kbytes */
  20. #define ADDR_FLASH_SECTOR_18     ((uint32_t)0x08140000) /* Base @ of Sector 18, 128 Kbytes */
  21. #define ADDR_FLASH_SECTOR_19     ((uint32_t)0x08160000) /* Base @ of Sector 19, 128 Kbytes */
  22. #define ADDR_FLASH_SECTOR_20     ((uint32_t)0x08180000) /* Base @ of Sector 20, 128 Kbytes */
  23. #define ADDR_FLASH_SECTOR_21     ((uint32_t)0x081A0000) /* Base @ of Sector 21, 128 Kbytes */
  24. #define ADDR_FLASH_SECTOR_22     ((uint32_t)0x081C0000) /* Base @ of Sector 22, 128 Kbytes */
  25. #define ADDR_FLASH_SECTOR_23     ((uint32_t)0x081E0000) /* Base @ of Sector 23, 128 Kbytes */
  26. [color=#ff0000][b]因此如果相对 flash具体的地址空间操作的话,我们将需要一个转换函数static uint32_t GetSector(uint32_t Address),将地址转换为扇区的对应编号,并在编程中,以扇区的空间编号进行读写及其他操作。[/b][/color]
复制代码
下面的操作既是对flash某块区域进行写入数据操作,直接上代码:
  1. FLASH_Unlock();[color=#ff0000][b][b][color=#ff0000]//[/color][/b]对flash区进行解锁[/b][/color]
  2. 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]
  3. [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]
  4.   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]
  5. [color=#ff0000][b]// GetSector是从地址空间与扇区编号的对应函数,上文提及过[/b][/color]
  6.   uwSectorCounter = uwStartSector;
  7.   while (uwSectorCounter <= uwEndSector)
  8.   {
  9. [color=#ff0000][b]// 施加3v电压,擦除扇区数据[/b][/color]
  10.     if (FLASH_EraseSector(uwSectorCounter, VoltageRange_3) != FLASH_COMPLETE)
  11.     {
  12.       while (1)
  13.       {
  14.       }
  15.     }
  16. [b][color=#ff0000]// 跳到下一个扇区,通过扇区编号表可了解到从扇区11跳到扇区12相差(0x80-0x58=0x28)[/color][/b]
  17.     if (uwSectorCounter == FLASH_Sector_11)
  18.     {
  19.       uwSectorCounter += 40;
  20.     }
  21.     else
  22.     {
  23.       uwSectorCounter += 8;
  24.     }
  25.   }
  26. [backcolor=#f7f7f7]uwAddress = FLASH_USER_START_ADDR;[/backcolor]
  27.   while (uwAddress < FLASH_USER_END_ADDR)
  28.   {
  29.     if (FLASH_ProgramWord(uwAddress, DATA_32) == FLASH_COMPLETE) [b][color=#ff0000]//DATA_32为自定义数据,这里我定义为32F429DC[/color][/b]
  30.     {
  31.       uwAddress = uwAddress + 4;
  32.     }
  33.     else
  34.     {
  35.       while (1)
  36.       {
  37.       }
  38.     }
  39.   }
  40. [b][color=#ff0000]//最后要对flash进行加锁操作[/color][/b]
  41.   FLASH_Lock();
复制代码
至此,对flash指定扇区写入数据操作结束,烧入板子的部分就不做过多介绍。
可同过STM32 ST-Link Utility工具进行查证。
无标题.png


小弟最近了解的就是这些东西了,文章中的不足大神们可以随便喷。
还有,小弟有个问题:stm32系列的芯片上电后,是如何去查找flash的起始地址的,难道是将flash中的数据读到内存sram中进行执行的么,还是就是直接执行flash中的程序的。
希望看到帖子的能给个答复~~~

评分

参与人数 1 +5 收起 理由
eric2013 + 5

查看全部评分

回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107445
QQ
发表于 2015-4-10 15:07:22 | 显示全部楼层
[s:151]
谢谢楼主分享,我论坛上的添加代码工具不能设置字体大小和颜色,算是一个缺点吧。
回复

使用道具 举报

4

主题

4

回帖

4

积分

新手上路

积分
4
 楼主| 发表于 2015-4-10 15:31:33 | 显示全部楼层

回 eric2013 的帖子

eric2013:[s:151]
谢谢楼主分享,我论坛上的添加代码工具不能设置字体大小和颜色,算是一个缺点吧。 (2015-04-10 15:07) 
老大~帖子最后的问题能不能给个答案啊~~
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107445
QQ
发表于 2015-4-10 17:41:37 | 显示全部楼层
看工程里面的.S文件启动程序,系统上电后会进行一次复位,进入复位中断:

Reset_Handler    PROC
       EXPORT  Reset_Handler             [WEAK]
        IMPORT  SystemInit
        IMPORT  __main

                 LDR     R0, =SystemInit   //这个函数是设置时钟
                 BLX     R0
                 LDR     R0, =__main       //从这里就可以跳转到main函数开始执行
                 BX      R0
                 ENDP
回复

使用道具 举报

6

主题

390

回帖

408

积分

高级会员

积分
408
发表于 2015-5-3 16:17:00 | 显示全部楼层
[s:151]  [s:151]  [s:143]  [s:143]  [s:143]
回复

使用道具 举报

0

主题

3

回帖

0

积分

新手上路

积分
0
发表于 2015-5-18 08:38:22 | 显示全部楼层
C语言代码?????????
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-6-1 08:26 , Processed in 0.314201 second(s), 38 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表