Implement UART debugging by reusing DEBUG_PRINT when ITW not active

This commit is contained in:
2026-06-24 12:58:29 +02:00
parent 89cd1e00d0
commit 0ba8ecda64
4 changed files with 122 additions and 69 deletions
+49 -20
View File
@@ -20,11 +20,17 @@
#include "main.h"
#include "cmsis_os.h"
#include "canlog.h"
#include <stdio.h>
#include <stdarg.h>
/* USER CODE END Includes */
extern UART_HandleTypeDef huart1;
extern osSemaphoreId_t xUARTDMASemaphore;
extern osMessageQueueId_t xUARTQueue;
extern volatile uint32_t can1_rx_isr_count;
extern volatile uint32_t can2_rx_isr_count;
extern CAN_HandleTypeDef hcan1;
extern CAN_HandleTypeDef hcan2;
/* BEGIN format_can_message */
/**
@@ -86,10 +92,7 @@ static int format_can_message(char *buf, uint8_t source, const CanMessage_t *mes
*/
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART1)
{
osSemaphoreRelease(xUARTDMASemaphore);
}
if (huart->Instance == USART1) osSemaphoreRelease(xUARTDMASemaphore);
}
/* END HAL_UART_TxCpltCallback */
@@ -102,7 +105,9 @@ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
void vUARTLogger(void *argument)
{
CanMessage_t message;
char tx_buffer[45];
static char tx_buffer[45];
static char diag_buf[128];
#ifdef DEBUG_DUMMY_FRAME
CanMessage_t dummy_frame = {
@@ -116,30 +121,27 @@ void vUARTLogger(void *argument)
for (;;)
{
DEBUG_PRINT("Waiting for CAN traffic...\r\n");
#ifdef DEBUG_DUMMY_FRAME
uint32_t queue_timeout = 1000U;
#else
uint32_t queue_timeout = osWaitForever;
#endif
//ALT: uint32_t queue_timeout = osWaitForever;
#ifdef DEBUG_DUMMY_FRAME
osMessageQueuePut(xUARTQueue, &dummy_frame, 0U, 0U);
osDelay(1000);
#endif
if (osMessageQueueGet(xUARTQueue, &message, NULL, queue_timeout) == osOK)
{
if (osMessageQueueGet(xUARTQueue, &message, NULL, queue_timeout) == osOK)
{
if (huart1.gState != HAL_UART_STATE_READY)
{
HAL_UART_Abort(&huart1);
HAL_UART_Init(&huart1);
}
if (osSemaphoreAcquire(xUARTDMASemaphore, queue_timeout) == osOK)
{
int len = format_can_message(tx_buffer, message.source, &message);
if (HAL_UART_Transmit_DMA(&huart1, (uint8_t*)tx_buffer, len) == HAL_OK)
{
DEBUG_PRINT("CAN frame sent via UART\r\n");
}
else
if (HAL_UART_Transmit_DMA(&huart1, (uint8_t*)tx_buffer, len) != HAL_OK)
{
osSemaphoreRelease(xUARTDMASemaphore);
DEBUG_PRINT("HAL error, CAN frame NOT sent!\r\n");
@@ -148,8 +150,35 @@ void vUARTLogger(void *argument)
else
{
DEBUG_PRINT("ERROR: Semaphore timeout! UART might be stuck in BUSY state.\r\n");
HAL_UART_Abort(&huart1);
HAL_UART_Init(&huart1);
osSemaphoreRelease(xUARTDMASemaphore);
}
}
}
else
{
if (osSemaphoreAcquire(xUARTDMASemaphore, queue_timeout) == osOK)
{
int len = snprintf(diag_buf, sizeof(diag_buf),
"[D] ISR1:%lu ISR2:%lu | ST1:%lu ST2:%lu | ER1:0x%lX ER2:0x%lX\r\n",
(unsigned long)can1_rx_isr_count, (unsigned long)can2_rx_isr_count,
(unsigned long)HAL_CAN_GetState(&hcan1), (unsigned long)HAL_CAN_GetState(&hcan2),
(unsigned long)HAL_CAN_GetError(&hcan1), (unsigned long)HAL_CAN_GetError(&hcan2));
if (len > 0)
{
if (HAL_UART_Transmit_DMA(&huart1, (uint8_t*)diag_buf, len) != HAL_OK) osSemaphoreRelease(xUARTDMASemaphore);
}
else osSemaphoreRelease(xUARTDMASemaphore);
}
else
{
HAL_UART_Abort(&huart1);
HAL_UART_Init(&huart1);
osSemaphoreRelease(xUARTDMASemaphore);
DEBUG_PRINT("ERROR: Semaphore timeout! UART might be stuck in BUSY state.\r\n");
}
}
}
}
/* END vUARTLoggerListen */