mikroSDK Reference Manual

CAN Driver API Reference. More...

Functions list

void can_configure_default (can_config_t *config)
 Configure CAN Driver configuration structure. More...
 
void can_filter_configure_default (can_filter_config_t *filter_config)
 Configure CAN Driver filter configuration structure. More...
 
err_t can_open (can_t *obj, can_config_t *config)
 Open the CAN driver object. More...
 
err_t can_init (can_t *obj)
 Initialize CAN module. More...
 
err_t can_set_filter (can_t *obj, can_filter_config_t *filter_config)
 Initialize CAN module filter. More...
 
err_t can_set_frequency (can_t *obj, uint32_t frequency)
 Set CAN module frequency. More...
 
err_t can_get_frequency (can_t *obj)
 Get CAN module frequency. More...
 
err_t can_set_mode (can_t *obj, can_mode_t mode)
 Set CAN module mode. More...
 
err_t can_get_mode (can_t *obj)
 Get CAN module mode. More...
 
err_t can_transmit (can_t *obj, can_transmit_message_struct *transmit_message)
 Transmit data to the first free TX mailbox. More...
 
void can_transmission_stop (can_t *obj)
 Stop transmit operation. More...
 
err_t can_receive (can_t *obj, can_receive_message_struct *receive_message)
 Get data from the RX FIFO. More...
 
err_t can_close (can_t *obj)
 Close CAN driver object. More...
 

Detailed Description

The CAN driver provides standard CAN functionality.

Function Documentation

◆ can_configure_default()

void can_configure_default ( can_config_t config)

Configures can_config_t structure to default initialization values. Take into consideration that this is just structure variable initial values setting. Values need to be redefined by user.

Parameters
[in,out]configconfigure CAN driver configuration structure. See can_config_t structure definition for detailed explanation.

Default values:

Function Default value
Tx pin 0xFFFFFFFF (invalid pin)
Rx pin 0xFFFFFFFF (invalid pin)
Mode CAN_MODE_NORMAL
Frequency 500000
Returns
Nothing.

Example

// CAN driver configuration structure.
static can_config_t can_cfg;
// Fill structure with default values.

◆ can_filter_configure_default()

void can_filter_configure_default ( can_filter_config_t filter_config)

Configures can_filter_config_t structure to default initialization values. Take into consideration that this is just structure variable initial values setting. Values need to be redefined by user.

Parameters
[in,out]filter_configconfigure CAN driver filter configuration structure. See can_filter_config_t structure definition for detailed explanation.

Default values:

Function Default value
ID high 0x0000
ID low 0x0000
ID mask high 0x0000
ID mask low 0x0000
Filter mode CAN_FILTER_MODE_IDMASK
Filter scale CAN_FILTER_SCALE_16BIT
Filter enable CAN_FILTER_DISABLE
Filter FIFO CAN_FILTER_FIFO0
Filter bank 0
Returns
Nothing.

Example

// CAN driver configuration structure.
static can_filter_config_t can_filter_cfg;
// Fill structure with default values.
can_filter_configure_default( &can_filter_cfg );

◆ can_open()

err_t can_open ( can_t obj,
can_config_t config 
)

Opens the CAN driver object on selected pins. Allocates memory and pins for specified object.

Parameters
[in,out]objCAN driver object. See can_t structure definition for detailed explanation.
[in]configCAN driver configuration settings. See can_config_t structure definition for detailed explanation.
Returns
The function can return one of the values defined by can_err_t, which is size dependant on the architecture.
Precondition
Make sure that config structure has been adequately populated beforehand. See can_configure_default definition for detailed explanation.
Note
It is recommended to check return value for error.

Example

// CAN driver context structure.
static can_t can;
// CAN driver configuration structure.
static can_config_t can_cfg;
// Specify desired TX pin.
can_cfg.tx_pin = CAN_TX;
// Specify desired RX pin.
can_cfg.rx_pin = CAN_RX;
// Allocate resources for CAN module.
if ( can_open( &can, &can_cfg ) == CAN_ERROR )
{
// Error handling strategy.
}

◆ can_init()

err_t can_init ( can_t obj)

Sets CAN module parameters.

Parameters
[in]objCAN driver object. See can_t structure definition for detailed explanation.
Returns
The function can return one of the values defined by can_err_t, which is size dependant on the architecture.
Precondition
Before calling this function, the user is expected to call can_open function.
Postcondition
This function initializes CAN module and sets adequate parameters.
Note
It is recommended to check return value for error.

Example

