Skip to content

File CAN.h

FileList > bsp > Inc > CAN.h

Go to the source code of this file

CAN peripheral driver for LHRS embedded systems. More...

  • #include "stm32xx_hal.h"

Public Types

Type Name
enum can_status_t

Public Functions

Type Name
can_status_t can_deinit (CAN_HandleTypeDef * handle)
Deinitializes the CAN peripheral.
can_status_t can_init (CAN_HandleTypeDef * handle, CAN_FilterTypeDef * filter)
Initializes the CAN peripheral.
can_status_t can_recv (CAN_HandleTypeDef * handle, uint16_t id, CAN_RxHeaderTypeDef * header, uint8_t data, TickType_t delay_ticks)
Receives a CAN message.
can_status_t can_send (CAN_HandleTypeDef * handle, const CAN_TxHeaderTypeDef * header, const uint8_t data, TickType_t delay_ticks)
Sends a CAN message.
can_status_t can_start (CAN_HandleTypeDef * handle)
Starts the CAN peripheral.
can_status_t can_stop (CAN_HandleTypeDef * handle)
Stops the CAN peripheral.

Detailed Description

This driver provides initialization, deinitialization, start/stop, sending, and receiving functionality for CAN peripherals.

Function Descriptions:

  • Init: Initializes the CAN peripheral given the configuration (does not start the CAN peripheral).
  • DeInit: Deinitializes the CAN peripheral; resets configuration to default values.
  • Start: Starts the CAN peripheral, enabling interrupts and message handling.
  • Stop: Stops the CAN peripheral; configuration is unchanged, but interrupts and message handling are disabled.
  • Send: Prepares a message to be sent when the CAN peripheral is ready (puts it into a queue).
  • Recv: Reads a message from the queue depending on ID.

How to Use This Driver:

  • Init to initialize the CAN peripheral.
  • Start to activate the CAN peripheral.
  • Use Send, Recv, Stop, and Start as needed.
  • DeInit to deinitialize the CAN peripheral.

Usage Notes:

  • CAN2 is usually a slave CAN, sharing transistors with CAN1. Ensure CAN1 is initialized before CAN2.
  • CAN2 often shares filter banks with CAN1; carefully consider filter configurations passed to can_init.
  • CAN3 is usually a master CAN; no sharing considerations are needed.
  • This driver uses a macro to define which messages a CAN interface should receive. See can1_recv_entries.h for examples. Ensure entries align with filter configurations.

Public Types Documentation

enum can_status_t

enum can_status_t {
    CAN_ERR,
    CAN_OK,
    CAN_SENT,
    CAN_RECV,
    CAN_EMPTY
};

Public Functions Documentation

function can_deinit

Deinitializes the CAN peripheral.

can_status_t can_deinit (
    CAN_HandleTypeDef * handle
) 

This function deinitializes the HAL CAN driver and disables CAN interrupts.

Parameters:

  • handle Pointer to the CAN handle structure.

Returns:

can_status_t Returns CAN_OK on success, CAN_ERR on failure.


function can_init

Initializes the CAN peripheral.

can_status_t can_init (
    CAN_HandleTypeDef * handle,
    CAN_FilterTypeDef * filter
) 

This function initializes the CAN peripheral, sets up send/receive queues, configures the HAL CAN driver, applies the filter configuration, and enables CAN interrupts.

Parameters:

  • handle Pointer to the CAN handle structure.
  • filter Pointer to the CAN filter configuration structure.

Returns:

can_status_t Returns CAN_OK on success, CAN_ERR on failure.


function can_recv

Receives a CAN message.

can_status_t can_recv (
    CAN_HandleTypeDef * handle,
    uint16_t id,
    CAN_RxHeaderTypeDef * header,
    uint8_t data,
    TickType_t delay_ticks
) 

Reads a message from the receive queue corresponding to the specified ID. Supports blocking or non-blocking behavior depending on delay_ticks.

Parameters:

  • handle Pointer to the CAN handle structure.
  • id CAN identifier of the message to receive.
  • header Pointer to a CAN_RxHeaderTypeDef structure to store the received header.
  • data Array to store the received data.
  • delay_ticks Maximum delay to wait if queue is empty (FreeRTOS ticks).

Returns:

can_status_t Returns CAN_RECV if a message was received, CAN_EMPTY if the queue was empty, CAN_ERR on failure or invalid ID.


function can_send

Sends a CAN message.

can_status_t can_send (
    CAN_HandleTypeDef * handle,
    const CAN_TxHeaderTypeDef * header,
    const uint8_t data,
    TickType_t delay_ticks
) 

Places a CAN message into the transmit mailbox if available, otherwise queues it in the send queue for later transmission.

Parameters:

  • handle Pointer to the CAN handle structure.
  • header Pointer to the CAN transmit header structure.
  • data Array containing the data to send.
  • delay_ticks Maximum delay to wait if queue is full (FreeRTOS ticks).

Returns:

can_status_t Returns CAN_SENT if message was successfully sent or queued, CAN_ERR on failure.


function can_start

Starts the CAN peripheral.

can_status_t can_start (
    CAN_HandleTypeDef * handle
) 

Activates the CAN peripheral so it can transmit and receive messages.

Parameters:

  • handle Pointer to the CAN handle structure.

Returns:

can_status_t Returns CAN_OK on success, CAN_ERR on failure.


function can_stop

Stops the CAN peripheral.

can_status_t can_stop (
    CAN_HandleTypeDef * handle
) 

Stops the CAN peripheral; the configuration is preserved, but the peripheral will not process messages or trigger interrupts.

Parameters:

  • handle Pointer to the CAN handle structure.

Returns:

can_status_t Returns CAN_OK on success, CAN_ERR on failure.



The documentation for this class was generated from the following file bsp/Inc/CAN.h