WU-YANG Technology Co., Ltd. SIOC 實驗 5 : Embedded Flash 實驗 郭明聰 / 陳慶瀚 MIAT...

Post on 19-Dec-2015

232 views 8 download

Transcript of WU-YANG Technology Co., Ltd. SIOC 實驗 5 : Embedded Flash 實驗 郭明聰 / 陳慶瀚 MIAT...

WU-YANG Technology Co., Ltd.

SIOC 實驗 5 : Embedded Flash 實驗

郭明聰 / 陳慶瀚MIAT 實驗室

2

實驗目的 瞭解 STM32 內部 Flash 記憶體與處理器界面的

架構;實驗存取 Flash 的資料,並透過 VCP 傳送到超級終端機觀察結果

Flash 界面架構

3

SIOC 為 64KB

FLITF Features

Read interface with prefetch buffer (2x64-bit words)

Option byte Loader Flash Program / Erase operation Read / Write protection

4

Memory Mapping

5

Memory Mapping

6

0x800 3000DFU 程式區塊, User 不可使用

0x800 80000x800 C000

Embedded Flash Memory

7

8

Flash memory interface registers

Flash memory interface register

9

Bit 9 OPTWRE: Option bytes write enableWhen set, the option bytes can be programmed. This bit is set on writing the correct key sequence to the FLASH_OPTKEYR register.This bit can be reset by softwareBit 8 Reserved, must be kept cleared.Bit 7 LOCK: LockWrite to 1 only. When it is set, it indicates that the FPEC and FLASH_CR are locked. This bit is reset by hardware after detecting the unlock sequence.In the event of unsuccessful unlock operation, this bit remains set until the next reset.

Bit 6 STRT: StartThis bit triggers an ERASE operation when set. This bit is set only by software and reset when the BSY bit is reset.Bit 5 OPTER: Option byte eraseOption byte erase chosen.Bit 4 OPTPG: Option byte programmingOption byte programming chosen.Bit 3 Reserved, must be kept cleared.Bit 2 MER: Mass eraseErase of all user pages chosen.Bit 1 PER: Page erasePage Erase chosen.Bit 0 PG: ProgrammingFlash programming chosen.

FLASH library function

10

FLASH library function(conti.)

11

Main Flash memory programming

12

Flash memory Page Erase

13

Flash memory Mass Erase

14

Option byte programming

15

Option byte Erase

16

WU-YANG Technology Co., Ltd.

實驗

實驗 1 : flash memory 資料存取實驗 2 :非揮發性資料寫入實驗 3 : flash 資料寫入鎖定

WU-YANG Technology Co., Ltd.

實驗 1 : flash memory 資料存取

程式架構

19

Programming

BootupSTM32F10x

NVIC Configure

int main(void){ u32 *temp; Set_System();

/*pause*/ getchar(); printf("start \r\n"); FLASHStatus = FLASH_COMPLETE; MemoryProgramStatus = PASSED; Data = 0x15041925;

/* NVIC configuration */ NVIC_Configuration(); …....

Flash memory R/W operation

FLASH Unlock

FLASH ClearFlag

FLASH ErasePage

FLASH ProgramWord

程式架構 (conti.)

20

/* Unlock the Flash Program Erase controller */FLASH_Unlock(); /* Define the number of page to be erased */ NbrOfPage = (EndAddr - StartAddr) / FLASH_PAGE_SIZE; /* Clear All pending flags */FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);/* Erase the FLASH pages */for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++){ FLASHStatus = FLASH_ErasePage(StartAddr + (FLASH_PAGE_SIZE * EraseCounter));}/* FLASH Word program of data 0x15041979 at addresses defined by StartAddr and EndAddr*/ Address = StartAddr;while((Address < EndAddr) && (FLASHStatus == FLASH_COMPLETE)){ FLASHStatus = FLASH_ProgramWord(Address, Data); temp=(u32 *)Address; printf("0x%x data is 0x%x \r\n",Address,*temp); Address = Address + 4;}

Flash memory R/W operation

FLASH Unlock

FLASH ClearFlag

FLASH ErasePage

FLASH ProgramWord

預設定義說明 #define StartAddr ((u32)0x08008000)

定義 Flash 使用起始點 使用起始點必需大於 0x8003000 + Code Size

