NVRAM Manager in AUTOSAR || Part -2


In Part -1 of NVRAM manager (NvM) you have understood working principle of NVRAM manager  Basic storage object. After reading this article you will understand :

1. Services provided by NvM module. 
2  Read block and Write block mechanism.
3. Read all and Write all  functionality.
4. Error Detection mechanism.
5. Error recovery (Implicit and Explicit ) methods.
7. Write Protection mechanism.
8. Resistant to changed software.
9. RAM block and NV block synchronization (Implicit and Explicit) methods.
10. Service ports provided by NvM module.
11. Accessing NVM using RTE
12. Important Configuration Parameter of NvM module in AUTOSAR

1. Introduction :

NvM module is present in service layer of AUTOSAR and provides services to Software Components (SWCs) regarding  Non -Volatile (NV) data storage (e.g. read and write service) and maintenance. NvM module administrates NV data of EEPROM and FLASH EEPROM module. NV RAM manager is only module, using which NV memory can be accessed. 

In order to configure memory stack you should understand functionality of NV RAM manager.
What does NV RAM do :

1. Provides Services to SWCs and BSW modules to read/write memory.
2. Performs Error detection of NV memory and  Error recovery( Implicit or Explicit).
3. Validation of RAM block data.
4. Provides multi-block read and write request.
5. Performs write verification.
In below section functionalities of  NVRAM manager are described.

2.  Functionalities of NvM Module :

This section describes important functionalities of NVRAM module.

2.1 RAM Block States : 

Depending on different API calls, status of RAM block is changed. NVRAM manager changes states of RAM blocks and these states are used while reading or writing. Status of each RAM block is stored in Administrative blocks. Three states of RAM block are :
1.Valid Unchanged  2.Valid Changed  3. Invalid Unchanged.

1. Valid Unchanged :

This state shows that content of RAM blocks are not changed they are same as NV memory. To enter into this state read/write for single block or multi block request has to be successful (i.e. NvM_ReadBlock() / NvM_ReadAll() or NvM_WriteBlock() / NvM_WriteAll() state changes to Valid Unchanged ).

2. Valid Changed : 

This state shows that content of RAM block is changed. 
  • If NvM_SetRamBlockStatus called with TRUE for the block or
  • If Write all or write block request are ongoing or
  • If NvM_RestoreBlockDefaults called for the block finishes successfully then state of RAM block is changed to Valid changed.
  • Read all or read block gives default data
3. Invalid Unchanged :

This implies that state of RAM block is invalid. After initialization state of RAM block is Invalid UnchangedTo enter the INVALID / UNCHANGED state, at least one of the following must occur:
  • NvM_SetRamBlockStatus called with FALSE for the block
  • Read block/Read all/Write block/Write all operation is unsuccessful 
  • NvM_EraseNvBlock finished successfully for the block
  • NvM_InvalidateNvBlock finished successfully for the block

2.2 Read Block and Read All :

Read block and read all requests are Asynchronous request. Read block function reads a single block (i.e. particular NV block). Reads data from a NV block and updates temporary RAM block. NvM_ReadBlock() function is used for this purpose, NvM_ReadBlock() function will copy data from NV memory to temporary RAM block.

NvM_ReadPRAMBlock() function will copy data from NV block to permanent RAM block

Read all function will read all NV block at a time. NvM_ReadAll() reads data from NV block and loads into RAM block, if there is error while reading a block i.e. content of RAM block does not matches with NV block i.e. CRC error and Read from NV fails(Invalid Unchanged), default data is loaded into RAM block and if block is of redundant type then read from redundant block is performed and if this fails then default value is loaded to RAM block. NvM_ReadAll() function is used at time of initialization of ECU, and called by ECU state manager.

After read block or read all request, if request  is successful state of RAM block is changed to Valid Unchanged. 

2.2.2 Static ID check : 

When NV block is written to NV memory an ID i.e. static block ID is also written to NV memory. Static ID is stored into block header. This ID is checked against requested block ID. e.g if you have written a Block with ID 10 in memory, and when you requested to read block with ID 10, so requested ID is compared against stored block ID, to detect an error. If wrong block ID error is detected then error NVM_E_WRONG_BLOCK_ID is reported to DEM. 

Static ID check can be enabled using configuration parameter. If static ID check failed for RAM block, status of RAM block changed to Invalid Unchanged.

2.2.3 Read Retries :