// CAN driver context structure.
static can_t can;
// Call the function.
if ( can_init( &can ) == CAN_ERROR )
{
// Error handling strategy.
}

◆ can_set_filter()

err_t can_set_filter ( can_t obj,
can_filter_config_t filter_config 
)

Sets CAN module filter parameters.

Parameters
[in]objCAN driver object. See can_t structure definition for detailed explanation.
[in]filter_configCAN filter config structure. See can_filter_config_t structure definition for detailed explanation.
Returns
The function can return one of the values defined by can_err_t, which is size dependant on the architecture.
Precondition
Make sure that can_filter_config_t structure has been adequately filled out.
Postcondition
This function initializes CAN filter and sets adequate parameters.
Note
It is recommended to check return value for error.

Example

// CAN driver context structure.
static can_t can;
// CAN driver filter config structure.
static can_filter_config_t can_filter_cfg;
// Configure CAN filter.
if ( can_set_filter( &can, &can_filter_cfg ) == CAN_ERROR )
{
// Error handling strategy.
}

◆ can_set_frequency()

err_t can_set_frequency ( can_t obj,
uint32_t  frequency 
)

Sets CAN module frequency to frequency value if possible. Frequency must be set to exact amount as requested, otherwise error is returned.

Parameters
[in]objCAN driver object. See can_t structure definition for detailed explanation.
[in]frequencySpecified CAN transmission rate.
Returns
The function can return one of the values defined by can_err_t, which is size dependant on the architecture.
Precondition
Make sure that adequate memory has been allocated beforehand. See can_open definition for detailed explanation.
Note
It is recommended to check return value for error.

Example

// CAN driver context structure.
static can_t can;
// Set transmission rate.
if ( can_set_frequency( &can, 500000 ) == CAN_ERROR )
{
// Error handling strategy.
}

◆ can_get_frequency()

err_t can_get_frequency ( can_t obj)

Gets the currently configured CAN frequency.

Parameters
[in]objCAN driver object. See can_t structure definition for detailed explanation.
Returns
The function can return the CAN_ERROR value defined by can_err_t, which is size dependant on the architecture, or the actual frequency.
Precondition
Make sure that adequate memory has been allocated beforehand. See can_open definition for detailed explanation.

Example

// CAN driver context structure.
static can_t can;
// Get transmission rate.
int32_t frequency = can_get_frequency( &can );

◆ can_set_mode()

err_t can_set_mode ( can_t obj,
can_mode_t  mode 
)

Sets CAN module mode to mode if possible.

Parameters
[in]objCAN driver object. See can_t structure definition for detailed explanation.
[in]modeSpecified CAN mode. See can_mode_t definition for detailed explanation.
Returns
The function can return one of the values defined by can_err_t, which is size dependant on the architecture.
Precondition
Make sure that adequate memory has been allocated beforehand. See can_open definition for detailed explanation.
Note
It is recommended to check return value for error.

Example

// CAN driver context structure.
static can_t can;
// Set mode.
{
// Error handling strategy.
}

◆ can_get_mode()

err_t can_get_mode ( can_t obj)

Gets the currently configured CAN mode.

Parameters
[in]objCAN driver object. See can_t structure definition for detailed explanation.
Returns
The function can return the CAN_ERROR value defined by can_err_t, which is size dependant on the architecture, or the actual mode.
Precondition
Make sure that adequate memory has been allocated beforehand. See can_open definition for detailed explanation.

Example

// CAN driver context structure.
static can_t can;
// Get mode.
can_mode_t mode = can_get_mode( &can );

◆ can_transmit()

err_t can_transmit ( can_t obj,
can_transmit_message_struct transmit_message 
)

Function sets parameters from the transmit message structure in the appropriate registers and then sends out the data.

Parameters
[in]objCAN driver object. See can_t structure definition for detailed explanation.
[in]transmit_messageStructure containing necessary information about the message being transmitted. See #can_message_struct structure definition for detailed explanation.
Returns
The function can return one of the values defined by can_err_t, which is size dependant on the architecture.
Precondition
Make sure that adequate memory has been allocated beforehand, and the module has been initialized to adequate CAN frequency. See can_open definition and can_set_frequency for detailed explanation.
Note
It is recommended to check return value for error.

Example

