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
+6 -3
View File
@@ -45,15 +45,18 @@ extern "C" {
/* Exported macro ------------------------------------------------------------*/ /* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */ /* USER CODE BEGIN EM */
//#define DEBUG_ITM //#define DEBUG_ITW
//#define DEBUG_DUMMY_FRAME //#define DEBUG_DUMMY_FRAME
#ifdef DEBUG_ITM #ifdef DEBUG_ITW
#include <stdio.h> #include <stdio.h>
#define DEBUG_PRINT(...) printf(__VA_ARGS__) #define DEBUG_PRINT(...) printf(__VA_ARGS__)
#else #else
#define DEBUG_PRINT(...) #include <stdarg.h>
void uart_debug_print(const char *fmt, ...);
#define DEBUG_PRINT(...) uart_debug_print(__VA_ARGS__)
#endif #endif
/* USER CODE END EM */ /* USER CODE END EM */
/* Exported functions prototypes ---------------------------------------------*/ /* Exported functions prototypes ---------------------------------------------*/
+7 -6
View File
@@ -28,6 +28,9 @@ extern CAN_HandleTypeDef hcan2;
extern osThreadId_t xCAN1LedTask; extern osThreadId_t xCAN1LedTask;
extern osThreadId_t xCAN2LedTask; extern osThreadId_t xCAN2LedTask;
volatile uint32_t can1_rx_isr_count = 0;
volatile uint32_t can2_rx_isr_count = 0;
/* Private function prototypes -----------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */ /* USER CODE BEGIN FunctionPrototypes */
/** /**
@@ -51,7 +54,6 @@ void CAN_Logger_Init(CAN_HandleTypeDef *hcan1, CAN_HandleTypeDef *hcan2)
filter.FilterBank = 0; filter.FilterBank = 0;
if (HAL_CAN_ConfigFilter(hcan1, &filter) != HAL_OK) Error_Handler(); if (HAL_CAN_ConfigFilter(hcan1, &filter) != HAL_OK) Error_Handler();
filter.FilterBank = 14; filter.FilterBank = 14;
if (HAL_CAN_ConfigFilter(hcan2, &filter) != HAL_OK) Error_Handler(); if (HAL_CAN_ConfigFilter(hcan2, &filter) != HAL_OK) Error_Handler();
} }
@@ -65,6 +67,9 @@ void CAN_Logger_Init(CAN_HandleTypeDef *hcan1, CAN_HandleTypeDef *hcan2)
*/ */
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{ {
if (hcan->Instance == CAN1) can1_rx_isr_count++;
else can2_rx_isr_count++;
CanMessage_t message; CanMessage_t message;
CAN_RxHeaderTypeDef rxHeader; CAN_RxHeaderTypeDef rxHeader;
uint8_t data[8]; uint8_t data[8];
@@ -93,10 +98,7 @@ void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
led_task = xCAN2LedTask; led_task = xCAN2LedTask;
} }
if (osMessageQueuePut(queue, &message, 0U, 0U) == osOK) if (osMessageQueuePut(queue, &message, 0U, 0U) == osOK) osThreadFlagsSet(led_task, 0x01);
{
osThreadFlagsSet(led_task, 0x01);
}
} }
/* END HAL_CAN_RxFifo0MsgPendingCallback */ /* END HAL_CAN_RxFifo0MsgPendingCallback */
@@ -128,7 +130,6 @@ void vCANListener(void *argument)
osMessageQueuePut(xUARTQueue, &message, 0U, 0U); osMessageQueuePut(xUARTQueue, &message, 0U, 0U);
} }
else DEBUG_PRINT("No CAN yet!\r\n"); else DEBUG_PRINT("No CAN yet!\r\n");
} }
} }
/* END vCANListener */ /* END vCANListener */
+46 -17
View File
@@ -20,11 +20,17 @@
#include "main.h" #include "main.h"
#include "cmsis_os.h" #include "cmsis_os.h"
#include "canlog.h" #include "canlog.h"
#include <stdio.h>
#include <stdarg.h>
/* USER CODE END Includes */ /* USER CODE END Includes */
extern UART_HandleTypeDef huart1; extern UART_HandleTypeDef huart1;
extern osSemaphoreId_t xUARTDMASemaphore; extern osSemaphoreId_t xUARTDMASemaphore;
extern osMessageQueueId_t xUARTQueue; 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 */ /* 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) void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{ {
if (huart->Instance == USART1) if (huart->Instance == USART1) osSemaphoreRelease(xUARTDMASemaphore);
{
osSemaphoreRelease(xUARTDMASemaphore);
}
} }
/* END HAL_UART_TxCpltCallback */ /* END HAL_UART_TxCpltCallback */
@@ -102,7 +105,9 @@ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
void vUARTLogger(void *argument) void vUARTLogger(void *argument)
{ {
CanMessage_t message; CanMessage_t message;
char tx_buffer[45];
static char tx_buffer[45];
static char diag_buf[128];
#ifdef DEBUG_DUMMY_FRAME #ifdef DEBUG_DUMMY_FRAME
CanMessage_t dummy_frame = { CanMessage_t dummy_frame = {
@@ -116,13 +121,9 @@ void vUARTLogger(void *argument)
for (;;) for (;;)
{ {
DEBUG_PRINT("Waiting for CAN traffic...\r\n");
#ifdef DEBUG_DUMMY_FRAME
uint32_t queue_timeout = 1000U; uint32_t queue_timeout = 1000U;
#else
uint32_t queue_timeout = osWaitForever; //ALT: uint32_t queue_timeout = osWaitForever;
#endif
#ifdef DEBUG_DUMMY_FRAME #ifdef DEBUG_DUMMY_FRAME
osMessageQueuePut(xUARTQueue, &dummy_frame, 0U, 0U); osMessageQueuePut(xUARTQueue, &dummy_frame, 0U, 0U);
@@ -131,15 +132,16 @@ void vUARTLogger(void *argument)
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) if (osSemaphoreAcquire(xUARTDMASemaphore, queue_timeout) == osOK)
{ {
int len = format_can_message(tx_buffer, message.source, &message); int len = format_can_message(tx_buffer, message.source, &message);
if (HAL_UART_Transmit_DMA(&huart1, (uint8_t*)tx_buffer, len) != HAL_OK)
if (HAL_UART_Transmit_DMA(&huart1, (uint8_t*)tx_buffer, len) == HAL_OK)
{
DEBUG_PRINT("CAN frame sent via UART\r\n");
}
else
{ {
osSemaphoreRelease(xUARTDMASemaphore); osSemaphoreRelease(xUARTDMASemaphore);
DEBUG_PRINT("HAL error, CAN frame NOT sent!\r\n"); DEBUG_PRINT("HAL error, CAN frame NOT sent!\r\n");
@@ -148,6 +150,33 @@ void vUARTLogger(void *argument)
else else
{ {
DEBUG_PRINT("ERROR: Semaphore timeout! UART might be stuck in BUSY state.\r\n"); 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");
} }
} }
} }
+23 -3
View File
@@ -21,6 +21,8 @@
/* USER CODE BEGIN Includes */ /* USER CODE BEGIN Includes */
#include "canlog.h" #include "canlog.h"
#include "cansend.h" #include "cansend.h"
#include <stdio.h>
#include <stdarg.h>
/* USER CODE END Includes */ /* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/ /* Private typedef -----------------------------------------------------------*/
@@ -64,7 +66,6 @@ osMessageQueueId_t xUARTQueue;
osSemaphoreId_t xUARTDMASemaphore; osSemaphoreId_t xUARTDMASemaphore;
osThreadId_t xUartTask; osThreadId_t xUartTask;
/* USER CODE END PV */ /* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/
@@ -75,14 +76,33 @@ static void MX_CAN1_Init(void);
static void MX_CAN2_Init(void); static void MX_CAN2_Init(void);
static void MX_SDIO_SD_Init(void); static void MX_SDIO_SD_Init(void);
static void MX_USART1_UART_Init(void); static void MX_USART1_UART_Init(void);
/* USER CODE END PFP */
/* USER CODE BEGIN PFP */ /* USER CODE BEGIN PF */
int _write(int file, char *ptr, int len) int _write(int file, char *ptr, int len)
{ {
for (int i = 0; i < len; i++) ITM_SendChar((*ptr++)); for (int i = 0; i < len; i++) ITM_SendChar((*ptr++));
return len; return len;
} }
/* USER CODE END PFP */
#ifndef DEBUG_ITM
void uart_debug_print(const char *fmt, ...)
{
char buf[128];
va_list args;
va_start(args, fmt);
int len = vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
if (len > 0)
{
while (HAL_UART_GetState(&huart1) & HAL_UART_STATE_BUSY_TX);
HAL_UART_Transmit(&huart1, (uint8_t*)buf, len, 100);
}
}
#endif
/* USER CODE END PF */
/* Private user code ---------------------------------------------------------*/ /* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */ /* USER CODE BEGIN 0 */