From 7dcdbbadf1a397b132b2b2af8f0212a07b5f4754 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Wed, 24 Jun 2026 17:10:37 +0200 Subject: [PATCH] Implement print for UART logging --- Core/Inc/main.h | 2 +- Core/Src/cansend.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/Core/Inc/main.h b/Core/Inc/main.h index 4f23e18..950de2d 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -52,7 +52,7 @@ extern "C" { #include #define DEBUG_PRINT(...) printf(__VA_ARGS__) #else -#define DEBUG_PRINT(...) +#define DEBUG_PRINT(...) uart_printf(__VA_ARGS__) #endif /* USER CODE END EM */ diff --git a/Core/Src/cansend.c b/Core/Src/cansend.c index eb2e567..afb4f00 100644 --- a/Core/Src/cansend.c +++ b/Core/Src/cansend.c @@ -20,11 +20,46 @@ #include "main.h" #include "cmsis_os.h" #include "canlog.h" +#include +#include +#include "cmsis_os.h" +#include "stm32f4xx.h" /* USER CODE END Includes */ extern UART_HandleTypeDef huart1; extern osSemaphoreId_t xUARTDMASemaphore; extern osMessageQueueId_t xUARTQueue; +extern UART_HandleTypeDef huart1; +extern osSemaphoreId_t xUARTDMASemaphore; + +/* BEGIN uart_printf */ +/** + * @brief Implementation of a simple print to UART. + * @param arguments: strings to print + * @retval None + */ +void uart_printf(const char *fmt, ...) +{ + static char buf[128]; + va_list args; + va_start(args, fmt); + int len = vsnprintf(buf, sizeof(buf), fmt, args); + va_end(args); + + if (len > 0) + { + if (osKernelGetState() == osKernelRunning && __get_IPSR() == 0) + { + if (osSemaphoreAcquire(xUARTDMASemaphore, osWaitForever) == osOK) + { + if (HAL_UART_Transmit_DMA(&huart1, (uint8_t*)buf, len) != HAL_OK) osSemaphoreRelease(xUARTDMASemaphore); + else osSemaphoreAcquire(xUARTDMASemaphore, osWaitForever); + } + } + else HAL_UART_Transmit(&huart1, (uint8_t*)buf, len, HAL_MAX_DELAY); + } +} +/* END uart_printf */ /* BEGIN format_can_message */ /**