From a8bdf26574318dea5cab5df3d751a6cc7fdb5fb3 Mon Sep 17 00:00:00 2001 From: Pawel Jalocha Date: Wed, 6 May 2020 17:31:43 +0100 Subject: [PATCH] Revive logging to the SD card --- main/gps.cpp | 4 ++- main/main.cpp | 8 +++++ main/proc.cpp | 26 ++++++++------ main/sdlog.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ main/sdlog.h | 17 ++++++++++ main/sens.cpp | 3 ++ 6 files changed, 138 insertions(+), 12 deletions(-) create mode 100644 main/sdlog.cpp create mode 100644 main/sdlog.h diff --git a/main/gps.cpp b/main/gps.cpp index 1fe1ca6..1232bc1 100644 --- a/main/gps.cpp +++ b/main/gps.cpp @@ -5,13 +5,15 @@ #include "hal.h" #include "gps.h" #include "ctrl.h" - #include "nmea.h" #include "ubx.h" #ifdef WITH_MAVLINK #include "mavlink.h" #include "atmosphere.h" #endif +#ifdef WITH_SDLOG +#include "sdlog.h" +#endif #include "ogn.h" diff --git a/main/main.cpp b/main/main.cpp index 7f438bc..e7918b9 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -16,6 +16,10 @@ #include "sound.h" // sounds, warnings, alarms #include "disp.h" +#ifdef WITH_SDLOG +#include "sdlog.h" +#endif + #ifdef WITH_AERO #include "aero.h" #endif @@ -70,6 +74,10 @@ void app_main(void) xTaskCreate(vTaskRF, "RF", 2048, 0, tskIDLE_PRIORITY+4, 0); #ifdef WITH_LOG xTaskCreate(vTaskLOG , "LOG", 4096, 0, tskIDLE_PRIORITY+1, 0); +#endif +#ifdef WITH_SDLOG + Log_Mutex = xSemaphoreCreateMutex(); + xTaskCreate(vTaskSDLOG, "SDLOG", 4096, 0, tskIDLE_PRIORITY+1, 0); #endif xTaskCreate(vTaskPROC, "PROC", 2048, 0, tskIDLE_PRIORITY+3, 0); xTaskCreate(vTaskGPS, "GPS", 2048, 0, tskIDLE_PRIORITY+4, 0); diff --git a/main/proc.cpp b/main/proc.cpp index b7109bf..c02eadb 100644 --- a/main/proc.cpp +++ b/main/proc.cpp @@ -16,6 +16,9 @@ #ifdef WITH_FLASHLOG // log own track to unused Flash pages (STM32 only) #include "flashlog.h" #endif +#ifdef WITH_SDLOG +#include "sdlog.h" +#endif #ifdef WITH_SOUND #include "sound.h" @@ -289,7 +292,7 @@ static void ReadStatus(OGN_Packet &Packet) #ifdef WITH_SDLOG if(Log_Free()>=128) { xSemaphoreTake(Log_Mutex, portMAX_DELAY); - Format_String(Log_Write, Line, Len); // send the NMEA out to the log file + Format_String(Log_Write, Line, 0, Len); // send the NMEA out to the log file xSemaphoreGive(Log_Mutex); } #endif } @@ -334,11 +337,18 @@ static void ProcessRxPacket(OGN_RxPacket *RxPacket, uint8_t RxPacket { RxPacket->calcRelayRank(GPS_Altitude/10); // calculate the relay-rank (priority for relay) OGN_RxPacket *PrevRxPacket = RelayQueue.addNew(RxPacketIdx); #ifdef WITH_POGNT - if(Parameters.Verbose) { uint8_t Len=RxPacket->WritePOGNT(Line); // print on the console as $POGNT - xSemaphoreTake(CONS_Mutex, portMAX_DELAY); - Format_String(CONS_UART_Write, Line, 0, Len); - xSemaphoreGive(CONS_Mutex); } + if(Parameters.Verbose) + { xSemaphoreTake(CONS_Mutex, portMAX_DELAY); + Format_String(CONS_UART_Write, Line, 0, Len); + xSemaphoreGive(CONS_Mutex); } +#ifdef WITH_SDLOG + if(Log_Free()>=128) + { xSemaphoreTake(Log_Mutex, portMAX_DELAY); + Format_String(Log_Write, Line, 0, Len); + xSemaphoreGive(Log_Mutex); } +#endif + } #endif // Len=RxPacket->Packet.WriteAPRS(Line, RxTime); // print on the console as APRS message // xSemaphoreTake(CONS_Mutex, portMAX_DELAY); @@ -367,12 +377,6 @@ static void ProcessRxPacket(OGN_RxPacket *RxPacket, uint8_t RxPacket if(!Signif) Signif=OGN_isSignif(&(RxPacket->Packet), &(PrevRxPacket->Packet)); if(Signif) SPIFFSlog(RxPacket, RxTime); // log only significant packets #endif -#ifdef WITH_SDLOG - if(Log_Free()>=128) - { xSemaphoreTake(Log_Mutex, portMAX_DELAY); - Format_String(Log_Write, Line, Len); - xSemaphoreGive(Log_Mutex); } -#endif #ifdef WITH_PFLAA if( Parameters.Verbose // print PFLAA on the console for received packets #ifdef WITH_LOOKOUT diff --git a/main/sdlog.cpp b/main/sdlog.cpp new file mode 100644 index 0000000..e4f71ad --- /dev/null +++ b/main/sdlog.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include +#include + +#include "hal.h" +#include "gps.h" +#include "sdlog.h" +#include "timesync.h" +#include "fifo.h" + +static char LogFileName[32]; +static FILE *LogFile = 0; + +static uint16_t LogDate = 0; // [~days] date = FatTime>>16 +static TickType_t LogOpenTime; // [msec] when was the log file (re)open +static const TickType_t LogReopen = 30000; // [msec] when to close and re-open the log file + +static FIFO Log_FIFO; // 16K buffer for SD-log + SemaphoreHandle_t Log_Mutex; // Mutex for the FIFO to prevent mixing between threads + +void Log_Write(char Byte) // write a byte into the log file buffer (FIFO) +{ if(Log_FIFO.Write(Byte)>0) return; // if byte written into FIFO return + while(Log_FIFO.Write(Byte)<=0) vTaskDelay(1); } // wait while the FIFO is full - we have to use vTaskDelay not TaskYIELD + +int Log_Free(void) { return Log_FIFO.Free(); } // how much space left in the buffer + +static int Log_Open(void) +{ LogDate=GPS_FatTime>>16; // get the FAT-time date part + int32_t Day = LogDate &0x1F; // get day, month, year + int32_t Month = (LogDate>>5)&0x0F; + int32_t Year = (LogDate>>9)-20; + uint32_t Date = 0; + if(Year>=0) Date = Day*10000 + Month*100 + Year; // create DDMMYY number for easy printout + strcpy(LogFileName, "/sdcard/TR000000.LOG"); + Format_UnsDec(LogFileName+10, Date, 6); // format the date into the log file name + LogFile = fopen(LogFileName, "at"); + if(LogFile==0) return -1; + LogOpenTime=xTaskGetTickCount(); + return 0; } + + // TaskYIELD would not give time to lower priority task like log-writer +static void Log_Check(void) // time check: +{ if(!LogFile) return; // if last operation in error then don't do anything + TickType_t TimeSinceOpen = xTaskGetTickCount()-LogOpenTime; // when did we (re)open the log file last time + if(LogDate) + { if(TimeSinceOpen=0) // get file attributes (maybe not really needed) + { LogTime.actime = Time; // set access and modification times of the dest. file + LogTime.modtime = Time; + utime(LogFileName, &LogTime); } +} + + +static int WriteLog(size_t MaxBlock=8192) // process the queue of lines to be written to the log +{ if(!LogFile) return 0; + int Count=0; + for( ; ; ) + { char *Block; size_t Len=Log_FIFO.getReadBlock(Block); if(Len==0) break; + if(Len>MaxBlock) Len=MaxBlock; + int Write=fwrite(Block, 1, Len, LogFile); + Log_FIFO.flushReadBlock(Len); + if(Write!=Len) { fclose(LogFile); LogFile=0; return -1; } + Count+=Write; } + return Count; } + +extern "C" + void vTaskSDLOG(void* pvParameters) +{ Log_FIFO.Clear(); + + for( ; ; ) + { if(!SD_isMounted()) + { vTaskDelay(5000); SD_Mount(); continue; } + if(!LogFile) + { Log_Open(); + if(!LogFile) { SD_Unmount(); vTaskDelay(1000); continue; } + } + int Write; + do { Write=WriteLog(); } while(Write>0); + if(Write<0) { SD_Unmount(); vTaskDelay(1000); continue; } + if(Write==0) vTaskDelay(100); + Log_Check(); } +} + diff --git a/main/sdlog.h b/main/sdlog.h new file mode 100644 index 0000000..4b5a73e --- /dev/null +++ b/main/sdlog.h @@ -0,0 +1,17 @@ +#ifndef __SDLOG_H__ +#define __SDLOG_H__ + +#include + +#include "hal.h" + +void Log_Write(char Byte); +int Log_Free(void); +extern SemaphoreHandle_t Log_Mutex; + +#ifdef __cplusplus + extern "C" +#endif + void vTaskSDLOG(void* pvParameters); + +#endif // __SDLOG_H__ diff --git a/main/sens.cpp b/main/sens.cpp index be5b75f..b9294d9 100644 --- a/main/sens.cpp +++ b/main/sens.cpp @@ -9,6 +9,9 @@ #include "proc.h" #include "ctrl.h" #include "gps.h" +#ifdef WITH_SDLOG +#include "sdlog.h" +#endif // #define DEBUG_PRINT