Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Implementation of UHC

Internal implementation.

Functions

static void uhc_remotewakeup (bool b_enable)
 Enables or disables the remote wakeup feature of all devices connected. More...
 
static void uhc_setup_request_callback (usb_add_t add, uhd_trans_status_t status, uint16_t payload_trans)
 Callback used to signal the end of a setup request. More...
 

Internal variables to manage the USB host stack

typedef void(* uhc_sof_timeout_callback_t )(void)
 Type of callback on a SOF timeout. More...
 
static uhc_device_t g_uhc_device_root
 Entry point of all devices connected on USB tree. More...
 
static uint8_t uhc_enum_try
 Number of enumeration try. More...
 
static uhc_sof_timeout_callback_t uhc_sof_timeout_callback
 Callback currently registered on a SOF timeout. More...
 
uint8_t uhc_sof_timeout
 Number of SOF remaining before call uhc_sof_timeout_callback callback. More...
 
static uhi_api_t uhc_uhis [] = {USB_HOST_UHI}
 Array of all UHI available. More...
 
static volatile bool uhc_setup_request_finish
 Volatile flag to pool the end of Get USB string setup request. More...
 
static volatile bool uhc_setup_request_finish_status
 Volatile flag to know the status of Get USB string setup request. More...
 
#define UHC_ENUM_NB_TRY   4
 Maximum try to enumerate a device. More...
 
#define UHC_USB_ADD_NOT_VALID   0xFF
 Entry point of all devices connected on USB tree. More...
 
#define UHC_DEVICE_ENUM_ADD   1
 USB address of the USB device under enumeration process. More...
 
#define uhc_dev_enum   (&g_uhc_device_root)
 Device under enumeration process. More...
 
#define uhc_power_running   0
 Total power of the devices connected. More...
 
#define UHC_NB_UHI   (sizeof(uhc_uhis)/sizeof(uhc_uhis[0]))
 Number of UHI available. More...
 

Internal functions to manage the USB device enumeration

static void uhc_enable_timeout_callback (uint8_t timeout, uhc_sof_timeout_callback_t callback)
 Enable a internal timeout on SOF events. More...
 
static void uhc_enumeration_suspend (void)
 Enters a specific device in USB suspend mode Suspend the USB line or a port on USB hub. More...
 
static void uhc_enumeration_reset (uhd_callback_reset_t callback)
 Sends the USB Reset signal on the USB line of a device. More...
 
static void uhc_connection_tree (bool b_plug, uhc_device_t *dev)
 Manage a device plug or unplug on the USB tree. More...
 
static void uhc_enumeration_step1 (void)
 Device enumeration step 1 Reset USB line. More...
 
static void uhc_enumeration_step2 (void)
 Device enumeration step 2 Lets USB line in IDLE state during 20ms. More...
 
static void uhc_enumeration_step3 (void)
 Device enumeration step 3 Reset USB line. More...
 
static void uhc_enumeration_step4 (void)
 Device enumeration step 4 Lets USB line in IDLE state during 100ms. More...
 
static void uhc_enumeration_step5 (void)
 Device enumeration step 5 Requests the USB device descriptor. More...
 
static void uhc_enumeration_step6 (usb_add_t add, uhd_trans_status_t status, uint16_t payload_trans)
 Device enumeration step 6 End of Get device descriptor request. More...
 
static void uhc_enumeration_step7 (void)
 Device enumeration step 7 Reset USB line. More...
 
static void uhc_enumeration_step8 (void)
 Device enumeration step 8 Lets USB line in IDLE state during 100ms. More...
 
static void uhc_enumeration_step9 (void)
 Device enumeration step 9 Send a Set address setup request. More...
 
static void uhc_enumeration_step10 (usb_add_t add, uhd_trans_status_t status, uint16_t payload_trans)
 Device enumeration step 10 End of Set address request. More...
 
static void uhc_enumeration_step11 (void)
 Device enumeration step 11 Updates USB host pipe with the new USB address. More...
 
static void uhc_enumeration_step12 (usb_add_t add, uhd_trans_status_t status, uint16_t payload_trans)
 Device enumeration step 12 Requests the first USB structure of the USB configuration descriptor. More...
 
static void uhc_enumeration_step13 (usb_add_t add, uhd_trans_status_t status, uint16_t payload_trans)
 Device enumeration step 13 Requests a complete Get configuration descriptor. More...
 
