AtMega CAN Library
Well documented CAN library for AtMega processors utilizing the original Atmel driver
Macros | Enumerations | Functions
can_drv.h File Reference

This file contains the prototypes and the macros of the low level functions (drivers) of: More...

#include "canlibraryconfiguration.h"
#include <stdint.h>
#include <avr/io.h>

Go to the source code of this file.

Macros

#define max(a, b)
 
#define CAN_AUTOBAUD   0
 
#define Can_bit_timing(mode)   (can_auto_baudrate(mode) )
 < Fclkio = 16 MHz, Tclkio = 62.5 ns More...
 
#define ERR_GEN_MSK   ((1<<SERG)|(1<<CERG)|(1<<FERG)|(1<<AERG))
 
#define INT_GEN_MSK   ((1<<BOFFIT)|(1<<BXOK)|(ERR_GEN_MSK))
 
#define BRP_MSK   ((1<<BRP5)|(1<<BRP4)|(1<<BRP3)|(1<<BRP2)|(1<<BRP1)|(1<<BRP0))
 
#define SJW_MSK   ((1<<SJW1)|(1<<SJW0))
 
#define PRS_MSK   ((1<<PRS2)|(1<<PRS1)|(1<<PRS0))
 
#define PHS2_MSK   ((1<<PHS22)|(1<<PHS21)|(1<<PHS20))
 
#define PHS1_MSK   ((1<<PHS12)|(1<<PHS11)|(1<<PHS10))
 
#define BRP   (BRP0)
 
#define SJW   (SJW0)
 
#define PRS   (PRS0)
 
#define PHS2   (PHS20)
 
#define PHS1   (PHS10)
 
#define HPMOB_MSK   ((1<<HPMOB3)|(1<<HPMOB2)|(1<<HPMOB1)|(1<<HPMOB0))
 
#define MOBNB_MSK   ((1<<MOBNB3)|(1<<MOBNB2)|(1<<MOBNB1)|(1<<MOBNB0))
 
#define ERR_MOB_MSK   ((1<<BERR)|(1<<SERR)|(1<<CERR)|(1<<FERR)|(1<<AERR))
 
#define INT_MOB_MSK   ((1<<TXOK)|(1<<RXOK)|(1<<BERR)|(1<<SERR)|(1<<CERR)|(1<<FERR)|(1<<AERR))
 
#define CONMOB_MSK   ((1<<CONMOB1)|(1<<CONMOB0))
 
#define DLC_MSK   ((1<<DLC3)|(1<<DLC2)|(1<<DLC1)|(1<<DLC0))
 
#define CONMOB   (CONMOB0)
 
#define DLC   (DLC0)
 
#define BRP_MIN   1
 
#define BRP_MAX   64
 
#define NTQ_MIN   8
 
#define NTQ_MAX   25
 
#define PRS_MIN   1
 True segment values in TQ.
 
#define PRS_MAX   8
 
#define PHS1_MIN   2
 
#define PHS1_MAX   8
 
#define PHS2_MIN   2
 
#define PHS2_MAX   8
 
#define SJW_MIN   1
 
#define SJW_MAX   4
 
#define NB_DATA_MAX   8
 
#define LAST_MOB_NB   (NB_MOB-1)
 
#define NO_MOB   0xFF
 
#define STATUS_CLEARED   0x00
 
#define MOB_NOT_COMPLETED   0x00
 
#define MOB_TX_COMPLETED   (1<<TXOK)
 
#define MOB_RX_COMPLETED   (1<<RXOK)
 
#define MOB_RX_COMPLETED_DLCW   ((1<<RXOK)|(1<<DLCW))
 
#define MOB_ACK_ERROR   (1<<AERR)
 
#define MOB_FORM_ERROR   (1<<FERR)
 
#define MOB_CRC_ERROR   (1<<CERR)
 
#define MOB_STUFF_ERROR   (1<<SERR)
 
#define MOB_BIT_ERROR   (1<<BERR)
 
