Revive logging to the SD card

pull/20/head
Pawel Jalocha 2020-05-06 17:31:43 +01:00
rodzic 947e96a890
commit a8bdf26574
6 zmienionych plików z 138 dodań i 12 usunięć

Wyświetl plik

@ -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"

Wyświetl plik

@ -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);

Wyświetl plik

@ -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<OGN_Packet> *RxPacket, uint8_t RxPacket
{ RxPacket->calcRelayRank(GPS_Altitude/10); // calculate the relay-rank (priority for relay)
OGN_RxPacket<OGN_Packet> *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<OGN_Packet> *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

92
main/sdlog.cpp 100644
Wyświetl plik

@ -0,0 +1,92 @@
#include <stdint.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <utime.h>
#include <unistd.h>
#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<char, 16384> 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<LogReopen) return; } // if fresh (less than 30 seconds) then nothing to do
else
{ if(TimeSinceOpen<(LogReopen/4)) return; }
fclose(LogFile); LogFile=0; // close and reopen the log file when older than 10 seconds
uint32_t Time = TimeSync_Time();
struct stat LogStat;
struct utimbuf LogTime;
if(stat(LogFileName, &LogStat)>=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(); }
}

17
main/sdlog.h 100644
Wyświetl plik

@ -0,0 +1,17 @@
#ifndef __SDLOG_H__
#define __SDLOG_H__
#include <stdint.h>
#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__

Wyświetl plik

@ -9,6 +9,9 @@
#include "proc.h"
#include "ctrl.h"
#include "gps.h"
#ifdef WITH_SDLOG
#include "sdlog.h"
#endif
// #define DEBUG_PRINT