Skip to content

Commit 040ef06

Browse files
committed
more dynamic controller for host
1 parent 96feced commit 040ef06

File tree

4 files changed

+27
-22
lines changed

4 files changed

+27
-22
lines changed

src/device/usbd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ bool tud_init (uint8_t rhport)
384384
// skip if already initialized
385385
if ( tud_inited() ) return true;
386386

387-
TU_LOG2("USBD init rhport %u\r\n", rhport);
387+
TU_LOG2("USBD init on controller %u\r\n", rhport);
388388
TU_LOG2_INT(sizeof(usbd_device_t));
389389

390390
tu_varclr(&_usbd_dev);

src/host/usbh.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ typedef struct {
125125
// Invalid driver ID in itf2drv[] ep2drv[][] mapping
126126
enum { DRVID_INVALID = 0xFFu };
127127
enum { ADDR_INVALID = 0xFFu };
128+
enum { CONTROLLER_INVALID = 0xFFu };
128129

129130
#if CFG_TUSB_DEBUG >= 2
130131
#define DRIVER_NAME(_name) .name = _name,
@@ -203,7 +204,7 @@ enum { CONFIG_NUM = 1 }; // default to use configuration 1
203204
// sum of end device + hub
204205
#define TOTAL_DEVICES (CFG_TUH_DEVICE_MAX + CFG_TUH_HUB)
205206

206-
static bool _usbh_initialized = false;
207+
static uint8_t _usbh_controller = CONTROLLER_INVALID;
207208

208209
// Device with address = 0 for enumeration
209210
static usbh_dev0_t _dev0;
@@ -280,8 +281,8 @@ void osal_task_delay(uint32_t msec)
280281
{
281282
(void) msec;
282283

283-
const uint32_t start = hcd_frame_number(TUH_OPT_RHPORT);
284-
while ( ( hcd_frame_number(TUH_OPT_RHPORT) - start ) < msec ) {}
284+
const uint32_t start = hcd_frame_number(_usbh_controller);
285+
while ( ( hcd_frame_number(_usbh_controller) - start ) < msec ) {}
285286
}
286287
#endif
287288

@@ -335,15 +336,15 @@ static void clear_device(usbh_device_t* dev)
335336

336337
bool tuh_inited(void)
337338
{
338-
return _usbh_initialized;
339+
return _usbh_controller != CONTROLLER_INVALID;
339340
}
340341

341-
bool tuh_init(uint8_t rhport)
342+
bool tuh_init(uint8_t controller_id)
342343
{
343344
// skip if already initialized
344-
if (_usbh_initialized) return _usbh_initialized;
345+
if ( tuh_inited() ) return true;
345346

346-
TU_LOG2("USBH init rhport %u\r\n", rhport);
347+
TU_LOG2("USBH init on controller %u\r\n", controller_id);
347348
TU_LOG2_INT(sizeof(usbh_device_t));
348349
TU_LOG2_INT(sizeof(hcd_event_t));
349350
TU_LOG2_INT(sizeof(_ctrl_xfer));
@@ -376,10 +377,11 @@ bool tuh_init(uint8_t rhport)
376377
usbh_class_drivers[drv_id].init();
377378
}
378379

379-
TU_ASSERT(hcd_init(rhport));
380-
hcd_int_enable(rhport);
380+
_usbh_controller = controller_id;;
381+
382+
TU_ASSERT(hcd_init(controller_id));
383+
hcd_int_enable(controller_id);
381384

382-
_usbh_initialized = true;
383385
return true;
384386
}
385387

@@ -721,13 +723,13 @@ uint8_t* usbh_get_enum_buf(void)
721723

722724
void usbh_int_set(bool enabled)
723725
{
724-
// TODO all host controller
726+
// TODO all host controller if multiple is used
725727
if (enabled)
726728
{
727-
hcd_int_enable(TUH_OPT_RHPORT);
729+
hcd_int_enable(_usbh_controller);
728730
}else
729731
{
730-
hcd_int_disable(TUH_OPT_RHPORT);
732+
hcd_int_disable(_usbh_controller);
731733
}
732734
}
733735

