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
This commit was merged in pull request #8.
This commit is contained in:
+9
-12
@@ -23,28 +23,25 @@
|
|||||||
#define CANLOG_H
|
#define CANLOG_H
|
||||||
|
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
#include "cmsis_os.h"
|
||||||
|
|
||||||
/* Decladerations for queues */
|
|
||||||
extern osMessageQueueId_t xCAN1RxQueue;
|
extern osMessageQueueId_t xCAN1RxQueue;
|
||||||
extern osMessageQueueId_t xCAN2RxQueue;
|
extern osMessageQueueId_t xCAN2RxQueue;
|
||||||
|
extern osSemaphoreId_t xLEDSemaphoreCAN1;
|
||||||
|
extern osSemaphoreId_t xLEDSemaphoreCAN2;
|
||||||
|
|
||||||
/* Decladerations for thread handles and attributes */
|
typedef struct {
|
||||||
extern osThreadId_t vCAN1_rx;
|
LED_Config *led;
|
||||||
extern osThreadId_t vCAN2_rx;
|
osSemaphoreId_t semaphore;
|
||||||
extern osThreadId_t vLED_CAN1_Heartbeat;
|
osTimerId_t timer;
|
||||||
extern osThreadId_t vLED_CAN2_Heartbeat;
|
} LEDContext;
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Initializes the CAN logger modules, OS threads, queues, and hardware.
|
* @brief Initializes the CAN logger modules, OS threads, queues, and hardware.
|
||||||
* @param hcan1 Pointer to the CAN1 handle
|
* @param hcan1 Pointer to the CAN1 handle
|
||||||
* @param hcan2 Pointer to the CAN2 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 HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan);
|
||||||
void vCANLoggerListen(void *argument);
|
void vCANLoggerListen(void *argument);
|
||||||
void vLEDHeartbeat(void *argument);
|
void vLEDHeartbeat(void *argument);
|
||||||
|
|||||||
+19
-8
@@ -82,12 +82,12 @@ void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
|
|||||||
memcpy(message.payload, data, rxHeader.DLC);
|
memcpy(message.payload, data, rxHeader.DLC);
|
||||||
|
|
||||||
osMessageQueueId_t queue = (hcan->Instance == CAN1) ? xCAN1RxQueue : xCAN2RxQueue;
|
osMessageQueueId_t queue = (hcan->Instance == CAN1) ? xCAN1RxQueue : xCAN2RxQueue;
|
||||||
if (osMessageQueuePut(queue, &message, 0U, 0U) != osOK) Error_Handler();
|
if (osMessageQueuePut(queue, &message, 0U, 0U) != osOK)
|
||||||
|
{
|
||||||
// TODO: better handling: flash error_led on osTimeout, call Error_Handler() when other errors
|
// TODO: better handling: flash error_led on osTimeout, call Error_Handler() when other errors
|
||||||
|
|
||||||
// Issue #1: set an event flag for led task
|
// send errors like queue full via UART
|
||||||
// osEventFlagsSet(xCanEventFlags, (hcan->Instance == CAN1) ? 0x01 : 0x02);
|
}
|
||||||
|
|
||||||
/* CODE END */
|
/* CODE END */
|
||||||
}
|
}
|
||||||
/* END HAL_CAN_RxFifo0MsgPendingCallback */
|
/* END HAL_CAN_RxFifo0MsgPendingCallback */
|
||||||
@@ -109,11 +109,13 @@ void vCANLoggerListen(void *argument)
|
|||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
if (osMessageQueueGet(queue, &msg, NULL, osWaitForever) == osOK)
|
if (osMessageQueueGet(queue, &message, NULL, osWaitForever) == osOK)
|
||||||
{
|
{
|
||||||
// TODO: parse message send message to serial/uart
|
// 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 */
|
/* CODE END */
|
||||||
}
|
}
|
||||||
@@ -128,8 +130,17 @@ void vCANLoggerListen(void *argument)
|
|||||||
void vLEDHeartbeat(void *argument)
|
void vLEDHeartbeat(void *argument)
|
||||||
{
|
{
|
||||||
/* CODE BEGIN */
|
/* CODE BEGIN */
|
||||||
LED_Config *led = (LED_Config*)argument;
|
LEDContext *ctx = (LEDContext*)argument;
|
||||||
HAL_GPIO_TogglePin(led->port, led->pin);
|
|
||||||
|
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 */
|
/* CODE END */
|
||||||
}
|
}
|
||||||
/* END vLEDHeartbeat */
|
/* END vLEDHeartbeat */
|
||||||
|
|||||||
+39
-13
@@ -23,7 +23,6 @@
|
|||||||
/* Private includes ----------------------------------------------------------*/
|
/* Private includes ----------------------------------------------------------*/
|
||||||
/* USER CODE BEGIN Includes */
|
/* USER CODE BEGIN Includes */
|
||||||
#include "canlog.h"
|
#include "canlog.h"
|
||||||
#include <system_error>
|
|
||||||
/* USER CODE END Includes */
|
/* USER CODE END Includes */
|
||||||
|
|
||||||
/* Private typedef -----------------------------------------------------------*/
|
/* 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_can2 = {GPIOB, GPIO_PIN_5};
|
||||||
LED_Config led_error = {GPIOB, GPIO_PIN_3};
|
LED_Config led_error = {GPIOB, GPIO_PIN_3};
|
||||||
|
|
||||||
|
LEDContext ledContextCAN1;
|
||||||
|
LEDContext ledContextCAN2;
|
||||||
|
|
||||||
|
osSemaphoreId_t xLEDSemaphoreCAN1;
|
||||||
|
osSemaphoreId_t xLEDSemaphoreCAN2;
|
||||||
|
|
||||||
osTimerId_t xHeartbeatTimerCAN1;
|
osTimerId_t xHeartbeatTimerCAN1;
|
||||||
osTimerId_t xHeartbeatTimerCAN2;
|
osTimerId_t xHeartbeatTimerCAN2;
|
||||||
|
|
||||||
|
osMessageQueueId_t xCAN1RxQueue;
|
||||||
|
osMessageQueueId_t xCAN2RxQueue;
|
||||||
/* USER CODE END PV */
|
/* USER CODE END PV */
|
||||||
|
|
||||||
/* Private function prototypes -----------------------------------------------*/
|
/* Private function prototypes -----------------------------------------------*/
|
||||||
@@ -147,24 +155,32 @@ int main(void)
|
|||||||
/* USER CODE END RTOS_MUTEX */
|
/* USER CODE END RTOS_MUTEX */
|
||||||
|
|
||||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
/* 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 END RTOS_SEMAPHORES */
|
||||||
|
|
||||||
/* USER CODE BEGIN RTOS_TIMERS */
|
/* USER CODE BEGIN RTOS_TIMERS */
|
||||||
xHeartbeatTimerCAN1 = osTimerNew(vLEDHeartbeat, osTimerPeriodic, &led_can1, NULL);
|
xHeartbeatTimerCAN1 = osTimerNew(vLEDHeartbeat, osTimerOnce, &ledContextCAN1, NULL);
|
||||||
osTimerStart(xHeartbeatTimerCAN1, 250U);
|
xHeartbeatTimerCAN2 = osTimerNew(vLEDHeartbeat, osTimerOnce, &ledContextCAN2, NULL);
|
||||||
|
|
||||||
xHeartbeatTimerCAN2 = osTimerNew(vLEDHeartbeat, osTimerPeriodic, &led_can2, NULL);
|
osTimerStart(xHeartbeatTimerCAN1, 25U);
|
||||||
osTimerStart(xHeartbeatTimerCAN2, 250U);
|
osTimerStart(xHeartbeatTimerCAN2, 25U);
|
||||||
|
|
||||||
if (xHeartbeatTimerCAN1 == NULL || xHeartbeatTimerCAN2 == NULL) Error_Handler();
|
if (xHeartbeatTimerCAN1 == NULL || xHeartbeatTimerCAN2 == NULL) Error_Handler();
|
||||||
|
|
||||||
|
ledContextCAN1.timer = xHeartbeatTimerCAN1;
|
||||||
|
ledContextCAN2.timer = xHeartbeatTimerCAN2;
|
||||||
/* USER CODE END RTOS_TIMERS */
|
/* USER CODE END RTOS_TIMERS */
|
||||||
|
|
||||||
/* USER CODE BEGIN RTOS_QUEUES */
|
/* USER CODE BEGIN RTOS_QUEUES */
|
||||||
osMessageQueueId_t xCAN1RxQueue;
|
|
||||||
xCAN1RxQueue = osMessageQueueNew(32, sizeof(CanMessage_t), NULL);
|
xCAN1RxQueue = osMessageQueueNew(32, sizeof(CanMessage_t), NULL);
|
||||||
|
|
||||||
osMessageQueueId_t xCAN2RxQueue;
|
|
||||||
xCAN2RxQueue = osMessageQueueNew(32, sizeof(CanMessage_t), NULL);
|
xCAN2RxQueue = osMessageQueueNew(32, sizeof(CanMessage_t), NULL);
|
||||||
|
|
||||||
if (xCAN1RxQueue == NULL || xCAN2RxQueue == NULL) Error_Handler();
|
if (xCAN1RxQueue == NULL || xCAN2RxQueue == NULL) Error_Handler();
|
||||||
@@ -179,6 +195,16 @@ int main(void)
|
|||||||
/* add events, ... */
|
/* add events, ... */
|
||||||
/* USER CODE END RTOS_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 */
|
/* Start scheduler */
|
||||||
osKernelStart();
|
osKernelStart();
|
||||||
|
|
||||||
@@ -190,9 +216,9 @@ int main(void)
|
|||||||
{
|
{
|
||||||
/* USER CODE END WHILE */
|
/* 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 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
|
* @brief Period elapsed callback in non blocking mode
|
||||||
|
|||||||
Reference in New Issue
Block a user