diff --git a/Core/Inc/canlog.h b/Core/Inc/canlog.h index 2a037ed..813dcfe 100644 --- a/Core/Inc/canlog.h +++ b/Core/Inc/canlog.h @@ -28,6 +28,7 @@ extern osMessageQueueId_t xCAN1RxQueue; extern osMessageQueueId_t xCAN2RxQueue; +extern osMessageQueueId_t xUARTQueue; /* USER CODE BEGIN PTD */ typedef struct { @@ -40,6 +41,7 @@ typedef struct { uint8_t payload[8]; uint8_t dlc; uint8_t isExtended; + uint8_t source; //uint32_t timestamp; //Enable TIM2: 42 MHz / (41+1) = 1 MHz → 1 µs tick; 32bit autoreload freerunning // this is for getting timestamps on can messages } CanMessage_t; diff --git a/Core/Inc/cansend.h b/Core/Inc/cansend.h index 713aeb5..7124050 100644 --- a/Core/Inc/cansend.h +++ b/Core/Inc/cansend.h @@ -20,6 +20,8 @@ #ifndef CANSEND_H #define CANSEND_H +#include "main.h" + void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart); void vUARTLogger(void *argument); diff --git a/Core/Src/canlog.c b/Core/Src/canlog.c index cca6a25..38a9466 100644 --- a/Core/Src/canlog.c +++ b/Core/Src/canlog.c @@ -27,8 +27,6 @@ extern CAN_HandleTypeDef hcan2; extern osThreadId_t xCAN1LedTask; extern osThreadId_t xCAN2LedTask; -extern osEventFlagsId_t xCanEventFlags; - /* Private function prototypes -----------------------------------------------*/ /* USER CODE BEGIN FunctionPrototypes */ /** @@ -82,6 +80,7 @@ void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) message.id = rxHeader.ExtId; message.dlc = rxHeader.DLC; message.isExtended = (rxHeader.IDE == CAN_ID_EXT); + message.source = (hcan->Instance == CAN1) ? 1 : 2; memcpy(message.payload, data, rxHeader.DLC); if (hcan->Instance == CAN1) @@ -99,16 +98,8 @@ void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) if (osMessageQueuePut(queue, &message, 0U, 0U) == osOK) { - osEventFlagsSet(xCanEventFlags, flag); osThreadFlagsSet(led_task, 0x01); } - else - { - // TODO: better handling: flash error_led on osTimeout, call Error_Handler() when other errors - - // send errors like queue full via UART - } - /* CODE END */ } /* END HAL_CAN_RxFifo0MsgPendingCallback */ @@ -128,11 +119,14 @@ void vCANLoggerListen(void *argument) for (;;) { - if (osMessageQueueGet(queue, &message, NULL, osWaitForever) == osOK) - { - } + if (osMessageQueueGet(queue, &message, NULL, osWaitForever) == osOK) + { + // J1939 decoding + + // RELAY: Push to the UART queue + osMessageQueuePut(xUARTQueue, &message, 0U, 0U); + } } - /* CODE END */ } /* END vCANLoggerListen */ @@ -153,7 +147,6 @@ void vLEDHeartbeat(void *argument) if (notification & 0x01) HAL_GPIO_WritePin(led->port, led->pin, GPIO_PIN_SET); else HAL_GPIO_WritePin(led->port, led->pin, GPIO_PIN_RESET); } - /* CODE END */ } /* END vLEDHeartbeat */ /* USER CODE END FunctionPrototypes */ diff --git a/Core/Src/cansend.c b/Core/Src/cansend.c index 2e7809e..50485d9 100644 --- a/Core/Src/cansend.c +++ b/Core/Src/cansend.c @@ -17,7 +17,9 @@ #include "cansend.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ +#include "main.h" #include "cmsis_os.h" +#include "canlog.h" /* USER CODE END Includes */ /* BEGIN format_can_message */ @@ -80,7 +82,7 @@ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) if (huart->Instance == USART1) { BaseType_t xHigherPriorityTaskWoken = pdFALSE; - osSemaphoreReleaseFromISR(xUartDmaSem, &xHigherPriorityTaskWoken); + osSemaphoreReleaseFromISR(xUARTDMASemaphore, &xHigherPriorityTaskWoken); portYIELD_FROM_ISR(xHigherPriorityTaskWoken); } } @@ -94,39 +96,17 @@ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) */ void vUARTLogger(void *argument) { - CanMessage_t message; - char tx_buffer[45]; // converted extended frame - bool queues_empty; + CanMessage_t msg; + char tx_buffer[45]; - for (;;) - { - osEventFlagsWait(xCanEventFlags, 0x03, osFlagsWaitAny, osWaitForever); - - do + for (;;) { - queues_empty = true; - - if (osMessageQueueGet(xCAN1RxQueue, &message, NULL, 0U) == osOK) + if (osMessageQueueGet(xUARTQueue, &msg, NULL, osWaitForever) == osOK) { - format_can_message(tx_buffer, 1, &message); - + format_can_message(tx_buffer, msg.source, &msg); // Assuming you add 'source' to the struct HAL_UART_Transmit_DMA(&huart1, (uint8_t*)tx_buffer, 44); - osSemaphoreAcquire(xUartDmaSem, osWaitForever); - - quesues_empty = false; + osSemaphoreAcquire(xUARTDMASemaphore, osWaitForever); } - if (osMessageQueueGet(xCAN2RxQueue, &message, NULL, 0U) == osOK) - { - format_can_message(tx_buffer, 2, &message); - - HAL_UART_Transmit_DMA(&huart1, (uint8_t*)tx_buffer, 44); - osSemaphoreAcquire(xUartDmaSem, osWaitForever); - - queues_empty = false; - } - } - while (!queues_empty); - } } /* END vUARTLoggerListen */ diff --git a/Core/Src/main.c b/Core/Src/main.c index 2e1a4b5..c4fddcd 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -54,10 +54,11 @@ LED_Config led_error = {GPIOB, GPIO_PIN_3}; osMessageQueueId_t xCAN1RxQueue; osMessageQueueId_t xCAN2RxQueue; +osMessageQueueId_t xUARTQueue; -osEventFlagsId_t xCanEventFlags; osSemaphoreId_t xUARTDMASemaphore; osThreadId_t xUartTask; + /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ @@ -168,8 +169,9 @@ int main(void) /* USER CODE BEGIN RTOS_QUEUES */ xCAN1RxQueue = osMessageQueueNew(32, sizeof(CanMessage_t), NULL); xCAN2RxQueue = osMessageQueueNew(32, sizeof(CanMessage_t), NULL); + xUARTQueue = osMessageQueueNew(32, sizeof(CanMessage_t), NULL); - if (xCAN1RxQueue == NULL || xCAN2RxQueue == NULL) Error_Handler(); + if (xCAN1RxQueue == NULL || xCAN2RxQueue == NULL || xUARTQueue == NULL) Error_Handler(); /* USER CODE END RTOS_QUEUES */ /* USER CODE BEGIN RTOS_THREADS */ @@ -181,8 +183,6 @@ int main(void) /* USER CODE END RTOS_THREADS */ /* USER CODE BEGIN RTOS_EVENTS */ - xCanEventFlags = osEventFlagsNew(NULL); - if (xCanEventFlags == NULL) Error_Handler(); /* USER CODE END RTOS_EVENTS */ /* Start scheduler */