@@ -1238,14 +1240,15 @@ static void process_enumeration(tuh_xfer_t* xfer)
12381240

12391241
#if 0
12401242
case ENUM_RESET_2:
1241-
// XXX note used by now, but may be needed for some devices !?
1243+
// TODO not used by now, but may be needed for some devices !?
12421244
// Reset device again before Set Address
12431245
TU_LOG2("Port reset2 \r\n");
12441246
if (_dev0.hub_addr == 0)
12451247
{
12461248
// connected directly to roothub
12471249
hcd_port_reset( _dev0.rhport );
1248-
osal_task_delay(RESET_DELAY);
1250+
osal_task_delay(RESET_DELAY); // TODO may not work for no-OS on MCU that require reset_end() since
1251+
// sof of controller may not running while reseting
12491252
hcd_port_reset_end(_dev0.rhport);
12501253
// TODO: fall through to SET ADDRESS, refactor later
12511254
}
@@ -1364,7 +1367,8 @@ static bool enum_new_device(hcd_event_t* event)
13641367
// connected/disconnected directly with roothub
13651368
// wait until device is stable TODO non blocking
13661369
hcd_port_reset(_dev0.rhport);
1367-
osal_task_delay(RESET_DELAY);
1370+
osal_task_delay(RESET_DELAY); // TODO may not work for no-OS on MCU that require reset_end() since
1371+
// sof of controller may not running while reseting
13681372
hcd_port_reset_end( _dev0.rhport);
13691373

13701374
// device unplugged while delaying

src/host/usbh.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ TU_ATTR_WEAK void tuh_umount_cb(uint8_t daddr);
9595
// Should be called before tuh_init()
9696
// - cfg_id : configure ID (TBD)
9797
// - cfg_param: configure data, structure depends on the ID
98-
bool tuh_configure(uint8_t rhport, uint32_t cfg_id, const void* cfg_param);
98+
bool tuh_configure(uint8_t controller_id, uint32_t cfg_id, const void* cfg_param);
9999

100100
// Init host stack
101-
bool tuh_init(uint8_t rhport);
101+
bool tuh_init(uint8_t controller_id);
102102

103103
// Check if host stack is already initialized
104104
bool tuh_inited(void);

src/tusb_option.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#define _TUSB_OPTION_H_
2929

3030
// To avoid GCC compiler warnings when -pedantic option is used (strict ISO C)
31-
typedef int make_iso_compilers_happy ;
31+
typedef int make_iso_compilers_happy;
3232

3333
#include "common/tusb_compiler.h"
3434

@@ -217,6 +217,9 @@ typedef int make_iso_compilers_happy ;
217217
#define CFG_TUD_MAX_SPEED (TUD_RHPORT_MODE & OPT_MODE_SPEED_MASK)
218218
#endif
219219

220+
// For backward compatible
221+
#define TUSB_OPT_DEVICE_ENABLED CFG_TUD_ENABLED
222+
220223
// highspeed support indicator
221224
#define TUD_OPT_HIGH_SPEED (CFG_TUD_MAX_SPEED ? CFG_TUD_MAX_SPEED : TUP_RHPORT_HIGHSPEED)
222225

@@ -230,7 +233,6 @@ typedef int make_iso_compilers_happy ;
230233
#define TUH_OPT_RHPORT 1
231234
#else
232235
#define TUH_RHPORT_MODE OPT_MODE_NONE
233-
#define TUH_OPT_RHPORT -1
234236
#endif
235237

236238
#ifndef CFG_TUH_ENABLED
@@ -244,7 +246,6 @@ typedef int make_iso_compilers_happy ;
244246
#endif
245247

246248
// For backward compatible
247-
#define TUSB_OPT_DEVICE_ENABLED CFG_TUD_ENABLED
248249
#define TUSB_OPT_HOST_ENABLED CFG_TUH_ENABLED
249250

250251
//--------------------------------------------------------------------+

0 commit comments

Comments
 (0)