Read Retries is performed when there is error while reading NV data. i.e. NVRAM manager will try to read NV data again, NV RAM manager will perform read retries as per the value of configuration parameter NVM_MAX_NUM_OF_READ_RETRIES. e.g. if read retry value is 2 then NvM will read block 2nd time if there is error in reading block 1st time.

After read retries if there is still error then NVRAM manager either read ROM block or read redundant NV Block (if configured).

Read retries will be performed under following condition :
1. If NVRAM manager detects  CRC error
2. If NVRAM manager detects Static Block ID check

2.3 Write Block and Write All:

Write block and write all requests are Asynchronous request. Write block functions writes a single block into NV memory. To write a block NvM_WriteBlock() function is used. This function will copy data from temporary RAM block to ROM block. NvM_WritePRAMBlock() will write data from Permanent RAM block to NV block. 

Write all will write all the blocks at a time. NvM_WriteAll() function, will calculates CRC at a time of writing, if  CRC is matches and RAM block status is valid then the corresponding block will not be written into NV memory. For permanent RAM block : state can be changed by API NvM_SetRamBlockStatus() to VALID_CHANGED and can be written at time of NvM_WriteAll().

If Write Block and NvM_WriteAll  is successful then RAM block state changed to Valid Unchanged and during  Write Block and NvM_WriteAll request processing RAM block status is Valid Changed.

2.3.1 Write Protection :

If write protection is enabled, RAM block can be modified but NV block can not be modified. Write protection is performed by configuring parameter NvMBlockWriteProt = TRUE. 

NvM_SetBlockProtection() function can be use to enable/disable write protection (explicit mechanism), if NvMWriteBlockOnce is FALSE (regardless of value of NvMBlockWriteProt).

If NvMWriteBlockOnce is True then enabling/disabling is not possible using NvM_SetBlockProtection() function validation of RAM block

2.3.2 Write Verification : 

When a RAM Block is written to NV memory the NV block shall be immediately read back and compared with the original content in RAM Block. Write Verification can be configured using parameter NvMWriteVerification.

If the original content in RAM Block is not the same as read back then the production code error NVM_E_VERIFY_FAILED shall be reported to DEM.

If write retries is configured and if  write verification fails then NVRAM manager will try to write data again. Write retries can be configured by parameter NvMMaxNumOfWriteRetries.  NVM_E_REQ_FAILED error will occur if  max number of retries tried.

2.4 Error Detection NVRAM Block Data Consistency check :

Error is detected by CRC comparison mechanism. The NvM module internally uses CRC generation routines (8/16/32 bit) to check and to generate CRC for NVRAM blocks as a configurable option. If CRC of RAM block and CRC of ROM block does not match then error occurs.  Data consistency is checked by using CRC calculation mechanism. Implicit way is to enable configuration parameter NvMBlockUseCrc and NvMCalcRamBlockCrc

1. CRC is calculated if NvM_SetRamBlockStatus() is called and NvMCalcRamBlockCrc == TRUE.
2. CRC will be calculated in backround by NvM_Main() function.
3. CRC is calculated after NvM_ReadBlock() is processed and NvMCalcRamBlockCrc == TRUE.
4. CRC is calculated when NvM_WriteBlock() and NvM_WriteAll() requests CRC calculation before writing into NV block.
5. CRC is calculated after NvM_RestoreBlockDefaults(), crc of RAM block is calculated.
6. CRC is calculated during NvM_ReadAll().

If CRC mismatch occurs then Integrity fail error occurs after CRC mismatch.

2.5 Error Recovery :

Error recovery shall  provided for read NV data or for Write NV data. Default values are loaded to RAM block from ROM block if there is error while reading data.

Write retries is type of error recovery if there is error while writing data i.e. content of RAM block are not same as what is written.

Recovery of RAM data with ROM Data (Implicit) :

Implicit recovery provided while startup ( at time of NvM_ReadAll() )under condition
1. ROM block is configured and
2. CRC mismatch of RAM block and ROM block and
3. Read attempt from NV fails.

Recovery of RAM data with ROM Data (Explicit) :

Explicit recoveryry is performed by calling APIs NvM_RestoreBlockDefaults() or NvM_RestorePRAMBlockDefaults(). These APIs are called by application SWC if application detects error at run time.

2.5 Initialization and Shutdown of NVRAM Module :

NvM is initialised by ECU state manager by calling NvM_Init() function, but this function does not contains initialization of RAM Blocks. Ram blocks are initialized by function NvM_ReadAll(). NvMReadAll()  will copy data from NV block to RAM block and initializes them. Shutdown is performed by calling Nvm_WriteAll(), this function will write data from RAM block to NV block.

2.6 Resistance to SW Change :

