firmware
IEM Firmware Documentation
|
FreeRTOS CAN receive and transmit tasks for STM32H7xx. More...
#include "can_rtos.h"
#include "iem_utils.h"
#include "iem_types.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "timers.h"
#include "stm32h7xx_hal.h"
#include <string.h>
Data Structures | |
struct | can_tx_params |
struct | msg_timeout |
struct | can_rx_params |
Macros | |
#define | MSG_RX_NOTIF_IDX 0 |
#define | MSG_TX_NOTIF_IDX 0 |
#define | CAN_STD_ID_MAX 0x7FF |
#define | CAN_EXT_ID_MAX 0x1FFFFFFF |
#define | MS_PER_S 1000 |
#define | TIMESTAMP_PRESCALER 16U |
#define | TIMESTAMP_PRESCALER_HAL FDCAN_TIMESTAMP_PRESC_16 |
#define | FIFO_FULL_BLOCK_TIMEOUT pdTICKS_TO_MS(25) |
Functions | |
static void | task_can_tx (void *task_params_ptr) |
Implements the CAN transmit task. | |
static void | task_can_rx (void *task_params_ptr) |
Implements the CAN receive task. | |
static void | can_rx_pending_callback (FDCAN_HandleTypeDef *hfdcan, uint32_t rx_its) |
CAN message receive pending ISR. Unblocks the CAN receive task for the bus/peripheral through a direct-to-task notification. | |
static void | can_rx_wraparound_callback (FDCAN_HandleTypeDef *hfdcan) |
FDCAN peripheral timestamp counter wraparound ISR. Increments the timestamp_ovf_since_last_rx field for each tracked message for the peripheral/task. | |
static void | can_tx_cplt_callback (FDCAN_HandleTypeDef *hfdcan, uint32_t buffer_idxs) |
CAN message transmit complete ISR. Unblocks the CAN transmit task for the bus/peripheral through a direct-to-task notification if the task is waiting for free space the hardware Tx FIFO. | |
static void | can_tx_periodic_msg_callback (TimerHandle_t timer_hdl) |
Scheduled CAN message FreeRTOS timer callback. Timer created and started in canlib_register_scheduled_msgs() | |
static void | can_error_status_callback (FDCAN_HandleTypeDef *hfdcan, uint32_t error_status_its) |
static void | update_msg_timeout (struct can_rx_params *params_ptr, uint32_t msg_id, uint16_t rx_timestamp) |
Updates the timeout timestamp for a CAN ID. Called after a message is received by the CAN receive task. | |
static void | insertion_sort (uint32_t arr_ptr[], unsigned length) |
Sorts a uint32_t array in ascending order. | |
static __always_inline enum can_bus | get_bus (FDCAN_HandleTypeDef *hfdcan) |
Gets the CAN bus associated with an FDCAN peripheral. | |
static __always_inline FDCAN_HandleTypeDef * | get_hfdcan (enum can_bus bus) |
Gets the FDCAN peripheral associated with a CAN bus. | |
void | canlib_tx_task_init (Task_Init_t *init_ptr, enum can_bus bus, FDCAN_HandleTypeDef *hfdcan, uint16_t tx_queue_length) |
Initializes a FreeRTOS CAN transmit task for bus and creates a transmit queue with capacity tx_queue_length . | |
void | canlib_rx_task_init (Task_Init_t *init_ptr, enum can_bus bus, FDCAN_HandleTypeDef *hfdcan, uint32_t rx_location, CAN_Rx_Handler_t handler) |
Initializes a FreeRTOS CAN receive task for bus that receives messages from rx_location . Calls callback when a new message is received. | |
void | canlib_start () |
Starts all STM32 FDCAN peripherals that were registered to a CAN transmit or receive task. | |
bool | canlib_tx_generic (const CAN_Msg_t *msg_ptr, TickType_t block_time, bool is_critical) |
Queues a CAN message for transmission. | |
TimerHandle_t | canlib_register_scheduled_msgs (struct canlib_scheduled_msg_params *params_ptr) |
Registers an array of CAN messages for periodic transmission on a set interval with a FreeRTOS software timer. | |
void | canlib_register_timeout_ids (enum can_bus bus, FDCAN_HandleTypeDef *hfdcan, uint32_t timeout_ids_ptr[], unsigned timeout_ids_cnt) |
Registers CAN messages for timeout tracking by the CAN receive task for a CAN bus (also sorts the array of CAN IDs in ascending order, lol) | |
uint32_t | canlib_get_msg_timeout (enum can_bus bus, uint32_t msg_id) |
Get the time since a message with ID msg_id was received on bus . If the CAN ID wasn't registered with canlib_register_timeout_ids() first on initialization, the returned value will be CANLIB_TIMEOUT_INVALID (UINT32_MAX) | |
bool | canlib_is_bus_ok (enum can_bus bus) |
Checks for protocol errors on bus . |
Variables | |
static TaskHandle_t | g_can_tx_hdls [CAN_BUS_CNT] |
static struct can_tx_params | g_can_tx_params [CAN_BUS_CNT] |
static TaskHandle_t | g_can_rx_hdls [CAN_BUS_CNT] |
static struct can_rx_params | g_can_rx_params [CAN_BUS_CNT] |
FreeRTOS CAN receive and transmit tasks for STM32H7xx.
#define CAN_EXT_ID_MAX 0x1FFFFFFF |
#define CAN_STD_ID_MAX 0x7FF |
#define FIFO_FULL_BLOCK_TIMEOUT pdTICKS_TO_MS(25) |
#define MS_PER_S 1000 |
#define MSG_RX_NOTIF_IDX 0 |
#define MSG_TX_NOTIF_IDX 0 |
#define TIMESTAMP_PRESCALER 16U |
#define TIMESTAMP_PRESCALER_HAL FDCAN_TIMESTAMP_PRESC_16 |
|
static |
hfdcan | |
error_status_its |
|
static |
CAN message receive pending ISR. Unblocks the CAN receive task for the bus/peripheral through a direct-to-task notification.
Registered through HAL in canlib_rx_task_init() Called when a new CAN message is received and stored in the hardware Rx FIFO
hfdcan | FDCAN peripheral triggering the message pending IRQ |
rx_its | (unused, but part of the HAL callback function signature) |
|
static |
FDCAN peripheral timestamp counter wraparound ISR. Increments the timestamp_ovf_since_last_rx field for each tracked message for the peripheral/task.
Registered through HAL in canlib_register_timeout_ids() This means for a message that's timed out, we'll get a false negative every ~2 days rather than every ~2 seconds
hfdcan | FDCAN peripheral triggering the timestamp counter wraparound IRQ |
|
static |
CAN message transmit complete ISR. Unblocks the CAN transmit task for the bus/peripheral through a direct-to-task notification if the task is waiting for free space the hardware Tx FIFO.
Registered through HAL in canlib_tx_task_init() Called when a CAN message is successfully transmitted
hfdcan | FDCAN peripheral triggering the transmit complete IRQ |
buffer_idxs | (unused, but part of the HAL callback function signature) |
|
static |
Scheduled CAN message FreeRTOS timer callback. Timer created and started in canlib_register_scheduled_msgs()
timer_hdl | FreeRTOS timer handle. The timer ID points to the canlib_scheduled_msg_params struct for this set of periodic messages |
|
static |
Gets the CAN bus associated with an FDCAN peripheral.
|
static |
Gets the FDCAN peripheral associated with a CAN bus.
|
static |
Sorts a uint32_t array in ascending order.
Used to sort CAN IDs for timeout checks during initialization RIP CAN library quicksort 2025-2025 :(
arr_ptr | Array to sort |
length | Length of array |
|
static |
Implements the CAN receive task.
task_params_ptr | Pointer to the CAN receive task parameter struct |
This task is scheduled when a CAN message is received by an enabled FDCAN peripheral. It reads pending messages from the hardware FIFO queue for the peripheral and calls th
|
static |
Implements the CAN transmit task.
task_params_ptr | Pointer to the CAN transmit task parameter struct |
This task is scheduled whenever there are pending messages in the transmit queue. It sends messages on the correct CAN bus and handles full FIFO queues and other hardware errors (bus disconnected, etc.)
|
static |
Updates the timeout timestamp for a CAN ID. Called after a message is received by the CAN receive task.
params_ptr | CAN receive task parameter struct |
msg_id | Target CAN ID |
rx_timestamp | Timestamp (captured by FDCAN peripheral at SOF, units of TIMESTAMP_PRESCALER * nominal bit time) |
|
static |
|
static |
|
static |
|
static |