Skip to content

Commit

Permalink
Merge pull request #1 from adafruit/rp2350b
Browse files Browse the repository at this point in the history
Enable use of pins >32 on RP2350B
  • Loading branch information
dhalbert authored Dec 27, 2024
2 parents 7902e9f + 445ca64 commit f4e50b2
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 772 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
fail-fast: false
matrix:
sdk_version:
- '2.1.0'
- '2.0.0'
- '1.5.1'
runs-on: ubuntu-latest
Expand Down Expand Up @@ -56,7 +57,5 @@ jobs:
path: |
examples/build/usb_device/usb_device.uf2
examples/build/usb_device/usb_device.hex
examples/build/capture_hid_report/capture_hid_report.uf2
examples/build/capture_hid_report/capture_hid_report.hex
examples/build/host_hid_to_device_cdc/host_hid_to_device_cdc.uf2
examples/build/host_hid_to_device_cdc/host_hid_to_device_cdc.hex
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ https://user-images.githubusercontent.com/43873124/146642806-bdf34af6-4342-4a95-

## Examples

- [capture_hid_report.c](examples/capture_hid_report/capture_hid_report.c) is a USB host sample program which print HID reports received from device. Open serial port and connect devices to pico. Default D+/D- is gp0/gp1. Call `pio_usb_add_port()` to use additional ports.
- [host_hid_to_device_cdc.c](examples/host_hid_to_device_cdc/host_hid_to_device_cdc.c) which print mouse/keyboard report from host port to device port's cdc. TinyUSB is used to manage both device (native usb) and host (pio usb) stack.
- [usb_device.c](examples/usb_device/usb_device.c) is a HID USB FS device sample which moves mouse cursor every 0.5s. External 1.5kohm pull-up register is necessary to D+ pin (Default is gp0).
- [host_hid_to_device_cdc.c](examples/host_hid_to_device_cdc/host_hid_to_device_cdc.c) is similar to **capture_hid_report.c** which print mouse/keyboard report from host port to device port's cdc. TinyUSB is used to manage both device (native usb) and host (pio usb) stack.

```bash
cd examples
Expand Down
1 change: 0 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ set(PICO_PIO_USB_DIR "${CMAKE_CURRENT_LIST_DIR}/../")
# a subdirectory, it's out of tree.
add_subdirectory(${PICO_PIO_USB_DIR} pico_pio_usb)

add_subdirectory(capture_hid_report)
add_subdirectory(usb_device)
add_subdirectory(host_hid_to_device_cdc)
add_subdirectory(test_ll)
15 changes: 0 additions & 15 deletions examples/capture_hid_report/CMakeLists.txt

This file was deleted.

77 changes: 0 additions & 77 deletions examples/capture_hid_report/capture_hid_report.c

This file was deleted.

2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Pico PIO USB
version=0.6.0
version=0.6.1
author=sekigon-gonnoc
maintainer=sekigon-gonnoc
sentence=Pico PIO USB library for Arduino
Expand Down
26 changes: 21 additions & 5 deletions src/pio_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ int __no_inline_not_in_flash_func(pio_usb_bus_receive_packet_and_handshake)(
bool crc_match = false;
int16_t t = 240;
uint16_t idx = 0;
uint16_t nak_timeout = 10000;
const uint16_t rx_buf_len = sizeof(pp->usb_rx_buffer) / sizeof(pp->usb_rx_buffer[0]);

while (t--) {
if (pio_sm_get_rx_fifo_level(pp->pio_usb_rx, pp->sm_rx)) {
Expand All @@ -192,7 +194,7 @@ int __no_inline_not_in_flash_func(pio_usb_bus_receive_packet_and_handshake)(
// timing critical start
if (t > 0) {
if (handshake == USB_PID_ACK) {
while ((pp->pio_usb_rx->irq & IRQ_RX_COMP_MASK) == 0) {
while ((pp->pio_usb_rx->irq & IRQ_RX_COMP_MASK) == 0 && idx < rx_buf_len - 1) {
if (pio_sm_get_rx_fifo_level(pp->pio_usb_rx, pp->sm_rx)) {
uint8_t data = pio_sm_get(pp->pio_usb_rx, pp->sm_rx) >> 24;
crc_prev2 = crc_prev;
Expand All @@ -212,7 +214,7 @@ int __no_inline_not_in_flash_func(pio_usb_bus_receive_packet_and_handshake)(
}
} else {
// just discard received data since we NAK/STALL anyway
while ((pp->pio_usb_rx->irq & IRQ_RX_COMP_MASK) == 0) {
while ((pp->pio_usb_rx->irq & IRQ_RX_COMP_MASK) == 0 && nak_timeout--) {
continue;
}
pio_sm_clear_fifos(pp->pio_usb_rx, pp->sm_rx);
Expand Down Expand Up @@ -289,17 +291,30 @@ static void apply_config(pio_port_t *pp, const pio_usb_configuration_t *c,
pp->sm_eop = c->sm_eop;
port->pin_dp = c->pin_dp;

uint highest_pin;
if (c->pinout == PIO_USB_PINOUT_DPDM) {
port->pin_dm = c->pin_dp + 1;
highest_pin = port->pin_dm;
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;
highest_pin = port->pin_dp;
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;
}

#if defined(PICO_PIO_USE_GPIO_BASE) && PICO_PIO_USE_GPIO_BASE+0
if (highest_pin > 32) {
pio_set_gpio_base(pp->pio_usb_tx, 16);
pio_set_gpio_base(pp->pio_usb_rx, 16);
}
#else
(void)highest_pin;
#endif

port->pinout = c->pinout;

pp->debug_pin_rx = c->debug_pin_rx;
Expand Down Expand Up @@ -458,12 +473,13 @@ uint8_t __no_inline_not_in_flash_func(pio_usb_ll_encode_tx_data)(
encoded_data[byte_idx] |= PIO_USB_TX_ENCODED_DATA_COMP;
bit_idx++;

// terminate buffers with K
do {
byte_idx = bit_idx >> 2;
encoded_data[byte_idx] <<= 2;
encoded_data[byte_idx] |= PIO_USB_TX_ENCODED_DATA_K;
bit_idx++;
} while (bit_idx & 0x07);
} while (bit_idx & 0x03);

byte_idx = bit_idx >> 2;
return byte_idx;
Expand Down Expand Up @@ -570,8 +586,8 @@ int pio_usb_host_add_port(uint8_t pin_dp, PIO_USB_PINOUT pinout) {
pio_gpio_init(pio_port[0].pio_usb_tx, root->pin_dm);
gpio_set_inover(pin_dp, 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,
(1 << pin_dp) | (1 << root->pin_dm));
pio_sm_set_pindirs_with_mask64(pio_port[0].pio_usb_tx, pio_port[0].sm_tx, 0,
(1ull << pin_dp) | (1ull << root->pin_dm));
port_pin_drive_setting(root);
root->initialized = true;

Expand Down
Loading

0 comments on commit f4e50b2

Please sign in to comment.