In this section we will discuss about two configuration parameters :
NvMDynamicConfiguration and NvMResistantToChangedSw.

Consider you have already configured 4 NV BLOCKs, data is written to them. Now you have changed the block size of one of the block or new block is added, due to which IDs of other blocks got changed this will leads to wrong data read at time of read all process.

for such case you can have an option not to initialize RAM block with NV block data this can be done by setting parameter NvMDynamicConfiguration = TRUE and change of configuration has to notified by configuration parameter NvmCompiledConfigID

What is NvmCompiledConfigID ? this is a configuration parameter, stored in separate NV block and at startup value of NvmCompiledConfigID is compared with value stored in NV block, if value compared is different then default values are loaded to RAM block and at write all new values are written to NV block, so in next startup cycle new values will be available to RAM block if 

NvMResistantToChangedSw parameter set to TRUE and 

NvMResistantToChangedSw parameter set to false then default values are loaded to RAM block irrespective of validity of RAM block.

In short :

1. Consider NV blocks contains old data and NvMDynamicConfiguration = TRUE
2. Configuration is changed.
3. At read all NvmCompiledConfigID comparison shows configuration is changed.
4. If NvMResistantToChangedSw parameter set to TRUE, then content from NV blocks will be loaded to RAM block which will cause inconstancy (as data from old blocks will get written into new blocks).
5. If NvMResistantToChangedSw parameter set to FALSE, then at time of  Read All, defaults values will be loaded to RAM block.

So if you are going to change configuration better to set NvMResistantToChangedSw parameter to FALSE.

2.7 Job End and Job Error Notification :

When read data from NVM is finished or write data from NVM is finished with status ok, the underlying memory abstraction to signal end of job without error. NvM_JobEndNotification() is getting called. Similarly  NvM_JobErrorNotification() is getting called if read data from NVM is aborted with error or write data from NVM is aborted with error.

2.8 Immediate Data :

You can configure block to store data immediately i.e. when NvM_Write is requested data should be written in NV memory immediately, then you can enable configuration  parameter immediate write ON. 

Generally if immediate data is disabled, and NV data write will be done at when write all is in processing i.e. at shutdown time. Till then if data is changed it will be stored in RAM blocks, and  write all will call single block write and writes data into NV memory if data is changed.

3. NV data accessing :

NVRAM Basic working principle and basic storage objects i.e. RAM Block, ROM Block and NV Block  and NV data is read from ROM block to RAM block and while writing data is copied to NV block from RAM block. Application SWC reads or Writes RAM block to read NV data and to write NV data respectively.

Also there is RAM block and RAM Mirror, difference between them will be get clear in this section. There is data transfer between NV block and application SWC using RAM block, this data transfer is either explicit or implicit.

In implicit data from RAM block is copied to NV Block and from NV block to RAM block, once application SWC request for Read or write data. While in case of explicit communication NV module need to use APIs to read/write data from RAM block, once application SWC request for Read or write data

3. 1 Implicit data transfer between NV block and RAM block :

In implicit communication  RAM block is used. Application SWC and NV block exchanges data using RAM block. RAM block can be permanent or temporary. Permanent RAM blocks are associated with one application SWC and permanent RAM block can be accessed by respective SWC only. Temporary ram blocks are created at run time i.e section of RAM is used.

Fig.4 Implicit Synchronization

3.1.1 Write Block Request  :

Write Operation : Application SWC fills RAM block and content of RAM blocks are copied into NV block. Application SWC has to follow below rules while write request.

1. Application  fills RAM block using APIs NvM_WriteBlock()/NvM_WritePRAMBlock().
2. Now request is transferred to NvM Module and till then  application SWC should not modify RAM block (to maintain data consistency).
3. By polling method or by call-back notification (job end notification) application SWC gets status of request (write operation successful or failed), then after application can use RAM block again.

3.1.2 Read Block Request : 

Read operation : NV blocks updates RAM block and application SWC reads RAM block. Application SWC has to follow below rules while read request.

1. To read NV data, application SWC need to  provide RAM block.
2. Use API NvM_ReadBlock() or NvM_ReadPRAMBlock() to read data from NV.
3. Status of read request can be read by polling mechanism or call back notification, till then application SWC should not read/write RAM block.
4. After success of request, application SWC can use RAM block data (Ram block contains data from NV block).

3.2 Explicit data transfer between NV block and RAM block :

Explicit synchronization RAM mirror is used to transfer data between RAM block and NV Block.  Application writes/reads data to /from RAM Mirror using  NV Block reads/writes RAM mirror and copies data to/from NV Block. 

