From 2c77e1211e7bdcf9b2e09cb4780f33b41417890a Mon Sep 17 00:00:00 2001 From: Oliver Walter Date: Sat, 10 Feb 2024 00:25:18 +0100 Subject: [PATCH] added ulog logging system --- .gitmodules | 3 +++ Core/Src/main.c | 40 ++++++++++++++++++++++++++++++++++++++-- lib/ulog | 1 + 3 files changed, 42 insertions(+), 2 deletions(-) create mode 160000 lib/ulog diff --git a/.gitmodules b/.gitmodules index 68449e0..5ca5316 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [submodule "Application/CLS"] path = Application/CLS url = git@git.lan:oliver/libCLS.git +[submodule "lib/ulog"] + path = lib/ulog + url = https://github.com/rdpoor/ulog.git diff --git a/Core/Src/main.c b/Core/Src/main.c index 31851be..b731fc8 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -29,12 +29,14 @@ #include "rng.h" #include "rtc.h" #include "sdmmc.h" +#include "tim.h" #include "usb_device.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ - +#include "ulog.h" +#include "stdio.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -65,7 +67,7 @@ static void MPU_Initialize(void); static void MPU_Config(void); void MX_FREERTOS_Init(void); /* USER CODE BEGIN PFP */ - +void ULOG_SendLPUART(ulog_level_t level, char *msg); /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ @@ -123,10 +125,17 @@ int main(void) MX_I2C1_Init(); MX_CRC_Init(); MX_RNG_Init(); + MX_TIM2_Init(); /* USER CODE BEGIN 2 */ + ULOG_INIT(); + ULOG_SUBSCRIBE(ULOG_SendLPUART,ULOG_DEBUG_LEVEL); + ULOG_DEBUG("Setup Logger"); HAL_GPIO_WritePin(Periph_Power_GPIO_Port,Periph_Power_Pin,GPIO_PIN_RESET); HAL_GPIO_WritePin(CLS_POWER_GPIO_Port, CLS_POWER_Pin, GPIO_PIN_SET); + ULOG_DEBUG("Enable Power on CLS and Periph"); gCLS_DEVICE_ADDRESS = 0b10001; + ULOG_DEBUG("Setting Global CLS address to 0b10001"); + ULOG_DEBUG("Init Kernel and start schedule"); /* USER CODE END 2 */ /* Init scheduler */ @@ -243,6 +252,33 @@ void PeriphCommonClock_Config(void) void MPU_Initialize(void) { } + +#define LOG_TIMEOUT 100 +#define ULOG_MAX_TIME_LENGTH 11 // 10 digits max uint32 +1 space +#define ULOG_MAX_LEVEL_LENGTH 16 +#define ULOG_MAX_FORMAT_LENGTH 6 +#define ULOG_SEND_BUFFER_LENGTH ULOG_MAX_MESSAGE_LENGTH + ULOG_MAX_LEVEL_LENGTH + ULOG_MAX_FORMAT_LENGTH + +#define ANSI_COLOR_BLUE "\x1b[34m" +#define ANSI_COLOR_RED "\x1b[31m" +#define ANSI_COLOR_GREEN "\x1b[32m" +#define ANSI_COLOR_RESET "\x1b[0m" +static char ulog_send_buffer[ULOG_SEND_BUFFER_LENGTH] ={0}; + +/** + * @brief Function for sending messages over LPUART + * @param level: The logging level for the message + * @param msg: The message to be sent + */ +void ULOG_SendLPUART(ulog_level_t level, char *msg) { + uint32_t current_runtime_ms = osKernelGetTickCount(); + uint32_t time_part_ms = current_runtime_ms % 1000; + uint32_t time_part_s = current_runtime_ms / 1000; + const char * leven_str = ulog_level_name(level); + uint32_t send_length = snprintf(ulog_send_buffer,ULOG_SEND_BUFFER_LENGTH,"\x1b[34m%5ld:%03ld \x1b[31m[%s]\x1b[0m -- %s\n\r", time_part_s, time_part_ms, leven_str, msg); + HAL_UART_Transmit(&hlpuart1, (const uint8_t*)ulog_send_buffer, send_length, LOG_TIMEOUT); +} + /* USER CODE END 4 */ /* MPU Configuration */ diff --git a/lib/ulog b/lib/ulog new file mode 160000 index 0000000..c6ffbf4 --- /dev/null +++ b/lib/ulog @@ -0,0 +1 @@ +Subproject commit c6ffbf4a27a1ff71110bce16b7c737abc2810bc2