static void uhc_enumeration_step14 (usb_add_t add, uhd_trans_status_t status, uint16_t payload_trans)
 Device enumeration step 14 Enable USB configuration, if unless one USB interface is supported by UHIs. More...
 
static void uhc_enumeration_step15 (usb_add_t add, uhd_trans_status_t status, uint16_t payload_trans)
 Device enumeration step 15 Enables UHI interfaces. More...
 
static void uhc_enumeration_error (uhc_enum_status_t status)
 Manage error during device enumeration. More...
 

Callbacks used by USB Host Driver (UHD) to notify events

void uhc_notify_connection (bool b_plug)
 Notify device connection or disconnection. More...
 
void uhc_notify_sof (bool b_micro)
 Notify each start of frame sent by driver. More...
 
void uhc_notify_resume (void)
 Notify that a resume bus occurs A resume can occur after a downstream or an upstream resume. More...
 

Functions to control the USB host stack

void uhc_start (void)
 Starts the host mode. More...
 
void uhc_stop (bool b_id_stop)
 Stops the host mode. More...
 
void uhc_suspend (bool b_remotewakeup)
 Suspends a USB line. More...
 
bool uhc_is_suspend (void)
 Test if the suspend state is enabled on the USB line. More...
 
void uhc_resume (void)
 Resumes the USB line. More...
 

User functions to manage the devices

uint8_t uhc_get_device_number (void)
 Returns the number of connected devices. More...
 
char * uhc_dev_get_string_manufacturer (uhc_device_t *dev)
 Gets the USB string manufacturer from a USB device. More...
 
char * uhc_dev_get_string_product (uhc_device_t *dev)
 Gets the USB string product from a USB device. More...
 
char * uhc_dev_get_string_serial (uhc_device_t *dev)
 Gets the USB string serial from a USB device. More...
 
char * uhc_dev_get_string (uhc_device_t *dev, uint8_t str_id)
 Gets a USB string from a USB device. More...
 
uint16_t uhc_dev_get_power (uhc_device_t *dev)
 Gets the maximum consumption of a device (mA) More...
 
uhd_speed_t uhc_dev_get_speed (uhc_device_t *dev)
 Returns the current device speed. More...
 
bool uhc_dev_is_high_speed_support (uhc_device_t *dev)
 Tests if the device supports the USB high speed This function can wait the end of a setup request and the timing can be long (1ms to 5s). More...
 
void uhc_dev_reset (uhc_device_t *dev)
 Reset to re-enumerate. More...
 

#define uhc_dev_enum   (&g_uhc_device_root)

Device under enumeration process.

Referenced by uhc_enumeration_step13().

#define UHC_DEVICE_ENUM_ADD   1

USB address of the USB device under enumeration process.

Referenced by uhc_enumeration_step11(), uhc_enumeration_step12(), uhc_enumeration_step13(), uhc_enumeration_step14(), and uhc_enumeration_step9().

#define UHC_ENUM_NB_TRY   4

Maximum try to enumerate a device.

Referenced by uhc_enumeration_error().

#define UHC_NB_UHI   (sizeof(uhc_uhis)/sizeof(uhc_uhis[0]))
#define uhc_power_running   0

Total power of the devices connected.

Referenced by uhc_connection_tree(), uhc_enumeration_step13(), and uhc_notify_connection().

#define UHC_USB_ADD_NOT_VALID   0xFF

Entry point of all devices connected on USB tree.

Referenced by uhc_connection_tree(), uhc_get_device_number(), uhc_is_suspend(), uhc_notify_connection(), and uhc_start().

typedef void(* uhc_sof_timeout_callback_t)(void)

Type of callback on a SOF timeout.

static void uhc_connection_tree ( bool  b_plug,
uhc_device_t dev 
)
static

Manage a device plug or unplug on the USB tree.

Parameters
b_plugtrue, if it is a device connection
devInformation about device connected or disconnected

References uhc_device_t::address, uhc_device_t::conf_desc, dev, NULL, UHC_CONNECTION_EVENT, uhc_enum_try, uhc_enumeration_step1(), UHC_NB_UHI, uhc_power_running, uhc_sof_timeout, UHC_USB_ADD_NOT_VALID, uhd_ep_free(), and uhi_api_t::uninstall.

Referenced by uhc_notify_connection().

uint16_t uhc_dev_get_power ( uhc_device_t dev)

