Featured image of post Communicating Between Two Machines Using CAN Bus

Communicating Between Two Machines Using CAN Bus

A tutorial on setting up communication between two machines using the CAN bus. This guide includes the necessary code and steps to simulate the communication using Renode.

In this tutorial, we’ll demonstrate how to set up communication between two machines using the CAN bus protocol. This project involves two machines: a sender and a receiver. The sender will transmit a message to the receiver, which will then display the message on the console. We’ll use the STM32F4 microcontroller for this demonstration and Renode for simulation.

Prerequisites

  1. Git: Ensure you have Git installed to clone the repository.
  2. Ceedling: A testing framework for C projects.
  3. Renode: A framework for simulating complex embedded systems.

Step 1: Clone the GitHub Repository

First, clone the GitHub repository that contains the project files:

1
git clone https://github.com/yassinedevop/stm32f4-tinyHal

Step 2: Compile the Project Using Ceedling

Navigate to the project directory and compile the code using Ceedling:

1
2
cd stm32f4-tinyHal/
ceedling release

This command will build the project and generate the necessary binary files.

Step 3: Create a Description File for Renode

Next, create a Renode description file to simulate the CAN bus communication. This file will set up the emulation environment, initialize the machines, and handle the CAN bus messaging, the description file used is this one renode-canhub.

Running the Simulation

  1. Start Renode: Launch Renode with the created description file to start the simulation.
1
renode renode-canhub
  1. Observe Communication: The sender will send messages over the CAN bus, which will be received by the receiver. The receiver will print these messages to the console.
1
2
start # in the Renode monitor to start the simulation or just 's'
runMacro $sendCANMessage # to send a CAN message from the sender 

why runMacro $sendCANMessage ? because we have a macro that sends a CAN message from the sender machine. The message is sent by pushing a button on GPIO 12, using the following Macro :

macro sendCANMessage

1
2
3
   sysbus.gpioPortC OnGPIO 12 True  # Set GPIO pin 12 high to send CAN message
   sleep 0.2  # Wait for 200 milliseconds
   sysbus.gpioPortC OnGPIO 12 False  # Set GPIO pin 12 low to end CAN message

Running the simulation

Conclusion

In this guide, we’ve shown how to set up a simple CAN bus communication system using STM32F4 microcontrollers and Renode for simulation. This setup can be expanded for more complex scenarios and integrated into larger systems for testing and development.

Feel free to clone the repository and modify the code to fit your specific needs. If you have any questions or run into issues, don’t hesitate to open an issue on the GitHub repository.


Built with Hugo
Theme Stack designed by Jimmy