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

共1条 1/1 1 跳转至

【S32K146】S32K146片内flash编程

高工
2025-06-13 06:58:56     打赏

【简介】

S32K146 的Flash Memory Module (FTFC) 包含P-flash/FlexNVM/FlexRAM 三种类型,其中P-flash 是我们存储代码的介质,FlexNVM 可以配置为存储代码数据或者模拟EEPROM使用,FlexRAM 可以作为RAM使用当FlexNVM  配置为EEPROM FlexRAM回加载EEPROM  数据至FlexRAM 供用户读写EEPROM使用,对应芯片手册描述如下:

image.png

image.png

image.png

【S32DS FLASH配置】

S32DS的flash 驱动程序配置也是很简单只是配置P-flash/FlexNVM/FlexRAM 的地址信息。

image.png

生成的配置结构体内容如下:

/***********************************************************************************************************************
 * This file was generated by the S32 Configuration Tools. Any manual edits made to this file
 * will be overwritten if the respective S32 Configuration Tools is used to update this file.
 **********************************************************************************************************************/

/* clang-format off */
/* TEXT BELOW IS USED AS SETTING FOR TOOLS *************************************
!!GlobalInfo
product: Peripherals v14.0
processor: S32K146
package_id: S32K146_LQFP144
mcu_data: s32sdk_s32k1xx_rtm_401
processor_version: 0.0.0
functionalGroups:
- name: BOARD_InitPeripherals
  UUID: 2d0c0af7-dbe7-4f00-821a-837510d6ab4e
  called_from_default_init: true
  selectedCore: core0
 * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS **********/
/* clang-format on */

/*******************************************************************************
 * Included files 
 ******************************************************************************/
#include "peripherals_flash_1.h"

/*******************************************************************************
 * flash_1 initialization code
 ******************************************************************************/
/* clang-format off */
/* TEXT BELOW IS USED AS SETTING FOR TOOLS *************************************
instance:
- name: 'flash_1'
- type: 'flash'
- mode: 'general'
- custom_name_enabled: 'false'
- type_id: 'flash'
- functional_group: 'BOARD_InitPeripherals'
- peripheral: 'FTFC'
- config_sets:
  - flash_driver:
    - flashConfig:
      - 0:
        - Configuration: 'Flash_InitConfig0'
        - readOnly: 'true'
        - PFlashBase: '0x00000000'
        - PFlashSize: '0x100000'
        - DFlashBase: '0x10000000'
        - EERAMBase: '0x14000000'
        - CallBack: 'NULL_CALLBACK'
    - quick_selection: 'flash_default'
 * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS **********/
/* clang-format on */

/**
 * @page misra_violations MISRA-C:2012 violations
 *
 * @section [global]
 * Violates MISRA 2012 Advisory Rule 8.7, External variable could be made static.
 * The external variables will be used in other source files in application code.
 *
 */

    /* Flash user configuration 0 */
const flash_user_config_t Flash_InitConfig0 =
{
    .PFlashBase = 0x0U,
    .PFlashSize = 0x100000U,
    .DFlashBase = 0x10000000U,
    .EERAMBase = 0x14000000U,
    .CallBack = NULL_CALLBACK
};

【FTFC控制指令】

FTFC 对flash 的编程通过FTFC的Flash Common Command Object Registers (FCCOB0 - FCCOBB) 寄存器控制,通过该寄存器组传递操作命令,对应的控制格式如下:

image.png

FCCOB0  代表控制命令,FCCOB01-FCCOB3发送操作的地址信息。

对应command sdk 代码中定义如下

/*******************************************************************************
* Flash hardware algorithm operation commands
*******************************************************************************/
#define FTFx_VERIFY_BLOCK             0x00U
#define FTFx_VERIFY_SECTION           0x01U
#define FTFx_PROGRAM_CHECK            0x02U
#define FTFx_READ_RESOURCE            0x03U
#define FTFx_PROGRAM_LONGWORD         0x06U
#define FTFx_PROGRAM_PHRASE           0x07U
#define FTFx_ERASE_BLOCK              0x08U
#define FTFx_ERASE_SECTOR             0x09U
#define FTFx_PROGRAM_SECTION          0x0BU
#define FTFx_VERIFY_ALL_BLOCK         0x40U
#define FTFx_READ_ONCE                0x41U
#define FTFx_PROGRAM_ONCE             0x43U
#define FTFx_ERASE_ALL_BLOCK          0x44U
#define FTFx_SECURITY_BY_PASS         0x45U
#define FTFx_PFLASH_SWAP              0x46U
#define FTFx_ERASE_ALL_BLOCK_UNSECURE 0x49U
#define FTFx_PROGRAM_PARTITION        0x80U
#define FTFx_SET_EERAM                0x81U


image.png


以下是FLASH_DRV_EraseSector 函数中擦除sector 代码的操作command

image.png

以下是FLASH_DRV_Program 函数中对flash 进行写操作的command 序列

image.png

【代码验证】

编写如下测试代码对flash 进行写入擦除实验

#include "sdk_project_config.h"
#include <stdio.h>
#include <string.h>
#include "func_init.h"

/********************************************************************************************************
 *                                 Private Macro definition                                             *
 *******************************************************************************************************/
#define DBG_ENABLE
#define DBG_TAG                         "FLS"
#define DBG_LEVEL                       DBG_LOG
#define DBG_COLOR
#include <rtdbg.h>

/********************************************************************************************************
 *                                  Private Variable Definitions                                        *
 *******************************************************************************************************/
flash_ssd_config_t Pss_InitConfig0;

/********************************************************************************************************
 *                                  Global Function Declarations                                        *
 *******************************************************************************************************/

static int chip_flash_init(void)
{
    status_t ret;
    /* Init lpi2c master */
    ret = FLASH_DRV_Init(&Flash_InitConfig0,&Pss_InitConfig0);

    if(ret != STATUS_SUCCESS)
    {
        LOG_E("FTFC init failed %x.",ret);
    }
    else
    {
        LOG_I("FTFC init OK.");
    }

    return 1;
}

INIT_BOARD_EXPORT_LEVEL1(chip_flash_init);

/********************************************************************************************************
 *                                  Private Function Declarations                                       *
 *******************************************************************************************************/
unsigned int  flash(char argc,char *argv[])
{
    status_t ret;
    if(argc != 2)
        return 1;

    if(strcmp("write",argv[1]) == 0)
    {
        ret = FLASH_DRV_Program(&Pss_InitConfig0,0x80000,16,"0123356789abcdef");
        if(ret != STATUS_SUCCESS)
        {
            LOG_E("FTFC write failed %x.",ret);
        }
        else
        {
            LOG_I("FTFC write OK.");
        }
    }

    if(strcmp("erase",argv[1]) == 0)
    {
         ret = FLASH_DRV_EraseSector(&Pss_InitConfig0,0x80000,4096);
         if(ret != STATUS_SUCCESS)
        {
            LOG_E("FTFC erase failed %x.",ret);
        }
        else
        {
            LOG_I("FTFC erase OK.");
        }
    }
    return 0;
}

flash write 后通过IAR 的memory 窗口查看0x80000地址的数据内容已经按照预期的写入

image.png

flash erase 后后通过IAR 的memory 窗口查看0x80000地址的数据内容 发现已经被擦除

image.png

使用S32K1XX的SDK代码很容易就可以实现片内flash 的写入擦除处理。




共1条 1/1 1 跳转至

回复

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