Raspberry Pi CM5 IoT In C - Getting Started With PIO |
Written by Harry Fairhead | ||||||||
Wednesday, 04 June 2025 | ||||||||
Page 4 of 4
With the state machine that we are going to use settled, we can now configure it. You could do this directly, but the simplest and best way to do the job is to use the SDK. This allows you to set up a struct with all of the configuration values set and then initialize the state machine in one go. First we need a default configuration and this is another utility function provided by the header file that the assembler creates: pio_sm_config c = squarewave_program_get_default_config( Now we have to set the configuration as promised. We need to specify which pins are going to be in the SET group: sm_config_set_set_pins(&c, 2, 1); and in this case we have selected just one pin, GPIO 2, as the group starts at GPIO 2 and has just one pin. This means that our set instruction in the PIO program will toggle just pin GPIO 2. We also have to set the GPIO function to GPIO_FUNC_PIO0 to let the processor know that the pin is being controlled by the PIO. Instead of using the function from Gpio5: gpio_set_function(2, GPIO_FUNC_PIO0); you can use the specific PIO function to do the same job: pio_gpio_init(pio0, 2); Finally we can load the configuration into the state machine and start it running: pio_sm_init(pio0, sm, offset, &c); pio_sm_set_enabled(pio0, sm, true); If you now look at the output of GPIO 2 you will see a square wave. The complete program is listed below, but remember that it needs the PIO header that the assembler creates: #include "pico/stdlib.h" #include "hardware/pio.h" #include "sqwave.pio.h" The pico/stdlib.h and hardware/pio.h headers are supplied by the library. stdlib.h simply includes: #include <stddef.h> #include <stdio.h> #include <stdlib.h> #include "piolib.h" pio.h header doesn’t do anything and is only included for compatibility with Pico programs so it can be deleted. To make this work you also need to let VS Code know where the PIO include files are. Add: "args": [ "-fdiagnostics-color=always", "-I../piolib/include", "-g", "${file}", "../piolib/piolib.c", "../piolib/pio_rp1.c", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], to the args section of the C/C++: gcc build active file task in tasks.json . This assumes that the header files and PIO C files are in a folder called piolib in the same folder as the project folder. Note: At the time of writing you cannot use piolib at the same time as Gpio5 as some of the functions they define are in common and hence clash. The simplest solution is to add Gpio5_ to the start of each gpio function in the Gpio5 library – this works but it means you cannot run a Pico program without modification. If you try this out you will find that the program does produce a square wave, but it isn’t suitable as a Blinky example. At the moment the clock rate is set to produce a square wave at around 66 MHz and we need to bring down the frequency to something more reasonable so that we can see an LED blink on and off. Notice that the CM5’s PIO runs at 200MHz and this is faster than the standard Pico PIO, which means that timings will change when you port a program from the Pico to the CM5. Notice that the PIO program runs as fast as the PIO clock allows it to run, but the C program runs slower than in the case of the Pico due to the fact that the PIO isn’t directly connected to the CPU running the C program. For programs that simply load the PIO with a program and some data this makes no difference, but for programs that interact with the running PIO program it can introduce timing problems. In chapter but not in this extract
Summary
Raspberry Pi Compute Module 5
|
Swift 6.2 Adds WebAssembly Support 17/06/2025 Swift 6.2 has been released with features to enhance performance, concurrency, and interoperability with other languages like C++, Java, and JavaScript. It also adds support for WebAssembly. |
Ktor 3.2 Adds HTMX Support 26/06/2025 Ktor 3.2 has been released with new modules for dependency injection and HTMX. This version also adds support for Gradle version catalogs. |
More News
|
Comments
or email your comment to: comments@i-programmer.info