Explicit mechanism can be configured per block by parameter NvMBlockUseSyncMechanism. 
NvMBlockUseSyncMechanism == TRUE.

Drawback of this method is extra RAM mirror is required of size of max RAM block. Advantage is RAM block can be controlled efficiently and data consistency is maintained better way. Only 1 RAM mirror is present irrespective of number of RAM blocks ( at 1 RAM has to be configured).

Fig.5 Explicit Synchronization

3.2.1 Write Data

Application SWC writes data to ram block then data is copied to RAM mirror and NV block reads data from RAM mirror. Application has to follow below points while write request:

1. Application SWC to use API NvM_WriteBlock() or NvM_WritePRAMBlock() to fill RAM block with data.
2. NvM calls routine NvMWriteRamBlockToNvM() to read data from RAM block. Once this routine is called application SWC has to provide consistent 
3. Application SWC can read/write RAM block as data is copied to RAM Mirror.
4. Status of write operation can be read by polling mechanism or by call back function.

3.2.2 Read Data

Application SWC readsNV data from RAM mirror (i.e.copies data from RAM mirror to RAM blcok) and RAM mirror is updated by NV block. Application has to follow below rules while read request:

1. Application SWC to use API NvM_ReadBlock() or NvM_ReadPRAMBlock() to read data from RAM block.
2. Application SWC to provide RAM block to be filled with data.
3. NvM module calls NvMReadRamBlockFromNvM() function, application SWC can copy data to RAM block from RAM mirror.
4.  Status of read operation can be read by polling mechanism or by call back function.

4. NvM Accessing by RTE:

To access NvM interfaces are provided by NVRAM manager are client server or NvDataInterface. Normal SWC can use client server interface and NvData interface is used with NvBlock SWC. Please refer image below.

Fig.6 Accessing NVRAM Manager

Fig. 6 shows Application SWC uses client server interface to access NVRAM manager, i.e. uses service ports provided by NVRAM manager. Also There is one more type NvBlock SWC, which can be accessed by multiple SWC using sender receiver and NvBlock SWC uses NVservice ports to use services provided by NVRAM manager. SWC can use Permanent RAM block or Temporary RAM bock and based on synchronization mechanism either ram mirror is used or not.

So ways to access NVRAM using SWC are :
1. Application SWC uses temporary RAM block with or without RAM mirror
2. Application SWC uses permanent RAM blocks with or without RAM mirror
3. NV SWC with RAM mirror only

A.  Client-server interface :

A client/server interface provides a number of operations that can be invoked on a server by a client. In case of services provided by NvM to the Application, NvM acts as server and Application acts as client. Also notifications to be provided to the Applications from NvM can be implemented using same; role of client and server is exchanged in this case.

B. NvDataInterface :

A non-volatile data interface defines a number of VariableDataPrototypes to be exchanged between non-volatile block components and atomic software components. These VariableDataPrototypes can be mapped to complete RAM block or elements of RAM block implemented inside non-volatile block components.

C.  Service Ports :

Below are the some service ports (with operations/services) provided by NVRAM manager. SWC can use one of them to access provided service by service to port.

1 NvM_Admin :

NvM_Admin provides service for setting/resetting the write protection for a NV Block. To perform operations related to write protection Application SWC can use SetBlockProtection() API . SetBlockProtection() operation provided by NvM_Admin service port.

2 NvM_Mirror :

If ram mirror is configured then application need to copy data to/from RAM Mirror, for that call-back functions are used ( WriteRamBlockToNvM and ReadRamBlockFromNvM ) and these functions are provided by NvM_Mirror service port.

3 NvM_NotifyInitBlock :

Callback is  called by NvM module when default data need to be restored in RAM blok.  InitBlock() is the callback called by NvM module when initialization of RAM block is completed i.e. after loading default data to RAM block. InitBlock() is called as there is no rom block is present to load default value.

4 NvM_NotifyJobfinished :

This service port provides call-back which is called when NvM job is finished ( i.e. after Read Block or Write Block operation is finished )

5 NvM_Service :

This is important service port. This service port provides many operations to Application SWC.

These operations/APIs are :
1. EraseBlock()
2. ReadBlock()
3. WriteBlock()
4. RestoreBlockDefaults()
5. GetDataIndex()
6. GetErrorStatus()
7. InvalidateNVBlock()
8. ReadPRAMBlock()
9. WritePRAMBlock()

5. Important Configuration Parameters:

To configure NVRAM manager you should never forget these important parameters :

