17#ifdef HAVE_APP_STORAGE
19#include "app_storage.h"
24#define APP_STORAGE_ERASE_BLOCK_SIZE 256
33CONST app_storage_t app_storage_real
__attribute__((section(
".storage_section")));
34#define app_storage (*(volatile app_storage_t *) PIC(&app_storage_real))
39static bool app_storage_is_initalized(
void)
41 if (memcmp((
const void *) &app_storage.header.tag, APP_STORAGE_TAG, APP_STORAGE_TAG_LEN) != 0) {
50static inline void system_header_reset(
void)
53 app_storage_header_t header = {0};
55 memcpy(&header.tag, (
void *) APP_STORAGE_TAG, APP_STORAGE_TAG_LEN);
57 header.struct_version = APP_STORAGE_HEADER_STRUCT_VERSION;
59 header.data_version = APP_STORAGE_INITIAL_APP_DATA_VERSION;
61 header.properties = ((HAVE_APP_STORAGE_PROP_SETTINGS << 0) | (HAVE_APP_STORAGE_PROP_DATA << 1));
62 nvm_write((
void *) &app_storage.header, &header,
sizeof(header));
73void app_storage_init(
void)
75 if (app_storage_is_initalized()) {
78 system_header_reset();
84void app_storage_reset(
void)
86 system_header_reset();
88 uint8_t erase_buf[APP_STORAGE_ERASE_BLOCK_SIZE] = {0};
90 if (APP_STORAGE_SIZE > APP_STORAGE_ERASE_BLOCK_SIZE) {
91 for (; offset < APP_STORAGE_SIZE - APP_STORAGE_ERASE_BLOCK_SIZE;
92 offset += APP_STORAGE_ERASE_BLOCK_SIZE) {
93 nvm_write((
void *) &app_storage.data[offset],
95 APP_STORAGE_ERASE_BLOCK_SIZE);
98 if (APP_STORAGE_SIZE > offset) {
100 (
void *) &app_storage.data[offset], (
void *) erase_buf, APP_STORAGE_SIZE - offset);
107uint32_t app_storage_get_size(
void)
109 return app_storage.header.size;
115uint32_t app_storage_get_data_version(
void)
117 return app_storage.header.data_version;
123uint16_t app_storage_get_properties(
void)
125 return app_storage.header.properties;
131void app_storage_increment_data_version(
void)
133 uint32_t data_version = app_storage.header.data_version;
135 if (data_version == 0) {
140 data_version = APP_STORAGE_INITIAL_APP_DATA_VERSION;
143 (
void *) &app_storage.header.data_version, (
void *) &data_version,
sizeof(data_version));
149void app_storage_set_data_version(uint32_t data_version)
152 (
void *) &app_storage.header.data_version, (
void *) &data_version,
sizeof(data_version));
158int32_t app_storage_write(
const void *buf, uint32_t nbyte, uint32_t offset)
162 return APP_STORAGE_ERR_INVALID_ARGUMENT;
165 return APP_STORAGE_ERR_INVALID_ARGUMENT;
167 uint32_t max_offset = 0;
168 if (__builtin_add_overflow(offset, nbyte, &max_offset)) {
169 return APP_STORAGE_ERR_INVALID_ARGUMENT;
173 if (max_offset > APP_STORAGE_SIZE) {
174 return APP_STORAGE_ERR_OVERFLOW;
178 nvm_write((
void *) &app_storage.data[offset], (
void *) buf, nbyte);
181 if (app_storage.header.size < max_offset) {
182 nvm_write((
void *) &app_storage.header.size, (
void *) &max_offset,
sizeof(max_offset));
190int32_t app_storage_read(
void *buf, uint32_t nbyte, uint32_t offset)
194 return APP_STORAGE_ERR_INVALID_ARGUMENT;
197 return APP_STORAGE_ERR_INVALID_ARGUMENT;
199 uint32_t max_offset = 0;
200 if (__builtin_add_overflow(offset, nbyte, &max_offset)) {
201 return APP_STORAGE_ERR_INVALID_ARGUMENT;
205 if (max_offset > app_storage.header.size) {
206 return APP_STORAGE_ERR_NO_DATA_AVAILABLE;
210 memcpy((
void *) buf, (
void *) &app_storage.data[offset], nbyte);
__attribute__((section("._nbgl_fonts_"))) const
return the non-unicode font corresponding to the given font ID