Interrupt-Based CAN Communication Using MCAN blocks of Infineon AURIX TC4x
This example shows how to use the MCAN Transmit and Receive blocks from Embedded Coder® Support Package for Infineon® AURIX™ TC4x Microcontrollers to handle interrupt-based communication between different CAN modules of Infineon® AURIX™ TC4x Microcontrollers.
Introduction
Controller area network (CAN) is widely employed communication protocol in automotive and industrial settings that facilitates real-time data exchange among electronic control units (ECUs). Serving as a multi-controller serial bus standard connecting a minimum of two nodes, CAN operates on a message-based protocol initially intended for in-vehicle communication. Its key advantages include a notable reduction in wiring complexity and the effective prevention of message collisions. CAN with flexible data-rate (CAN FD) is an extension of the classic CAN protocol that improves real-time performance. It allows data rates higher than 1 Mbps and payloads longer than 8 bytes and up to 64 data bytes.
This example implements polling-based transmission and interrupt-based reception of CAN messages using MCAN blocks. The LEDs on the hardware board blink in the pattern that matches the binary value of the transmitted messages. Infineon AURIX TC4x microcontrollers have an onboard transceiver, TLE9351VSJ, which you can use for CAN communication.
Prerequisites
Complete the Getting Started with Embedded Coder Support Package for Infineon AURIX TC4x Microcontrollers example.
Required Hardware
Infineon AURIX TC4x - TriBoards
Micro-USB cable
Available Model
Open the tc4xMcanWithInterrupts model.
The free-running counter sends 0-15 values to the MCAN Transmit block at a 1-second interval. You can change the connection to 3-bit counter to transmit values from 0 to 7.
The MCAN Transmit block uses the Module 0 Node 0
configuration to transmit the counter data in raw data format with a data length of 1 byte and the standard identifier set to 100. The MCAN receive block uses the Module 1 Node 0
configuration to receive data in dual id filter (id1 set to 0 and id2 set to 100) configured in the first standard filter.
Inside the tc4xMcanWithInterrupts/Initialize callback function, the MCAN Receive block uses New message in FIFO - 0
interrupt to receive new incoming messages in FIFO 0 in the packed form, and CAN FD Unpack block unpacks the received message data. You can analyze the configured parameters in MCAN Transmit, MCAN Receive blocks, and Hardware Mapping tool.
The model converts the received value to binary values. These binary values are used to blink the LEDs at the P13.0, P13.1, P13.2, and P13.3 pins on the hardware board. The pattern in which the LEDs blink corresponds to binary numbers from 0 to 15 that change at a 1-second interval.
The tc4xMcanWithInterrupts/Initialize subsystem in the model enables the onboard CAN transceiver, TLE9351VSJ.
Configure the Model
1. Open the tc4xMcanWithInterrupts model.
2. Double-click the MCAN Transmit block and confirm that the Data format set to Raw data
, Identifier to 100
, and length to 1
byte.
3. The MCAN Receive block uses FIFO register to receive data in packed form. Analyze Read source and Output type parameters by double-clicking the MCAN Receive block.
4. The CAN FD Unpack block unpacks the received message. Double-click the CAN FD Unpack block to ensure that the Identifier and Length (bytes) parameter values match with those in the MCAN Transmit block.
5. Press Ctrl+E to open the Configuration Parameters dialog box. Click the Hardware Implementation in the left pane and set Hardware board to Infineon AURIX™ TC4x - TriBoards.
6. On the Hardware tab, click Hardware Mapping to open the mapping browser associated with the model.
7. The model transmits data from Module 0 to Module 1 of Node 0. Observe that the the Node 1 parameter of the MCAN Transmit peripheral set to Module 0 Node 0
, and the Node 1 parameter of the MCAN Receive peripheral set to Module 1 Node 0
.
8. Click MCAN on the left pane, navigate to Module 1 Node 0 > Standard filter, and verfiy the Configuration, Filter type, Id1, Id2 parameter values. The values must match the values provided in the MCAN Receive and the CAN FD Unpack blocks.
9. Navigate to Module 1 Node 0 > Receive events to verify that New message in FIFO - 0 parameter is enabled.
10. Click on Hardware Interrupt in the left pane to open Hardware Interrupt window, where the *Interrupt group must be set to MCAN
.
Generate Code and Deploy Model to Target Hardware
Complete the following steps to generate code and run the model on the target hardware.
1. Open the tc4xMcanWithInterrupts model.
2. Click Build, Deploy & Start on the Hardware tab to deploy the target model to the hardware.
3. Follow the build process by opening the diagnostic viewer using the link provided at the bottom of the model canvas. You can observe the code generation report that contains code for model traceability. Infineon AURIX deploys the model to the hardware using a downloading tool.
4. Observe the blinking LEDs available at P13.0, P13.1, P13.2, and P13.3 pins on the hardware board. The pattern in which the LEDs blink corresponds from 0 to 15 that change at a 1-second interval.
Run Model in Monitor and Tune
1. Open the tc4xMcanWithInterrupts model.
2. Select a COM Port. To see a list of available COM ports on your computer, select Start > Control Panel > Device Manager > Ports (COM &LPT).
3. In your Simulink model, press Ctrl+E or click Modeling > Model Settings to open the Configuration Parameters dialog box. Navigate to Hardware Implementation > Target hardware resources > Connectivity. Set the Connectivity interface to Serial(ASCLIN0)
for the external mode.
4. On the Hardware tab, click Monitor & Tune to configure the model for simulation. At this point, your model runs on the Infineon AURIX TC4x hardware board and communicates with Simulink so that you can monitor and tune the parameters.
5. Observe the blinking LEDs that correspond to binary numbers from 0 to 15. You can also send the data from 3-bit counter and observe three blinking LEDs corresponding to binary numbers from 0 to 7.