當前位置:首頁 > 單片機 > 單片機
[導讀] STM32F4Discovery開發(fā)幫使用的STM32F407VGT6芯片,內部FLASH有1M之多。平時寫的代碼,燒寫完之后還有大量的剩余。有效利用這剩余的FLASH能存儲不少數(shù)據(jù)。因此研究了一下STM32F4讀寫內部FLASH的一些操作

STM32F4Discovery開發(fā)幫使用的STM32F407VGT6芯片,內部FLASH有1M之多。平時寫的代碼,燒寫完之后還有大量的剩余。有效利用這剩余的FLASH能存儲不少數(shù)據(jù)。因此研究了一下STM32F4讀寫內部FLASH的一些操作。

【STM32F4 內部Flash的一些信息】

STM32F407VG的內部FLASH的地址是:0x08000000,大小是0x00100000。

寫FLASH的時候,如果發(fā)現(xiàn)寫入地址的FLASH沒有被擦出,數(shù)據(jù)將不會寫入。FLASH的擦除操作,只能按Sector進行。不能單獨擦除一個地址上的數(shù)據(jù)。因此在寫數(shù)據(jù)之前需要將地址所在Sector的所有數(shù)據(jù)擦除。

在STM32F4的編程手冊上可找到FLASH的Sector劃分,我們現(xiàn)在只操作Main memory:

參考Demo中的例子,將FLASH的頁的其實地址(基地址)可定義如下:

/* 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 */

在庫里邊,F(xiàn)LASH的Sector編號定義如下,這是供庫里邊的幾個函數(shù)使用的。需要將地址轉換成Sector編號:

#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中有將地址轉換成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;
}

有了這些定義之后,我們就可以開始正式操作FLASH了

首先,要向FLASH寫入數(shù)據(jù)需要先將FLASH解鎖。根據(jù)手冊定義,解鎖FLASH需要先向寄存器FLASH_KEYR寫入0x45670123之后再向這個寄存器寫入0xCDEF89AB。這兩個數(shù)據(jù)在庫中已經定義成了:FLASH_KEY1和FLASH_KEY2.

使用庫函數(shù)不用這么麻煩,函數(shù)FLASH_Unlock()即可完成對FLASH的解鎖。

解鎖FLASH之后,使用函數(shù)FLASH_ClearFlag清除FLASH的狀態(tài)寄存器。然后就可以對FLASH進行寫操作了。我按照示例工程,擦除兩塊FLASH。

下邊是操作FLASH的代碼,首先擦除兩塊FLASH,然后向這兩塊FLASH中寫入數(shù)據(jù)。最后進行校驗:

要寫入的數(shù)據(jù)定義:

#define DATA_32 ((uint32_t)0x12345678)

開始FLASH操作:

FLASH_Unlock(); //解鎖FLASH后才能向FLASH中寫數(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編號
EndSector = GetSector(FLASH_USER_END_ADDR);

//擦除FLASH
for (i = StartSector; i < EndSector; i += 8) //每次FLASH編號增加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)
{
}
}
}

/*擦除完畢*/
/*開始寫入*/
Address = FLASH_USER_START_ADDR;

while (Address < FLASH_USER_END_ADDR)
{
if (FLASH_ProgramWord(Address, DATA_32) == FLASH_COMPLETE) //將DATA_32寫入相應的地址。
{
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ù) 檢查寫入值是否正確
Address = FLASH_USER_START_ADDR;
MemoryProgramStatus = 0x0;
while (Address < FLASH_USER_END_ADDR)
{
data32 = *(__IO uint32_t*)Address; //讀FLASH中的數(shù)據(jù),直接給出地址就行了。跟從內存中讀數(shù)據(jù)一樣。

if (data32 != DATA_32)
{
MemoryProgramStatus++;
}

Address = Address + 4;
}

下邊是使用STLink Utility讀出的數(shù)據(jù),檢查一下,確實寫進去數(shù)據(jù)了:

參考文檔是ST的 STM32F40xxx and STM32F41xxx Flash programming manual??稍赟T網站下載。文檔編號:PM0081。FLASH的有不少寄存器,各個含義手冊上有詳細介紹。我只是簡單地看了下。使用庫函數(shù)操作,好像不大需要詳細理解這些寄存器了。


本站聲明: 本文章由作者或相關機構授權發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內容真實性等。需要轉載請聯(lián)系該專欄作者,如若文章內容侵犯您的權益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或將催生出更大的獨角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關鍵字: 阿維塔 塞力斯 華為

加利福尼亞州圣克拉拉縣2024年8月30日 /美通社/ -- 數(shù)字化轉型技術解決方案公司Trianz今天宣布,該公司與Amazon Web Services (AWS)簽訂了...

關鍵字: AWS AN BSP 數(shù)字化

倫敦2024年8月29日 /美通社/ -- 英國汽車技術公司SODA.Auto推出其旗艦產品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時1.5...

關鍵字: 汽車 人工智能 智能驅動 BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務能7×24不間斷運行,同時企業(yè)卻面臨越來越多業(yè)務中斷的風險,如企業(yè)系統(tǒng)復雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務連續(xù)性,提升韌性,成...

關鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報道,騰訊和網易近期正在縮減他們對日本游戲市場的投資。

關鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產業(yè)博覽會開幕式在貴陽舉行,華為董事、質量流程IT總裁陶景文發(fā)表了演講。

關鍵字: 華為 12nm EDA 半導體

8月28日消息,在2024中國國際大數(shù)據(jù)產業(yè)博覽會上,華為常務董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權最終是由生態(tài)的繁榮決定的。

關鍵字: 華為 12nm 手機 衛(wèi)星通信

要點: 有效應對環(huán)境變化,經營業(yè)績穩(wěn)中有升 落實提質增效舉措,毛利潤率延續(xù)升勢 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務引領增長 以科技創(chuàng)新為引領,提升企業(yè)核心競爭力 堅持高質量發(fā)展策略,塑強核心競爭優(yōu)勢...

關鍵字: 通信 BSP 電信運營商 數(shù)字經濟

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺與中國電影電視技術學會聯(lián)合牽頭組建的NVI技術創(chuàng)新聯(lián)盟在BIRTV2024超高清全產業(yè)鏈發(fā)展研討會上宣布正式成立。 活動現(xiàn)場 NVI技術創(chuàng)新聯(lián)...

關鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會上,軟通動力信息技術(集團)股份有限公司(以下簡稱"軟通動力")與長三角投資(上海)有限...

關鍵字: BSP 信息技術
關閉
關閉