From 771cdd3997de06d63e7b83e66315691e552c809e Mon Sep 17 00:00:00 2001 From: Pawel Jalocha Date: Thu, 7 May 2020 22:40:19 +0100 Subject: [PATCH] Log more data to SD --- main/sdlog.cpp | 11 ++++++----- main/sens.cpp | 35 +++++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/main/sdlog.cpp b/main/sdlog.cpp index e4f71ad..503b0cb 100644 --- a/main/sdlog.cpp +++ b/main/sdlog.cpp @@ -18,7 +18,8 @@ static uint16_t LogDate = 0; // [~days] dat 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 +const size_t FIFOsize = 16384; +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) @@ -59,13 +60,12 @@ static void Log_Check(void) // time check: utime(LogFileName, &LogTime); } } - -static int WriteLog(size_t MaxBlock=8192) // process the queue of lines to be written to the log +static int WriteLog(size_t MaxBlock=FIFOsize/2) // 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; + if(Len>MaxBlock) Len/=2; int Write=fwrite(Block, 1, Len, LogFile); Log_FIFO.flushReadBlock(Len); if(Write!=Len) { fclose(LogFile); LogFile=0; return -1; } @@ -83,10 +83,11 @@ extern "C" { Log_Open(); if(!LogFile) { SD_Unmount(); vTaskDelay(1000); continue; } } + if(Log_FIFO.Full()0); if(Write<0) { SD_Unmount(); vTaskDelay(1000); continue; } - if(Write==0) vTaskDelay(100); + // if(Write==0) vTaskDelay(100); Log_Check(); } } diff --git a/main/sens.cpp b/main/sens.cpp index b9294d9..97d28af 100644 --- a/main/sens.cpp +++ b/main/sens.cpp @@ -101,8 +101,7 @@ static Delay PressDelay; // 4-second delay for long-term clim static char Line[96]; // line to prepare the barometer NMEA sentence static uint8_t InitBaro(void) -{ // xSemaphoreTake(I2C_Mutex, portMAX_DELAY); - Baro.Bus=BARO_I2C; +{ Baro.Bus=BARO_I2C; uint8_t Err=Baro.CheckID(); if(Err==0) Err=Baro.ReadCalib(); #ifdef WITH_BMP180 @@ -113,7 +112,6 @@ static uint8_t InitBaro(void) if(Err==0) Err=Baro.Acquire(); if(Err==0) { Baro.Calculate(); } #endif - // xSemaphoreGive(I2C_Mutex); // if(Err) LED_BAT_On(); return Err==0 ? Baro.ADDR:0; } @@ -129,22 +127,16 @@ static void ProcBaro(void) #ifdef WITH_BMP180 TickType_t Start=xTaskGetTickCount(); - // xSemaphoreTake(I2C_Mutex, portMAX_DELAY); uint8_t Err=Baro.AcquireRawTemperature(); // measure temperature - // xSemaphoreGive(I2C_Mutex); if(Err==0) { Baro.CalcTemperature(); AverPress=0; AverCount=0; } // clear the average else { PipeCount=0; - // xSemaphoreTake(I2C_Mutex, portMAX_DELAY); I2C_Restart(Baro.Bus); - // xSemaphoreGive(I2C_Mutex); vTaskDelay(20); InitBaro(); // try to recover I2C bus and baro return; } for(uint8_t Idx=0; Idx<16; Idx++) - { // xSemaphoreTake(I2C_Mutex, portMAX_DELAY); - uint8_t Err=Baro.AcquireRawPressure(); // take pressure measurement - // xSemaphoreGive(I2C_Mutex); + { uint8_t Err=Baro.AcquireRawPressure(); // take pressure measurement if(Err==0) { Baro.CalcPressure(); AverPress+=Baro.Pressure; AverCount++; } // sum-up average pressure TickType_t Time=xTaskGetTickCount()-Start; if(Time>=200) break; } // but no longer than 250ms to fit into 0.5 second slot @@ -161,12 +153,15 @@ static void ProcBaro(void) #endif #endif #if defined(WITH_BMP280) || defined(WITH_MS5607) || defined(WITH_BME280) || defined(WITH_MS5611) - // xSemaphoreTake(I2C_Mutex, portMAX_DELAY); uint8_t Err=Baro.Acquire(); - // xSemaphoreGive(I2C_Mutex); if(Err==0) { Baro.Calculate(); } else { PipeCount=0; return; } - AverPress = Baro.Pressure; // [0.25Pa] + AverPress = Baro.Pressure; + Err=Baro.Acquire(); + if(Err==0) { Baro.Calculate(); } + else { PipeCount=0; return; } + AverPress += Baro.Pressure; + AverPress/=2; // [0.25Pa] #endif BaroPipe.Input(AverPress); // [0.25Pa] if(PipeCount<255) PipeCount++; // count data going to the slope fitting pipe @@ -255,7 +250,7 @@ static void ProcBaro(void) #ifdef WITH_SDLOG if(Log_Free()>=128) { xSemaphoreTake(Log_Mutex, portMAX_DELAY); - Format_String(Log_Write, Line, Len, 0); // send NMEA sentence to the log file + Format_String(Log_Write, Line, 0, Len); // send NMEA sentence to the log file xSemaphoreGive(Log_Mutex); } #endif @@ -270,6 +265,12 @@ static void ProcBaro(void) { xSemaphoreTake(CONS_Mutex, portMAX_DELAY); Format_String(CONS_UART_Write, Line, 0, Len); // send NMEA sentence to the console (UART1) xSemaphoreGive(CONS_Mutex); } +#ifdef WITH_SDLOG + if(Log_Free()>=128) + { xSemaphoreTake(Log_Mutex, portMAX_DELAY); + Format_String(Log_Write, Line, 0, Len); // send NMEA sentence to the log file + xSemaphoreGive(Log_Mutex); } +#endif Len=0; Len+=Format_String(Line+Len, "$LK8EX1,"); @@ -288,6 +289,12 @@ static void ProcBaro(void) { xSemaphoreTake(CONS_Mutex, portMAX_DELAY); Format_String(CONS_UART_Write, Line, 0, Len); // send NMEA sentence to the console (UART1) xSemaphoreGive(CONS_Mutex); } +#ifdef WITH_SDLOG + if(Log_Free()>=128) + { xSemaphoreTake(Log_Mutex, portMAX_DELAY); + Format_String(Log_Write, Line, 0, Len); // send NMEA sentence to the log file + xSemaphoreGive(Log_Mutex); } +#endif }