PIO on the Raspberry Pi Pico – Part 2

Having got all the theory out of the way in PIO on the Raspberry Pi Pico now is the time to actually start programming. Whilst I have the option of using the C/C++ SDK or one of the Python variants, I’m particularly interested in getting it going from within the Arduino environment, just because that is where I do pretty much all of my other microcontroller messing about.

I’m not using the official Arduino for Pico support though, I’m using Earl Philhower’s version from here: https://github.com/earlephilhower/arduino-pico

Pico Arduino Getting Started

Before getting too far into PIO land, there are a few things to note about using the unofficial Arduino Pico core with the Raspberry Pi Pico.

On first boot, hold down the BOOT switch and the Pico will be detected as a “UF2 Board”. This will allow the first upload to take place (more here). I’ve selected “Raspberry Pi Pico” or “Raspberry Pi Pico 2” as appropriate for the board.

Prior to the first download, the configuration should set the Debug Port to Serial. Then once the first sketch is downloaded the board can be redetected via a serial link which will allow both Serial.print() and automatic reset on download of new sketches.

Aside: there are three serial ports (more here):

  • Serial – the USB serial port – the one used here
  • Serial1 – UART0
  • Serial2 – UART1

Here is a simple starter program to make sure everything is working:

void setup() {
Serial.begin(9600);
pinMode (LED_BUILTIN, OUTPUT);
}

unsigned counter;
void loop() {
Serial.println(counter);
counter++;
delay(1000);
digitalWrite (LED_BUILTIN, (counter & 1));
}

Assuming everything is working, every second the LED will flash on or off and the counter value will be printed to the serial monitor.

Hello PIO

I’m starting off with a simple pulse on a GPIO pin and will be using the online PIO assembler from https://wokwi.com/tools/pioasm to build it.

My PIO Source:

.program pulse

.wrap_target
set pins, 1 [3] // 4 cycles
set pins, 0 [11] // 12 cycles
.wrap

% c-sdk {
static inline void pulse_program_init(PIO pio, uint sm, uint offset, uint pin) {
pio_sm_config c = pulse_program_get_default_config(offset);

// set_base=pin, count=1
sm_config_set_set_pins(&c, pin, 1);
pio_gpio_init(pio, pin);

// pins_base=pin, pin_count=1, is_out=true
pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true);

// 440 Hz pulse over 16 cycles
float div = (float)clock_get_hz(clk_sys) / (440.0 * 16.0);
sm_config_set_clkdiv(&c, div);

pio_sm_init(pio, sm, offset, &c);
pio_sm_set_enabled(pio, sm, true);
}
%}

The online assembler turns the above into the following, which is pasted into a pulse_pio.h file within an Arduino sketch.

// -------------------------------------------------- //
// This file is autogenerated by pioasm; do not edit! //
// -------------------------------------------------- //

#pragma once

#if !PICO_NO_HARDWARE
#include "hardware/pio.h"
#endif

// ----- //
// pulse //
// ----- //

#define pulse_wrap_target 0
#define pulse_wrap 1

static const uint16_t pulse_program_instructions[] = {
// .wrap_target
0xe301, // 0: set pins, 1 [3]
0xeb00, // 1: set pins, 0 [11]
// .wrap
};

#if !PICO_NO_HARDWARE
static const struct pio_program pulse_program = {
.instructions = pulse_program_instructions,
.length = 2,
.origin = -1,
};

static inline pio_sm_config pulse_program_get_default_config(uint offset) {
pio_sm_config c = pio_get_default_sm_config();
sm_config_set_wrap(&c, offset + pulse_wrap_target, offset + pulse_wrap);
return c;
}

static inline void pulse_program_init(PIO pio, uint sm, uint offset, uint pin) {
pio_sm_config c = pulse_program_get_default_config(offset);
// set_base=pin, count=1
sm_config_set_set_pins(&c, pin, 1);
pio_gpio_init(pio, pin);
// pins_base=pin, pin_count=1, is_out=true
pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true);
// 440 Hz pulse over 16 cycles
float div = (float)clock_get_hz(clk_sys) / (440.0 * 16.0);
sm_config_set_clkdiv(&c, div);
pio_sm_init(pio, sm, offset, &c);
pio_sm_set_enabled(pio, sm, true);
}

#endif

Adding the appropriate additional PIO initialisation code to my previous test sketch now gives me the following complete code:

#include <PIOProgram.h>
#include "pulse_pio.h"

#define PULSE_PIN 2

void setup() {
Serial.begin(9600);
pinMode (LED_BUILTIN, OUTPUT);
PIO pio;
uint sm, offset;
if (!pio_claim_free_sm_and_add_program(&pulse_program, &pio, &sm, &offset)) {
for (;;) {
Serial.print("No PIO or SM");
delay(10000);
}
}
pulse_program_init(pio, sm, offset, PULSE_PIN);
}

unsigned counter;
void loop() {
Serial.println(counter);
counter++;
delay(1000);
digitalWrite (LED_BUILTIN, (counter & 1));
}

Notes:

  • As I’m using “set” in the pio program I need to use the “set” group of pins in the various API calls – hence the use of sm_config_set_set_pins() which configures which pins to use with the set command. In this case, just one pin determined by the “pin” parameter.
  • I’m using the wait [] instructions to put the pulse HIGH for 4 cycles and LOW for 12 cycles, giving 16 cycles in total.
  • The waiting cycles have to account for the single cycle of the actual executed instruction, hence using [3] and [11].
  • When setting the clock divisor, I’m dividing the system frequency by my required frequency * 16 as there are 16 cycles in the complete program.
  • When I had a single cycle HIGH and 3 cycles LOW and then used the value 440.0 * 4.0 I wasn’t getting an accurate frequency (I was getting ~1.9K rather than 440). I’m guessing (I haven’t done the maths) this was overflowing the integer part of the divisor maybe.

The PIO and state machine used are allocated dynamically by the system using pio_claim_free_sm_and_add_program(). The first version had hard-coded PIO 0, state machine 0:

PIO pio = pio0;
int sm = 0;
uint offset = pio_add_program(pio, &pulse_program);

The final result can be seen on the oscilloscope trace below.

Conclusion

I’ve now been through the theory and a real, albeit simple, application and am feeling like I understand a lot more what is going on now. I still am somewhat bewildered by the huge array of API calls and do feel like they could be grouped together somehow to make them more accessible to people who haven’t swallowed the entire chip datasheet and SDK guidebooks…

But yes, I’m slowly starting to feel like I’m getting to grips with PIO a bit more now. I want to do something that now grabs some input from the GPIO and sticks it into memory, ideally using the DMA system, so that is probably where I’ll go next.

Kevin

#pio #raspberryPiPico #rp2040 #rp2350