static int8u erasePage(int32u page) { StStatus status; int32u i, k; int32u address; int8u *flash; //Erasing a LEFT or RIGHT page requires erasing all of the flash pages. //Since the mgmt bytes are stored at the bottom of a page, the flash pages //are erased from the top down ensuring that that mgmt words are the last //data to be erased. This way, if a reset occurs while erasing, the mgmt //words are still valid the next time determineState() is called. for(i=NVM_FLASH_PAGE_COUNT;i>0;i--) { address = (page+((i-1)*MFB_PAGE_SIZE_B)); flash = (int8u *)address; //Scan the page to determine if it is fully erased already. //If the flash is not erased, erase it. The purpose of scanning //first is to save a little time if erasing is not required. for(k=0;k<MFB_PAGE_SIZE_B;k++,flash++) { if(*flash != 0xFF) { status = halInternalFlashErase(MFB_PAGE_ERASE, address); if(status != ST_SUCCESS) { return status; } //Don't bother looking at the rest of this flash page and just //move to the next. k=MFB_PAGE_SIZE_B; } } } return ST_SUCCESS; }
/******************************************************************************* ** 函数名称: set_startup_info ** 函数功能: 将重启信息出入flash ** 入口参数: StartUp_Info:描述重启信息结构体 ** 出口参数: 无 ** 备 注: 无 *******************************************************************************/ static void set_startup_info(StartUp_Info *value) { uint32_t len; //写入flash中的长度(半字长) len = (sizeof(StartUp_Info)+1)/2; //半字长度为单位 #define POWERON_FLASH (*((volatile int32u *)addr)) halInternalFlashErase(MFB_PAGE_ERASE,addr); //将StartUp_Info结构写入flash while(halFlashEraseIsActive()); halInternalFlashWrite(addr,(unsigned short*)value,len); }
void stm32w_flash_write(uint32_t address, const void *data, uint32_t length) { const uint32_t end = address + length; uint32_t i; uint32_t next_page, curr_page; uint16_t offset; #if STATIC_FLASH_BUFFER static uint8_t buf[FLASH_PAGE_SIZE]; #else uint8_t buf[FLASH_PAGE_SIZE]; #endif for(i = address; i < end;) { next_page = (i | (FLASH_PAGE_SIZE - 1)) + 1; curr_page = i & ~(FLASH_PAGE_SIZE - 1); offset = i - curr_page; if(next_page > end) { next_page = end; } /* Read a page from flash and put it into a mirror buffer. */ stm32w_flash_read(curr_page, buf, FLASH_PAGE_SIZE); /* Update flash mirror data with new data. */ memcpy(buf + offset, data, next_page - i); /* Erase flash page. */ ENERGEST_ON(ENERGEST_TYPE_FLASH_WRITE); halInternalFlashErase(MFB_PAGE_ERASE, i); /* Write modified data form mirror buffer into the flash. */ halInternalFlashWrite(curr_page, (uint16_t *) buf, FLASH_PAGE_SIZE / 2); ENERGEST_OFF(ENERGEST_TYPE_FLASH_WRITE); data = (uint8_t *) data + next_page - i; i = next_page; } }