Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
sdLog.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2013-2016 Gautier Hattenberger, Alexandre Bustico
3 *
4 * This file is part of paparazzi.
5 *
6 * paparazzi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * paparazzi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with paparazzi; see the file COPYING. If not, write to
18 * the Free Software Foundation, 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22/*
23 * @file modules/loggers/sdlog_chibios/sdLog.c
24 * @brief sdlog API using ChibiOS and Fatfs
25 *
26 */
27
28#include <string.h>
29#include <stdlib.h>
30#include <ch.h>
31#include <hal.h>
32#include <ff.h>
34#include "printf.h"
35#include "mcu_periph/sdio.h"
36#include <ctype.h>
37
38#ifndef MIN
39#define MIN(x , y) (((x) < (y)) ? (x) : (y))
40#endif
41#ifndef MAX
42#define MAX(x , y) (((x) > (y)) ? (x) : (y))
43#endif
44#define IS_POWER_OF_TWO(s) ((s) && !((s) & ((s) - 1)))
45
46#ifndef SDLOG_NUM_FILES
47#error SDLOG_NUM_FILES should be defined in mcuconf.h
48#endif
49
50
51#ifndef FFCONF_DEF
52#define FFCONF_DEF _FATFS
53#endif
54
55#if FFCONF_DEF < 8000
56#error upgrade FATFS to 0.14 at least
57#else // FFCONF_DEF > 8000
58#if FF_FS_LOCK != 0 && FF_FS_LOCK < SDLOG_NUM_FILES
59#error if FF_FS_LOCK is not zero, it should be equal of superior to SDLOG_NUM_FILES
60#endif
61#endif
62
63#ifndef SDLOG_ALL_BUFFERS_SIZE
64#error SDLOG_ALL_BUFFERS_SIZE should be defined in mcuconf.h
65#endif
66
67#if SDLOG_ALL_BUFFERS_SIZE > 65536
68#error constraint 512 <= SDLOG_ALL_BUFFERS_SIZE <= 65536 not meet
69#endif
70
71#define SDLOG_WRITE_BUFFER_SIZE (SDLOG_ALL_BUFFERS_SIZE/SDLOG_NUM_FILES)
72
73#ifndef SDLOG_MAX_MESSAGE_LEN
74#error SDLOG_MAX_MESSAGE_LENshould be defined in mcuconf.h
75#endif
76
77#ifndef SDLOG_QUEUE_BUCKETS
78#error SDLOG_QUEUE_BUCKETS should be defined in mcuconf.h
79#endif
80
81#if FF_FS_REENTRANT == 0
82#warning "FF_FS_REENTRANT = 0 in ffconf.h DO NOT open close file during log"
83#endif
84
85#if SDLOG_WRITE_BUFFER_SIZE < 512
86#error SDLOG_ALL_BUFFERS_SIZE / SDLOG_NUM_FILES cannot be < 512
87#endif
88
89#if (!(IS_POWER_OF_TWO (SDLOG_WRITE_BUFFER_SIZE)))
90#error SDLOG_ALL_BUFFERS_SIZE / SDLOG_NUM_FILES should be a POWER OF 2
91#endif
92
93#ifdef SDLOG_NEED_QUEUE
95
96#include "mcu_periph/ram_arch.h"
97
98
99/*
100 The buffers that do DMA are the caches (named buf) in the FIL and FATFS struct of fatfs library
101 It's the only buffers that have to reside in DMA capable memory.
102
103 The buffer associated with message queue, and the cache buffer for caching file write
104 could reside in non DMA capable memory.
105
106 stm32f4 : regular sram : 128ko, dma, slow
107 ccm sram : 64ko, no_dma, fast
108
109 stm32f7 : regular sram : 256ko, dma only possible if data cache are explicitely flushed, fast
110 dtcm sram : 64ko, dma, slow (no cache)
111
112 stm32h7 :
113 ram0nc (wx) : org = 0x24000000, len = 128k : non cached for DMA stuff
114 ram0 (wx) : org = 0x24000000, len = 384k : standard
115 ram1 (wx) : org = 0x30000000, len = 256k
116 ram2 (wx) : org = 0x30000000, len = 288k : ram1+ram3
117 ram3 (wx) : org = 0x30040000, len = 32k
118 ram4 (wx) : org = 0x38000000, len = 64k
119 ram5 (wx) : org = 0x20000000, len = 128k : DTCM : fast ram
120 ram6 (wx) : org = 0x00000000, len = 64k
121 ram7 (wx) : org = 0x38800000, len = 4k
122 */
123
124
125
126#ifdef STM32H7XX
127#define IN_SDMMC_DMA_SECTION(x) IN_SDMMC_SECTION(x)
128#define IN_SDMMC_DMA_SECTION_CLEAR(x) IN_SDMMC_SECTION_CLEAR(x)
129#define IN_SDMMC_DMA_SECTION_NOINIT(x) IN_SDMMC_SECTION_NOINIT(x)
130#else
131#define IN_SDMMC_DMA_SECTION(x) IN_DMA_SECTION(x)
132#define IN_SDMMC_DMA_SECTION_CLEAR(x) IN_DMA_SECTION_CLEAR(x)
133#define IN_SDMMC_DMA_SECTION_NOINIT(x) IN_DMA_SECTION_NOINIT(x)
134#endif
135/*
136 s*printf functions are supposed to be thread safe, but the ones provided by
137 newlib function are not. One has to use _s*printf_r family that take a
138 struct _reent as first parameter.
139 */
140
141// #pragma GCC diagnostic push
142// #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
143// static struct _reent reent = _REENT_INIT(reent);
144// #pragma GCC diagnostic pop
145
146
147
148
149static msg_t IN_STD_SECTION_CLEAR(queMbBuffer[SDLOG_QUEUE_BUCKETS]);
150static MsgQueue messagesQueue;
151
152#define WRITE_BYTE_CACHE_SIZE 15 // limit overhead :
153// malloc (15+1) occupies 20bytes
154typedef struct {
155 uint8_t fcntl: 2;
156 uint8_t fd: 6;
157} FileOp;
158
159struct LogMessage {
160 FileOp op;
161 char mess[0];
162};
163
164struct FilePoolUnit {
165 FIL fil;
166 uint32_t autoFlushPeriod;
167 systime_t lastFlushTs;
168 bool inUse;
169 bool tagAtClose;
170 // optimise write byte by caching at send level now that we are based upon tlsf where
171 // allocation granularity is 16 bytes
172 LogMessage *writeByteCache;
173 uint8_t writeByteSeek;
174};
175
176static struct FilePoolUnit IN_SDMMC_DMA_SECTION(fileDes[SDLOG_NUM_FILES]) = {
177 [0 ... SDLOG_NUM_FILES - 1] = {
178 .fil = {{0}}, .inUse = false, .tagAtClose = false,
179 .writeByteCache = NULL, .writeByteSeek = 0
180 }
181};
182
183static volatile size_t nbBytesWritten = 0;
184static SdioError storageStatus = SDLOG_OK;
185
186typedef enum {
187 FCNTL_WRITE = 0b00,
188 FCNTL_FLUSH = 0b01,
189 FCNTL_CLOSE = 0b10,
190 FCNTL_EXIT = 0b11
191} FileFcntl;
192
193struct _SdLogBuffer {
194 LogMessage *lm;
195 size_t len;
197} ;
198
199
200#define LOG_MESSAGE_PREBUF_LEN (SDLOG_MAX_MESSAGE_LEN+sizeof(LogMessage))
201#endif // SDLOG_NEED_QUEUE
202
203/* File system object */
204static IN_SDMMC_DMA_SECTION(FATFS fatfs);
205
206#ifdef SDLOG_NEED_QUEUE
207static size_t logMessageLen(const LogMessage *lm);
208static size_t logRawLen(const size_t len);
209static SdioError sdLoglaunchThread(void);
210static SdioError sdLogStopThread(void);
211static thread_t *sdLogThd = NULL;
212static SdioError getNextFIL(FileDes *fd);
213static void removeFromQueue(const size_t nbMsgToRFemov);
214static void cleanQueue(const bool allQueue);
215static SdioError sdLogExpandLogFile(const FileDes fileObject, const size_t sizeInMo,
216 const bool preallocate);
217
218static void thdSdLog(void *arg) ;
219
220#endif // SDLOG_NEED_QUEUE
221
222static int32_t uiGetIndexOfLogFile(const char *prefix, const char *fileName) ;
224
225static FRESULT lastFatfsResult = FR_OK;
226
231
236static SdioError fatfsError(FRESULT rc)
237{
238 lastFatfsResult = rc;
239 if (rc == FR_NOT_READY || rc == FR_DISK_ERR) {
240 return SDLOG_NOCARD;
241 }
242 return SDLOG_FATFS_ERROR;
243}
244
246{
247 DWORD clusters = 0;
248 FATFS *fsp = NULL;
249 nbBytesWritten = 0;
250
251 lastFatfsResult = FR_OK;
252
253 // if init is already done, return ERROR
254 if (sdLogThd != NULL) {
255 *freeSpaceInKo = 0;
256 return storageStatus = SDLOG_WAS_LAUNCHED;
257 }
258
259#ifdef SDLOG_NEED_QUEUE
260 msgqueue_init(&messagesQueue, &HEAP_DEFAULT, queMbBuffer, SDLOG_QUEUE_BUCKETS);
261#endif
262
263 if (!sdc_lld_is_card_inserted(NULL)) {
264 return storageStatus = SDLOG_NOCARD;
265 }
266
267
268 sdio_connect(&SDLOG_SDIO);
269 chThdSleepMilliseconds(10);
270 sdio_disconnect(&SDLOG_SDIO);
271
272 if (sdio_connect(&SDLOG_SDIO) == FALSE) {
273 return storageStatus = SDLOG_NOCARD;
274 }
275
276#if FFCONF_DEF < 8000
277 FRESULT rc = f_mount(0, &fatfs);
278#else
279 FRESULT rc = f_mount(&fatfs, "/", 1);
280#endif
281
282 if (rc != FR_OK) {
283 return storageStatus = fatfsError(rc);
284 }
285
286 if (freeSpaceInKo != NULL) {
287 f_getfree("/", &clusters, &fsp);
288 *freeSpaceInKo = clusters * (uint32_t)fatfs.csize / 2;
289 }
290
291#ifdef SDLOG_NEED_QUEUE
292 for (uint8_t i = 0; i < SDLOG_NUM_FILES; i++) {
293 fileDes[i].inUse = fileDes[i].tagAtClose = false;
294 fileDes[i].writeByteCache = NULL;
295 fileDes[i].writeByteSeek = 0;
296 }
297
298 storageStatus = sdLoglaunchThread();
299#else
300 storageStatus = SDLOG_OK;
301#endif
302 return storageStatus;
303
304}
305
306
308{
309#if FFCONF_DEF < 8000
310 FRESULT rc = f_mount(0, NULL);
311#else
312 FRESULT rc = f_mount(NULL, "", 0);
313#endif
314 if (rc != FR_OK) {
315 return storageStatus = SDLOG_FATFS_ERROR;
316 }
317
318 // if we mount, unmount, don't disconnect sdio
319 /* if (sdio_disconnect (&SDLOG_SDIO) == FALSE) */
320 /* return SDLOG_NOCARD; */
321
322 return storageStatus = SDLOG_OK;
323}
324
325
326
327#ifdef SDLOG_NEED_QUEUE
328SdioError sdLogOpenLog(FileDes *fd, const char *directoryName, const char *prefix,
329 const uint32_t autoFlushPeriod, const bool appendTagAtClose,
330 const size_t sizeInMo, const bool preallocate, char *fileName, const size_t nameLength)
331{
332 FRESULT rc; /* fatfs result code */
333 SdioError sde = SDLOG_OK; /* sdio result code */
334 //DIR dir; /* Directory object */
335 //FILINFO fno; /* File information object */
336
337 /* local file descriptor
338 using fd is a bad idea since fd is set before fatfs objets are coherents
339 in a multithreaded application where sdLogXXX are done before sdLogWriteLog is done
340 we can have a race condition. setting fd only when fatfs files are opened resolve the problem
341 */
342 FileDes ldf;
343
344 sde = getNextFIL(&ldf);
345 if (sde != SDLOG_OK) {
346 return storageStatus = sde;
347 }
348
349 sde = getFileName(prefix, directoryName, fileName, nameLength, +1);
350 if (sde != SDLOG_OK) {
351 // sd card is not inserted (or the folder cannot be read), so logging task can be deleted
352 fileDes[ldf].inUse = false;
353 return storageStatus = (sde == SDLOG_NOCARD) ? SDLOG_NOCARD : SDLOG_FATFS_ERROR;
354 }
355
356
357 rc = f_open(&fileDes[ldf].fil, fileName, FA_WRITE | FA_CREATE_ALWAYS);
358 if (rc) {
359 fileDes[ldf].inUse = false;
360 return storageStatus = fatfsError(rc);
361 } else {
362 fileDes[ldf].tagAtClose = appendTagAtClose;
363 fileDes[ldf].autoFlushPeriod = autoFlushPeriod;
364 fileDes[ldf].lastFlushTs = 0;
365 sde = sdLogExpandLogFile(ldf, sizeInMo, preallocate);
366 }
367
368 *fd = ldf;
369 return storageStatus = sde;
370}
371
372SdioError sdLogCloseAllLogs(bool flush)
373{
374 FRESULT rc = 0; /* Result code */
375
376 // do not flush what is in ram, close as soon as possible
377 if (flush == false) {
378 UINT bw;
379 // stop worker thread then close file
380 cleanQueue(true);
381 sdLogStopThread();
382
383 for (FileDes fd = 0; fd < SDLOG_NUM_FILES; fd++) {
384 if (fileDes[fd].inUse) {
385 FIL *fo = &fileDes[fd].fil;
386 if (fileDes[fd].tagAtClose) {
387 f_write(fo, "\r\nEND_OF_LOG\r\n", 14, &bw);
388 nbBytesWritten += bw;
389 }
390 FRESULT trc = f_close(fo);
391 fileDes[fd].inUse = false;
392 if (!rc) {
393 rc = trc;
394 }
395 }
396 }
397
398 if (rc) {
399 return storageStatus = SDLOG_FATFS_ERROR;
400 }
401
402 // flush ram buffer then close
403 } else { // flush == true
404 if (sdLogThd == NULL) {
405 // something goes wrong, log thread is no more working
406 return storageStatus = SDLOG_NOTHREAD;
407 }
408
409 // queue flush + close order, then stop worker thread
410 for (FileDes fd = 0; fd < SDLOG_NUM_FILES; fd++) {
411 if (fileDes[fd].inUse) {
413 sdLogCloseLog(fd);
414 }
415 }
416
417 LogMessage *lm = tlsf_malloc_r(&HEAP_DEFAULT, sizeof(LogMessage));
418 if (lm == NULL) {
419 return storageStatus = SDLOG_MEMFULL;
420 }
421
422 lm->op.fcntl = FCNTL_EXIT;
423
424 if (msgqueue_send(&messagesQueue, lm, sizeof(LogMessage), MsgQueue_REGULAR) < 0) {
425 return storageStatus = SDLOG_QUEUEFULL;
426 } else {
427 chThdWait(sdLogThd);
428 sdLogThd = NULL;
429 }
430
431 }
432 return storageStatus = SDLOG_OK;
433}
434
436{
438 if (sdLogThd == NULL) {
439 // something goes wrong, log thread is no more working
440 return storageStatus = SDLOG_NOTHREAD;
441 }
442
443 // queue flush + close order, then stop worker thread
444 for (FileDes fd = 0; fd < SDLOG_NUM_FILES; fd++) {
445 if (fileDes[fd].inUse) {
447 if (status != SDLOG_OK) {
448 break;
449 }
450 }
451 }
452
453 return storageStatus = status;
454}
455
456
457#define FD_CHECK(fd) if ((fd < 0) || (fd >= SDLOG_NUM_FILES) \
458 || (fileDes[fd].inUse == false)) \
459 return SDLOG_FATFS_ERROR
460
461
462SdioError sdLogExpandLogFile(const FileDes fd, const size_t sizeInMo,
463 const bool preallocate)
464{
465 FD_CHECK(fd);
466
467 // expand with opt=1 : pre allocate file now
468 const FRESULT rc = f_expand(&fileDes[fd].fil, sizeInMo * 1024 * 1024, preallocate);
469 return (rc == FR_OK) ? SDLOG_OK : SDLOG_CANNOT_EXPAND;
470}
471
472SdioError sdLogWriteLog(const FileDes fd, const char *fmt, ...)
473{
474 FD_CHECK(fd);
475
477 storageStatus = status;
478 if (status != SDLOG_OK) {
479 return status;
480 }
481
482 va_list ap;
483 va_start(ap, fmt);
484
485 LogMessage *lm = tlsf_malloc_r(&HEAP_DEFAULT, LOG_MESSAGE_PREBUF_LEN);
486 if (lm == NULL) {
487 va_end(ap);
488 return storageStatus = SDLOG_MEMFULL;
489 }
490
491 lm->op.fcntl = FCNTL_WRITE;
492 lm->op.fd = fd & 0x1f;
493
494 chvsnprintf(lm->mess, SDLOG_MAX_MESSAGE_LEN - 1, fmt, ap);
495 lm->mess[SDLOG_MAX_MESSAGE_LEN - 1] = 0;
496 va_end(ap);
497
498 const size_t msgLen = logMessageLen(lm);
499 lm = tlsf_realloc_r(&HEAP_DEFAULT, lm, msgLen);
500 if (lm == NULL) {
501 return storageStatus = SDLOG_MEMFULL;
502 }
503
504 if (msgqueue_send(&messagesQueue, lm, msgLen, MsgQueue_REGULAR) < 0) {
505 return storageStatus = SDLOG_QUEUEFULL;
506 }
507
508 return SDLOG_OK;
509}
510
512{
513 FD_CHECK(fd);
514
516 storageStatus = status;
517 if (status != SDLOG_OK) {
518 return status;
519 }
520
521 // give room to send a flush order if the queue is full
522 cleanQueue(false);
523
524 LogMessage *lm = tlsf_malloc_r(&HEAP_DEFAULT, sizeof(LogMessage));
525 if (lm == NULL) {
526 return storageStatus = SDLOG_MEMFULL;
527 }
528
529 lm->op.fcntl = FCNTL_FLUSH;
530 lm->op.fd = fd & 0x1f;
531
532 if (msgqueue_send(&messagesQueue, lm, sizeof(LogMessage), MsgQueue_REGULAR) < 0) {
533 return storageStatus = SDLOG_QUEUEFULL;
534 }
535
536 return SDLOG_OK;
537}
538
539SdioError sdLogCloseLog(const FileDes fd)
540{
541 FD_CHECK(fd);
542
543 cleanQueue(false);
544 LogMessage *lm = tlsf_malloc_r(&HEAP_DEFAULT, sizeof(LogMessage));
545 if (lm == NULL) {
546 return storageStatus = SDLOG_MEMFULL;
547 }
548
549 lm->op.fcntl = FCNTL_CLOSE;
550 lm->op.fd = fd & 0x1f;
551
552 if (msgqueue_send(&messagesQueue, lm, sizeof(LogMessage), MsgQueue_REGULAR) < 0) {
553 return storageStatus = SDLOG_QUEUEFULL;
554 }
555
556 return storageStatus = SDLOG_OK;
557}
558
559
560
561
562
563static inline SdioError flushWriteByteBuffer(const FileDes fd)
564{
565 FD_CHECK(fd);
566
567 if (unlikely(fileDes[fd].writeByteCache != NULL)) {
568 if (msgqueue_send(&messagesQueue, fileDes[fd].writeByteCache,
569 sizeof(LogMessage) + fileDes[fd].writeByteSeek,
570 MsgQueue_REGULAR) < 0) {
571 return storageStatus = SDLOG_QUEUEFULL;
572 }
573 fileDes[fd].writeByteCache = NULL;
574 }
575 return storageStatus = SDLOG_OK;
576}
577
578SdioError sdLogWriteRaw(const FileDes fd, const uint8_t *buffer, const size_t len)
579{
580 FD_CHECK(fd);
581
583 storageStatus = status;
584 if (status != SDLOG_OK) {
585 return status;
586 }
587
588 LogMessage *lm = tlsf_malloc_r(&HEAP_DEFAULT, logRawLen(len));
589 if (lm == NULL) {
590 return storageStatus = SDLOG_MEMFULL;
591 }
592
593 lm->op.fcntl = FCNTL_WRITE;
594 lm->op.fd = fd & 0x1f;
595 memcpy(lm->mess, buffer, len);
596
597 if (msgqueue_send(&messagesQueue, lm, logRawLen(len), MsgQueue_REGULAR) < 0) {
598 return storageStatus = SDLOG_QUEUEFULL;
599 }
600
601 return SDLOG_OK;
602}
603
604SdioError sdLogAllocSDB(SdLogBuffer **sdb, const size_t len)
605{
606 *sdb = tlsf_malloc_r(&HEAP_DEFAULT, logRawLen(len));
607 if (*sdb == NULL) {
608 return storageStatus = SDLOG_MEMFULL;
609 }
610
611 LogMessage *lm = tlsf_malloc_r(&HEAP_DEFAULT, logRawLen(len));
612 if (lm == NULL) {
614 return storageStatus = SDLOG_MEMFULL;
615 }
616
617 (*sdb)->lm = lm;
618 (*sdb)->len = len;
619 (*sdb)->offset = 0;
620 return storageStatus = SDLOG_OK;
621}
622
623char *sdLogGetBufferFromSDB(SdLogBuffer *sdb)
624{
625 return sdb->lm->mess + sdb->offset;
626}
627
628bool sdLogSeekBufferFromSDB(SdLogBuffer *sdb, uint32_t offset)
629{
630 if ((sdb->offset + offset) < sdb->len) {
631 sdb->offset += offset;
632 return true;
633 } else {
634 return false;
635 }
636}
637
638size_t sdLogGetBufferLenFromSDB(SdLogBuffer *sdb)
639{
640 return sdb->len - sdb->offset;
641}
642
643SdioError sdLogWriteSDB(const FileDes fd, SdLogBuffer *sdb)
644{
646
647 if ((fd < 0) || (fd >= SDLOG_NUM_FILES) || (fileDes[fd].inUse == false)) {
649 goto fail;
650 }
651
653 if (status != SDLOG_OK) {
654 goto fail;
655 }
656
657
658 sdb->lm->op.fcntl = FCNTL_WRITE;
659 sdb->lm->op.fd = fd & 0x1f;
660
661 if (msgqueue_send(&messagesQueue, sdb->lm, logRawLen(sdb->len), MsgQueue_REGULAR) < 0) {
662 // msgqueue_send take care of freeing lm memory even in case of failure
663 // just need to free sdb memory
665 }
666
667 goto exit;
668
669fail:
670 tlsf_free_r(&HEAP_DEFAULT, sdb->lm);
671
672exit:
674 return storageStatus = status;
675}
676
677
678
679
680SdioError sdLogWriteByte(const FileDes fd, const uint8_t value)
681{
682 FD_CHECK(fd);
683 LogMessage *lm;
684
685 if (fileDes[fd].writeByteCache == NULL) {
686 lm = tlsf_malloc_r(&HEAP_DEFAULT, sizeof(LogMessage) + WRITE_BYTE_CACHE_SIZE);
687 if (lm == NULL) {
688 return storageStatus = SDLOG_MEMFULL;
689 }
690
691 lm->op.fcntl = FCNTL_WRITE;
692 lm->op.fd = fd & 0x1f;
693
694 fileDes[fd].writeByteCache = lm;
695 fileDes[fd].writeByteSeek = 0;
696 } else {
697 lm = fileDes[fd].writeByteCache;
698 }
699
700 lm->mess[fileDes[fd].writeByteSeek++] = value;
701
702 if (fileDes[fd].writeByteSeek == WRITE_BYTE_CACHE_SIZE) {
704 // message is not sent so allocated buffer will not be released by receiver side
705 // instead of freeing buffer, we just reset cache seek.
706 if (status == SDLOG_QUEUEFULL) {
707 fileDes[fd].writeByteSeek = 0;
708 return storageStatus = SDLOG_QUEUEFULL;
709 }
710 }
711 return storageStatus = SDLOG_OK;
712}
713
714/*
715 if fatfs use stack for working buffers, stack size should be reserved accordingly
716 */
717#define WA_LOG_BASE_SIZE 1024
718#if FF_USE_LFN == 2
719#if FF_FS_EXFAT
720static IN_SDMMC_DMA_SECTION_NOINIT(THD_WORKING_AREA(waThdSdLog, WA_LOG_BASE_SIZE+((FF_MAX_LFN+1)*2)+(19*32)));
721#else
722static IN_SDMMC_DMA_SECTION_NOINIT(THD_WORKING_AREA(waThdSdLog, WA_LOG_BASE_SIZE+((FF_MAX_LFN+1)*2)));
723#endif
724#else
725static THD_WORKING_AREA(waThdSdLog, WA_LOG_BASE_SIZE);
726#endif
727
728SdioError sdLoglaunchThread ()
729{
730 chThdSleepMilliseconds(100);
731
732 sdLogThd = chThdCreateStatic(waThdSdLog, sizeof(waThdSdLog),
733 NORMALPRIO + 1, thdSdLog, NULL);
734 if (sdLogThd == NULL) {
735 return storageStatus = SDLOG_INTERNAL_ERROR;
736 } else {
737 return storageStatus = SDLOG_OK;
738 }
739}
740
741SdioError sdLogStopThread(void)
742{
743 SdioError retVal = SDLOG_OK;
744
745 storageStatus = retVal;
746 if (sdLogThd == NULL) {
747 return storageStatus = SDLOG_NOTHREAD;
748 }
749
750 LogMessage lm;
751
752 /* // ask for closing (after flushing) all opened files */
753 /* for (uint8_t i=0; i<SDLOG_NUM_FILES; i++) { */
754 /* if (fileDes[i].inUse) { */
755 /* flushWriteByteBuffer (i); */
756 /* lm.op.fcntl = FCNTL_CLOSE; */
757 /* lm.op.fd = i & 0x1f; */
758 /* if (msgqueue_copy_send (&messagesQueue, &lm, sizeof(LogMessage), MsgQueue_OUT_OF_BAND) < 0) { */
759 /* retVal= SDLOG_QUEUEFULL; */
760 /* } */
761 /* } */
762 /* } */
763
764 lm.op.fcntl = FCNTL_EXIT;
765 if (msgqueue_copy_send(&messagesQueue, &lm, sizeof(LogMessage), MsgQueue_OUT_OF_BAND) < 0) {
766 retVal = SDLOG_QUEUEFULL;
767 }
768
769 chThdWait(sdLogThd);
770 sdLogThd = NULL;
771 return storageStatus = retVal;
772}
773#endif
774
775
776SdioError getFileName(const char *prefix, const char *directoryName,
777 char *nextFileName, const size_t nameLength, const int indexOffset)
778{
779 DIR dir; /* Directory object */
780 FRESULT rc; /* Result code */
781 FILINFO fno; /* File information object */
782 int32_t fileIndex ;
783 int32_t maxCurrentIndex = 0;
784
785
786 const size_t directoryNameLen = MIN(strlen(directoryName), 128);
787 const size_t slashDirNameLen = directoryNameLen + 2;
788 char slashDirName[slashDirNameLen];
789 strlcpy(slashDirName, "/", slashDirNameLen);
790 strlcat(slashDirName, directoryName, slashDirNameLen);
791
792 rc = f_opendir(&dir, directoryName);
793 if (rc != FR_OK) {
794 rc = f_mkdir(slashDirName);
795 if (rc != FR_OK) {
796 return storageStatus = fatfsError(rc);
797 }
798 rc = f_opendir(&dir, directoryName);
799 if (rc != FR_OK) {
800 return storageStatus = fatfsError(rc);
801 }
802 }
803
804 for (;;) {
805 rc = f_readdir(&dir, &fno); /* Read a directory item */
806 if (rc != FR_OK || fno.fname[0] == 0) { break; } /* Error or end of dir */
807
808
809 if (fno.fname[0] == '.') { continue; }
810
811 if (!(fno.fattrib & AM_DIR)) {
812 // DebugTrace ("fno.fsize=%d fn=%s\n", fno.fsize, fn);
813 fileIndex = uiGetIndexOfLogFile(prefix, fno.fname);
814 maxCurrentIndex = MAX(maxCurrentIndex, fileIndex);
815 }
816 }
817 if (rc) {
818 return storageStatus = fatfsError(rc);
819 }
820
821 rc = f_closedir(&dir);
822 if (rc) {
823 return storageStatus = fatfsError(rc);
824 }
825
826 if (maxCurrentIndex < NUMBERMAX) {
827 chsnprintf(nextFileName, nameLength, NUMBERFMF,
828 directoryName, prefix, maxCurrentIndex + indexOffset);
829 return storageStatus = SDLOG_OK;
830 } else {
831 chsnprintf(nextFileName, nameLength, "%s\\%s%.ERR",
832 directoryName, prefix);
833 return storageStatus = SDLOG_LOGNUM_ERROR;
834 }
835}
836
837SdioError removeEmptyLogs(const char *directoryName, const char *prefix, const size_t sizeConsideredEmpty)
838{
839 DIR dir; /* Directory object */
840 FRESULT rc; /* Result code */
841 FILINFO fno; /* File information object */
842
843
844 rc = f_opendir(&dir, directoryName);
845 if (rc != FR_OK) {
846 return storageStatus = SDLOG_FATFS_NOENT;
847 }
848
849 for (;;) {
850 rc = f_readdir(&dir, &fno); /* Read a directory item */
851 if (rc != FR_OK || fno.fname[0] == 0) { break; } /* Error or end of dir */
852
853 if (fno.fname[0] == '.') { continue; }
854
855 if (!(fno.fattrib & AM_DIR)) {
856 // DebugTrace ("fno.fsize=%d fn=%s\n", fno.fsize, fn);
857 if ((strncmp(fno.fname, prefix, strlen(prefix)) == 0) && (fno.fsize <= sizeConsideredEmpty)) {
858 char absPathName[128];
859 strlcpy(absPathName, directoryName, sizeof(absPathName));
860 strlcat(absPathName, "/", sizeof(absPathName));
861 strlcat(absPathName, fno.fname, sizeof(absPathName));
862 rc = f_unlink(absPathName);
863 if (rc) {
864 break;
865 }
866 }
867 }
868 }
869
870 if (rc) {
871 return storageStatus = SDLOG_FATFS_ERROR;
872 }
873
874 rc = f_closedir(&dir);
875 if (rc) {
876 return storageStatus = SDLOG_FATFS_ERROR;
877 }
878
879 return storageStatus = SDLOG_OK;
880}
881
882SdioError sdLogWriteFile(const char *directoryName, const char *fileName,
883 const uint8_t *data, const size_t len)
884{
885 // DMA capable buffers: the data may be in flash, it is copied sector by sector
886 static FIL IN_SDMMC_DMA_SECTION_NOINIT(fil);
887 static uint8_t IN_SDMMC_DMA_SECTION_NOINIT(buf[512]);
888 char path[128], tmpPath[136];
889 FILINFO fno;
890 UINT written;
891
892 // a longer name (very long aircraft name) would be cut silently
893 if (strlen(directoryName) + strlen(fileName) + 2 >= sizeof(path)) {
895 }
896 chsnprintf(path, sizeof(path), "/%s/%s", directoryName, fileName);
897 if (f_stat(path, &fno) == FR_OK) {
898 return SDLOG_OK;
899 }
900
901 // the directory may not exist yet
902 chsnprintf(tmpPath, sizeof(tmpPath), "/%s", directoryName);
903 FRESULT rc = f_mkdir(tmpPath);
904 if (rc != FR_OK && rc != FR_EXIST) {
905 return SDLOG_FATFS_ERROR;
906 }
907
908 // written under a temporary name first, so a power cut never leaves a partial file
909 chsnprintf(tmpPath, sizeof(tmpPath), "%s.tmp", path);
910 if (f_open(&fil, tmpPath, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) {
911 return SDLOG_FATFS_ERROR;
912 }
913 rc = FR_OK;
914 for (size_t ofs = 0; ofs < len && rc == FR_OK; ofs += sizeof(buf)) {
915 const size_t n = MIN(len - ofs, sizeof(buf));
916 memcpy(buf, &data[ofs], n);
917 rc = f_write(&fil, buf, n, &written);
918 if (rc == FR_OK && written != n) {
919 rc = FR_DENIED; // disk full
920 }
921 }
922 if (f_close(&fil) != FR_OK || rc != FR_OK) {
923 f_unlink(tmpPath);
924 return (rc == FR_DENIED) ? SDLOG_FSFULL : SDLOG_FATFS_ERROR;
925 }
926 if (f_rename(tmpPath, path) != FR_OK) {
927 f_unlink(tmpPath);
928 return SDLOG_FATFS_ERROR;
929 }
930 return SDLOG_OK;
931}
932
933/*
934# _____ _ _
935# | __ \ (_) | |
936# | |__) | _ __ _ __ __ __ _ | |_ ___
937# | ___/ | '__| | | \ \ / / / _` | | __| / _ \
938# | | | | | | \ V / | (_| | \ |_ | __/
939# |_| |_| |_| \_/ \__,_| \__| \___|
940*/
941
942
943
944
945
946
947
948int32_t uiGetIndexOfLogFile(const char *prefix, const char *fileName)
949{
950 const size_t len = strlen(prefix);
951
952 // if filename does not began with prefix, return 0
953 if (strncmp(prefix, fileName, len) != 0) {
954 return 0;
955 }
956
957 // we point on the first char after prefix
958 const char *suffix = &(fileName[len]);
959
960 // we test that suffix is valid (at least begin with digit)
961 if (!isdigit((int) suffix[0])) {
962 // DebugTrace ("DBG> suffix = %s", suffix);
963 return 0;
964 }
965
966 return (int32_t) atoi(suffix);
967}
968
969
970#ifdef SDLOG_NEED_QUEUE
971static void cleanQueue(const bool allQueue)
972{
973 if (allQueue == false) {
974 do {
975 struct tlsf_stat_t stat;
976 tlsf_stat_r(&HEAP_DEFAULT, &stat);
977 const size_t freeRam = stat.mfree;
978 chSysLock();
979 const bool queue_full = (chMBGetFreeCountI(&messagesQueue.mb) <= 0);
980 chSysUnlock();
981 // DebugTrace ("sdLogCloseLog freeRam=%d queue_full=%d", freeRam, queue_full);
982 if ((freeRam < 200) || (queue_full == true)) {
983 removeFromQueue(1);
984 } else {
985 break;
986 }
987 } while (true);
988 } else {
989 removeFromQueue(SDLOG_QUEUE_BUCKETS);
990 }
991}
992
993static void removeFromQueue(const size_t nbMsgToRFemove)
994{
995 /* struct tlsf_stat_t stat; */
996
997 /* tlsf_stat_r (&HEAP_DEFAULT, &stat); */
998 /* size_t freeRam = stat.mfree; */
999 /* chSysLock(); */
1000 /* size_t queueBuckets = chMBGetFreeCountI(&messagesQueue.mb); */
1001 /* chSysUnlock(); */
1002
1003 /* DebugTrace ("Before removeFromQueue (%d) : ram=%d buck=%d", nbMsgToRFemove, freeRam, queueBuckets); */
1004
1005 LogMessage *lm = NULL;
1006 for (size_t i = 0; i < nbMsgToRFemove; i++) {
1007 const int32_t retLen = (int32_t)(msgqueue_pop_timeout(&messagesQueue, (void **) &lm, TIME_IMMEDIATE));
1008 if (retLen < 0) {
1009 break;
1010 }
1012 }
1013
1014 /* tlsf_stat_r (&HEAP_DEFAULT, &stat); */
1015 /* freeRam = stat.mfree; */
1016 /* chSysLock(); */
1017 /* queueBuckets = chMBGetFreeCountI(&messagesQueue.mb); */
1018 /* chSysUnlock(); */
1019
1020 /* DebugTrace ("After removeFromQueue (%d) : ram=%d buck=%d", nbMsgToRFemove, freeRam, queueBuckets); */
1021}
1022
1023
1024static void thdSdLog(void *arg)
1025{
1026 (void) arg;
1027 struct PerfBuffer {
1028 // each element of buffer should be word aligned for sdio efficient write
1029 ALIGNED_VAR(4) uint8_t buffer[SDLOG_WRITE_BUFFER_SIZE] ;
1030 uint16_t size;
1031 } ;
1032
1033 UINT bw;
1034 static IN_SDMMC_DMA_SECTION_CLEAR(struct PerfBuffer perfBuffers[SDLOG_NUM_FILES]);
1035 storageStatus = SDLOG_OK;
1036 chRegSetThreadName("thdSdLog");
1037 while (true) {
1038 LogMessage *lm = NULL;
1039 const int32_t retLen = (int32_t)(msgqueue_pop(&messagesQueue, (void **) &lm));
1040 if (retLen > 0) {
1041 FIL *fo = &fileDes[lm->op.fd].fil;
1042 uint8_t *const perfBuffer = perfBuffers[lm->op.fd].buffer;
1043
1044 switch (lm->op.fcntl) {
1045
1046 case FCNTL_FLUSH:
1047 case FCNTL_CLOSE: {
1048 const uint16_t curBufFill = perfBuffers[lm->op.fd].size;
1049 if (fileDes[lm->op.fd].inUse) {
1050 if (curBufFill) {
1051 f_write(fo, perfBuffer, curBufFill, &bw);
1052 nbBytesWritten += bw;
1053 perfBuffers[lm->op.fd].size = 0;
1054 }
1055 if (lm->op.fcntl == FCNTL_FLUSH) {
1056 f_sync(fo);
1057 } else { // close
1058 if (fileDes[lm->op.fd].tagAtClose) {
1059 f_write(fo, "\r\nEND_OF_LOG\r\n", 14, &bw);
1060 nbBytesWritten += bw;
1061 }
1062 f_close(fo);
1063 fileDes[lm->op.fd].inUse = false; // store that file is closed
1064 }
1065 }
1066 }
1067 break;
1068
1069 case FCNTL_EXIT:
1070 tlsf_free_r(&HEAP_DEFAULT, lm); // to avoid a memory leak
1071 chThdExit(storageStatus = SDLOG_NOTHREAD);
1072 break; /* To exit from thread when asked : chThdTerminate
1073 then send special message with FCNTL_EXIT */
1074
1075
1076 case FCNTL_WRITE: {
1077 const uint16_t curBufFill = perfBuffers[lm->op.fd].size;
1078 if (fileDes[lm->op.fd].inUse) {
1079 const int32_t messLen = retLen - (int32_t)(sizeof(LogMessage));
1080 if (messLen < (SDLOG_WRITE_BUFFER_SIZE - curBufFill)) {
1081 // the buffer can accept this message
1082 memcpy(&(perfBuffer[curBufFill]), lm->mess, (size_t)(messLen));
1083 perfBuffers[lm->op.fd].size = (uint16_t)((perfBuffers[lm->op.fd].size) + messLen);
1084 } else {
1085 // fill the buffer
1086 const int32_t stayLen = SDLOG_WRITE_BUFFER_SIZE - curBufFill;
1087 memcpy(&(perfBuffer[curBufFill]), lm->mess, (size_t)(stayLen));
1088 FRESULT rc = f_write(fo, perfBuffer, SDLOG_WRITE_BUFFER_SIZE, &bw);
1089 nbBytesWritten += bw;
1090 // if there an autoflush period specified, flush to the mass storage media
1091 // if timer has expired and rearm.
1092 if (fileDes[lm->op.fd].autoFlushPeriod) {
1093 const systime_t now = chVTGetSystemTimeX();
1094 if ((now - fileDes[lm->op.fd].lastFlushTs) >
1095 (fileDes[lm->op.fd].autoFlushPeriod * CH_CFG_ST_FREQUENCY)) {
1096 f_sync(fo);
1097 fileDes[lm->op.fd].lastFlushTs = now;
1098 }
1099 }
1100 if (rc) {
1101 //chThdExit(storageStatus = SDLOG_FATFS_ERROR);
1102 storageStatus = SDLOG_FATFS_ERROR;
1103 } else if (bw != SDLOG_WRITE_BUFFER_SIZE) {
1104 chThdExit(storageStatus = SDLOG_FSFULL);
1105 }
1106
1107 memcpy(perfBuffer, &(lm->mess[stayLen]), (uint32_t)(messLen - stayLen));
1108 perfBuffers[lm->op.fd].size = (uint16_t)(messLen - stayLen); // curBufFill
1109 }
1110 }
1111 }
1112 }
1114 } else {
1115 chThdExit(storageStatus = SDLOG_INTERNAL_ERROR);
1116 }
1117 }
1118}
1119
1120static size_t logMessageLen(const LogMessage *lm)
1121{
1122 return sizeof(LogMessage) + strnlen(lm->mess, SDLOG_MAX_MESSAGE_LEN);
1123}
1124
1125static size_t logRawLen(const size_t len)
1126{
1127 return sizeof(LogMessage) + len;
1128}
1129
1130static SdioError getNextFIL(FileDes *fd)
1131{
1132 // if there is a free slot in fileDes, use it
1133 // else, if all slots are buzy, maximum open files limit
1134 // is reach.
1135 for (FileDes i = 0; i < SDLOG_NUM_FILES; i++) {
1136 if (fileDes[i].inUse == false) {
1137 *fd = i;
1138 fileDes[i].inUse = true;
1139 return SDLOG_OK;
1140 }
1141 }
1142 return SDLOG_FDFULL;
1143}
1144
1146{
1147 return nbBytesWritten;
1148}
1149
1151{
1152 return storageStatus;
1153}
1154
1155
1156#endif
static uint8_t status
static int fo
Definition chdk_pipe.c:24
static THD_WORKING_AREA(wa_thd_spi1, SPI_THREAD_STACK_SIZE)
#define FF_MAX_LFN
Definition ffconf.h:121
void chvsnprintf(char *buffer, size_t size, const char *fmt, va_list ap)
Definition printf.c:372
void chsnprintf(char *buffer, size_t size, const char *fmt,...)
Definition printf.c:377
#define CH_CFG_ST_FREQUENCY
System tick frequency.
Definition chconf.h:75
#define SDLOG_MAX_MESSAGE_LEN
Definition mcuconf_h7.h:691
#define SDLOG_QUEUE_BUCKETS
Definition mcuconf_h7.h:690
#define SDLOG_NUM_FILES
Definition mcuconf_h7.h:692
#define true
Definition microrl.h:6
int32_t msgqueue_pop_timeout(MsgQueue *que, void **msgPtr, const systime_t timout)
receive message specifying timeout
Definition msg_queue.c:139
int32_t msgqueue_send(MsgQueue *que, void *msg, const uint16_t msgLen, const MsgQueueUrgency urgency)
send a buffer previously allocated by msgqueue_malloc_before_send
Definition msg_queue.c:69
int32_t msgqueue_pop(MsgQueue *que, void **msgPtr)
wait then receive message
Definition msg_queue.c:134
void msgqueue_init(MsgQueue *que, tlsf_memory_heap_t *heap, msg_t *mb_buf, const cnt_t mb_size)
initialise MsgQueue
Definition msg_queue.c:42
int32_t msgqueue_copy_send(MsgQueue *que, const void *msg, const uint16_t msgLen, const MsgQueueUrgency urgency)
send a buffer NOT previously allocated
Definition msg_queue.c:127
@ MsgQueue_REGULAR
Definition msg_queue.h:47
@ MsgQueue_OUT_OF_BAND
Definition msg_queue.h:47
mailbox_t mb
Definition msg_queue.h:226
Mini printf-like functionality.
Specific RAM section for DMA usage on F7.
#define IN_STD_SECTION_CLEAR(var)
Definition ram_arch.h:78
SdioError sdLogFinish(void)
unmount filesystem
Definition sdLog.c:307
SdioError removeEmptyLogs(const char *directoryName, const char *prefix, const size_t sizeConsideredEmpty)
remove spurious log file left on sd
Definition sdLog.c:837
static int32_t uiGetIndexOfLogFile(const char *prefix, const char *fileName)
Definition sdLog.c:948
SdioError getFileName(const char *prefix, const char *directoryName, char *nextFileName, const size_t nameLength, const int indexOffset)
get last used name for a pattern, then add offset and return valid filename
Definition sdLog.c:776
SdioError sdLogInit(uint32_t *freeSpaceInKo)
initialise sdLog
Definition sdLog.c:245
static SdioError fatfsError(FRESULT rc)
Keep the FatFS result of a failed start step.
Definition sdLog.c:236
uint8_t sdLogGetFatfsResult(void)
Definition sdLog.c:227
#define MIN(x, y)
Definition sdLog.c:39
static SdioError flushWriteByteBuffer(const FileDes fd)
#define MAX(x, y)
Definition sdLog.c:42
SdioError sdLogWriteFile(const char *directoryName, const char *fileName, const uint8_t *data, const size_t len)
write a whole file, if it does not exist yet
Definition sdLog.c:882
static IN_SDMMC_DMA_SECTION(FATFS fatfs)
static FRESULT lastFatfsResult
Definition sdLog.c:225
#define SDLOG_WRITE_BUFFER_SIZE
Definition sdLog.c:71
struct _SdLogBuffer SdLogBuffer
Definition sdLog.h:127
#define NUMBERFMF
Definition sdLog.h:36
#define NUMBERMAX
Definition sdLog.h:35
int8_t FileDes
Definition sdLog.h:128
SdioError
Definition sdLog.h:111
@ SDLOG_NOCARD
Definition sdLog.h:113
@ SDLOG_QUEUEFULL
Definition sdLog.h:118
@ SDLOG_LOGNUM_ERROR
Definition sdLog.h:123
@ SDLOG_FATFS_ERROR
Definition sdLog.h:114
@ SDLOG_FSFULL
Definition sdLog.h:116
@ SDLOG_WAS_LAUNCHED
Definition sdLog.h:124
@ SDLOG_OK
Definition sdLog.h:112
@ SDLOG_FDFULL
Definition sdLog.h:117
@ SDLOG_MEMFULL
Definition sdLog.h:119
@ SDLOG_FATFS_NOENT
Definition sdLog.h:115
@ SDLOG_NOTHREAD
Definition sdLog.h:120
@ SDLOG_INTERNAL_ERROR
Definition sdLog.h:121
@ SDLOG_CANNOT_EXPAND
Definition sdLog.h:122
arch independent SDIO API
bool sdio_connect(SDCDriver *sdc)
Connect a SD card on SDIO peripheral.
Definition sdio_arch.c:48
bool sdio_disconnect(SDCDriver *sdc)
Disconnect a SD card on SDIO peripheral.
Definition sdio_arch.c:73
SdioError sdLogFlushAllLogs(void)
SdioError sdLogWriteLog(const FileDes fd, const char *fmt,...)
SdioError sdLogWriteByte(const FileDes fd, const uint8_t value)
SdioError sdLogGetStorageStatus(void)
SdioError sdLogWriteRaw(const FileDes fd, const uint8_t *buffer, const size_t len)
size_t sdLogGetNbBytesWrittenToStorage(void)
SdioError sdLogFlushLog(const FileDes fd)
int fd
Definition serial.c:26
static const float dir[]
#define FALSE
Definition std.h:5
void tlsf_free_r(tlsf_memory_heap_t *heap, void *ptr)
void * tlsf_malloc_r(tlsf_memory_heap_t *heap, size_t bytes)
void tlsf_stat_r(tlsf_memory_heap_t *heap, struct tlsf_stat_t *stat)
void * tlsf_realloc_r(tlsf_memory_heap_t *heap, void *ptr, size_t bytes)
#define HEAP_DEFAULT
uint32_t DWORD
Definition usb_msd.c:133
static const float offset[]
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
int int32_t
Typedef defining 32 bit int type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.