From 6ae43c94cd917b61e89fa51a43c0e3b01108f161 Mon Sep 17 00:00:00 2001 From: Nicolas Roggeman Date: Fri, 6 Oct 2023 08:28:48 +0200 Subject: [PATCH] Create a common structure for Apps NVRAM --- Makefile.standard_app | 8 ++++ include/nvram_struct.h | 60 +++++++++++++++++++++++++++++ lib_standard_app/nvram_struct.c | 68 +++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 include/nvram_struct.h create mode 100644 lib_standard_app/nvram_struct.c diff --git a/Makefile.standard_app b/Makefile.standard_app index 6aa7cbfe6..5d2577f92 100644 --- a/Makefile.standard_app +++ b/Makefile.standard_app @@ -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 # ##################################################################### diff --git a/include/nvram_struct.h b/include/nvram_struct.h new file mode 100644 index 000000000..d893b5645 --- /dev/null +++ b/include/nvram_struct.h @@ -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 +#include + +#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 diff --git a/lib_standard_app/nvram_struct.c b/lib_standard_app/nvram_struct.c new file mode 100644 index 000000000..5a43545e3 --- /dev/null +++ b/lib_standard_app/nvram_struct.c @@ -0,0 +1,68 @@ +/** + * @file nvram_struct.c + * @brief helpers to access NVRAM features + * TODO: put in SDK + */ +#ifdef HAVE_APP_NVRAM +#include +#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