#define MOB_PENDING   ((1<<RXOK)|(1<<TXOK))
 
#define MOB_NOT_REACHED   ((1<<AERR)|(1<<FERR)|(1<<CERR)|(1<<SERR)|(1<<BERR))
 
#define MOB_DISABLE   0xFF
 
#define MOB_Tx_ENA   1
 
#define MOB_Rx_ENA   2
 
#define MOB_Rx_BENA   3
 
#define Can_reset()   ( CANGCON = (1<<SWRES) )
 
#define Can_enable()   ( CANGCON |= (1<<ENASTB))
 
#define Can_disable()   ( CANGCON &= ~(1<<ENASTB))
 
#define Can_full_abort()   { CANGCON |= (1<<ABRQ); CANGCON &= ~(1<<ABRQ); }
 
#define Can_conf_bt()   { CANBT1=CONF_CANBT1; CANBT2=CONF_CANBT2; CANBT3=CONF_CANBT3; }
 
#define Can_set_mob(mob)   { CANPAGE = ((mob) << 4);}
 
#define Can_set_mask_mob()   { CANIDM4=0xFF; CANIDM3=0xFF; CANIDM2=0xFF; CANIDM1=0xFF; }
 
#define Can_clear_mask_mob()   { CANIDM4=0x00; CANIDM3=0x00; CANIDM2=0x00; CANIDM1=0x00; }
 
#define Can_clear_status_mob()   { CANSTMOB=0x00; }
 
#define Can_clear_mob()   { uint8_t volatile *__i_; for (__i_=&CANSTMOB; __i_<&CANSTML; __i_++) { *__i_=0x00 ;}}
 
#define Can_mob_abort()   ( DISABLE_MOB )
 
#define Can_set_dlc(dlc)   ( CANCDMOB |= (dlc) )
 
#define Can_set_ide()   ( CANCDMOB |= (1<<IDE) )
 
#define Can_set_rtr()   ( CANIDT4 |= (1<<RTRTAG) )
 
#define Can_set_rplv()   ( CANCDMOB |= (1<<RPLV) )
 
#define Can_clear_dlc()   ( CANCDMOB &= ~DLC_MSK )
 
#define Can_clear_ide()   ( CANCDMOB &= ~(1<<IDE) )
 
#define Can_clear_rtr()   ( CANIDT4 &= ~(1<<RTRTAG) )
 
#define Can_clear_rplv()   ( CANCDMOB &= ~(1<<RPLV) )
 
#define DISABLE_MOB   ( CANCDMOB &= (~CONMOB_MSK) )
 
#define Can_config_tx()   { DISABLE_MOB; CANCDMOB |= (MOB_Tx_ENA << CONMOB); }
 
#define Can_config_rx()   { DISABLE_MOB; CANCDMOB |= (MOB_Rx_ENA << CONMOB); }
 
#define Can_config_rx_buffer()   { CANCDMOB |= (MOB_Rx_BENA << CONMOB); }
 
#define Can_get_dlc()   ((CANCDMOB & DLC_MSK) >> DLC )
 
#define Can_get_ide()   ((CANCDMOB & (1<<IDE)) >> IDE )
 
#define Can_get_rtr()   ((CANIDT4 & (1<<RTRTAG)) >> RTRTAG)
 
#define Can_set_rtrmsk()   ( CANIDM4 |= (1<<RTRMSK) )
 
#define Can_set_idemsk()   ( CANIDM4 |= (1<<IDEMSK) )
 
#define Can_clear_rtrmsk()   ( CANIDM4 &= ~(1<<RTRMSK) )
 
#define Can_clear_idemsk()   ( CANIDM4 &= ~(1<<IDEMSK) )
 
#define Can_get_std_id(identifier)
 < STD ID TAG Reading More...
 
#define Can_get_ext_id(identifier)
 < EXT ID TAG Reading More...
 
#define CAN_SET_STD_ID_10_4(identifier)   (((*((uint8_t *)(&(identifier))+1))<<5)+((* (uint8_t *)(&(identifier)))>>3))
 < STD ID Construction
 
