libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
fdcan_common.c File Reference
#include <libopencm3/stm32/fdcan.h>
#include <libopencm3/stm32/rcc.h>
#include <stddef.h>
Include dependency graph for fdcan_common.c:

Go to the source code of this file.

Functions

int fdcan_cccr_init_cfg (uint32_t canport, bool set, uint32_t timeout)
 Routine implementing FDCAN_CCCR's INIT bit manipulation. More...
 
static int fdcan_get_free_txbuf (uint32_t canport)
 Return ID of next free Tx buffer. More...
 
static void fdcan_get_fill_rxfifo (uint32_t canport, uint8_t fifo_id, unsigned *get_index, unsigned *pending_frames)
 Returns fill state and next available get index from receive FIFO. More...
 
struct fdcan_standard_filterfdcan_get_flssa_addr (uint32_t canport)
 Returns standard filter start address in message RAM. More...
 
struct fdcan_extended_filterfdcan_get_flesa_addr (uint32_t canport)
 Returns extended filter start address in message RAM. More...
 
struct fdcan_rx_fifo_elementfdcan_get_rxfifo_addr (uint32_t canport, unsigned fifo_id, unsigned element_id)
 Returns a pointer to an RX FIFO element in message RAM. More...
 
struct fdcan_tx_event_elementfdcan_get_txevt_addr (uint32_t canport)
 Returns transmit event start address in message RAM. More...
 
struct fdcan_tx_buffer_elementfdcan_get_txbuf_addr (uint32_t canport, unsigned element_id)
 Returns a pointer to an TX FIFO element in message RAM. More...
 
uint32_t fdcan_length_to_dlc (uint8_t length)
 Converts frame length to DLC value. More...
 
uint8_t fdcan_dlc_to_length (uint32_t dlc)
 Converts DLC value into frame payload length. More...
 
int fdcan_init (uint32_t canport, uint32_t timeout)
 Put FDCAN block into INIT mode for setup. More...
 
void fdcan_set_can (uint32_t canport, bool auto_retry_disable, bool rx_fifo_locked, bool tx_queue_mode, bool silent, uint32_t n_sjw, uint32_t n_ts1, uint32_t n_ts2, uint32_t n_br_presc)
 Set essential FDCAN block parameters for plain CAN operation. More...
 
void fdcan_set_fdcan (uint32_t canport, bool brs_enable, bool fd_op_enable, uint32_t f_sjw, uint32_t f_ts1, uint32_t f_ts2, uint32_t f_br_presc)
 Set FDCAN block parameters for FDCAN transmission. More...
 
void fdcan_set_test (uint32_t canport, bool testing, bool loopback)
 Set FDCAN block testing features. More...
 
int fdcan_get_init_state (uint32_t canport)
 Return current FDCAN block operation state. More...
 
void fdcan_set_std_filter (uint32_t canport, uint32_t nr, uint8_t id_list_mode, uint32_t id1, uint32_t id2, uint8_t action)
 Configure filter rule for standard ID frames. More...
 
void fdcan_set_ext_filter (uint32_t canport, uint32_t nr, uint8_t id_list_mode, uint32_t id1, uint32_t id2, uint8_t action)
 Configure filter rule for extended ID frames. More...
 
int fdcan_transmit (uint32_t canport, uint32_t id, bool ext, bool rtr, bool fdcan_fmt, bool btr_switch, uint8_t length, const uint8_t *data)
 Transmit Message using FDCAN. More...
 
int fdcan_receive (uint32_t canport, uint8_t fifo_id, bool release, uint32_t *id, bool *ext, bool *rtr, uint8_t *fmi, uint8_t *length, uint8_t *data, uint16_t *timestamp)
 Receive Message from FDCAN FIFO. More...
 
void fdcan_release_fifo (uint32_t canport, uint8_t fifo_id)
 Release receive oldest FIFO entry. More...
 
void fdcan_enable_irq (uint32_t canport, uint32_t irq)
 Enable IRQ from FDCAN block. More...
 
void fdcan_disable_irq (uint32_t canport, uint32_t irq)
 Disable IRQ from FDCAN block. More...
 
bool fdcan_available_tx (uint32_t canport)
 Check if there is free transmit buffer. More...
 
bool fdcan_available_rx (uint32_t canport, uint8_t fifo)
 Tell if there is message waiting in receive FIFO. More...
 

Function Documentation

◆ fdcan_cccr_init_cfg()

int fdcan_cccr_init_cfg ( uint32_t  canport,
bool  set,
uint32_t  timeout 
)

Routine implementing FDCAN_CCCR's INIT bit manipulation.

This routine will change INIT bit and wait for it to actually change its value. If change won't happen before timeout, error is signalized. If INIT bit already has value which should be set, this function will return immediately.

Parameters
[in]canportFDCAN block base address. See FDCAN block base addresses.
[in]setnew value of INIT, true means set
[in]timeoutAmount of busyloop cycles, function will wait for FDCAN to switch it's state. If set to 0, then function returns immediately.
Returns
FDCAN_E_OK on success, FDCAN_E_TIMEOUT if INIT bit value didn't change before timeout has expired.

Definition at line 41 of file fdcan_common.c.

References FDCAN_CCCR, FDCAN_CCCR_INIT, FDCAN_E_OK, and FDCAN_E_TIMEOUT.

Referenced by fdcan_init(), and fdcan_start().

Here is the caller graph for this function:

◆ fdcan_dlc_to_length()

uint8_t fdcan_dlc_to_length ( uint32_t  dlc)

Converts DLC value into frame payload length.

Works for both CAN and FDCAN DLC values.

