Journey's End

Aug 11
2024

PIO and Interrupts

Introduction

I want to be able to raise system level interrupts from PIO programs and there aren’t too many examples of this, so here is my simple contribution.

PIO program

First, the PIO program, which simply waits for a pin to change and then immediately raises PIO IRQ 0.

.program irq_example
.wrap_target

wait 0 pin 0    ; wait for a 0 on IN pin 0
irq nowait 0    ; raise PIO IRQ 0

.wrap

Compile this with pioasm then #include the .h in your C program.

C Program

Interrupt Setup

There are four system level interrupts related to PIO: PIO0_IRQ_0, PIO0_IRQ_1, PIO1_IRQ_0, PIO1_IRQ_1. We shall use PIO0_IRQ_0.

First we attach an interrupt handler to PIO0_IRQ_0:

void isr(void) {
    // clear PIO IRQ 0 that was raised by irq nowait 0
    pio_interrupt_clear(pio0, 0);
}

irq_add_shared_handler(PIO0_IRQ_0,
                       isr,
                       PICO_SHARED_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY);

It is important that pio_interrupt_clear() is called otherwise the CPU will lock up constantly executing the interrupt handler (aka ISR - interrupt service routine)

We then enable the interrupt:

irq_set_enabled(PIO0_IRQ_0, true);

Finally we configure PIO0_IRQ_0 to be triggered when PIO IRQ 0 is raised:

pio_set_irq0_source_enabled(pio0, pis_interrupt0, true);

The functions used above are described in the Pico C SDK documentation.

PIO Setup

The PIO program can now by installed the usual way by first setting up the state machine:

uint sm = pio_claim_unused_sm(pio0, true);
uint offset = pio_add_program(pio0, &irq_example_program);
pio_sm_config cfg = irq_example_program_get_default_config(offset);

Configure and connect GPIO to the state machine:

// connect GP 2 to IN of state machine
sm_config_set_in_pins(&cfg, 2);

// it is an input pin
pio_sm_set_consecutive_pindirs(pio0, sm, 2, 1, false);

// we want pull up on the pin
gpio_pull_up(2);

Now we are ready to start the state machine:

pio_sm_init(pio0, sm, offset, &cfg);
pio_sm_set_enabled(pio0, sm, true);

Sep 17
2023

Build Log: SCD41 CO2 Sensor

CO2 concentration is a proxy for how well a space, especially an enclosed one, is ventillated. To this end I build a quick-n-dirty CO2 sensor using a SCD41 breakout board from pimoroni and bits and pieces I had in the workshop.

Took the opportunity to practice making enclosures, especially for …

Dec 26
2022

Apr 16
2016

Mar 23
2016

May 03
2015

Apr 23
2012

Notes on RF-0417C

Some notes on using the popular Bluetooth serial module RF-0417C:

  • When sending AT commands, do not include \r\n either together or singly.
  • It appears to be happy to receive 5V inputs to TX.
  • It is not happy to receive 5V to Vcc.
  • If you connect a LED, it blinks …

May 07
2011

Success for Science

Just finished packaging my engineering thesis project into a more portable format: an altoids tin! I have seen people put some neat stuff inside them, and I am chuffed I managed to as well :) Though I had to cut through the lid for the display. And I only had to …

Feb 08
2010

Yet Another Arduino Float Print Function

Note: in arduino-0017, floating point printing is supported by default. The function below is not necessary.

``` brush:c void floatPrint(float f, int places=6) { int _d; if (f < 0) { Serial.print("-"); f*=-1; }

    _d = (int)f;

    if (!places)
    {
            return;
    }

    Serial.print(_d, DEC);
    Serial.print('.');

    while(places--)
    {
            f-=_d …
ts=13:12 tags=[arduino,code]

Dec 31
2009

Dec 21
2009

Jun 03
2009

microbric viper review

The microbric viper is neat. Good quality parts and unique idea. Makes a decent robotics platform if you get the wheel add-on. However, you gotta have small fingers to get some of the parts in place. Despite this, the hardware is solid, I like it. The one thing I would …

Jun 01
2009

May 17
2009

Feb 09
2009

Auto leveller

A simple 2 DoF setup with an accelerometer (LIS302DL) attached to the effector. The seeeduino attempts to drive the servos so the Z and Y axis measure zero acceleration. The sketch is available at the usual place.

The algorithm employed is a simple PD controller with pseduo-gradient stepping to determine …

Jan 04
2009

Dec 12
2008

2x16 character LCD displays

2x16 character LCD displays can be tricky beasts. I got mine working today with my seeeduino (an arduino compatible board with more awesome), and really wished I had known that:

  1. The contrast pin needs to be in the same power network as the power and ground pins. The contrast pin …