The Pico has a set of PIO co-processors. They are real-time controllers that can execute logic with deterministic timing. Ideal to run strict-timed sequences and state machines. And to implement extra peripherals (like a CAN here). |
The purpose of this blog isn't to write a PIO developer tutorial. I'm trying to backtrack how the developer implemented a standard protocol using the PIO instructions. I'm using C, CMake, VSCode and the Pico C SDK. Expected skill level: you can build and run the official pico-examples.
In a usual situation (skipping OSI here) CAN has two layers:
As communication peers, I use two other CAN capable devices: A Raspberry Pi derivate with a CAN peripheral, and a Microchip CAN Bus analyser. An Arduino (MKR) with CAN shield works too.
|
This post does not look into the code. It's just a test bed so that I can see if I can set up a CAN conversation.
Get the can2040 library
Clone or download the sources from Kevin's github.
git clone https://github.com/KevinOConnor/can2040.git
Set an environment variable that points to this location. This will take care that we can create a make script that's not dependent on where you keep 3rd party code. I'm using VSCode, and will define the environment variable there. You can also do it in your OS settings, shell script, ...
Project Folder
I created a directory with a CMake file, and a src folder to contain the very simple test file (taken from here, but I referenced the can2040 sources instead of importing them in the project).
src/main.c
// source: https://gitea.predevolution-technologies.de/anme/CAN2040_Test #include <stdio.h> #include <stdint.h> #include "pico/stdlib.h" #include "pico/binary_info.h" #include "hardware/irq.h" #include "can2040.h" #include "RP2040.h" static struct can2040 cbus; static void can2040_cb(struct can2040 *cd, uint32_t notify, struct can2040_msg *msg) { // Add message processing code here... } static void PIOx_IRQHandler(void) { can2040_pio_irq_handler(&cbus); } void canbus_setup(void) { uint32_t pio_num = 0; uint32_t sys_clock = 125000000, bitrate = 125000; uint32_t gpio_rx = 14, gpio_tx = 15; // Setup canbus can2040_setup(&cbus, pio_num); can2040_callback_config(&cbus, can2040_cb); // Enable irqs irq_set_exclusive_handler(PIO0_IRQ_0_IRQn, PIOx_IRQHandler); NVIC_SetPriority(PIO0_IRQ_0_IRQn, 1); NVIC_EnableIRQ(PIO0_IRQ_0_IRQn); // Start canbus can2040_start(&cbus, sys_clock, bitrate, gpio_rx, gpio_tx); } int main(void){ const uint LED_PIN = PICO_DEFAULT_LED_PIN; gpio_init(LED_PIN); gpio_set_dir(LED_PIN, GPIO_OUT); int32_t ledState = 0; stdio_init_all(); canbus_setup(); while(1){ printf("bla\n"); gpio_put(LED_PIN, ledState); if (ledState == 0){ ledState = 1; } else{ ledState = 0; } sleep_ms(1000); } }
./CMakeList.txt
cmake_minimum_required(VERSION 3.13) # Pull in SDK (must be before project) include(pico_sdk_import.cmake) project(can2040_project0 C CXX ASM) set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 17) #I've set this to allow breakpoints on any source line set(PICO_DEOPTIMIZED_DEBUG=1) pico_sdk_init() add_executable(can2040_project0 source/main.c $ENV{CAN2040_LIB_PATH}/src/can2040.c ) target_include_directories(can2040_project0 PRIVATE ${CMAKE_CURRENT_LIST_DIR}/source $ENV{CAN2040_LIB_PATH}/src ) target_link_libraries(can2040_project0 pico_stdlib cmsis_core) pico_add_extra_outputs(can2040_project0)
side bar: Pico PIO and other predictive, time critical, co-processors The Pico PIO state machines are little co-controllers that execute each instruction at a predictable speed. |
Testing
I started a debug session, with a breakpoint at the can2040_cb() callback function. From one of my other devices, I sent a CAN message:
The RP2040 stops at the breakpoint, and I can see the message ID, DLC (length) and payload:
I'm attaching a ZIP of my VSCode project. Don't forget to download the can2040 source and set the environment variable.