MSS_CAN_init

Function MSS_CAN_init 

Source
pub unsafe extern "C" fn MSS_CAN_init(
    this_can: *mut mss_can_instance_t,
    bitrate: u32,
    pcan_config: pmss_can_config_reg,
    basic_can_rx_mb: u8,
    basic_can_tx_mb: u8,
) -> u8
Expand description

The MSS_CAN_init() function initializes the CAN driver as well as the CAN controller. The basic_can_rx_mb and basic_can_tx_mb are used to configure the number of receive and transmit mailboxes in basic CAN operation. This function configures the CAN channel speed as per the “bitrate” parameter. It initializes all receive mailboxes and make it ready for configuration. This is the first function to be called before using any other function.

@param this_can The this_can parameter is a pointer to the mss_can_instance_t structure.

@param bitrate The bitRate parameter is used to configure CAN speed. The following standard preset definitions are provided for systems with a PCLK1 of 8MHz, 16MHz or 32MHz: +—————––+––––––––––+––––––––––+ | 8MHz PCLK1 | 16MHz PCLK1 | 32MHz PCLK1 | +—————––+––––––––––+––––––––––+ | CAN_SPEED_8M_5K | CAN_SPEED_16M_5K | CAN_SPEED_32M_5K | | CAN_SPEED_8M_10K | CAN_SPEED_16M_10K | CAN_SPEED_32M_10K | | CAN_SPEED_8M_20K | CAN_SPEED_16M_20K | CAN_SPEED_32M_20K | | CAN_SPEED_8M_50K | CAN_SPEED_16M_50K | CAN_SPEED_32M_50K | | CAN_SPEED_8M_100K | CAN_SPEED_16M_100K | CAN_SPEED_32M_100K | | CAN_SPEED_8M_125K | CAN_SPEED_16M_125K | CAN_SPEED_32M_125K | | CAN_SPEED_8M_250K | CAN_SPEED_16M_250K | CAN_SPEED_32M_250K | | CAN_SPEED_8M_500K | CAN_SPEED_16M_500K | CAN_SPEED_32M_500K | | CAN_SPEED_8M_1M | CAN_SPEED_16M_1M | CAN_SPEED_32M_1M | +—————––+––––––––––+––––––––––+

For custom settings, use CAN_SPEED_MANUAL and configure the settings via pcan_config.

The default configurations can be altered by the addition of 0 or more of the following:

  • CAN_AUTO_RESTART
  • CAN_ARB_FIXED_PRIO
  • CAN_LITTLE_ENDIAN

@param pcan_config The pcan_config parameter is a pointer to a mss_can_config_reg structure. This structure is only used when bitrate is configured as CAN_SPEED_MANUAL.

When populating the mss_can_config_reg structure, the following should be noted:

  1. CFG_BITRATE defines the length of a CAN time quantum in terms of PCLK1 with 0 = 1 PCLK1, 1 = 2 PCLK1s and so on.
  2. A CAN bit time is made up of between 8 and 25 time quanta and the bit rate is PCLK1 / ((CFG_BITRATE + 1) * number of time quanta per bit).
  3. There is a fixed overhead of 1 time quantum for synchronization at the start of every CAN bit and the remaining time quanta in the bit are allocated with CFG_TSEG1 and CFG_TSEG2.
  4. CFG_TSEG1 can have a value between 2 and 15 which represents between 3 and 16 time quanta.
  5. If SAMPLING_MODE is 0, CFG_TSEG2 can have a value between 1 and 7 which represents between 2 and 8 time quanta and if SAMPLING_MODE is 1, CFG_TSEG2 can have a value between 2 and 7 which represents between 3 and 8 time quanta.
  6. Receive sampling takes place at the end of the segment defined by CFG_TSEG1.

For example, if CFG_TSEG1 = 3 and CFG_TSEG2 = 2 we get:

|<———— 1 CAN bit time (8 time quanta)————>| /——+——+——+——+——+——+——+——
-+ Synch | CFG_TSEG1 + 1 | CFG_TSEG2 + 1 +- -—–+——+——+——+——+——+——+——/ | Receiver samples date here –>|

@param basic_can_rx_mb The basic_can_rx_mb parameter is the number of receive mailboxes used in basic CAN mode.

@param basic_can_tx_mb The basic_can_tx_mb parameter is the number of transmit mailboxes used in basic CAN mode.

@return This function returns CAN_OK on successful execution, otherwise it will returns following error codes:

ConstantsDescription
CAN_ERRIndicates error condition
CAN_TSEG1_TOO_SMALLValue provided to configure TSEG1 is too
small
CAN_TSEG2_TOO_SMALLValue provided to configure TSEG2 is too
small
CAN_SJW_TOO_BIGValue provided to configure synchronous jump
width (SJW) is too big.

Example 1: Using a default set for bitrate, tseg1, tseg2, and sjw and additional configuration parameters. @code mss_can_instance_t g_mss_can_0_lo; int e51(void) { MSS_CAN_init(&g_mss_can_0_lo, (CAN_SPEED_16M_500K | CAN_AUTO_RESTART |
CAN_LITTLE_ENDIAN),(pmss_can_config_reg)0,16u,7u);

return(0); } @endcode

Example 2: Using custom settings for bitrate, tseg1, tseg2, and sjw. @code mss_can_instance_t g_mss_can_0_lo;

#define SYSTEM_CLOCK 8000000 #define BITS_PER_SECOND 10000

int e51(void) { mss_can_config_reg canreg;

canreg.CFG_BITRATE = (SYSTEM_CLOCK / (BITS_PER_SECOND * 8) - 1; canreg.CFG_TSEG1 = 4; canreg.CFG_TSEG2 = 1; canreg.AUTO_RESTART = 0; canreg.CFG_SJW = 0; canreg.SAMPLING_MODE = 0; canreg.EDGE_MODE = 0; canreg.ENDIAN = 1;

MSS_CAN_init(&g_mss_can_0_lo,CAN_SPEED_MANUAL,&canreg,8,4);

return(0); } @endcode