23 Commits

Author SHA1 Message Date
eeeck e32f501285 Add IRQ handler for CAN bus interrupt 2026-07-04 19:25:31 +02:00
eeeck 85c8cd4eed Use timer for queue read 2026-07-04 18:25:36 +02:00
eeeck 9a58e41d55 Change to static 2026-07-04 18:25:07 +02:00
eeeck 48aaaf7fb8 Gracefully handle errors instead of sending to error handler 2026-07-04 18:24:57 +02:00
eeeck 4236c204a6 Use stricter semaphore timing 2026-07-04 18:24:18 +02:00
eeeck 2d7e7f514a Fix function descriptor 2026-07-04 18:23:58 +02:00
eeeck 600a4e24d8 Lower NVIC priority 2026-07-04 18:23:45 +02:00
eeeck 6954fc5497 Add function prototype 2026-07-04 18:23:31 +02:00
eeeck 03fec5ff34 Initialize DMA and GPIO first 2026-07-04 18:23:07 +02:00
eeeck 76f6526036 Initialize CAN peripherals as GPIO pins 2026-07-04 18:22:36 +02:00
eeeck ff1335b0dc Use software counter in error handler after HAL is deactivated 2026-07-04 18:22:22 +02:00
eeeck 397711e9fb Update function 2026-06-25 20:37:05 +02:00
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
6 changed files with 140 additions and 69 deletions
+1
View File
@@ -22,6 +22,7 @@
#include "main.h"
void uart_printf(const char *fmt, ...);
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart);
void vUARTLogger(void *argument);
+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 */
+4 -8
View File
@@ -18,6 +18,7 @@
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "cansend.h"
#include "cmsis_os.h"
#include <string.h>
/* USER CODE END Includes */
@@ -93,10 +94,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 */
@@ -116,7 +114,7 @@ void vCANListener(void *argument)
HAL_CAN_ActivateNotification(hcan, CAN_IT_RX_FIFO0_MSG_PENDING);
uint32_t irqn = (hcan->Instance == CAN1) ? CAN1_RX0_IRQn : CAN2_RX0_IRQn;
HAL_NVIC_SetPriority(irqn, 6, 0);
HAL_NVIC_SetPriority(irqn, 5, 0);
HAL_NVIC_EnableIRQ(irqn);
for (;;)
@@ -127,15 +125,13 @@ void vCANListener(void *argument)
osMessageQueuePut(xUARTQueue, &message, 0U, 0U);
}
else DEBUG_PRINT("No CAN yet!\r\n");
}
}
/* END vCANListener */
/* BEGIN vLEDHeartbeat */
/**
* @brief Blink a LED in heartbeat mode when CAN traffic is detected
* @brief Turn on LED when CAN traffic is detected
* @param argument: LED GPIO (port and pin)
* @retval None
*/
+73 -33
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, 100U) == 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 */
/**
@@ -86,14 +124,35 @@ 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 */
/* BEGIN vUARTLoggerListen */
/* BEGIN HAL_UART_ErrorCallback */
/**``MEN
* @brief Handle UART errors
* @param argument: UART handle
* @retval None
*/
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART1)
{
__HAL_UART_CLEAR_OREFLAG(huart);
__HAL_UART_CLEAR_FEFLAG(huart);
__HAL_UART_CLEAR_NEFLAG(huart);
__HAL_UART_CLEAR_PEFLAG(huart);
HAL_UART_DMAStop(huart);
huart->gState = HAL_UART_STATE_READY;
osSemaphoreRelease(xUARTDMASemaphore);
}
}
/* END HAL_UART_ErrorCallback */
/* BEGIN vUARTLogger */
/**
* @brief Send CAN bus traffic saved in FIFO buffer to USART1
* @param argument: Not used
@@ -102,7 +161,7 @@ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
void vUARTLogger(void *argument)
{
CanMessage_t message;
char tx_buffer[45];
static char tx_buffer[45];
#ifdef DEBUG_DUMMY_FRAME
CanMessage_t dummy_frame = {
@@ -116,40 +175,21 @@ 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
#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, 1000U) == osOK)
{
if (osSemaphoreAcquire(xUARTDMASemaphore, queue_timeout) == osOK)
if (osSemaphoreAcquire(xUARTDMASemaphore, 100U) == 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");
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 */
+41 -24
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_GPIO_Init();
MX_DMA_Init();
MX_GPIO_Init();
MX_USART1_UART_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 */
}
@@ -497,8 +498,24 @@ static void MX_GPIO_Init(void)
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
//*Configure CAN1 pins : PB8, PB9 */
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
//*Configure CAN2 pins : PB12, PB13 */
GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_CAN2;
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 +550,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 */
@@ -555,7 +572,7 @@ void Error_Handler(void)
while (1)
{
HAL_GPIO_TogglePin(led_error.port, led_error.pin);
HAL_Delay(250);
for(volatile uint32_t i = 0; i < 4000000; i++);
}
/* USER CODE END Error_Handler_Debug */
}
+18 -1
View File
@@ -41,7 +41,8 @@
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
extern CAN_HandleTypeDef hcan1;
extern CAN_HandleTypeDef hcan2;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
@@ -250,4 +251,20 @@ void DMA2_Stream7_IRQHandler(void)
/* USER CODE BEGIN 1 */
/**
* @brief This function handles CAN1 RX0 interrupt.
*/
void CAN1_RX0_IRQHandler(void)
{
HAL_CAN_IRQHandler(&hcan1);
}
/**
* @brief This function handles CAN2 RX0 interrupt.
*/
void CAN2_RX0_IRQHandler(void)
{
HAL_CAN_IRQHandler(&hcan2);
}
/* USER CODE END 1 */