Gets the maximum consumption of a device (mA)

Parameters
devDevice to request
Returns
Maximum consumption of the device (mA)

References usb_conf_desc_t::bMaxPower, uhc_device_t::conf_desc, and NULL.

uhd_speed_t uhc_dev_get_speed ( uhc_device_t dev)

Returns the current device speed.

Parameters
devDevice to request
Returns
Device speed

References uhc_device_t::speed.

char* uhc_dev_get_string ( uhc_device_t dev,
uint8_t  str_id 
)

Gets a USB string from a USB device.

This function waits the end of setup requests and the timing can be long (3ms to 15s). Thus, do not call it in an interrupt routine. This function allocates a buffer which must be free by user application.

Parameters
devDevice to request
str_idString ID requested
Returns
Pointer on unicode string, or NULL if function fails.

References usb_str_desc_t::bLength, usb_setup_req_t::bmRequestType, usb_setup_req_t::bRequest, le16_to_cpu, NULL, usb_str_lgid_desc_t::string, uhc_setup_request_callback(), uhc_setup_request_finish, uhc_setup_request_finish_status, uhd_setup_request(), UNUSED, USB_DT_STRING, USB_REQ_DIR_IN, USB_REQ_GET_DESCRIPTOR, USB_REQ_RECIP_DEVICE, USB_REQ_TYPE_STANDARD, usb_setup_req_t::wIndex, usb_setup_req_t::wLength, and usb_setup_req_t::wValue.

Referenced by uhc_dev_get_string_manufacturer(), uhc_dev_get_string_product(), and uhc_dev_get_string_serial().

char* uhc_dev_get_string_manufacturer ( uhc_device_t dev)

Gets the USB string manufacturer from a USB device.

This function waits the end of setup requests and the timing can be long (3ms to 15s). Thus, do not call it in an interrupt routine. This function allocates a buffer which must be free by user application.

Parameters
devDevice to request
Returns
Pointer on unicode string, or NULL if function fails.

References uhc_device_t::dev_desc, usb_dev_desc_t::iManufacturer, NULL, and uhc_dev_get_string().

char* uhc_dev_get_string_product ( uhc_device_t dev)

Gets the USB string product from a USB device.

This function waits the end of setup requests and the timing can be long (3ms to 15s). Thus, do not call it in an interrupt routine. This function allocates a buffer which must be free by user application.

Parameters
devDevice to request
Returns
Pointer on unicode string, or NULL if function fails.

References uhc_device_t::dev_desc, usb_dev_desc_t::iProduct, NULL, and uhc_dev_get_string().

char* uhc_dev_get_string_serial ( uhc_device_t dev)

Gets the USB string serial from a USB device.

This function waits the end of setup requests and the timing can be long (3ms to 15s). Thus, do not call it in an interrupt routine. This function allocates a buffer which must be free by user application.

Parameters
devDevice to request
Returns
Pointer on unicode string, or NULL if function fails.

References uhc_device_t::dev_desc, usb_dev_desc_t::iSerialNumber, NULL, and uhc_dev_get_string().

bool uhc_dev_is_high_speed_support ( uhc_device_t dev)

Tests if the device supports the USB high speed This function can wait the end of a setup request and the timing can be long (1ms to 5s).

Thus, do not call it in an interrupt routine.

Parameters
devDevice to request
Returns
True, if high speed is supported

References usb_setup_req_t::bmRequestType, usb_setup_req_t::bRequest, NULL, uhc_device_t::speed, uhc_setup_request_callback(), uhc_setup_request_finish, uhc_setup_request_finish_status, uhd_setup_request(), UHD_SPEED_FULL, UHD_SPEED_HIGH, USB_DT_DEVICE_QUALIFIER, USB_REQ_DIR_IN, USB_REQ_GET_DESCRIPTOR, USB_REQ_RECIP_DEVICE, USB_REQ_TYPE_STANDARD, usb_setup_req_t::wIndex, usb_setup_req_t::wLength, and usb_setup_req_t::wValue.

void uhc_dev_reset ( uhc_device_t dev)

Reset to re-enumerate.

Parameters
devDevice to request

References uhc_device_t::conf_desc, dev, NULL, uhc_enum_try, uhc_enumeration_step3(), UHC_NB_UHI, and uhi_api_t::uninstall.

static void uhc_enable_timeout_callback ( uint8_t  timeout,
uhc_sof_timeout_callback_t  callback 
)
static

