8 Commits

Author SHA1 Message Date
eeeck b737c27b49 Merge pull request 'Remove complex timer based toggling and switch to RTOS task with semaphore based delay' (#18) from dev-rtos-semaphore into main
Reviewed-on: erickahmed/j1939_logger#18
2026-06-15 14:33:38 +02:00
eeeck 2c14d095f7 Better way to avoid race condition, only notification lives in CAN task
- Runs only once
- Only critical notification is inside task
- CAN message during boot will not be lost, instead put inside FIFO0
2026-06-15 14:30:35 +02:00
eeeck 975acb8c3e Indentation fixes 2026-06-15 14:27:05 +02:00
eeeck b1744ce941 Fix double semaphore release 2026-06-15 14:17:12 +02:00
eeeck c1d2e65e20 Move to RTOS_THREADS
- ops...
2026-06-15 14:12:47 +02:00
eeeck ce85e473b3 Move before starting scheduler to avoid race condition
- If a CAN message arrives during boot time ISR will fire and try to put
  data into xCAN1RxQueue. At that time queue is NULL: calling RTOS APIs
  on NULL handles will cause a HardFault
2026-06-15 14:11:11 +02:00
eeeck 8c8fe93d5b Release CANrx semaphore in ISR 2026-06-15 14:09:51 +02:00
eeeck ab8b68313b Transition from timer based to task based LED signal
- Remove RTOS timers
- Create LED task
- Avoid polling, instead wait for semaphore
2026-06-15 14:06:51 +02:00
2 changed files with 18 additions and 11 deletions
+16 -9
View File
@@ -47,14 +47,11 @@ void CAN_Logger_Init(CAN_HandleTypeDef *hcan1, CAN_HandleTypeDef *hcan2)
filter.FilterBank = 0;
if (HAL_CAN_ConfigFilter(hcan1, &filter) != HAL_OK) Error_Handler();
if (HAL_CAN_Start(hcan1) != HAL_OK) Error_Handler();
filter.FilterBank = 14;
if (HAL_CAN_ConfigFilter(hcan2, &filter) != HAL_OK) Error_Handler();
if (HAL_CAN_Start(hcan1) != HAL_OK) Error_Handler();
if (HAL_CAN_Start(hcan2) != HAL_OK) Error_Handler();
if (HAL_CAN_ActivateNotification(hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK) Error_Handler();
if (HAL_CAN_ActivateNotification(hcan2, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK) Error_Handler();
}
/* END CAN_Logger_Init */
@@ -85,6 +82,9 @@ void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
// send errors like queue full via UART
}
osSemaphoreId_t semaphore = (hcan->Instance == CAN1) ? xSemaphoreCAN1 : xSemaphoreCAN2;
osSemaphoreRelease(semaphore);
/* CODE END */
}
/* END HAL_CAN_RxFifo0MsgPendingCallback */
@@ -98,13 +98,20 @@ void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
void vCANLoggerListen(void *argument)
{
/* CODE BEGIN */
LEDContext *context = (LEDContext*)argument;
CAN_HandleTypeDef *hcan = (CAN_HandleTypeDef*)argument;
osMessageQueueId_t queue = (hcan->Instance == CAN1) ? xCAN1RxQueue : xCAN2RxQueue;
CanMessage_t message;
HAL_CAN_ActivateNotification(hcan, CAN_IT_RX_FIFO0_MSG_PENDING);
for (;;)
{
if (osSemaphoreAcquire(context->semaphore, 25U) == osOK) HAL_GPIO_WritePin(context->led->port, context->led->pin, GPIO_PIN_SET);
else HAL_GPIO_WritePin(context->led->port, context->led->pin, GPIO_PIN_RESET);
/* CODE END */
if (osMessageQueueGet(queue, &message, NULL, osWaitForever) == osOK)
{
//
}
}
/* CODE END */
}
/* END vCANLoggerListen */
+2 -2
View File
@@ -169,8 +169,6 @@ int main(void)
/* USER CODE BEGIN RTOS_QUEUES */
xCAN1RxQueue = osMessageQueueNew(32, sizeof(CanMessage_t), NULL);
xCAN2RxQueue = osMessageQueueNew(32, sizeof(CanMessage_t), NULL);
xLEDHeartbeatCAN1 = osThreadNew(vLEDHeartbeat, &ledContextCAN1, &LEDHeartbeatCAN1Attributes);
xLEDHeartbeatCAN2 = osThreadNew(vLEDHeartbeat, &ledContextCAN2, &LEDHeartbeatCAN2Attributes);
if (xCAN1RxQueue == NULL || xCAN2RxQueue == NULL) Error_Handler();
/* USER CODE END RTOS_QUEUES */
@@ -178,6 +176,8 @@ int main(void)
/* USER CODE BEGIN RTOS_THREADS */
xCAN1rx = osThreadNew(vCANLoggerListen, &hcan1, &CAN1rxAttributes);
xCAN2rx = osThreadNew(vCANLoggerListen, &hcan2, &CAN2rxAttributes);
xLEDHeartbeatCAN1 = osThreadNew(vLEDHeartbeat, &ledContextCAN1, &LEDHeartbeatCAN1Attributes);
xLEDHeartbeatCAN2 = osThreadNew(vLEDHeartbeat, &ledContextCAN2, &LEDHeartbeatCAN2Attributes);
/* USER CODE END RTOS_THREADS */
/* USER CODE BEGIN RTOS_EVENTS */