#define CAN_SET_STD_ID_3_0(identifier)   (( * (uint8_t *)(&(identifier)) )<<5)
 
#define Can_set_std_id(identifier)
 < STD ID TAG writing More...
 
#define Can_set_std_msk(mask)
 < STD ID MASK writing More...
 
#define CAN_SET_EXT_ID_28_21(identifier)   (((*((uint8_t *)(&(identifier))+3))<<3)+((*((uint8_t *)(&(identifier))+2))>>5))
 < EXT ID Construction
 
#define CAN_SET_EXT_ID_20_13(identifier)   (((*((uint8_t *)(&(identifier))+2))<<3)+((*((uint8_t *)(&(identifier))+1))>>5))
 
#define CAN_SET_EXT_ID_12_5(identifier)   (((*((uint8_t *)(&(identifier))+1))<<3)+((* (uint8_t *)(&(identifier)) )>>5))
 
#define CAN_SET_EXT_ID_4_0( identifier)   ((* (uint8_t *)(&(identifier)) )<<3)
 
#define Can_set_ext_id(identifier)
 < EXT ID TAG writing More...
 
#define Can_set_ext_msk(mask)
 < EXT ID MASK writing More...
 

Enumerations

enum  can_mob_t {
  MOB_0, MOB_1, MOB_2, MOB_3,
  MOB_4, MOB_5, MOB_6, MOB_7,
  MOB_8, MOB_9, MOB_10, MOB_11,
  MOB_12, MOB_13, MOB_14
}
 

Functions

void can_clear_all_mob (void)
 
uint8_t can_get_mob_free (void)
 
uint8_t can_get_mob_status (void)
 
void can_get_data (uint8_t *p_can_message_data)
 
uint8_t can_auto_baudrate (uint8_t eval)
 
uint8_t can_fixed_baudrate (uint8_t eval)
 

Detailed Description

This file contains the prototypes and the macros of the low level functions (drivers) of:

Copyright (c) 2007 Atmel.

Use of this program is subject to Atmel's End User License Agreement. Please read file license.txt for copyright notice.

        - CAN (Controller Array Network)
        - for AT90CAN128/64/32.

This file can be parsed by Doxygen for automatic documentation generation. This file has been validated with AVRStudio-413528/WinAVR-20070122.

Version
Revision
3.20
Name
jtellier
Todo:
Bug:

Macro Definition Documentation

◆ Can_bit_timing

#define Can_bit_timing (   mode)    (can_auto_baudrate(mode) )

< Fclkio = 16 MHz, Tclkio = 62.5 ns

< Fclkio = 12 MHz, Tclkio = 83.333 ns < Fclkio = 8 MHz, Tclkio = 125 ns The first action of "can_bit_timing()" is to reset the CAN controller "can_bit_timing()" lets the CAN controller disable "can_bit_timing()" returns 1 if the setting of CANBTx registers is available and returns 0 if the setting of CANBTx registers is wrong

◆ Can_get_ext_id

#define Can_get_ext_id (   identifier)
Value:
{ *((uint8_t *)(&(identifier))+3) = CANIDT1>>3 ; \
*((uint8_t *)(&(identifier))+2) = (CANIDT2>>3)+(CANIDT1<<5); \
*((uint8_t *)(&(identifier))+1) = (CANIDT3>>3)+(CANIDT2<<5); \
*((uint8_t *)(&(identifier)) ) = (CANIDT4>>3)+(CANIDT3<<5); }

< EXT ID TAG Reading

◆ Can_get_std_id

#define Can_get_std_id (   identifier)
Value:
{ *((uint8_t *)(&(identifier))+1) = CANIDT1>>5 ; \
*((uint8_t *)(&(identifier)) ) = (CANIDT2>>5)+(CANIDT1<<3); }

< STD ID TAG Reading

◆ Can_set_ext_id

