Refactor: implement data relay from vCANLoggerListen to xUARTQueue
- No RTOS event, just queue put and get: cleaner way to send data and more clear separation of concerns
This commit is contained in:
@@ -28,6 +28,7 @@
|
|||||||
|
|
||||||
extern osMessageQueueId_t xCAN1RxQueue;
|
extern osMessageQueueId_t xCAN1RxQueue;
|
||||||
extern osMessageQueueId_t xCAN2RxQueue;
|
extern osMessageQueueId_t xCAN2RxQueue;
|
||||||
|
extern osMessageQueueId_t xUARTQueue;
|
||||||
|
|
||||||
/* USER CODE BEGIN PTD */
|
/* USER CODE BEGIN PTD */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -40,6 +41,7 @@ typedef struct {
|
|||||||
uint8_t payload[8];
|
uint8_t payload[8];
|
||||||
uint8_t dlc;
|
uint8_t dlc;
|
||||||
uint8_t isExtended;
|
uint8_t isExtended;
|
||||||
|
uint8_t source;
|
||||||
//uint32_t timestamp; //Enable TIM2: 42 MHz / (41+1) = 1 MHz → 1 µs tick; 32bit autoreload freerunning
|
//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
|
// this is for getting timestamps on can messages
|
||||||
} CanMessage_t;
|
} CanMessage_t;
|
||||||
|
|||||||
@@ -20,6 +20,8 @@
|
|||||||
#ifndef CANSEND_H
|
#ifndef CANSEND_H
|
||||||
#define CANSEND_H
|
#define CANSEND_H
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
|
|
||||||
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart);
|
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart);
|
||||||
void vUARTLogger(void *argument);
|
void vUARTLogger(void *argument);
|
||||||
|
|
||||||
|
|||||||
+8
-15
@@ -27,8 +27,6 @@ extern CAN_HandleTypeDef hcan2;
|
|||||||
extern osThreadId_t xCAN1LedTask;
|
extern osThreadId_t xCAN1LedTask;
|
||||||
extern osThreadId_t xCAN2LedTask;
|
extern osThreadId_t xCAN2LedTask;
|
||||||
|
|
||||||
extern osEventFlagsId_t xCanEventFlags;
|
|
||||||
|
|
||||||
/* Private function prototypes -----------------------------------------------*/
|
/* Private function prototypes -----------------------------------------------*/
|
||||||
/* USER CODE BEGIN FunctionPrototypes */
|
/* USER CODE BEGIN FunctionPrototypes */
|
||||||
/**
|
/**
|
||||||
@@ -82,6 +80,7 @@ void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
|
|||||||
message.id = rxHeader.ExtId;
|
message.id = rxHeader.ExtId;
|
||||||
message.dlc = rxHeader.DLC;
|
message.dlc = rxHeader.DLC;
|
||||||
message.isExtended = (rxHeader.IDE == CAN_ID_EXT);
|
message.isExtended = (rxHeader.IDE == CAN_ID_EXT);
|
||||||
|
message.source = (hcan->Instance == CAN1) ? 1 : 2;
|
||||||
memcpy(message.payload, data, rxHeader.DLC);
|
memcpy(message.payload, data, rxHeader.DLC);
|
||||||
|
|
||||||
if (hcan->Instance == CAN1)
|
if (hcan->Instance == CAN1)
|
||||||
@@ -99,16 +98,8 @@ void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
|
|||||||
|
|
||||||
if (osMessageQueuePut(queue, &message, 0U, 0U) == osOK)
|
if (osMessageQueuePut(queue, &message, 0U, 0U) == osOK)
|
||||||
{
|
{
|
||||||
osEventFlagsSet(xCanEventFlags, flag);
|
|
||||||
osThreadFlagsSet(led_task, 0x01);
|
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 */
|
/* END HAL_CAN_RxFifo0MsgPendingCallback */
|
||||||
|
|
||||||
@@ -128,11 +119,14 @@ void vCANLoggerListen(void *argument)
|
|||||||
|
|
||||||
for (;;)
|
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 */
|
/* END vCANLoggerListen */
|
||||||
|
|
||||||
@@ -153,7 +147,6 @@ void vLEDHeartbeat(void *argument)
|
|||||||
if (notification & 0x01) HAL_GPIO_WritePin(led->port, led->pin, GPIO_PIN_SET);
|
if (notification & 0x01) HAL_GPIO_WritePin(led->port, led->pin, GPIO_PIN_SET);
|
||||||
else HAL_GPIO_WritePin(led->port, led->pin, GPIO_PIN_RESET);
|
else HAL_GPIO_WritePin(led->port, led->pin, GPIO_PIN_RESET);
|
||||||
}
|
}
|
||||||
/* CODE END */
|
|
||||||
}
|
}
|
||||||
/* END vLEDHeartbeat */
|
/* END vLEDHeartbeat */
|
||||||
/* USER CODE END FunctionPrototypes */
|
/* USER CODE END FunctionPrototypes */
|
||||||
|
|||||||
+9
-29
@@ -17,7 +17,9 @@
|
|||||||
#include "cansend.h"
|
#include "cansend.h"
|
||||||
/* Private includes ----------------------------------------------------------*/
|
/* Private includes ----------------------------------------------------------*/
|
||||||
/* USER CODE BEGIN Includes */
|
/* USER CODE BEGIN Includes */
|
||||||
|
#include "main.h"
|
||||||
#include "cmsis_os.h"
|
#include "cmsis_os.h"
|
||||||
|
#include "canlog.h"
|
||||||
/* USER CODE END Includes */
|
/* USER CODE END Includes */
|
||||||
|
|
||||||
/* BEGIN format_can_message */
|
/* BEGIN format_can_message */
|
||||||
@@ -80,7 +82,7 @@ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
|
|||||||
if (huart->Instance == USART1)
|
if (huart->Instance == USART1)
|
||||||
{
|
{
|
||||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||||
osSemaphoreReleaseFromISR(xUartDmaSem, &xHigherPriorityTaskWoken);
|
osSemaphoreReleaseFromISR(xUARTDMASemaphore, &xHigherPriorityTaskWoken);
|
||||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -94,39 +96,17 @@ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
|
|||||||
*/
|
*/
|
||||||
void vUARTLogger(void *argument)
|
void vUARTLogger(void *argument)
|
||||||
{
|
{
|
||||||
CanMessage_t message;
|
CanMessage_t msg;
|
||||||
char tx_buffer[45]; // converted extended frame
|
char tx_buffer[45];
|
||||||
bool queues_empty;
|
|
||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
|
||||||
osEventFlagsWait(xCanEventFlags, 0x03, osFlagsWaitAny, osWaitForever);
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
{
|
||||||
queues_empty = true;
|
if (osMessageQueueGet(xUARTQueue, &msg, NULL, osWaitForever) == osOK)
|
||||||
|
|
||||||
if (osMessageQueueGet(xCAN1RxQueue, &message, NULL, 0U) == 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);
|
HAL_UART_Transmit_DMA(&huart1, (uint8_t*)tx_buffer, 44);
|
||||||
osSemaphoreAcquire(xUartDmaSem, osWaitForever);
|
osSemaphoreAcquire(xUARTDMASemaphore, osWaitForever);
|
||||||
|
|
||||||
quesues_empty = false;
|
|
||||||
}
|
}
|
||||||
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 */
|
/* END vUARTLoggerListen */
|
||||||
|
|||||||
+4
-4
@@ -54,10 +54,11 @@ LED_Config led_error = {GPIOB, GPIO_PIN_3};
|
|||||||
|
|
||||||
osMessageQueueId_t xCAN1RxQueue;
|
osMessageQueueId_t xCAN1RxQueue;
|
||||||
osMessageQueueId_t xCAN2RxQueue;
|
osMessageQueueId_t xCAN2RxQueue;
|
||||||
|
osMessageQueueId_t xUARTQueue;
|
||||||
|
|
||||||
osEventFlagsId_t xCanEventFlags;
|
|
||||||
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 -----------------------------------------------*/
|
||||||
@@ -168,8 +169,9 @@ int main(void)
|
|||||||
/* USER CODE BEGIN RTOS_QUEUES */
|
/* USER CODE BEGIN RTOS_QUEUES */
|
||||||
xCAN1RxQueue = osMessageQueueNew(32, sizeof(CanMessage_t), NULL);
|
xCAN1RxQueue = osMessageQueueNew(32, sizeof(CanMessage_t), NULL);
|
||||||
xCAN2RxQueue = 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 END RTOS_QUEUES */
|
||||||
|
|
||||||
/* USER CODE BEGIN RTOS_THREADS */
|
/* USER CODE BEGIN RTOS_THREADS */
|
||||||
@@ -181,8 +183,6 @@ int main(void)
|
|||||||
/* USER CODE END RTOS_THREADS */
|
/* USER CODE END RTOS_THREADS */
|
||||||
|
|
||||||
/* USER CODE BEGIN RTOS_EVENTS */
|
/* USER CODE BEGIN RTOS_EVENTS */
|
||||||
xCanEventFlags = osEventFlagsNew(NULL);
|
|
||||||
if (xCanEventFlags == NULL) Error_Handler();
|
|
||||||
/* USER CODE END RTOS_EVENTS */
|
/* USER CODE END RTOS_EVENTS */
|
||||||
|
|
||||||
/* Start scheduler */
|
/* Start scheduler */
|
||||||
|
|||||||
Reference in New Issue
Block a user