Skip to content

Commit

Permalink
update of XP and PPUSB libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
lyusupov committed Oct 24, 2023
1 parent 3c94f77 commit f2692c2
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 152 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Pico PIO USB
version=0.5.2
version=0.5.3
author=sekigon-gonnoc
maintainer=sekigon-gonnoc
sentence=Pico PIO USB library for Arduino
Expand Down
46 changes: 30 additions & 16 deletions software/firmware/source/libraries/Pico-PIO-USB/src/pio_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static void __no_inline_not_in_flash_func(send_pre)(const pio_port_t *pp) {
// send PRE token in full-speed
pio_sm_set_enabled(pp->pio_usb_tx, pp->sm_tx, false);
for (uint i = 0; i < USB_TX_EOP_DISABLER_LEN; ++i) {
uint16_t instr = usb_tx_fs_pre_program.instructions[i + USB_TX_EOP_OFFSET];
uint16_t instr = pp->fs_tx_pre_program->instructions[i + USB_TX_EOP_OFFSET];
pp->pio_usb_tx->instr_mem[pp->offset_tx + i + USB_TX_EOP_OFFSET] = instr;
}

Expand All @@ -59,7 +59,7 @@ static void __no_inline_not_in_flash_func(send_pre)(const pio_port_t *pp) {
// change bus speed to low-speed
pio_sm_set_enabled(pp->pio_usb_tx, pp->sm_tx, false);
for (uint i = 0; i < USB_TX_EOP_DISABLER_LEN; ++i) {
uint16_t instr = usb_tx_fs_program.instructions[i + USB_TX_EOP_OFFSET];
uint16_t instr = pp->fs_tx_program->instructions[i + USB_TX_EOP_OFFSET];
pp->pio_usb_tx->instr_mem[pp->offset_tx + i + USB_TX_EOP_OFFSET] = instr;
}
SM_SET_CLKDIV(pp->pio_usb_tx, pp->sm_tx, pp->clk_div_ls_tx);
Expand Down Expand Up @@ -215,25 +215,26 @@ static __always_inline void add_pio_host_rx_program(PIO pio,

static void __no_inline_not_in_flash_func(initialize_host_programs)(
pio_port_t *pp, const pio_usb_configuration_t *c, root_port_t *port) {
pp->offset_tx = pio_add_program(pp->pio_usb_tx, &usb_tx_fs_program);
pp->offset_tx = pio_add_program(pp->pio_usb_tx, pp->fs_tx_program);
usb_tx_fs_program_init(pp->pio_usb_tx, pp->sm_tx, pp->offset_tx,
port->pin_dp);
port->pin_dp, port->pin_dm);

add_pio_host_rx_program(pp->pio_usb_rx, &usb_nrzi_decoder_program,
&usb_nrzi_decoder_debug_program, &pp->offset_rx,
c->debug_pin_rx);
usb_rx_fs_program_init(pp->pio_usb_rx, pp->sm_rx, pp->offset_rx, port->pin_dp,
c->debug_pin_rx);
port->pin_dm, c->debug_pin_rx);
pp->rx_reset_instr = pio_encode_jmp(pp->offset_rx);
pp->rx_reset_instr2 = pio_encode_set(pio_x, 0);

add_pio_host_rx_program(pp->pio_usb_rx, &usb_edge_detector_program,
&usb_edge_detector_debug_program, &pp->offset_eop,
c->debug_pin_eop);
eop_detect_fs_program_init(pp->pio_usb_rx, c->sm_eop, pp->offset_eop,
port->pin_dp, true, c->debug_pin_eop);
port->pin_dp, port->pin_dm, true,
c->debug_pin_eop);

usb_tx_configure_pins(pp->pio_usb_tx, pp->sm_tx, port->pin_dp);
usb_tx_configure_pins(pp->pio_usb_tx, pp->sm_tx, port->pin_dp, port->pin_dm);

pio_sm_set_jmp_pin(pp->pio_usb_rx, pp->sm_rx, port->pin_dp);
pio_sm_set_jmp_pin(pp->pio_usb_rx, pp->sm_eop, port->pin_dm);
Expand Down Expand Up @@ -261,7 +262,18 @@ static void apply_config(pio_port_t *pp, const pio_usb_configuration_t *c,
pp->sm_rx = c->sm_rx;
pp->sm_eop = c->sm_eop;
port->pin_dp = c->pin_dp;
port->pin_dm = c->pin_dp + 1;

if (c->pinout == PIO_USB_PINOUT_DPDM) {
port->pin_dm = c->pin_dp + 1;
pp->fs_tx_program = &usb_tx_dpdm_program;
pp->fs_tx_pre_program = &usb_tx_pre_dpdm_program;
pp->ls_tx_program = &usb_tx_dmdp_program;
} else {
port->pin_dm = c->pin_dp - 1;
pp->fs_tx_program = &usb_tx_dmdp_program;
pp->fs_tx_pre_program = &usb_tx_pre_dmdp_program;
pp->ls_tx_program = &usb_tx_dpdm_program;
}

pp->debug_pin_rx = c->debug_pin_rx;
pp->debug_pin_eop = c->debug_pin_eop;
Expand Down Expand Up @@ -430,24 +442,26 @@ void __no_inline_not_in_flash_func(pio_usb_ll_transfer_complete)(
ep->has_transfer = false;
}

int pio_usb_host_add_port(uint8_t pin_dp) {
int pio_usb_host_add_port(uint8_t pin_dp, PIO_USB_PINOUT pinout) {
for (int idx = 0; idx < PIO_USB_ROOT_PORT_CNT; idx++) {
root_port_t *root = PIO_USB_ROOT_PORT(idx);
if (!root->initialized) {
root->pin_dp = pin_dp;
root->pin_dm = pin_dp + 1;

PIO_USB_ROOT_PORT(idx)->pin_dp = pin_dp;
PIO_USB_ROOT_PORT(idx)->pin_dm = pin_dp + 1;
if (pinout == PIO_USB_PINOUT_DPDM) {
root->pin_dm = pin_dp + 1;
} else {
root->pin_dm = pin_dp - 1;
}

gpio_pull_down(pin_dp);
gpio_pull_down(pin_dp + 1);
gpio_pull_down(root->pin_dm);
pio_gpio_init(pio_port[0].pio_usb_tx, pin_dp);
pio_gpio_init(pio_port[0].pio_usb_tx, pin_dp + 1);
pio_gpio_init(pio_port[0].pio_usb_tx, root->pin_dm);
gpio_set_inover(pin_dp, GPIO_OVERRIDE_INVERT);
gpio_set_inover(pin_dp + 1, GPIO_OVERRIDE_INVERT);
gpio_set_inover(root->pin_dm, GPIO_OVERRIDE_INVERT);
pio_sm_set_pindirs_with_mask(pio_port[0].pio_usb_tx, pio_port[0].sm_tx, 0,
(0b11 << pin_dp));
(1 << pin_dp) | (1 << root->pin_dm));
port_pin_drive_setting(root);
root->initialized = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

// Host functions
usb_device_t *pio_usb_host_init(const pio_usb_configuration_t *c);
int pio_usb_host_add_port(uint8_t pin_dp);
int pio_usb_host_add_port(uint8_t pin_dp, PIO_USB_PINOUT pinout);
void pio_usb_host_task(void);
void pio_usb_host_stop(void);
void pio_usb_host_restart(void);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

#pragma once

typedef enum {
PIO_USB_PINOUT_DPDM = 0, // DM = DP+1
PIO_USB_PINOUT_DMDP, // DM = DP-1
} PIO_USB_PINOUT;

typedef struct {
uint8_t pin_dp;
uint8_t pio_tx_num;
Expand All @@ -13,14 +18,13 @@ typedef struct {
int8_t debug_pin_rx;
int8_t debug_pin_eop;
bool skip_alarm_pool;
PIO_USB_PINOUT pinout;
} pio_usb_configuration_t;

#ifndef PIO_USB_DP_PIN_DEFAULT
#define PIO_USB_DP_PIN_DEFAULT 0
#endif

#define PIO_USB_DM_PIN_DEFAULT (PIO_USB_DP_PIN_DEFAULT + 1)

#define PIO_USB_TX_DEFAULT 0
#define PIO_SM_USB_TX_DEFAULT 0
#define PIO_USB_DMA_TX_DEFAULT 0
Expand All @@ -31,12 +35,12 @@ typedef struct {

#define PIO_USB_DEBUG_PIN_NONE (-1)

#define PIO_USB_DEFAULT_CONFIG \
{ \
PIO_USB_DP_PIN_DEFAULT, PIO_USB_TX_DEFAULT, PIO_SM_USB_TX_DEFAULT, \
PIO_USB_DMA_TX_DEFAULT, PIO_USB_RX_DEFAULT, PIO_SM_USB_RX_DEFAULT, \
PIO_SM_USB_EOP_DEFAULT, NULL, PIO_USB_DEBUG_PIN_NONE, \
PIO_USB_DEBUG_PIN_NONE, .skip_alarm_pool = false \
#define PIO_USB_DEFAULT_CONFIG \
{ \
PIO_USB_DP_PIN_DEFAULT, PIO_USB_TX_DEFAULT, PIO_SM_USB_TX_DEFAULT, \
PIO_USB_DMA_TX_DEFAULT, PIO_USB_RX_DEFAULT, PIO_SM_USB_RX_DEFAULT, \
PIO_SM_USB_EOP_DEFAULT, NULL, PIO_USB_DEBUG_PIN_NONE, \
PIO_USB_DEBUG_PIN_NONE, false, PIO_USB_PINOUT_DPDM \
}

#define PIO_USB_EP_POOL_CNT 32
Expand Down
34 changes: 17 additions & 17 deletions software/firmware/source/libraries/Pico-PIO-USB/src/pio_usb_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ static __always_inline void override_pio_rx_program(PIO pio,

static void __no_inline_not_in_flash_func(configure_fullspeed_host)(
pio_port_t const *pp, root_port_t *port) {
override_pio_program(pp->pio_usb_tx, &usb_tx_fs_program, pp->offset_tx);
override_pio_program(pp->pio_usb_tx, pp->fs_tx_program, pp->offset_tx);
SM_SET_CLKDIV(pp->pio_usb_tx, pp->sm_tx, pp->clk_div_fs_tx);

pio_sm_set_jmp_pin(pp->pio_usb_rx, pp->sm_rx, port->pin_dp);
Expand All @@ -136,12 +136,12 @@ static void __no_inline_not_in_flash_func(configure_fullspeed_host)(
pio_sm_set_in_pins(pp->pio_usb_rx, pp->sm_eop, port->pin_dp);
SM_SET_CLKDIV(pp->pio_usb_rx, pp->sm_eop, pp->clk_div_fs_rx);

usb_tx_configure_pins(pp->pio_usb_tx, pp->sm_tx, port->pin_dp);
usb_tx_configure_pins(pp->pio_usb_tx, pp->sm_tx, port->pin_dp, port->pin_dm);
}

static void __no_inline_not_in_flash_func(configure_lowspeed_host)(
pio_port_t const *pp, root_port_t *port) {
override_pio_program(pp->pio_usb_tx, &usb_tx_ls_program, pp->offset_tx);
override_pio_program(pp->pio_usb_tx, pp->ls_tx_program, pp->offset_tx);
SM_SET_CLKDIV(pp->pio_usb_tx, pp->sm_tx, pp->clk_div_ls_tx);

pio_sm_set_jmp_pin(pp->pio_usb_rx, pp->sm_rx, port->pin_dm);
Expand All @@ -151,7 +151,7 @@ static void __no_inline_not_in_flash_func(configure_lowspeed_host)(
pio_sm_set_in_pins(pp->pio_usb_rx, pp->sm_eop, port->pin_dm);
SM_SET_CLKDIV(pp->pio_usb_rx, pp->sm_eop, pp->clk_div_ls_rx);

usb_tx_configure_pins(pp->pio_usb_tx, pp->sm_tx, port->pin_dp);
usb_tx_configure_pins(pp->pio_usb_tx, pp->sm_tx, port->pin_dp, port->pin_dm);
}

static void __no_inline_not_in_flash_func(configure_root_port)(
Expand Down Expand Up @@ -333,21 +333,20 @@ void pio_usb_host_port_reset_start(uint8_t root_idx) {
root->suspended = true;

// Force line state to SE0
pio_sm_set_pins_with_mask(pp->pio_usb_tx, pp->sm_tx, (0b00 << root->pin_dp),
(0b11u << root->pin_dp));
pio_sm_set_pins_with_mask(pp->pio_usb_tx, pp->sm_tx, 0,
(1 << root->pin_dp) | (1 << root->pin_dm));
pio_sm_set_pindirs_with_mask(pp->pio_usb_tx, pp->sm_tx,
(0b11u << root->pin_dp),
(0b11u << root->pin_dp));
(1 << root->pin_dp) | (1 << root->pin_dm),
(1 << root->pin_dp) | (1 << root->pin_dm));
}

void pio_usb_host_port_reset_end(uint8_t root_idx) {
root_port_t *root = PIO_USB_ROOT_PORT(root_idx);
pio_port_t *pp = PIO_USB_PIO_PORT(0);

// line state to input
pio_sm_set_pindirs_with_mask(pp->pio_usb_tx, pp->sm_tx,
(0b00u << root->pin_dp),
(0b11u << root->pin_dp));
pio_sm_set_pindirs_with_mask(pp->pio_usb_tx, pp->sm_tx, 0,
(1 << root->pin_dp) | (1 << root->pin_dm));

busy_wait_us(100); // TODO check if this is neccessary

Expand Down Expand Up @@ -596,15 +595,16 @@ static void on_device_connect(pio_port_t *pp, root_port_t *root,
fullspeed_flag = false;
}

pio_sm_set_pins_with_mask(pp->pio_usb_tx, pp->sm_tx, (0b00 << root->pin_dp),
(0b11u << root->pin_dp));
pio_sm_set_pindirs_with_mask(pp->pio_usb_tx, pp->sm_tx, (0b11u << root->pin_dp),
(0b11u << root->pin_dp));
pio_sm_set_pins_with_mask(pp->pio_usb_tx, pp->sm_tx, 0,
(1 << root->pin_dp) | (1 << root->pin_dm));
pio_sm_set_pindirs_with_mask(pp->pio_usb_tx, pp->sm_tx,
(1 << root->pin_dp) | (1 << root->pin_dm),
(1 << root->pin_dp) | (1 << root->pin_dm));

busy_wait_ms(100);

pio_sm_set_pindirs_with_mask(pp->pio_usb_tx, pp->sm_tx, (0b00u << root->pin_dp),
(0b11u << root->pin_dp));
pio_sm_set_pindirs_with_mask(pp->pio_usb_tx, pp->sm_tx, 0,
(1 << root->pin_dp) | (1 << root->pin_dm));

busy_wait_us(100);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ typedef struct {
int8_t debug_pin_rx;
int8_t debug_pin_eop;

const pio_program_t *fs_tx_program;
const pio_program_t *fs_tx_pre_program;
const pio_program_t *ls_tx_program;

pio_clk_div_t clk_div_fs_tx;
pio_clk_div_t clk_div_fs_rx;
pio_clk_div_t clk_div_ls_tx;
Expand Down Expand Up @@ -99,8 +103,8 @@ extern pio_port_t pio_port[1];
// Bus functions
//--------------------------------------------------------------------+

#define IRQ_TX_EOP_MASK (1 << usb_tx_fs_IRQ_EOP)
#define IRQ_TX_COMP_MASK (1 << usb_tx_fs_IRQ_COMP)
#define IRQ_TX_EOP_MASK (1 << usb_tx_dpdm_IRQ_EOP)
#define IRQ_TX_COMP_MASK (1 << usb_tx_dpdm_IRQ_COMP)
#define IRQ_TX_ALL_MASK (IRQ_TX_EOP_MASK | IRQ_TX_COMP_MASK)
#define IRQ_RX_COMP_MASK (1 << IRQ_RX_EOP)
#define IRQ_RX_ALL_MASK \
Expand Down
16 changes: 10 additions & 6 deletions software/firmware/source/libraries/Pico-PIO-USB/src/usb_rx.pio
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,16 @@ static __always_inline void pio_sm_set_jmp_pin(PIO pio, uint sm, uint jmp_pin) {
(jmp_pin << PIO_SM0_EXECCTRL_JMP_PIN_LSB);
}

static inline void usb_rx_fs_program_init(PIO pio, uint sm, uint offset, uint pin_dp, int pin_debug) {
pio_sm_set_consecutive_pindirs(pio, sm, pin_dp, 2, false);
static inline void usb_rx_fs_program_init(PIO pio, uint sm, uint offset, uint pin_dp, uint pin_dm, int pin_debug) {
if (pin_dp < pin_dm) {
pio_sm_set_consecutive_pindirs(pio, sm, pin_dp, 2, false);
} else {
pio_sm_set_consecutive_pindirs(pio, sm, pin_dm, 2, false);
}
gpio_pull_down(pin_dp);
gpio_pull_down(pin_dp + 1); // dm
gpio_pull_down(pin_dm);
gpio_set_inover(pin_dp, GPIO_OVERRIDE_INVERT);
gpio_set_inover(pin_dp + 1, GPIO_OVERRIDE_INVERT);
gpio_set_inover(pin_dm, GPIO_OVERRIDE_INVERT);

pio_sm_config c;

Expand Down Expand Up @@ -204,7 +208,7 @@ static inline void usb_rx_fs_program_init(PIO pio, uint sm, uint offset, uint pi
}

static inline void eop_detect_fs_program_init(PIO pio, uint sm, uint offset,
uint pin_dp, bool is_fs, int pin_debug) {
uint pin_dp, uint pin_dm, bool is_fs, int pin_debug) {
pio_sm_config c;

if (pin_debug < 0) {
Expand All @@ -218,7 +222,7 @@ static inline void eop_detect_fs_program_init(PIO pio, uint sm, uint offset,
}

sm_config_set_in_pins(&c, pin_dp); // for WAIT, IN
sm_config_set_jmp_pin(&c, pin_dp + 1); // for JMP
sm_config_set_jmp_pin(&c, pin_dm); // for JMP

sm_config_set_in_shift(&c, false, false, 8);

Expand Down
16 changes: 10 additions & 6 deletions software/firmware/source/libraries/Pico-PIO-USB/src/usb_rx.pio.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,16 @@ static __always_inline void pio_sm_set_jmp_pin(PIO pio, uint sm, uint jmp_pin) {
(pio->sm[sm].execctrl & ~PIO_SM0_EXECCTRL_JMP_PIN_BITS) |
(jmp_pin << PIO_SM0_EXECCTRL_JMP_PIN_LSB);
}
static inline void usb_rx_fs_program_init(PIO pio, uint sm, uint offset, uint pin_dp, int pin_debug) {
pio_sm_set_consecutive_pindirs(pio, sm, pin_dp, 2, false);
static inline void usb_rx_fs_program_init(PIO pio, uint sm, uint offset, uint pin_dp, uint pin_dm, int pin_debug) {
if (pin_dp < pin_dm) {
pio_sm_set_consecutive_pindirs(pio, sm, pin_dp, 2, false);
} else {
pio_sm_set_consecutive_pindirs(pio, sm, pin_dm, 2, false);
}
gpio_pull_down(pin_dp);
gpio_pull_down(pin_dp + 1); // dm
gpio_pull_down(pin_dm);
gpio_set_inover(pin_dp, GPIO_OVERRIDE_INVERT);
gpio_set_inover(pin_dp + 1, GPIO_OVERRIDE_INVERT);
gpio_set_inover(pin_dm, GPIO_OVERRIDE_INVERT);
pio_sm_config c;
if (pin_debug < 0) {
c = usb_nrzi_decoder_program_get_default_config(offset);
Expand All @@ -214,7 +218,7 @@ static inline void usb_rx_fs_program_init(PIO pio, uint sm, uint offset, uint pi
pio_sm_set_enabled(pio, sm, false);
}
static inline void eop_detect_fs_program_init(PIO pio, uint sm, uint offset,
uint pin_dp, bool is_fs, int pin_debug) {
uint pin_dp, uint pin_dm, bool is_fs, int pin_debug) {
pio_sm_config c;
if (pin_debug < 0) {
c = usb_edge_detector_program_get_default_config(offset);
Expand All @@ -226,7 +230,7 @@ static inline void eop_detect_fs_program_init(PIO pio, uint sm, uint offset,
sm_config_set_sideset_pins(&c, pin_debug);
}
sm_config_set_in_pins(&c, pin_dp); // for WAIT, IN
sm_config_set_jmp_pin(&c, pin_dp + 1); // for JMP
sm_config_set_jmp_pin(&c, pin_dm); // for JMP
sm_config_set_in_shift(&c, false, false, 8);
float div;
if (is_fs) {
Expand Down
Loading

0 comments on commit f2692c2

Please sign in to comment.