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

#pragma once

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

#define USB_TX_EOP_OFFSET 4
#define USB_TX_EOP_DISABLER_LEN 4

// --------- //
// usb_tx_fs //
// --------- //

#define usb_tx_fs_wrap_target 2
#define usb_tx_fs_wrap 21

#define usb_tx_fs_IRQ_COMP 0
#define usb_tx_fs_IRQ_EOP 1

static const uint16_t usb_tx_fs_program_instructions[] = {
    0xf445, //  0: set    y, 5            side 1     
    0xe083, //  1: set    pindirs, 3                 
            //     .wrap_target
    0x00ea, //  2: jmp    !osre, 10                  
    0xa142, //  3: nop                           [1] 
    0xd301, //  4: irq    nowait 1        side 0 [3] 
    0xa342, //  5: nop                           [3] 
    0xb442, //  6: nop                    side 1     
    0xe380, //  7: set    pindirs, 0             [3] 
    0xc020, //  8: irq    wait 0                     
    0x0000, //  9: jmp    0                          
    0x6021, // 10: out    x, 1                       
    0x002e, // 11: jmp    !x, 14                     
    0x1482, // 12: jmp    y--, 2          side 1     
    0xa242, // 13: nop                           [2] 
    0xf845, // 14: set    y, 5            side 2     
    0x00f1, // 15: jmp    !osre, 17                  
    0x0104, // 16: jmp    4                      [1] 
    0x6021, // 17: out    x, 1                       
    0x0035, // 18: jmp    !x, 21                     
    0x188f, // 19: jmp    y--, 15         side 2     
    0xa242, // 20: nop                           [2] 
    0xf445, // 21: set    y, 5            side 1     
            //     .wrap
};

#if !PICO_NO_HARDWARE
static const struct pio_program usb_tx_fs_program = {
    .instructions = usb_tx_fs_program_instructions,
    .length = 22,
    .origin = -1,
};

static inline pio_sm_config usb_tx_fs_program_get_default_config(uint offset) {
    pio_sm_config c = pio_get_default_sm_config();
    sm_config_set_wrap(&c, offset + usb_tx_fs_wrap_target, offset + usb_tx_fs_wrap);
    sm_config_set_sideset(&c, 3, true, false);
    return c;
}
#endif

// ------------- //
// usb_tx_fs_pre //
// ------------- //

#define usb_tx_fs_pre_wrap_target 2
#define usb_tx_fs_pre_wrap 21

#define usb_tx_fs_pre_IRQ_COMP 0
#define usb_tx_fs_pre_IRQ_EOP 1

static const uint16_t usb_tx_fs_pre_program_instructions[] = {
    0xf445, //  0: set    y, 5            side 1     
    0xe083, //  1: set    pindirs, 3                 
            //     .wrap_target
    0x00ea, //  2: jmp    !osre, 10                  
    0xa142, //  3: nop                           [1] 
    0xd701, //  4: irq    nowait 1        side 1 [3] 
    0xe080, //  5: set    pindirs, 0                 
    0xa042, //  6: nop                               
    0xa042, //  7: nop                               
    0xc020, //  8: irq    wait 0                     
    0x0000, //  9: jmp    0                          
    0x6021, // 10: out    x, 1                       
    0x002e, // 11: jmp    !x, 14                     
    0x1482, // 12: jmp    y--, 2          side 1     
    0xa242, // 13: nop                           [2] 
    0xf845, // 14: set    y, 5            side 2     
    0x00f1, // 15: jmp    !osre, 17                  
    0x0104, // 16: jmp    4                      [1] 
    0x6021, // 17: out    x, 1                       
    0x0035, // 18: jmp    !x, 21                     
    0x188f, // 19: jmp    y--, 15         side 2     
    0xa242, // 20: nop                           [2] 
    0xf445, // 21: set    y, 5            side 1     
            //     .wrap
};

#if !PICO_NO_HARDWARE
static const struct pio_program usb_tx_fs_pre_program = {
    .instructions = usb_tx_fs_pre_program_instructions,
    .length = 22,
    .origin = -1,
};

static inline pio_sm_config usb_tx_fs_pre_program_get_default_config(uint offset) {
    pio_sm_config c = pio_get_default_sm_config();
    sm_config_set_wrap(&c, offset + usb_tx_fs_pre_wrap_target, offset + usb_tx_fs_pre_wrap);
    sm_config_set_sideset(&c, 3, true, false);
    return c;
}
#endif

// --------- //
// usb_tx_ls //
// --------- //

