From bd8779b5aad44e0e93755a804e4d5f59c42a8c2d Mon Sep 17 00:00:00 2001 From: Oliver Walter Date: Tue, 21 May 2024 22:18:05 +0200 Subject: [PATCH] added loggint to SD card --- Application/BSP/BSP_SDLogger.c | 90 ++++++++++++++++++++++++++++++++++ Application/BSP/BSP_SDLogger.h | 10 +++- Application/BSP/CMakeLists.txt | 1 + Core/Src/freertos.c | 11 +++-- 4 files changed, 107 insertions(+), 5 deletions(-) create mode 100644 Application/BSP/BSP_SDLogger.c diff --git a/Application/BSP/BSP_SDLogger.c b/Application/BSP/BSP_SDLogger.c new file mode 100644 index 0000000..3efa7e6 --- /dev/null +++ b/Application/BSP/BSP_SDLogger.c @@ -0,0 +1,90 @@ +#include "BSP_SDLogger.h" +#include "fatfs.h" +#include "ulog.h" +#include "stdio.h" +#include "string.h" + +uint8_t block_buffer[512] = {0}; +size_t block_buffer_index = 0; +char file_name[20]; + +char ulog_buffer[ULOG_MAX_MESSAGE_LENGTH] = {0}; + +void BSP_SDLogger_ULOG(ulog_level_t level, char *msg) { + uint32_t send_length = snprintf(ulog_buffer, ULOG_MAX_MESSAGE_LENGTH, "[%s] %s\n", ulog_level_name(level), msg); + BSP_SDLogger_Write(ulog_buffer, send_length); +} + +// init the logger buffer and the file +// filename is generated based on the log number +void BSP_SDLogger_Init(int log_number) { + + // create the file name + sprintf(file_name, "log_%d.txt", log_number); + + // open the file + FIL file; + FRESULT res = f_open(&file, file_name, FA_CREATE_ALWAYS | FA_WRITE); + if (res != FR_OK) { + ULOG_ERROR("Failed to open file %s", file_name); + } + + ULOG_SUBSCRIBE(BSP_SDLogger_ULOG, ULOG_DEBUG_LEVEL); + + // close the file + f_close(&file); + +} + + +void BSP_SDLogger_Flush() { + + // open the file + FIL file; + FRESULT res = f_open(&file, file_name, FA_WRITE); + if (res != FR_OK) { + ULOG_ERROR("Failed to open file %s", file_name); + } + + // write the buffer to the file + UINT bytes_written; + res = f_write(&file, block_buffer, block_buffer_index, &bytes_written); + if (res != FR_OK) { + ULOG_ERROR("Failed to write to file %s", file_name); + } + + // close the file + f_close(&file); + + // reset the buffer index + block_buffer_index = 0; + + +} + +// write data to the logger +// data is only written to the buffer! +// once the buffer is full, the data is written to the file +void BSP_SDLogger_Write(char *data, size_t length) { + + + size_t buffer_size = sizeof(block_buffer); + size_t remaining_size = buffer_size - block_buffer_index; + + // if the data is larger than the remaining space in the buffer + if (length > remaining_size) { + // write the remaining space to the buffer + memcpy(&block_buffer[block_buffer_index], data, remaining_size); + // write the buffer to the file + BSP_SDLogger_Flush(); + // write the remaining data to the buffer + memcpy(&block_buffer[block_buffer_index], &data[remaining_size], length - remaining_size); + block_buffer_index = length - remaining_size; + } else { + // write the data to the buffer + memcpy(&block_buffer[block_buffer_index], data, length); + block_buffer_index += length; + } + + +} diff --git a/Application/BSP/BSP_SDLogger.h b/Application/BSP/BSP_SDLogger.h index 5d11b5d..a4b6647 100644 --- a/Application/BSP/BSP_SDLogger.h +++ b/Application/BSP/BSP_SDLogger.h @@ -1,4 +1,12 @@ // SD logger steam is logged to sd card // buffernd logger collect logs until some amount of data is collected. // needs to be flushed before shutdown -// used with ULOG \ No newline at end of file +// used with ULOG + +#include +#include + +void BSP_SDLogger_Init(int log_number); + + +void BSP_SDLogger_Write(char *data, size_t length); \ No newline at end of file diff --git a/Application/BSP/CMakeLists.txt b/Application/BSP/CMakeLists.txt index 983e9f0..1dca8a4 100644 --- a/Application/BSP/CMakeLists.txt +++ b/Application/BSP/CMakeLists.txt @@ -5,6 +5,7 @@ BSP_INA.c BSP_POWER.c BSP_GPIO.c BSP_ADC.c +BSP_SDLogger.c ) target_include_directories(BSP PUBLIC ./) diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c index 798a8a8..b5d5286 100644 --- a/Core/Src/freertos.c +++ b/Core/Src/freertos.c @@ -39,6 +39,7 @@ #include "LightState.h" #include "BSP_GPIO.h" #include "BSP_ADC.h" +#include "BSP_SDLogger.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -186,6 +187,8 @@ void StartDefaultTask(void *argument) ULOG_INFO(output); } + + char INA_LOG[72]; /* Infinite loop */ @@ -247,13 +250,13 @@ void WaitForStartConfirm_Task(void *argument) { } if(tick > maxDelayTime) { - BSP_POWER_EnterStandby(); - ULOG_INFO("System in standby mode"); - osThreadExit(); while (1) { - osDelay(1000); + ULOG_INFO("System in standby mode"); + BSP_POWER_EnterStandby(); + NVIC_SystemReset(); } + osThreadExit(); } } }