STM32的flash數據頁轉存過程分析!
stm32模擬eeprom要實現flash數據頁轉存,實現函數為
1 /** 2 * @brief Transfers last updated variables data from the full Page to 3 * an empty one. 4 * @param VirtAddress: 16 bit virtual address of the variable 5 * @param Data: 16 bit data to be written as variable value 6 * @retval Success or error status: 7 * - FLASH_COMPLETE: on success8 * - PAGE_FULL: if valid page is full 9 * - NO_VALID_PAGE: if no valid page was found 10 * - Flash error code: on write Flash error 11 */ 12 uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
這個函數的過程為:
1. 得到當前可讀有效頁,並設置其為oldPage,另一頁為NewPage。
2. 給NewPage設置RECEIVE_DATA狀態
3.初始化寫地址 切換到接收頁
4. 寫一個當前要保存的數據,如果單純發起一次轉存,則隨意保存一個數據。
5. 讀出除第三步保存過的所有要保存的變量數據,保存到NewPage中。
6. 擦除OldPage(執行時間15ms-2s,根據扇區大小而定(1K~128K))
7. 給NewPage設置VALID_PAGE狀態。
第5步中 Get the valid Page end Address
1 if((PageStatus0 == RECEIVE_DATA)||(PageStatus1 == RECEIVE_DATA))//當頁傳輸的時候把valid Page end Address切換到可讀有效頁的最後 2 { 3 Address = (uint32_t)((EEPROM_START_ADDRESS - 2) + (uint32_t)((1 + ValidPage) * PAGE_SIZE)); 4 } 5 else 6 { 7 Address=CurWrAddress-2; 8 }
轉存完成後的各頁的狀態:NewPage:VALID_PAGE;
OldPage:ERASED;
但是在轉存過程中系統斷電,則各頁的狀態可能出現
1.NewPage:ERASED; OldPage:VALID_PAGE; 這種情況發生在EE_PageTransfer函數執行到第二步之前系統掉電。那麽這次轉存過程就當沒有發生過,要保存的那個數據變量也得不到保存。
2.NewPage:RECEIVE_DATA; OldPage:VALID_PAGE; 這種情況發生在EE_PageTransfer函數執行到第六步之前系統掉電。
3.NewPage:RECEIVE_DATA; OldPage:ERASED; 這種情況發生第六步執行過程中系統掉電。
主要討論第2、3中情況下開機初始化eeprom的時候處
1 //NewPage:RECEIVE_DATA; OldPage:VALID_PAGE; 2 InitCurrWrAddress();//初始化寫地址
2 //這裏NewPage為PAGE1 OldPage為Page0
3 4 /* Transfer data from Page0 to Page1 */ 5 for (VarIdx = 0; VarIdx < NumbOfVar; VarIdx++) 6 { 7 if ((*(__IO uint16_t*)(PAGE1_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx]) 8 { 9 x = VarIdx; 10 } 11 if (VarIdx != x) 12 { 13 /* Read the last variables‘ updates */ 14 ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar); 15 /* In case variable corresponding to the virtual address was found */ 16 if (ReadStatus != 0x1) 17 { 18 /* Transfer the variable to the Page1 */ 19 EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar); 20 /* If program operation was failed, a Flash error code is returned */ 21 if (EepromStatus != FLASH_COMPLETE) 22 { 23 return EepromStatus; 24 } 25 } 26 } 27 }//for (VarIdx... 28 /* Mark Page1 as valid */ 29 FlashStatus = FLASH_ProgramHalfWord(PAGE1_BASE_ADDRESS, VALID_PAGE); 30 /* If program operation was failed, a Flash error code is returned */ 31 if (FlashStatus != FLASH_COMPLETE) 32 { 33 return FlashStatus; 34 } 35 /* Erase Page0 */ 36 FlashStatus = FLASH_EraseSector(PAGE0_ID, VOLTAGE_RANGE); 37 /* If erase operation was failed, a Flash error code is returned */ 38 if (FlashStatus != FLASH_COMPLETE) 39 { 40 return FlashStatus; 41 }
過程:
1.讀出除第一個位置保存過的所有要保存的變量數據,保存到NewPage中。
2. 把RECEIVE_DATA狀態改為VALID_PAGE
3. 擦除OldPage。
1 //NewPage:RECEIVE_DATA; OldPage:ERASED;OldPage:Page0 NewPage:Page1 2 /* Erase Page0 */ 3 FlashStatus = FLASH_EraseSector(PAGE0_ID, VOLTAGE_RANGE); 4 /* If erase operation was failed, a Flash error code is returned */ 5 if (FlashStatus != FLASH_COMPLETE) 6 { 7 return FlashStatus; 8 } 9 /* Mark Page1 as valid */ 10 FlashStatus = FLASH_ProgramHalfWord(PAGE1_BASE_ADDRESS,VALID_PAGE); 11 /* If program operation was failed, a Flash error code is returned */ 12 if (FlashStatus != FLASH_COMPLETE) 13 { 14 return FlashStatus; 15 }
第三種情況下
1. 擦除OldPage
2. 把RECEIVE_DATA狀態改為VALID_PAGE
STM32的flash數據頁轉存過程分析!