-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtitan_mem.c
494 lines (416 loc) · 12.6 KB
/
titan_mem.c
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/**
*
* Flash memory interface Library
* TITANLAB INC 2019
* Keith Wakeham
*
*
*/
#include "titan_mem.h"
#include "nrf_fstorage_sd.h"
// #include "nrf_fstorage.h" //no soft device
#include "nrf_log.h"
#include "nrf_sdh_ble.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "fds.h"
#include "app_scheduler.h"
#include "nrf_pwr_mgmt.h"
#define MEM_START 0x70000
#define MEM_END 0x71fff
#define FILE_ID 0x0001 /* The ID of the file to write the records into. */
#define RECORD_KEY_1 0x1234 /* A key for the first record. */
#define FILE_ID_SLEEP 0x0002 /* The ID of the file to write the records into. */
#define RECORD_KEY_SLEEP_1 0x0ABC /* A key for the first record. */
static uint32_t const m_deadbeef = 0xDEADBEEF;
// static char const m_hello[] = "Hello, world!";
fds_record_t record;
fds_record_desc_t record_desc;
// static nrf_fstorage_api_t * p_fs_api;
static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt);
bool gc_flag = false; //garbage collection flag
/* Flag to check fds initialization. */
static bool volatile m_fds_initialized2 = false;
char const * fds_err_str[] =
{
"NRF_SUCCESS",
"FDS_ERR_OPERATION_TIMEOUT",
"FDS_ERR_NOT_INITIALIZED",
"FDS_ERR_UNALIGNED_ADDR",
"FDS_ERR_INVALID_ARG",
"FDS_ERR_NULL_ARG",
"FDS_ERR_NO_OPEN_RECORDS",
"FDS_ERR_NO_SPACE_IN_FLASH",
"FDS_ERR_NO_SPACE_IN_QUEUES",
"FDS_ERR_RECORD_TOO_LARGE",
"FDS_ERR_NOT_FOUND",
"FDS_ERR_NO_PAGES",
"FDS_ERR_USER_LIMIT_REACHED",
"FDS_ERR_CRC_CHECK_FAILED",
"FDS_ERR_BUSY",
"FDS_ERR_INTERNAL",
};
/* Array to map FDS events to strings. */
static char const * fds_evt_str[] =
{
"FDS_EVT_INIT",
"FDS_EVT_WRITE",
"FDS_EVT_UPDATE",
"FDS_EVT_DEL_RECORD",
"FDS_EVT_DEL_FILE",
"FDS_EVT_GC",
};
/* Keep track of the progress of a delete_all operation. */
static struct
{
bool delete_next; //!< Delete next record.
bool pending; //!< Waiting for an fds FDS_EVT_DEL_RECORD event, to delete the next record.
} m_delete_all;
/* Configuration data. */
static epx_configuration_t m_epx_cfg =
{
.num_gears = 0,
.gear_pos = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
.Kp = 0.0f,
.Ki = 0.0f,
.Kd = 0.0f,
.sin_min = 0,
.sin_max = 0,
.cos_min = 0,
.cos_max = 0,
};
static epx_position_configuration_t m_epx_position_cfg =
{
.current_rotations = 0,
.target_angle = 0,
.current_gear = 0,
.upshifts = 0,
.downshifts = 0,
};
/* A record containing configuration data. */
static fds_record_t const m_epx_record =
{
.file_id = FILE_ID,
.key = RECORD_KEY_1,
.data.p_data = &m_epx_cfg,
/* The length of a record is always expressed in 4-byte units (words). */
.data.length_words = (sizeof(m_epx_cfg)+3) / sizeof(uint32_t), //+3 for rounding
};
/* A record containing configuration data. */
static fds_record_t const m_epx_position_record =
{
.file_id = FILE_ID_SLEEP,
.key = RECORD_KEY_SLEEP_1,
.data.p_data = &m_epx_position_cfg,
/* The length of a record is always expressed in 4-byte units (words). */
.data.length_words = (sizeof(m_epx_position_cfg)+3) / sizeof(uint32_t), //+3 for rounding
};
NRF_FSTORAGE_DEF(nrf_fstorage_t titan_mem) =
{
.evt_handler = fstorage_evt_handler,
.start_addr = MEM_START,
.end_addr = MEM_END,
};
static void tm_fds_evt_handler(fds_evt_t const * p_evt)
{
NRF_LOG_INFO("Event: %s received (%s)",
fds_evt_str[p_evt->id],
fds_err_str[p_evt->result]);
switch (p_evt->id)
{
case FDS_EVT_INIT:
if (p_evt->result == NRF_SUCCESS)
{
m_fds_initialized2 = true;
NRF_LOG_INFO("FDS initialized");
}
break;
case FDS_EVT_WRITE:
{
if (p_evt->result == NRF_SUCCESS)
{
NRF_LOG_INFO("Record ID:\t0x%04x", p_evt->write.record_id);
NRF_LOG_INFO("File ID:\t0x%04x", p_evt->write.file_id);
NRF_LOG_INFO("Record key:\t0x%04x", p_evt->write.record_key);
}
} break;
case FDS_EVT_DEL_RECORD:
{
if (p_evt->result == NRF_SUCCESS)
{
NRF_LOG_INFO("Record ID:\t0x%04x", p_evt->del.record_id);
NRF_LOG_INFO("File ID:\t0x%04x", p_evt->del.file_id);
NRF_LOG_INFO("Record key:\t0x%04x", p_evt->del.record_key);
}
m_delete_all.pending = false;
} break;
case FDS_EVT_GC:
{
if (p_evt->result == NRF_SUCCESS)
{
gc_flag = false;
NRF_LOG_INFO("GC successful");
}
m_delete_all.pending = false;
} break;
default:
break;
}
}
static void wait_for_fds_ready(void)
{
while(!m_fds_initialized2)
{
app_sched_execute();
nrf_pwr_mgmt_run();
}
}
void tm_fds_init()
{
ret_code_t ret = fds_register(tm_fds_evt_handler);
if (ret != NRF_SUCCESS)
{
// Registering of the FDS event handler has failed.
}
NRF_LOG_INFO("MEM: registered event handler");
ret = fds_init();
if (ret != NRF_SUCCESS)
{
// Handle error.
}
NRF_LOG_INFO("MEM: fds initialize");
wait_for_fds_ready();
}
void tm_fds_test_write()
{
record.file_id = FILE_ID;
record.key = RECORD_KEY_1;
record.data.p_data = &m_deadbeef;
record.data.length_words = 1; /* one word is four bytes. */
ret_code_t rc;
rc = fds_record_write(&record_desc, &record);
if (rc != NRF_SUCCESS)
{
/* Handle error. */
}
}
epx_configuration_t tm_fds_epx_config (void)
{
return m_epx_cfg;
}
epx_position_configuration_t tm_fds_epx_position (void)
{
NRF_LOG_INFO("current_rotations: %d", m_epx_position_cfg.current_rotations);
NRF_LOG_INFO("target_angle: %d", m_epx_position_cfg.target_angle);
NRF_LOG_INFO("current_gear: %d", m_epx_position_cfg.current_gear);
return m_epx_position_cfg;
}
void tm_fds_test_retrieve()
{
// ret_code_t rc;
fds_flash_record_t flash_record;
fds_record_desc_t record_desc;
fds_find_token_t ftok;
uint32_t test_data;
/* It is required to zero the token before first use. */
memset(&ftok, 0x00, sizeof(fds_find_token_t));
/* Loop until all records with the given key and file ID have been found. */
while (fds_record_find(FILE_ID, RECORD_KEY_1, &record_desc, &ftok) == NRF_SUCCESS)
{
if (fds_record_open(&record_desc, &flash_record) != NRF_SUCCESS)
{
/* Handle error. */
}
/* Access the record through the flash_record structure. */
memcpy(&test_data, flash_record.p_data, sizeof(epx_configuration_t));
NRF_LOG_INFO("The record is: %d",test_data);
/* Close the record when done. */
if (fds_record_close(&record_desc) != NRF_SUCCESS)
{
/* Handle error. */
}
}
}
void tm_fds_test_delete()
{
ret_code_t ret = fds_record_delete(&record_desc);
if (ret != NRF_SUCCESS)
{
/* Error. */
}
}
void tm_fds_config_init()
{
ret_code_t rc;
fds_record_desc_t desc = {0};
fds_find_token_t tok = {0};
fds_stat_t stat = {0};
rc = fds_stat(&stat);
APP_ERROR_CHECK(rc);
NRF_LOG_INFO("Found %d valid records.", stat.valid_records);
NRF_LOG_INFO("Found %d dirty records (ready to be garbage collected).", stat.dirty_records);
tm_fds_gc();
rc = fds_record_find(FILE_ID, RECORD_KEY_1, &desc, &tok);
if (rc == NRF_SUCCESS)
{
NRF_LOG_INFO("Found config file...");
/* A config file is in flash. Let's update it. */
fds_flash_record_t config = {0};
/* Open the record and read its contents. */
rc = fds_record_open(&desc, &config);
APP_ERROR_CHECK(rc);
/* Copy the configuration from flash into m_epx_cfg. */
memcpy(&m_epx_cfg, config.p_data, sizeof(epx_configuration_t));
/* Close the record when done reading. */
rc = fds_record_close(&desc);
APP_ERROR_CHECK(rc);
}
else
{
/* System config not found; write a new one. */
NRF_LOG_INFO("Writing config file...");
rc = fds_record_write(&desc, &m_epx_record);
APP_ERROR_CHECK(rc);
}
rc = fds_record_find(FILE_ID_SLEEP, RECORD_KEY_SLEEP_1, &desc, &tok);
if (rc == NRF_SUCCESS)
{
NRF_LOG_INFO("Found sleep file...");
/* A config file is in flash. Let's update it. */
fds_flash_record_t config = {0};
/* Open the record and read its contents. */
rc = fds_record_open(&desc, &config);
APP_ERROR_CHECK(rc);
/* Copy the configuration from flash into m_epx_cfg. */
memcpy(&m_epx_position_cfg, config.p_data, sizeof(epx_position_configuration_t));
/* Close the record when done reading. */
rc = fds_record_close(&desc);
APP_ERROR_CHECK(rc);
}
else
{
/* System config not found; write a new one. */
NRF_LOG_INFO("Writing sleep file...");
rc = fds_record_write(&desc, &m_epx_position_record);
APP_ERROR_CHECK(rc);
}
}
void tm_fds_gc()
{
ret_code_t rc;
fds_stat_t stat = {0};
rc = fds_stat(&stat);
APP_ERROR_CHECK(rc);
if (!gc_flag)
{
if(stat.dirty_records > 60)
{
gc_flag = true;
fds_gc();
NRF_LOG_INFO("Garbage Collecting");
}
else
{
gc_flag = false;
}
}
}
void tm_fds_config_update()
{
ret_code_t rc;
fds_record_desc_t desc = {0};
fds_find_token_t tok = {0};
tm_fds_gc(); //run garbage collection
rc = fds_record_find(FILE_ID, RECORD_KEY_1, &desc, &tok);
if (rc == NRF_SUCCESS)
{
//debug info
// char tm_debug_message[20];
// sprintf(tm_debug_message,"%ld, %.5f \n",m_epx_cfg.zero, m_epx_cfg.calibration);
// NRF_LOG_INFO("%s",tm_debug_message);
rc = fds_record_update(&desc, &m_epx_record);
APP_ERROR_CHECK(rc);
}
else
{
/* System config not found; write a new one. */
NRF_LOG_INFO("Writing config file...");
rc = fds_record_write(&desc, &m_epx_record);
APP_ERROR_CHECK(rc);
}
}
void tm_fds_position_update()
{
ret_code_t rc;
fds_record_desc_t desc = {0};
fds_find_token_t tok = {0};
tm_fds_gc(); //run garbage collection
rc = fds_record_find(FILE_ID_SLEEP, RECORD_KEY_SLEEP_1, &desc, &tok);
if (rc == NRF_SUCCESS)
{
//debug info
// char tm_debug_message[20];
// sprintf(tm_debug_message,"%ld, %.5f \n",m_epx_cfg.zero, m_epx_cfg.calibration);
// NRF_LOG_INFO("%s",tm_debug_message);
NRF_LOG_INFO("Updating position");
rc = fds_record_update(&desc, &m_epx_position_record);
APP_ERROR_CHECK(rc);
if (rc == NRF_SUCCESS)
{
NRF_LOG_INFO("SUCCESS Updated position");
} else
{
NRF_LOG_INFO("FAIL position not saved");
}
}
else
{
/* System config not found; write a new one. */
NRF_LOG_INFO("Writing position file...");
rc = fds_record_write(&desc, &m_epx_position_record);
APP_ERROR_CHECK(rc);
}
}
void storage_init()
{
ret_code_t rc;
rc = nrf_fstorage_init(
&titan_mem, /* You fstorage instance, previously defined. */
&nrf_fstorage_sd, /* Name of the backend. */
NULL /* Optional parameter, backend-dependant. */
);
APP_ERROR_CHECK(rc);
}
static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt)
{
if (p_evt->result != NRF_SUCCESS)
{
NRF_LOG_INFO("--> Event received: ERROR while executing an fstorage operation.");
return;
}
switch (p_evt->id)
{
case NRF_FSTORAGE_EVT_WRITE_RESULT:
{
NRF_LOG_INFO("--> Event received: wrote %d bytes at address 0x%x.",
p_evt->len, p_evt->addr);
} break;
case NRF_FSTORAGE_EVT_ERASE_RESULT:
{
NRF_LOG_INFO("--> Event received: erased %d page from address 0x%x.",
p_evt->len, p_evt->addr);
} break;
default:
break;
}
}
void mem_epx_config_update(epx_configuration_t config_towrite)
{
m_epx_cfg = config_towrite;
tm_fds_config_update();
}
void mem_epx_position_update(epx_position_configuration_t position_config_towrite)
{
m_epx_position_cfg = position_config_towrite;
tm_fds_position_update();
}