added loggint to SD card

This commit is contained in:
2024-05-21 22:18:05 +02:00
parent 04c5eb047e
commit bd8779b5aa
4 changed files with 107 additions and 5 deletions

View File

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

View File

@@ -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
// used with ULOG
#include <stdint.h>
#include <stddef.h>
void BSP_SDLogger_Init(int log_number);
void BSP_SDLogger_Write(char *data, size_t length);

View File

@@ -5,6 +5,7 @@ BSP_INA.c
BSP_POWER.c
BSP_GPIO.c
BSP_ADC.c
BSP_SDLogger.c
)
target_include_directories(BSP PUBLIC ./)

View File

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