Midnight Sun Firmware
Loading...
Searching...
No Matches
CAN

CAN library. More...

Collaboration diagram for CAN:

Classes

struct  CanStorage
 Storage class for the device ID and RX data. More...
 
struct  CanSettings
 CAN Settings. More...
 
union  CanId
 CAN message ID. More...
 
struct  CanMessage
 CAN message structure. More...
 
struct  CanQueue
 CAN queue data structure to store received CAN messages. More...
 
struct  CanWatchDog
 Software defined CAN watchdog. More...
 

Macros

#define CAN_TIMEOUT_MS   5U
 Maximum time permitted for a CAN transaction.
 
#define CAN_HW_DEV_INTERFACE   "vcan0"
 
#define CAN_MSG_MAX_STD_IDS   (1 << 11)
 Maximum permitted CAN ID for 11-bit identifiers.
 
#define CAN_QUEUE_SIZE   64U
 Maximum number of CAN messages permitted for the CAN queue.
 
#define can_queue_init(can_queue)
 
#define can_queue_push(can_queue, source)    queue_send(&(can_queue)->queue, (source), 0)
 
#define can_queue_push_from_isr(can_queue, source, high_prio_woken)    queue_send_from_isr(&(can_queue)->queue, (source), high_prio_woken)
 
#define can_queue_peek(can_queue, dest)    queue_peek(&(can_queue)->queue, (dest), 0)
 
#define can_queue_pop(can_queue, dest)    queue_receive(&(can_queue)->queue, (dest), 0)
 
#define can_queue_pop_from_isr(can_queue, dest, higher_prio_woken)    queue_receive_from_isr(&(can_queue)->queue, (dest), high_prio_woken)
 
#define can_queue_size(can_queue)    queue_get_num_items(&(can_queue)->queue)
 

Typedefs

typedef struct CanStorage CanStorage
 Storage class for the device ID and RX data.
 
typedef struct CanSettings CanSettings
 CAN Settings. More...
 
typedef uint32_t CanMessageId
 CAN message ID. More...
 
typedef union CanId CanId
 CAN message ID.
 
typedef struct CanMessage CanMessage
 CAN message structure. More...
 
typedef struct CanQueue CanQueue
 CAN queue data structure to store received CAN messages. More...
 
typedef struct CanWatchDog CanWatchDog
 Software defined CAN watchdog.
 

Enumerations

enum  CanHwBusStatus { CAN_HW_BUS_STATUS_OK = 0 , CAN_HW_BUS_STATUS_ERROR , CAN_HW_BUS_STATUS_OFF }
 CAN Bus status flags. More...
 
enum  CanHwBitrate {
  CAN_HW_BITRATE_125KBPS , CAN_HW_BITRATE_250KBPS , CAN_HW_BITRATE_500KBPS , CAN_HW_BITRATE_1000KBPS ,
  NUM_CAN_HW_BITRATES
}
 Selection for the supported CAN Bitrates. More...
 

Functions

StatusCode can_init (CanStorage *storage, const CanSettings *settings)
 Initialize the CAN interface. More...
 
StatusCode can_add_filter_in (CanMessageId msg_id)
 Sets a filter on the CAN interface. More...
 
StatusCode can_transmit (const CanMessage *msg)
 Transmits CAN data on the bus. More...
 
StatusCode can_receive (const CanMessage *msg)
 Receives CAN data from the bus. More...
 
StatusCode run_can_tx_all ()
 Transmit all CAN data. More...
 
StatusCode run_can_tx_fast ()
 Transmit all fast-cycle CAN data. More...
 
StatusCode run_can_tx_medium ()
 Transmit all medium-cycle CAN data. More...
 
StatusCode run_can_tx_slow ()
 Transmit all slow-cycle CAN data. More...
 
StatusCode run_can_rx_all ()
 Receive all CAN data. More...
 
StatusCode clear_rx_struct ()
 Clear the RX data struct. More...
 
StatusCode clear_tx_struct ()
 Clear the TX data struct. More...
 
StatusCode can_hw_init (const CanQueue *rx_queue, const CanSettings *settings)
 Initialize the CAN interface. More...
 
StatusCode can_hw_add_filter_in (uint32_t mask, uint32_t filter, bool extended)
 Sets a filter on the CAN interface. More...
 
CanHwBusStatus can_hw_bus_status (void)
 Fetches the CAN bus status. More...
 
StatusCode can_hw_transmit (uint32_t id, bool extended, const uint8_t *data, size_t len)
 Transmits CAN data on the bus. More...
 
bool can_hw_receive (uint32_t *id, bool *extended, uint64_t *data, size_t *len)
 Receives CAN data from the bus. More...
 
StatusCode check_all_can_watchdogs ()
 Checks all CAN watchdogs. More...
 
StatusCode check_fast_can_watchdogs ()
 Checks CAN watchdogs for messages in fast-cycle. More...
 
StatusCode check_medium_can_watchdogs ()
 Checks CAN watchdogs for messages in medium-cycle. More...
 
