STM32F4 FLASH笔记
Posted -Robot-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STM32F4 FLASH笔记相关的知识,希望对你有一定的参考价值。
STM32 FLASH
Cortex M3 M4存储器映射:
地址空间可以通过一种方式分为8个512MB的空间:
STM32F4xx寄存器边界地址:
STM32F4xx内存和Flash地址:
STM库函数操作FLASH:
1. FLASH解锁、上锁函数
对内部FLASH解锁、上锁的函数见代码清单 502。
FLASH解锁、上锁
1
2 #define FLASH_KEY1 ((uint32_t)0x45670123)
3 #define FLASH_KEY2 ((uint32_t)0xCDEF89AB)
4 /**
5 * @brief Unlocks the FLASH control register access
6 * @param None
7 * @retval None
8 */
9 void FLASH_Unlock(void)
10 {
11 if ((FLASH->CR & FLASH_CR_LOCK) != RESET) {
12 /* Authorize the FLASH Registers access */
13 FLASH->KEYR = FLASH_KEY1;
14 FLASH->KEYR = FLASH_KEY2;
15 }
16 }
17
18 /**
19 * @brief Locks the FLASH control register access
20 * @param None
21 * @retval None
22 */
23 void FLASH_Lock(void)
24 {
25 /* Set the LOCK Bit to lock the FLASH Registers access */
26 FLASH->CR |= FLASH_CR_LOCK;
27 }
解锁的时候,它对FLASH_KEYR寄存器写入两个解锁参数,上锁的时候,对FLASH_CR寄存器的FLASH_CR_LOCK位置1。
2. 设置操作位数及擦除扇区
解锁后擦除扇区时可调用FLASH_EraseSector完成,见代码清单 503。
擦除扇区
26 FLASH_Status FLASH_EraseSector(uint32_t FLASH_Sector, uint8_t VoltageRange)
27 {
28 uint32_t tmp_psize = 0x0;
29 FLASH_Status status = FLASH_COMPLETE;
30
31 /* Check the parameters */
32 assert_param(IS_FLASH_SECTOR(FLASH_Sector));
33 assert_param(IS_VOLTAGERANGE(VoltageRange));
34
35 if (VoltageRange == VoltageRange_1) {
36 tmp_psize = FLASH_PSIZE_BYTE;
37 } else if (VoltageRange == VoltageRange_2) {
38 tmp_psize = FLASH_PSIZE_HALF_WORD;
39 } else if (VoltageRange == VoltageRange_3) {
40 tmp_psize = FLASH_PSIZE_WORD;
41 } else {
42 tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
43 }
44 /* Wait for last operation to be completed */
45 status = FLASH_WaitForLastOperation();
46
47 if (status == FLASH_COMPLETE) {
48 /* if the previous operation is completed, proceed to erase the sector */
49 FLASH->CR &= CR_PSIZE_MASK;
50 FLASH->CR |= tmp_psize;
51 FLASH->CR &= SECTOR_MASK;
52 FLASH->CR |= FLASH_CR_SER | FLASH_Sector;
53 FLASH->CR |= FLASH_CR_STRT;
54
55 /* Wait for last operation to be completed */
56 status = FLASH_WaitForLastOperation();
57
58 /* if the erase operation is completed, disable the SER Bit */
59 FLASH->CR &= (~FLASH_CR_SER);
60 FLASH->CR &= SECTOR_MASK;
61 }
62 /* Return the Erase Status */
63 return status;
64 }
本函数包含两个输入参数,分别是要擦除的扇区号和工作电压范围,选择不同电压时实质是选择不同的数据操作位数,参数中可输入的宏在注释里已经给出。函数根据输入参数配置PSIZE位,然后擦除扇区,擦除扇区的时候需要等待一段时间,它使用FLASH_WaitForLastOperation等待,擦除完成的时候才会退出FLASH_EraseSector函数。
3. 写入数据
对内部FLASH写入数据不像对SDRAM操作那样直接指针操作就完成了,还要设置一系列的寄存器,利用FLASH_ProgramWord、FLASH_ProgramHalfWord和FLASH_ProgramByte函数可按字、半字及字节单位写入数据
16 FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
17 {
18 FLASH_Status status = FLASH_COMPLETE;
19
20 /* Check the parameters */
21 assert_param(IS_FLASH_ADDRESS(Address));
22
23 /* Wait for last operation to be completed */
24 status = FLASH_WaitForLastOperation();
25
26 if (status == FLASH_COMPLETE) {
27/* if the previous operation is completed, proceed to program the new data */
28 FLASH->CR &= CR_PSIZE_MASK;
29 FLASH->CR |= FLASH_PSIZE_WORD;
30 FLASH->CR |= FLASH_CR_PG;
31
32 *(__IO uint32_t*)Address = Data;
33
34 /* Wait for last operation to be completed */
35 status = FLASH_WaitForLastOperation();
36
37 /* if the program operation is completed, disable the PG Bit */
38 FLASH->CR &= (~FLASH_CR_PG);
39 }
40 /* Return the Program Status */
41 return status;
42 }
以上是关于STM32F4 FLASH笔记的主要内容,如果未能解决你的问题,请参考以下文章