more dynamic controller for host

This commit is contained in:
hathach 2022-06-10 14:53:03 +07:00
parent 96fecedabe
commit 040ef0640d
4 changed files with 27 additions and 22 deletions

View File

@ -384,7 +384,7 @@ bool tud_init (uint8_t rhport)
// skip if already initialized // skip if already initialized
if ( tud_inited() ) return true; 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_LOG2_INT(sizeof(usbd_device_t));
tu_varclr(&_usbd_dev); tu_varclr(&_usbd_dev);

View File

@ -125,6 +125,7 @@ typedef struct {
// Invalid driver ID in itf2drv[] ep2drv[][] mapping // Invalid driver ID in itf2drv[] ep2drv[][] mapping
enum { DRVID_INVALID = 0xFFu }; enum { DRVID_INVALID = 0xFFu };
enum { ADDR_INVALID = 0xFFu }; enum { ADDR_INVALID = 0xFFu };
enum { CONTROLLER_INVALID = 0xFFu };
#if CFG_TUSB_DEBUG >= 2 #if CFG_TUSB_DEBUG >= 2
#define DRIVER_NAME(_name) .name = _name, #define DRIVER_NAME(_name) .name = _name,
@ -203,7 +204,7 @@ enum { CONFIG_NUM = 1 }; // default to use configuration 1
// sum of end device + hub // sum of end device + hub
#define TOTAL_DEVICES (CFG_TUH_DEVICE_MAX + CFG_TUH_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 // Device with address = 0 for enumeration
static usbh_dev0_t _dev0; static usbh_dev0_t _dev0;
@ -280,8 +281,8 @@ void osal_task_delay(uint32_t msec)
{ {
(void) msec; (void) msec;
const uint32_t start = hcd_frame_number(TUH_OPT_RHPORT); const uint32_t start = hcd_frame_number(_usbh_controller);
while ( ( hcd_frame_number(TUH_OPT_RHPORT) - start ) < msec ) {} while ( ( hcd_frame_number(_usbh_controller) - start ) < msec ) {}
} }
#endif #endif
@ -335,15 +336,15 @@ static void clear_device(usbh_device_t* dev)
bool tuh_inited(void) 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 // 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(usbh_device_t));
TU_LOG2_INT(sizeof(hcd_event_t)); TU_LOG2_INT(sizeof(hcd_event_t));
TU_LOG2_INT(sizeof(_ctrl_xfer)); TU_LOG2_INT(sizeof(_ctrl_xfer));
@ -376,10 +377,11 @@ bool tuh_init(uint8_t rhport)
usbh_class_drivers[drv_id].init(); usbh_class_drivers[drv_id].init();
} }
TU_ASSERT(hcd_init(rhport)); _usbh_controller = controller_id;;
hcd_int_enable(rhport);
TU_ASSERT(hcd_init(controller_id));
hcd_int_enable(controller_id);
_usbh_initialized = true;
return true; return true;
} }
@ -721,13 +723,13 @@ uint8_t* usbh_get_enum_buf(void)
void usbh_int_set(bool enabled) void usbh_int_set(bool enabled)
{ {
// TODO all host controller // TODO all host controller if multiple is used
if (enabled) if (enabled)
{ {
hcd_int_enable(TUH_OPT_RHPORT); hcd_int_enable(_usbh_controller);
}else }else
{ {
hcd_int_disable(TUH_OPT_RHPORT); hcd_int_disable(_usbh_controller);
} }
} }
@ -1238,14 +1240,15 @@ static void process_enumeration(tuh_xfer_t* xfer)
#if 0 #if 0
case ENUM_RESET_2: 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 // Reset device again before Set Address
TU_LOG2("Port reset2 \r\n"); TU_LOG2("Port reset2 \r\n");
if (_dev0.hub_addr == 0) if (_dev0.hub_addr == 0)
{ {
// connected directly to roothub // connected directly to roothub
hcd_port_reset( _dev0.rhport ); 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); hcd_port_reset_end(_dev0.rhport);
// TODO: fall through to SET ADDRESS, refactor later // TODO: fall through to SET ADDRESS, refactor later
} }
@ -1364,7 +1367,8 @@ static bool enum_new_device(hcd_event_t* event)
// connected/disconnected directly with roothub // connected/disconnected directly with roothub
// wait until device is stable TODO non blocking // wait until device is stable TODO non blocking
hcd_port_reset(_dev0.rhport); 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); hcd_port_reset_end( _dev0.rhport);
// device unplugged while delaying // device unplugged while delaying

View File

@ -95,10 +95,10 @@ TU_ATTR_WEAK void tuh_umount_cb(uint8_t daddr);
// Should be called before tuh_init() // Should be called before tuh_init()
// - cfg_id : configure ID (TBD) // - cfg_id : configure ID (TBD)
// - cfg_param: configure data, structure depends on the ID // - 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 // Init host stack
bool tuh_init(uint8_t rhport); bool tuh_init(uint8_t controller_id);
// Check if host stack is already initialized // Check if host stack is already initialized
bool tuh_inited(void); bool tuh_inited(void);

View File

@ -28,7 +28,7 @@
#define _TUSB_OPTION_H_ #define _TUSB_OPTION_H_
// To avoid GCC compiler warnings when -pedantic option is used (strict ISO C) // 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" #include "common/tusb_compiler.h"
@ -217,6 +217,9 @@ typedef int make_iso_compilers_happy ;
#define CFG_TUD_MAX_SPEED (TUD_RHPORT_MODE & OPT_MODE_SPEED_MASK) #define CFG_TUD_MAX_SPEED (TUD_RHPORT_MODE & OPT_MODE_SPEED_MASK)
#endif #endif
// For backward compatible
#define TUSB_OPT_DEVICE_ENABLED CFG_TUD_ENABLED
// highspeed support indicator // highspeed support indicator
#define TUD_OPT_HIGH_SPEED (CFG_TUD_MAX_SPEED ? CFG_TUD_MAX_SPEED : TUP_RHPORT_HIGHSPEED) #define TUD_OPT_HIGH_SPEED (CFG_TUD_MAX_SPEED ? CFG_TUD_MAX_SPEED : TUP_RHPORT_HIGHSPEED)
@ -230,7 +233,6 @@ typedef int make_iso_compilers_happy ;
#define TUH_OPT_RHPORT 1 #define TUH_OPT_RHPORT 1
#else #else
#define TUH_RHPORT_MODE OPT_MODE_NONE #define TUH_RHPORT_MODE OPT_MODE_NONE
#define TUH_OPT_RHPORT -1
#endif #endif
#ifndef CFG_TUH_ENABLED #ifndef CFG_TUH_ENABLED
@ -244,7 +246,6 @@ typedef int make_iso_compilers_happy ;
#endif #endif
// For backward compatible // For backward compatible
#define TUSB_OPT_DEVICE_ENABLED CFG_TUD_ENABLED
#define TUSB_OPT_HOST_ENABLED CFG_TUH_ENABLED #define TUSB_OPT_HOST_ENABLED CFG_TUH_ENABLED
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+