Microchip® Advanced Software Framework

D_Nv.h File Reference

NV component header.

The segment D_NV_MEMORY must be defined and reserved in the linker script (.xcl file), for example like this:

-D_D_NV_MEMORY_START=400 -D_D_NV_MEMORY_SIZE=4000

-Z(CODE)D_NV_MEMORY+_D_NV_MEMORY_SIZE=[_D_NV_MEMORY_START:+_D_NV_MEMORY_SIZE]

Note that nothing should actually be placed in the segment. These linker script commands will ensure that the entire range is reserved and not programmed (left as 0xff).

The segment should start on a flash page boundary, and the size should correspond to the define D_NV_MEMORY_SIZE.

Further, since the device can only erase a whole page and not arbitrary locations in flash, the sector size must be an integer multiple of the flash page size. Otherwise, erasing the first or last page of a sector will also erase content in flash locations neighbouring the sector.

Copyright (c) 2018 Microchip Technology Inc. and its subsidiaries.

#include <compiler.h>

Macros

#define ADD_COMMA(value, n)   value,
 
#define ARRAY_SIZE(a)   (sizeof(a)/sizeof(a[0]))
 
#define CEIL(a, b)   (((a) - 1U)/(b) + 1U)
 
#define COMPARE_WITH_THRESHOLD(a, b, threshold)   (abs((a) - (b)) < (threshold) ? ((a) > (b) ? 1 : 0) : ((a) > (b) ? 0 : 1))
 
#define D_NV_FIRST_SECTOR   0
 
#define D_NV_MEMORY_SIZE   (D_NV_SECTOR_COUNT * D_NV_SECTOR_SIZE)
 
#define D_NV_SECTOR_COUNT   2
 
#define D_NV_SECTOR_SIZE   (8192U)
 
#define FIELD_OFFSET(struct_type, field_name)   (((uint8_t*) &((struct_type *)(NULL))->field_name) - (uint8_t*)NULL)
 
#define FIRST_BIT_CLEARED(bitmask)   (~(bitmask) & ((bitmask) + 1))
 
#define FIRST_BIT_SET(bitmask)   ((bitmask) & (~(bitmask) + 1))
 
#define FLASH_PTR
 
#define GET_CONST_PARENT_BY_FIELD(TYPE, FIELD, FIELD_POINTER)   ((const TYPE *)(((const uint8_t *)FIELD_POINTER) - offsetof(TYPE, FIELD)))
 
#define GET_FIELD_PTR(structPtr, typeName, fieldName)   ((uint8_t *)(structPtr) + offsetof(typeName, fieldName))
 
#define GET_INDEX_FROM_OFFSET(PTR1, PTR2)   (PTR1 - PTR2)
 
#define GET_PARENT_BY_FIELD(TYPE, FIELD, FIELD_POINTER)   ((TYPE *)(((uint8_t *)FIELD_POINTER) - offsetof(TYPE, FIELD)))
 
#define GET_STRUCT_BY_FIELD_POINTER(struct_type, field_name, field_pointer)   ((struct_type *) (((uint8_t *) field_pointer) - FIELD_OFFSET(struct_type, field_name)))
 
#define INIT_ARRAY(initValue, size)   {REPEAT_MACRO(ADD_COMMA, initValue, size)}
 
#define MAX(x, y)   ((x)<(y)?(y):(x))
 
#define MIN(x, y)   ((x)<(y)?(x):(y))
 
#define SLICE_SIZE(type, firstField, lastField)   (offsetof(type, lastField) - offsetof(type, firstField) + sizeof(((type *)0)->lastField))
 

Typedefs

typedef uint16_t D_Nv_Size_t
 

Functions

void D_Nv_EraseSector (uint8_t sector)
 Erases a sector of the internal NV. More...
 
bool D_Nv_IsEmpty (uint8_t sector, uint16_t offset, D_Nv_Size_t numberOfBytes)
 Checks if the requested range is empty (containing all 0xFF). More...
 
bool D_Nv_IsEqual (uint8_t sector, uint16_t offset, uint8_t *pBuffer, D_Nv_Size_t numberOfBytes)
 Compare bytes with contents of the internal NV. More...
 
void D_Nv_Read (uint8_t sector, uint16_t offset, uint8_t *pBuffer, D_Nv_Size_t numberOfBytes)
 Read bytes from the internal NV. More...
 
void D_Nv_SetSystemIntegrityCheckFunction (void(*pf)(void))
 The function to set system integrity check callback function. More...
 
void D_Nv_Write (uint8_t sector, uint16_t offset, uint8_t *pBuffer, D_Nv_Size_t numberOfBytes)
 Write bytes to the internal NV. More...
 

#define ADD_COMMA (   value,
 
)    value,
#define ARRAY_SIZE (   a)    (sizeof(a)/sizeof(a[0]))
#define CEIL (   a,
 
)    (((a) - 1U)/(b) + 1U)
#define COMPARE_WITH_THRESHOLD (   a,
  b,
  threshold 
)    (abs((a) - (b)) < (threshold) ? ((a) > (b) ? 1 : 0) : ((a) > (b) ? 0 : 1))
#define D_NV_FIRST_SECTOR   0
#define D_NV_MEMORY_SIZE   (D_NV_SECTOR_COUNT * D_NV_SECTOR_SIZE)

Referenced by D_Nv_Init().

