grblHAL core  20251111
nvs.h
Go to the documentation of this file.
1 /*
2  nvs.h - non-volative storage data structures
3 
4  Part of grblHAL
5 
6  Copyright (c) 2017-2025 Terje Io
7  Copyright (c) 2009-2011 Simen Svale Skogsrud
8 
9  grblHAL is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  grblHAL is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #ifndef _NVS_H_
24 #define _NVS_H_
25 
26 #ifndef NVS_SIZE
30 #define NVS_SIZE 2048
31 #endif
32 
33 #ifndef NVS_SIZE_MAX
37 #define NVS_SIZE_MAX 4096 // Limit to 4K for now
38 #endif
39 
41 #ifndef NVS_CRC_BYTES
42 #define NVS_CRC_BYTES 2
43 #endif
44 
45 #define NVS_SIZE_PARAMETERS ((sizeof(coord_data_t) + NVS_CRC_BYTES) * N_CoordinateSystems)
46 #define NVS_SIZE_BUILD_INFO (sizeof(stored_line_t) + NVS_CRC_BYTES)
47 #define NVS_SIZE_STARTUP_BLOCK (N_STARTUP_LINE * (sizeof(stored_line_t) + NVS_CRC_BYTES))
48 
52 #if N_AXIS > 5
53 #define GRBL_NVS_END (NVS_ADDR_GLOBAL + ((sizeof(settings_t) + NVS_CRC_BYTES + 4) & 0xFFFC) + NVS_SIZE_PARAMETERS + NVS_SIZE_BUILD_INFO + NVS_SIZE_STARTUP_BLOCK + 1)
54 #else
55 #define GRBL_NVS_END 1023
56 #endif
57 
58 #if NVS_CRC_BYTES > 1
59 #define calc_checksum(data, length) modbus_crc16x(data, length)
60 #else
61 #define calc_checksum(data, length) grbl_crc8(data, length)
62 #endif
63 
71 #define NVS_ADDR_GLOBAL 1U
72 #if N_AXIS > 5
73 #define NVS_ADDR_PARAMETERS ((sizeof(settings_t) + NVS_CRC_BYTES + 4) & 0xFFFC) // align to word boundary
74 #else
75 #define NVS_ADDR_PARAMETERS 512U
76 #endif
77 #define NVS_ADDR_BUILD_INFO (GRBL_NVS_END - NVS_SIZE_BUILD_INFO)
78 #define NVS_ADDR_STARTUP_BLOCK (NVS_ADDR_BUILD_INFO - NVS_SIZE_STARTUP_BLOCK - 1)
79 #if N_TOOLS
80 #define NVS_ADDR_TOOL_TABLE (GRBL_NVS_END + 1)
81 #define GRBL_NVS_SIZE (GRBL_NVS_END + 1 + N_TOOLS * (sizeof(tool_data_t) + NVS_CRC_BYTES))
82 #else
83 #define GRBL_NVS_SIZE (GRBL_NVS_END + 1)
84 #endif
86 
87 typedef enum {
88  NVS_None = 0,
94 
96 typedef struct {
97  uint8_t *mem_address;
98  uint16_t address;
99  uint16_t size;
101 
106 typedef uint8_t (*get_byte_ptr)(uint32_t addr);
107 
112 typedef void (*put_byte_ptr)(uint32_t addr, uint8_t new_value);
113 
121 typedef bool (*memcpy_from_nvs_ptr)(uint8_t *dest, uint32_t source, uint32_t size, bool with_checksum);
122 
130 typedef bool (*memcpy_to_nvs_ptr)(uint32_t dest, uint8_t *source, uint32_t size, bool with_checksum);
131 
136 typedef bool (*memcpy_from_flash_ptr)(uint8_t *dest);
137 
142 typedef bool (*memcpy_to_flash_ptr)(uint8_t *source);
143 
145 typedef struct {
147  uint32_t size;
148  uint32_t size_max;
151 
157 
162 } nvs_io_t;
163 
164 #endif
bool(* memcpy_from_nvs_ptr)(uint8_t *dest, uint32_t source, uint32_t size, bool with_checksum)
Pointer to function for reading a block of data from NVS storage.
Definition: nvs.h:121
bool(* memcpy_to_nvs_ptr)(uint32_t dest, uint8_t *source, uint32_t size, bool with_checksum)
Pointer to function for writing a block of data to NVS storage.
Definition: nvs.h:130
uint8_t(* get_byte_ptr)(uint32_t addr)
Pointer to function for getting a byte from NVS storage.
Definition: nvs.h:106
bool(* memcpy_from_flash_ptr)(uint8_t *dest)
Pointer to function for reading a block of data from flash based NVS storage.
Definition: nvs.h:136
nvs_type
Definition: nvs.h:87
@ NVS_Emulated
4 - used by the core for buffered read and write
Definition: nvs.h:92
@ NVS_EEPROM
1
Definition: nvs.h:89
@ NVS_FRAM
2
Definition: nvs.h:90
@ NVS_None
0
Definition: nvs.h:88
@ NVS_Flash
3
Definition: nvs.h:91
bool(* memcpy_to_flash_ptr)(uint8_t *source)
Pointer to function for reading a block of data from flash based NVS storage.
Definition: nvs.h:142
void(* put_byte_ptr)(uint32_t addr, uint8_t new_value)
Pointer to function for putting a byte into NVS storage.
Definition: nvs.h:112
uint8_t * addr
Definition: nvs_buffer.c:45
uint32_t size
Definition: nvs_buffer.c:46
Structure for keeping track of NVS area used by driver and/or plugin code.
Definition: nvs.h:96
uint16_t address
Index based address into the storage area where the driver area starts.
Definition: nvs.h:98
uint16_t size
Actual size of driver area in bytes.
Definition: nvs.h:99
uint8_t * mem_address
Pointer to location in RAM where driver area is located.
Definition: nvs.h:97
Handler functions and variables for NVS storage of settings and data.
Definition: nvs.h:145
uint32_t size_max
Physical size of non-volatile storage area in bytes.
Definition: nvs.h:148
memcpy_from_flash_ptr memcpy_from_flash
Handler for reading a block of data from flash.
Definition: nvs.h:159
memcpy_from_nvs_ptr memcpy_from_nvs
Handler for writing a block of data to EEPROM or FRAM.
Definition: nvs.h:155
nvs_type type
Type of NVS storage.
Definition: nvs.h:146
nvs_driver_area_t driver_area
Definition: nvs.h:149
put_byte_ptr put_byte
Handler for writing a byte to EEPROM or FRAM.
Definition: nvs.h:153
get_byte_ptr get_byte
Handler for reading a byte from EEPROM or FRAM.
Definition: nvs.h:152
uint32_t size
Actual size of non-volatile storage area in bytes.
Definition: nvs.h:147
memcpy_to_nvs_ptr memcpy_to_nvs
Handler for reading a block of data from EEPROM or FRAM.
Definition: nvs.h:154
memcpy_to_flash_ptr memcpy_to_flash
Handler for writing a block of data to flash.
Definition: nvs.h:160