/*********************************************************************
?* @fn? ? ? CFG_ERASE
?*
?* @brief? ?erase Data-Flash block, minimal block is 256B
?*
?* @param? ?Page_Address - the address of the page being erased.
?*? ? ? ? ? Length - Erased data length
?*
?* @return? none
?*/
void CFG_ERASE(uint32_t Page_Address, u32 Length) {
? ? u32 NbrOfPage, EraseCounter;
? ? FLASH_Unlock_Fast();
? ? NbrOfPage = Length / FLASH_PAGE_SIZE;
? ? for (EraseCounter = 0; EraseCounter < NbrOfPage; EraseCounter++) {
? ? ? ? //FLASH_ErasePage( Page_Address + (FLASH_PAGE_SIZE * EraseCounter));
? ? ? ? FLASH_ErasePage_Fast( Page_Address + (FLASH_PAGE_SIZE * EraseCounter));
? ? }
? ? FLASH_Lock_Fast();
}
/*********************************************************************
?* @fn? ? ? CFG_WRITE
?*
?* @brief? ?write Data-Flash data block
?*
?* @param? ?StartAddr - the address of the page being written.
?*? ? ? ? ? Buffer - data buff
?*? ? ? ? ? Length - written data length
?*
?* @return? FLASH_Status
?*/
FLASH_Status CFG_WRITE( u32 StartAddr, u8 *Buffer, u32 Length )
{
? ? u32 address = StartAddr;
? ? u32 *p_buff = (u32 *)Buffer;
? ? FLASH_Status FLASHStatus = FLASH_COMPLETE;
? ? FLASH_Unlock();
? ? while((address < (StartAddr + Length)) && (FLASHStatus == FLASH_COMPLETE))
? ? {
? ? ? ? FLASHStatus = FLASH_ProgramWord(address, *p_buff);
? ? ? ? address += 4;
? ? ? ? p_buff++;
? ? }
? ? FLASH_Lock();
? ? return FLASHStatus;
}
/*********************************************************************
?* @fn? ? ? CFG_READ
?*
?* @brief? ?read Data-Flash data block
?*
?* @param? ?StartAddr - the address of the page being read.
?*? ? ? ? ? Buffer - data buff
?*? ? ? ? ? Length - read data length
?*
?* @return? none
?*/
void CFG_READ( u32 StartAddr, u8 *Buffer, u32 Length )
{
? ? u32 address = StartAddr;
? ? u32 *p_buff = (u32 *)Buffer;
? ? while(address < (StartAddr + Length))
? ? {
? ? ? ? *p_buff = (*(u32 *)address);
? ? ? ? address += 4;
? ? ? ? p_buff++;
? ? }
}
//配置參數(shù)
__MODBUS_PARAM g_configParam;
//保存
void configParam_save(void){
? ? u8 crc8bit=0;
? ? //計(jì)算校驗(yàn)
? ? crc8bit=crc8(g_configParam.buf,PARAM_SIZE-1);
? ? g_configParam.buf[PARAM_SIZE-1]=crc8bit;
? ? //寫入flash
? ? FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP |FLASH_FLAG_WRPRTERR);
? ? //擦除
? ? CFG_ERASE(PARAM_WRITE_START_ADDR,PARAM_SIZE);
? ? //讀取
? ? //CFG_READ(PARAM_WRITE_START_ADDR,&g_configParam,PARAM_SIZE );//debug
? ? //寫入
? ? CFG_WRITE(PARAM_WRITE_START_ADDR,&g_configParam,PARAM_SIZE );
? ? //讀取
? ? CFG_READ(PARAM_WRITE_START_ADDR,&g_configParam,PARAM_SIZE );//debug? 讀取的內(nèi)容并不是寫入的結(jié)構(gòu)體信息;
? ? FLASH_Lock();
}