Skip to content

Commit 65e01ff

Browse files
committed
add tuh_edpt_close() API, it will abort any pending transfer
implement hcd_edpt_close() for pio-usb and max3421e, also move max3421e api into its own header.
1 parent b99b811 commit 65e01ff

File tree

8 files changed

+218
-164
lines changed

8 files changed

+218
-164
lines changed

examples/host/bare_api/src/main.c

Lines changed: 114 additions & 140 deletions
Large diffs are not rendered by default.

src/host/hcd.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ void hcd_device_close(uint8_t rhport, uint8_t dev_addr);
165165
// Open an endpoint
166166
bool hcd_edpt_open(uint8_t rhport, uint8_t daddr, tusb_desc_endpoint_t const * ep_desc);
167167

168+
// Close an endpoint
169+
bool hcd_edpt_close(uint8_t rhport, uint8_t daddr, uint8_t ep_addr);
170+
168171
// Submit a transfer, when complete hcd_event_xfer_complete() must be invoked
169172
bool hcd_edpt_xfer(uint8_t rhport, uint8_t daddr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen);
170173

src/host/usbh.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,12 @@ bool tuh_edpt_open(uint8_t dev_addr, tusb_desc_endpoint_t const* desc_ep) {
994994
return hcd_edpt_open(usbh_get_rhport(dev_addr), dev_addr, desc_ep);
995995
}
996996

997+
bool tuh_edpt_close(uint8_t daddr, uint8_t ep_addr) {
998+
TU_VERIFY(0 != tu_edpt_number(ep_addr)); // cannot close EP0
999+
tuh_edpt_abort_xfer(daddr, ep_addr); // abort any pending transfer
1000+
return hcd_edpt_close(usbh_get_rhport(daddr), daddr, ep_addr);
1001+
}
1002+
9971003
bool usbh_edpt_busy(uint8_t dev_addr, uint8_t ep_addr) {
9981004
usbh_device_t* dev = get_device(dev_addr);
9991005
TU_VERIFY(dev);

src/host/usbh.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333

3434
#include "common/tusb_common.h"
3535

36+
#if CFG_TUH_MAX3421
37+
#include "portable/analog/max3421/hcd_max3421.h"
38+
#endif
39+
3640
//--------------------------------------------------------------------+
3741
// MACRO CONSTANT TYPEDEF
3842
//--------------------------------------------------------------------+
@@ -227,6 +231,9 @@ bool tuh_edpt_xfer(tuh_xfer_t* xfer);
227231
// Open a non-control endpoint
228232
bool tuh_edpt_open(uint8_t daddr, tusb_desc_endpoint_t const * desc_ep);
229233

234+
// Close a non-control endpoint, it will abort any pending transfer
235+
bool tuh_edpt_close(uint8_t daddr, uint8_t ep_addr);
236+
230237
// Abort a queued transfer. Note: it can only abort transfer that has not been started
231238
// Return true if a queued transfer is aborted, false if there is no transfer to abort
232239
bool tuh_edpt_abort_xfer(uint8_t daddr, uint8_t ep_addr);

src/portable/analog/max3421/hcd_max3421.c

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -252,28 +252,6 @@ static tuh_configure_max3421_t _tuh_cfg = {
252252
.pinctl = 0, // default: negative edge interrupt
253253
};
254254

255-
//--------------------------------------------------------------------+
256-
// API: SPI transfer with MAX3421E
257-
// - spi_cs_api(), spi_xfer_api(), int_api(): must be implemented by application
258-
// - reg_read(), reg_write(): is implemented by this driver, can be used by application
259-
//--------------------------------------------------------------------+
260-
261-
// API to control MAX3421 SPI CS
262-
extern void tuh_max3421_spi_cs_api(uint8_t rhport, bool active);
263-
264-
// API to transfer data with MAX3421 SPI
265-
// Either tx_buf or rx_buf can be NULL, which means transfer is write or read only
266-
extern bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const* tx_buf, uint8_t* rx_buf, size_t xfer_bytes);
267-
268-
// API to enable/disable MAX3421 INTR pin interrupt
269-
extern void tuh_max3421_int_api(uint8_t rhport, bool enabled);
270-
271-
// API to read MAX3421's register. Implemented by TinyUSB
272-
uint8_t tuh_max3421_reg_read(uint8_t rhport, uint8_t reg, bool in_isr);
273-
274-
// API to write MAX3421's register. Implemented by TinyUSB
275-
bool tuh_max3421_reg_write(uint8_t rhport, uint8_t reg, uint8_t data, bool in_isr);
276-
277255
//--------------------------------------------------------------------+
278256
// SPI Commands and Helper
279257
//--------------------------------------------------------------------+
@@ -632,6 +610,9 @@ bool hcd_edpt_open(uint8_t rhport, uint8_t daddr, tusb_desc_endpoint_t const * e
632610
if (daddr == 0 && ep_num == 0) {
633611
ep = &_hcd_data.ep[0];
634612
}else {
613+
if (NULL != find_ep_not_addr0(daddr, ep_num, ep_dir)) {
614+
return false; // endpoint already opened
615+
}
635616
ep = allocate_ep();
636617
TU_ASSERT(ep);
637618
ep->daddr = daddr;
@@ -645,6 +626,21 @@ bool hcd_edpt_open(uint8_t rhport, uint8_t daddr, tusb_desc_endpoint_t const * e
645626
return true;
646627
}
647628

629+
bool hcd_edpt_close(uint8_t rhport, uint8_t daddr, uint8_t ep_addr) {
630+
(void) rhport;
631+
uint8_t const ep_num = tu_edpt_number(ep_addr);
632+
tusb_dir_t const ep_dir = tu_edpt_dir(ep_addr);
633+
max3421_ep_t * ep = find_ep_not_addr0(daddr, ep_num, ep_dir);
634+
635+
if (!ep) {
636+
return false; // not opened
637+
}
638+
639+
tu_memclr(ep, sizeof(max3421_ep_t));
640+
641+
return true;
642+
}
643+
648644
/* The microcontroller repeatedly writes the SNDFIFO register R2 to load the FIFO with up to 64 data bytes.
649645
* Then the microcontroller writes the SNDBC register, which this does three things:
650646
* 1. Tells the MAX3421E SIE (Serial Interface Engine) how many bytes in the FIFO to send.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2025 Ha Thach (tinyusb.org)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
* This file is part of the TinyUSB stack.
25+
*/
26+
#ifndef TUSB_HCD_MAX3421_H
27+
#define TUSB_HCD_MAX3421_H
28+
29+
#ifdef __cplusplus
30+
extern "C" {
31+
#endif
32+
33+
//--------------------------------------------------------------------+
34+
// SPI transfer API with MAX3421E are implemented by application
35+
// - spi_cs_api(), spi_xfer_api(), int_api()
36+
//--------------------------------------------------------------------+
37+
38+
// API to control MAX3421 SPI CS
39+
extern void tuh_max3421_spi_cs_api(uint8_t rhport, bool active);
40+
41+
// API to transfer data with MAX3421 SPI
42+
// Either tx_buf or rx_buf can be NULL, which means transfer is write or read only
43+
extern bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const* tx_buf, uint8_t* rx_buf, size_t xfer_bytes);
44+
45+
// API to enable/disable MAX3421 INTR pin interrupt
46+
extern void tuh_max3421_int_api(uint8_t rhport, bool enabled);
47+
48+
//--------------------------------------------------------------------+
49+
// API for read/write MAX3421 registers
50+
// are implemented by this driver, can be used by application
51+
//--------------------------------------------------------------------+
52+
53+
// API to read MAX3421's register. Implemented by TinyUSB
54+
uint8_t tuh_max3421_reg_read(uint8_t rhport, uint8_t reg, bool in_isr);
55+
56+
// API to write MAX3421's register. Implemented by TinyUSB
57+
bool tuh_max3421_reg_write(uint8_t rhport, uint8_t reg, uint8_t data, bool in_isr);
58+
59+
#ifdef __cplusplus
60+
}
61+
#endif
62+
63+
#endif

src/portable/raspberrypi/pio_usb/hcd_pio_usb.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const
122122
return pio_usb_host_endpoint_open(pio_rhport, dev_addr, (uint8_t const *) desc_ep, need_pre);
123123
}
124124

