Create context structure for timer callback

Strucure is made of
-  LED (port, pin)
- Semaphore
- Timer handle
This commit is contained in:
2026-06-14 15:12:54 +02:00
parent 5b500d72ff
commit 643cac8ea6
2 changed files with 19 additions and 5 deletions
+4 -5
View File
@@ -132,16 +132,15 @@ void vLEDHeartbeat(void *argument)
/* CODE BEGIN */
LED_Config *led = (LED_Config*)argument;
if (osSemaphoreAcquire(xCanActivitySemaphore, 0U) == osOK)
if (osSemaphoreAcquire(ctx->sem, 0U) == osOK)
{
HAL_GPIO_WritePin(led->port, led->pin, GPIO_PIN_SET);
osTimerStart(xHeartbeatTimerCAN1, 25U);
HAL_GPIO_WritePin(ctx->led->port, ctx->led->pin, GPIO_PIN_SET);
osTimerStart(ctx->timer, 25U);
}
else
{
HAL_GPIO_WritePin(led->port, led->pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(ctx->led->port, ctx->led->pin, GPIO_PIN_RESET);
}
/* CODE END */
}
/* END vLEDHeartbeat */
+15
View File
@@ -41,6 +41,12 @@ typedef struct {
//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;
typedef struct {
LED_Config *led;
osSemaphoreId_t sem;
osTimerId_t timer;
} LEDContext;
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
@@ -66,6 +72,9 @@ 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 ledCtxCAN1;
LEDContext ledCtxCAN2;
osSemaphoreId_t xLEDSemaphoreCAN1;
osSemaphoreId_t xLEDSemaphoreCAN2;
@@ -109,7 +118,13 @@ int main(void)
HAL_Init();
/* USER CODE BEGIN Init */
ledCtxCAN1.led = &led_can1;
ledCtxCAN1.sem = xLEDSemaphoreCAN1;
ledCtxCAN1.timer = xHeartbeatTimerCAN1;
ledCtxCAN2.led = &led_can2;
ledCtxCAN2.sem = xLEDSemaphoreCAN2;
ledCtxCAN2.timer = xHeartbeatTimerCAN2;
/* USER CODE END Init */
/* Configure the system clock */