#define Can_set_ext_id (   identifier)
Value:
{ CANIDT1 = CAN_SET_EXT_ID_28_21(identifier); \
CANIDT2 = CAN_SET_EXT_ID_20_13(identifier); \
CANIDT3 = CAN_SET_EXT_ID_12_5( identifier); \
CANIDT4 = CAN_SET_EXT_ID_4_0( identifier); \
CANCDMOB |= (1<<IDE); }
#define CAN_SET_EXT_ID_28_21(identifier)
< EXT ID Construction
Definition: can_drv.h:306

< EXT ID TAG writing

◆ Can_set_ext_msk

#define Can_set_ext_msk (   mask)
Value:
{ CANIDM1 = CAN_SET_EXT_ID_28_21(mask); \
CANIDM2 = CAN_SET_EXT_ID_20_13(mask); \
CANIDM3 = CAN_SET_EXT_ID_12_5( mask); \
CANIDM4 = CAN_SET_EXT_ID_4_0( mask); }
#define CAN_SET_EXT_ID_28_21(identifier)
< EXT ID Construction
Definition: can_drv.h:306

< EXT ID MASK writing

◆ Can_set_std_id

#define Can_set_std_id (   identifier)
Value:
{ CANIDT1 = CAN_SET_STD_ID_10_4(identifier); \
CANIDT2 = CAN_SET_STD_ID_3_0( identifier); \
CANCDMOB &= (~(1<<IDE)) ; }
#define CAN_SET_STD_ID_10_4(identifier)
< STD ID Construction
Definition: can_drv.h:293

< STD ID TAG writing

◆ Can_set_std_msk

#define Can_set_std_msk (   mask)
Value:
{ CANIDM1 = CAN_SET_STD_ID_10_4(mask); \
CANIDM2 = CAN_SET_STD_ID_3_0( mask); }
#define CAN_SET_STD_ID_10_4(identifier)
< STD ID Construction
Definition: can_drv.h:293

< STD ID MASK writing

◆ max

#define max (   a,
 
)
Value:
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })

Function Documentation

◆ can_auto_baudrate()

uint8_t can_auto_baudrate ( uint8_t  mode)

This function programs itself the CANBTx registers if there is some communication (activity) on the CAN bus.

Warning
complex function not yet implemented
Parameters
Evaluationneeded ==0: start the evaluation from faster baudrate ==1: (re)start an evaluation with CANBTx registers contents
Returns
Baudrate Status ==0: research of bit timing configuration failed ==1: baudrate performed

Temporary variable

Bit timing segment variables

Computing needed

Keys for "while()" loops

Key for configurate CAN

Timer overflow count

Count of bit timing configuration tried

Return flag

— Default setting

— Init segment variables with MIN values if mode=0 or init segment variables with CANBTx if mode=1

Try this configuration

Enable "while (wait_for_rx ..." loop

mode = 1

To enter in "while (evaluate ..." loop

Look for the next configuration

Skip "while (wait_for_rx ..." loop

— Clear all MOb's (CANMSG not cleared)

Page index

All MOb Registers = 0x00

— CANBTx registers update (sjw = phs2/2, 3 sample points)

— Set CAN-Timer - Used for time-out There are 641 (0x281) possible evaluations. The first one provides the faster the faster bit timing, the last one gives the slower. It is necessary to modulate the time-out versus bit timing (0x281>>3=0x50, matching an U8).

— MOb configuration

Use MOb-0

Reset MOb status (undone by "Can_reset()")

MOb 0 in receive mode

CAN controller configuration

Enable CAN controller in "listen" mode

Wait for Enable OK

Reset General errors and OVRTIM flag

— WAIT_FOR_RX LOOP:

Try to perform a CAN message reception in "LISTEN" mode without error and before a time_out done by CAN-Timer. Else gives the hand to "EVALUATE LOOP" to have a new set of bit timing.

— RxOK received ?

— It is the successful output of "can_auto_baudrate" function

Out of "while (wait_for_rx ..." loop

Will skip "while (evaluate ..." loop

Out of "while (bt_not_found ..." loop

