// -------------------------------------------------- //
// This file is autogenerated by pioasm; do not edit! //
// -------------------------------------------------- //

#pragma once

#if !PICO_NO_HARDWARE
#include "hardware/pio.h"
#endif

// ----- //
// hsync //
// ----- //

#define hsync_wrap_target 1
#define hsync_wrap 8
#define hsync_pio_version 1

static const uint16_t hsync_program_instructions[] = {
    0x80a0, //  0: pull   block                      
            //     .wrap_target
    0xa027, //  1: mov    x, osr                     
    0x0042, //  2: jmp    x--, 2                     
    0xff00, //  3: set    pins, 0                [31]
    0xff00, //  4: set    pins, 0                [31]
    0xff00, //  5: set    pins, 0                [31]
    0xff01, //  6: set    pins, 1                [31]
    0xec01, //  7: set    pins, 1                [12]
    0xc100, //  8: irq    nowait 0               [1] 
            //     .wrap
};

#if !PICO_NO_HARDWARE
static const struct pio_program hsync_program = {
    .instructions = hsync_program_instructions,
    .length = 9,
    .origin = -1,
    .pio_version = 1,
#if PICO_PIO_VERSION > 0
    .used_gpio_ranges = 0x0
#endif
};

static inline pio_sm_config hsync_program_get_default_config(uint offset) {
    pio_sm_config c = pio_get_default_sm_config();
    sm_config_set_wrap(&c, offset + hsync_wrap_target, offset + hsync_wrap);
    return c;
}

static inline void hsync_program_init(PIO pio, uint sm, uint offset, uint pin) {
    // creates state machine configuration object c, sets
    // to default configurations. I believe this function is auto-generated
    // and gets a name of <program name>_program_get_default_config
    // Yes, page 40 of SDK guide
    pio_sm_config c = hsync_program_get_default_config(offset);
    // Map the state machine's SET pin group to one pin, namely the `pin`
    // parameter to this function.
    sm_config_set_set_pins(&c, pin, 1);
    // Set clock division (div by 6 for 25 MHz state machine on rp2350)
    sm_config_set_clkdiv(&c, 6) ;
    // Set this pin's GPIO function (connect PIO to the pad)
    pio_gpio_init(pio, pin);
    // pio_gpio_init(pio, pin+1);
    // Set the pin direction to output at the PIO
    pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true);
    // Load our configuration, and jump to the start of the program
    pio_sm_init(pio, sm, offset, &c);
    // Set the state machine running (commented out so can be synchronized w/ vsync)
    // pio_sm_set_enabled(pio, sm, true);
}

#endif

