39 #define MIN(x , y) (((x) < (y)) ? (x) : (y))
42 #define MAX(x , y) (((x) > (y)) ? (x) : (y))
44 #define IS_POWER_OF_TWO(s) ((s) && !((s) & ((s) - 1)))
46 #ifndef SDLOG_NUM_FILES
47 #error SDLOG_NUM_FILES should be defined in mcuconf.h
52 #define FFCONF_DEF _FATFS
56 #error upgrade FATFS to 0.14 at least
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
63 #ifndef SDLOG_ALL_BUFFERS_SIZE
64 #error SDLOG_ALL_BUFFERS_SIZE should be defined in mcuconf.h
67 #if SDLOG_ALL_BUFFERS_SIZE > 65536
68 #error constraint 512 <= SDLOG_ALL_BUFFERS_SIZE <= 65536 not meet
71 #define SDLOG_WRITE_BUFFER_SIZE (SDLOG_ALL_BUFFERS_SIZE/SDLOG_NUM_FILES)
73 #ifndef SDLOG_MAX_MESSAGE_LEN
74 #error SDLOG_MAX_MESSAGE_LENshould be defined in mcuconf.h
77 #ifndef SDLOG_QUEUE_BUCKETS
78 #error SDLOG_QUEUE_BUCKETS should be defined in mcuconf.h
81 #if FF_FS_REENTRANT == 0
82 #warning "FF_FS_REENTRANT = 0 in ffconf.h DO NOT open close file during log"
85 #if SDLOG_WRITE_BUFFER_SIZE < 512
86 #error SDLOG_ALL_BUFFERS_SIZE / SDLOG_NUM_FILES cannot be < 512
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
93 #ifdef SDLOG_NEED_QUEUE
118 #define WRITE_BYTE_CACHE_SIZE 15
130 struct FilePoolUnit {
133 systime_t lastFlushTs;
138 LogMessage *writeByteCache;
144 .fil = {{0}}, .inUse =
false, .tagAtClose =
false,
145 .writeByteCache = NULL, .writeByteSeek = 0
149 static volatile size_t nbBytesWritten = 0;
159 struct _SdLogBuffer {
166 #define LOG_MESSAGE_PREBUF_LEN (SDLOG_MAX_MESSAGE_LEN+sizeof(LogMessage))
172 #ifdef SDLOG_NEED_QUEUE
173 static size_t logMessageLen(
const LogMessage *lm);
174 static size_t logRawLen(
const size_t len);
175 static SdioError sdLoglaunchThread(
void);
177 static thread_t *sdLogThd = NULL;
179 static void removeFromQueue(
const size_t nbMsgToRFemov);
180 static void cleanQueue(
const bool allQueue);
181 static SdioError sdLogExpandLogFile(
const FileDes fileObject,
const size_t sizeInMo,
182 const bool preallocate);
184 static void thdSdLog(
void *arg) ;
198 if (sdLogThd != NULL) {
203 #ifdef SDLOG_NEED_QUEUE
207 if (!sdc_lld_is_card_inserted(NULL)) {
213 chThdSleepMilliseconds(10);
220 #if FFCONF_DEF < 8000
221 FRESULT rc = f_mount(0, &fatfs);
223 FRESULT rc = f_mount(&fatfs,
"/", 1);
230 if (freeSpaceInKo != NULL) {
231 f_getfree(
"/", &clusters, &fsp);
232 *freeSpaceInKo = clusters * (
uint32_t)fatfs.csize / 2;
235 #ifdef SDLOG_NEED_QUEUE
237 fileDes[i].inUse = fileDes[i].tagAtClose =
false;
238 fileDes[i].writeByteCache = NULL;
239 fileDes[i].writeByteSeek = 0;
242 storageStatus = sdLoglaunchThread();
246 return storageStatus;
253 #if FFCONF_DEF < 8000
254 FRESULT rc = f_mount(0, NULL);
256 FRESULT rc = f_mount(NULL,
"", 0);
271 #ifdef SDLOG_NEED_QUEUE
273 const uint32_t autoFlushPeriod,
const bool appendTagAtClose,
274 const size_t sizeInMo,
const bool preallocate,
char *fileName,
const size_t nameLength)
288 sde = getNextFIL(&ldf);
290 return storageStatus = sde;
293 sde =
getFileName(prefix, directoryName, fileName, nameLength, +1);
300 rc = f_open(&fileDes[ldf].fil, fileName, FA_WRITE | FA_CREATE_ALWAYS);
302 fileDes[ldf].inUse =
false;
305 fileDes[ldf].tagAtClose = appendTagAtClose;
306 fileDes[ldf].autoFlushPeriod = autoFlushPeriod;
307 fileDes[ldf].lastFlushTs = 0;
308 sde = sdLogExpandLogFile(ldf, sizeInMo, preallocate);
312 return storageStatus = sde;
320 if (flush ==
false) {
327 if (fileDes[
fd].inUse) {
328 FIL *
fo = &fileDes[
fd].fil;
329 if (fileDes[
fd].tagAtClose) {
330 f_write(
fo,
"\r\nEND_OF_LOG\r\n", 14, &bw);
331 nbBytesWritten += bw;
333 FRESULT trc = f_close(
fo);
334 fileDes[
fd].inUse =
false;
347 if (sdLogThd == NULL) {
354 if (fileDes[
fd].inUse) {
365 lm->op.fcntl = FCNTL_EXIT;
381 if (sdLogThd == NULL) {
388 if (fileDes[
fd].inUse) {
396 return storageStatus =
status;
400 #define FD_CHECK(fd) if ((fd < 0) || (fd >= SDLOG_NUM_FILES) \
401 || (fileDes[fd].inUse == false)) \
402 return SDLOG_FATFS_ERROR
406 const bool preallocate)
411 const FRESULT rc = f_expand(&fileDes[
fd].fil, sizeInMo * 1024 * 1024, preallocate);
434 lm->op.fcntl = FCNTL_WRITE;
435 lm->op.fd =
fd & 0x1f;
441 const size_t msgLen = logMessageLen(lm);
472 lm->op.fcntl = FCNTL_FLUSH;
473 lm->op.fd =
fd & 0x1f;
492 lm->op.fcntl = FCNTL_CLOSE;
493 lm->op.fd =
fd & 0x1f;
510 if (unlikely(fileDes[
fd].writeByteCache != NULL)) {
512 sizeof(LogMessage) + fileDes[
fd].writeByteSeek,
516 fileDes[
fd].writeByteCache = NULL;
536 lm->op.fcntl = FCNTL_WRITE;
537 lm->op.fd =
fd & 0x1f;
538 memcpy(lm->mess, buffer, len);
568 return sdb->lm->mess + sdb->offset;
573 if ((sdb->offset +
offset) < sdb->len) {
583 return sdb->len - sdb->offset;
601 sdb->lm->op.fcntl = FCNTL_WRITE;
602 sdb->lm->op.fd =
fd & 0x1f;
617 return storageStatus =
status;
628 if (fileDes[
fd].writeByteCache == NULL) {
634 lm->op.fcntl = FCNTL_WRITE;
635 lm->op.fd =
fd & 0x1f;
637 fileDes[
fd].writeByteCache = lm;
638 fileDes[
fd].writeByteSeek = 0;
640 lm = fileDes[
fd].writeByteCache;
643 lm->mess[fileDes[
fd].writeByteSeek++] = value;
645 if (fileDes[
fd].writeByteSeek == WRITE_BYTE_CACHE_SIZE) {
650 fileDes[
fd].writeByteSeek = 0;
660 #define WA_LOG_BASE_SIZE 1024
673 chThdSleepMilliseconds(100);
675 sdLogThd = chThdCreateStatic(waThdSdLog,
sizeof(waThdSdLog),
676 NORMALPRIO + 1, thdSdLog, NULL);
677 if (sdLogThd == NULL) {
688 storageStatus = retVal;
689 if (sdLogThd == NULL) {
707 lm.op.fcntl = FCNTL_EXIT;
714 return storageStatus = retVal;
720 char *nextFileName,
const size_t nameLength,
const int indexOffset)
729 const size_t directoryNameLen =
MIN(strlen(directoryName), 128);
730 const size_t slashDirNameLen = directoryNameLen + 2;
731 char slashDirName[slashDirNameLen];
732 strlcpy(slashDirName,
"/", slashDirNameLen);
733 strlcat(slashDirName, directoryName, slashDirNameLen);
735 rc = f_opendir(&
dir, directoryName);
737 rc = f_mkdir(slashDirName);
741 rc = f_opendir(&
dir, directoryName);
748 rc = f_readdir(&
dir, &fno);
749 if (rc != FR_OK || fno.fname[0] == 0) {
break; }
752 if (fno.fname[0] ==
'.') {
continue; }
754 if (!(fno.fattrib & AM_DIR)) {
757 maxCurrentIndex =
MAX(maxCurrentIndex, fileIndex);
764 rc = f_closedir(&
dir);
771 directoryName, prefix, maxCurrentIndex + indexOffset);
774 chsnprintf(nextFileName, nameLength,
"%s\\%s%.ERR",
775 directoryName, prefix);
787 rc = f_opendir(&
dir, directoryName);
793 rc = f_readdir(&
dir, &fno);
794 if (rc != FR_OK || fno.fname[0] == 0) {
break; }
796 if (fno.fname[0] ==
'.') {
continue; }
798 if (!(fno.fattrib & AM_DIR)) {
800 if ((strncmp(fno.fname, prefix, strlen(prefix)) == 0) && (fno.fsize <= sizeConsideredEmpty)) {
801 char absPathName[128];
802 strlcpy(absPathName, directoryName,
sizeof(absPathName));
803 strlcat(absPathName,
"/",
sizeof(absPathName));
804 strlcat(absPathName, fno.fname,
sizeof(absPathName));
805 rc = f_unlink(absPathName);
817 rc = f_closedir(&
dir);
842 const size_t len = strlen(prefix);
845 if (strncmp(prefix, fileName, len) != 0) {
850 const char *suffix = &(fileName[len]);
853 if (!isdigit((
int) suffix[0])) {
862 #ifdef SDLOG_NEED_QUEUE
863 static void cleanQueue(
const bool allQueue)
865 if (allQueue ==
false) {
869 const size_t freeRam = stat.mfree;
871 const bool queue_full = (chMBGetFreeCountI(&messagesQueue.
mb) <= 0);
874 if ((freeRam < 200) || (queue_full ==
true)) {
885 static void removeFromQueue(
const size_t nbMsgToRFemove)
897 LogMessage *lm = NULL;
898 for (
size_t i = 0; i < nbMsgToRFemove; i++) {
916 static void thdSdLog(
void *arg)
928 chRegSetThreadName("thdSdLog");
930 LogMessage *lm = NULL;
933 FIL *
fo = &fileDes[lm->op.fd].fil;
934 uint8_t *
const perfBuffer = perfBuffers[lm->op.fd].buffer;
936 switch (lm->op.fcntl) {
940 const uint16_t curBufFill = perfBuffers[lm->op.fd].size;
941 if (fileDes[lm->op.fd].inUse) {
943 f_write(
fo, perfBuffer, curBufFill, &bw);
944 nbBytesWritten += bw;
945 perfBuffers[lm->op.fd].size = 0;
947 if (lm->op.fcntl == FCNTL_FLUSH) {
950 if (fileDes[lm->op.fd].tagAtClose) {
951 f_write(
fo,
"\r\nEND_OF_LOG\r\n", 14, &bw);
952 nbBytesWritten += bw;
955 fileDes[lm->op.fd].inUse =
false;
969 const uint16_t curBufFill = perfBuffers[lm->op.fd].size;
970 if (fileDes[lm->op.fd].inUse) {
974 memcpy(&(perfBuffer[curBufFill]), lm->mess, (
size_t)(messLen));
975 perfBuffers[lm->op.fd].size = (
uint16_t)((perfBuffers[lm->op.fd].size) + messLen);
979 memcpy(&(perfBuffer[curBufFill]), lm->mess, (
size_t)(stayLen));
981 nbBytesWritten += bw;
984 if (fileDes[lm->op.fd].autoFlushPeriod) {
985 const systime_t now = chVTGetSystemTimeX();
986 if ((now - fileDes[lm->op.fd].lastFlushTs) >
989 fileDes[lm->op.fd].lastFlushTs = now;
999 memcpy(perfBuffer, &(lm->mess[stayLen]), (
uint32_t)(messLen - stayLen));
1000 perfBuffers[lm->op.fd].size = (
uint16_t)(messLen - stayLen);
1012 static size_t logMessageLen(
const LogMessage *lm)
1017 static size_t logRawLen(
const size_t len)
1019 return sizeof(LogMessage) + len;
1028 if (fileDes[i].inUse ==
false) {
1030 fileDes[i].inUse =
true;
1037 size_t sdLogGetNbBytesWrittenToStorage(
void)
1039 return nbBytesWritten;
1044 return storageStatus;
#define SDLOG_MAX_MESSAGE_LEN
#define SDLOG_QUEUE_BUCKETS
static const float offset[]
void chvsnprintf(char *buffer, size_t size, const char *fmt, va_list ap)
void chsnprintf(char *buffer, size_t size, const char *fmt,...)
#define CH_CFG_ST_FREQUENCY
System tick frequency.
THD_WORKING_AREA(wa_thd_ap, THD_WORKING_AREA_MAIN)
int32_t msgqueue_pop_timeout(MsgQueue *que, void **msgPtr, const systime_t timout)
receive message specifying timeout
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
int32_t msgqueue_pop(MsgQueue *que, void **msgPtr)
wait then receive message
void msgqueue_init(MsgQueue *que, tlsf_memory_heap_t *heap, msg_t *mb_buf, const cnt_t mb_size)
initialise MsgQueue
int32_t msgqueue_copy_send(MsgQueue *que, const void *msg, const uint16_t msgLen, const MsgQueueUrgency urgency)
send a buffer NOT previously allocated
Mini printf-like functionality.
Specific RAM section for DMA usage on F7.
#define IN_STD_SECTION_CLEAR(var)
#define IN_DMA_SECTION_CLEAR(var)
#define IN_DMA_SECTION_NOINIT(var)
SdioError sdLogFinish(void)
unmount filesystem
SdioError removeEmptyLogs(const char *directoryName, const char *prefix, const size_t sizeConsideredEmpty)
remove spurious log file left on sd
static int32_t uiGetIndexOfLogFile(const char *prefix, const char *fileName)
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
SdioError sdLogInit(uint32_t *freeSpaceInKo)
initialise sdLog
static SdioError flushWriteByteBuffer(const FileDes fd)
static IN_DMA_SECTION(FATFS fatfs)
#define SDLOG_WRITE_BUFFER_SIZE
struct _SdLogBuffer SdLogBuffer
arch independent SDIO API
bool sdio_disconnect(void)
Disconnect a SD card on SDIO peripheral.
bool sdio_connect(void)
Connect a SD card on SDIO peripheral.
void tlsf_free_r(tlsf_memory_heap_t *heap, void *ptr)
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)
void * tlsf_malloc_r(tlsf_memory_heap_t *heap, size_t bytes)
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.