This article shows how to configure an CAN bus nodes with NXP's Model-Based Design Toolbox. The application show how you can easily mix together various communication protocols like CAN, I2C and UART to achieve a complex setup that allows to read the temperature from a remote sensors, convert that data into a CAN message, sent it via CAN bus, decode the information and display it's variation in time using a host PC application.
|Fig. 1: S32K144EVB's main features|
|Fig. 2: A typical vehicle distributed control architecture and places where S32K is a perfect match|
|Fig. 3: CAN-bus network used to emulate a car body control module|
Part A: CAN node-SLAVE Programming
|Fig. 4: Simulink model for the application running on the SLAVE board: It implements 2 communication protocols: CAN and I2C|
This model implements three basic functionalities:
A.1. Reading Data From Sensor
A standard I2C peripheral can be used to implement the communication with the sensor. To demonstrate the capabilities of the S32K144 we choose to use the FlexIO peripheral since it can be configured in multiple way to extend the functionalities of standard communication ports.
|Fig. 5: FlexIO peripheral configuration|
|Fig. 6: FlexIO I2C communication between S32K144 and Si7021 temperature sensor|
A.2. Handle the CAN receive messages
|Fig. 7: CAN peripheral configuration|
|Fig. 8: CAN receive message handling|
|Fig. 9: Get CAN message on an interrupt|
A.3. Handle the CAN transmit messages
|Fig. 10: CAN transmit message|
Part B: CAN node-MASTER Programming
|Fig. 11: Simulink model for the application running on the MASTER board: It implements 2 communication protocols: CAN and UART|
|Fig. 12: CAN transmit message to trigger temperature acquisition on node-slave|
|Fig. 13: Handling the incoming CAN messages on node-MASTER|
|Fig. 14: Checking the application with FreeMASTER over the UART interface.|
The second method that might be more expensive is based on the existence of a CAN-Sniffer. In our case we used an IXXAT can to usb converter to check the messaged on the CAN bus.
|Fig. 15: Checking CAN messages with dedicated hardware sniffer|
We hope you find this information useful. Feel free to LIKE this article and comment below.
The models used in this article are attached below.
constantinrazvan.chivu Amazing topic.
Thank you for posting such a helpful tutorial!
I have been trying to use the same tutorials with two different NXP EVB. The first is S32K144 and the second is S32K148. In order to adapt the model, I did the following changes.
1: Changed the Block's libraries-link to the new NXP library.
2: Changed the MBD_S32K14x_Config_Information to match different boards.
3: Changed the FCAN_Config to match different boards.
4: Changed the LED outputs to match different boards.
The model is not performing as expected in the tutorial and since I don't have a CAN sniffer, I find it difficult to debug the code.
Kindly find attached the updated/edited master (S32K148) and salve (S32K144) models. Can you help me figure out where the problem is?
Are you looking for a way to send one CAN message at some frequency and another CAN message at a different frequency?
If yes, then you have 2 options to do this:
- second, a hardware approach where you are using a timer to do the scheduling for you
If you have 1 timer free on your board, that would be the way to go and just default to the software timer if you have none that you can use.
The implementation would be easy:
- get 2 timer blocks (LPIT) and generate an ISR at the desired periods (while having CAN Send in the respective ISRs) - here you can control the period from the LPIT settings
- get 1 timer block to trigger 1 CAN Send, and leave the other in the main loop (not inside another ISR) - here you can control the period by setting the fixed step value
Hi, this tutorial is amazing!
I am new to simulink and I wanted to use an I2C sensor which is not FlexIO. What configuration settings are needed for something like that.
Is there an article that explains it?
Thank you for your appreciation!
Let me clarify some things first: I2C is a protocol that allows the serial data connection between many devices with unique 7 bits addresses over just 2 wires SDA/SCL while FlexIO is an adaptable peripheral on our S32K, which supports a wide range of protocols like UART, I2C, SPI, according to the user’s needs, so there should be no problems connecting your I2C sensor using our FlexIO peripheral.
In the above article, the section A.1 describes how to read data from a sensor, using PTD0(SDA) and PTD1(SCL) as I2C lines, how to address your sensor and how to get data from it, but obviously, you should read your sensor’s datasheet, which can tell you more about the configuration settings you need.
For more information about FlexIO, we recommend you to read constantinrazvan.chivu's article [FlexIO] Flex your muscles with the latest MBDToolbox release and the “Chapter 51 Flexible I/O (FlexIO)” from the S32K reference manual.
What we recommend you to do is to download and install our toolbox, in which you will find more other examples of I2C communication.
Thank you this was helpful!
I am using a lux sensor( calculating the actual value from the raw value is slightly tricky here).
But I have understood the procedure better.