Enable a internal timeout on SOF events.

Parameters
timeoutvalue of timeout (ms)
callbackCallback to call at the end of timeout

References callback, uhc_sof_timeout, and uhc_sof_timeout_callback.

Referenced by uhc_enumeration_step10(), uhc_enumeration_step2(), uhc_enumeration_step4(), uhc_enumeration_step6(), and uhc_enumeration_step8().

static void uhc_enumeration_reset ( uhd_callback_reset_t  callback)
static

Sends the USB Reset signal on the USB line of a device.

Parameters
callbackCallback to call at the end of Reset signal

References uhd_send_reset().

Referenced by uhc_enumeration_step1(), uhc_enumeration_step3(), and uhc_enumeration_step7().

static void uhc_enumeration_step1 ( void  )
static

Device enumeration step 1 Reset USB line.

References uhc_enumeration_reset(), and uhc_enumeration_step2().

Referenced by uhc_connection_tree(), and uhc_enumeration_error().

static void uhc_enumeration_step10 ( usb_add_t  add,
uhd_trans_status_t  status,
uint16_t  payload_trans 
)
static

Device enumeration step 10 End of Set address request.

Lets USB line in IDLE state during 20ms.

Parameters
addUSB address of the setup request
statusTransfer status
payload_transNumber of data transfered during DATA phase

References uhc_enable_timeout_callback(), UHC_ENUM_DISCONNECT, UHC_ENUM_FAIL, uhc_enumeration_error(), uhc_enumeration_step11(), UHD_TRANS_DISCONNECT, UHD_TRANS_NOERROR, and UNUSED.

Referenced by uhc_enumeration_step9().

static void uhc_enumeration_step12 ( usb_add_t  add,
uhd_trans_status_t  status,
uint16_t  payload_trans 
)
static
static void uhc_enumeration_step15 ( usb_add_t  add,
uhd_trans_status_t  status,
uint16_t  payload_trans 
)
static

Device enumeration step 15 Enables UHI interfaces.

Parameters
addUSB address of the setup request
statusTransfer status
payload_transNumber of data transfered during DATA phase

References usb_dev_desc_t::bcdUSB, uhc_device_t::dev_desc, uhi_api_t::enable, LE16, NULL, UHC_ENUM_DISCONNECT, UHC_ENUM_EVENT, UHC_ENUM_FAIL, UHC_ENUM_SUCCESS, uhc_enum_try, uhc_enumeration_error(), UHC_NB_UHI, UHD_TRANS_DISCONNECT, UHD_TRANS_NOERROR, uhi_api_t::uninstall, UNUSED, and USB_V2_1.

Referenced by uhc_enumeration_step14().

static void uhc_enumeration_step2 ( void  )
static

Device enumeration step 2 Lets USB line in IDLE state during 20ms.

References uhc_enable_timeout_callback(), and uhc_enumeration_step3().

Referenced by uhc_enumeration_step1().

static void uhc_enumeration_step3 ( void  )
static

Device enumeration step 3 Reset USB line.

References uhc_enumeration_reset(), and uhc_enumeration_step4().

Referenced by uhc_dev_reset(), and uhc_enumeration_step2().

static void uhc_enumeration_step4 ( void  )
static

Device enumeration step 4 Lets USB line in IDLE state during 100ms.

References uhc_device_t::speed, uhc_enable_timeout_callback(), uhc_enumeration_step5(), and uhd_get_speed().

Referenced by uhc_enumeration_step3().

static void uhc_enumeration_step6 ( usb_add_t  add,
uhd_trans_status_t  status,
uint16_t  payload_trans 
)
static

Device enumeration step 6 End of Get device descriptor request.

Wait 20ms in IDLE state.

Parameters
addUSB address of the setup request
statusTransfer status
payload_transNumber of data transfered during DATA phase

References usb_dev_desc_t::bDescriptorType, uhc_device_t::dev_desc, uhc_enable_timeout_callback(), UHC_ENUM_DISCONNECT, UHC_ENUM_FAIL, uhc_enumeration_error(), uhc_enumeration_step7(), UHD_TRANS_DISCONNECT, UHD_TRANS_NOERROR, UNUSED, and USB_DT_DEVICE.

Referenced by uhc_enumeration_step5().

static void uhc_enumeration_step7 ( void  )
static

Device enumeration step 7 Reset USB line.

