Microchip® Advanced Software Framework

can.c File Reference

Controller Area Network (CAN) driver module for SAM.

Copyright (c) 2011-2018 Microchip Technology Inc. and its subsidiaries.

#include "can.h"

Data Structures

struct  can_bit_timing_t
 

Macros

#define CAN_BAUDRATE_MAX_DIV   128
 The max value for CAN baudrate prescale. More...
 
#define CAN_BIT_IPT   2
 
#define CAN_BIT_SYNC   1
 Define the fixed bit time value. More...
 
#define CAN_MAX_TQ_NUM   25
 
#define CAN_MIN_TQ_NUM   8
 Define the scope for TQ. More...
 
#define CAN_TIMEOUT   100000
 
#define TIMEMARK_MASK   0x0000ffff
 Define the timemark mask. More...
 

Functions

void can_disable (Can *p_can)
 Disable CAN Controller. More...
 
void can_disable_autobaud_listen_mode (Can *p_can)
 Disable CAN Controller autobaud/listen mode. More...
 
void can_disable_interrupt (Can *p_can, uint32_t dw_mask)
 Disable CAN interrupt. More...
 
void can_disable_low_power_mode (Can *p_can)
 Disable CAN Controller low power mode. More...
 
void can_disable_overload_frame (Can *p_can)
 CAN Controller won't generate overload frame. More...
 
void can_disable_time_triggered_mode (Can *p_can)
 Disable CAN Controller time triggered mode. More...
 
void can_disable_timer_freeze (Can *p_can)
 Disable CAN Controller timer freeze. More...
 
void can_disable_tx_repeat (Can *p_can)
 Disable CAN Controller transmit repeat function. More...
 
void can_enable (Can *p_can)
 Enable CAN Controller. More...
 
void can_enable_autobaud_listen_mode (Can *p_can)
 Enable CAN Controller autobaud/listen mode. More...
 
void can_enable_interrupt (Can *p_can, uint32_t dw_mask)
 Enable CAN interrupt. More...
 
void can_enable_low_power_mode (Can *p_can)
 Enable CAN Controller low power mode. More...
 
void can_enable_overload_frame (Can *p_can)
 CAN Controller will generate an overload frame after each successful reception for mailboxes configured in Receive mode, Producer and Consumer. More...
 
void can_enable_time_triggered_mode (Can *p_can)
 Enable CAN Controller time triggered mode. More...
 
void can_enable_timer_freeze (Can *p_can)
 Enable CAN Controller timer freeze. More...
 
void can_enable_tx_repeat (Can *p_can)
 Enable CAN Controller transmit repeat function. More...
 
uint32_t can_get_internal_timer_value (Can *p_can)
 Get the 16-bit free-running internal timer count. More...
 
uint32_t can_get_interrupt_mask (Can *p_can)
 Get CAN Interrupt Mask. More...
 
uint8_t can_get_rx_error_cnt (Can *p_can)
 Get CAN receive error counter. More...
 
uint32_t can_get_status (Can *p_can)
 Get CAN status. More...
 
uint32_t can_get_timestamp_value (Can *p_can)
 Get CAN timestamp register value. More...
 
uint8_t can_get_tx_error_cnt (Can *p_can)
 Get CAN transmit error counter. More...
 
void can_global_send_abort_cmd (Can *p_can, uint8_t uc_mask)
 Send global abort request. More...
 
void can_global_send_transfer_cmd (Can *p_can, uint8_t uc_mask)
 Send global transfer request. More...
 
uint32_t can_init (Can *p_can, uint32_t ul_mck, uint32_t ul_baudrate)
 Initialize CAN controller. More...
 
uint32_t can_mailbox_get_status (Can *p_can, uint8_t uc_index)
 Get status of the mailbox. More...
 
void can_mailbox_init (Can *p_can, can_mb_conf_t *p_mailbox)
 Initialize the mailbox in different mode and set up related configuration. More...
 
uint32_t can_mailbox_read (Can *p_can, can_mb_conf_t *p_mailbox)
 Read receive information for the mailbox. More...
 
void can_mailbox_send_abort_cmd (Can *p_can, can_mb_conf_t *p_mailbox)
 Send single mailbox abort request. More...
 
void can_mailbox_send_transfer_cmd (Can *p_can, can_mb_conf_t *p_mailbox)
 Send single mailbox transfer request. More...
 
void can_mailbox_set_timemark (Can *p_can, uint8_t uc_index, uint16_t us_cnt)
 Configure the timemark for the mailbox. More...
 
