Skip to content

Commit

Permalink
Create a common structure for Apps NVRAM
Browse files Browse the repository at this point in the history
  • Loading branch information
nroggeman-ledger authored and jarevalo-ledger committed Jun 4, 2024
1 parent 3fae7ab commit 6ae43c9
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Makefile.standard_app
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ ifeq ($(ENABLE_SWAP), 1)
DEFINES += HAVE_SWAP
endif

#####################################################################
# NVRAM #
#####################################################################
ifeq ($(ENABLE_NVRAM), 1)
HAVE_APP_NVRAM = 1
DEFINES += HAVE_APP_NVRAM
endif

#####################################################################
# DEBUG #
#####################################################################
Expand Down
60 changes: 60 additions & 0 deletions include/nvram_struct.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @file nvram_struct.h
* @brief All definitions of the common part of NVRAM structure, and helpers to access it
* TODO: put in SDK
*/
#pragma once

#include <stdint.h>
#include <stdbool.h>

#ifndef HAVE_BOLOS
/* "nvram_data.h" needs to be created in all apps including this file */
#include "nvram_data.h"
#endif // HAVE_BOLOS

/**
* @brief Structure defining the header of NVRAM
*
*/
typedef struct Nvram_header_s {
char tag[4]; ///< ['N','V','R','A'] array, when properly initialized
uint32_t size; ///< size in bytes of the data
uint32_t struct_version; ///< version of the structure of data (to be set once at first
///< application start-up)
uint32_t data_version; ///< version of the content of data (to be updated every time data are
///< updated)
} Nvram_header_t;

/**
* @brief Structure defining the NVRAM
*
*/
typedef struct Nvram_s {
Nvram_header_t header; ///< header describing the data
#ifndef HAVE_BOLOS
Nvram_data_t data; ///< application data, Nvram_data_t must be defined in "nvram_data.h" file
#endif // HAVE_BOLOS
} Nvram_t;

#ifndef HAVE_BOLOS
/**
* @brief This variable must be defined in Application code, and never used directly,
* except by @ref N_nvram
*
*/
extern const Nvram_t N_nvram_real;

/**
* @brief To be used by function accessing NVRAM data (not N_nvram_real)
*
*/
#define N_nvram (*(volatile Nvram_t *) PIC(&N_nvram_real))

extern void nvram_init(void);
extern uint32_t nvram_get_size(void);
extern uint8_t nvram_get_struct_version(void);
extern uint8_t nvram_get_data_version(void);
extern bool nvram_is_initalized(void);

#endif // HAVE_BOLOS
68 changes: 68 additions & 0 deletions lib_standard_app/nvram_struct.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* @file nvram_struct.c
* @brief helpers to access NVRAM features
* TODO: put in SDK
*/
#ifdef HAVE_APP_NVRAM
#include <string.h>
#include "nvram_struct.h"
#include "os_nvm.h"
#include "os_pic.h"

const Nvram_t N_nvram_real;

/**
* @brief init header of NVRAM structure (set "NVRA" tag)
*/
void nvram_init(void)
{
Nvram_header_t header;

memcpy(header.tag, (void *) "NVRA", 4);
// NVRAM_STRUCT_VERSION and NVRAM_DATA_VERSION must be defined in nvram_data.h
header.struct_version = NVRAM_STRUCT_VERSION;
header.data_version = NVRAM_DATA_VERSION;
header.size = sizeof(Nvram_data_t);
nvm_write((void *) &N_nvram.header, (void *) &header, sizeof(Nvram_header_t));
}

/**
* @brief get the size of app data
*/
uint32_t nvram_get_size(void)
{
return N_nvram.header.size;
;
}

/**
* @brief get the version of app data structure
*/
uint8_t nvram_get_struct_version(void)
{
return N_nvram.header.struct_version;
}

/**
* @brief get the version of app data data
*/
uint8_t nvram_get_data_version(void)
{
return N_nvram.header.data_version;
}

/**
* @brief ensure NVRAM struct is initialized
*/
bool nvram_is_initalized(void)
{
if (memcmp((const void *) N_nvram.header.tag, "NVRA", 4)) {
return false;
}
if (N_nvram.header.size == 0) {
return false;
}
return true;
}

#endif // HAVE_APP_NVRAM

0 comments on commit 6ae43c9

Please sign in to comment.