Parameters
[in]dlcDLC value
Returns
data payload length in bytes

Definition at line 221 of file fdcan_common.c.

Referenced by fdcan_get_fifo_element_size(), fdcan_get_txbuf_element_size(), and fdcan_receive().

Here is the caller graph for this function:

◆ fdcan_get_fill_rxfifo()

static void fdcan_get_fill_rxfifo ( uint32_t  canport,
uint8_t  fifo_id,
unsigned *  get_index,
unsigned *  pending_frames 
)
static

Returns fill state and next available get index from receive FIFO.

Examines FDCAN receive FIFO and returns fill status of FIFO and ID of next message available for reading. If fill status is 0 (FIFO is empty), then get index is undefined.

Parameters
[in]canportFDCAN block base address. See FDCAN block base addresses.
[in]fifo_idID of fifo queried (currently 0 or 1)
[out]get_indexAddress of buffer where next get index will be stored
[out]pending_framesAddress of buffer where amount of pending frame will be stored.

Definition at line 109 of file fdcan_common.c.

References FDCAN_RXFIFO_FL_MASK, FDCAN_RXFIFO_FL_SHIFT, FDCAN_RXFIFO_GI_MASK, FDCAN_RXFIFO_GI_SHIFT, and FDCAN_RXFIS.

Referenced by fdcan_receive().

Here is the caller graph for this function:

◆ fdcan_get_flesa_addr()

struct fdcan_extended_filter * fdcan_get_flesa_addr ( uint32_t  canport)

Returns extended filter start address in message RAM.

Parameters
[in]canportFDCAN block base address. See FDCAN block base addresses.
Returns
Base address of extended filter configuration block.

Definition at line 136 of file fdcan_common.c.

References CAN_MSG_BASE, and FDCAN_LFESA_OFFSET.

Referenced by fdcan_init_filter(), and fdcan_set_ext_filter().

Here is the caller graph for this function:

◆ fdcan_get_flssa_addr()

struct fdcan_standard_filter * fdcan_get_flssa_addr ( uint32_t  canport)

Returns standard filter start address in message RAM.

Parameters
[in]canportFDCAN block base address. See FDCAN block base addresses.
Returns
Base address of standard filter configuration block.

Definition at line 124 of file fdcan_common.c.

References CAN_MSG_BASE, and FDCAN_LFSSA_OFFSET.

Referenced by fdcan_init_filter(), and fdcan_set_std_filter().

Here is the caller graph for this function:

◆ fdcan_get_free_txbuf()

static int fdcan_get_free_txbuf ( uint32_t  canport)
static

Return ID of next free Tx buffer.

Examines transmit buffer allocation in message RAM and returns ID of buffer, which is free.

Parameters
[in]canportFDCAN block base address. See FDCAN block base addresses.
Returns
Non-negative number ID of Tx buffer which is free, or FDCAN_E_BUSY if no Tx buffer is available

Definition at line 85 of file fdcan_common.c.

References FDCAN_E_BUSY, FDCAN_TXBRP, FDCAN_TXBRP_TRP0, FDCAN_TXBRP_TRP1, and FDCAN_TXBRP_TRP2.

Referenced by fdcan_available_tx(), and fdcan_transmit().

Here is the caller graph for this function:

◆ fdcan_get_rxfifo_addr()

struct fdcan_rx_fifo_element * fdcan_get_rxfifo_addr ( uint32_t  canport,
unsigned  fifo_id,
unsigned  element_id 
)

Returns a pointer to an RX FIFO element in message RAM.

Parameters
[in]canportFDCAN block base address. See FDCAN block base addresses.
[in]fifo_idID of FIFO whose address is requested
[in]element_idthe element number in the fifo we're requesting
Returns
a pointer to the individual element in the message ram

Definition at line 150 of file fdcan_common.c.

References CAN_MSG_BASE, fdcan_get_fifo_element_size(), and FDCAN_RXFIFO_OFFSET.

Referenced by fdcan_receive().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fdcan_get_txbuf_addr()

struct fdcan_tx_buffer_element * fdcan_get_txbuf_addr ( uint32_t  canport,
unsigned  element_id 
)

Returns a pointer to an TX FIFO element in message RAM.

Parameters
[in]canportFDCAN block base address. See FDCAN block base addresses.
[in]element_idthe element number in the fifo we're requesting
Returns
a pointer to the individual element in the message ram

Definition at line 178 of file fdcan_common.c.

References CAN_MSG_BASE, fdcan_get_txbuf_element_size(), and FDCAN_TXBUF_OFFSET.

Referenced by fdcan_transmit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fdcan_get_txevt_addr()

struct fdcan_tx_event_element * fdcan_get_txevt_addr ( uint32_t  canport)

Returns transmit event start address in message RAM.

Parameters
[in]canportFDCAN block base address. See FDCAN block base addresses.
Returns
Base address of transmit event block.

Definition at line 165 of file fdcan_common.c.

References CAN_MSG_BASE, and FDCAN_TXEVT_OFFSET.

◆ fdcan_length_to_dlc()

uint32_t fdcan_length_to_dlc ( uint8_t  length)

Converts frame length to DLC value.

Works for both CAN and FDCAN frame lengths. If length is invalid value, then returns 0xFF.

Parameters
[in]lengthintended frame payload length in bytes
Returns
DLC value representing lengths or 0xFF if length cannot be encoded into DLC format (applies only to FDCAN frame lengths)

Definition at line 197 of file fdcan_common.c.

Referenced by fdcan_set_rx_element_size(), fdcan_set_tx_element_size(), and fdcan_transmit().

Here is the caller graph for this function: