加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

关于STM32F4内部flash的操作

发布时间:2020-12-15 17:40:45 所属栏目:百科 来源:网络整理
导读:stm32f4系列,我用的407有1m的内部flash扇区有12个 初始化以及写入操作如下【这是从官方的库文件中摘出来的】 /**************** * =================================================================== * How to use this driver * =====================

stm32f4系列,我用的407有1m的内部flash扇区有12个


初始化以及写入操作如下【这是从官方的库文件中摘出来的】

/****************
  *          ===================================================================
  *                                 How to use this driver
  *          ===================================================================
  *                           
  *          This driver provides functions to configure and program the FLASH 
  *          memory of all STM32F4xx devices.
  *          These functions are split in 4 groups:
  * 
  *           1. FLASH Interface configuration functions: this group includes the
  *              management of the following features:
  *                    - Set the latency
  *                    - Enable/Disable the prefetch buffer
  *                    - Enable/Disable the Instruction cache and the Data cache
  *                    - Reset the Instruction cache and the Data cache
  *  
  *           2. FLASH Memory Programming functions: this group includes all needed
  *              functions to erase and program the main memory:
  *                    - Lock and Unlock the FLASH interface
  *                    - Erase function: Erase sector,erase all sectors
  *                    - Program functions: byte,half word,word and double word
  *  
  *           3. Option Bytes Programming functions: this group includes all needed
  *              functions to manage the Option Bytes:
  *                    - Set/Reset the write protection
  *                    - Set the Read protection Level
  *                    - Set the BOR level
  *                    - Program the user Option Bytes
  *                    - Launch the Option Bytes loader
  *  
  *           4. Interrupts and flags management functions: this group 
  *              includes all needed functions to:
  *                    - Enable/Disable the FLASH interrupt sources
  *                    - Get flags status
  *                    - Clear flags
  *                    - Get FLASH operation status
 *********************************/

于是初始化就可以写成这样

	FLASH_SetLatency(FLASH_Latency_4);
	FLASH_PrefetchBufferCmd(ENABLE);
	FLASH_DataCacheCmd(ENABLE);
	FLASH_InstructionCacheCmd(ENABLE);
	FLASH_DataCacheReset();
	FLASH_InstructionCacheReset();
	FLASH_OB_Unlock();
	FLASH_Unlock();
	FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGAERR | FLASH_FLAG_WRPERR); 
写操作像这样

先解锁,读操作无关解锁,然后清标志位,再是根据自己的需要存入适当的位置

	FLASH_Unlock();
	FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGAERR | FLASH_FLAG_WRPERR); 
	flash_status = FLASH_EraseSector(FLASH_Sector_11,VoltageRange_3);
写操作官方的库提供以下几种不同的大小的字节的方式

FLASH_Status FLASH_ProgramByte(uint32_t Address,uint8_t Data);
FLASH_Status FLASH_ProgramDoubleWord(uint32_t Address,uint64_t Data);
FLASH_Status FLASH_ProgramHalfWord(uint32_t Address,uint16_t Data);
FLASH_Status FLASH_ProgramWord(uint32_t Address,uint32_t Data)?

读操作,像这样就行了

the_data_u_need = *(__IO uint8_t *)(0x080E0002);
the_data_u_need = *(__IO uint16_t *)(0x080E0002);
the_data_u_need = *(__IO uint32_t *)(0x080E0002);
the_data_u_need = *(__IO uint64_t *)(0x080E0002);



PS:玩的时候,最好不要去用sector0,因为bootloader和主程序在那里,会让程序跑飞的,最好放最后一区。????? 一开始debug的时候我用的sector0然后就飞了,后来才发现是这原因。。。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读