125+
bool hcd_edpt_close(uint8_t rhport, uint8_t daddr, uint8_t ep_addr) {
126+
uint8_t const pio_rhport = RHPORT_PIO(rhport);
127+
return pio_usb_host_endpoint_close(pio_rhport, daddr, ep_addr);
128+
}
129+
125130
bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t *buffer, uint16_t buflen) {
126131
uint8_t const pio_rhport = RHPORT_PIO(rhport);
127132
return pio_usb_host_endpoint_transfer(pio_rhport, dev_addr, ep_addr, buffer, buflen);

tools/get_deps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@
5858
'hw/mcu/nxp/mcux-sdk': ['https://github.com/hathach/mcux-sdk.git',
5959
'144f1eb7ea8c06512e12f12b27383601c0272410',
6060
'kinetis_k kinetis_k32l2 kinetis_kl lpc51 lpc54 lpc55 mcx imxrt'],
61-
'hw/mcu/raspberry_pi/Pico-PIO-USB': ['https://github.com/sekigon-gonnoc/Pico-PIO-USB.git',
62-
'0ca3657d55ea20e7fa4483bbd21ce951bc1d6fa5',
61+
'hw/mcu/raspberry_pi/Pico-PIO-USB': ['https://github.com/hathach/Pico-PIO-USB.git',
62+
'810653f66adadba3e0e4b4b56d5167ac4f7fdbf7',
6363
'rp2040'],
6464
'hw/mcu/renesas/fsp': ['https://github.com/renesas/fsp.git',
6565
'edcc97d684b6f716728a60d7a6fea049d9870bd6',

0 commit comments

Comments
 (0)