-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a common structure for Apps NVRAM
- Loading branch information
1 parent
3fae7ab
commit 6ae43c9
Showing
3 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |