#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/pio.h"

// -------- //
// ntc64pio //
// -------- //

#define ntc64pio_wrap_target 0
#define ntc64pio_wrap 4

static const uint16_t ntc64pio_program_instructions[] = {
            //     .wrap_target
    0xea0f, //  0: set    pins, 15               [10]
    0xea00, //  1: set    pins, 0                [10]
    0xe10f, //  2: set    pins, 15               [1] 
    0xe000, //  3: set    pins, 0                [0] 
    0x0000, //  4: jmp    0                          
            //     .wrap
};

#if !PICO_NO_HARDWARE
static const struct pio_program ntc64pio_program = {
    .instructions = ntc64pio_program_instructions,
    .length = 5,
    .origin = -1,
};

static inline pio_sm_config ntc64pio_program_get_default_config(uint offset) {
    pio_sm_config c = pio_get_default_sm_config();
    sm_config_set_wrap(&c, offset + ntc64pio_wrap_target, offset + ntc64pio_wrap);
    return c;
}

static inline void ntc64pio_program_init(PIO pio, uint sm, uint offset, uint pin, float div) {
    const uint pin_count = 5;
    pio_sm_config c = ntc64pio_program_get_default_config(offset);
    sm_config_set_out_pins(&c, pin, pin_count);
    sm_config_set_in_pins(&c, pin);
    sm_config_set_set_pins(&c, pin, pin_count);
    sm_config_set_in_shift(&c, false /* true: shift ISR right */, false, 0);
    sm_config_set_out_shift(&c, true /* true: shift OSR right */, false, 0);
    pio_sm_set_pindirs_with_mask(pio, sm, ~0u, 0b11111111);
    for(uint i=0; i<pin_count; i++) {
        // connect PIO to the pad
        pio_gpio_init(pio, pin+i);
        // enable pullup
        gpio_set_pulls(pin+i, true, false);
        // set drive to 2mA
        gpio_set_drive_strength(pin+i, GPIO_DRIVE_STRENGTH_2MA);
        // enable slew rate limiting
        gpio_set_slew_rate(pin+i, GPIO_SLEW_RATE_SLOW);
    }
    // Set the pin direction to output at the PIO
    //pio_sm_set_consecutive_pindirs(pio, sm, pin, pin_count, true);
    // Load our configuration, and jump to the start of the program
    pio_sm_init(pio, sm, offset, &c);
    // Set PIO clock divider
    pio_sm_set_clkdiv(pio, sm, div);
    pio_sm_set_enabled(pio, sm, true);
}

#endif

int main() {
  stdio_init_all();
 
  static const uint start_pin = 2;
  PIO pio = pio0;
  uint sm = pio_claim_unused_sm(pio, true);
  uint offset = pio_add_program(pio, &ntc64pio_program);
  ntc64pio_program_init(pio, sm, offset, start_pin, 10);

  /*
  uint32_t v =
      (((uint32_t) 0b00000) << 0) |
      (((uint32_t) 0b11111) << 5) |
      (((uint32_t) 0b1111) << 10) |
      (((uint32_t) 0b10000) << 14);

  pio_sm_put_blocking(pio, sm, v);
  uint32_t rv = pio_sm_get_blocking(pio, sm);
  printf("%08lx\n", rv);
  sleep_ms(1);
  pio_sm_set_enabled(pio, sm, false);
  */
  while (true) {}
}


BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT
D0D1D2D3D4D5D6D7GNDLOGIC