References uhc_enumeration_reset(), and uhc_enumeration_step8().

Referenced by uhc_enumeration_step6().

static void uhc_enumeration_step8 ( void  )
static

Device enumeration step 8 Lets USB line in IDLE state during 100ms.

References uhc_enable_timeout_callback(), and uhc_enumeration_step9().

Referenced by uhc_enumeration_step7().

static void uhc_enumeration_suspend ( void  )
static

Enters a specific device in USB suspend mode Suspend the USB line or a port on USB hub.

References uhd_suspend().

Referenced by uhc_enumeration_error(), uhc_enumeration_step13(), and uhc_enumeration_step14().

uint8_t uhc_get_device_number ( void  )

Returns the number of connected devices.

Returns
Number of device connected on USB tree

References uhc_device_t::address, dev, g_uhc_device_root, NULL, and UHC_USB_ADD_NOT_VALID.

bool uhc_is_suspend ( void  )

Test if the suspend state is enabled on the USB line.

Returns
USB line in SUSPEND state or device not connected, if true

References uhc_device_t::address, UHC_USB_ADD_NOT_VALID, and uhd_is_suspend().

Referenced by uhc_resume().

void uhc_notify_connection ( bool  b_plug)

Notify device connection or disconnection.

Parameters
b_plugDevice connection, if true

References uhc_device_t::address, Assert, uhc_connection_tree(), uhc_power_running, and UHC_USB_ADD_NOT_VALID.

Referenced by uhd_disable(), and uhd_interrupt().

void uhc_notify_resume ( void  )

Notify that a resume bus occurs A resume can occur after a downstream or an upstream resume.

References uhc_remotewakeup(), and UHC_WAKEUP_EVENT.

Referenced by uhd_delayed_resume().

void uhc_notify_sof ( bool  b_micro)

Notify each start of frame sent by driver.

Parameters
b_microIt is a micro start of frame, if true

References NULL, uhi_api_t::sof_notify, UHC_NB_UHI, uhc_sof_timeout, and uhc_sof_timeout_callback.

Referenced by uhd_sof_interrupt().

void uhc_resume ( void  )

Resumes the USB line.

References uhc_is_suspend(), and uhd_resume().

static void uhc_setup_request_callback ( usb_add_t  add,
uhd_trans_status_t  status,
uint16_t  payload_trans 
)
static

Callback used to signal the end of a setup request.

Parameters
addUSB address of the setup request
statusTransfer status
payload_transNumber of data transfered during DATA phase

References uhc_setup_request_finish, uhc_setup_request_finish_status, UHD_TRANS_NOERROR, and UNUSED.

Referenced by uhc_dev_get_string(), and uhc_dev_is_high_speed_support().

void uhc_start ( void  )
void uhc_stop ( bool  b_id_stop)

Stops the host mode.

Parameters
b_id_stopStop USB ID pin management, if true.

References uhd_disable().

Referenced by otg_id_handler().

void uhc_suspend ( bool  b_remotewakeup)

Suspends a USB line.

Parameters
b_remotewakeupAuthorize the remote wakeup features, if true

References uhc_enum_try, uhc_remotewakeup(), and uhd_suspend().

uhc_device_t g_uhc_device_root
static

Entry point of all devices connected on USB tree.

Referenced by uhc_enumeration_step9(), uhc_get_device_number(), and uhc_remotewakeup().

uint8_t uhc_enum_try
static
volatile bool uhc_setup_request_finish
static

Volatile flag to pool the end of Get USB string setup request.

Referenced by uhc_dev_get_string(), uhc_dev_is_high_speed_support(), and uhc_setup_request_callback().

volatile bool uhc_setup_request_finish_status
static

Volatile flag to know the status of Get USB string setup request.

Referenced by uhc_dev_get_string(), uhc_dev_is_high_speed_support(), and uhc_setup_request_callback().

uint8_t uhc_sof_timeout

Number of SOF remaining before call uhc_sof_timeout_callback callback.

Referenced by uhc_connection_tree(), uhc_enable_timeout_callback(), uhc_notify_sof(), and uhc_start().

uhc_sof_timeout_callback_t uhc_sof_timeout_callback
static

Callback currently registered on a SOF timeout.

Referenced by uhc_enable_timeout_callback(), and uhc_notify_sof().

uhi_api_t uhc_uhis[] = {USB_HOST_UHI}
static

Array of all UHI available.