www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁(yè) > 單片機(jī) > 單片機(jī)
[導(dǎo)讀]STM32F4Discovery開(kāi)發(fā)幫使用的STM32F407VGT6芯片,內(nèi)部FLASH有1M之多。平時(shí)寫(xiě)的代碼,燒寫(xiě)完之后還有大量的剩余。有效利用這剩余的FLASH能存儲(chǔ)不少數(shù)據(jù)。因此研究了一下STM32F4讀寫(xiě)內(nèi)部FLASH的一些操作?!維TM32F4 內(nèi)

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ù)里邊,F(xiàn)LASH的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)。


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

9月2日消息,不造車(chē)的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

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

關(guān)鍵字: 汽車(chē) 人工智能 智能驅(qū)動(dòng) BSP

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

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

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

關(guān)鍵字: 騰訊 編碼器 CPU

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

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

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

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

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

關(guān)鍵字: 通信 BSP 電信運(yùn)營(yíng)商 數(shù)字經(jīng)濟(jì)

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

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

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

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