Files
powertrain-build/docs/target_link/non_volatile_memory.md
Henrik Wahlqvist 1f388f1d2d Add docs about TargetLink
Change-Id: I027ec3815c741022d3860820e8ac8c521506c1c8
2024-10-04 10:00:48 +02:00

8.2 KiB

Overview NVM


[TOC]

Non-volatile memory (NVM) is used when data needs to be stored long-term persistently. Examples of NVM variables are different types of adaption values for actuators/sensors or specific functions, e.g. for monitoring purposes which need to follow the behavior of the software/functionality over the lifecycle of the vehicle.

There are different concepts for storing NVM variables. One way is to save the data to random-access memory (RAM) during the execution of the application software and then during the afterun (end of the driving cycle), save this RAM-mirror to a specific area in the flash memory. Another way is to allocate a specific area, which is supplied over the vehicle battery, in RAM to which the parameters are allocated.

At initialization this area is then read back into RAM-mirror and made available to the application software.

To utilize NVM in powertrain_build, a special Simulink block needs to be created, which should be able to read, write and reset the NVM-parameter.

The block should contain a combination of TL_DataStore* and TL_UnitDelay blocks.

The code for finding these blocks can be seen in powertrain-build/matlab_scripts/parseNVM.m.

These blocks must use the CVC_DISP_NVM* variable class.

There is one class for normal NVM handling and one for persistent data.

nvm_block_example

NVM Handling in powertrain-build

powertrain-build collects the NVM blocks from the models. The NVM variables are matched against a specific json file, called Projects/<PROJECT_NAME>/conf.local/nvm_structs.json. This list is updated automatically after running the powertrain-build "build" command. However, the generated file needs to manually be moved to the conf.local folder.

The NVM-area is divided into six structs.

nvm_concept

powertrain-build currently assumes that the order of these variables needs to be kept inbetween iterations of the software. Therefore, powertrain-build automatically adds new parameters at the end of the relevant NVM struct. If a parameter needs to be removed, it is replaced by a dummy parameter. This assures that the backward compatibility always is meet.

Below is an example of the contents of a nvm_structs.json file.

