/*********************************************************************
* @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++;
}
}
//配置参数
__MODBUS_PARAM g_configParam;
//保存
void configParam_save(void){
u8 crc8bit=0;
//计算校验
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 读取的内容并不是写入的结构体信息;
FLASH_Lock();
}