STM32F4读写内部FLASH【使用库函数】
STM32F4Discovery開(kāi)發(fā)幫使用的STM32F407VGT6芯片,內(nèi)部FLASH有1M之多。平時(shí)寫(xiě)的代碼,燒寫(xiě)完之后還有大量的剩余。有效利用這剩余的FLASH能存儲(chǔ)不少數(shù)據(jù)。因此研究了一下STM32F4讀寫(xiě)內(nèi)部FLASH的一些操作。
【STM32F4 內(nèi)部Flash的一些信息】
STM32F407VG的內(nèi)部FLASH的地址是:0x08000000,大小是0x00100000。
寫(xiě)FLASH的時(shí)候,如果發(fā)現(xiàn)寫(xiě)入地址的FLASH沒(méi)有被擦出,數(shù)據(jù)將不會(huì)寫(xiě)入。FLASH的擦除操作,只能按Sector進(jìn)行。不能單獨(dú)擦除一個(gè)地址上的數(shù)據(jù)。因此在寫(xiě)數(shù)據(jù)之前需要將地址所在Sector的所有數(shù)據(jù)擦除。
在STM32F4的編程手冊(cè)上可找到FLASH的Sector劃分,我們現(xiàn)在只操作Main memory:
參考Demo中的例子,將FLASH的頁(yè)的其實(shí)地址(基地址)可定義如下:
/* 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 */
#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 */
在庫(kù)里邊,FLASH的Sector編號(hào)定義如下,這是供庫(kù)里邊的幾個(gè)函數(shù)使用的。需要將地址轉(zhuǎn)換成Sector編號(hào):
#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 */
Demo中有將地址轉(zhuǎn)換成Sector的代碼:
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;
}
?
有了這些定義之后,我們就可以開(kāi)始正式操作FLASH了
首先,要向FLASH寫(xiě)入數(shù)據(jù)需要先將FLASH解鎖。根據(jù)手冊(cè)定義,解鎖FLASH需要先向寄存器FLASH_KEYR寫(xiě)入0x45670123之后再向這個(gè)寄存器寫(xiě)入0xCDEF89AB。這兩個(gè)數(shù)據(jù)在庫(kù)中已經(jīng)定義成了:FLASH_KEY1和FLASH_KEY2.
使用庫(kù)函數(shù)不用這么麻煩,函數(shù)FLASH_Unlock()即可完成對(duì)FLASH的解鎖。
解鎖FLASH之后,使用函數(shù)FLASH_ClearFlag清除FLASH的狀態(tài)寄存器。然后就可以對(duì)FLASH進(jìn)行寫(xiě)操作了。我按照示例工程,擦除兩塊FLASH。
下邊是操作FLASH的代碼,首先擦除兩塊FLASH,然后向這兩塊FLASH中寫(xiě)入數(shù)據(jù)。最后進(jìn)行校驗(yàn):
要寫(xiě)入的數(shù)據(jù)定義:
#define DATA_32???????????????? ((uint32_t)0x12345678)
開(kāi)始FLASH操作:
? FLASH_Unlock(); //解鎖FLASH后才能向FLASH中寫(xiě)數(shù)據(jù)。
? FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
????????????????? FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
? /* Get the number of the start and end sectors */
? StartSector = GetSector(FLASH_USER_START_ADDR);? //獲取FLASH的Sector編號(hào)
? EndSector = GetSector(FLASH_USER_END_ADDR);
?
? //擦除FLASH
? for (i = StartSector; i < EndSector; i += 8)? //每次FLASH編號(hào)增加8,可參考上邊FLASH Sector的定義。
? {
??? /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
?????? be done by word */
??? if (FLASH_EraseSector(i, VoltageRange_3) != FLASH_COMPLETE)
??? {
????? while (1)
????? {
????? }
??? }
? }
?
? /*擦除完畢*/
? /*開(kāi)始寫(xiě)入*/
? Address = FLASH_USER_START_ADDR;
?
??? while (Address < FLASH_USER_END_ADDR)
? {
??? if (FLASH_ProgramWord(Address, DATA_32) == FLASH_COMPLETE)?? //將DATA_32寫(xiě)入相應(yīng)的地址。
??? {
????? Address = Address + 4;
??? }
??? else
??? {
????? /* Error occurred while writing data in Flash memory.
???????? User can add here some code to deal with this error */
????? while (1)
????? {
????? }
??? }
? }
?
? FLASH_Lock();? //讀FLASH不需要FLASH處于解鎖狀態(tài)。
?
//讀出數(shù)據(jù) 檢查寫(xiě)入值是否正確
? Address = FLASH_USER_START_ADDR;
? MemoryProgramStatus = 0x0;
?? while (Address < FLASH_USER_END_ADDR)
? {
??? data32 = *(__IO uint32_t*)Address;?? //讀FLASH中的數(shù)據(jù),直接給出地址就行了。跟從內(nèi)存中讀數(shù)據(jù)一樣。
??? if (data32 != DATA_32)
??? {
????? MemoryProgramStatus++;?
??? }
??? Address = Address + 4;
? }?
?
下邊是使用STLink Utility讀出的數(shù)據(jù),檢查一下,確實(shí)寫(xiě)進(jìn)去數(shù)據(jù)了:
參考文檔是ST的 STM32F40xxx and STM32F41xxx Flash programming manual。可在ST網(wǎng)站下載。文檔編號(hào):PM0081。FLASH的有不少寄存器,各個(gè)含義手冊(cè)上有詳細(xì)介紹。我只是簡(jiǎn)單地看了下。使用庫(kù)函數(shù)操作,好像不大需要詳細(xì)理解這些寄存器了。
?
PS:這個(gè)實(shí)驗(yàn)主要代碼來(lái)自ST的Demo。這里我只是加入了個(gè)人的注釋。不當(dāng)之處,望高人指點(diǎn)。
總結(jié)
以上是生活随笔為你收集整理的STM32F4读写内部FLASH【使用库函数】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: cocos中添加显示文字的三种方式(CC
- 下一篇: 域账号的登录过程