Skip to content

more dynamic controller for host #1506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/device/usbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ bool tud_init (uint8_t rhport)
// skip if already initialized
if ( tud_inited() ) return true;

TU_LOG2("USBD init rhport %u\r\n", rhport);
TU_LOG2("USBD init on controller %u\r\n", rhport);
TU_LOG2_INT(sizeof(usbd_device_t));

tu_varclr(&_usbd_dev);
Expand Down
36 changes: 20 additions & 16 deletions src/host/usbh.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ typedef struct {
// Invalid driver ID in itf2drv[] ep2drv[][] mapping
enum { DRVID_INVALID = 0xFFu };
enum { ADDR_INVALID = 0xFFu };
enum { CONTROLLER_INVALID = 0xFFu };

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

static bool _usbh_initialized = false;
static uint8_t _usbh_controller = CONTROLLER_INVALID;

// Device with address = 0 for enumeration
static usbh_dev0_t _dev0;
Expand Down Expand Up @@ -280,8 +281,8 @@ void osal_task_delay(uint32_t msec)
{
(void) msec;

const uint32_t start = hcd_frame_number(TUH_OPT_RHPORT);
while ( ( hcd_frame_number(TUH_OPT_RHPORT) - start ) < msec ) {}
const uint32_t start = hcd_frame_number(_usbh_controller);
while ( ( hcd_frame_number(_usbh_controller) - start ) < msec ) {}
}
#endif

Expand Down Expand Up @@ -335,15 +336,15 @@ static void clear_device(usbh_device_t* dev)

bool tuh_inited(void)
{
return _usbh_initialized;
return _usbh_controller != CONTROLLER_INVALID;
}

bool tuh_init(uint8_t rhport)
bool tuh_init(uint8_t controller_id)
{
// skip if already initialized
if (_usbh_initialized) return _usbh_initialized;
if ( tuh_inited() ) return true;

TU_LOG2("USBH init rhport %u\r\n", rhport);
TU_LOG2("USBH init on controller %u\r\n", controller_id);
TU_LOG2_INT(sizeof(usbh_device_t));
TU_LOG2_INT(sizeof(hcd_event_t));
TU_LOG2_INT(sizeof(_ctrl_xfer));
Expand Down Expand Up @@ -376,10 +377,11 @@ bool tuh_init(uint8_t rhport)
usbh_class_drivers[drv_id].init();
}

TU_ASSERT(hcd_init(rhport));
hcd_int_enable(rhport);
_usbh_controller = controller_id;;

TU_ASSERT(hcd_init(controller_id));
hcd_int_enable(controller_id);

_usbh_initialized = true;
return true;
}

Expand Down Expand Up @@ -721,13 +723,13 @@ uint8_t* usbh_get_enum_buf(void)

void usbh_int_set(bool enabled)
{
// TODO all host controller
// TODO all host controller if multiple is used
if (enabled)
{
hcd_int_enable(TUH_OPT_RHPORT);
hcd_int_enable(_usbh_controller);
}else
{
hcd_int_disable(TUH_OPT_RHPORT);
hcd_int_disable(_usbh_controller);
}
}

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

#if 0
case ENUM_RESET_2:
// XXX note used by now, but may be needed for some devices !?
// TODO not used by now, but may be needed for some devices !?
// Reset device again before Set Address
TU_LOG2("Port reset2 \r\n");
if (_dev0.hub_addr == 0)
{
// connected directly to roothub
hcd_port_reset( _dev0.rhport );
osal_task_delay(RESET_DELAY);
osal_task_delay(RESET_DELAY); // TODO may not work for no-OS on MCU that require reset_end() since
// sof of controller may not running while reseting
hcd_port_reset_end(_dev0.rhport);
// TODO: fall through to SET ADDRESS, refactor later
}
Expand Down Expand Up @@ -1364,7 +1367,8 @@ static bool enum_new_device(hcd_event_t* event)
// connected/disconnected directly with roothub
// wait until device is stable TODO non blocking
hcd_port_reset(_dev0.rhport);
osal_task_delay(RESET_DELAY);
osal_task_delay(RESET_DELAY); // TODO may not work for no-OS on MCU that require reset_end() since
// sof of controller may not running while reseting
hcd_port_reset_end( _dev0.rhport);

// device unplugged while delaying
Expand Down
4 changes: 2 additions & 2 deletions src/host/usbh.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ TU_ATTR_WEAK void tuh_umount_cb(uint8_t daddr);
// Should be called before tuh_init()
// - cfg_id : configure ID (TBD)
// - cfg_param: configure data, structure depends on the ID
bool tuh_configure(uint8_t rhport, uint32_t cfg_id, const void* cfg_param);
bool tuh_configure(uint8_t controller_id, uint32_t cfg_id, const void* cfg_param);

// Init host stack
bool tuh_init(uint8_t rhport);
bool tuh_init(uint8_t controller_id);

// Check if host stack is already initialized
bool tuh_inited(void);
Expand Down
7 changes: 4 additions & 3 deletions src/tusb_option.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#define _TUSB_OPTION_H_

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

#include "common/tusb_compiler.h"

Expand Down Expand Up @@ -217,6 +217,9 @@ typedef int make_iso_compilers_happy ;
#define CFG_TUD_MAX_SPEED (TUD_RHPORT_MODE & OPT_MODE_SPEED_MASK)
#endif

// For backward compatible
#define TUSB_OPT_DEVICE_ENABLED CFG_TUD_ENABLED

// highspeed support indicator
#define TUD_OPT_HIGH_SPEED (CFG_TUD_MAX_SPEED ? CFG_TUD_MAX_SPEED : TUP_RHPORT_HIGHSPEED)

Expand All @@ -230,7 +233,6 @@ typedef int make_iso_compilers_happy ;
#define TUH_OPT_RHPORT 1
#else
#define TUH_RHPORT_MODE OPT_MODE_NONE
#define TUH_OPT_RHPORT -1
#endif

#ifndef CFG_TUH_ENABLED
Expand All @@ -244,7 +246,6 @@ typedef int make_iso_compilers_happy ;
#endif

// For backward compatible
#define TUSB_OPT_DEVICE_ENABLED CFG_TUD_ENABLED
#define TUSB_OPT_HOST_ENABLED CFG_TUH_ENABLED

//--------------------------------------------------------------------+
Expand Down