StatusCode check_slow_can_watchdogs ()
 Checks CAN watchdogs for messages in slow-cycle. More...
 
void clear_all_rx_received ()
 
void clear_fast_rx_received ()
 
void clear_medium_rx_received ()
 
void clear_slow_rx_received ()
 

Detailed Description

CAN library.

Macro Definition Documentation

◆ can_queue_init

#define can_queue_init (   can_queue)
Value:
({ \
Queue* queue = &(can_queue)->queue; \
queue->item_size = sizeof(CanMessage); \
queue->storage_buf = (uint8_t*) (&(can_queue)->msg_nodes); \
queue_init(&(can_queue)->queue); \
})
#define CAN_QUEUE_SIZE
Maximum number of CAN messages permitted for the CAN queue.
Definition: can_queue.h:27
CAN message structure.
Definition: can_msg.h:55
Queue storage and access struct.
Definition: queues.h:32
uint32_t num_items
Definition: queues.h:33
uint32_t item_size
Definition: queues.h:34
uint8_t * storage_buf
Definition: queues.h:35

Typedef Documentation

◆ CanMessage

typedef struct CanMessage CanMessage

CAN message structure.

CAN ID is subjected to arbitration where lower ID's are given higher priority Extended ID flag indicates a 29-bit ID or an 11-bit ID Data length code ias 4 bit value indicating the number of bytes in the payload CAN Frames hold 8 bytes of data at most

◆ CanMessageId

typedef uint32_t CanMessageId

CAN message ID.

11 Bits in standard mode 29 Bits in extended mode

◆ CanQueue

typedef struct CanQueue CanQueue

CAN queue data structure to store received CAN messages.

CAN data received is stored in the CanQueue data structure The Queue can hold a maximum of 64 CAN messages

◆ CanSettings

typedef struct CanSettings CanSettings

CAN Settings.

TX Pin will transmit data. RX pin will receive data. The bitrate is the number of bits sent per second. The device ID is the CAN ID of the STM32 node. Loopback will internally connect the transmit and receive CAN lines for testing. Silent mode is used for only listening to the bus.

Enumeration Type Documentation

◆ CanHwBitrate

Selection for the supported CAN Bitrates.

CAN bit timing is composed of multiple time segments:

  • Synchronization Segment (Sync_Seg): Allows resynchronization
  • Propagation Segment (Prop_Seg): Compensates for physical delays
  • Phase Buffer Segments (Phase_Seg1 and Phase_Seg2):
    • Absorb time quanta variations
    • Provide sampling point adjustment
      Note
      Typical sampling occurs between 75-80% of bit time Higher bitrates require shorter time quantas Bitrate selection impacts signal reliability and bus length
Enumerator
CAN_HW_BITRATE_125KBPS 

125 KBits per second

CAN_HW_BITRATE_250KBPS 

250 KBits per second

CAN_HW_BITRATE_500KBPS 

500 KBits per second

CAN_HW_BITRATE_1000KBPS 

1000 KBits per second

NUM_CAN_HW_BITRATES 

Number of supported bit rates

◆ CanHwBusStatus

CAN Bus status flags.

Enumerator
CAN_HW_BUS_STATUS_OK 

CAN Bus is operating correctly

CAN_HW_BUS_STATUS_ERROR 

CAN Bus has detected an error

CAN_HW_BUS_STATUS_OFF 

CAN Bus is not initialized

Function Documentation

◆ can_add_filter_in()

StatusCode can_add_filter_in ( CanMessageId  msg_id)

Sets a filter on the CAN interface.

Parameters
msg_idMessage ID of the message to filter
Returns
STATUS_CODE_OK if the filter is added successfully STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect STATUS_CODE_INTERNAL_ERROR if HAL initialization fails

◆ can_hw_add_filter_in()

StatusCode can_hw_add_filter_in ( uint32_t  mask,
uint32_t  filter,
bool  extended 
)

Sets a filter on the CAN interface.

The filter works as such: if INCOMING_MSG_ID & mask == filter & mask, the message is handled

Parameters
maskDetermines which bits in the received ID are considered during filtering
filterSpecifies the pattern the CAN ID must adhere to
extendedBoolean to use CAN extended ID feature
Returns
STATUS_CODE_OK if adding the filter succeeded STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect

◆ can_hw_bus_status()

CanHwBusStatus can_hw_bus_status ( void  )

Fetches the CAN bus status.

Returns
CAN_HW_BUS_STATUS_OK if bus is running CAN_HW_BUS_STATUS_ERROR if error is detected in the CAN bus CAN_HW_BUS_STATUS_OFF if bus is not initialized

◆ can_hw_init()

StatusCode can_hw_init ( const CanQueue rx_queue,
const CanSettings settings 
)

Initialize the CAN interface.

Parameters
rx_queuePointer to the CAN RX queue
settingsPointer to the CAN settings
Returns
STATUS_CODE_OK if initialization succeeded STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect STATUS_CODE_INTERNAL_ERROR if HAL initialization fails

