flash好像没有写入是流程上少什么了吗?



/*********************************************************************

 * @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();

}


您好,在进行FLASH编程时,若使用标准编程方式,建议擦写都用标准编程方式;若用快速编程方式,则擦写都用快速编程方式。在MCU对应的EVT中都有FLASH相关例程,可直接在官网搜索MCU型号下载对应的EVT看一下。后续若有问题,可邮箱(lzs@wch.cn)和我沟通。


只有登录才能回复,可以选择微信账号登录