[
    {
        "name": "NVM_LIST_CRITICAL2",
        "allowed_datatypes": [
            "NotApplicable"
        ],
        "size": 16,
        "instanceName": "nvm_list_critical2",
        "default_datatype": "UInt16",
        "includeStop": "",
        "includeStart": "",
        "persistent": false,
        "signals": [
            {
                "type": "UInt16",
                "x_size": 1,
                "name": "sVcModelName_D_DummyOne",
                "y_size": 1
            }
        ]
    },
    {
        "name": "NVM_LIST_CRITICAL1",
        "allowed_datatypes": [
            "NotApplicable"
        ],
        "size": 16,
        "instanceName": "nvm_list_critical1",
        "default_datatype": "UInt16",
        "includeStop": "",
        "includeStart": "",
        "persistent": false,
        "signals": [
            {
                "type": "UInt16",
                "x_size": 1,
                "name": "sVcModelName_D_DummyTwo",
                "y_size": 1
            }
        ]
    },
    {
        "name": "NVM_LIST_8",
        "allowed_datatypes": [
            "Bool",
            "UInt8",
            "Int8"
        ],
        "size": 130,
        "instanceName": "nvm_list_8",
        "default_datatype": "UInt8",
        "includeStop": "",
        "includeStart": "",
        "persistent": false,
        "signals": [
            {
                "type": "UInt8",
                "x_size": 1,
                "name": "Pos_8bit_0",
                "y_size": 1
            },
            {
                "type": "UInt8",
                "x_size": 1,
                "name": "sVcModelName_D_DummyThree",
                "y_size": 1
            }
        ]
    },
    {
        "name": "NVM_LIST_16",
        "allowed_datatypes": [
            "UInt16",
            "Int16"
        ],
        "size": 768,
        "instanceName": "nvm_list_16",
        "default_datatype": "UInt16",
        "includeStop": "",
        "includeStart": "",
        "persistent": false,
        "signals": [
            {
                "type": "UInt16",
                "x_size": 7,
                "name": "sVcModelName_D_DummyFour",
                "y_size": 1
            }
        ]
    },
    {
        "name": "NVM_LIST_32",
        "allowed_datatypes": [
            "Float32",
            "UInt32",
            "Int32"
        ],
        "size": 896,
        "instanceName": "nvm_list_32",
        "default_datatype": "UInt32",
        "includeStop": "",
        "includeStart": "",
        "persistent": false,
        "signals": [
            {
                "type": "UInt32",
                "x_size": 9,
                "name": "sVcModelName_D_DummyFive",
                "y_size": 1
            }
        ]
    },
    {
        "name": "NVM_LIST_8_PER",
        "allowed_datatypes": [
            "Bool",
            "UInt8",
            "Int8"
        ],
        "size": 40,
        "instanceName": "nvm_list_8_per",
        "default_datatype": "UInt8",
        "includeStop": "",
        "includeStart": "",
        "persistent": true,
        "signals": [
            {
                "name": "sVcModelName_D_DummySix",
                "type": "Bool",
                "x_size": 1,
                "y_size": 1
            },
            {
                "name": "sVcModelName_D_DummySeven",
                "type": "Bool",
                "x_size": 1,
                "y_size": 1
            }
        ]
    },
    {
        "name": "NVM_LIST_16_PER",
        "allowed_datatypes": [
            "UInt16",
            "Int16"
        ],
        "size": 119,
        "instanceName": "nvm_list_16_per",
        "default_datatype": "UInt16",
        "includeStop": "",
        "includeStart": "",
        "persistent": true,
        "signals": [
            {
                "name": "sVcModelName_D_DummyEight",
                "type": "UInt16",
                "x_size": 1,
                "y_size": 1
            }
        ]
    },
    {
        "name": "NVM_LIST_32_PER",
        "allowed_datatypes": [
            "Float32",
            "UInt32",
            "Int32"
        ],
        "size": 112,
        "instanceName": "nvm_list_32_per",
        "default_datatype": "UInt32",
        "includeStop": "",
        "includeStart": "",
        "persistent": true,
        "signals": [
            {
                "name": "sVcModelName_D_DummyNine",
                "type": "UInt32",
                "x_size": 1,
                "y_size": 1
            }
        ]
    }
]

Critical NVM Parameters

If there is a possibility to write an NVM parameter directly to flash. This is useful e.g. for safety critical parameters that shall not be lost if a reset occurs. Two specific NVM struct areas called nvm_list_critical1/2 can be added. These areas are non-persistent and holds all the datatypes (4,2,1 bytes). The two areas are needed two give higher integraty of the writing/read of the parameters.

Generated Files

powertrain-build generates C code definings structs for each area. See small examples below.

vcc_nvm_struct.h

/*
 *  vcc_nvm_struct.h - struct for NVM signals
 */

#ifndef VCC_NVM_STRUCT_H
#define VCC_NVM_STRUCT_H

#include "tl_basetypes.h"
#include "CVC_NVM_START.h"
struct NVM_LIST_8 {
   UInt8 unused[130];
}; /* 0 bytes used of 130 */
#include "CVC_NVM_END.h"

#include "CVC_NVM_START.h"
struct NVM_LIST_32 {
   Float32 _sVcModelName_D_DummyOne;
   UInt32 unused[895];
}; /* 4 bytes used of 3584 */
#include "CVC_NVM_END.h"

#include "PREDECL_START.h"
extern struct NVM_LIST_8 nvm_list_8;
extern struct NVM_LIST_32 nvm_list_32;
#include "PREDECL_END.h"


#define sVcModelName_D_DummyOne         nvm_list_32._sVcModelName_D_DummyOne

#endif /* VCC_NVM_STRUCT_H */

vcc_nvm_struct.c

#include "vcc_nvm_struct.h"

#include "CVC_NVM_START.h"
struct NVM_LIST_8 nvm_list_8;
#include "CVC_NVM_END.h"

#include "CVC_NVM_START.h"
struct NVM_LIST_32 nvm_list_32;
#include "CVC_NVM_END.h"