23 Commits

Author SHA1 Message Date
eeeck 5a3223af29 Merge pull request 'Use semaphores and one shot RTOS timers to better manage LEDs for incoming CAN message on FIFO buffer' (#8) from led-semaphore into j1939
Reviewed-on: erickahmed/j1939_logger#8
2026-06-14 16:49:17 +02:00
eeeck 53772ccb7d Remove error handling as it would cause crash on full queue 2026-06-14 16:45:02 +02:00
eeeck 9d4d807cfb Fix race condition that could cause a hard fault 2026-06-14 16:21:55 +02:00
eeeck 5ec806ffea Add CMSIS include 2026-06-14 16:17:01 +02:00
eeeck 8777f742ac Move typedef to header 2026-06-14 16:15:33 +02:00
eeeck 4aafa81009 Change CODE BEGIN N value 2026-06-14 16:14:20 +02:00
eeeck ac16f8150e Use a more sensible number of semaphores 2026-06-14 16:10:18 +02:00
eeeck f0d2990b8d Move below timer setup 2026-06-14 16:10:00 +02:00
eeeck 72ffb2644e Use more clear variable names 2026-06-14 16:09:45 +02:00
eeeck 2a3d5d17b3 Remove unnecessary declarations 2026-06-14 16:08:43 +02:00
eeeck 151bac7a13 Fix typo 2026-06-14 16:08:21 +02:00
eeeck 75efc4840d Use new context structure instead of direct pass 2026-06-14 15:17:03 +02:00
eeeck a75d42faeb Create context structure for timer callback
Strucure is made of
-  LED (port, pin)
- Semaphore
- Timer handle
2026-06-14 15:16:23 +02:00
eeeck 5b500d72ff Avoid having to declare additional variable 2026-06-14 14:56:32 +02:00
eeeck 5f1695dc0d Start timer once for automatic rearming with semaphore release 2026-06-14 14:51:46 +02:00
eeeck de6dc24dd3 Acquire semaphore and restart timer if new message incoming 2026-06-14 14:51:33 +02:00
eeeck 1b38c0f29a Release semaphore on incoming message
- Discard previous solution with timer only
2026-06-14 14:50:49 +02:00
eeeck 7ece650f72 Move declaration on top 2026-06-14 14:49:54 +02:00
eeeck ebbd1816fa Write LOW status on LED when timer triggers 2026-06-14 14:19:44 +02:00
eeeck 2e2150ebd2 Write HIGH status on LED on incoming CAN message and start 25ms timer 2026-06-14 14:19:30 +02:00
eeeck d6ea04e33e Change variable name 2026-06-14 14:18:40 +02:00
eeeck 7c7d774522 Create semaphores for signalling new message in FIFO buffer 2026-06-14 14:18:28 +02:00
eeeck c65341c662 Change periodic timers to one shot timers 2026-06-14 14:18:07 +02:00
3 changed files with 68 additions and 34 deletions
+9 -12
View File
@@ -23,28 +23,25 @@
#define CANLOG_H
#include "main.h"
#include "cmsis_os.h"
/* Decladerations for queues */
extern osMessageQueueId_t xCAN1RxQueue;
extern osMessageQueueId_t xCAN2RxQueue;
extern osSemaphoreId_t xLEDSemaphoreCAN1;
extern osSemaphoreId_t xLEDSemaphoreCAN2;
/* Decladerations for thread handles and attributes */
extern osThreadId_t vCAN1_rx;
extern osThreadId_t vCAN2_rx;
extern osThreadId_t vLED_CAN1_Heartbeat;
extern osThreadId_t vLED_CAN2_Heartbeat;
extern const osThreadAttr_t vCAN1_rx_attributes;
extern const osThreadAttr_t vCAN2_rx_attributes;
extern const osThreadAttr_t vLED_CAN1_Heartbeat_attributes;
extern const osThreadAttr_t vLED_CAN2_Heartbeat_attributes;
typedef struct {
LED_Config *led;
osSemaphoreId_t semaphore;
osTimerId_t timer;
} LEDContext;
/**
* @brief Initializes the CAN logger modules, OS threads, queues, and hardware.
* @param hcan1 Pointer to the CAN1 handle
* @param hcan2 Pointer to the CAN2 handle
*/
void vCANLoggerInit(CAN_HandleTypeDef *hcan1, CAN_HandleTypeDef *hcan2);
void CAN_Logger_Init(CAN_HandleTypeDef *hcan1, CAN_HandleTypeDef *hcan2);
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan);
void vCANLoggerListen(void *argument);
void vLEDHeartbeat(void *argument);
+20 -9
View File
@@ -82,12 +82,12 @@ void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
memcpy(message.payload, data, rxHeader.DLC);
osMessageQueueId_t queue = (hcan->Instance == CAN1) ? xCAN1RxQueue : xCAN2RxQueue;
if (osMessageQueuePut(queue, &message, 0U, 0U) != osOK) Error_Handler();
// TODO: better handling: flash error_led on osTimeout, call Error_Handler() when other errors
// Issue #1: set an event flag for led task
// osEventFlagsSet(xCanEventFlags, (hcan->Instance == CAN1) ? 0x01 : 0x02);
if (osMessageQueuePut(queue, &message, 0U, 0U) != osOK)
{
// 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 */
@@ -109,11 +109,13 @@ void vCANLoggerListen(void *argument)
for (;;)
{
if (osMessageQueueGet(queue, &msg, NULL, osWaitForever) == osOK)
if (osMessageQueueGet(queue, &message, NULL, osWaitForever) == osOK)
{
// TODO: parse message send message to serial/uart
// TODO: add rtos semaphore to blink the LED with the timer already set
osSemaphoreRelease( (hcan->Instance == CAN1) ? xLEDSemaphoreCAN1 : xLEDSemaphoreCAN2 );
}
}
/* CODE END */
}
@@ -128,8 +130,17 @@ void vCANLoggerListen(void *argument)
void vLEDHeartbeat(void *argument)
{
/* CODE BEGIN */
LED_Config *led = (LED_Config*)argument;
HAL_GPIO_TogglePin(led->port, led->pin);
LEDContext *ctx = (LEDContext*)argument;
if (osSemaphoreAcquire(ctx->sem, 0U) == osOK)
{
HAL_GPIO_WritePin(ctx->led->port, ctx->led->pin, GPIO_PIN_SET);
osTimerStart(ctx->timer, 25U);
}
else
{
HAL_GPIO_WritePin(ctx->led->port, ctx->led->pin, GPIO_PIN_RESET);
}
/* CODE END */
}
/* END vLEDHeartbeat */
+39 -13
View File
@@ -23,7 +23,6 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "canlog.h"
#include <system_error>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@@ -66,8 +65,17 @@ LED_Config led_can1 = {GPIOB, GPIO_PIN_2};
LED_Config led_can2 = {GPIOB, GPIO_PIN_5};
LED_Config led_error = {GPIOB, GPIO_PIN_3};
LEDContext ledContextCAN1;
LEDContext ledContextCAN2;
osSemaphoreId_t xLEDSemaphoreCAN1;
osSemaphoreId_t xLEDSemaphoreCAN2;
osTimerId_t xHeartbeatTimerCAN1;
osTimerId_t xHeartbeatTimerCAN2;
osMessageQueueId_t xCAN1RxQueue;
osMessageQueueId_t xCAN2RxQueue;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
@@ -147,24 +155,32 @@ int main(void)
/* USER CODE END RTOS_MUTEX */
/* USER CODE BEGIN RTOS_SEMAPHORES */
/* add semaphores, ... */
xLEDSemaphoreCAN1 = osSemaphoreNew(10, 0, NULL);
xLEDSemaphoreCAN2 = osSemaphoreNew(10, 0, NULL);
if (xLEDSemaphoreCAN1 == NULL || xLEDSemaphoreCAN2 == NULL) Error_Handler();
ledContextCAN1.led = &led_can1;
ledContextCAN1.semaphore = xLEDSemaphoreCAN1;
ledContextCAN2.led = &led_can2;
ledContextCAN2.semaphore = xLEDSemaphoreCAN2;
/* USER CODE END RTOS_SEMAPHORES */
/* USER CODE BEGIN RTOS_TIMERS */
xHeartbeatTimerCAN1 = osTimerNew(vLEDHeartbeat, osTimerPeriodic, &led_can1, NULL);
osTimerStart(xHeartbeatTimerCAN1, 250U);
xHeartbeatTimerCAN1 = osTimerNew(vLEDHeartbeat, osTimerOnce, &ledContextCAN1, NULL);
xHeartbeatTimerCAN2 = osTimerNew(vLEDHeartbeat, osTimerOnce, &ledContextCAN2, NULL);
xHeartbeatTimerCAN2 = osTimerNew(vLEDHeartbeat, osTimerPeriodic, &led_can2, NULL);
osTimerStart(xHeartbeatTimerCAN2, 250U);
osTimerStart(xHeartbeatTimerCAN1, 25U);
osTimerStart(xHeartbeatTimerCAN2, 25U);
if (xHeartbeatTimerCAN1 == NULL || xHeartbeatTimerCAN2 == NULL) Error_Handler();
ledContextCAN1.timer = xHeartbeatTimerCAN1;
ledContextCAN2.timer = xHeartbeatTimerCAN2;
/* USER CODE END RTOS_TIMERS */
/* USER CODE BEGIN RTOS_QUEUES */
osMessageQueueId_t xCAN1RxQueue;
xCAN1RxQueue = osMessageQueueNew(32, sizeof(CanMessage_t), NULL);
osMessageQueueId_t xCAN2RxQueue;
xCAN2RxQueue = osMessageQueueNew(32, sizeof(CanMessage_t), NULL);
if (xCAN1RxQueue == NULL || xCAN2RxQueue == NULL) Error_Handler();
@@ -179,6 +195,16 @@ int main(void)
/* add events, ... */
/* USER CODE END RTOS_EVENTS */
/* USER CODE BEGIN 3 */
ledContextCAN1.led = &led_can1;
ledContextCAN1.semaphore = xLEDSemaphoreCAN1;
ledContextCAN1.timer = xHeartbeatTimerCAN1;
ledContextCAN2.led = &led_can2;
ledContextCAN2.semaphore = xLEDSemaphoreCAN2;
ledContextCAN2.timer = xHeartbeatTimerCAN2;
/* USER CODE END 3 */
/* Start scheduler */
osKernelStart();
@@ -190,9 +216,9 @@ int main(void)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
/* USER CODE BEGIN 4 */
}
/* USER CODE END 3 */
/* USER CODE END 4 */
}
/**
@@ -442,9 +468,9 @@ static void MX_GPIO_Init(void)
/* USER CODE END MX_GPIO_Init_2 */
}
/* USER CODE BEGIN 4 */
/* USER CODE BEGIN 5 */
/* USER CODE END 4 */
/* USER CODE END 5 */
/**
* @brief Period elapsed callback in non blocking mode