AtMega CAN Library
Well documented CAN library for AtMega processors utilizing the original Atmel driver
Functions
can_drv.c File Reference

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

#include "can_drv.h"

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 mode)
 
uint8_t can_fixed_baudrate (uint8_t mode)
 

Detailed Description

This file contains 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:

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 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 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