SLAAEN4 March   2025 MSPM0G1106 , MSPM0G1107 , MSPM0G1506 , MSPM0G1507 , MSPM0G1518 , MSPM0G1519 , MSPM0G3106 , MSPM0G3106-Q1 , MSPM0G3107 , MSPM0G3107-Q1 , MSPM0G3506 , MSPM0G3506-Q1 , MSPM0G3507 , MSPM0G3507-Q1 , MSPM0G3518 , MSPM0G3518-Q1 , MSPM0G3519 , MSPM0G3519-Q1

 

  1.   1
  2.   Abstract
  3.   Trademarks
  4. 1Introduction
    1. 1.1 Bridge Between CAN and UART
  5. 2Implementation
    1. 2.1 Principle
    2. 2.2 Structure
  6. 3Software Description
    1. 3.1 Software Functionality
    2. 3.2 Configurable Parameters
    3. 3.3 Structure of Custom Element
    4. 3.4 Structure of FIFO
    5. 3.5 UART Receive and Transmit (Transparent Transmission)
    6. 3.6 UART Receive and Transmit (Protocol Transmission)
    7. 3.7 CAN Receive and Transmit
    8. 3.8 Application Integration
  7. 4Hardware
  8. 5Application Aspects
    1. 5.1 Flexible structure
    2. 5.2 Optional Configuration for CAN
    3. 5.3 CAN Bus Multi-Node Communication Example
  9. 6Summary
  10. 7References

Structure

The structure of CAN-UART bridge with protocol transmission can be seen in Figure 2-3. The CAN-UART bridge can be divided into four independent tasks: receive from UART, receive from CAN, transmit through CAN, transmit through UART. Two FIFOs implement bidirectional message transfer and message caching.

Figure 2-4 shows the structure of a CAN-UART bridge with transparent transmission. A timer interrupt is added to detect the timeout as the end of one packet.

Both UART and CAN reception are set to interrupt trigger so that messages can be received in time. When entering an interrupt, the message is first fetched through getXXXRxMsg().

For CAN, the CAN frame is a fixed format. MSPM0 supports classic CAN or CANFD. The frame for CANFD is shown in Figure 2-2. The example in this article can define 0, one, and four bytes of additional ID in the data area for protocol transmission.

 CAN FD Frame Figure 2-2 CAN FD Frame
Table 2-1 CAN Packet Form
ID Area Data
Protocol Transmission 4/1/0 bytes (Data Length) Bytes

For UART protocol transmission, messages are identified based on serial frame information. The UART message format is listed in UART Packet Form.

Table 2-2 UART Packet Form
Header ID Area Data Length Data
Protocol Transmission 0×55 0×AA 4/1/0 bytes 1 byte (Data Length) bytes
Transparent Transmission (Data Length) bytes

The header is a fixed hex number combined 0x55 0xAA, which means the start of the group. ID area occupies four bytes by default to match CAN ID, which can be configured as one byte or the ID area does not exist. The data length area occupies one byte. After Data Length area, a certain length of data is followed. This format is provided as an example. Users can modify the format according to application requirements.

For UART transparent transmission, messages are identified when timeout occurs. All bytes are regarded as pure data. The default value is the load for packet information. (For example, ID).

After receiving the message, processXXXRxMsg() converts the format of the message and stores the message in the FIFO as a new element. The format of the FIFO element is shown in Figure 2-5. In the format of the FIFO element, there are There are four categories in the FIFO element: Origin_ID, Destination ID, Data Length and Data. Users can also change the message items according to application requirements. In addition, this scheme also checks whether the FIFO is full for overload control. Users can add overload control actions according to application requirements.

Both CAN and UART transmissions are performed in the main function. When it is detected that the FIFO is not empty, the FIFO element is fetched. The message is formatted and sent. For CAN, the CAN frame is a fixed format. For UART, messages are sent in the format as described in CAN Packet Form. In the design of this article, UART TX interrupt is used to fill the data into the UART TX buffer.

 Structure of CAN-UART Bridge:
                    Protocol Figure 2-3 Structure of CAN-UART Bridge: Protocol
 Structure of CAN-UART Bridge:
                    Transparent Figure 2-4 Structure of CAN-UART Bridge: Transparent

The structure of FIFO can be seen in Figure 2-5. Each FIFO uses three global variables to indicate the FIFO status. For gUart2Can_FIFO, gUart2Can_FIFO.fifo_in indicates the write position, gUart2Can_FIFO.fifo_out indicates the read position,and gUart2Can_FIFO.fifo_Count indicates the number of elements in the gUart2Can_FIFO.

If the gUart2Can_FIFO is empty, gUart2Can_FIFO.fifo_in equals the gUart2Can_FIFO.fifo_out, and the gUart2Can_FIFO.fifo_count is zero.

When performing processUartRxMsg(), a new message from UART is stored to gUart2Can_FIFO. So the gUart2Can_FIFO.fifo_in moves to the next position, and the gUart2Can_FIFO.fifo_count is incremented by one.

When transmitting a message from gUart2Can_FIFO to CAN, gUart2Can_FIFO.fifo_out moves to the next position, and the gUart2Can_FIFO.fifo_count subtracts one. gCan2Uart_FIFO is similar to gUart2Can_FIFO.

 Structure of FIFO Figure 2-5 Structure of FIFO