这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 行业应用 » 汽车电子 » 【S32K3XX】操作寄存器对flash编程

共1条 1/1 1 跳转至

【S32K3XX】操作寄存器对flash编程

高工
2026-01-24 19:42:30     打赏

【简介】

             在之前帖子(【S32K3XX】Flash驱动使用)介绍了S32K3芯片的 C40 IP 的flash驱动的适配方法,在RTD驱动的实现流程中讲述了寄存器的操作流程(【S32K3XX】Flash驱动擦除写入流程)该文章中对flash 的擦除写入的流程已经介绍的比较详细了。我们按照芯片手册的流程即可,以下是RM 手册上对擦除流程的说明。

【FLASH 擦除流程】

image.png

我们按照上述的流程编写如下的擦除代码。

void flash_erase(uint32_t address)
{
    /* 1. write address to pflash controler */
    IP_PFLASH->PFCPGM_PEADR_L = address;

    /* 2. One and only one DATA register must also be written */
    IP_FLASH->DATA[0] = 0x00000000UL;

    /* 3.Change the value in MCR[ERS] from a 0 to a 1, and at the same time select the size of erase using MCR[ESS]. */
    IP_FLASH->MCR |= FLASH_MCR_ERS_MASK;
    IP_FLASH->MCR &= ~FLASH_MCR_ESS_MASK;

    /* 4.Write a 1 to MCR[EHV] to start the internal erase sequence */
    IP_FLASH->MCR |= FLASH_MCR_EHV_MASK;

    /* 5.Wait until MCRS[DONE] becomes 1 */
    while (0U == (IP_FLASH->MCRS & FLASH_MCRS_DONE_MASK))
    {
        /* wait */
    }

    /* 6. Confirm MCRS[PEG] = 1 */
    while (0U == (IP_FLASH->MCRS & FLASH_MCRS_PEG_MASK))
    {
        /* wait */
    }

    /* 7. Write 0 to MCR[EHV]. */
    IP_FLASH->MCR &= ~FLASH_MCR_EHV_MASK;

    /* 8. Write 0 to MCR[ERS] to terminate the erase, and if set MCR[ESS] should also be cleared (else the field auto clears) */
    IP_FLASH->MCR &= ~FLASH_MCR_ERS_MASK;
}

   编写如下代码,使用上述接口擦除pflash

        C40_Ip_StatusType t_ReturnValue = C40_IP_STATUS_ERROR;

        t_ReturnValue = C40_Ip_ClearLock(FLS_SECTOR_TEST, FLS_MASTER_ID);
        if(C40_IP_STATUS_SUCCESS != t_ReturnValue)
        {
            /* erase failed */
            PRINTF("Flash clear lock failed %x \r\n", t_ReturnValue);
        }
        else
        {
            PRINTF("Flash clear lock success \r\n");
        }

        flash_erase(0x500000);

        

运行上述代码后pflash 按照预期的被成功擦除。

image.png   

【FLASH 写入流程】

以下是PFLASH 的写入流程

image.png

按照上述的流程我们编写如下的代码写入数据。

void flash_write(uint32_t address,uint32_t * data)
{
    /* 1. Write the address to be programmed using logical address registers located in the PFC. The result of this write will also
          be reflected physically in the PEADR register. */
    IP_PFLASH->PFCPGM_PEADR_L = address;

    /*2. Data to be programmed must be written in the appropriate DATAX register (where X is 0 to 31). All unwritten data words
         default to FFFF_FFFFh */
    IP_FLASH->DATA[0] = data[0];
    IP_FLASH->DATA[1] = data[1];

    /* 3. Change the value in MCR[PGM] from a 0 to a 1 to initiate the program operation. */
    IP_FLASH->MCR |= FLASH_MCR_PGM_MASK;

    /* 4. Write a 1 to MCR[EHV] to start the internal program sequence. */
    IP_FLASH->MCR |= FLASH_MCR_EHV_MASK;

    /* 5. Wait until MCRS[DONE] becomes 1 */
    while (0U == (IP_FLASH->MCRS & FLASH_MCRS_DONE_MASK))
    {
        /* wait */
    }

    /* 6. Confirm MCRS[PEG] = 1 */
    while (0U == (IP_FLASH->MCRS & FLASH_MCRS_PEG_MASK))
    {
        /* wait */
    }

    /* 7. Write 0 to MCR[EHV]. */
    IP_FLASH->MCR &= ~FLASH_MCR_EHV_MASK;

    /* 8. Write 0 to MCR[PGM] to terminate the program operation. */
    IP_FLASH->MCR &= ~FLASH_MCR_PGM_MASK;
}

编写如下的测试代码验证写flash 接口

        const uint32_t wData[2] = {0x12345678,0x9ABCDEF0};

        flash_write(0x500000,(uint32_t *)wData);

上述代码运行后按照预期的更新flash.

image.png




共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]