FR801x系列芯片Flash添加写保护方法



  • 问题描述:
    FR801x系列芯片在使用过程中,由于断电不彻底(断电后VBAT电压没有到0V)、断电到上电时间小于10ms,导致芯片Flash可能会被异常修改的问题。

    问题解决方式:
    1、 建议在烧写时,烧写工具勾选“写保护”,该操作对程序所对应Flash区域进行写保护操作,
    烧写工具:
    链接:https://pan.baidu.com/s/1fAaTMjRb-HQ1mT3QItd1_w
    提取码:1p3y

    2、对整片Flash进行写保护操作,参考如下代码:
    #define FLASH_WRITE_DISABLE_OPCODE 0x04
    #define FLASH_WRITE_ENABLE_OPCODE 0x06
    #define FLASH_WRITE_STATUS_REG_OPCODE 0x01
    #define FLASH_READ_STATUS_REG_OPCODE 0x05

    static void flash_write_enable(void)
    {
    ssp_put_byte(FLASH_WRITE_ENABLE_OPCODE);
    ssp_enable();
    ssp_wait_busy_bit();
    ssp_disable();
    }

    static void flash_write_disable(void)
    {
    ssp_put_byte(FLASH_WRITE_DISABLE_OPCODE);
    ssp_enable();
    ssp_wait_busy_bit();
    ssp_disable();
    }

    static uint8_t flash_read_status_reg(void)
    {
    uint8_t buffer[2] = {0x00, 0x00};
    ssp_put_byte(FLASH_READ_STATUS_REG_OPCODE);
    ssp_put_byte(0xff);
    ssp_enable();
    ssp_get_data(buffer, 2);
    ssp_wait_busy_bit();
    ssp_disable();
    return buffer[1];
    }

    static void flash_write_status_reg(uint8_t status)
    {
    ssp_put_byte(FLASH_WRITE_STATUS_REG_OPCODE);
    ssp_put_byte(status);
    ssp_enable();
    ssp_wait_busy_bit();
    ssp_disable();
    }

    static void flash_start_protection(void)
    {
    uint32_t flash_id;
    uint8_t flash_status;
    flash_id = flash_init();
    flash_status = flash_read_status_reg();
    flash_status &= 0x83;

    if((flash_id & 0xFF) == 0x85) { //PUYA
    flash_status |= 0x2C; // protect lower 256K for Q40H Q20H D21H
    }
    else { //XTX
    flash_status |= 0x0C; // protect lower 256K for XT25F02D
    }
    flash_write_enable();
    flash_write_status_reg(flash_status);
    flash_write_disable();
    while(flash_read_status_reg()&0x03) {
    //delay
    co_delay_100us(20);
    }
    printf("flash_start_protection *************\r\n ");
    }
    static void flash_clear_protection(void)
    {
    uint8_t status;
    status = flash_read_status_reg();
    //status &= 0x83;
    flash_write_enable();
    flash_write_status_reg(0);
    flash_write_disable();
    ssp_clear_rx_fifo();
    while(flash_read_status_reg()&0x03) {
    //delay
    co_delay_100us(20);
    }
    printf("flash_clear_protection *************\r\n ");
    }