Skip to content

Instantly share code, notes, and snippets.

@amb
Last active July 14, 2024 14:32
Show Gist options
  • Save amb/76e21721de62c1451189e2152f748972 to your computer and use it in GitHub Desktop.
Save amb/76e21721de62c1451189e2152f748972 to your computer and use it in GitHub Desktop.
Simple parallel out with RP2040 PIO
#include <stdio.h>
#include "hardware/clocks.h"
#include "hardware/pio.h"
#include "hardware/timer.h"
#include "main.pio.h"
#include "pico/stdlib.h"
#define START_PIN 10
#define ROW_PINS 4
// PIO program is a simple pull and shift out
// simply copy & paste the following into main.pio file
// .program main
// pull
// out pins, 4
void main_program_init(PIO pio, uint sm, uint offset, uint pin) {
// Initialize ROW_PINS amount of pins, starting from function input value pin
for(int i = 0; i < ROW_PINS; i++) {
pio_gpio_init(pio, pin + i);
}
pio_sm_set_consecutive_pindirs(pio, sm, pin, ROW_PINS, true);
// Start building PIO config
pio_sm_config c = main_program_get_default_config(offset);
// NOTE: You have to config out and set pins separately, or they just won't write anything
sm_config_set_out_pins(&c, pin, ROW_PINS);
sm_config_set_out_shift(&c, true, false, 16);
// Begin PIO state machine
pio_sm_init(pio, sm, offset, &c);
}
int main() {
stdio_init_all();
PIO pio = pio0;
uint offset = pio_add_program(pio, &main_program);
uint sm = pio_claim_unused_sm(pio, true);
main_program_init(pio, sm, offset, START_PIN);
pio_sm_set_clkdiv(pio, sm, 20);
pio_sm_set_enabled(pio, sm, true);
uint8_t counter = 0;
while(1) {
pio_sm_put(pio, sm, (uint32_t)counter);
sleep_us(5);
counter++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment