void halInternalSetMfgTokenData(int16u token, void *data, int8u len) { StStatus flashStatus; int32u realAddress = (DATA_BIG_INFO_BASE|token); int8u * flash = (int8u *)realAddress; int32u i; //The flash library (and hardware) requires the address and length to both //be multiples of 16bits. Since this API is only valid for writing to //the CIB, verify that the token+len falls within the CIB. assert((token&1) != 1); assert((len&1) != 1); assert((realAddress>=CIB_BOTTOM) && ((realAddress+len-1)<=CIB_TOP)); //CIB manufacturing tokens can only be written by on-chip code if the token //is currently unprogrammed. Verify the entire token is unwritten. The //flash library performs a similar check, but verifying here ensures that //the entire token is unprogrammed and will prevent partial writes. for(i=0;i<len;i++) { assert(flash[i] == 0xFF); } //Remember, the flash library operates in 16bit quantities, but the //token system operates in 8bit quantities. Hence the divide by 2. flashStatus = halInternalFlashWrite(realAddress, data, (len/2)); assert(flashStatus == 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_erase(uint8_t sector) { /* halInternalFlashErase(MFB_PAGE_ERASE, COFFEE_START + (sector) * COFFEE_SECTOR_SIZE); */ uint16_t data = 0; uint32_t addr = COFFEE_START + (sector) * COFFEE_SECTOR_SIZE; uint32_t end = addr + COFFEE_SECTOR_SIZE; /* This prevents from accidental write to CIB. */ if(!(addr >= MFB_BOTTOM && end <= MFB_TOP + 1)) { return; } for(; addr < end; addr += 2) { halInternalFlashWrite(addr, &data, 1); } }
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; } }