Main Content

CAN Communication Using eCAN Blocks

This example shows how to use the eCAN blocks to set up CAN communication between the target hardware and your host computer for Embedded Coder® Support Package for Texas Instruments™ C2000™ Processors.

Introduction

Using this example, you will:

  • Learn how to establish a communication between target hardware and the host computer using eCAN Transmit and Receive blocks on the target hardware and Vehicle Network Toolbox™ (VNT) CAN Transmit and Receive blocks on the host.

  • Demonstrate using acceptance filtering to receive specific messages with standard and extended IDs on target hardware.

  • Demonstrate the use of the polling and interrupt methods to receive data on target hardware.

Prerequisites

Complete the following tutorials:

Required MathWorks Products:

  • Simulink®

  • Embedded Coder®

  • Embedded Coder® Support Package for Texas Instruments™ C2000™

  • Vehicle Network Toolbox™

Required Hardware:

  • Any Texas Instruments™ C2000™ board with CAN module

  • Vector CAN hardware

Note: If you are trying to run this example on a Launchpad, connect the TX and RX pin to the vector CAN device. Incase of Control card, a Transceiver (TCAN EVM transceiver) is required between vector CAN device and Control card.

Task 1 - CAN Communication between Host Computer and Target Hardware

In this task, you will transmit counter data from the host to the target hardware in two parts.

  • When the target hardware receives a new message from host, it transmits that data with a different message ID back to the host.

  • The host then displays the transmitted and received CAN signals.

Target Model

1. Open the target model.

open_system('c28x_CAN_comm.slx');

2. The example model is configured for the TI Piccolo F28069M launchpad. To select a different target hardware, in the Simulink® Editor, browse to Configuration Parameters > Hardware Implementation > Hardware board.

3. Navigate to Hardware Implementation > Target Hardware Resources > eCAN_A and ensure that you set the Baud rate to 1Mbps.

4. The model is configured to receive the data at sample time of 0.05s and to transmit the same data with a different message ID once it receives the message.

5. Following are the eCAN block configurations done for target model. Double-click on the blocks to open block parameter configurations. Ensure the specified parameter values are the same if you want to run this example for other hardware board.

  • The received length is used as an input to extract specific data which is transmitted from host.

6. Click Build, Deploy & Start in the Hardware tab or press Ctrl+B to build and download the executable file.

Run Host Model

1. Open the host model.

open_system('c2000_host_CAN_comm.slx');

2. Configure the device for CAN configuration, CAN Transmit and CAN Receive blocks. Ensure that you set the baud rate to 1Mbps in CAN configuration block.

Note:

  • The models in this example use the Vector VN1610 1 hardware. However, you can connect your models to other supported hardware.

  • Select the CAN device and channel as per your configuration.

  • CAN Configuration, CAN Transmit and CAN Receive blocks on the host are from the Vehicle Network Toolbox™ library.

3. Set the Stop time to Inf and click on Run drop-down and enable the Simulation Pacing in order to ensure real time communication with target.

4. Click Run.

5. Observe and compare the CAN data received from target hardware and transmitted from host.

Task 2 - Use Acceptance Filtering on Target Hardware to Receive Specific Messages

In this task, you will do the following in the host and target models:

Host

  • Transmit multiple standard and extended messages at different rates to the target.

  • Receive messages from the target and compare.

Target

  • Receive the standard and extended messages from the host using two methods (polling and interrupt).

  • Use ID filtering to receive only the required message from the host.

  • Send the received data back to the host with a different ID for comparison.

Target Model

1. Open the target model.

open_system('c28x_CAN_acceptance_filtering.slx');

2. The example model is configured for the TI Piccolo F28069M launchpad. To select a different target hardware, in the Simulink Editor, browse to Configuration Parameters > Hardware Implementation > Hardware board.

3. Navigate to Hardware Implementation > Target Hardware Resources > eCAN_A and ensure that you set the Baud rate to 1Mbps.

4. Target hardware receives standard ID messages using the polling method at the rate of 0.001 and extended ID messages using the interrupt method (ECAN1INTA is used to receive).

5. Following are the eCAN block configurations done for target model. Double-click on the blocks to open block parameter configurations. Ensure the specified parameter values are the same if you want to run this example for other hardware board

  • The eCAN receive block is configured to receive standard messages as shown below:

  • The eCAN receive block is configured to receive extended messages as shown below:

6. On receiving the new message, the target hardware transmits the received data with updated message IDs. There are multiple CAN Transmit blocks used to send the standard or extended messages with different Message identifier depending upon the received messages from the CAN receive block.

7. Click Build, Deploy & Start in the Hardware tab or press Ctrl+B to build and download the executable file.

Run on Host Model

1. Open the host model

open_system('c2000_host_CAN_acceptance_filtering.slx');

2. Configure the device for CAN configuration, CAN Transmit and CAN Receive blocks. Ensure that the baud rate is set to 1Mbps in CAN configuration block.

3. The model is configured to transmit three messages with different standard IDs and extended IDs.

4. The CAN Transmit blocks are configured to transmit messages with Standard IDs at the following sample rates:

  • hex2dec('0x00000371'): at 0.005

  • hex2dec('0x00000372'): at 0.007

  • hex2dec('0x00000374'): at 0.009

5. The CAN Transmit blocks are configured to transmit messages with Extended IDs at the following sample rates:

  • hex2dec('0x1C00000B'): at 0.005

  • hex2dec('0x1C000003'): at 0.007

  • hex2dec('0x1C000007'): at 0.009

6. The host model has two Receive blocks, one receives all the standard messages and the other receives all the extended messages at the rate of sample time 0.001s.

7. Set the Stop time to Inf and click on Run drop-down and enable the Simulation Pacing.

8. Click Run

9. Observe the CAN data received on the host in the Display block. The data received is corresponding to the data of the messages received on the target hardware with different IDs.

Analysis of Acceptance Filtering Behaviour

eCAN Receive block configured with Standard Message Type in Target Model

  • Message type : Standard

  • Message identifier mask : hex2dec('0x00000007')

  • Message identifier : hex2dec('0x00000001')

As the Message identifier mask is set to hex2dec('0x00000007'), only the last 3 bits from Message identifier is considered for filtering.

These 3 bits in Message identifier is set to 1, hence, it receives messages with only standard ID hex2dec('0x00000371') on the target hardware.

eCAN Receive block configured with Extended Message Type in Target Model

  • Message type : Extended

  • Message identifier mask : hex2dec('0x00000007')

  • Message identifier : hex2dec(''0x00000003'')

As the Message identifier mask is set to hex2dec('0x00000007'), only the last 3 bits from Message identifier is considered for filtering.

These 3 bits in Message identifier is set to 3, hence, it receives messages with only extended IDs hex2dec('0x1C00000B') and hex2dec('0x1C000003') on the target hardware.

When the target receives these messages, it transmits the same data with the modified message IDs to the host for comparison. The message IDs are modified in the target hardware are as shown here:

The host receives these corresponding IDs and data depending on the filtered IDs on the target hardware and it displays them.

Other Things to Try

  • In the eCAN Receive block, which is configured to receive standard messages, change the Message identifier parameter to hex2dec('0x00000002') or hex2dec('0x00000004'), to receive messages with standard IDs of hex2dec('0x00000372') or hex2dec('0x00000374'), respectively, on the target hardware.

  • In the eCAN Receive block, which is configured to receive extended messages, change the Message identifier mask parameter to hex2dec('0x00000003'), to receive all messages with extended IDs.

More About