-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.diff
281 lines (261 loc) · 10 KB
/
test.diff
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
diff --git a/software/CMakeLists.txt b/software/CMakeLists.txt
index 9b3d98d..c1cf464 100644
--- a/software/CMakeLists.txt
+++ b/software/CMakeLists.txt
@@ -42,6 +42,7 @@ SET(SOURCES
"${PROJECT_SOURCE_DIR}/src/sdmmc.c"
"${PROJECT_SOURCE_DIR}/src/sd.c"
"${PROJECT_SOURCE_DIR}/src/sd_new_file_objects.c"
+ "${PROJECT_SOURCE_DIR}/src/data_storage.c"
"${PROJECT_SOURCE_DIR}/src/littlefs/lfs.c"
"${PROJECT_SOURCE_DIR}/src/littlefs/lfs_util.c"
diff --git a/software/src/communication.c b/software/src/communication.c
index f7d03ce..a37ede8 100644
--- a/software/src/communication.c
+++ b/software/src/communication.c
@@ -34,6 +34,7 @@
#include "eeprom.h"
#include "sd.h"
#include "sdmmc.h"
+#include "data_storage.h"
#include "xmc_rtc.h"
@@ -131,6 +132,8 @@ BootloaderHandleMessageResponse handle_message(const void *message, void *respon
case FID_GET_DATE_TIME: return get_date_time(message, response);
case FID_SET_LED_STATE: return set_led_state(message);
case FID_GET_LED_STATE: return get_led_state(message, response);
+ case FID_GET_DATA_STORAGE: return get_data_storage(message, response);
+ case FID_SET_DATA_STORAGE: return set_data_storage(message);
default: return HANDLE_MESSAGE_RESPONSE_NOT_SUPPORTED;
}
}
@@ -533,6 +536,33 @@ BootloaderHandleMessageResponse get_date_time(const GetDateTime *data, GetDateTi
return HANDLE_MESSAGE_RESPONSE_NEW_MESSAGE;
}
+BootloaderHandleMessageResponse get_data_storage(const GetDataStorage *data, GetDataStorage_Response *response) {
+ if(data->page >= DATA_STORAGE_PAGES) {
+ return HANDLE_MESSAGE_RESPONSE_INVALID_PARAMETER;
+ }
+
+ response->header.length = sizeof(GetDataStorage_Response);
+ memcpy(response->data, data_storage.storage[data->page], 63);
+
+ return HANDLE_MESSAGE_RESPONSE_NEW_MESSAGE;
+}
+
+BootloaderHandleMessageResponse set_data_storage(const SetDataStorage *data) {
+ if(data->page >= DATA_STORAGE_PAGES) {
+ return HANDLE_MESSAGE_RESPONSE_INVALID_PARAMETER;
+ }
+
+ // Copy data into storage and set new change time.
+ // Data will be copied from RAM to SD after 10 minutes.
+ if(memcmp(data_storage.storage[data->page], data->data, 63) != 0) {
+ memcpy(data_storage.storage[data->page], data->data, 63);
+ if(data_storage.last_change_time[data->page] == 0) {
+ data_storage.last_change_time[data->page] = system_timer_get_ms();
+ }
+ }
+
+ return HANDLE_MESSAGE_RESPONSE_EMPTY;
+}
bool handle_sd_wallbox_data_points_low_level_callback(void) {
static bool is_buffered = false;
diff --git a/software/src/communication.h b/software/src/communication.h
index 6ab92d1..331ed17 100644
--- a/software/src/communication.h
+++ b/software/src/communication.h
@@ -104,6 +104,8 @@ void communication_init(void);
#define FID_GET_DATE_TIME 30
#define FID_SET_LED_STATE 31
#define FID_GET_LED_STATE 32
+#define FID_GET_DATA_STORAGE 33
+#define FID_SET_DATA_STORAGE 34
#define FID_CALLBACK_SD_WALLBOX_DATA_POINTS_LOW_LEVEL 24
#define FID_CALLBACK_SD_WALLBOX_DAILY_DATA_POINTS_LOW_LEVEL 25
@@ -463,6 +465,22 @@ typedef struct {
uint16_t hue;
} __attribute__((__packed__)) GetLEDState_Response;
+typedef struct {
+ TFPMessageHeader header;
+ uint8_t page;
+} __attribute__((__packed__)) GetDataStorage;
+
+typedef struct {
+ TFPMessageHeader header;
+ uint8_t data[63];
+} __attribute__((__packed__)) GetDataStorage_Response;
+
+typedef struct {
+ TFPMessageHeader header;
+ uint8_t page;
+ uint8_t data[63];
+} __attribute__((__packed__)) SetDataStorage;
+
// Function prototypes
BootloaderHandleMessageResponse set_contactor(const SetContactor *data);
@@ -493,6 +511,8 @@ BootloaderHandleMessageResponse set_date_time(const SetDateTime *data);
BootloaderHandleMessageResponse get_date_time(const GetDateTime *data, GetDateTime_Response *response);
BootloaderHandleMessageResponse set_led_state(const SetLEDState *data);
BootloaderHandleMessageResponse get_led_state(const GetLEDState *data, GetLEDState_Response *response);
+BootloaderHandleMessageResponse get_data_storage(const GetDataStorage *data, GetDataStorage_Response *response);
+BootloaderHandleMessageResponse set_data_storage(const SetDataStorage *data);
// Callbacks
bool handle_sd_wallbox_data_points_low_level_callback(void);
diff --git a/software/src/configs/config_logging.h b/software/src/configs/config_logging.h
index 265ab7f..456bdc4 100644
--- a/software/src/configs/config_logging.h
+++ b/software/src/configs/config_logging.h
@@ -23,8 +23,8 @@
#define CONFIG_LOGGING_H
#define LOGGING_UARTBB
-//#define LOGGING_LEVEL LOGGING_DEBUG
-#define LOGGING_LEVEL LOGGING_NONE
+#define LOGGING_LEVEL LOGGING_DEBUG
+//#define LOGGING_LEVEL LOGGING_NONE
#define LOGGING_USE_BASENAME
#define LOGGING_HAVE_SYSTEM_TIME
diff --git a/software/src/main.c b/software/src/main.c
index 89dab79..3e45bbb 100644
--- a/software/src/main.c
+++ b/software/src/main.c
@@ -37,6 +37,7 @@
#include "eeprom.h"
#include "date_time.h"
#include "sd.h"
+#include "data_storage.h"
int main(void) {
logging_init();
@@ -51,6 +52,7 @@ int main(void) {
eeprom_init();
date_time_init();
sd_init();
+ data_storage_init();
while(true) {
bootloader_tick();
@@ -62,5 +64,6 @@ int main(void) {
voltage_tick();
date_time_tick();
sd_tick();
+ data_storage_tick();
}
}
diff --git a/software/src/sd.c b/software/src/sd.c
index ff43b7b..ccd9d9c 100644
--- a/software/src/sd.c
+++ b/software/src/sd.c
@@ -34,6 +34,7 @@
#include "xmc_wdt.h"
#include "lfs.h"
#include "sd_new_file_objects.h"
+#include "data_storage.h"
SD sd;
CoopTask sd_task;
@@ -652,6 +653,73 @@ bool sd_read_energy_manager_daily_data_point(uint8_t year, uint8_t month, uint8_
return true;
}
+bool sd_write_storage(uint8_t page) {
+ char f[SD_PATH_LENGTH] = "storage/X.sp";
+ f[8] = '0' + page;
+
+ lfs_file_t file;
+ memset(sd.lfs_file_config.buffer, 0, 512);
+ sd.lfs_file_config.attrs = NULL;
+ sd.lfs_file_config.attr_count = 0;
+
+ int err = lfs_file_opencfg(&sd.lfs, &file, f, LFS_O_RDWR, &sd.lfs_file_config);
+ if((err == LFS_ERR_EXIST) || (err == LFS_ERR_NOENT)) {
+ err = lfs_file_close(&sd.lfs, &file);
+ lfs_mkdir(&sd.lfs, "storage");
+ err = lfs_file_opencfg(&sd.lfs, &file, f, LFS_O_CREAT | LFS_O_RDWR, &sd.lfs_file_config);
+ if(err != LFS_ERR_OK) {
+ logw("lfs_file_opencfg %s: %d\n\r", f, err);
+ return false;
+ }
+ }
+
+ lfs_ssize_t size = lfs_file_write(&sd.lfs, &file, data_storage.storage[page], DATA_STORAGE_SIZE);
+ if(size != DATA_STORAGE_SIZE) {
+ logw("lfs_file_write size %d vs %d\n\r", size, DATA_STORAGE_SIZE);
+ err = lfs_file_close(&sd.lfs, &file);
+ logw("lfs_file_close %d\n\r", err);
+ return false;
+ }
+
+ err = lfs_file_close(&sd.lfs, &file);
+ if(err != LFS_ERR_OK) {
+ logw("lfs_file_close %d\n\r", err);
+ return false;
+ }
+
+ return true;
+}
+
+bool sd_read_storage(uint8_t page) {
+ char f[SD_PATH_LENGTH] = "storage/X.sp";
+ f[8] = '0' + page;
+
+ lfs_file_t file;
+ memset(sd.lfs_file_config.buffer, 0, 512);
+ sd.lfs_file_config.attrs = NULL;
+ sd.lfs_file_config.attr_count = 0;
+
+ int err = lfs_file_opencfg(&sd.lfs, &file, f, LFS_O_RDONLY, &sd.lfs_file_config);
+ if((err == LFS_ERR_EXIST) || (err == LFS_ERR_NOENT)) {
+ return true;
+ }
+
+ lfs_ssize_t size = lfs_file_read(&sd.lfs, &file, data_storage.storage[page], DATA_STORAGE_SIZE);
+ if(size != DATA_STORAGE_SIZE) {
+ logw("lfs_file_read size %d vs %d\n\r", size, DATA_STORAGE_SIZE);
+ err = lfs_file_close(&sd.lfs, &file);
+ logd("lfs_file_close %d\n\r", err);
+ return false;
+ }
+ err = lfs_file_close(&sd.lfs, &file);
+ if(err != LFS_ERR_OK) {
+ logw("lfs_file_close %d\n\r", err);
+ return false;
+ }
+
+ return true;
+}
+
void sd_init_task(void) {
memset(&sd, 0, sizeof(SD));
@@ -778,6 +846,10 @@ void sd_init_task(void) {
// Set sd status at the end, to make sure that everything is completely initialized
// before any other code tries to access the sd card
sd.sd_status = sdmmc_error;
+
+ for(uint8_t i = 0; i < DATA_STORAGE_PAGES; i++) {
+ data_storage.read_from_sd[i] = true;
+ }
}
void sd_tick_task_handle_wallbox_data(void) {
@@ -1047,6 +1119,20 @@ void sd_tick_task_handle_energy_manager_daily_data(void) {
}
}
+void sd_tick_task_handle_storage(void) {
+ for(uint8_t i = 0; i < DATA_STORAGE_PAGES; i++) {
+ if(data_storage.read_from_sd[i]) {
+ sd_read_storage(i);
+ data_storage.read_from_sd[i] = false;
+ data_storage.write_to_sd[i] = false; // We don't need to write to sd anymore, because we just read it into RAM
+ }
+ if(data_storage.write_to_sd[i]) {
+ sd_write_storage(i);
+ data_storage.write_to_sd[i] = false;
+ }
+ }
+}
+
void sd_tick_task(void) {
// Pre-initialize sd and lfs status.
// If no sd card is inserted, the sd_init code is never called and the status would in this case never be set.
@@ -1111,6 +1197,7 @@ void sd_tick_task(void) {
sd_tick_task_handle_wallbox_daily_data();
sd_tick_task_handle_energy_manager_data();
sd_tick_task_handle_energy_manager_daily_data();
+ sd_tick_task_handle_storage();
}
coop_task_yield();