我想用內(nèi)部FLASH 保存幾十個自己的數(shù)據(jù),但是我嘗試寫入的時(shí)候發(fā)現(xiàn)寫入時(shí)間需要4-5S才能完成。
????uint32_t?secpos;????????/*?扇區(qū)地址?*/ ????uint16_t?secoff;????????/*?扇區(qū)內(nèi)偏移地址(16位字計(jì)算)?*/ ????uint16_t?secremain;?????/*?扇區(qū)內(nèi)剩余地址(16位字計(jì)算)?*/ ????uint16_t?i; ????uint32_t?offaddr;???????/*?去掉0X08000000后的地址?*/ ?? ????if?(waddr?<?STM32_FLASH_BASE?||?(waddr?>=?(STM32_FLASH_BASE?+?1024?*?STM32_FLASH_SIZE))) ????{ ????????return;?????/*?非法地址?*/ ????} ????FLASH_Unlock();?????/*?FLASH解鎖?*/ ????FLASH_ClearFlag(FLASH_FLAG_BSY?|?FLASH_FLAG_EOP?|FLASH_FLAG_WRPRTERR); ????offaddr?=?waddr?-?STM32_FLASH_BASE;?????????????/*?實(shí)際偏移地址.?*/ ????secpos?=?offaddr?/?STM32_SECTOR_SIZE;???????????/*?扇區(qū)地址??0~255?for?STM32F103ZET6?*/ ????secoff?=?(offaddr?%?STM32_SECTOR_SIZE)?/?2;?????/*?在扇區(qū)內(nèi)的偏移(2個字節(jié)為基本單位.)?*/ ????secremain?=?STM32_SECTOR_SIZE?/?2?-?secoff;?????/*?扇區(qū)剩余空間大小?*/ ????if?(length?<=?secremain) ????{ ????????secremain?=?length;?/*?不大于該扇區(qū)范圍?*/ ????} ????while?(1) ????{ ????????flash_read(secpos?*?STM32_SECTOR_SIZE?+?STM32_FLASH_BASE,?g_flashbuf,?STM32_SECTOR_SIZE?/?2);?/*?讀出整個扇區(qū)的內(nèi)容?*/ ????????for?(i?=?0;?i?<?secremain;?i++)??????????????????????????????????????????????????????????????/*?校驗(yàn)數(shù)據(jù)?*/ ????????{ ????????????if?(g_flashbuf[secoff?+?i]?!=?0XFFFF) ????????????{ ????????????????break;??????/*?需要擦除?*/ ????????????} ????????} ????????if?(i?<?secremain)??/*?需要擦除?*/ ????????{? ????????????FLASH_ErasePage(secpos?*?STM32_SECTOR_SIZE?+?STM32_FLASH_BASE);??//Erase?4KB ????????????for?(i?=?0;?i?<?secremain;?i++)?????????/*?復(fù)制?*/ ????????????{ ????????????????g_flashbuf[i?+?secoff]?=?pbuf[i]; ????????????} ????????????flash_write_nocheck(secpos?*?STM32_SECTOR_SIZE?+?STM32_FLASH_BASE,?g_flashbuf,?STM32_SECTOR_SIZE?/?2);?/*?寫入整個扇區(qū)?*/ ????????} ????????else ????????{ ????????????flash_write_nocheck(waddr,?pbuf,?secremain);?/*?寫已經(jīng)擦除了的,直接寫入扇區(qū)剩余區(qū)間.?*/ ????????} ????????if?(length?==?secremain) ????????{ ????????????break;?/*?寫入結(jié)束了?*/ ????????} ????????else???????/*?寫入未結(jié)束?*/ ????????{ ????????????secpos++;???????????????/*?扇區(qū)地址增1?*/ ????????????secoff?=?0;?????????????/*?偏移位置為0?*/ ????????????pbuf?+=?secremain;??????/*?指針偏移?*/ ????????????waddr?+=?secremain?*?2;?/*?寫地址偏移(16位數(shù)據(jù)地址,需要*2)?*/ ????????????length?-=?secremain;????/*?字節(jié)(16位)數(shù)遞減?*/ ???????????? ????????????if?(length?>?(STM32_SECTOR_SIZE?/?2)) ????????????{ ????????????????secremain?=?STM32_SECTOR_SIZE?/?2;?/*?下一個扇區(qū)還是寫不完?*/ ????????????} ????????????else ????????????{ ????????????????secremain?=?length;?/*?下一個扇區(qū)可以寫完了?*/ ????????????} ????????} ????} ????FLASH_Lock();;?/*?上鎖?*/
熱門產(chǎn)品 :
CH390:以太網(wǎng)控制器芯片