#define EndAddr ((u32)0x0800C000) 定義 Flash 使用結束點 使用起始點必需小於 0x8040000

Data = 0x15041975; 寫入資料 32Bit

#define FLASH_PAGE_SIZE ((u16)0x400) 每一個 Page 有 1KByte

21

• 附註 : 0x8000000~0x8003000 為 DFU 程式區塊,使用此區塊將造成無法燒錄程式

• Code Size 可由產生的 HEX 檔得知

Intel HEX Format

22

HEX Example

23

資料填入起始位置為 0x08003000 每一行有 0x10(16) 個 Bytes 最後一筆資料為 04000005080030EDD2 資料結束點為 0x08005C10 (0x08005C10 -0x08000000)/0x400 = 23 需以 0x400 為單位,所以可存的 flash 位置起點為 0x08006000

:020000040800F2:10300000100E002001310008913E0008DD3D00084F………:105C00001A4497292829106908011B3C020406CC74:0C5C10002D0102030404850607080900AA:04000005080030EDD2:00000001FF

程式執行結果

24

Flash 存取控制流程

25

改變 Putty Lines of scrollback 數值

26

實驗 1 注意事項 #define StartAddr ((u32)0x08020000) /* 定義 Flash 使用起始點

*/

使用起始點必需大於 0x8003000 + Code Size 0x8000000~0x8003000 為 DFU 程式區塊,使用此區塊

將造成無法燒錄程式 Code Size 可由產生的 HEX 檔得知

u32 Address = 0x08020000; 定義 Address 初始值 初始值必需與 Flash 使用起始點相同

WU-YANG Technology Co., Ltd.

實驗 2 :非揮發性資料寫入觀察 Flash 寫入後電源關閉 ( 拔除 USB連線 ) ,再重新接電後觀察寫入資料是否仍存在

29

實驗 2 程式說明

/* Erase the FLASH pages */ for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++) { FLASHStatus = FLASH_ErasePage(StartAddr + (FLASH_PAGE_SIZE * EraseCounter)); }

/* FLASH Word program of data 0x15041979 at addresses defined by StartAddr and EndAddr*/ Address = StartAddr;

while((Address < EndAddr) && (FLASHStatus == FLASH_COMPLETE)) { FLASHStatus = FLASH_ProgramWord(Address, Data); temp=(u32 *)Address; printf("0x%x data is 0x%x \r\n",Address,*temp); Address = Address + 4; }

Flash memory R/W operation

FLASH ErasePage

FLASH ProgramWord

WU-YANG Technology Co., Ltd.

實驗 3 : flash 資料寫入鎖定

實驗 3 說明 注意 :

請使用預設 0x08006000 之後的位置,避免覆蓋 DFU與使用者程式碼區塊

更改寫入位置測試寫入鎖定時需同時更改 FLASH_EnableWriteProtection 鎖定之 Page

練習 : 打開 #define WriteProtection_Enable 測試是否可寫入 打開 #define WriteProtection_Disable 測試是否可寫入 修改存取位置測試是否正常 修改寫入資料測試是否正常

32

/* Uncomment this line to Enable Write Protection *///#define WriteProtection_Enable/* Uncomment this line to Disable Write Protection */#define WriteProtection_Disable#ifdef WriteProtection_Disable printf("WriteProtection_Disable \r\n"); if (ProtectedPages == 0x00) {/* Pages are write protected */ /* Disable the write protection */ FLASHStatus = FLASH_EraseOptionBytes(); /* Generate System Reset to load the new option byte values */ NVIC_GenerateSystemReset(); }#else #ifdef WriteProtection_Enable printf("WriteProtection_Enable \r\n"); if (ProtectedPages != 0x00) {/* Pages not write protected */ /* Disable the write protection */ FLASHStatus = FLASH_EraseOptionBytes(); /* Enable the pages write protection */ //FLASHStatus = FLASH_EnableWriteProtection(FLASH_WRProt_Pages12to13 |FLASH_WRProt_Pages14to15); // 0x00006000 / 0x800 =12 0x0000 8000 / 0x800= 16

/* Generate System Reset to load the new option byte values */ NVIC_GenerateSystemReset(); } #endif#endif

實驗 3 說明

WU-YANG Technology Co., Ltd.

Q & A