#define D_NV_SECTOR_COUNT   2
#define D_NV_SECTOR_SIZE   (8192U)
#define FIELD_OFFSET (   struct_type,
  field_name 
)    (((uint8_t*) &((struct_type *)(NULL))->field_name) - (uint8_t*)NULL)
#define FIRST_BIT_CLEARED (   bitmask)    (~(bitmask) & ((bitmask) + 1))
#define FIRST_BIT_SET (   bitmask)    ((bitmask) & (~(bitmask) + 1))
#define FLASH_PTR
#define GET_CONST_PARENT_BY_FIELD (   TYPE,
  FIELD,
  FIELD_POINTER 
)    ((const TYPE *)(((const uint8_t *)FIELD_POINTER) - offsetof(TYPE, FIELD)))
#define GET_FIELD_PTR (   structPtr,
  typeName,
  fieldName 
)    ((uint8_t *)(structPtr) + offsetof(typeName, fieldName))
#define GET_INDEX_FROM_OFFSET (   PTR1,
  PTR2 
)    (PTR1 - PTR2)
#define GET_PARENT_BY_FIELD (   TYPE,
  FIELD,
  FIELD_POINTER 
)    ((TYPE *)(((uint8_t *)FIELD_POINTER) - offsetof(TYPE, FIELD)))
#define GET_STRUCT_BY_FIELD_POINTER (   struct_type,
  field_name,
  field_pointer 
)    ((struct_type *) (((uint8_t *) field_pointer) - FIELD_OFFSET(struct_type, field_name)))
#define INIT_ARRAY (   initValue,
  size 
)    {REPEAT_MACRO(ADD_COMMA, initValue, size)}
#define MAX (   x,
 
)    ((x)<(y)?(y):(x))
#define MIN (   x,
 
)    ((x)<(y)?(x):(y))
#define SLICE_SIZE (   type,
  firstField,
  lastField 
)    (offsetof(type, lastField) - offsetof(type, firstField) + sizeof(((type *)0)->lastField))

typedef uint16_t D_Nv_Size_t

void D_Nv_EraseSector ( uint8_t  sector)

Erases a sector of the internal NV.

Parameters
sectorThe sector to erase (0..D_NV_SECTOR_COUNT)

References D_NV_FIRST_SECTOR, D_NV_MEMORY_END, D_NV_MEMORY_START, D_NV_SECTOR_SIZE, and nvm_erase_row().

bool D_Nv_IsEmpty ( uint8_t  sector,
uint16_t  offset,
D_Nv_Size_t  numberOfBytes 
)

Checks if the requested range is empty (containing all 0xFF).

Parameters
sectorThe sector to use (0..D_NV_SECTOR_COUNT)
offsetThe start offset to check
numberOfBytesThe number of bytes to check
Returns
true if the range is empty, FALSE otherwise.
Note
Sector crossing is not permitted

References CompareData().

bool D_Nv_IsEqual ( uint8_t  sector,
uint16_t  offset,
uint8_t *  pBuffer,
D_Nv_Size_t  numberOfBytes 
)

Compare bytes with contents of the internal NV.

Parameters
sectorThe sector to use (0..D_NV_SECTOR_COUNT)
offsetThe offset to start comparing with
[in]pBufferThe start address of the buffer that contains the data to compare with
numberOfBytesThe number of bytes to compare
Note
offset+numberOfBytes must be <= D_NV_SECTOR_SIZE (meaning sector crossing is not permitted)

References CompareData().

void D_Nv_Read ( uint8_t  sector,
uint16_t  offset,
uint8_t *  pBuffer,
D_Nv_Size_t  numberOfBytes 
)

Read bytes from the internal NV.

Parameters
sectorThe sector to use (0..D_NV_SECTOR_COUNT)
offsetThe offset to start reading at
[out]pBufferThe start address of the buffer to store the read data to
numberOfBytesThe number of bytes to read
Note
offset+numberOfBytes must be <= D_NV_SECTOR_SIZE (meaning sector crossing is not permitted)

References D_NV_FIRST_SECTOR, D_NV_MEMORY_END, D_NV_MEMORY_START, D_NV_SECTOR_SIZE, and FLASH_PTR.

void D_Nv_SetSystemIntegrityCheckFunction ( void(*)(void)  pf)

The function to set system integrity check callback function.

References s_pfSystemCheckCallback.

void D_Nv_Write ( uint8_t  sector,
uint16_t  offset,
uint8_t *  pBuffer,
D_Nv_Size_t  numberOfBytes 
)

Write bytes to the internal NV.

Parameters
sectorThe sector to use (0..D_NV_SECTOR_COUNT)
offsetThe offset to start writing to
[in]pBufferThe start address of the buffer that contains the data to write
numberOfBytesThe number of bytes to write
Note
offset+numberOfBytes must be <= D_NV_SECTOR_SIZE (meaning sector crossing is not permitted)
pBuffer cannot be const because it is passed to HAL_FillFlashPageBuffer()

References D_NV_FIRST_SECTOR, D_NV_MEMORY_END, D_NV_MEMORY_START, D_NV_SECTOR_SIZE, MIN, nvm_write_buffer(), s_pfSystemCheckCallback, STATUS_BUSY, STATUS_OK, system_interrupt_enter_critical_section(), and system_interrupt_leave_critical_section().