2 Commits

Author SHA1 Message Date
eeeck 7e2ffd3971 Implement LED heartbeat with RTOS timers
- Less memory overhead (no stack allocation)
- Less context switch
- Better timing accuracy (less jitter)
2026-06-10 14:04:05 +02:00
eeeck 0c3c93040b Simplify CAN error code handler 2026-06-10 13:16:19 +02:00
2 changed files with 15 additions and 13 deletions
+2 -8
View File
@@ -121,17 +121,11 @@ void vLED_Heartbeat(void *argument)
{
/* CODE BEGIN */
LED_Config *led = (LED_Config*)argument;
HAL_GPIO_WritePin(led->port, led->pin, GPIO_RESET);
for(;;)
{
//HAL_GPIO_WritePin(led->port, led->pin, GPIO_PIN_RESET);
// if semaphore for canrx, do the rest
HAL_GPIO_WritePin(led->port, led->pin, GPIO_PIN_SET);
osDelay(100);
HAL_GPIO_WritePin(led->port, led->pin, GPIO_PIN_RESET);
osDelay(100);
HAL_GPIO_TogglePin(led->port, led->pin);
}
/* CODE END */
}
+13 -5
View File
@@ -23,6 +23,7 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "canlog.h"
#include <system_error>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@@ -55,6 +56,9 @@ DMA_HandleTypeDef hdma_sdio_tx;
LED_Config led_can1 = {GPIOB, GPIO_PIN_2};
LED_Config led_can2 = {GPIOB, GPIO_PIN_5};
LED_Config led_error = {GPIOB, GPIO_PIN_3};
osTimerId_t heartbeat_timer_can1;
osTimerId_t heartbeat_timer_can2;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
@@ -126,7 +130,11 @@ int main(void)
/* USER CODE END RTOS_SEMAPHORES */
/* USER CODE BEGIN RTOS_TIMERS */
/* start timers, add new ones, ... */
heartbeat_timer_can1 = osTimerNew(vLED_Heartbeat, osTimerPeriodic, &led_can1, NULL);
if (heartbeat_timer_can1 != NULL) osTimerStart(heartbeat_timer_can1, 250U);
heartbeat_timer_can2 = osTimerNew(vLED_Heartbeat, osTimerPeriodic, &led_can2, NULL);
if (heartbeat_timer_can2 != NULL) osTimerStart(heartbeat_timer_can2, 250U);
/* USER CODE END RTOS_TIMERS */
/* USER CODE BEGIN RTOS_QUEUES */
@@ -445,11 +453,11 @@ void Error_Handler(void)
//TODO: check if it is necessary to stop FreeRTOS
uint32_t error_can1 == 0;
uint32_t error_can2 == 0;
uint32_t error_code_can1;
uint32_t error_code_can2;
if (HAL_CAN_GetState(&hcan1) != HAL_CAN_STATE_READY) error_code = HAL_CAN_GetError(&hcan1);
else if (HAL_CAN_GetState(&hcan2) != HAL_CAN_STATE_READY) error_code = HAL_CAN_GetError(&hcan2);
if (HAL_CAN_GetState(&hcan1) != HAL_CAN_STATE_READY) error_code_can1 = HAL_CAN_GetError(&hcan1);
if (HAL_CAN_GetState(&hcan2) != HAL_CAN_STATE_READY) error_code_can2 = HAL_CAN_GetError(&hcan2);
// TODO: write error to SD and/or serial