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
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)
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)
152#define WRITE_BYTE_CACHE_SIZE 15
167 systime_t lastFlushTs;
172 LogMessage *writeByteCache;
178 .fil = {{0}}, .inUse =
false, .tagAtClose =
false,
179 .writeByteCache = NULL, .writeByteSeek = 0
183static volatile size_t nbBytesWritten = 0;
200#define LOG_MESSAGE_PREBUF_LEN (SDLOG_MAX_MESSAGE_LEN+sizeof(LogMessage))
206#ifdef SDLOG_NEED_QUEUE
207static size_t logMessageLen(
const LogMessage *lm);
208static size_t logRawLen(
const size_t len);
211static thread_t *sdLogThd = NULL;
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);
218static void thdSdLog(
void *arg) ;
239 if (rc == FR_NOT_READY || rc == FR_DISK_ERR) {
254 if (sdLogThd != NULL) {
259#ifdef SDLOG_NEED_QUEUE
263 if (!sdc_lld_is_card_inserted(NULL)) {
269 chThdSleepMilliseconds(10);
277 FRESULT rc = f_mount(0, &fatfs);
279 FRESULT rc = f_mount(&fatfs,
"/", 1);
286 if (freeSpaceInKo != NULL) {
287 f_getfree(
"/", &clusters, &fsp);
288 *freeSpaceInKo = clusters * (
uint32_t)fatfs.csize / 2;
291#ifdef SDLOG_NEED_QUEUE
293 fileDes[i].inUse = fileDes[i].tagAtClose =
false;
294 fileDes[i].writeByteCache = NULL;
295 fileDes[i].writeByteSeek = 0;
298 storageStatus = sdLoglaunchThread();
302 return storageStatus;
310 FRESULT rc = f_mount(0, NULL);
312 FRESULT rc = f_mount(NULL,
"", 0);
327#ifdef SDLOG_NEED_QUEUE
329 const uint32_t autoFlushPeriod,
const bool appendTagAtClose,
330 const size_t sizeInMo,
const bool preallocate,
char *fileName,
const size_t nameLength)
344 sde = getNextFIL(&ldf);
346 return storageStatus = sde;
349 sde =
getFileName(prefix, directoryName, fileName, nameLength, +1);
352 fileDes[ldf].inUse =
false;
357 rc = f_open(&fileDes[ldf].fil, fileName, FA_WRITE | FA_CREATE_ALWAYS);
359 fileDes[ldf].inUse =
false;
362 fileDes[ldf].tagAtClose = appendTagAtClose;
363 fileDes[ldf].autoFlushPeriod = autoFlushPeriod;
364 fileDes[ldf].lastFlushTs = 0;
365 sde = sdLogExpandLogFile(ldf, sizeInMo, preallocate);
369 return storageStatus = sde;
377 if (flush ==
false) {
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;
390 FRESULT trc = f_close(
fo);
391 fileDes[
fd].inUse =
false;
404 if (sdLogThd == NULL) {
411 if (fileDes[
fd].inUse) {
422 lm->op.fcntl = FCNTL_EXIT;
438 if (sdLogThd == NULL) {
445 if (fileDes[
fd].inUse) {
453 return storageStatus =
status;
457#define FD_CHECK(fd) if ((fd < 0) || (fd >= SDLOG_NUM_FILES) \
458 || (fileDes[fd].inUse == false)) \
459 return SDLOG_FATFS_ERROR
463 const bool preallocate)
468 const FRESULT rc = f_expand(&fileDes[
fd].fil, sizeInMo * 1024 * 1024, preallocate);
491 lm->op.fcntl = FCNTL_WRITE;
492 lm->op.fd =
fd & 0x1f;
498 const size_t msgLen = logMessageLen(lm);
529 lm->op.fcntl = FCNTL_FLUSH;
530 lm->op.fd =
fd & 0x1f;
549 lm->op.fcntl = FCNTL_CLOSE;
550 lm->op.fd =
fd & 0x1f;
567 if (unlikely(fileDes[
fd].writeByteCache != NULL)) {
569 sizeof(LogMessage) + fileDes[
fd].writeByteSeek,
573 fileDes[
fd].writeByteCache = NULL;
593 lm->op.fcntl = FCNTL_WRITE;
594 lm->op.fd =
fd & 0x1f;
595 memcpy(lm->mess, buffer, len);
625 return sdb->lm->mess + sdb->offset;
630 if ((sdb->offset +
offset) < sdb->len) {
640 return sdb->len - sdb->offset;
658 sdb->lm->op.fcntl = FCNTL_WRITE;
659 sdb->lm->op.fd =
fd & 0x1f;
674 return storageStatus =
status;
685 if (fileDes[
fd].writeByteCache == NULL) {
691 lm->op.fcntl = FCNTL_WRITE;
692 lm->op.fd =
fd & 0x1f;
694 fileDes[
fd].writeByteCache = lm;
695 fileDes[
fd].writeByteSeek = 0;
697 lm = fileDes[
fd].writeByteCache;
700 lm->mess[fileDes[
fd].writeByteSeek++] = value;
702 if (fileDes[
fd].writeByteSeek == WRITE_BYTE_CACHE_SIZE) {
707 fileDes[
fd].writeByteSeek = 0;
717#define WA_LOG_BASE_SIZE 1024
730 chThdSleepMilliseconds(100);
732 sdLogThd = chThdCreateStatic(waThdSdLog,
sizeof(waThdSdLog),
733 NORMALPRIO + 1, thdSdLog, NULL);
734 if (sdLogThd == NULL) {
745 storageStatus = retVal;
746 if (sdLogThd == NULL) {
764 lm.op.fcntl = FCNTL_EXIT;
771 return storageStatus = retVal;
777 char *nextFileName,
const size_t nameLength,
const int indexOffset)
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);
792 rc = f_opendir(&
dir, directoryName);
794 rc = f_mkdir(slashDirName);
798 rc = f_opendir(&
dir, directoryName);
805 rc = f_readdir(&
dir, &fno);
806 if (rc != FR_OK || fno.fname[0] == 0) {
break; }
809 if (fno.fname[0] ==
'.') {
continue; }
811 if (!(fno.fattrib & AM_DIR)) {
814 maxCurrentIndex =
MAX(maxCurrentIndex, fileIndex);
821 rc = f_closedir(&
dir);
828 directoryName, prefix, maxCurrentIndex + indexOffset);
831 chsnprintf(nextFileName, nameLength,
"%s\\%s%.ERR",
832 directoryName, prefix);
844 rc = f_opendir(&
dir, directoryName);
850 rc = f_readdir(&
dir, &fno);
851 if (rc != FR_OK || fno.fname[0] == 0) {
break; }
853 if (fno.fname[0] ==
'.') {
continue; }
855 if (!(fno.fattrib & AM_DIR)) {
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);
874 rc = f_closedir(&
dir);
883 const uint8_t *data,
const size_t len)
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];
893 if (strlen(directoryName) + strlen(fileName) + 2 >=
sizeof(path)) {
896 chsnprintf(path,
sizeof(path),
"/%s/%s", directoryName, fileName);
897 if (f_stat(path, &fno) == FR_OK) {
902 chsnprintf(tmpPath,
sizeof(tmpPath),
"/%s", directoryName);
903 FRESULT rc = f_mkdir(tmpPath);
904 if (rc != FR_OK && rc != FR_EXIST) {
909 chsnprintf(tmpPath,
sizeof(tmpPath),
"%s.tmp", path);
910 if (f_open(&fil, tmpPath, FA_WRITE | FA_CREATE_ALWAYS) != 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) {
922 if (f_close(&fil) != FR_OK || rc != FR_OK) {
926 if (f_rename(tmpPath, path) != FR_OK) {
950 const size_t len = strlen(prefix);
953 if (strncmp(prefix, fileName, len) != 0) {
958 const char *suffix = &(fileName[len]);
961 if (!isdigit((
int) suffix[0])) {
970#ifdef SDLOG_NEED_QUEUE
971static void cleanQueue(
const bool allQueue)
973 if (allQueue ==
false) {
977 const size_t freeRam = stat.mfree;
979 const bool queue_full = (chMBGetFreeCountI(&messagesQueue.
mb) <= 0);
982 if ((freeRam < 200) || (queue_full ==
true)) {
993static void removeFromQueue(
const size_t nbMsgToRFemove)
1005 LogMessage *lm = NULL;
1006 for (
size_t i = 0; i < nbMsgToRFemove; i++) {
1024static void thdSdLog(
void *arg)
1034 static IN_SDMMC_DMA_SECTION_CLEAR(struct PerfBuffer perfBuffers[
SDLOG_NUM_FILES]);
1036 chRegSetThreadName(
"thdSdLog");
1038 LogMessage *lm = NULL;
1041 FIL *
fo = &fileDes[lm->op.fd].fil;
1042 uint8_t *
const perfBuffer = perfBuffers[lm->op.fd].buffer;
1044 switch (lm->op.fcntl) {
1048 const uint16_t curBufFill = perfBuffers[lm->op.fd].size;
1049 if (fileDes[lm->op.fd].inUse) {
1051 f_write(
fo, perfBuffer, curBufFill, &bw);
1052 nbBytesWritten += bw;
1053 perfBuffers[lm->op.fd].size = 0;
1055 if (lm->op.fcntl == FCNTL_FLUSH) {
1058 if (fileDes[lm->op.fd].tagAtClose) {
1059 f_write(
fo,
"\r\nEND_OF_LOG\r\n", 14, &bw);
1060 nbBytesWritten += bw;
1063 fileDes[lm->op.fd].inUse =
false;
1077 const uint16_t curBufFill = perfBuffers[lm->op.fd].size;
1078 if (fileDes[lm->op.fd].inUse) {
1082 memcpy(&(perfBuffer[curBufFill]), lm->mess, (
size_t)(messLen));
1083 perfBuffers[lm->op.fd].size = (
uint16_t)((perfBuffers[lm->op.fd].size) + messLen);
1087 memcpy(&(perfBuffer[curBufFill]), lm->mess, (
size_t)(stayLen));
1089 nbBytesWritten += bw;
1092 if (fileDes[lm->op.fd].autoFlushPeriod) {
1093 const systime_t now = chVTGetSystemTimeX();
1094 if ((now - fileDes[lm->op.fd].lastFlushTs) >
1097 fileDes[lm->op.fd].lastFlushTs = now;
1107 memcpy(perfBuffer, &(lm->mess[stayLen]), (
uint32_t)(messLen - stayLen));
1108 perfBuffers[lm->op.fd].size = (
uint16_t)(messLen - stayLen);
1120static size_t logMessageLen(
const LogMessage *lm)
1125static size_t logRawLen(
const size_t len)
1127 return sizeof(LogMessage) + len;
1136 if (fileDes[i].inUse ==
false) {
1138 fileDes[i].inUse =
true;
1147 return nbBytesWritten;
1152 return storageStatus;
static THD_WORKING_AREA(wa_thd_spi1, SPI_THREAD_STACK_SIZE)
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.
#define SDLOG_MAX_MESSAGE_LEN
#define SDLOG_QUEUE_BUCKETS
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)
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 fatfsError(FRESULT rc)
Keep the FatFS result of a failed start step.
uint8_t sdLogGetFatfsResult(void)
static SdioError flushWriteByteBuffer(const FileDes fd)
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
static IN_SDMMC_DMA_SECTION(FATFS fatfs)
static FRESULT lastFatfsResult
#define SDLOG_WRITE_BUFFER_SIZE
struct _SdLogBuffer SdLogBuffer
arch independent SDIO API
bool sdio_connect(SDCDriver *sdc)
Connect a SD card on SDIO peripheral.
bool sdio_disconnect(SDCDriver *sdc)
Disconnect a SD card on SDIO peripheral.
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)
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)
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.