uint32_t can_mailbox_tx_remote_frame (Can *p_can, can_mb_conf_t *p_mailbox)
 Require to send out a remote frame. More...
 
uint32_t can_mailbox_write (Can *p_can, can_mb_conf_t *p_mailbox)
 Prepare transmit information and write them into the mailbox. More...
 
void can_reset_all_mailbox (Can *p_can)
 Reset the eight mailboxes. More...
 
void can_reset_internal_timer (Can *p_can)
 Reset the internal free-running 16-bit timer. More...
 
static uint32_t can_set_baudrate (Can *p_can, uint32_t ul_mck, uint32_t ul_baudrate)
 Configure CAN baudrate. More...
 
void can_set_rx_sync_stage (Can *p_can, uint32_t ul_stage)
 Configure CAN Controller reception synchronization stage. More...
 
void can_set_timestamp_capture_point (Can *p_can, uint32_t ul_flag)
 Configure the timestamp capture point, at the start or the end of frame. More...
 

Variables

const can_bit_timing_t can_bit_time []
 Values of bit time register for different baudrates, Sample point = ((1 + uc_prog + uc_phase1) / uc_tq) * 100%. More...
 

#define CAN_BAUDRATE_MAX_DIV   128

The max value for CAN baudrate prescale.

Referenced by can_set_baudrate().

#define CAN_BIT_IPT   2
#define CAN_BIT_SYNC   1

Define the fixed bit time value.

#define CAN_MAX_TQ_NUM   25

Referenced by can_set_baudrate().

#define CAN_MIN_TQ_NUM   8

Define the scope for TQ.

Referenced by can_set_baudrate().

#define CAN_TIMEOUT   100000

Referenced by can_init().

#define TIMEMARK_MASK   0x0000ffff

Define the timemark mask.

Referenced by can_mailbox_set_timemark().

static uint32_t can_set_baudrate ( Can *  p_can,
uint32_t  ul_mck,
uint32_t  ul_baudrate 
)
static

Configure CAN baudrate.

Parameters
p_canPointer to a CAN peripheral instance.
ul_mckThe input main clock for the CAN module.
ul_baudrateBaudrate value (kB/s), allowed values: 1000, 800, 500, 250, 125, 50, 25, 10, 5.
Return values
Setthe baudrate successfully or not.

References CAN_BAUDRATE_MAX_DIV, can_disable(), CAN_MAX_TQ_NUM, CAN_MIN_TQ_NUM, can_bit_timing_t::uc_phase1, can_bit_timing_t::uc_phase2, can_bit_timing_t::uc_prog, and can_bit_timing_t::uc_sjw.

Referenced by can_init().

const can_bit_timing_t can_bit_time[]
Initial value:
= {
{ 8, (2 + 1), (1 + 1), (1 + 1), (2 + 1), 75},
{ 9, (1 + 1), (2 + 1), (2 + 1), (1 + 1), 67},
{10, (2 + 1), (2 + 1), (2 + 1), (2 + 1), 70},
{11, (3 + 1), (2 + 1), (2 + 1), (3 + 1), 72},
{12, (2 + 1), (3 + 1), (3 + 1), (3 + 1), 67},
{13, (3 + 1), (3 + 1), (3 + 1), (3 + 1), 77},
{14, (3 + 1), (3 + 1), (4 + 1), (3 + 1), 64},
{15, (3 + 1), (4 + 1), (4 + 1), (3 + 1), 67},
{16, (4 + 1), (4 + 1), (4 + 1), (3 + 1), 69},
{17, (5 + 1), (4 + 1), (4 + 1), (3 + 1), 71},
{18, (4 + 1), (5 + 1), (5 + 1), (3 + 1), 67},
{19, (5 + 1), (5 + 1), (5 + 1), (3 + 1), 68},
{20, (6 + 1), (5 + 1), (5 + 1), (3 + 1), 70},
{21, (7 + 1), (5 + 1), (5 + 1), (3 + 1), 71},
{22, (6 + 1), (6 + 1), (6 + 1), (3 + 1), 68},
{23, (7 + 1), (7 + 1), (6 + 1), (3 + 1), 70},
{24, (6 + 1), (7 + 1), (7 + 1), (3 + 1), 67},
{25, (7 + 1), (7 + 1), (7 + 1), (3 + 1), 68}
}

Values of bit time register for different baudrates, Sample point = ((1 + uc_prog + uc_phase1) / uc_tq) * 100%.