Communication Using CAN or FDCAN Blocks for STMicroelectronics STM32 Processor Based Boards
This example shows how to use CAN or FDCAN blocks to communicate with the STM32 processor based boards using Embedded Coder® Support Package for STMicroelectronics® STM32 Processors Based Boards.
Using this example, you can implement the CAN protocol communication in following modes:
Internal Loopback Mode - Write and read data using CAN/FDCAN write and CAN/FDCAN read blocks in internal loopback mode.
Polling Based - Send and receive data value using FDCAN protocol from one board to another in polling based.
Interrupt Based - Send and receive data value using FDCAN protocol from one board to another in an interrupt based.
Interrupt Based and HTS221 Sensor Data - Send more than 8 data bytes at a higher bit rate with interrupt and read Temperature and Humidity values.
Classic CAN Interrupt Based - Communicating of classic CAN frame format between FDCAN and CAN peripheral. Send the humidity and temperature values from B-L475E-IOT01A2 Board, upon receiving the remote frame from NUCLEO-L552ZE-Q.
Introduction
The Embedded Coder Support Package for STMicroelectronics STM32 Processors enables you to use CAN peripherals and STM32CubeMX tool to configure the peripherals on the processor.
Controller Area Network (CAN) stands as a widely employed communication protocol in automotive and industrial settings, facilitating real-time data exchange among electronic control units (ECUs). Serving as a multi-master 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.
For better real-time performance, CAN with Flexible Data-Rate (CAN FD) is used as an extension to the classic CAN protocol. It allows data rates higher than 1 MBit/s and payloads longer than 8 bytes per frame (up to 64 data bytes).
Prerequisite
Complete the following tutorials:
Required Hardware
To run this example you need the following hardware:
Two STMicroelectronics NUCLEO-L552ZE-Q Board (FD-CAN peripheral)
STMicroelectronics B-L475E-IOT01A2 Board (CAN peripheral)
Two CAN transceivers (MCP2551 is used here during implementation)
STMicroelectronics X-NUCLEO-IKS01A2 sensor expansion board
Available Models
stm32_fdcan_gettingstarted.slx - Internal Loopback for FDCAN
stm32_can_gettingstarted.slx - Internal Loopback for CAN
stm32_fdcan_polling_led_stm32l5_1.slx - Polling Based for FDCAN
stm32_fdcan_polling_led_stm32l5_2.slx - Polling Based for FDCAN
stm32_fdcan_intr_led_stm32l5_1.slx - Interrupt Based for FDCAN
stm32_fdcan_intr_led_stm32l5_2.slx - Interrupt Based for FDCAN
stm32_fdcan_sensor_stm32l5_1.slx - Interrupt Based with sensor for FDCAN
stm32_fdcan_sensor_stm32l5_2.slx - Interrupt Based with sensor for FDCAN
stm32_can_sensor_stm32l4disco.slx - Classic CAN using FDCAN peripheral with sensor
stm32_can_sensor_stm32l5.slx - Classic CAN using FDCAN peripheral with sensor
LED Blinking Using Loopback Mode
In this task, you will write the data 0-7
using FDCAN write and read the same data using FDCAN read to glow LED based on value using internal loopback mode. LED's on the board will glow indicating 0-7 binary numbers changing at an interval of 1 sec.
Open the stm32_fdcan_gettingstarted.slx model. The model is configured with NUCLEO-L552ZE-Q Board.
1. Free running counter sends 0-7 values to FDCAN write block at an interval of 1 sec.
2. In FDCAN write block, the Data format is set to Raw data
, Identifier to 1200
, length to 1
byte.
3. FDCAN read is configured to read at a Sample time of 0.01
sec. The output type is Packed.
4. The received message is unpacked by specifying Identifier and data length.
5. The received value is converted to binary and sent to the Digital Write blocks.
6. Ensure that the correct COM port is provided in the Configuration Parameters.
Model Configurations
1. Open the Modeling tab and press CTRL+E to open the Configuration Parameters dialog box.
2. Go to Hardware Implementation > Target Hardware Resources > FDCAN.
3. Ensure to enable the filter configuration as shown.
STM32CubeMX Configurations
1. Open the Modeling tab and press CTRL+E to open the Configuration Parameters dialog box. Go to Hardware Implementation > Target Hardware Resources > Build options.
2. Click Browse. Browse for the stm32_fdcan_gettingstarted.ioc file and click OK
3. Click launch to open the IOC file.
4 In the IOC file, Go to Pinout & Configuration > Connectivity > FDCAN1.
5. In Parameter settings, ensure the following parameters are selected.
Frame is set FD mode without BitRate Switching,
Mode is set Internal Loopback mode
Bit rate parameters are set to get a Nominal baud rate of 1 Mbps and a Data baud rate of 2.5 Mbps
Note - Nominal bit values are used to calculate the baud rate when frame format is Classic mode or FD mode without bitrate switching.
Generate Code and Load on Hardware Board from Simulink Model
1. To generate the code for the model, press Ctrl+B or click Build, Deploy & Start.
2. Follow the build process by opening the diagnostic viewer using the link provided at the bottom of the model canvas. After you load the code on the board, the LED blinks on the hardware board, indicating that the code is running.
You can repeat the same steps as explained above to the stm32_can_gettingstarted.slx model which is configured to B-L475E-IOT01A2 Board and observe the led blinking. For this model stm32_can_gettingstarted.ioc file is used.
Polling Based Synchronous LED Blinking
In this task, you will send the data value (0 to 7) is sent using FDCAN protocol every second from one board to another. The value received is used to display the value in binary on the LEDs of the respective boards.
Write a free-running counter value (0-7) from one board to another
Read the received value and Turn ON LEDs based on the values
Send the received FDCAN frame back
Read the received value and Turn on LEDs based on values on the second board
Open the stm32_fdcan_polling_led_stm32l5_1.slx model. The model is configured with NUCLEO-L552ZE-Q Board.
Model Configuration
Perform the same steps explained in the Loopback mode model configuration. Unselect the Auto detect board to download and run, and select manually a board from the list.
STM32CubeMX Configurations
1. Open the Modeling tab and press CTRL+E to open the Configuration Parameters dialog box. Go to Hardware Implementation > Target Hardware Resources > Build options.
2. Click Browse. Browse for the stm32_fdcan_polling_led_stm32l5_1.ioc file and click OK
3. Click launch to open the IOC file.
4 In the IOC file, Go to Pinout & Configuration > Connectivity > FDCAN1.
5. In Parameter settings, ensure the following parameters are selected.
Frame is set FD mode without BitRate Switching, Mode is set Normal mode.
Bit rate parameters are set to get a Nominal baud rate of 1 Mbps and a Data baud rate of 2.5 Mbps.
Note - Nominal bit values are used to calculate the baud rate when frame format is Classic mode or FD mode without bitrate switching.
Generate Code and Load on Hardware Board from Simulink Model
1. To generate the code for the model, press Ctrl+B or click Build, Deploy & Start.
2. Follow the build process by opening the diagnostic viewer using the link provided at the bottom of the model canvas. After you load the code on the board, the LED blinks on the hardware board, indicating that the code is running.
You can repeat the same steps as explained above to the stm32_fdcan_polling_led_stm32l5_2.slx model which is configured to NUCLEO-L552ZE-Q Board and observe the led blinking. In this model, the FDCAN write block is used with Data format as CAN message
and stm32_fdcan_polling_led_stm32l5_2.ioc file is used.
Interrupt Based Synchronous LED Blinking
In this task, you can send the data value (0 to 7) is sent using FDCAN protocol every second from one board to another. Value is received using an interrupt. The value received is used to display the value in binary on the LEDs of the respective boards.
Open the stm32_fdcan_intr_led_stm32l5_1.slx model. The model is configured with NUCLEO-L552ZE-Q Board.
Free running counter sends 0-7 values to FDCAN write block at an interval of 1 sec.
A hardware interrupt is used to receive new incoming messages in FIFO0 > Interrupt line 0.
Inside the function call subsystem, the message is unpacked and processed as per the model.
Model Configuration
1. Open the Modeling tab and press CTRL+E to open the Configuration Parameters dialog box.
2. Go to Hardware Implementation > Target Hardware Resources > FDCAN.
3. Ensure to enable the filter configuration as shown.
4. Hardware Interrupt is used to receive new incoming messages. They are stored in FIFO0 if passed by the filter.
STM32CubeMX Configurations
Perform the same steps explained in the Loopback mode STM32CubeMX configuration. In this model, stm32_fdcan_intr_led_stm32l5_1.ioc file is used.
Generate Code and Load on Hardware Board from Simulink Model
1. To generate the code for the model, press Ctrl+B or click Build, Deploy & Start.
2. Follow the build process by opening the diagnostic viewer using the link provided at the bottom of the model canvas. After you load the code on the board, the LED blinks on the hardware board, indicating that the code is running.
You can repeat the same steps as explained above to the stm32_fdcan_intr_led_stm32l5_2.slx model which is configured to NUCLEO-L552ZE-Q Board and observe the led blinking. In this model, stm32_fdcan_intr_led_stm32l5_2.ioc file is used.
Interrupt Based Synchronous LED Blinking with HTS221 Sensor Data
In this task, you can send more than 8 data bytes at a higher bit rate using Bit rate switching. The HTS221 sensor on the X-NUCLEO-IKS01A2 sensor expansion Board is used to read and transmit Temperature and Humidity values.
Write a free-running counter value (0-7) from one board to another.
Bit rate switching is enabled in the FDCAN write block.
Read the received value using interrupt and Turn ON LEDs based on the values. The received packet is sent back.
Humidity and temperature values are read from HTS221 on the X-NUCLEO-IKS01A2 sensor expansion Board.
A total of 16 bytes of data (8 bytes each('double' data type) of humidity and temperature) are sent from one board to another
Humidity and temperature, and LED value are read.
Perform Monitor and Tune to receive humidity and temperature values.
Open the stm32_fdcan_sensor_stm32l5_1.slx model. The model is configured with NUCLEO-L552ZE-Q Board.
Hardware Interrupt block is used to receive incoming FDCAN message.
In the function-call subsystem, Humidity and temperature values are read using the I2C protocol from HTS221.
Refer to the following documentation to understand how values are read and interpreted:
Humidity and temperature values are mixed and fed to the FDCAN write block as a single message of size 16 bytes(8 bytes each('double' data type)
The LED value received is processed as in previous task.
Model Configuration
Perform the same steps explained in the Loopback mode model configuration.
STM32CubeMX Configuration
In Parameter settings, the following parameters are selected to ensure the frame is FD mode with Bit Rate Switching, Mode is Normal.
Bit rate parameters are set to get a Nominal baud rate of 1 Mbps and a Data baud rate of 2.5 Mbps.
I2C1 is also configured, as it is required to read sensor data.
Model is configured with stm32_fdcan_sensor_stm32l5_1.ioc file.
Generate Code and Load on Hardware Board from Simulink Model
1. To generate the code for the model, press Ctrl+B or click Build, Deploy & Start.
2. Follow the build process by opening the diagnostic viewer using the link provided at the bottom of the model canvas. After you load the code on the board, the LED blinks on the hardware board, indicating that the code is running.
Open the stm32_fdcan_sensor_stm32l5_2.slx model. The model is configured with NUCLEO-L552ZE-Q Board.
Free running counter sends 0-7 values to FDCAN write block at an interval of 1 sec. FDCAN write block has Bit rate switching enabled.
A hardware interrupt is used to receive new incoming messages in FIFO0 > Interrupt line 0.
Inside the function call subsystem, both the received messages are unpacked by specifying the ID and data length.
Humidity and temperature values are separated and unpacked to double data type using demux and byte unpack block respectively.
All 3 values can be viewed on the displays.
Model Configuration
Perform the same steps explained in the Interrupt based synchronous mode model configuration.
STM32CubeMX Configuration
Perform the same steps explained in the Interrupt based synchronous mode STM32CubeMX configuration. Model is configured with stm32_fdcan_sensor_stm32l5_2.ioc file.
Run the Model
1. Open Hardware tab and click Monitor & Tune.
2. Use the diagnostic viewer to follow the build progress and wait until the code loads and runs on the target hardware.
3. Observe the logged data on the Display block.
HTS221 Sensor Data Transmitted Using Classic CAN
In this task, the Classic CAN network is demonstrated by sending Humidity and temperature values from B-L475E-IOT01A2 Board, upon receiving the remote frame from NUCLEO-L552ZE-Q.
A remote frame is sent from NUCLEO-L552ZE-Q to request sensor data.
Upon receiving the remote frame on B-L475E-IOT01A2 Discovery, using the onboard HTS221 sensor, Humidity and Temperature are read and calibrated.
Humidity and Temperature values are sent to NUCLEO-L552ZE-Q.
NUCLEO-L552ZE-Q is run using the external mode to view values received on the scope.
Open the stm32_can_sensor_stm32l4disco.slx model. The model is configured with B-L475E-IOT01A2 Board.
Hardware Interrupt block is used to receive incoming CAN message. In the function-call subsystem, the Humidity subsystem and the temperature are read and sent as two CAN packets of length 8 bytes.
Model Configuration
1. Open the Modeling tab and press CTRL+E to open the Configuration Parameters dialog box.
2. Go to Hardware Implementation > Target Hardware Resources > CAN.
3. Ensure to enable the filter configuration as shown.
STM32CubeMX Configuration
Select the stm32_can_sensor_stm32l4disco.ioc as the IOC file for the model configuration.
CAN is configured at a baud rate of 1 Mbps.
I2C2 is also configured, as it is required to read sensor data.
Generate Code and Load on Hardware Board from Simulink Model
1. To generate the code for the model, press Ctrl+B or click Build, Deploy & Start.
2. Follow the build process by opening the diagnostic viewer using the link provided at the bottom of the model canvas. After you load the code on the board, the LED blinks on the hardware board, indicating that the code is running.
Open the stm32_can_sensor_stm32l5.slx model. The model is configured with NUCLEO-L552ZE-Q Board.
FDCAN write block is used to send a remote(request) frame to B-L475E-IOT01A2 Board.
Data of
0
is given to the FDCAN write block. It is mandatory to specify the length in the CAN message. However, the data is not transmitted in the CAN frame as the remote frame does not have a data field.When a receive interrupt is received. The message is unpacked and using byte unpack to data type double.
A display/scope block can be used to view the temperature and humidity values received.
Model Configuration
Perform the same steps explained in the polling based synchronous mode model configuration.
STM32CubeMX Configuration
Mode is selected as Classic and Bit values are set to get a Baud rate of 1 Mbps. The Data bit values are not used for Classic CAN. The model is configure with stm32_can_sensor_stm32l5.ioc file.
Ensure the correct COM port is provided in the Model Configuration.
Run the Model
1. Open Hardware tab and click Monitor & Tune.
2. Use the diagnostic viewer to follow the build progress and wait until the code loads and runs on the target hardware. Run the model in the external mode for 10 seconds
3. Observe the logged data on the Display block.