/
sample_store.c
391 lines (348 loc) · 11.2 KB
/
sample_store.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
#include "sample_store.h"
#include "flash_list.h"
#include "spiflash_utils.h"
#define MAX(a,b) ((a) > (b)) ? (a) : (b)
#define MIN(a,b) ((a) < (b)) ? (a) : (b)
// number of reserved sectors at beginning of addressing space
#define RESERVED_SECTORS 1
static bool sample_store_ready = false;
static struct spiflash_params *flash_params = NULL;
static uint32_t flash_size = 0;
static enum sample_store_full_behavior full_behavior = STOP_ON_FULL;
#define SECTOR_SIZE 4096
#define SAMPLES_PER_SECTOR (SECTOR_SIZE / sizeof(struct sample))
typedef unsigned int sector_n;
typedef uint32_t flash_addr;
static sector_n
sample_idx_to_sector(unsigned int sample_idx)
{
return sample_idx / SAMPLES_PER_SECTOR + RESERVED_SECTORS;
}
static flash_addr
sample_idx_to_address(unsigned int sample_idx)
{
sector_n sector = sample_idx_to_sector(sample_idx);
unsigned int offset = sample_idx % SAMPLES_PER_SECTOR;
return SECTOR_SIZE * sector + offset * sizeof(struct sample);
}
static uint32_t
address_to_sample_idx(uint32_t addr)
{
sector_n sector = addr / SECTOR_SIZE;
unsigned int offset = addr % SAMPLES_PER_SECTOR;
return (sector - RESERVED_SECTORS) * SAMPLES_PER_SECTOR + offset;
}
/*
* reading samples
*/
int
sample_store_read(struct spiflash_transaction *trans, struct sample *buffer,
unsigned int start, unsigned int nsamples,
spi_cb cb, void *cbdata)
{
if (sample_idx_to_address(start+nsamples) >= flash_size)
return 1;
return spiflash_read_page(&onboard_flash, trans,
(uint8_t*) buffer,
sample_idx_to_address(start),
nsamples * sizeof(struct sample),
cb, cbdata);
}
void
sample_store_set_full_behavior(enum sample_store_full_behavior s)
{
full_behavior = s;
}
enum sample_store_full_behavior
sample_store_get_full_behavior()
{
return full_behavior;
}
/*
* writing samples
*
* we need to ensure that we only issue once write at a time, hence
* the queue.
*/
struct write_sample {
struct sample sample;
struct spiflash_write_bytes write_bytes;
bool pending;
uint32_t addr;
struct write_sample *next;
};
static volatile unsigned int sample_idx = 0;
static volatile int last_erased_sector = RESERVED_SECTORS - 1;
/*
* the write queue
* a write will remain at the head until it has completed
*/
static struct write_sample *write_queue;
#define N_WRITE_SAMPLES 4
static struct write_sample write_samples[N_WRITE_SAMPLES];
static int write_sample(struct write_sample *w);
static void
ensure_erased(void *cbdata)
{
struct write_sample *w = cbdata;
/* We add one here to ensure we've always erased one sector ahead of where
* we are writing. This allows us to easily reconstruct where
* we last wrote to on power-loss
*/
int next_sector = w->addr / SECTOR_SIZE + 1;
if (next_sector > last_erased_sector) {
// erase one sector and check again
last_erased_sector++;
// NOTE: we are reusing write_bytes' transaction
spiflash_erase_sector(&onboard_flash, &w->write_bytes.trans,
last_erased_sector * SECTOR_SIZE,
ensure_erased, w);
} else {
write_sample(w);
}
}
/*
* dispatch head of write queue
* call in critical section
*/
static void
_dispatch_queue()
{
struct write_sample *w = write_queue;
if (w == NULL)
return;
ensure_erased(w);
}
static void
sample_written(void *cbdata)
{
struct write_sample *w = cbdata;
crit_enter();
w->pending = false;
write_queue = w->next;
_dispatch_queue();
crit_exit();
}
static int
write_sample(struct write_sample *w)
{
spiflash_write_bytes(&onboard_flash, &w->write_bytes, w->addr,
(const uint8_t*) &w->sample, sizeof(struct sample),
sample_written, w);
return 0;
}
/* call in critical section */
static int
_enqueue_sample_write(struct write_sample *w)
{
// append write to end of queue
w->next = NULL;
if (write_queue == NULL) {
write_queue = w;
_dispatch_queue();
return 0;
} else {
struct write_sample *tail = write_queue;
while (tail->next)
tail = tail->next;
tail->next = w;
return 0; // although we don't know whether _dispatch_queue will fail
}
}
int
sample_store_push(const struct sample s)
{
if (!sample_store_ready)
return 3;
crit_enter();
// handle FLASH full condition
uint32_t addr = sample_idx_to_address(sample_idx);
if (addr >= flash_size) {
switch (full_behavior) {
case WRAP_ON_FULL:
sample_store_reset();
break;
case STOP_ON_FULL:
crit_exit();
return 4;
}
}
// find available write_sample
struct write_sample *w = NULL;
for (unsigned int i=0; i<N_WRITE_SAMPLES; i++) {
if (!write_samples[i].pending)
w = &write_samples[i];
}
if (w == NULL) {
crit_exit();
return 1;
} else {
w->pending = true;
w->sample = s;
w->addr = addr;
if (w->addr > flash_size) {
crit_exit();
return 2;
}
int ret = _enqueue_sample_write(w);
if (ret == 0)
sample_idx++;
crit_exit();
return ret;
}
}
unsigned int
sample_store_get_count()
{
return sample_idx;
}
/*
* Identifying next empty sector after power-loss
*
* By virtue of the fact that the next unused FLASH sector has always
* been cleared, we can identify it by looking at the first byte of
* each sector and check whether it has been cleared. The first sector
* with a first byte of 0xff is the next unused sector.
*/
void
find_sector_cb(void *cbdata)
{
struct find_empty_sector_ctx *ctx = cbdata;
if (ctx->buffer == 0xffffffff) {
// we've found our sector
ctx->cb(ctx->next_sector, ctx->cbdata);
} else {
ctx->next_sector += SECTOR_SIZE;
if (ctx->next_sector < flash_size) {
int res = spiflash_read_page(&onboard_flash, &ctx->trans,
(uint8_t *) &ctx->buffer,
ctx->next_sector, 4,
find_sector_cb, ctx);
if (res)
ctx->cb(INVALID_SECTOR, ctx->cbdata);
} else {
ctx->cb(INVALID_SECTOR, ctx->cbdata);
}
}
}
void
sample_store_find_empty_sector(struct find_empty_sector_ctx *ctx,
unsigned int start_sector,
find_empty_sector_cb cb, void *cbdata)
{
ctx->next_sector = SECTOR_SIZE * (start_sector + RESERVED_SECTORS);
ctx->cb = cb;
ctx->cbdata = cbdata;
spiflash_read_page(&onboard_flash, &ctx->trans,
(uint8_t *) &ctx->buffer, ctx->next_sector, 4,
find_sector_cb, ctx);
}
/*
* recovery from power loss
*/
typedef void (*sample_store_recover_done_cb)();
struct recover_ctx {
bool running;
sample_store_recover_done_cb cb;
struct find_empty_sector_ctx find_empty_sector;
uint32_t pos;
struct sample sample;
} recover_ctx;
/* look for first invalid sample */
static void
sample_store_recover_find_sample(void *cbdata)
{
struct recover_ctx *ctx = cbdata;
if (ctx->sample.time == 0xffffffff) {
// found first invalid sample
sample_idx = ctx->pos - 1;
last_erased_sector = sample_idx_to_sector(ctx->pos);
sample_store_ready = true;
if (ctx->cb)
ctx->cb();
} else {
// WARNING: we are reusing find_empty_sector's spiflash_transaction
int res = sample_store_read(&ctx->find_empty_sector.trans,
&ctx->sample,
ctx->pos, 1,
sample_store_recover_find_sample,
ctx);
if (res) {
// error (e.g. end of FLASH)
sample_store_reset();
if (ctx->cb)
ctx->cb();
}
ctx->pos += 1;
}
}
static void
sample_store_recover_empty_sector_found(uint32_t addr, void *cbdata)
{
struct recover_ctx *ctx = cbdata;
if (addr != INVALID_SECTOR) {
ctx->pos = address_to_sample_idx(addr) - SAMPLES_PER_SECTOR;
ctx->sample.time = 0; // initialize as a valid sample
sample_store_recover_find_sample(ctx);
} else {
sample_store_reset();
if (ctx->cb)
ctx->cb();
}
}
int
sample_store_recover(sample_store_recover_done_cb done_cb)
{
if (recover_ctx.running)
return 1;
sample_store_ready = false;
sample_store_find_empty_sector(&recover_ctx.find_empty_sector, 0,
sample_store_recover_empty_sector_found,
&recover_ctx);
return 0;
}
/*
* initialization
*/
/* reset sample_idx to beginning of store */
void
sample_store_reset()
{
crit_enter();
sample_idx = 0;
last_erased_sector = RESERVED_SECTORS - 1;
if (flash_params)
sample_store_ready = true;
crit_exit();
}
static struct spiflash_transaction trans;
static void
flash_recovered_cb()
{}
static void
flash_unprotected_cb(void *cbdata)
{
sample_store_recover(flash_recovered_cb);
}
static void
identify_flash_cb(void *cbdata, uint8_t mfg_id, uint8_t memtype, uint8_t capacity)
{
if (mfg_id == 0)
return;
for (struct spiflash_params *i = spiflash_device_params; i->mfg_id != 0x00; i++) {
if (mfg_id == i->mfg_id
&& memtype == i->device_id1
&& capacity == i->device_id2) {
flash_params = i;
flash_size = spiflash_block_size_to_bytes(i->block_size)
* i->n_blocks;
break;
}
}
spiflash_set_protection(&onboard_flash, &trans, false, flash_unprotected_cb, NULL);
}
void
sample_store_init()
{
spiflash_get_id(&onboard_flash, &trans, identify_flash_cb, NULL);
}