#define usb_tx_ls_wrap_target 2
#define usb_tx_ls_wrap 21

#define usb_tx_ls_IRQ_COMP 0
#define usb_tx_ls_IRQ_EOP 1

static const uint16_t usb_tx_ls_program_instructions[] = {
    0xf845, //  0: set    y, 5            side 2     
    0xe083, //  1: set    pindirs, 3                 
            //     .wrap_target
    0x00ea, //  2: jmp    !osre, 10                  
    0xa142, //  3: nop                           [1] 
    0xd301, //  4: irq    nowait 1        side 0 [3] 
    0xa342, //  5: nop                           [3] 
    0xb842, //  6: nop                    side 2     
    0xe380, //  7: set    pindirs, 0             [3] 
    0xc020, //  8: irq    wait 0                     
    0x0000, //  9: jmp    0                          
    0x6021, // 10: out    x, 1                       
    0x002e, // 11: jmp    !x, 14                     
    0x1882, // 12: jmp    y--, 2          side 2     
    0xa242, // 13: nop                           [2] 
    0xf445, // 14: set    y, 5            side 1     
    0x00f1, // 15: jmp    !osre, 17                  
    0x0104, // 16: jmp    4                      [1] 
    0x6021, // 17: out    x, 1                       
    0x0035, // 18: jmp    !x, 21                     
    0x148f, // 19: jmp    y--, 15         side 1     
    0xa242, // 20: nop                           [2] 
    0xf845, // 21: set    y, 5            side 2     
            //     .wrap
};

#if !PICO_NO_HARDWARE
static const struct pio_program usb_tx_ls_program = {
    .instructions = usb_tx_ls_program_instructions,
    .length = 22,
    .origin = -1,
};

static inline pio_sm_config usb_tx_ls_program_get_default_config(uint offset) {
    pio_sm_config c = pio_get_default_sm_config();
    sm_config_set_wrap(&c, offset + usb_tx_ls_wrap_target, offset + usb_tx_ls_wrap);
    sm_config_set_sideset(&c, 3, true, false);
    return c;
}

#include "hardware/clocks.h"
  static void __no_inline_not_in_flash_func(usb_tx_configure_pins)(PIO pio, uint sm, uint pin_dp) {
    pio_sm_set_out_pins(pio, sm, pin_dp, 2);
    pio_sm_set_set_pins(pio, sm, pin_dp, 2);
    pio_sm_set_sideset_pins(pio, sm, pin_dp);
  }
  static inline void usb_tx_fs_program_init(PIO pio, uint sm, uint offset,
                                         uint pin_dp) {
    pio_sm_set_pins_with_mask(pio, sm, (0b01 << pin_dp), (0b11 << pin_dp));
    gpio_pull_down(pin_dp);
    gpio_pull_down(pin_dp + 1); // dm
    pio_gpio_init(pio, pin_dp);
    pio_gpio_init(pio, pin_dp + 1); // dm
    pio_sm_config c = usb_tx_fs_program_get_default_config(offset);
    // shifts to left, autopull, 8bit
    sm_config_set_out_shift(&c, true, true, 8);
    sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
    // run at 48MHz
    // clk_sys should be multiply of 12MHz
    float div = (float)clock_get_hz(clk_sys) / (48000000UL);
    sm_config_set_clkdiv(&c, div);
    pio_sm_init(pio, sm, offset, &c);
    usb_tx_configure_pins(pio, sm, pin_dp);
    pio_sm_set_enabled(pio, sm, true);
  }
  static inline void usb_tx_ls_program_init(PIO pio, uint sm, uint offset,
                                         uint pin_dp) {
    pio_sm_set_pins_with_mask(pio, sm, (0b10 << pin_dp), (0b11 << pin_dp));
    gpio_pull_down(pin_dp);
    gpio_pull_down(pin_dp + 1); // dm
    pio_gpio_init(pio, pin_dp);
    pio_gpio_init(pio, pin_dp + 1); // dm
    pio_sm_config c = usb_tx_ls_program_get_default_config(offset);
    // shifts to left, autopull, 8bit
    sm_config_set_out_shift(&c, true, true, 8);
    sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
    // run at 6MHz
    // clk_sys should be multiply of 12MHz
    float div = (float)clock_get_hz(clk_sys) / (6000000UL);
    sm_config_set_clkdiv(&c, div);
    pio_sm_init(pio, sm, offset, &c);
    usb_tx_configure_pins(pio, sm, pin_dp);
    pio_sm_set_enabled(pio, sm, true);
  }

#endif

