Site Information

 Loading... Please wait...

Programming the PiCAN-FD with Python: Implementing CAN FD on Raspberry Pi

Posted by Wilfried Voss on

PICAN CAN Bus FD Board With Real-Time Clock For Raspberry Pi with SMPS

Introduction

The PiCAN FD board equips the Raspberry Pi with Controller Area Network (CAN) Bus capabilities, specifically supporting the CAN FD (Flexible Data Rate) protocol. At its core, it utilizes the Microchip MCP2517FD or MCP2518FD CAN controller, interfacing with the Raspberry Pi via the SPI bus. This combination is ideal for applications requiring high-speed data transmission and real-time processing, such as automotive diagnostics, industrial automation, and robotics.

Hardware Overview

- CAN Controller: Microchip MCP2517FD or MCP2518FD, supporting both classic CAN and CAN FD protocols.

- CAN Transceiver: MCP2562FD ensures robust communication over the CAN bus.

- Connectors: Options for DB9 or 4-way screw terminal connections, facilitating flexible integration into various systems.

- Real-Time Clock (RTC): An onboard RTC with battery backup (battery not included) maintains accurate timekeeping, essential for timestamping CAN messages.

- Switch-Mode Power Supply (SMPS): A 5V 3A SMPS can power both the PiCAN FD board and the Raspberry Pi, ensuring stable operation.

Software Installation

1. Update the System: Ensure all existing packages are up to date.

sudo apt-get update
sudo apt-get upgrade
sudo reboot

2. Enable SPI Interface: The MCP2517FD/MCP2518FD communicates over SPI, which must be enabled.

sudo raspi-config

3. Install Necessary Tools: Install the CAN utilities package.

sudo apt-get install can-utils

4. Configure the CAN Interface: Add the following lines to /boot/config.txt to configure the CAN interface.

sudo nano /boot/config.txt

Append:
dtparam=spi=on
dtoverlay=mcp2517fd-can0,spi0-0,interrupt=25

5. Reboot the System: Apply the changes.

sudo reboot

6. Bring Up the CAN Interface: After rebooting, set up the CAN interface with the desired bit rates.

sudo ip link set can0 up type can bitrate 500000 dbitrate 2000000 fd on

Programming with Python

Python, combined with the `python-can` library, offers a convenient way to interact with the CAN bus. Here’s how to set it up:

Install python-can

pip install python-can

Sending a CAN Message

import can

# Initialize the CAN bus
bus = can.interface.Bus(channel='can0', bustype='socketcan', fd=True)

# Create a CAN message
msg = can.Message(
    arbitration_id=0x7DF,
    is_extended_id=False,
    data=[0x02, 0x01, 0x05] + [0x00] * 5,  # Example data
    is_fd=True,
    bitrate_switch=True
)

# Send the message
bus.send(msg)

Receiving CAN Messages

import can

# Initialize the CAN bus
bus = can.interface.Bus(channel='can0', bustype='socketcan', fd=True)

# Callback function to process received messages
def on_message_received(msg):
    print(f"Message received: {msg}")

# Add a listener
listener = can.Listener(on_message_received)
notifier = can.Notifier(bus, [listener])

# Keep the script running
while True:
    pass

For more advanced usage and examples, refer to the python-can documentation and the PiCAN FD Python examples repository.

Additional Resources

- User Manual: Detailed setup and configuration instructions are available in the PiCAN FD User Guide: https://copperhilltech.com/content/PICAN_FD_UGB_11.pdf

- SocketCAN Documentation: Comprehensive information on SocketCAN can be found in the Linux kernel documentation.

By integrating the PiCAN FD board with the Raspberry Pi, developers can harness the power of CAN FD networks, enabling high-speed and efficient data communication for a wide range of applications.


PICAN CAN Bus FD Board With Real-Time Clock For Raspberry PiPICAN CAN Bus FD Board With Real-Time Clock For Raspberry Pi

The PiCAN FD board enhances the Raspberry Pi 3 with CAN Bus FD (Flexible Data Rate) capabilities, enabling high-speed and efficient data communication. It features the Microchip MCP2517FD CAN FD controller paired with the MCP2562FD CAN transceiver. Developed by Bosch, CAN FD is an extension of the original CAN protocol, as specified in ISO 11898-1, designed to meet the growing bandwidth demands of modern automotive networks.

For connectivity, the board offers both DB9 and 4-way screw terminal options. Additionally, a variant with a 5V 1A switch-mode power supply (SMPS) is available, allowing the Raspberry Pi to be powered via the screw terminal or DB9 connector.

The board also includes a real-time clock with battery backup support (battery not included), ensuring accurate timekeeping for timestamped CAN messages.

An easy-to-install SocketCAN driver simplifies integration, and programming can be done in either C or Python, providing flexibility for developers. More information...