// CAN driver context structure.
static can_t can;
// CAN transmit structure.
// Set the ID type.
tx_message.tx_id_type = CAN_FRAME_FORMAT_STANDARD_11BITS;
// Set a unique standard ID.
tx_message.tx_std_id = 0x321;
// Set the length of the message in bytes.
tx_message.tx_data_len = 3;
// Set the data.
tx_message.tx_data[0] = 0xAA;
tx_message.tx_data[1] = 0xBB;
tx_message.tx_data[2] = 0xCC;
if ( can_transmit( &can, &tx_message ) == CAN_ERROR )
{
// Error handling strategy.
}

◆ can_transmission_stop()

void can_transmission_stop ( can_t obj)

Function accesses hardware transmit registers, finds the transmit mailbox that has message transmission request in progress and stops data transmission.

Parameters
[in]objCAN driver object.
Returns
void None.
Precondition
Make sure that there is transmission in progress. See can_transmit definition for detailed explanation.

Example

// CAN HAL context structure.
static hal_can_t hal_can;
// Stop transmission.
hal_can_transmission_stop( &hal_can );

◆ can_receive()

err_t can_receive ( can_t obj,
can_receive_message_struct receive_message 
)

Function stores previously transmitted data into the rx_data in receive_message structure.

Parameters
[in]objCAN driver object. See can_t structure definition for detailed explanation.
[in]receive_messageStructure designed to store received information and data retrieved from the CAN registers. See #can_message_struct structure definition for detailed explanation.
Returns
The function can return one of the values defined by can_err_t, which is size dependant on the architecture.
Precondition
Make sure that adequate memory has been allocated beforehand, and the module has been initialized to adequate CAN frequency. See can_open definition and can_set_frequency for detailed explanation.
Note
It is recommended to check return value for error.

Example

// CAN driver context structure.
static can_t can;
// CAN receive structure.
if ( can_receive( &can, &rx_message ) == CAN_ERROR )
{
// Error handling strategy.
}

◆ can_close()

err_t can_close ( can_t obj)

De-allocates hardware resources for specific driver object and de-initializes the module on a hardware level, resets pin AF to default values, clears all buffers used by object and disables module clock for lower power consumption.

Parameters
[in,out]objCAN driver object. See can_t structure definition for detailed explanation.
Returns
The function can return one of the values defined by can_err_t, which is size dependant on the architecture.

Example

// CAN driver context structure.
static can_t can;
// Close the CAN module object handler.
if ( CAN_SUCCESS == can_close( &can ) ) {
// No error
} else {
// Handle the error.
}
can_get_frequency
err_t can_get_frequency(can_t *obj)
Get CAN module frequency.
can_set_mode
err_t can_set_mode(can_t *obj, can_mode_t mode)
Set CAN module mode.
can_t
CAN driver context structure.
Definition: drv_can.h:337
can_mode_t
can_mode_t
Definition: drv_can.h:65
CAN_MODE_NORMAL
Definition: drv_can.h:68
can_filter_configure_default
void can_filter_configure_default(can_filter_config_t *filter_config)
Configure CAN Driver filter configuration structure.
can_configure_default
void can_configure_default(can_config_t *config)
Configure CAN Driver configuration structure.
can_set_frequency
err_t can_set_frequency(can_t *obj, uint32_t frequency)
Set CAN module frequency.
can_open
err_t can_open(can_t *obj, can_config_t *config)
Open the CAN driver object.
can_config_t::rx_pin
pin_name_t rx_pin
Definition: drv_can.h:322
can_config_t
CAN init configuration structure.
Definition: drv_can.h:319
can_config_t::tx_pin
pin_name_t tx_pin
Definition: drv_can.h:321
can_get_mode
err_t can_get_mode(can_t *obj)
Get CAN module mode.
can_set_filter
err_t can_set_filter(can_t *obj, can_filter_config_t *filter_config)
Initialize CAN module filter.
can_transmit_message_struct
CAN driver transmit message structure.
Definition: drv_can.h:381
CAN_ERROR
Definition: drv_can.h:59
can_receive_message_struct
CAN driver receive message structure.
Definition: drv_can.h:397
CAN_SUCCESS
Definition: drv_can.h:58
can_receive
err_t can_receive(can_t *obj, can_receive_message_struct *receive_message)
Get data from the RX FIFO.
CAN_FRAME_FORMAT_STANDARD_11BITS
Definition: drv_can.h:101
can_filter_config_t
CAN filter init configuration structure.
Definition: drv_can.h:270
can_init
err_t can_init(can_t *obj)
Initialize CAN module.
can_transmit
err_t can_transmit(can_t *obj, can_transmit_message_struct *transmit_message)
Transmit data to the first free TX mailbox.
can_close
err_t can_close(can_t *obj)
Close CAN driver object.