-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathflash.cpp
207 lines (187 loc) · 7.55 KB
/
flash.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
@file flash.cpp
@author Bernd Giesecke ([email protected])
@brief Initialize, read and write parameters from/to internal flash memory
@version 0.1
@date 2021-01-10
@copyright Copyright (c) 2021
*/
#include "main.h"
s_lorawan_settings g_flash_content;
#include <Adafruit_LittleFS.h>
#include <InternalFileSystem.h>
using namespace Adafruit_LittleFS_Namespace;
static const char settings_name[] = "RAK";
File file(InternalFS);
void flash_reset(void);
/**
@brief Initialize access to nRF52 internal file system
*/
void init_flash(void)
{
// Initialize Internal File System
InternalFS.begin();
// Check if file exists
file.open(settings_name, FILE_O_READ);
if (!file)
{
MYLOG("FLASH", "File doesn't exist, force format");
delay(100);
flash_reset();
return;
}
file.read((uint8_t *)&g_lorawan_settings, sizeof(s_lorawan_settings));
file.close();
// Check if it is LoRaWAN settings
if ((g_lorawan_settings.valid_mark_1 != 0xAA) || (g_lorawan_settings.valid_mark_2 != LORAWAN_DATA_MARKER))
{
// Data is not valid, reset to defaults
MYLOG("FLASH", "Invalid data set, deleting and restart node");
InternalFS.format();
delay(1000);
sd_nvic_SystemReset();
}
log_settings();
}
/**
@brief Save changed settings if required
@return boolean
result of saving
*/
boolean save_settings(void)
{
bool result = true;
// Read saved content
file.open(settings_name, FILE_O_READ);
if (!file)
{
MYLOG("FLASH", "File doesn't exist, force format");
delay(100);
flash_reset();
}
file.read((uint8_t *)&g_flash_content, sizeof(s_lorawan_settings));
file.close();
if (memcmp((void *)&g_flash_content, (void *)&g_lorawan_settings, sizeof(s_lorawan_settings)) != 0)
{
MYLOG("FLASH", "Flash content changed, writing new data");
delay(100);
InternalFS.remove(settings_name);
if (file.open(settings_name, FILE_O_WRITE))
{
file.write((uint8_t *)&g_lorawan_settings, sizeof(s_lorawan_settings));
file.flush();
}
else
{
result = false;
}
file.close();
}
log_settings();
return result;
}
/**
@brief Reset content of the filesystem
*/
void flash_reset(void)
{
InternalFS.format();
if (file.open(settings_name, FILE_O_WRITE))
{
file.write((uint8_t *)&g_lorawan_settings, sizeof(s_lorawan_settings));
file.flush();
file.close();
}
}
/**
@brief Printout of all settings
*/
void log_settings(void)
{
uint8_t index = 0;
MYLOG("FLASH", "Saved settings:");
MYLOG("FLASH", "%03d Marks: %02X %02X", index, g_lorawan_settings.valid_mark_1, g_lorawan_settings.valid_mark_2);
index += 2;
MYLOG("FLASH", "%03d Auto join %s", index, g_lorawan_settings.auto_join ? "enabled" : "disabled");
index += 1;
MYLOG("FLASH", "%03d OTAA %s", index, g_lorawan_settings.otaa_enabled ? "enabled" : "disabled");
index += 1;
MYLOG("FLASH", "%03d Dev EUI %02X %02X %02X %02X %02X %02X %02X %02X", index, g_lorawan_settings.node_device_eui[0], g_lorawan_settings.node_device_eui[1],
g_lorawan_settings.node_device_eui[2], g_lorawan_settings.node_device_eui[3],
g_lorawan_settings.node_device_eui[4], g_lorawan_settings.node_device_eui[5],
g_lorawan_settings.node_device_eui[6], g_lorawan_settings.node_device_eui[7]);
index += 8;
MYLOG("FLASH", "%03d App EUI %02X %02X %02X %02X %02X %02X %02X %02X", index, g_lorawan_settings.node_app_eui[0], g_lorawan_settings.node_app_eui[1],
g_lorawan_settings.node_app_eui[2], g_lorawan_settings.node_app_eui[3],
g_lorawan_settings.node_app_eui[4], g_lorawan_settings.node_app_eui[5],
g_lorawan_settings.node_app_eui[6], g_lorawan_settings.node_app_eui[7]);
index += 8;
MYLOG("FLASH", "%03d App Key %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X",
index,
g_lorawan_settings.node_app_key[0], g_lorawan_settings.node_app_key[1],
g_lorawan_settings.node_app_key[2], g_lorawan_settings.node_app_key[3],
g_lorawan_settings.node_app_key[4], g_lorawan_settings.node_app_key[5],
g_lorawan_settings.node_app_key[6], g_lorawan_settings.node_app_key[7],
g_lorawan_settings.node_app_key[8], g_lorawan_settings.node_app_key[9],
g_lorawan_settings.node_app_key[10], g_lorawan_settings.node_app_key[11],
g_lorawan_settings.node_app_key[12], g_lorawan_settings.node_app_key[13],
g_lorawan_settings.node_app_key[14], g_lorawan_settings.node_app_key[15]);
index += 16;
MYLOG("FLASH", "%03d NWS Key %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X",
index,
g_lorawan_settings.node_nws_key[0], g_lorawan_settings.node_nws_key[1],
g_lorawan_settings.node_nws_key[2], g_lorawan_settings.node_nws_key[3],
g_lorawan_settings.node_nws_key[4], g_lorawan_settings.node_nws_key[5],
g_lorawan_settings.node_nws_key[6], g_lorawan_settings.node_nws_key[7],
g_lorawan_settings.node_nws_key[8], g_lorawan_settings.node_nws_key[9],
g_lorawan_settings.node_nws_key[10], g_lorawan_settings.node_nws_key[11],
g_lorawan_settings.node_nws_key[12], g_lorawan_settings.node_nws_key[13],
g_lorawan_settings.node_nws_key[14], g_lorawan_settings.node_nws_key[15]);
index += 16;
MYLOG("FLASH", "%03d Apps Key %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X",
index,
g_lorawan_settings.node_apps_key[0], g_lorawan_settings.node_apps_key[1],
g_lorawan_settings.node_apps_key[2], g_lorawan_settings.node_apps_key[3],
g_lorawan_settings.node_apps_key[4], g_lorawan_settings.node_apps_key[5],
g_lorawan_settings.node_apps_key[6], g_lorawan_settings.node_apps_key[7],
g_lorawan_settings.node_apps_key[8], g_lorawan_settings.node_apps_key[9],
g_lorawan_settings.node_apps_key[10], g_lorawan_settings.node_apps_key[11],
g_lorawan_settings.node_apps_key[12], g_lorawan_settings.node_apps_key[13],
g_lorawan_settings.node_apps_key[14], g_lorawan_settings.node_apps_key[15]);
index += 16;
MYLOG("FLASH", "%03d Dev Addr %08lX", index, g_lorawan_settings.node_dev_addr);
index += 4;
MYLOG("FLASH", "%03d Repeat time %ld", index, g_lorawan_settings.send_repeat_time);
index += 4;
MYLOG("FLASH", "%03d ADR %s", index, g_lorawan_settings.adr_enabled ? "enabled" : "disabled");
index += 1;
MYLOG("FLASH", "%03d %s Network", index, g_lorawan_settings.public_network ? "Public" : "Private");
index += 1;
MYLOG("FLASH", "%03d Dutycycle %s", index, g_lorawan_settings.duty_cycle_enabled ? "enabled" : "disabled");
index += 1;
MYLOG("FLASH", "%03d Join trials %d", index, g_lorawan_settings.join_trials);
index += 1;
MYLOG("FLASH", "%03d TX Power %d", index, g_lorawan_settings.tx_power);
index += 1;
MYLOG("FLASH", "%03d DR %d", index, g_lorawan_settings.data_rate);
index += 1;
MYLOG("FLASH", "%03d Class %d", index, g_lorawan_settings.lora_class);
index += 1;
MYLOG("FLASH", "%03d Subband %d", index, g_lorawan_settings.subband_channels);
index += 1;
MYLOG("FLASH", "%03d Fport %d", index, g_lorawan_settings.app_port);
index += 1;
MYLOG("FLASH", "%03d %s Message", index, g_lorawan_settings.confirmed_msg_enabled ? "Confirmed" : "Unconfirmed");
uint8_t *raw_data = (uint8_t *)&g_lorawan_settings.valid_mark_1;
MYLOG("FLASH", "Size %d", sizeof(s_lorawan_settings));
for (int idx = 0; idx < sizeof(s_lorawan_settings); idx++)
{
Serial.printf("%02X ", raw_data[idx]);
}
Serial.println("");
for (int idx = 0; idx < sizeof(s_lorawan_settings); idx++)
{
Serial.printf("%02d ", idx);
}
Serial.println("");
}