◆ can_hw_receive()

bool can_hw_receive ( uint32_t *  id,
bool *  extended,
uint64_t *  data,
size_t *  len 
)

Receives CAN data from the bus.

Parameters
idPointer to store the CAN ID received
extendedPointer to a flag to indicate CAN extended ID feature
dataPointer to a buffer to store data
lenPointer to the number of CAN messages received
Returns
true if data is retrieved successfully false if one of the parameters are incorrect or internal error occurred

◆ can_hw_transmit()

StatusCode can_hw_transmit ( uint32_t  id,
bool  extended,
const uint8_t *  data,
size_t  len 
)

Transmits CAN data on the bus.

Parameters
idCAN message ID
extendedBoolean to use CAN extended ID feature
dataPointer to the data to transmit
lenSize of the data to transfer
Returns
STATUS_CODE_OK if data is transmitted successfully STATUS_CODE_RESOURCE_EXHAUSTED if CAN mailbox is full STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect

◆ can_init()

StatusCode can_init ( CanStorage storage,
const CanSettings settings 
)

Initialize the CAN interface.

Parameters
storagePointer to the CAN storage
settingsPointer to the CAN settings
Returns
STATUS_CODE_OK if initialization succeeded STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect STATUS_CODE_INTERNAL_ERROR if initialization fails

◆ can_receive()

StatusCode can_receive ( const CanMessage msg)

Receives CAN data from the bus.

Parameters
msgPointer to the message to update on receive
Returns
STATUS_CODE_OK if data is retrieved successfully STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect STATUS_CODE_UNINITIALIZED if the CAN bus is not initialized

◆ can_transmit()

StatusCode can_transmit ( const CanMessage msg)

Transmits CAN data on the bus.

Parameters
msgPointer to the message to transmit
Returns
STATUS_CODE_OK if data is transmitted successfully STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect STATUS_CODE_UNINITIALIZED if the CAN bus is not initialized

◆ check_all_can_watchdogs()

StatusCode check_all_can_watchdogs ( )

Checks all CAN watchdogs.

This will throw a LOG_WARN if a message has missed its watchdog

Returns
STATUS_CODE_OK if all watchdogs are checked succesfully

◆ check_fast_can_watchdogs()

StatusCode check_fast_can_watchdogs ( )

Checks CAN watchdogs for messages in fast-cycle.

This will throw a LOG_WARN if a message has missed its watchdog

Returns
STATUS_CODE_OK if all watchdogs are checked succesfully

◆ check_medium_can_watchdogs()

StatusCode check_medium_can_watchdogs ( )

Checks CAN watchdogs for messages in medium-cycle.

This will throw a LOG_WARN if a message has missed its watchdog

Returns
STATUS_CODE_OK if all watchdogs are checked succesfully

◆ check_slow_can_watchdogs()

StatusCode check_slow_can_watchdogs ( )

Checks CAN watchdogs for messages in slow-cycle.

This will throw a LOG_WARN if a message has missed its watchdog

Returns
STATUS_CODE_OK if all watchdogs are checked succesfully

◆ clear_rx_struct()

StatusCode clear_rx_struct ( )

Clear the RX data struct.

Returns
STATUS_CODE_OK if initialization succeeded STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect STATUS_CODE_INTERNAL_ERROR if HAL initialization fails

◆ clear_tx_struct()

StatusCode clear_tx_struct ( )

Clear the TX data struct.

Returns
STATUS_CODE_OK if initialization succeeded STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect STATUS_CODE_INTERNAL_ERROR if HAL initialization fails

◆ run_can_rx_all()

StatusCode run_can_rx_all ( )

Receive all CAN data.

Returns
STATUS_CODE_OK if the cache is cleared successfully STATUS_CODE_INTERNAL_ERROR if HAL initialization fails

◆ run_can_tx_all()

StatusCode run_can_tx_all ( )

Transmit all CAN data.

Returns
STATUS_CODE_OK if the cache is cleared successfully STATUS_CODE_INTERNAL_ERROR if HAL initialization fails

◆ run_can_tx_fast()

StatusCode run_can_tx_fast ( )

Transmit all fast-cycle CAN data.

Returns
STATUS_CODE_OK if the cache is cleared successfully STATUS_CODE_INTERNAL_ERROR if HAL initialization fails

◆ run_can_tx_medium()

StatusCode run_can_tx_medium ( )

Transmit all medium-cycle CAN data.

Returns
STATUS_CODE_OK if the cache is cleared successfully STATUS_CODE_INTERNAL_ERROR if HAL initialization fails

◆ run_can_tx_slow()

StatusCode run_can_tx_slow ( )

Transmit all slow-cycle CAN data.

Returns
STATUS_CODE_OK if the cache is cleared successfully STATUS_CODE_INTERNAL_ERROR if HAL initialization fails