pio_usb.c
1 /** 2 * Copyright (c) 2021 sekigon-gonnoc 3 */ 4 5 #pragma GCC push_options 6 #pragma GCC optimize("-O3") 7 8 #include <stdio.h> 9 #include <stdint.h> 10 #include <string.h> // memcpy 11 12 #include "hardware/clocks.h" 13 #include "hardware/dma.h" 14 #include "hardware/pio.h" 15 #include "hardware/pio_instructions.h" 16 #include "hardware/sync.h" 17 #include "pico/bootrom.h" 18 #include "pico/stdlib.h" 19 #include "pico/platform.h" 20 21 #include "pio_usb.h" 22 #include "usb_definitions.h" 23 #include "pio_usb_configuration.h" 24 #include "pio_usb_ll.h" 25 #include "usb_crc.h" 26 #include "usb_tx.pio.h" 27 #include "usb_rx.pio.h" 28 29 #define UNUSED_PARAMETER(x) (void)x 30 31 usb_device_t pio_usb_device[PIO_USB_DEVICE_CNT]; 32 pio_port_t pio_port[1]; 33 root_port_t pio_usb_root_port[PIO_USB_ROOT_PORT_CNT]; 34 endpoint_t pio_usb_ep_pool[PIO_USB_EP_POOL_CNT]; 35 36 static uint8_t ack_encoded[5]; 37 static uint8_t nak_encoded[5]; 38 static uint8_t stall_encoded[5]; 39 static uint8_t pre_encoded[5]; 40 41 //--------------------------------------------------------------------+ 42 // Bus functions 43 //--------------------------------------------------------------------+ 44 45 static void __no_inline_not_in_flash_func(send_pre)(const pio_port_t *pp) { 46 // send PRE token in full-speed 47 uint16_t instr = pp->fs_tx_pre_program->instructions[0]; 48 pp->pio_usb_tx->instr_mem[pp->offset_tx] = instr; 49 50 SM_SET_CLKDIV(pp->pio_usb_tx, pp->sm_tx, pp->clk_div_fs_tx); 51 52 pio_sm_exec(pp->pio_usb_tx, pp->sm_tx, pp->tx_start_instr); 53 dma_channel_transfer_from_buffer_now(pp->tx_ch, pre_encoded, 54 sizeof(pre_encoded)); 55 pp->pio_usb_tx->irq = IRQ_TX_ALL_MASK; // clear complete flag 56 57 while ((pp->pio_usb_tx->irq & IRQ_TX_EOP_MASK) == 0) { 58 continue; 59 } 60 pio_sm_clear_fifos(pp->pio_usb_tx, pp->sm_tx); 61 62 // change bus speed to low-speed 63 pio_sm_set_enabled(pp->pio_usb_tx, pp->sm_tx, false); 64 instr = pp->fs_tx_program->instructions[0]; 65 pp->pio_usb_tx->instr_mem[pp->offset_tx] = instr; 66 SM_SET_CLKDIV(pp->pio_usb_tx, pp->sm_tx, pp->clk_div_ls_tx); 67 pio_sm_set_enabled(pp->pio_usb_tx, pp->sm_tx, true); 68 69 // pio_sm_clear_fifos(pp->pio_usb_tx, pp->sm_tx); 70 // pio_sm_exec(pp->pio_usb_tx, pp->sm_tx, pp->tx_start_instr); 71 // SM_SET_CLKDIV_MAXSPEED(pp->pio_usb_rx, pp->sm_rx); 72 73 pio_sm_set_enabled(pp->pio_usb_rx, pp->sm_eop, false); 74 SM_SET_CLKDIV(pp->pio_usb_rx, pp->sm_eop, pp->clk_div_ls_rx); 75 pio_sm_set_enabled(pp->pio_usb_rx, pp->sm_eop, true); 76 } 77 78 void __not_in_flash_func(pio_usb_bus_usb_transfer)(const pio_port_t *pp, 79 uint8_t *data, uint16_t len) { 80 if (pp->need_pre) { 81 send_pre(pp); 82 } 83 84 pio_sm_exec(pp->pio_usb_tx, pp->sm_tx, pp->tx_start_instr); 85 dma_channel_transfer_from_buffer_now(pp->tx_ch, data, len); 86 pp->pio_usb_tx->irq = IRQ_TX_ALL_MASK; // clear complete flag 87 88 io_ro_32 *pc = &pp->pio_usb_tx->sm[pp->sm_tx].addr; 89 while ((pp->pio_usb_tx->irq & IRQ_TX_ALL_MASK) == 0) { 90 continue; 91 } 92 pio_sm_clear_fifos(pp->pio_usb_tx, pp->sm_tx); 93 while (*pc < PIO_USB_TX_ENCODED_DATA_COMP) { 94 continue; 95 } 96 } 97 98 void __no_inline_not_in_flash_func(pio_usb_bus_send_handshake)( 99 const pio_port_t *pp, uint8_t pid) { 100 switch (pid) { 101 case USB_PID_ACK: 102 pio_usb_bus_usb_transfer(pp, ack_encoded, 5); 103 break; 104 105 case USB_PID_NAK: 106 pio_usb_bus_usb_transfer(pp, nak_encoded, 5); 107 break; 108 109 case USB_PID_STALL: 110 default: 111 pio_usb_bus_usb_transfer(pp, stall_encoded, 5); 112 break; 113 } 114 } 115 116 void __no_inline_not_in_flash_func(pio_usb_bus_send_token)(const pio_port_t *pp, 117 uint8_t token, 118 uint8_t addr, 119 uint8_t ep_num) { 120 121 uint8_t packet[4] = {USB_SYNC, token, 0, 0}; 122 uint16_t dat = ((uint16_t)(ep_num & 0xf) << 7) | (addr & 0x7f); 123 uint8_t crc = calc_usb_crc5(dat); 124 packet[2] = dat & 0xff; 125 packet[3] = (crc << 3) | ((dat >> 8) & 0x1f); 126 127 uint8_t packet_encoded[sizeof(packet) * 2 * 7 / 6 + 2]; 128 uint8_t encoded_len = pio_usb_ll_encode_tx_data(packet, sizeof(packet), packet_encoded); 129 130 pio_usb_bus_usb_transfer(pp, packet_encoded, encoded_len); 131 } 132 133 void __no_inline_not_in_flash_func(pio_usb_bus_prepare_receive)(const pio_port_t *pp) { 134 pio_sm_set_enabled(pp->pio_usb_rx, pp->sm_rx, false); 135 pio_sm_clear_fifos(pp->pio_usb_rx, pp->sm_rx); 136 pio_sm_restart(pp->pio_usb_rx, pp->sm_rx); 137 pio_sm_exec(pp->pio_usb_rx, pp->sm_rx, pp->rx_reset_instr); 138 pio_sm_exec(pp->pio_usb_rx, pp->sm_rx, pp->rx_reset_instr2); 139 pio_sm_set_enabled(pp->pio_usb_rx, pp->sm_rx, true); 140 } 141 142 void __no_inline_not_in_flash_func(pio_usb_bus_start_receive)(const pio_port_t *pp) { 143 pp->pio_usb_rx->irq = IRQ_RX_ALL_MASK; 144 } 145 146 uint8_t __no_inline_not_in_flash_func(pio_usb_bus_wait_handshake)(pio_port_t* pp) { 147 int16_t t = 240; 148 int16_t idx = 0; 149 150 while (t--) { 151 if (pio_sm_get_rx_fifo_level(pp->pio_usb_rx, pp->sm_rx)) { 152 uint8_t data = pio_sm_get(pp->pio_usb_rx, pp->sm_rx) >> 24; 153 pp->usb_rx_buffer[idx++] = data; 154 if (idx == 2) { 155 break; 156 } 157 } 158 } 159 160 if (t > 0) { 161 while ((pp->pio_usb_rx->irq & IRQ_RX_COMP_MASK) == 0) { 162 continue; 163 } 164 } 165 166 // pio_sm_set_enabled(pp->pio_usb_rx, pp->sm_rx, true); 167 168 return pp->usb_rx_buffer[1]; 169 } 170 171 int __no_inline_not_in_flash_func(pio_usb_bus_receive_packet_and_handshake)( 172 pio_port_t *pp, uint8_t handshake) { 173 uint16_t crc = 0xffff; 174 uint16_t crc_prev = 0xffff; 175 uint16_t crc_prev2 = 0xffff; 176 uint16_t crc_receive = 0xffff; 177 uint16_t crc_receive_inverse; 178 bool crc_match = false; 179 int16_t t = 240; 180 uint16_t idx = 0; 181 uint16_t nak_timeout = 10000; 182 const uint16_t rx_buf_len = sizeof(pp->usb_rx_buffer) / sizeof(pp->usb_rx_buffer[0]); 183 184 while (t--) { 185 if (pio_sm_get_rx_fifo_level(pp->pio_usb_rx, pp->sm_rx)) { 186 uint8_t data = pio_sm_get(pp->pio_usb_rx, pp->sm_rx) >> 24; 187 pp->usb_rx_buffer[idx++] = data; 188 if (idx == 2) { 189 break; 190 } 191 } 192 } 193 194 // timing critical start 195 if (t > 0) { 196 if (handshake == USB_PID_ACK) { 197 while ((pp->pio_usb_rx->irq & IRQ_RX_COMP_MASK) == 0 && idx < rx_buf_len - 1) { 198 if (pio_sm_get_rx_fifo_level(pp->pio_usb_rx, pp->sm_rx)) { 199 uint8_t data = pio_sm_get(pp->pio_usb_rx, pp->sm_rx) >> 24; 200 crc_prev2 = crc_prev; 201 crc_prev = crc; 202 crc = update_usb_crc16(crc, data); 203 pp->usb_rx_buffer[idx++] = data; 204 crc_receive = (crc_receive >> 8) | (data << 8); 205 crc_receive_inverse = crc_receive ^ 0xffff; 206 crc_match = (crc_receive_inverse == crc_prev2); 207 } 208 } 209 210 if (idx >= 4 && crc_match) { 211 pio_usb_bus_send_handshake(pp, USB_PID_ACK); 212 // timing critical end 213 return idx - 4; 214 } 215 } else { 216 // just discard received data since we NAK/STALL anyway 217 while ((pp->pio_usb_rx->irq & IRQ_RX_COMP_MASK) == 0 && nak_timeout--) { 218 continue; 219 } 220 pio_sm_clear_fifos(pp->pio_usb_rx, pp->sm_rx); 221 222 pio_usb_bus_send_handshake(pp, handshake); 223 } 224 } 225 226 return -1; 227 } 228 229 static __always_inline void add_pio_host_rx_program(PIO pio, 230 const pio_program_t *program, 231 const pio_program_t *debug_program, 232 uint *offset, int debug_pin) { 233 if (debug_pin < 0) { 234 *offset = pio_add_program(pio, program); 235 } else { 236 *offset = pio_add_program(pio, debug_program); 237 } 238 } 239 240 static void __no_inline_not_in_flash_func(initialize_host_programs)( 241 pio_port_t *pp, const pio_usb_configuration_t *c, root_port_t *port) { 242 // TX program should be placed at address 0 243 pio_add_program_at_offset(pp->pio_usb_tx, pp->fs_tx_program, 0); 244 pp->offset_tx = 0; 245 usb_tx_fs_program_init(pp->pio_usb_tx, pp->sm_tx, pp->offset_tx, port->pin_dp, 246 port->pin_dm); 247 pp->tx_start_instr = pio_encode_jmp(pp->offset_tx + 4); 248 pp->tx_reset_instr = pio_encode_jmp(pp->offset_tx + 2); 249 250 add_pio_host_rx_program(pp->pio_usb_rx, &usb_nrzi_decoder_program, 251 &usb_nrzi_decoder_debug_program, &pp->offset_rx, 252 c->debug_pin_rx); 253 usb_rx_fs_program_init(pp->pio_usb_rx, pp->sm_rx, pp->offset_rx, port->pin_dp, 254 port->pin_dm, c->debug_pin_rx); 255 pp->rx_reset_instr = pio_encode_jmp(pp->offset_rx); 256 pp->rx_reset_instr2 = pio_encode_set(pio_x, 0); 257 258 add_pio_host_rx_program(pp->pio_usb_rx, &usb_edge_detector_program, 259 &usb_edge_detector_debug_program, &pp->offset_eop, 260 c->debug_pin_eop); 261 eop_detect_fs_program_init(pp->pio_usb_rx, c->sm_eop, pp->offset_eop, 262 port->pin_dp, port->pin_dm, true, 263 c->debug_pin_eop); 264 265 usb_tx_configure_pins(pp->pio_usb_tx, pp->sm_tx, port->pin_dp, port->pin_dm); 266 267 pio_sm_set_jmp_pin(pp->pio_usb_rx, pp->sm_rx, port->pin_dp); 268 pio_sm_set_jmp_pin(pp->pio_usb_rx, pp->sm_eop, port->pin_dm); 269 pio_sm_set_in_pins(pp->pio_usb_rx, pp->sm_eop, port->pin_dp); 270 } 271 272 static void configure_tx_channel(uint8_t ch, PIO pio, uint sm) { 273 dma_channel_config conf = dma_channel_get_default_config(ch); 274 275 channel_config_set_read_increment(&conf, true); 276 channel_config_set_write_increment(&conf, false); 277 channel_config_set_transfer_data_size(&conf, DMA_SIZE_8); 278 channel_config_set_dreq(&conf, pio_get_dreq(pio, sm, true)); 279 280 dma_channel_set_config(ch, &conf, false); 281 dma_channel_set_write_addr(ch, &pio->txf[sm], false); 282 } 283 284 static void apply_config(pio_port_t *pp, const pio_usb_configuration_t *c, 285 root_port_t *port) { 286 pp->pio_usb_tx = pio_get_instance(c->pio_tx_num); 287 pp->sm_tx = c->sm_tx; 288 pp->tx_ch = c->tx_ch; 289 pp->pio_usb_rx = pio_get_instance(c->pio_rx_num); 290 pp->sm_rx = c->sm_rx; 291 pp->sm_eop = c->sm_eop; 292 port->pin_dp = c->pin_dp; 293 294 uint highest_pin; 295 if (c->pinout == PIO_USB_PINOUT_DPDM) { 296 port->pin_dm = c->pin_dp + 1; 297 highest_pin = port->pin_dm; 298 pp->fs_tx_program = &usb_tx_dpdm_program; 299 pp->fs_tx_pre_program = &usb_tx_pre_dpdm_program; 300 pp->ls_tx_program = &usb_tx_dmdp_program; 301 } else { 302 port->pin_dm = c->pin_dp - 1; 303 highest_pin = port->pin_dp; 304 pp->fs_tx_program = &usb_tx_dmdp_program; 305 pp->fs_tx_pre_program = &usb_tx_pre_dmdp_program; 306 pp->ls_tx_program = &usb_tx_dpdm_program; 307 } 308 309 #if defined(PICO_PIO_USE_GPIO_BASE) && PICO_PIO_USE_GPIO_BASE+0 310 if (highest_pin > 32) { 311 pio_set_gpio_base(pp->pio_usb_tx, 16); 312 pio_set_gpio_base(pp->pio_usb_rx, 16); 313 } 314 #else 315 (void)highest_pin; 316 #endif 317 318 port->pinout = c->pinout; 319 320 pp->debug_pin_rx = c->debug_pin_rx; 321 pp->debug_pin_eop = c->debug_pin_eop; 322 323 pio_sm_claim(pp->pio_usb_tx, pp->sm_tx); 324 pio_sm_claim(pp->pio_usb_rx, pp->sm_rx); 325 pio_sm_claim(pp->pio_usb_rx, pp->sm_eop); 326 } 327 328 static void port_pin_drive_setting(const root_port_t *port) { 329 gpio_set_slew_rate(port->pin_dp, GPIO_SLEW_RATE_FAST); 330 gpio_set_slew_rate(port->pin_dm, GPIO_SLEW_RATE_FAST); 331 gpio_set_drive_strength(port->pin_dp, GPIO_DRIVE_STRENGTH_12MA); 332 gpio_set_drive_strength(port->pin_dm, GPIO_DRIVE_STRENGTH_12MA); 333 } 334 335 void pio_usb_bus_init(pio_port_t *pp, const pio_usb_configuration_t *c, 336 root_port_t *root) { 337 memset(root, 0, sizeof(root_port_t)); 338 339 pp->pio_usb_tx = pio_get_instance(c->pio_tx_num); 340 dma_claim_mask(1<<c->tx_ch); 341 configure_tx_channel(c->tx_ch, pp->pio_usb_tx, c->sm_tx); 342 343 apply_config(pp, c, root); 344 initialize_host_programs(pp, c, root); 345 port_pin_drive_setting(root); 346 root->initialized = true; 347 root->dev_addr = 0; 348 349 // pre-encode handshake packets 350 uint8_t raw_packet[] = {USB_SYNC, USB_PID_ACK}; 351 pio_usb_ll_encode_tx_data(raw_packet, 2, ack_encoded); 352 raw_packet[1] = USB_PID_NAK; 353 pio_usb_ll_encode_tx_data(raw_packet, 2, nak_encoded); 354 raw_packet[1] = USB_PID_STALL; 355 pio_usb_ll_encode_tx_data(raw_packet, 2, stall_encoded); 356 raw_packet[1] = USB_PID_PRE; 357 pio_usb_ll_encode_tx_data(raw_packet, 2, pre_encoded); 358 } 359 360 //--------------------------------------------------------------------+ 361 // Application API 362 //--------------------------------------------------------------------+ 363 364 endpoint_t *pio_usb_get_endpoint(usb_device_t *device, uint8_t idx) { 365 uint8_t ep_id = device->endpoint_id[idx]; 366 if (ep_id == 0) { 367 return NULL; 368 } else if (ep_id >= 1) { 369 return &pio_usb_ep_pool[ep_id - 1]; 370 } 371 return NULL; 372 } 373 374 int __no_inline_not_in_flash_func(pio_usb_get_in_data)(endpoint_t *ep, 375 uint8_t *buffer, 376 uint8_t len) { 377 if (ep->has_transfer || ep->is_tx) { 378 return -1; 379 } 380 381 if (ep->new_data_flag) { 382 len = len < ep->actual_len ? len : ep->actual_len; 383 memcpy(buffer, (void *)ep->buffer, len); 384 385 ep->new_data_flag = false; 386 387 return pio_usb_ll_transfer_start(ep, ep->buffer, ep->size) ? len : -1; 388 } 389 390 return -1; 391 } 392 393 int __no_inline_not_in_flash_func(pio_usb_set_out_data)(endpoint_t *ep, 394 const uint8_t *buffer, 395 uint8_t len) { 396 if (ep->has_transfer || !ep->is_tx) { 397 return -1; 398 } 399 400 return pio_usb_ll_transfer_start(ep, (uint8_t *)buffer, len) ? 0 : -1; 401 } 402 403 //--------------------------------------------------------------------+ 404 // Low Level Function 405 //--------------------------------------------------------------------+ 406 407 void __no_inline_not_in_flash_func(pio_usb_ll_configure_endpoint)( 408 endpoint_t *ep, uint8_t const *desc_endpoint) { 409 const endpoint_descriptor_t *d = (const endpoint_descriptor_t *)desc_endpoint; 410 ep->size = d->max_size[0] | (d->max_size[1] << 8); 411 ep->ep_num = d->epaddr; 412 ep->attr = d->attr; 413 ep->interval = d->interval; 414 ep->interval_counter = 0; 415 ep->data_id = 0; 416 } 417 418 // Encode transfer data to 2bit sequence represents TX PIO instruction address 419 uint8_t __no_inline_not_in_flash_func(pio_usb_ll_encode_tx_data)( 420 uint8_t const *buffer, uint8_t buffer_len, uint8_t *encoded_data) { 421 uint16_t bit_idx = 0; 422 int current_state = 1; 423 int bit_stuffing = 6; 424 for (int idx = 0; idx < buffer_len; idx++) { 425 uint8_t byte = buffer[idx]; 426 for (int b = 0; b < 8; b++) { 427 uint8_t byte_idx = bit_idx >> 2; 428 encoded_data[byte_idx] <<= 2; 429 if (byte & (1 << b)) { 430 if (current_state) { 431 encoded_data[byte_idx] |= PIO_USB_TX_ENCODED_DATA_K; 432 } else { 433 encoded_data[byte_idx] |= PIO_USB_TX_ENCODED_DATA_J; 434 } 435 bit_stuffing--; 436 } else { 437 if (current_state) { 438 encoded_data[byte_idx] |= PIO_USB_TX_ENCODED_DATA_J; 439 current_state = 0; 440 } else { 441 encoded_data[byte_idx] |= PIO_USB_TX_ENCODED_DATA_K; 442 current_state = 1; 443 } 444 bit_stuffing = 6; 445 } 446 447 bit_idx++; 448 449 if (bit_stuffing == 0) { 450 byte_idx = bit_idx >> 2; 451 encoded_data[byte_idx] <<= 2; 452 453 if (current_state) { 454 encoded_data[byte_idx] |= PIO_USB_TX_ENCODED_DATA_J; 455 current_state = 0; 456 } else { 457 encoded_data[byte_idx] |= PIO_USB_TX_ENCODED_DATA_K; 458 current_state = 1; 459 } 460 bit_stuffing = 6; 461 bit_idx++; 462 } 463 } 464 } 465 466 uint8_t byte_idx = bit_idx >> 2; 467 encoded_data[byte_idx] <<= 2; 468 encoded_data[byte_idx] |= PIO_USB_TX_ENCODED_DATA_SE0; 469 bit_idx++; 470 471 byte_idx = bit_idx >> 2; 472 encoded_data[byte_idx] <<= 2; 473 encoded_data[byte_idx] |= PIO_USB_TX_ENCODED_DATA_COMP; 474 bit_idx++; 475 476 // terminate buffers with K 477 do { 478 byte_idx = bit_idx >> 2; 479 encoded_data[byte_idx] <<= 2; 480 encoded_data[byte_idx] |= PIO_USB_TX_ENCODED_DATA_K; 481 bit_idx++; 482 } while (bit_idx & 0x03); 483 484 byte_idx = bit_idx >> 2; 485 return byte_idx; 486 } 487 488 static inline __force_inline void prepare_tx_data(endpoint_t *ep) { 489 uint16_t const xact_len = pio_usb_ll_get_transaction_len(ep); 490 uint8_t buffer[PIO_USB_EP_SIZE + 4]; 491 buffer[0] = USB_SYNC; 492 buffer[1] = (ep->data_id == 1) ? USB_PID_DATA1 493 : USB_PID_DATA0; // USB_PID_SETUP also DATA0 494 memcpy(buffer + 2, ep->app_buf, xact_len); 495 496 uint16_t const crc16 = calc_usb_crc16(ep->app_buf, xact_len); 497 buffer[2 + xact_len] = crc16 & 0xff; 498 buffer[2 + xact_len + 1] = crc16 >> 8; 499 500 ep->encoded_data_len = 501 pio_usb_ll_encode_tx_data(buffer, xact_len + 4, ep->buffer); 502 } 503 504 bool __no_inline_not_in_flash_func(pio_usb_ll_transfer_start)(endpoint_t *ep, 505 uint8_t *buffer, 506 uint16_t buflen) { 507 if (ep->has_transfer) { 508 return false; 509 } 510 511 ep->app_buf = buffer; 512 ep->total_len = buflen; 513 ep->actual_len = 0; 514 515 if (ep->is_tx) { 516 prepare_tx_data(ep); 517 } else { 518 ep->new_data_flag = false; 519 } 520 521 ep->transfer_started = false; 522 ep->transfer_aborted = false; 523 ep->has_transfer = true; 524 525 return true; 526 } 527 528 bool __no_inline_not_in_flash_func(pio_usb_ll_transfer_continue)( 529 endpoint_t *ep, uint16_t xferred_bytes) { 530 ep->app_buf += xferred_bytes; 531 ep->actual_len += xferred_bytes; 532 ep->data_id ^= 1; 533 534 if ((xferred_bytes < ep->size) || (ep->actual_len >= ep->total_len)) { 535 // complete if all bytes transferred or short packet 536 pio_usb_ll_transfer_complete(ep, PIO_USB_INTS_ENDPOINT_COMPLETE_BITS); 537 return false; 538 } else { 539 if (ep->is_tx) { 540 prepare_tx_data(ep); 541 } 542 543 return true; 544 } 545 } 546 547 void __no_inline_not_in_flash_func(pio_usb_ll_transfer_complete)( 548 endpoint_t *ep, uint32_t flag) { 549 root_port_t *rport = PIO_USB_ROOT_PORT(ep->root_idx); 550 uint32_t const ep_mask = (1u << (ep - pio_usb_ep_pool)); 551 552 rport->ints |= flag; 553 554 if (flag == PIO_USB_INTS_ENDPOINT_COMPLETE_BITS) { 555 rport->ep_complete |= ep_mask; 556 if (!ep->is_tx) { 557 ep->new_data_flag = true; 558 } 559 } else if (flag == PIO_USB_INTS_ENDPOINT_ERROR_BITS) { 560 rport->ep_error |= ep_mask; 561 } else if (flag == PIO_USB_INTS_ENDPOINT_STALLED_BITS) { 562 rport->ep_stalled |= ep_mask; 563 } else { 564 // something wrong 565 } 566 567 ep->has_transfer = false; 568 } 569 570 int pio_usb_host_add_port(uint8_t pin_dp, PIO_USB_PINOUT pinout) { 571 for (int idx = 0; idx < PIO_USB_ROOT_PORT_CNT; idx++) { 572 root_port_t *root = PIO_USB_ROOT_PORT(idx); 573 if (!root->initialized) { 574 root->pin_dp = pin_dp; 575 576 if (pinout == PIO_USB_PINOUT_DPDM) { 577 root->pin_dm = pin_dp + 1; 578 } else { 579 root->pin_dm = pin_dp - 1; 580 } 581 root->pinout = pinout; 582 583 gpio_pull_down(pin_dp); 584 gpio_pull_down(root->pin_dm); 585 pio_gpio_init(pio_port[0].pio_usb_tx, pin_dp); 586 pio_gpio_init(pio_port[0].pio_usb_tx, root->pin_dm); 587 gpio_set_inover(pin_dp, GPIO_OVERRIDE_INVERT); 588 gpio_set_inover(root->pin_dm, GPIO_OVERRIDE_INVERT); 589 pio_sm_set_pindirs_with_mask64(pio_port[0].pio_usb_tx, pio_port[0].sm_tx, 0, 590 (1ull << pin_dp) | (1ull << root->pin_dm)); 591 port_pin_drive_setting(root); 592 root->initialized = true; 593 594 return 0; 595 } 596 } 597 598 return -1; 599 } 600 601 #pragma GCC pop_options