SLAAEJ4 January 2024 MSPM0G3507
This subsystem demonstrates how to build a CAN-I2C bridge. CAN-I2C bridge allows a device to send/receive information on one interface and receive/send the information on the other interface Download the code for this example.Two example codes are provided to support I2C to work in controller mode or target mode respectively.
Figure 1-1 shows a functional diagram of this subsystem. Please note that one line is added for IO interrupt to implement message transmission from I2C target to I2C controller.
This application requires CANFD and I2C.
Sub-block Functionality | Peripheral Use | Notes |
---|---|---|
CAN interface | (1x) CANFD | Called MCAN0_INST in code |
I2C interface | (1x) I2C | Called I2C_INST in code |
Based on the requirements in Table 1-1, this example is compatible with the devices in Table 1-2. The corresponding EVM can be used for prototyping.
Compatible Devices | EVM |
---|---|
MSPM0G35xx | LP-MSPM0G3507 |
/**
* @brief Structure for MCAN Rx Buffer element.
*/
typedef struct {
/*! Identifier */
uint32_t id;
/*! Remote Transmission Request
* 0 = Received frame is a data frame
* 1 = Received frame is a remote frame
*/
uint32_t rtr;
/*! Extended Identifier
* 0 = 11-bit standard identifier
* 1 = 29-bit extended identifier
*/
uint32_t xtd;
/*! Error State Indicator
* 0 = Transmitting node is error active
* 1 = Transmitting node is error passive
*/
uint32_t esi;
/*! Rx Timestamp */
uint32_t rxts;
/*! Data Length Code
* 0-8 = CAN + CAN FD: received frame has 0-8 data bytes
* 9-15 = CAN: received frame has 8 data bytes
* 9-15 = CAN FD: received frame has 12/16/20/24/32/48/64 data bytes
*/
uint32_t dlc;
/*! Bit Rat Switching
* 0 = Frame received without bit rate switching
* 1 = Frame received with bit rate switching
*/
uint32_t brs;
/*! FD Format
* 0 = Standard frame format
* 1 = CAN FD frame format (new DLC-coding and CRC)
*/
uint32_t fdf;
/*! Filter Index */
uint32_t fidx;
/*! Accepted Non-matching Frame
* 0 = Received frame matching filter index FIDX
* 1 = Received frame did not match any Rx filter element
*/
uint32_t anmf;
/*! Data bytes.
* Only first dlc number of bytes are valid.
*/
uint16_t data[DL_MCAN_MAX_PAYLOAD_BYTES];
} DL_MCAN_RxBufElement;
Header | Address | Data Length | Data |
---|---|---|---|
0x55 0xAA | 4 bytes | 1 byte | (Data Length) bytes |
Figure 1-3 shows the code flow diagram for CAN-I2C bridge which explains how the messages received in one interface and sent in the other interface. The CAN-I2C bridge can be divided into four independent tasks: receive from I2C, receive from CAN, transmit through CAN, transmit through I2C. Two FIFOs implement bidirectional message transfer and message caching.
Note that I2C is a communication method that I2C controller control the transmit and receive. In general, I2C target cannot initiate communication. For I2C target-to-controller communication, I2C target can pull down the IO when messages needed to be sent, as shown in Figure 1-3. I2C controller can initiate I2C read command in IO interrupt when IO is detected low, as shown in Figure 1-4. In this demo, I2C can be configured as I2C target or controller.
This application makes use of TI System Configuration Tool (SysConfig) graphical interface to generate the configuration code for the CAN and I2C. Using a graphical interface to configure the device peripherals streamlines the application prototyping process.
The code for what is described in Figure 1-3 can be found in the files from example code as shown in Figure 1-5.
The following code snippet shows where to modify the interface function. Functions in table are categorized into different files. Functions for I2C receive and transmit are included in bridge_i2c.c and bridge_i2c.h. Functions for CAN receive and transmit are included in bridge_can.c and bridge_can.h. Structure of FIFO element is defined in user_define.h.
Users can easily separate functions by file. For example, if only I2C functions are needed, users can reserve bridge_i2c.c and bridge_i2c.h to call the functions.
See the MSPM0 SDK and DriverLib documentation for the basic configuration of peripherals.
Tasks | Functions | Description | Location |
---|---|---|---|
I2C receive | readI2CRxMsg_controller() | Send a read request to slave (I2C master only) | bridge_i2c.c bridge_i2c.h |
getI2CRxMsg_controller() | Get the received I2C message (I2C master only) | ||
getI2CRxMsg_target() | Get the received I2C message (I2C slave only) | ||
processI2cRxMsg() | Convert the received I2C message format and store it into gI2C_RX_Element | ||
I2C transmit | processI2cTxMsg() | Convert the gI2C_TX_Element format to be sent through I2C | |
sendI2CTxMsg_controller() | Send message through I2C (I2C master only) | ||
sendI2CTxMsg_target() | Send message through I2C (I2C slave only) | ||
CAN receive | getCANRxMsg() | Get the received CAN message |
bridge_can.c bridge_can.h |
processCANRxMsg() | Convert the received CAN message format and store the message into gCAN_RX_Element | ||
CAN transmit | processCANTxMsg() | Convert the gCAN_TX_Element format to be sent through CAN | |
sendCANTxMsg() | Send message through CAN |
Custom_Element is the structure defined in user_define.h. Custom_Element is used as the structure of FIFO element, output element of I2C/CAN transmit and input element of I2C/CAN receive. Users can modify the structure according to the need.
typedef struct {
/*! Identifier */
uint32_t id;
/*! Data Length Code*/
uint32_t dlc;
/*! Data bytes*/
uint16_t data[64];
} Custom_Element;
For FIFO, there are 2 global variables used as FIFO. 6 global variables are used to trace the FIFO.
Custom_Element ItoC_FIFO[ItoC_FIFO_SIZE];
Custom_Element C2I_FIFO[C2I_FIFO_SIZE];
uint16_t ItoC_in = 0;
uint16_t ItoC_out = 0;
uint16_t ItoC_count = 0;
uint16_t C2I_in = 0;
uint16_t C2I_out = 0;
uint16_t C2I_count = 0;
By using CAN analyzer, users can send and receive messages on the CAN side. As a demonstration, two launchpads can be used as two CAN-I2C bridges (one I2C master and one I2C slave) to form a loop. When the CAN analyzer sends CAN messages through master launchpad, the analyzer can receive CAN messages from the slave launchpad.