/
blackbox_io.c
672 lines (574 loc) · 19.8 KB
/
blackbox_io.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*
* This file is part of Cleanflight and Betaflight.
*
* Cleanflight and Betaflight are free software. You can redistribute
* this software and/or modify this software under the terms of the
* GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Cleanflight and Betaflight are distributed in the hope that they
* will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software.
*
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "platform.h"
#ifdef USE_BLACKBOX
#include "blackbox.h"
#include "blackbox_io.h"
#include "common/maths.h"
#include "flight/pid.h"
#include "io/asyncfatfs/asyncfatfs.h"
#include "io/flashfs.h"
#include "io/serial.h"
#include "msp/msp_serial.h"
#ifdef USE_SDCARD
#include "drivers/sdcard.h"
#endif
#define BLACKBOX_SERIAL_PORT_MODE MODE_TX
// How many bytes can we transmit per loop iteration when writing headers?
static uint8_t blackboxMaxHeaderBytesPerIteration;
// How many bytes can we write *this* iteration without overflowing transmit buffers or overstressing the OpenLog?
int32_t blackboxHeaderBudget;
static serialPort_t *blackboxPort = NULL;
static portSharing_e blackboxPortSharing;
#ifdef USE_SDCARD
static struct {
afatfsFilePtr_t logFile;
afatfsFilePtr_t logDirectory;
afatfsFinder_t logDirectoryFinder;
uint32_t largestLogFileNumber;
enum {
BLACKBOX_SDCARD_INITIAL,
BLACKBOX_SDCARD_WAITING,
BLACKBOX_SDCARD_ENUMERATE_FILES,
BLACKBOX_SDCARD_CHANGE_INTO_LOG_DIRECTORY,
BLACKBOX_SDCARD_READY_TO_CREATE_LOG,
BLACKBOX_SDCARD_READY_TO_LOG
} state;
} blackboxSDCard;
#define LOGFILE_PREFIX "LOG"
#define LOGFILE_SUFFIX "BFL"
#endif // USE_SDCARD
void blackboxOpen(void)
{
serialPort_t *sharedBlackboxAndMspPort = findSharedSerialPort(FUNCTION_BLACKBOX, FUNCTION_MSP);
if (sharedBlackboxAndMspPort) {
mspSerialReleasePortIfAllocated(sharedBlackboxAndMspPort);
}
}
void blackboxWrite(uint8_t value)
{
switch (blackboxConfig()->device) {
#ifdef USE_FLASHFS
case BLACKBOX_DEVICE_FLASH:
flashfsWriteByte(value); // Write byte asynchronously
break;
#endif
#ifdef USE_SDCARD
case BLACKBOX_DEVICE_SDCARD:
afatfs_fputc(blackboxSDCard.logFile, value);
break;
#endif
case BLACKBOX_DEVICE_SERIAL:
default:
{
int txBytesFree = serialTxBytesFree(blackboxPort);
if (txBytesFree == 0) {
return;
}
serialWrite(blackboxPort, value);
}
break;
}
}
// Print the null-terminated string 's' to the blackbox device and return the number of bytes written
int blackboxWriteString(const char *s)
{
int length;
const uint8_t *pos;
switch (blackboxConfig()->device) {
#ifdef USE_FLASHFS
case BLACKBOX_DEVICE_FLASH:
length = strlen(s);
flashfsWrite((const uint8_t*) s, length, false); // Write asynchronously
break;
#endif // USE_FLASHFS
#ifdef USE_SDCARD
case BLACKBOX_DEVICE_SDCARD:
length = strlen(s);
afatfs_fwrite(blackboxSDCard.logFile, (const uint8_t*) s, length); // Ignore failures due to buffers filling up
break;
#endif // USE_SDCARD
case BLACKBOX_DEVICE_SERIAL:
default:
pos = (uint8_t*) s;
while (*pos) {
blackboxWrite(*pos);
pos++;
}
length = pos - (uint8_t*) s;
break;
}
return length;
}
/**
* If there is data waiting to be written to the blackbox device, attempt to write (a portion of) that now.
*
* Intended to be called regularly for the blackbox device to perform housekeeping.
*/
void blackboxDeviceFlush(void)
{
switch (blackboxConfig()->device) {
#ifdef USE_FLASHFS
/*
* This is our only output device which requires us to call flush() in order for it to write anything. The other
* devices will progressively write in the background without Blackbox calling anything.
*/
case BLACKBOX_DEVICE_FLASH:
flashfsFlushAsync();
break;
#endif // USE_FLASHFS
default:
;
}
}
/**
* If there is data waiting to be written to the blackbox device, attempt to write (a portion of) that now.
*
* Returns true if all data has been written to the device.
*/
bool blackboxDeviceFlushForce(void)
{
switch (blackboxConfig()->device) {
case BLACKBOX_DEVICE_SERIAL:
// Nothing to speed up flushing on serial, as serial is continuously being drained out of its buffer
return isSerialTransmitBufferEmpty(blackboxPort);
#ifdef USE_FLASHFS
case BLACKBOX_DEVICE_FLASH:
return flashfsFlushAsync();
#endif // USE_FLASHFS
#ifdef USE_SDCARD
case BLACKBOX_DEVICE_SDCARD:
/* SD card will flush itself without us calling it, but we need to call flush manually in order to check
* if it's done yet or not!
*/
return afatfs_flush();
#endif // USE_SDCARD
default:
return false;
}
}
/**
* Attempt to open the logging device. Returns true if successful.
*/
bool blackboxDeviceOpen(void)
{
switch (blackboxConfig()->device) {
case BLACKBOX_DEVICE_SERIAL:
{
serialPortConfig_t *portConfig = findSerialPortConfig(FUNCTION_BLACKBOX);
baudRate_e baudRateIndex;
portOptions_e portOptions = SERIAL_PARITY_NO | SERIAL_NOT_INVERTED;
if (!portConfig) {
return false;
}
blackboxPortSharing = determinePortSharing(portConfig, FUNCTION_BLACKBOX);
baudRateIndex = portConfig->blackbox_baudrateIndex;
if (baudRates[baudRateIndex] == 230400) {
/*
* OpenLog's 230400 baud rate is very inaccurate, so it requires a larger inter-character gap in
* order to maintain synchronization.
*/
portOptions |= SERIAL_STOPBITS_2;
} else {
portOptions |= SERIAL_STOPBITS_1;
}
blackboxPort = openSerialPort(portConfig->identifier, FUNCTION_BLACKBOX, NULL, NULL, baudRates[baudRateIndex],
BLACKBOX_SERIAL_PORT_MODE, portOptions);
/*
* The slowest MicroSD cards have a write latency approaching 400ms. The OpenLog's buffer is about 900
* bytes. In order for its buffer to be able to absorb this latency we must write slower than 6000 B/s.
*
* The OpenLager has a 125KB buffer for when the the MicroSD card is busy, so when the user configures
* high baud rates, assume the OpenLager is in use and so there is no need to constrain the writes.
*
* In all other cases, constrain the writes as follows:
*
* Bytes per loop iteration = floor((looptime_ns / 1000000.0) * 6000)
* = floor((looptime_ns * 6000) / 1000000.0)
* = floor((looptime_ns * 3) / 500.0)
* = (looptime_ns * 3) / 500
*/
switch (baudRateIndex) {
case BAUD_1000000:
case BAUD_1500000:
case BAUD_2000000:
case BAUD_2470000:
// assume OpenLager in use, so do not constrain writes
blackboxMaxHeaderBytesPerIteration = BLACKBOX_TARGET_HEADER_BUDGET_PER_ITERATION;
break;
default:
blackboxMaxHeaderBytesPerIteration = constrain((targetPidLooptime * 3) / 500, 1, BLACKBOX_TARGET_HEADER_BUDGET_PER_ITERATION);
break;
};
return blackboxPort != NULL;
}
break;
#ifdef USE_FLASHFS
case BLACKBOX_DEVICE_FLASH:
if (!flashfsIsSupported() || isBlackboxDeviceFull()) {
return false;
}
blackboxMaxHeaderBytesPerIteration = BLACKBOX_TARGET_HEADER_BUDGET_PER_ITERATION;
return true;
break;
#endif // USE_FLASHFS
#ifdef USE_SDCARD
case BLACKBOX_DEVICE_SDCARD:
if (afatfs_getFilesystemState() == AFATFS_FILESYSTEM_STATE_FATAL || afatfs_getFilesystemState() == AFATFS_FILESYSTEM_STATE_UNKNOWN || afatfs_isFull()) {
return false;
}
blackboxMaxHeaderBytesPerIteration = BLACKBOX_TARGET_HEADER_BUDGET_PER_ITERATION;
return true;
break;
#endif // USE_SDCARD
default:
return false;
}
}
/**
* Erase all blackbox logs
*/
#ifdef USE_FLASHFS
void blackboxEraseAll(void)
{
switch (blackboxConfig()->device) {
case BLACKBOX_DEVICE_FLASH:
flashfsEraseCompletely();
break;
default:
//not supported
break;
}
}
/**
* Check to see if erasing is done
*/
bool isBlackboxErased(void)
{
switch (blackboxConfig()->device) {
case BLACKBOX_DEVICE_FLASH:
return flashfsIsReady();
break;
default:
//not supported
return true;
break;
}
}
#endif
/**
* Close the Blackbox logging device.
*/
void blackboxDeviceClose(void)
{
switch (blackboxConfig()->device) {
case BLACKBOX_DEVICE_SERIAL:
// Can immediately close without attempting to flush any remaining data.
// Since the serial port could be shared with other processes, we have to give it back here
closeSerialPort(blackboxPort);
blackboxPort = NULL;
/*
* Normally this would be handled by mw.c, but since we take an unknown amount
* of time to shut down asynchronously, we're the only ones that know when to call it.
*/
if (blackboxPortSharing == PORTSHARING_SHARED) {
mspSerialAllocatePorts();
}
break;
#ifdef USE_FLASHFS
case BLACKBOX_DEVICE_FLASH:
// Some flash device, e.g., NAND devices, require explicit close to flush internally buffered data.
flashfsClose();
break;
#endif
default:
;
}
}
#ifdef USE_SDCARD
static void blackboxLogDirCreated(afatfsFilePtr_t directory)
{
if (directory) {
blackboxSDCard.logDirectory = directory;
afatfs_findFirst(blackboxSDCard.logDirectory, &blackboxSDCard.logDirectoryFinder);
blackboxSDCard.state = BLACKBOX_SDCARD_ENUMERATE_FILES;
} else {
// Retry
blackboxSDCard.state = BLACKBOX_SDCARD_INITIAL;
}
}
static void blackboxLogFileCreated(afatfsFilePtr_t file)
{
if (file) {
blackboxSDCard.logFile = file;
blackboxSDCard.largestLogFileNumber++;
blackboxSDCard.state = BLACKBOX_SDCARD_READY_TO_LOG;
} else {
// Retry
blackboxSDCard.state = BLACKBOX_SDCARD_READY_TO_CREATE_LOG;
}
}
static void blackboxCreateLogFile(void)
{
uint32_t remainder = blackboxSDCard.largestLogFileNumber + 1;
char filename[] = LOGFILE_PREFIX "00000." LOGFILE_SUFFIX;
for (int i = 7; i >= 3; i--) {
filename[i] = (remainder % 10) + '0';
remainder /= 10;
}
blackboxSDCard.state = BLACKBOX_SDCARD_WAITING;
afatfs_fopen(filename, "as", blackboxLogFileCreated);
}
/**
* Begin a new log on the SDCard.
*
* Keep calling until the function returns true (open is complete).
*/
static bool blackboxSDCardBeginLog(void)
{
fatDirectoryEntry_t *directoryEntry;
doMore:
switch (blackboxSDCard.state) {
case BLACKBOX_SDCARD_INITIAL:
if (afatfs_getFilesystemState() == AFATFS_FILESYSTEM_STATE_READY) {
blackboxSDCard.state = BLACKBOX_SDCARD_WAITING;
afatfs_mkdir("logs", blackboxLogDirCreated);
}
break;
case BLACKBOX_SDCARD_WAITING:
// Waiting for directory entry to be created
break;
case BLACKBOX_SDCARD_ENUMERATE_FILES:
while (afatfs_findNext(blackboxSDCard.logDirectory, &blackboxSDCard.logDirectoryFinder, &directoryEntry) == AFATFS_OPERATION_SUCCESS) {
if (directoryEntry && !fat_isDirectoryEntryTerminator(directoryEntry)) {
// If this is a log file, parse the log number from the filename
if (strncmp(directoryEntry->filename, LOGFILE_PREFIX, strlen(LOGFILE_PREFIX)) == 0
&& strncmp(directoryEntry->filename + 8, LOGFILE_SUFFIX, strlen(LOGFILE_SUFFIX)) == 0) {
char logSequenceNumberString[6];
memcpy(logSequenceNumberString, directoryEntry->filename + 3, 5);
logSequenceNumberString[5] = '\0';
blackboxSDCard.largestLogFileNumber = MAX((uint32_t) atoi(logSequenceNumberString), blackboxSDCard.largestLogFileNumber);
}
} else {
// We're done checking all the files on the card, now we can create a new log file
afatfs_findLast(blackboxSDCard.logDirectory);
blackboxSDCard.state = BLACKBOX_SDCARD_CHANGE_INTO_LOG_DIRECTORY;
goto doMore;
}
}
break;
case BLACKBOX_SDCARD_CHANGE_INTO_LOG_DIRECTORY:
// Change into the log directory:
if (afatfs_chdir(blackboxSDCard.logDirectory)) {
// We no longer need our open handle on the log directory
afatfs_fclose(blackboxSDCard.logDirectory, NULL);
blackboxSDCard.logDirectory = NULL;
blackboxSDCard.state = BLACKBOX_SDCARD_READY_TO_CREATE_LOG;
goto doMore;
}
break;
case BLACKBOX_SDCARD_READY_TO_CREATE_LOG:
blackboxCreateLogFile();
break;
case BLACKBOX_SDCARD_READY_TO_LOG:
return true; // Log has been created!
}
// Not finished init yet
return false;
}
#endif // USE_SDCARD
/**
* Begin a new log (for devices which support separations between the logs of multiple flights).
*
* Keep calling until the function returns true (open is complete).
*/
bool blackboxDeviceBeginLog(void)
{
switch (blackboxConfig()->device) {
#ifdef USE_SDCARD
case BLACKBOX_DEVICE_SDCARD:
return blackboxSDCardBeginLog();
#endif // USE_SDCARD
default:
return true;
}
}
/**
* Terminate the current log (for devices which support separations between the logs of multiple flights).
*
* retainLog - Pass true if the log should be kept, or false if the log should be discarded (if supported).
*
* Keep calling until this returns true
*/
bool blackboxDeviceEndLog(bool retainLog)
{
#ifndef USE_SDCARD
UNUSED(retainLog);
#endif
switch (blackboxConfig()->device) {
#ifdef USE_SDCARD
case BLACKBOX_DEVICE_SDCARD:
// Keep retrying until the close operation queues
if (
(retainLog && afatfs_fclose(blackboxSDCard.logFile, NULL))
|| (!retainLog && afatfs_funlink(blackboxSDCard.logFile, NULL))
) {
// Don't bother waiting the for the close to complete, it's queued now and will complete eventually
blackboxSDCard.logFile = NULL;
blackboxSDCard.state = BLACKBOX_SDCARD_READY_TO_CREATE_LOG;
return true;
}
return false;
#endif // USE_SDCARD
default:
return true;
}
}
bool isBlackboxDeviceFull(void)
{
switch (blackboxConfig()->device) {
case BLACKBOX_DEVICE_SERIAL:
return false;
#ifdef USE_FLASHFS
case BLACKBOX_DEVICE_FLASH:
return flashfsIsEOF();
#endif // USE_FLASHFS
#ifdef USE_SDCARD
case BLACKBOX_DEVICE_SDCARD:
return afatfs_isFull();
#endif // USE_SDCARD
default:
return false;
}
}
bool isBlackboxDeviceWorking(void)
{
switch (blackboxConfig()->device) {
case BLACKBOX_DEVICE_SERIAL:
return blackboxPort != NULL;
#ifdef USE_SDCARD
case BLACKBOX_DEVICE_SDCARD:
return sdcard_isInserted() && sdcard_isFunctional() && (afatfs_getFilesystemState() == AFATFS_FILESYSTEM_STATE_READY);
#endif
#ifdef USE_FLASHFS
case BLACKBOX_DEVICE_FLASH:
return flashfsIsReady();
#endif
default:
return false;
}
}
unsigned int blackboxGetLogNumber(void)
{
#ifdef USE_SDCARD
return blackboxSDCard.largestLogFileNumber;
#endif
return 0;
}
/**
* Call once every loop iteration in order to maintain the global blackboxHeaderBudget with the number of bytes we can
* transmit this iteration.
*/
void blackboxReplenishHeaderBudget(void)
{
int32_t freeSpace;
switch (blackboxConfig()->device) {
case BLACKBOX_DEVICE_SERIAL:
freeSpace = serialTxBytesFree(blackboxPort);
break;
#ifdef USE_FLASHFS
case BLACKBOX_DEVICE_FLASH:
freeSpace = flashfsGetWriteBufferFreeSpace();
break;
#endif
#ifdef USE_SDCARD
case BLACKBOX_DEVICE_SDCARD:
freeSpace = afatfs_getFreeBufferSpace();
break;
#endif
default:
freeSpace = 0;
}
blackboxHeaderBudget = MIN(MIN(freeSpace, blackboxHeaderBudget + blackboxMaxHeaderBytesPerIteration), BLACKBOX_MAX_ACCUMULATED_HEADER_BUDGET);
}
/**
* You must call this function before attempting to write Blackbox header bytes to ensure that the write will not
* cause buffers to overflow. The number of bytes you can write is capped by the blackboxHeaderBudget. Calling this
* reservation function doesn't decrease blackboxHeaderBudget, so you must manually decrement that variable by the
* number of bytes you actually wrote.
*
* When the Blackbox device is FlashFS, a successful return code guarantees that no data will be lost if you write that
* many bytes to the device (i.e. FlashFS's buffers won't overflow).
*
* When the device is a serial port, a successful return code guarantees that Cleanflight's serial Tx buffer will not
* overflow, and the outgoing bandwidth is likely to be small enough to give the OpenLog time to absorb MicroSD card
* latency. However the OpenLog could still end up silently dropping data.
*
* Returns:
* BLACKBOX_RESERVE_SUCCESS - Upon success
* BLACKBOX_RESERVE_TEMPORARY_FAILURE - The buffer is currently too full to service the request, try again later
* BLACKBOX_RESERVE_PERMANENT_FAILURE - The buffer is too small to ever service this request
*/
blackboxBufferReserveStatus_e blackboxDeviceReserveBufferSpace(int32_t bytes)
{
if (bytes <= blackboxHeaderBudget) {
return BLACKBOX_RESERVE_SUCCESS;
}
// Handle failure:
switch (blackboxConfig()->device) {
case BLACKBOX_DEVICE_SERIAL:
/*
* One byte of the tx buffer isn't available for user data (due to its circular list implementation),
* hence the -1. Note that the USB VCP implementation doesn't use a buffer and has txBufferSize set to zero.
*/
if (blackboxPort->txBufferSize && bytes > (int32_t) blackboxPort->txBufferSize - 1) {
return BLACKBOX_RESERVE_PERMANENT_FAILURE;
}
return BLACKBOX_RESERVE_TEMPORARY_FAILURE;
#ifdef USE_FLASHFS
case BLACKBOX_DEVICE_FLASH:
if (bytes > (int32_t) flashfsGetWriteBufferSize()) {
return BLACKBOX_RESERVE_PERMANENT_FAILURE;
}
if (bytes > (int32_t) flashfsGetWriteBufferFreeSpace()) {
/*
* The write doesn't currently fit in the buffer, so try to make room for it. Our flushing here means
* that the Blackbox header writing code doesn't have to guess about the best time to ask flashfs to
* flush, and doesn't stall waiting for a flush that would otherwise not automatically be called.
*/
flashfsFlushAsync();
}
return BLACKBOX_RESERVE_TEMPORARY_FAILURE;
#endif // USE_FLASHFS
#ifdef USE_SDCARD
case BLACKBOX_DEVICE_SDCARD:
// Assume that all writes will fit in the SDCard's buffers
return BLACKBOX_RESERVE_TEMPORARY_FAILURE;
#endif // USE_SDCARD
default:
return BLACKBOX_RESERVE_PERMANENT_FAILURE;
}
}
#endif // BLACKBOX