1. Block Id.
2. Block Length.
3. Is block should be processed at Read all or write all time.
4. RAM block (Temporary / Permanent).
5. CRC calculation.
6. Write Protection.
7. Resistance to change SW.
8. Job end notification.
9. Enable Synchronization ( i.e explicit synchronization)
10. If enabled synchronization then get and set ram mirror callbacks.
11. To provide default values, use of ROM block.
12. Block type ( Native or redundant or Data set ).

Post a Comment

  1. In Figure 4 red block is supposed to illustrate NV Block Right?

  2. Hello DUZ, yes youfyou arenare correctmcorrect, thank you for noticing, I will update this soon.

  3. Hello Padamanabh, while reading all data using function NVM_ReadAll(), why it is considered as error if RAM block and NV block mismatches. After initialization if read happens NV will have data from NV data and RAM will default values. Why it has be considered as error.

  4. RAM block will be temporary only right. How can a RAM block can be Permanent?
    What does it mean to say Permanent here?Can you please answer

  5. Hello Sitaram, At time of read all content from NV memory is loaded to RAM block. Content from NV block i.e. CRC should match with content in RAM block i.e CRC of RAM data (which is loaded from NV block).

    So if content mismatch then it is considered as error i.e. CRC error.

  6. Hello Sitaram, Yes ram is temporary, Permanent ram block means a dedicate RAM memory (fixed addresd) is assigned to a block this dedicated RAM memory will be used to write data into NV block or read data from NV block.

    Temporary ram block means, run time section from ram is used (not dedicated memory address) to read or write data from or to NV memory.

    Please read https://www.embeddedtutor.com/2020/05/nvram-manager-in-autosar-part-1.html?m=1

  7. Hello Padmanabh, what happens when reading data from particular block before writing data into that NV block? will it load data from default ROM memory for that block.

  8. If you read data from NV block ( consider NV block has some data) then it will return that data. As at time of shutdown/previous cycle you may have stored data into NVM.

    If data is not been written to NV block and it is 1st power on of device....so there would be garbage value in NV block (CRC field aslo garbage)
    and at time of initialisation ReadAll operation is performed, and CRC of read data is compared against CRC in NV block (garbage value), as CRC mismatch occurs , default value is loaded.

  9. Can you please explain the difference between permanent ram block and temporary ram block ?

    1. Permanent RAM blocks are associated with one application SWC and permanent RAM block can be accessed by respective SWC only. Temporary ram blocks are created at run time i.e section of RAM is used

      You can configure permanent RAM block and give name to permanent RAM block while configuration NV block. If you have not configured any RAM block then temporary RAM address is selected run time to hold data read from NV block and to hold data to be written to NV block.

  10. Hi Padmanabh,
    How can we test the nvm block whether the data is storing or not after creating the block while configuration and after configuration?

    1. Hello Prasad, If you have configured NBM block and if RAM block is configured then after configuration you can check if RAM block contains same value which you have written at run time.

      Then if you have configured immediate write or write at shutdown then perform shutdown and after shutdown , wakeup the system.

      After wakeup please check RAM block contains same data which you have written in previous cycle. In this way you can verify.

      Hope this answered your query.
      Please let me know if you have further query.

  11. What is the purpose of Checksum() mechanism

  12. after read_all() finish its execution, data will be available in RAM block. now can we read this RAM block? is there any API ?

  13. Can you state the difference between "NvM_SetBlockProtection" and "NvM_SetBlockLockStatus" ? Thank you.

  14. How to determine if NvM_ReadAll() is completed?

  15. How to determine when NvM_ReadAll() is completed?

  16. Hello. Thank you for such helpful description. I have a question regarding state of RAM block. Is it possible to check the status of RAM block directly.? That means how can it be made sure that after NvM_ReadBlock() the RAM has succesfully changed the block status to Valid unchanged.?

  17. what is the diffrence between permanent RAM and temporary RAM in RAM block

  18. while trying to write the data using rte CS interface for a specific block synchronous write is working and I have use the do while loop till the block status is not set to idle from pending state.
    But this do while loop while writing the data cause reset if wdg is enable so if I remove the do while loop and call only rte write interface the status is always going in block pending and write doesn't finish, could you please suggest what should i do in order to write the data asynchronously ?

  19. Hi Padmanabh,
    In the 2.4 section, If the CRC of RAM block and NV block does not matches then error will occur right ?
    In the 2.5 section, The error recovery of point -2, There also CRC of RAM and NV Block right instead of ROM CRC ?

Post a Comment
To Top