Simple implementation of CAN BUS logger with Raspberry Pico and MCP2518FD. Introducing the Raspberry Pico CAN FD Logger – a compact yet powerful tool for logging Controller Area Network (CAN) data with Flexible Data Rate (FD) support. This logger, built using the Raspberry Pico microcontroller, allows you to capture and store CAN bus messages efficiently.
Equipped with a CAN transceiver, the Raspberry Pico CAN FD Logger seamlessly interfaces with your vehicle's CAN network, enabling you to monitor and record communication between various electronic control units (ECUs). With its small form factor and low power consumption, it's perfect for automotive diagnostics, vehicle performance analysis, and more.
Using C/C++ programming languages, you can customize the logger to suit your specific needs. Whether you're a hobbyist, automotive enthusiast, or professional engineer, the Raspberry Pico CAN FD Logger offers an affordable and versatile solution for CAN bus logging.
The Raspberry Pico CAN FD Logger was developed using Visual Studio Code alongside the PICO SDK, providing a streamlined and efficient development environment. Leveraging the powerful features of Visual Studio Code, such as IntelliSense and debugging capabilities, developers could easily write, test, and debug their code for the Raspberry Pico microcontroller.
With the PICO SDK integration, developers had access to a rich set of libraries and APIs tailored specifically for the Raspberry Pico platform. This allowed for seamless interaction with the microcontroller's peripherals, including GPIOs, UART, SPI, and, crucially, the CAN controller.
By combining the versatility of Visual Studio Code with the flexibility of the PICO SDK, the development team was able to rapidly prototype and iterate on the CAN FD Logger firmware. The result is a robust and feature-rich logging solution that meets the needs of both hobbyists and professionals in the automotive industry.
Hardware implementationRaspberry Pi Pico W: As the central microcontroller, it runs your application code, processes data, and manages Wi-Fi connectivity. It's ideal for tasks requiring wireless communication or remote data monitoring.
Dual CAN PICO V1.0 : This carrier board acts as the bridge between your Raspberry Pi Pico W and the CAN BUS modules. It simplifies the hardware connections and potentially the software integration as well, making it easier to connect everything together and maintain a compact and efficient design.
CAN BUS FD V1.4 : This module is specifically designed for high-performance CAN communications, supporting the CAN FD protocol. It allows for faster data transmission rates compared to classic CAN, which is beneficial in environments where devices need to transmit a high volume of data rapidly or where greater bandwidth is required.
This setup allows the Raspberry Pi Pico W to interact with CAN networks via the Dual CAN PICO V1.0 and CAN BUS FD V1.4. This can be particularly useful in automotive applications, smart city infrastructure, or any industrial system where devices are spread out over large areas and need to communicate reliably and efficiently.
Sowftware implementationIn this tutorial we have selected Visual Studio Code as IDE, the main advantage in comparison with Arduino IDE is debug funzionality. The debug is fundamental to implement the porting of a software library on a different architecture. The point of start is MCP251xFD canfdspi API for SAME70 (v1.1) present in MCP1518FD producer webpage. This library was developed for SAM processor (ARM family), the main differents with Raspberry SDK are the following:
- GPIO definition
- SPI functions
- Board init (clock and serial communicatio)
Before to download the project repository, you need to install and test the PICO SDK, follow the official tutorial.
CMakeList
The CMakeList.txt contains the file that we need to compile and library to include, an important thing is the serial output selection (see the end part of file).
If you want to use a different Pico version from Pico W, add a comment on set(PICO_BOARD pico_w) and uncomment set(PICO_BOARD pico).
# Simple CAN logger with Dual Canbus FD Board
#
# Runs on the Raspberry Pi Pico connected to a Dual Canbus FD Board
# Set minimum required version of CMake
cmake_minimum_required(VERSION 3.12)
set(PICO_BOARD pico_w)
#set(PICO_BOARD pico)
# Include build functions from Pico SDK
include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake)
# Set name of project (as PROJECT_NAME) and C/C++ standards
project(DualCan-FD-Tester_V1.0 C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
add_definitions(-DMCP25xxFD)
# Creates a pico-sdk subdirectory in our project for the libraries
pico_sdk_init()
# Tell CMake where to find the executable source file
add_executable(${PROJECT_NAME}
main.c
app.c
led.c
drv_canfdspi_api.c
drv_spi.c
pcf85063.c
)
# Create map/bin/hex/uf2 files
pico_add_extra_outputs(${PROJECT_NAME})
# Link to pico_stdlib (gpio, time, etc. functions)
target_link_libraries(${PROJECT_NAME}
pico_stdlib
hardware_spi
hardware_gpio
hardware_i2c
)
if (${PICO_BOARD} MATCHES "pico_w")
target_link_libraries(${PROJECT_NAME}
pico_cyw43_arch_none
)
endif()
pico_enable_stdio_usb(${PROJECT_NAME} 1)
pico_enable_stdio_uart(${PROJECT_NAME} 0)
PINsdefinition
The PINs definition is in file common.h, remeber that the following pin assignment is valid only for the combinantion beetwen Pico with Dual Can Pico V1.0 and Can Bus FD 1.4. If you want to use different boards check the correct pin number.
#define MOSI_PIN 3 //RPI_PIN19 SPI0_TX
#define MISO_PIN 0 //RPI_PIN21 SPI0_RX
#define SCK_PIN 2 //RPI_PIN23 SPI0_SCK
#define CS_FD1 1 //RPI_PIN24 SPI0_CS0
#define CS_FD2 9 //RPI_PIN26 SPI0_CS1
#define INT_FD1 26 //RPI_PIN18 GP26
#define INT_FD2 12 //RPI_PIN22 IRQ2
#define LED_1 18 //RPI_PIN13 LED1
#define LED_2 19 //RPI_PIN7 LED2
#define SDA_PIN 20 //RPI_PIN3 I2C_0_SDA
#define SCL_PIN 21 //RPI_PIN5 I2C_0_SCL
Also in common.h we can find the following macro to enable the Can Bus FD peripherals: CAN1, CAN2 and RTC.
#define ENABLE_RTC
#define ENABLE_CAN1
#define ENABLE_CAN2
SPI functions
The most important modification of original Microchip code is located in spi_drv.c, where I have substituited the original SPI init and transfer function with Raspberry Pico SDK
int8_t DRV_SPI_TransferData(uint8_t spiSlaveDeviceIndex, uint8_t *SpiTxData, uint8_t *SpiRxData, uint16_t spiTransferSize)
{
if (spiSlaveDeviceIndex == CAN1)
{
gpio_put(CS_FD1, 0); // Select MCP2518FD
spi_write_read_blocking(SPI_DEVICE, SpiTxData, SpiRxData, spiTransferSize);
gpio_put(CS_FD1, 1); // Deselect MCP2518FD
}
else if (spiSlaveDeviceIndex == CAN2)
{
gpio_put(CS_FD2, 0); // Select MCP2518FD
spi_write_read_blocking(SPI_DEVICE, SpiTxData, SpiRxData, spiTransferSize);
gpio_put(CS_FD2, 1); // Deselect MCP2518FD
}
return 0;
}
void spi_master_init(void)
{
// These are the defaults anyway:
spi_set_format(SPI_DEVICE, 8U, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
// CAN FD board is clocked at 40MHz, so SPI must be no more than 17000000: FSCK must
// be less than or equal to 0.85 * (FSYSCLK/2). Other boards may use different clocks for
// the CAN controller and this should be reflected here.
spi_init(SPI_DEVICE, 17000000);
gpio_set_function(MISO_PIN, GPIO_FUNC_SPI);
gpio_set_function(SCK_PIN, GPIO_FUNC_SPI);
gpio_set_function(MOSI_PIN, GPIO_FUNC_SPI);
gpio_pull_up(MOSI_PIN);
gpio_pull_up(MISO_PIN);
// Set the chip select pin for the MCP25xxFD as a GPIO port
gpio_set_function(CS_FD1, GPIO_FUNC_SIO);
gpio_set_function(CS_FD2, GPIO_FUNC_SIO);
}
CAN BUS receive
To test our example you can take any can bus message generator and set the baud rate at 500 kbit\s
Comments
Please log in or sign up to comment.