added loggint to SD card
This commit is contained in:
90
Application/BSP/BSP_SDLogger.c
Normal file
90
Application/BSP/BSP_SDLogger.c
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,3 +2,11 @@
|
|||||||
// buffernd logger collect logs until some amount of data is collected.
|
// buffernd logger collect logs until some amount of data is collected.
|
||||||
// needs to be flushed before shutdown
|
// 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);
|
||||||
@@ -5,6 +5,7 @@ BSP_INA.c
|
|||||||
BSP_POWER.c
|
BSP_POWER.c
|
||||||
BSP_GPIO.c
|
BSP_GPIO.c
|
||||||
BSP_ADC.c
|
BSP_ADC.c
|
||||||
|
BSP_SDLogger.c
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(BSP PUBLIC ./)
|
target_include_directories(BSP PUBLIC ./)
|
||||||
|
|||||||
@@ -39,6 +39,7 @@
|
|||||||
#include "LightState.h"
|
#include "LightState.h"
|
||||||
#include "BSP_GPIO.h"
|
#include "BSP_GPIO.h"
|
||||||
#include "BSP_ADC.h"
|
#include "BSP_ADC.h"
|
||||||
|
#include "BSP_SDLogger.h"
|
||||||
/* USER CODE END Includes */
|
/* USER CODE END Includes */
|
||||||
|
|
||||||
/* Private typedef -----------------------------------------------------------*/
|
/* Private typedef -----------------------------------------------------------*/
|
||||||
@@ -186,6 +187,8 @@ void StartDefaultTask(void *argument)
|
|||||||
ULOG_INFO(output);
|
ULOG_INFO(output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
char INA_LOG[72];
|
char INA_LOG[72];
|
||||||
|
|
||||||
/* Infinite loop */
|
/* Infinite loop */
|
||||||
@@ -247,13 +250,13 @@ void WaitForStartConfirm_Task(void *argument) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(tick > maxDelayTime) {
|
if(tick > maxDelayTime) {
|
||||||
BSP_POWER_EnterStandby();
|
|
||||||
ULOG_INFO("System in standby mode");
|
|
||||||
osThreadExit();
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
osDelay(1000);
|
ULOG_INFO("System in standby mode");
|
||||||
|
BSP_POWER_EnterStandby();
|
||||||
|
NVIC_SystemReset();
|
||||||
}
|
}
|
||||||
|
osThreadExit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user