11 Commits

Author SHA1 Message Date
eeeck 4da8f59330 Fix spacing 2026-06-24 18:54:31 +02:00
eeeck c9f735acd9 Initialize USART1 as first peripheral 2026-06-24 18:54:21 +02:00
eeeck 30410f9b9e Rever CAN message format 2026-06-24 18:53:37 +02:00
eeeck e14a98eba4 Add error callback to recover from UART deadlock, use async UART
transmit
2026-06-24 17:40:26 +02:00
eeeck b55bd9468f Use better logging codes 2026-06-24 17:15:34 +02:00
eeeck 7dcdbbadf1 Implement print for UART logging 2026-06-24 17:10:37 +02:00
eeeck 334295e1cd Disable UARTrx 2026-06-24 17:03:22 +02:00
eeeck 89cd1e00d0 Remove whitespace 2026-06-24 08:11:11 +02:00
eeeck 591a96cf6d Increase CAN task size 2026-06-24 08:10:59 +02:00
eeeck 2be0f0370e Use larger baudrate 2026-06-24 08:10:49 +02:00
eeeck 58c4b66f34 Try different prescalers 2026-06-24 08:10:41 +02:00
4 changed files with 90 additions and 53 deletions
+2 -2
View File
@@ -46,13 +46,13 @@ extern "C" {
/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */
//#define DEBUG_ITM
//#define DEBUG_DUMMY_FRAME
#define DEBUG_DUMMY_FRAME
#ifdef DEBUG_ITM
#include <stdio.h>
#define DEBUG_PRINT(...) printf(__VA_ARGS__)
#else
#define DEBUG_PRINT(...)
#define DEBUG_PRINT(...) uart_printf(__VA_ARGS__)
#endif
/* USER CODE END EM */
+1 -6
View File
@@ -93,10 +93,7 @@ void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
led_task = xCAN2LedTask;
}
if (osMessageQueuePut(queue, &message, 0U, 0U) == osOK)
{
osThreadFlagsSet(led_task, 0x01);
}
if (osMessageQueuePut(queue, &message, 0U, 0U) == osOK) osThreadFlagsSet(led_task, 0x01);
}
/* END HAL_CAN_RxFifo0MsgPendingCallback */
@@ -127,8 +124,6 @@ void vCANListener(void *argument)
osMessageQueuePut(xUARTQueue, &message, 0U, 0U);
}
else DEBUG_PRINT("No CAN yet!\r\n");
}
}
/* END vCANListener */
+64 -23
View File
@@ -20,11 +20,49 @@
#include "main.h"
#include "cmsis_os.h"
#include "canlog.h"
#include <stdio.h>
#include <stdarg.h>
#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)
{
HAL_UART_Transmit(&huart1, (uint8_t*)buf, len, HAL_MAX_DELAY);
osSemaphoreRelease(xUARTDMASemaphore);
}
}
else
{
HAL_UART_Transmit(&huart1, (uint8_t*)buf, len, HAL_MAX_DELAY);
}
}
}
/* END uart_printf */
/* BEGIN format_can_message */
/**
@@ -93,7 +131,19 @@ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
}
/* END HAL_UART_TxCpltCallback */
/* BEGIN vUARTLoggerListen */
/* BEGIN HAL_UART_ErrorCallback */
/**
* @brief Safely release UART semaphore inside ISR and yield task if possible.
* @param argument: UART handle
* @retval None
*/
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART1) osSemaphoreRelease(xUARTDMASemaphore);
}
/* END HAL_UART_ErrorCallback */
/* BEGIN vUARTLogger */
/**
* @brief Send CAN bus traffic saved in FIFO buffer to USART1
* @param argument: Not used
@@ -116,18 +166,15 @@ void vUARTLogger(void *argument)
for (;;)
{
DEBUG_PRINT("Waiting for CAN traffic...\r\n");
uint32_t queue_timeout = 2500U;
#ifdef DEBUG_DUMMY_FRAME
uint32_t queue_timeout = 1000U;
#else
uint32_t queue_timeout = osWaitForever;
#endif
#ifdef DEBUG_DUMMY_FRAME
osMessageQueuePut(xUARTQueue, &dummy_frame, 0U, 0U);
osDelay(1000);
#endif
//#ifdef DEBUG_DUMMY_FRAME
//uint32_t queue_timeout = 100U;
//osMessageQueuePut(xUARTQueue, &dummy_frame, 0U, 0U);
//osDelay(500);
//#else
//uint32_t queue_timeout = osWaitForever;
// #endif
if (osMessageQueueGet(xUARTQueue, &message, NULL, queue_timeout) == osOK)
{
@@ -135,21 +182,15 @@ void vUARTLogger(void *argument)
{
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");
DEBUG_PRINT("[E] HAL error, CAN frame NOT sent!\r\n");
}
}
else
{
DEBUG_PRINT("ERROR: Semaphore timeout! UART might be stuck in BUSY state.\r\n");
}
else DEBUG_PRINT("[E] Semaphore timeout! UART might be stuck in BUSY state.\r\n");
}
else DEBUG_PRINT("[D] Waiting for CAN traffic...\r\n");
}
}
/* END vUARTLoggerListen */
/* END vUARTLogger */
+23 -22
View File
@@ -76,6 +76,8 @@ static void MX_CAN2_Init(void);
static void MX_SDIO_SD_Init(void);
static void MX_USART1_UART_Init(void);
void uart_printf(const char *fmt, ...);
/* USER CODE BEGIN PFP */
int _write(int file, char *ptr, int len)
{
@@ -95,7 +97,6 @@ int _write(int file, char *ptr, int len)
*/
int main(void)
{
/* USER CODE BEGIN 1 */
if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk)
{
@@ -105,7 +106,7 @@ int main(void)
ITM->TCR = ITM_TCR_ITMENA_Msk | ITM_TCR_SYNCENA_Msk | ITM_TCR_SWOENA_Msk;
}
DEBUG_PRINT("Booting system\r\n");
DEBUG_PRINT("[L] Booting system\r\n");
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
@@ -114,7 +115,7 @@ int main(void)
HAL_Init();
/* USER CODE BEGIN Init */
DEBUG_PRINT("Configuring system clock\r\n");
DEBUG_PRINT("[L] Configuring system clock\r\n");
/* USER CODE END Init */
@@ -122,35 +123,35 @@ int main(void)
SystemClock_Config();
/* USER CODE BEGIN SysInit */
DEBUG_PRINT("Initializing configured peripherals...\r\n");
DEBUG_PRINT("[L] Initializing configured peripherals...\r\n");
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_USART1_UART_Init();
MX_GPIO_Init();
MX_DMA_Init();
MX_CAN1_Init();
MX_CAN2_Init();
MX_SDIO_SD_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
DEBUG_PRINT("Initializing CAN bus logger\r\n");
DEBUG_PRINT("[L] Initializing CAN bus logger\r\n");
CAN_Logger_Init(&hcan1, &hcan2);
/* USER CODE END 2 */
/* Init scheduler */
osKernelInitialize();
DEBUG_PRINT("Creating RTOS entities\r\n");
DEBUG_PRINT("[L] Creating RTOS entities\r\n");
/* USER CODE BEGIN RTOS_TASKS */
const osThreadAttr_t CAN1rxAttributes = {
.name = "CAN1rx",
.stack_size = 128 * 4,
.stack_size = 512 * 4,
.priority = (osPriority_t) osPriorityRealtime1,
};
const osThreadAttr_t CAN2rxAttributes = {
.name = "CAN2rx",
.stack_size = 128 * 4,
.stack_size = 512 * 4,
.priority = (osPriority_t) osPriorityRealtime,
};
const osThreadAttr_t UartLoggerAttributes = {
@@ -201,7 +202,7 @@ int main(void)
/* USER CODE END RTOS_EVENTS */
/* Start scheduler */
DEBUG_PRINT("Starting RTOS init scheduler\r\n");
DEBUG_PRINT("[L] Starting RTOS init scheduler\r\n");
osKernelStart();
/* We should never get here as control is now taken by the scheduler */
@@ -213,7 +214,7 @@ int main(void)
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
DEBUG_PRINT("ERROR: RTOS scheduler crashed!\r\n");
DEBUG_PRINT("[E] RTOS scheduler crashed!\r\n");
/* USER CODE END 3 */
}
}
@@ -280,7 +281,7 @@ static void MX_CAN1_Init(void)
/* USER CODE END CAN1_Init 1 */
hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 8;
hcan1.Init.Prescaler = 4;
hcan1.Init.Mode = CAN_MODE_SILENT;
hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan1.Init.TimeSeg1 = CAN_BS1_16TQ;
@@ -296,7 +297,7 @@ static void MX_CAN1_Init(void)
Error_Handler();
}
/* USER CODE BEGIN CAN1_Init 2 */
DEBUG_PRINT("CAN1 initialized!\r\n");
DEBUG_PRINT("[L] CAN1 initialized\r\n");
/* USER CODE END CAN1_Init 2 */
}
@@ -317,7 +318,7 @@ static void MX_CAN2_Init(void)
/* USER CODE END CAN2_Init 1 */
hcan2.Instance = CAN2;
hcan2.Init.Prescaler = 16;
hcan2.Init.Prescaler = 8;
hcan2.Init.Mode = CAN_MODE_SILENT;
hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan2.Init.TimeSeg1 = CAN_BS1_16TQ;
@@ -333,7 +334,7 @@ static void MX_CAN2_Init(void)
Error_Handler();
}
/* USER CODE BEGIN CAN2_Init 2 */
DEBUG_PRINT("CAN2 initialized!\r\n");
DEBUG_PRINT("[L] CAN2 initialized\r\n");
/* USER CODE END CAN2_Init 2 */
}
@@ -369,7 +370,7 @@ static void MX_SDIO_SD_Init(void)
// Error_Handler();
//}
/* USER CODE BEGIN SDIO_Init 2 */
//DEBUG_PRINT("SDIO initialized!\r\n");
//DEBUG_PRINT("[L] SDIO initialized\r\n");
/* USER CODE END SDIO_Init 2 */
}
@@ -390,11 +391,11 @@ static void MX_USART1_UART_Init(void)
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.BaudRate = 1152000;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.Mode = UART_MODE_TX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
@@ -402,7 +403,7 @@ static void MX_USART1_UART_Init(void)
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
DEBUG_PRINT("USART1 initialized!\r\n");
DEBUG_PRINT("[L] USART1 initialized\r\n");
/* USER CODE END USART1_Init 2 */
}
@@ -428,7 +429,7 @@ static void MX_DMA_Init(void)
HAL_NVIC_EnableIRQ(DMA2_Stream7_IRQn);
/* USER CODE BEGIN MX_DMA_Init 1 */
DEBUG_PRINT("DMA initialized!\r\n");
DEBUG_PRINT("[L] DMA initialized\r\n");
/* USER CODE END MX_DMA_Init 1 */
}
@@ -498,7 +499,7 @@ static void MX_GPIO_Init(void)
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
DEBUG_PRINT("GPIO initialized!\r\n");
DEBUG_PRINT("[L] GPIO initialized\r\n");
/* USER CODE END MX_GPIO_Init_2 */
}
@@ -533,7 +534,7 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
*/
void Error_Handler(void)
{
DEBUG_PRINT("ERROR: entering error handler\r\n");
DEBUG_PRINT("[E] Entering error handler\r\n");
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */