-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.c
461 lines (347 loc) · 12.5 KB
/
log.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
#include <string.h>
#include "mk20dx128.h"
#include "sdio.h"
#include "fusion.h"
#include "usbserial.h"
#include "util.h"
#define RING_SIZE 16
#define INDEX_BLOCKS 16
#define ENTRY_SIZE (4 * sizeof(uint32_t))
#define SAMPLE_SIZE sizeof(float)
#define ENTRIES_PER_INDEX_BLOCK (512 / ENTRY_SIZE)
/*****************************************************
* Block 0: Master index block. *
* *
* +-----------------------------------------+-...-+ *
* | Count/32 | First key of ith index block | | *
* +----------+------------------------------+-...-+ *
* *
* Blocks 1 - INDEX_BLOCKS: Index blocks *
* *
* +-----------------------------------------+ *
* | KEY/32 | DATA/32 | BLOCK/32 | LINES/32 | *
* +--------+----------+----------+----------+ *
* . . . . . *
* . . . . . *
* . ENTRIES_PER_INDEX_BLOCK rows . *
* . . . . . *
* +--------+----------+----------+----------+ *
* *
****************************************************/
static struct {
volatile uint32_t lines; /* Total number of lines recorded. */
volatile uint32_t filling; /* Total block currently being filled. */
volatile uint32_t writing; /* Total block currently being written
* out. */
volatile uint16_t fill; /* The fill, in bytes, of the current
* buffer. */
uint32_t key, data, block; /* Entry details. */
uint32_t target; /* Number of entries to write. */
uint8_t width; /* The witdth, in bytes, of each
* line. */
uint8_t buffers[RING_SIZE][512];
} context;
static uint32_t data = (1 << 10) - 1;
static uint8_t *flush_filled_buffers(SDTransactionStatus status,
uint8_t *buffer,
void *userdata)
{
uint8_t *b;
assert(status != SDIO_FAILED);
toggle_led();
if (context.writing == context.filling) {
if (fusion_in_progress()) {
sleep_while(context.writing == context.filling);
} else {
return NULL;
}
}
/* usbserial_trace("< %f\n", (float)cycles() / cycles_in_ms(1)); */
b = context.buffers[context.writing % RING_SIZE];
context.writing += 1;
return b;
}
static void fusion_data_ready(float *samples)
{
uint8_t *line = (uint8_t *)samples;
int spillover;
/* This takes account of the fact that context.writing points to
* the _next_ page to be written. */
assert (context.filling - context.writing < RING_SIZE - 1);
spillover = context.width - (sizeof(context.buffers[0]) - context.fill);
assert(spillover < 0 || ((context.lines + 1) * context.width) % 512 == spillover);
assert(((float *)line)[9] > 10.0);
if (spillover < 0) {
/* Write entire lines to the buffer if they fit. */
memcpy (context.buffers[context.filling % RING_SIZE] + context.fill,
line, context.width);
context.fill += context.width;
} else {
/* If the line doesn't fit entirely, write the beginning of
* the next line. */
memcpy (context.buffers[context.filling % RING_SIZE] + context.fill,
line, context.width - spillover);
/* memset(context.buffers[(context.filling + 1) % RING_SIZE], */
/* 0xFA, sizeof(context.buffers[0])); */
/* If there's left-over data from the last line copy it to the
* start of the new buffer. */
if (spillover > 0) {
/* Make sure we're not spilling into the page currently
* being written out. */
assert (context.filling - context.writing < RING_SIZE - 2);
memcpy(context.buffers[(context.filling + 1) % RING_SIZE],
line + context.width - spillover, spillover);
}
context.filling += 1;
context.fill = spillover;
/* usbserial_trace("%d\n", */
/* context.block + context.filling); */
}
/* Increment the number of entries written. */
context.lines += 1;
if (context.lines == context.target) {
fusion_set_callback(NULL);
/* Force the last, half-finished page to be flushed, if
* needed. */
if(context.fill > 0) {
context.filling += 1;
}
}
assert((context.lines * context.width) % 512 == context.fill);
}
void log_initialize()
{
uint8_t buffer[512];
int i;
/* Just clear the index blocks. */
memset(buffer, 0, sizeof(buffer));
for (i = 0 ; i < INDEX_BLOCKS + 1 ; i += 1) {
sdio_write_single_block(i, buffer);
sleep_while(sdio_is_busy());
}
}
static inline int sample_line_width(uint32_t data)
{
uint32_t f;
int width;
/* Count the number of channels in the log by counting the
* number of bits set in the entry's data field. */
for (width = 0, f = data ; f ; width += SAMPLE_SIZE) {
f &= f - 1;
}
return width;
}
static inline int sample_data_blocks(lines, width)
{
return ((lines * width + 511) / 512);
}
void log_record(int count)
{
uint8_t buffer[512];
uint16_t index;
uint8_t offset;
{
uint32_t *header;
int m, n;
/* We're not currently logging so start now. */
sdio_read_single_block(0, buffer);
sleep_while(sdio_is_busy());
header = (uint32_t *)buffer;
n = header[0];
index = n / ENTRIES_PER_INDEX_BLOCK + 1;
offset = n % ENTRIES_PER_INDEX_BLOCK;
context.width = sample_line_width(data);
if (index == 1 && offset == 0) {
/* This is the first entry so we only need to skip the index
* blocks. */
m = 1 + INDEX_BLOCKS;
} else if (offset == 0) {
uint32_t *entry;
/* The last entry is stored in the previous block so we need
* to fetch it. */
sdio_read_single_block(index - 1, buffer);
sleep_while(sdio_is_busy());
entry = (uint32_t *)(buffer + (ENTRIES_PER_INDEX_BLOCK - 1) *
ENTRY_SIZE);
m = entry[2] + sample_data_blocks(entry[3], context.width);
} else {
uint32_t *entry;
sdio_read_single_block(index, buffer);
sleep_while(sdio_is_busy());
entry = (uint32_t *)(buffer + (offset - 1) *
ENTRY_SIZE);
m = entry[2] + sample_data_blocks(entry[3], context.width);
}
context.key = n + 1;
context.data = data;
context.block = m;
context.target = count;
context.lines = context.fill = context.filling = context.writing = 0;
fusion_set_callback(fusion_data_ready);
sdio_write_multiple_blocks(context.block, NULL,
flush_filled_buffers, NULL);
sleep_while(sdio_is_busy());
}
{
uint32_t *n;
/* Stop logging. */
assert(!(fusion_in_progress()));
assert(context.writing == context.filling);
/* Fetch the master index block. */
sdio_read_single_block(0, buffer);
sleep_while(sdio_is_busy());
/* Increment the number of entries. */
n = (uint32_t *)(buffer + 0);
index = *n / ENTRIES_PER_INDEX_BLOCK + 1;
offset = *n % ENTRIES_PER_INDEX_BLOCK;
*n += 1;
/* Update the master index, if we're starting a new block. */
if (offset == 0) {
n = (uint32_t *)buffer + offset + 1;
*n = context.key;
}
/* Write it back. */
sdio_write_single_block(0, buffer);
sleep_while(sdio_is_busy());
/* Fetch the last index block, add the entry and write it back. */
sdio_read_single_block(index, buffer);
sleep_while(sdio_is_busy());
{
uint32_t *entry = (uint32_t *)(buffer + offset * ENTRY_SIZE);
entry[0] = context.key;
entry[1] = context.data;
entry[2] = context.block;
entry[3] = context.lines;
}
sdio_write_single_block(index, buffer);
sleep_while(sdio_is_busy());
}
}
void log_list()
{
uint8_t buffer[512];
uint32_t *n_p, n;
int i;
/* Fetch the master index block and read the number of logs. */
sdio_read_single_block(0, buffer);
sleep_while(sdio_is_busy());
/* This is necessary so as not to break aliasing rules. */
n_p = (uint32_t *)(buffer + 0);
n = *n_p;
if (n == 0) {
usbserial_printf("No logs.\n");
} else {
for (i = 0 ; i < n ; i += 1) {
uint32_t *entry;
if (i % ENTRIES_PER_INDEX_BLOCK == 0) {
sdio_read_single_block(i / ENTRIES_PER_INDEX_BLOCK + 1, buffer);
sleep_while(sdio_is_busy());
}
entry = (uint32_t *)(buffer + (i % ENTRIES_PER_INDEX_BLOCK) *
ENTRY_SIZE);
usbserial_printf("%d %x %d %d\n",
entry[0], entry[1], entry[2], entry[3]);
}
}
}
void log_replay(uint32_t key, int lines)
{
uint8_t buffer[512];
uint32_t entry[4], *n_p, n;
int i, m, width;
/* Fetch the master index block and read the number of logs. */
sdio_read_single_block(0, buffer);
sleep_while(sdio_is_busy());
/* This is necessary so as not to break aliasing rules. */
n_p = (uint32_t *)(buffer + 0);
n = *n_p;
for (i = 0 ; i <= n / ENTRIES_PER_INDEX_BLOCK ; i += 1) {
if (key < ((uint32_t *)buffer)[i + 1]) {
break;
}
}
if (i == 0) {
usbserial_printf("No such log.\n");
return;
}
/* Fetch the appropriate index block. */
sdio_read_single_block(i, buffer);
sleep_while(sdio_is_busy());
if (i - 1 == n / ENTRIES_PER_INDEX_BLOCK) {
m = n - (i - 1) * ENTRIES_PER_INDEX_BLOCK;
} else {
m = ENTRIES_PER_INDEX_BLOCK;
}
/* usbserial_trace ("n = %d, i = %d, m = %d\n", n, i, m); */
for (i = 0 ; i < m ; i += 1) {
memcpy(entry, buffer + i * sizeof(entry), sizeof(entry));
if (key == entry[0]) {
break;
}
}
if (lines > 0 && lines < entry[3]) {
entry[3] = lines;
}
if (i == m) {
usbserial_printf("No such log.\n");
return;
}
width = sample_line_width(entry[1]);
{
uint8_t scratch[width];
int j, m, spillover = 0;
m = sample_data_blocks (entry[3], width);
for (j = 0 ; j < m ; j += 1) {
int k;
/* Fetch a new block. */
sdio_read_single_block(entry[2] + j, buffer);
while(sdio_is_busy());
if (spillover > 0) {
float *line;
memcpy (scratch + spillover, buffer, width - spillover);
line = (float *)scratch;
usbserial_printf("%+1.2f %+1.2f %+1.2f "
"%+1.2f %+1.2f %+1.2f "
"%+.2f\n",
line[0], line[1], line[2],
line[3], line[4], line[5],
line[9]);
}
if (j == m - 1) {
k = 512 - (m * 512 - entry[3] * width);
} else {
k = sizeof(buffer);
}
for (i = (width - spillover) % width;
i <= k - width;
i += width) {
float *line;
line = (float *)(buffer + i);
usbserial_printf("%+1.2f %+1.2f %+1.2f "
"%+1.2f %+1.2f %+1.2f "
"%+.2f\n",
line[0], line[1], line[2],
line[3], line[4], line[5],
line[9]);
}
if (j < m - 1) {
spillover = sizeof(buffer) - i;
assert (spillover < width);
} else {
spillover = 0;
}
if (spillover > 0) {
memcpy (scratch, buffer + i, spillover);
}
}
}
}
void log_set_data(uint32_t new_data)
{
data = new_data;
usbserial_printf("%d\n", new_data);
}
uint32_t log_get_data()
{
return data;
}