Return flag = TRUE

Disable MOb-0

Disable CAN controller & reset "listen" mode

Wait for Disable OK

— Else stop if any errors

— MOb error ?

Will enter in "while (evaluate ..." loop

Out of "while (wait_for_rx ..." loop

— Time_out reached ?

— First Time_out

— Second Time_out

Will enter in "while (evaluate ..." loop

Out of "while (wait_for_rx ..." loop

— General error ?

Will enter in "while (evaluate ..." loop

Out of "while (wait_for_rx ..." loop

Try this configuration

— EVALUATE LOOP:

Compute a new bit timing configuration. First, Phase 1 is increased, then Phase2=Phase1 and if Phase1>5, Phase1 can be equal to Phase2 or Phase2+1. After this, the number of TQ is increased up to its high limit and after it is the Prescaler. During the computing high (80%) and low (75%) limits of sampling point location are tested. SJW and the number of sampling points are not calculated in this loop.

— It is the failing of "can_auto_baudrate" function

Out of "while (evaluate ..." loop

Return flag = FALSE

Out of "while (bt_not_found ..." loop

Disable MOb-0

Disable CAN controller & reset "listen" mode

Wait for Disable OK

— If psh1 > 5 then phs1 =phs2 or =phs2+1, else phs1=phs2

— Test PRS limits

— Values accepted if 80% >= sampling point >= 75%

Out of "while (evaluate ..." loop &

new "while (bt_not_found ..." loop

◆ can_clear_all_mob()

void can_clear_all_mob ( void  )

This function clears the Mailbox content. It uses Can_clear_mob() macro and clears data FIFO o MOb[0] upto MOb[LAST_MOB_NB].

Warning
none.
Parameters
none.
Returns
none.

This function clears the Mailbox content. It reset CANSTMOB, CANCDMOB, CANIDTx & CANIDMx and clears data FIFO of MOb[0] upto MOb[LAST_MOB_NB].

Warning
: This version doesn't clears the data FIFO
Parameters
none
Returns
none

Page index

All MOb Registers=0

◆ can_fixed_baudrate()

uint8_t can_fixed_baudrate ( uint8_t  mode)

This function programs the CANBTx registers with the predefined values CONF_CANBT1, CONF_CANBT2, CONF_CANBT3.

Warning
Parameters
(unused!)
Returns
Baudrate Status fixed = 1: baudrate performed

◆ can_get_data()

void can_get_data ( uint8_t *  p_can_message_data)

This function copy the data from the selected MOb to the address passed as parameter.

Warning
none.
Parameters
CANmessage data address.
Returns
none.

◆ can_get_mob_free()

uint8_t can_get_mob_free ( void  )

This function returns the number of the first MOb available or 0xFF if no MOb available.

Warning
none.
Parameters
none.
Returns
Handle of MOb.
  • MOb[0] upto MOb[LAST_MOB_NB]
  • 0xFF if no MOb

This function returns the number of the first MOb available or 0xFF if no MOb is available.

Warning
none.
Parameters
none.
Returns
Handle of MOb.
  • MOb[0] upto MOb[LAST_MOB_NB]
  • 0xFF if no MOb

Disable configuration

◆ can_get_mob_status()

uint8_t can_get_mob_status ( void  )

This function returns information "MOB completed its job" if one of the RXOK or TXOK Flag is set or "MOB not completed its job if no RXOK and TXOK flags are set. Previously, this function checks if the MOb is configured or not and in case of the MOB not configured, the function returns "MOB_DISABLE".

Warning
none.
Parameters
none.
Returns
MOb Status.
  • MOB_NOT_COMPLETED
  • MOB_TX_COMPLETED
  • MOB_RX_COMPLETED
  • MOB_RX_DLC_WARNING
  • MOB_DISABLE or should be a combination of the following errors
  • MOB_ACK_ERROR
  • MOB_FORM_ERROR
  • MOB_CRC_ERROR
  • MOB_STUFF_ERROR
  • MOB_BIT_ERROR