ch32v203 内部Flash那块可以用户用

image.png 用你家开发的一款产品 现在要配置掉电不丢失 这个内部flash那块是可以我自己用的 然后又读写的例程参考吗  

ch32v203F6B6


0x0800000开始的CODE FALSH掉电不丢失,把数据放在程序后面就行,我们的EVT例程有操作FLASH的image.png


void Flash_Test(void) {

    printf("FLASH Test\n");

    /*When the main frequency exceeds 100MHz, attention should be paid when

     *operating FLASH: dividing HCLK by two will result in the related peripheral

     *clock of HCLK being divided by two. Attention should be paid when using.

     */

    RCC->CFGR0 |= (uint32_t) RCC_HPRE_DIV2; //将系统时钟的主频分频为2

    __disable_irq();

    USART_Printf_Init(115200);


    FLASH_Unlock();


    NbrOfPage = (PAGE_WRITE_END_ADDR - PAGE_WRITE_START_ADDR) / FLASH_PAGE_SIZE;


    FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_WRPRTERR); //用于清除闪存操作标志位


    for (EraseCounter = 0;

            (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE);

            EraseCounter++) {

        FLASHStatus = FLASH_ErasePage(

                PAGE_WRITE_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter)); //Erase 4KB


        if (FLASHStatus != FLASH_COMPLETE) {

            printf("FLASH Erase Fail\r\n");

        }

        printf("FLASH Erase Suc\r\n");

    }


    Address = PAGE_WRITE_START_ADDR;

    printf("Programing...\r\n");

    while((Address < PAGE_WRITE_END_ADDR) && (FLASHStatus == FLASH_COMPLETE))

    {

        FLASHStatus = FLASH_ProgramOptionByteData(Address, Data);

        Address = Address + 1;

        printf("Programing...\r\n");

    }


    Address = PAGE_WRITE_START_ADDR;


    printf("Program Cheking...\r\n");

    while((Address < PAGE_WRITE_END_ADDR) && (MemoryProgramStatus != FAILED))

    {

        if((*(__IO uint8_t*) Address) != Data)

        {

            MemoryProgramStatus = FAILED;

        }

        Address += 2;

    }


    if (MemoryProgramStatus == FAILED) {

        printf("Memory Program FAIL!\r\n");

    } else {

        printf("Memory Program PASS!\r\n");

    }


    FLASH_Lock();


    RCC->CFGR0 &= ~(uint32_t) RCC_HPRE_DIV2;

    __enable_irq();

    USART_Printf_Init(115200);

    printf("%08x \r\n", *(u32*) (FAST_FLASH_PROGRAM_START_ADDR));

}

例程是半字的写 我改成一字节写入 修改了写的数据类型为uint8  写入函数   还要改别的吗这是修改完的 但是提示写入失败  帮忙看一下谢谢啦


FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);你那个是编程用户字的函数,地址是固定的,请替换这个函数试一下


不能存uint8的数据吗


不支持单个字节编程,但是可以把数据强制转换再写进去,要是数组的话,把数组类型强制转换一下就行。根据使用半字或者字编程转换


明白谢谢


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