版本 1d78319a3d5e983cd76ccd19c9e795148c10f879
Flash
1.Flash 簡介
1.1 簡介
快閃記憶體(Flash Memory、閃存),主要用於一般性資料儲存,以及在電腦及其他數位產品間交換傳輸資料。如記憶卡、隨身碟等。快閃記憶體是一種特殊的、以大區塊抹寫的EEPROM(電子抹除式可複寫唯讀記憶體)。
快閃記憶體晶片的低階介面通常與透過支援外界的定址匯流排行隨機存取的DRAM、ROM、EEPROM等記憶體不同。 NOR Flash本身為讀取操作(支援隨機存取)提供外部定址匯流排;至於解鎖、抹除與寫入則須以區塊-區塊(Block-by-block)的方式進行,典型的區塊大小為64、128或256位元組。NAND Flash所有的動作都必須以區塊性基礎(Block-wise fashion)執行,包含讀、寫、解鎖與抹除。
快閃記憶體(wiki)<http://zh.wikipedia.org/wiki/%E5%BF%AB%E9%96%83%E8%A8%98%E6%86%B6%E9%AB%94>
_
1.2 NOR/NAND介紹
- NOR Flash :
- NOR的特點是芯片內執行(XIP, eXecute In Place),這樣應用程序可以直接在flash閃存內運行,不必再把代碼讀到系統RAM中
- 小容量時具有很高的成本效益,但是很低的寫入和擦除速度大大影響了它的性能
NAND Flash
- NOR Flash Structure :
.. image:: /NOR flash.JPG
- NAND Flash Structure :
.. image:: /NAND flash.JPG
1.3 NOR/NAND比較
讀取速度 | 寫入速度 | 擦除速度 | 單元尺寸 | 成本 | 每次操作區塊(KB) | 每次操作時間 | |
---|---|---|---|---|---|---|---|
NOR Flash | 快 | 慢 | 慢 | 大(單位密度低) | 高 |
|
|
NAND Flash | 慢 | 快 | 快 | 小(單位密度高) | 低 |
|
4ms(快125倍) |
- 關於NAND Flash 與 NOR Flash的比較可參考 http://www.8051faq.com.cn/manager/download/20077633203664115781250.PDF
2.Flash 記憶體架構介紹
STM32F407中的Flash有以下特性:
- 容量為1Mbyte
- 單次讀取data為128bits
- 單次寫入可以byte, half-word, word以及double word為單位
- 抹除可以sector為單位或是mass(全部)操作
.. image:: /Flash_system_architecture.JPG
圖中一些縮寫的解釋如下:
- I-Code bus : Instruction bus.這條bus由Cortex-M4F連至BusMatrix,核心透過這個bus取出指令。
- D-Code bus : Data bus.這條bus由Cortex-M4F 與 64-Kbyte CCM data RAM 連至BusMatrix,核心透過這個bus讀取字母(literal)與除錯。
- AHB : Advanced High-performance Bus.
- APB : Advanced Peripheral Bus.
關於AHB與APB差異可參考 Difference Between AHB and APB<http://www.differencebetween.net/technology/difference-between-ahb-and-apb/>
_
關於AMBA的介紹可參考 Advanced Microcontroller Bus Architecture (AMBA)<http://en.wikipedia.org/wiki/AMBA_High-performance_Bus>
_
- DMA : Direct Memory Access. DMA提供周邊裝置與記憶體、記憶體與記憶體間高速的傳輸,而不須經由CPU的動作。
- CCM : Core Coupled Memory.給core專用的全速64KB RAM,在沒有經過BusMatrix的情況下與core直接連結。
- FLITF : Flash memory interface.
Bus Matrix:
.. image:: /bus_matrix.JPG
Flash模組的組成如下:
- Main memory : 4個16Kbytes Sector、1個64Kbytes Sector及7個128Kbytes Sector,一共1024Kbytes(1Mbytes).
- System memory : bootloader code 放置的地方,30Kbytes.
- OTP (One-Time Programmable) : 一次性寫入的空間,共528Kbytes(512+16),如放軟體version,硬體version,key…等,基於安全的理由,可參考(http://forum.eepw.com.cn/thread/120354/1)
- Option byte : 用來設定讀寫保護、電壓level、軟硬體看門狗與Standby or Stop模式下的重置,共16Kbytes.
.. image:: /flash module organization.JPG
3 Flash 操作
基本觀念:
- Flash的每個cell在寫的時候只能1->0,不能從0->1;而erase後,該sector中所有的cell值皆為1。
- 當bit寫入由1寫成0時,不需要先erase;但若將0寫為1時,則需要先erase才能寫。
3.1 Read interface(讀取)
- 為了要正確的從Flash中讀取data,必須在Flash access control register(FLASH_ACR)中,依據CPU clock frequency(HCLK)與device供應的電壓,來設定正確的wait states(LATENCY)值。
- 因為CPU的運行速度遠比Flash快得多,依下表來看,STM32F407的Flash最快access速度為<=30MHZ,如果CPU frequency超過此速度,那就必須增加等待時間。wait states與CPU clock freqency的關係如下表所示 :
.. image:: /ws_hclk.JPG
- 在Reset之後,CPU clock frequency為16MHz,並且FLASH_ACR中的wait states值被設為0。
- 官方文件建議若要調整wait states值(當加快/減慢CPU frequency時),依據CPU frequency調試存取Flash所需的ws數。
當加快CPU frequency時
- 在FLASH_ACR register中的LATENCY bits設定新的wait states值。
::
000: 0ws(1 CPU cycle) 001: 1ws(2 CPU cycle) 010: 2ws(3 CPU cycle) 011: 3ws(4 CPU cycle) 100: 4ws(5 CPU cycle) 101: 5ws(6 CPU cycle) 110: 6ws(7 CPU cycle) 111: 7ws(8 CPU cycle)
- 透過讀取FLASH_ACR register,確認新的wait states值有被無設定成功。
- 再透過寫入RCC_CFGR(Reset and Clock Control Configuration Register)中的SW(System clock switch) bits來修改 CPU clock source.
::
sw : 00 : HSI (High Speed Internal) 01 : HSE (High Speed External) 10 : PLL (Phase Lock Loop) 11 : not allowed
- 當離開Stop或Standby模式時,或者當HSE Failure時,將由硬體強制轉為HSI。可參考(http://blog.csdn.net/joji_h/article/details/5581340)
- 如果需要,可透過寫入RCC_CFGR register中的HPRE(AHB Prescaler) bits來修改CPU clock prescaler以調整clock freqency.
::
HPRE bits: 0xxx : System clock not divided 1000 : System clock divided by 2 1001 : System clock divided by 4 1010 : System clock divided by 8 … 1111 : System clock divided by 512
- 透過讀取讀取RCC_CFGR register中的SWS(System clock switch status) bits 來確認新的SW(System clock switch) bits有無被設定成功;透過讀取RCC_CFGR register中的來確定新的HPRE(AHB Prescaler) bits有無被設定成功。
當減慢CPU frequency時
- 透過寫入RCC_CFGR(Reset and Clock Control Configuration Register)中的SW(System clock switch) bits來修改 CPU clock source.
- 如果需要,可透過寫入RCC_CFGR register中的HPRE(AHB Prescaler) bits來修改CPU clock prescaler以調整clock freqency.
- 透過讀取讀取RCC_CFGR register中的SWS(System clock switch status) bits 來確認新的SW(System clock switch) bits有無被設定成功;透過讀取RCC_CFGR register中的來確定新的HPRE(AHB Prescaler) bits有無被設定成功。
- 在FLASH_ACR register中的LATENCY bits設定新的wait states值。
- 透過讀取FLASH_ACR register,確認新的wait states值有被無設定成功。
p.s. 加快cpu freqency的step 1~2等於減慢cpu freqency的step4~5;加快cpu freqency的step 3~5等於減慢cpu freqency的step1~3。
3.2 Erase and program operations
操作前需知
- CPU clock frequency必須至少要在1MHZ以上,Flash才能做erase及program的操作,若在操作的途中發生reset,則不保證Flash中內容正確。
- Flash在program/erase操作時,任何欲read Flash的操作會被暫停,需直到program操作完成後,read操作才會被正確的執行。這也代表當program/erase操作正在執行中,code or data fetch(即read)無法執行。
在做抹除與寫入動作之前,必須先設定Flash control register(FLASH_CR),然而在Reset之後,FLASH_CR是不可寫入的。因此必須透過以下方法解鎖:
- 在Flash key register(FLASH_KEYR)中寫入0x45670123。
- 在Flash key register(FLASH_KEYR)中寫入0xCDEF89AB。
必須連續執行1與2才會解鎖
- 若步驟錯誤一樣會鎖住FLASH_CR,直到reset後才能用以上方式解鎖。
- 若解鎖後要再次鎖住,只要在FLASH_CR中設定LOCK bit即可。
此外,FLASH_CR在FLASH_SR的BSY值為1時是同樣不可寫入的。
3.3 Program/erase parallelism (寫入/抹除並行)
- Parallelism為在操作program或erase時,最大能夠寫幾bit的0。
- PSIZE是在FLASH_CR register中,此值必須在programming/erasing前正確的被寫入。對應表如下表:
.. image:: /parallelism.JPG
- 以下是各種型態單次寫入時所需的大小
- Byte : x8
- Half-word : x16
- Word : x32
- Double-word : x64
3.4 Erase (抹除)
Flash memory erase的操作可以以sector為單位,或是整個Flash memory全部erase,也就是Mass Erase。而Mass Erase並不會影響OTP sector或是設定區的sector。
- 區塊抹除(Sector Erase):
- 檢查Flash status register(FLASH_SR)中BSY(Busy) bit是否為0。(0代表目前沒有其他的Flash操作,1代表有其他Flash操作正在進行中)
- 設定FLASH_CR register中SER(Sector Erase) bit告知啟用區塊抹除,並設定SNB(Sector number) bit告知欲抹除的區塊。
::
0000 sector 0 0001 sector 1 … 1011 sector 11 不得超過此範圍。
- 設定FLASH_CR register中的STRT(Start) bit。
- 等待FLASH_SR register中的BSY bit清空。
- 大量抹除(Mass Erase):
- 檢查FLASH_SR register中BSY bit是否為0。
- 設定FLASH_CR register中的MER(Mass Erase) bit。
- 設定FLASH_CR register中的STRT bit。
- 等待FLASH_SR register中的BSY bit清空。
3.5 Program (寫入)
- 檢查FLASH_SR register中BSY bit是否為0。
- 設定FLASH_CR中的PG(Programming) bit。
- 在指定的位置(main memory block或OTP area)執行資料寫入的動作。
- Byte access in case of x8 parallelism
- Half-word access in case of x16 parallelism
- Word access in case of x32 parallelism
- Double word access in case of x64 parallelism
- 等待FLASH_SR中的BSY bit清空。
3.6 Interrupt
若要在erase或program結束時發出中斷,可以設定FLASH_CR register中的end of operation interrupt enable(EOPIE) bit,當然,FLASH_SR register中的BSY bit這時應是要0,而end of operation(EOP) bit此時為1。
如果在program或erase操作過程中有錯誤發生,FLASH_SR register中的某一flag會被設1:
- PGAERR, PGPERR, PGSERR (Program error flags)
- WRPERR (Protection error flag)
此時,若FLASH_CR register中的error interrupt enable bit(ERRIE)被打開(設為1),則FLASH_SR register中的operation error bit(OPERR)會被設為1。
3.7 Option bytes
關於寫入保護(Write Protections)
在12個User Sector中(請見5.Flash記憶體架構介紹),Flash memory可以設為保護狀態來避免非預期的寫入操作造成的數據遺失問題。當FLASH_OPTCR中的nWRPi(non-write protection) bit (0 <= i <= 11)為0時,i所對應的sector就不能夠被寫入或抹除。同樣地,在大量抹除中,只要在抹除範圍內有任何一個Sector是寫入保護的狀態,就不能夠進行抹除。
若嘗試在寫入保護的區域中進行寫入/抹除的行為,將會在FLASH_SR中設置WRPERR(Write protection error flag)。
3.8 One-time programmable bytes (OTP)
4.Flash interface registers
4.1 Flash access control register (FLASH_ACR)
Address offset : 0x00, Reset value: 0x0000 0000.
.. image:: /Flash_acr.JPG
::
Bits 31:11 保留位置,必須保持淨空。 Bit 12 DCRST : Data cache reset. 0: not reset; 1: reset. (當D cache未啟用時才可寫入) Bit 11 ICRST : Instruction cache reset. 0: not reset ; 1: reset. (當I cache未啟用時才可寫入) Bit 10 DCEN : Data cache enable. 0: disabled; 1: enabled. Bit 9 ICEN : Instruction cache enable. 0: disabled; 1: enabled. Bit 8 PRFTEN : Prefetch enable. 0: disabled; 1: enabled; Bits 7:3 保留位置,必須保持淨空。 Bits 2:0 LATENCY : Latency.
4.2 Flash key register (FLASH_KEYR)
Address offset : 0x04, Reset value: 0x0000 0000.
.. image:: /flash_keyr.JPG
::
Bits 31:0 FKEYR : FPEC key. 解鎖用,前方已經描述過如何解鎖FLASH_CR,在此不贅述。
4.3 Flash status register (FLASH_SR)
Address offset : 0x0C, Reset value: 0x0000 0000.
.. image:: /flash_sr.JPG
::
Bits 31:17 保留位置,必須保持淨空。 Bit 16 BSY : Busy. 0: 無其他記憶體操作; 1: 其他記憶體操作進行中。 Bits 15:8 保留位置,必須保持淨空。 Bit 7 PGSERR : Programming sequence error. 當程式出現錯誤時會設值。若寫入1則清空。 Bit 6 PGPERR : Programming parallelism error. 當程式寫入大小與型態不符(對照PSIZE)時會設值。若寫入1則清空。 Bit 5 PGAERR : Programming alignment error. 當資料寫入時無法塞入128-bit的Flash memory row會設值。若寫入1則清空。 Bit 4 WRPERR : Write protection error. 當嘗試寫入/抹除一個寫入保護的Flash memory區域時會設值。若寫入1則清空。 Bit 3:2 保留位置,必須保持淨空。 Bit 1 OPERR : Operation error. 當任何的Flash操作失效時設值,但這個bit只會在ERRIE(Error Interrupts)啟用時生效。 Bit 0 EOP : End of Operation. 當一或多個Flash操作成功完成時設值,這個bit只會在EOPIE(End of Operation Interrupts)啟用時生效。若寫入1則清空。
4.4 Flash control register (FLASH_CR)
Address offset : 0x10, Reset value: 0x8000 0000.
.. image:: /flash_cr.JPG
::
Bit 31 LOCK : Lock. 只能寫為1,當值為1時,FLASH_CR將鎖住不可更改,唯有偵測到解鎖序列才會清除。 Bits 30:26 保留位置,必須保持淨空。 Bit 25 ERRIE: Error interrupt enable. 0: Disabled; 1: Enabled. Bit 24 EOPIE: End of operation interrupt enable. 0: Disabled; 1: Enabled. Bits 23:17 保留位置,必須保持淨空。 Bit 16 STRT: Start. 只能由軟體設置,且在BSY bit清空時會一併清空。 Bits 15:10 保留位置,必須保持淨空。 Bits 9:8 PSIZE: Program size. 前方有提過PSIZE的設置,在此不贅述。 Bits 6:3 SNB: Sector number. 前方有提過SNB的設置,在此不贅述。 Bit 2 MER: Mass Erase. Bit 1 SER: Sector Erase. Bit 0 PG: Programming.
4.5 Flash option control register (FLASH_OPTCR)
Address offset : 0x14, Reset value: 0x0FFF AAED.
.. image:: /flash_optcr.JPG
::
Bits 31:28 保留位置,必須保持淨空。 Bits 27:16 nWRP: Not write protect. 0: write protection active; 1: write protection not active. Bits 15:8 RDP: Read protect. 0xAA : Level 0, read protection not active; 0xCC : Level 2, chip read protection active; Others : Level 1, read protection of memories active. Bits 7:5 USER: User option bytes Bit 4 保留位置,必須保持淨空。 Bits 3:2 BOR_Level: BOR reset level. 00 : BOR level 3, 電壓 2.70~3.60V 01 : BOR level 2, 電壓 2.40~2.70V 10 : BOR level 1, 電壓 2.10~2.40V 11 : BOR off, 電壓 1.80~2.10V Bit 1 OPTSTRT: Option start. Bit 0 OPTLOCK: Option lock.
5.Example
5.1 LED LEGEND
GREEN : FLASH is unlocked. BLUE : FLASH is writing. RED : FLASH is erasing. YELLOW: FLASH is reading.
5.2 git repository
-git repo: https://gitcafe.com/xturtle/NCKU-Embedded-2012-Flash-Demo
5.3 operation flow
- 1 FLASH_UNLOCK
- 2 GREEN LED ON
::
FLASH_Unlock(); STM_EVAL_LEDOn(LED4); FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
- 3 WRITE FROM SECTOR 2 TO SECTOR 5 AS “12345678”
- 4 BLUE LED ON
- 5 FINISHED WRITE PROCESS
- 6 BLUE LED OFF
::
while (Address < FLASH_USER_END_ADDR){ STM_EVAL_LEDOn(LED6); if (FLASH_ProgramWord(Address, DATA_32) == FLASH_COMPLETE) Address = Address + 4; else{ /* Error occurred while writing data in Flash memory. User can add here some code to deal with this error */ STM_EVAL_LEDOn(LED3); } STM_EVAL_LEDOff(LED6); }
- 7 FLASH_LOCK
- 8 GREEN LED OFF
::
FLASH_Lock(); STM_EVAL_LEDOff(LED4);
- 9 VERIFICATION
::
while (Address < FLASH_USER_END_ADDR){ STM_EVAL_LEDOn(LED3); data32 = *(__IO uint32_t*)Address; STM_EVAL_LEDOff(LED3); if (data32 != DATA_32){ MemoryProgramStatus++;
} Address = Address + 4; }
6.STM32F407VG 硬體介紹
- STM32F407VG版子外觀與模組如下圖所示:
.. image:: /STM32F4_2.jpg
- 上方USB Programmer/Debugger為ST-Link
- 石英晶體震盪器(Crystal),可參考(http://zh.wikipedia.org/zh-tw/%E7%9F%B3%E8%8B%B1%E6%99%B6%E4%BD%93%E8%B0%90%E6%8C%AF%E5%99%A8)
- 中央的晶片為ARM® Cortex™-M4F core with embedded Flash and SRAM,其內部結構如Figure 5所示。
- 三軸加速度感知器(3-axis Accelerometer)
- 音頻數位類比轉換(Audio DAC)
- 麥克風晶片(Chip Microphone)
- USB On The Go (http://forum.slime.com.tw/thread180246.html)
- 音效輸出(Audio Jack)
- 此外,板子上有兩顆按鈕,藍色按鈕為User Button,黑色按鈕為Reset。
.. image:: /block_diag_1.JPG
.. image:: /block_diag_2.JPG
7.Software STM32F4 Lib
.. image:: /lib_1.JPG
8.reference
- http://wiki.csie.ncku.edu.tw/embedded/learn-stm32-part-1.pdf
- http://wiki.csie.ncku.edu.tw/embedded/learn-stm32-part-6.pdf
- http://www.triplespark.net/elec/pdev/arm/stm32.html
- http://www.codeproject.com/Articles/14983/Remote-Debugging-using-GDB
- http://cms.mcuapps.com/products/stm32f4-discovery/README.html
- http://cms.mcuapps.com/techinfo/toolchains/openocd/
- http://www.st.com/internet/mcu/product/252140.jsp
- http://zh.wikipedia.org/wiki/%E9%97%AA%E5%AD%98