Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
USBC Host Driver

USB dual role configuration

The defines UHD_ENABLE and UDD_ENABLE must be added in project to allow the USB low level (UHD) to manage or not the dual role (device and host).

USBC Custom configuration

The following USBC driver configuration must be defined in conf_usb_host.h file of the application.

UHD_USB_INT_LEVEL
Option to change the interrupt priority (0 to 15) by default 5 (recommended).

UHD_USB_INT_FUN
Option to fit interrupt function to what defined in exception table.

UHD_BULK_INTERVAL_MIN
Feature to reduce or increase bulk token rate when it's NAKed (0, 1 ...). To adjust bandwidth usage. Default value 1.

UHD_NO_SLEEP_MGR
Feature to work without sleep manager module. Default not defined. Note that with this feature defined sleep manager must not be used in application.

management

The USB driver is fully managed by interrupt and does not request periodic task. Thereby, the USB events use callbacks to transfer the information. The callbacks can be declared in static during compilation or dynamically during code execution.

mode management

The driver uses the sleepmgr service to manage the different sleep modes. The sleep mode depends on USB driver state (uhd_usbc_state_enum).

Data Structures

struct  uhd_ctrl_request_t
 Structure to store the high level setup request. More...
 
struct  uhd_pipe_job_t
 Structure definition to store registered jobs on a pipe. More...
 

Functions

 ISR (UHD_USB_INT_FUN)
 Function called by USBC interrupt handler to manage USB interrupts. More...
 
void otg_dual_disable (void)
 Uninitialize the dual role This function is implemented in usbc_host.c file. More...
 
bool otg_dual_enable (void)
 Initialize the dual role This function is implemented in usbc_host.c file. More...
 
static void uhd_ctrl_interrupt (void)
 Manages the events related to control endpoint. More...
 
static void uhd_ctrl_phase_data_in (void)
 Manages the DATA IN phase on control endpoint. More...
 
static void uhd_ctrl_phase_data_in_start (void)
 Starts the DATA IN phase on control endpoint. More...
 
static void uhd_ctrl_phase_data_out (void)
 Manages the DATA OUT phase on control endpoint. More...
 
static void uhd_ctrl_phase_setup (void)
 Sends a USB setup packet to start a control request sequence. More...
 
static void uhd_ctrl_phase_zlp_in (void)
 Starts the ZLP IN phase on control endpoint. More...
 
static void uhd_ctrl_phase_zlp_out (void)
 Starts the ZLP OUT phase on control endpoint. More...
 
static void uhd_ctrl_request_end (uhd_trans_status_t status)
 Call the callback linked to control request and start the next request from the queue. More...
 
void uhd_disable (bool b_id_stop)
 Disables the USB host mode. More...
 
void uhd_enable (void)
 Enables the USB host mode Start the ID pin management if the ID pin is available. More...
 
bool uhd_ep0_alloc (usb_add_t add, uint8_t ep_size)
 Configures and enables a control endpoint 0. More...
 
void uhd_ep_abort (usb_add_t add, usb_ep_t endp)
 Aborts an on-going transfer on an endpoint. More...
 
static void uhd_ep_abort_pipe (uint8_t pipe, uhd_trans_status_t status)
 Aborts the on-going transfer on a pipe. More...
 
bool uhd_ep_alloc (usb_add_t add, usb_ep_desc_t *ep_desc, uhd_speed_t speed)
 Configures and enables an endpoint. More...
 
void uhd_ep_free (usb_add_t add, usb_ep_t endp)
 Disables an endpoint or all endpoint of a device. More...
 
bool uhd_ep_run (usb_add_t add, usb_ep_t endp, bool b_shortpacket, uint8_t *buf, iram_size_t buf_size, uint16_t timeout, uhd_callback_trans_t callback)
 Allows to receive or send data on an endpoint. More...
 
uint16_t uhd_get_frame_number (void)
 Returns the current Start Of Frame (SOF) number. More...
 
uint16_t uhd_get_microframe_number (void)
 Returns the current micro start of frame number. More...
 
static uint8_t uhd_get_pipe (usb_add_t add, usb_ep_t endp)
 Returns the pipe number matching a USB endpoint. More...
 
uhd_speed_t uhd_get_speed (void)
 Returns the speed of connected device. More...
 
static void uhd_interrupt (void)
 Function called by USBC interrupt to manage USB host interrupts. More...
 
bool uhd_is_suspend (void)
 Test if the suspend state is enabled on the USB line. More...
 
static void uhd_pipe_finish_job (uint8_t pipe, uhd_trans_status_t status)
 Call the callback linked to the end of pipe transfer. More...
 
static uhd_trans_status_t uhd_pipe_get_error (uint8_t pipe)
 Translates the USBC pipe error to UHD error. More...
 
static void uhd_pipe_interrupt (uint8_t pipe)
 Manages the following pipe interrupts: More...
 
static void uhd_pipe_trans_complet (uint8_t pipe)
 Computes and starts the next transfer on a pipe. More...
 
void uhd_resume (void)
 Enables the IDLE state on the USB line. More...
 
void uhd_send_reset (uhd_callback_reset_t callback)
 Enables the Reset state on the USB line. More...
 
bool uhd_setup_request (usb_add_t add, usb_setup_req_t *req, uint8_t *payload, uint16_t payload_size, uhd_callback_setup_run_t callback_run, uhd_callback_setup_end_t callback_end)
 Add a setup request in the control endpoint setup queue. More...
 
static void uhd_sof_interrupt (void)
 Manages timeouts and actions based on SOF events. More...
 
void uhd_suspend (void)
 Enables the suspend state on the USB line. More...
 

Power management

enum  uhd_usbc_state_enum {
  UHD_STATE_OFF = 0,
  UHD_STATE_WAIT_ID_HOST = 1,
  UHD_STATE_NO_VBUS = 2,
  UHD_STATE_DISCONNECT = 3,
  UHD_STATE_SUSPEND = 4,
  UHD_STATE_IDLE = 5
}
 States of USBC interface. More...
 
static void uhd_sleep_mode (enum uhd_usbc_state_enum new_state)
 Manages the sleep mode following the USBC state. More...
 
static volatile uhd_desc_table_t uhd_g_pipe_table [UHD_PEP_NB]
 USB SRAM data containing pipe descriptor table The content of the USB SRAM can be : More...
 
static bool otg_initialized = false
 State of USBC OTG initialization. More...
 
static uhd_callback_reset_t uhd_reset_callback = NULL
 Store the callback to be call at the end of reset signal. More...
 

Control endpoint low level management routine.

This function performs control endpoint management.

It handles the SETUP/DATA/HANDSHAKE phases of a control transaction.

enum  uhd_ctrl_request_phase_t {
  UHD_CTRL_REQ_PHASE_SETUP = 0,
  UHD_CTRL_REQ_PHASE_DATA_OUT = 1,
  UHD_CTRL_REQ_PHASE_DATA_IN = 2,
  UHD_CTRL_REQ_PHASE_ZLP_IN = 3,
  UHD_CTRL_REQ_PHASE_ZLP_OUT = 4
}
 Bit definitions to store setup request state machine. More...
 
uint32_t uhd_ctrl_buffer [64/4]
 Buffer to store the sent/received data on control endpoint. More...
 
static bool uhd_b_zlp_out_delayed = false
 Flag to delay a ZLP OUT on control endpoint. More...
 
struct uhd_ctrl_request_tuhd_ctrl_request_first
 Entry points of setup request list. More...
 
struct uhd_ctrl_request_tuhd_ctrl_request_last
 
volatile uint16_t uhd_ctrl_request_timeout
 Remaining time for on-going setup request (No request on-going if equal 0) More...
 
uint16_t uhd_ctrl_nb_trans
 Number of transfered byte on DATA phase of current setup request. More...
 
static bool uhd_b_suspend_requested
 Flag to delay a suspend request after all on-going setup request. More...
 
uhd_ctrl_request_phase_t uhd_ctrl_request_phase
 

Management of bulk/interrupt/isochronous endpoints

The UHD manages the data transfer on endpoints:

  • Start data transfer on endpoint with USB DMA
  • Send a ZLP packet if requested
  • Call registered callback to signal end of transfer The transfer abort and stall feature are supported.
static uhd_pipe_job_t uhd_pipe_job [UHD_PEP_NB-1]
 Array to register a job on bulk/interrupt/isochronous endpoint. More...
 
static uint8_t uhd_suspend_start
 Variables to manage the suspend/resume sequence. More...
 
static uint8_t uhd_resume_start
 
static uint8_t uhd_pipes_unfreeze
 
static uint8_t uhd_pipes_error
 Variable to manage error on pipes. More...
 

Bit definitions to store setup request state machine.

Enumerator
UHD_CTRL_REQ_PHASE_SETUP 

Wait a SETUP packet.

UHD_CTRL_REQ_PHASE_DATA_OUT 

Wait a OUT data packet.

UHD_CTRL_REQ_PHASE_DATA_IN 

Wait a IN data packet.

UHD_CTRL_REQ_PHASE_ZLP_IN 

Wait a IN ZLP packet.

UHD_CTRL_REQ_PHASE_ZLP_OUT 

Wait a OUT ZLP packet.

States of USBC interface.

Enumerator
UHD_STATE_OFF 
UHD_STATE_WAIT_ID_HOST 
UHD_STATE_NO_VBUS 
UHD_STATE_DISCONNECT 
UHD_STATE_SUSPEND 
UHD_STATE_IDLE 

ISR ( UHD_USB_INT_FUN  )

Function called by USBC interrupt handler to manage USB interrupts.

It manages the interrupt redirection between host or device interrupt. It answers to OTG events (ID pin change).

Note: Here, the global interrupt mask is not cleared when an USB interrupt is enabled because this one can not occurred during the USB ISR (=during INTX is masked). See Technical reference $3.8.3 Masking interrupt requests in peripheral modules.

References Is_otg_device_mode_enabled, otg_data_memory_barrier, udd_interrupt, and uhd_interrupt().

void otg_dual_disable ( void  )
bool otg_dual_enable ( void  )

Initialize the dual role This function is implemented in usbc_host.c file.

Returns
true if the ID pin management has been started, otherwise false.

References eic_enable(), Is_pad_id_device, otg_enable_device_mode, otg_enable_host_mode, otg_initialized, pad_id_init, sysclk_enable_usb(), udc_start, UHC_MODE_CHANGE, uhc_start(), uhd_sleep_mode(), UHD_STATE_OFF, UHD_STATE_WAIT_ID_HOST, UHD_USB_INT_LEVEL, and usbc_async_wake_up_enable().

Referenced by uhd_enable().

static void uhd_ctrl_phase_data_in ( void  )
static
static void uhd_ctrl_phase_zlp_out ( void  )
static
static void uhd_ctrl_request_end ( uhd_trans_status_t  status)
static
bool uhd_ep0_alloc ( usb_add_t  add,
uint8_t  ep_size 
)

Configures and enables a control endpoint 0.

Parameters
addUSB address of endpoint
ep_sizeEndpoint control maximum size
Returns
1 if the endpoint is enabled, otherwise 0.

References Is_uhd_pipe_enabled, uhd_configure_pipe, uhd_ctrl_buffer, uhd_enable_pipe, uhd_enable_pipe_error_interrupt, uhd_enable_pipe_interrupt, uhd_enable_stall_interrupt, uhd_g_pipe_table, uhd_udesc_set_buf0_addr, and uhd_udesc_set_uhaddr.

Referenced by uhc_enumeration_step11(), uhc_enumeration_step5(), and uhc_enumeration_step9().

void uhd_ep_abort ( usb_add_t  add,
usb_ep_t  endp 
)

Aborts an on-going transfer on an endpoint.

If a transfer is on going, then it is stopped and the callback registered is called to signal the end of transfer. Note: The control endpoint is not authorized.

Parameters
addUSB address of endpoint
endpEndpoint to abort

References uhd_ep_abort_pipe(), uhd_get_pipe(), UHD_PEP_NB, and UHD_TRANS_ABORTED.

void uhd_ep_free ( usb_add_t  add,
usb_ep_t  endp 
)

Disables an endpoint or all endpoint of a device.

Parameters
addUSB address of endpoint
endpEndpoint number and direction (USB_EP_DIR_IN/OUT). Remove all endpoints of USB address, if 0xFF.

References uhd_ctrl_request_t::add, uhd_ctrl_request_end(), uhd_disable_pipe, uhd_get_pipe_endpoint_address, UHD_PEP_NB, uhd_pipe_finish_job(), UHD_TRANS_DISCONNECT, uhd_udesc_get_epnum, and uhd_udesc_get_uhaddr.

Referenced by uhc_connection_tree(), uhc_enumeration_error(), uhc_enumeration_step11(), uhc_enumeration_step14(), uhc_enumeration_step5(), and uhc_enumeration_step9().

bool uhd_ep_run ( usb_add_t  add,
usb_ep_t  endp,
bool  b_shortpacket,
uint8_t *  buf,
iram_size_t  buf_size,
uint16_t  timeout,
uhd_callback_trans_t  callback 
)

Allows to receive or send data on an endpoint.

If a USB DMA is available, the driver uses it to transfer endpoint data from or to internal RAM. When the transfer is finished or aborted (stall, reset, ...), the callback is called. This callback returns the transfer status and eventually the number of byte transfered. Note: The control endpoint is not authorized.

Parameters
addUSB address of endpoint
endpEndpoint number
b_shortpacketEnabled automatic short packet
bufBuffer on Internal RAM to send or fill. It must be align, then use COMPILER_WORD_ALIGNED.
buf_sizeBuffer size to send or fill
timeoutTransfer timeout (ms)
callbackNULL or function to call at the end of transfer
Warning
About b_shortpacket, for OUT endpoint it means that a short packet or a Zero Length Packet (ZLP) must be sent to the USB line to properly close the USB transfer at the end of the data transfer. For Bulk and Interrupt IN endpoint, it will automatically stop the transfer at the end of the data transfer (received short packet).
About buf_size, for OUT endpoint the data is sent packet by packet until size is achieved. If the size is multiple of endpoint size ZLP may be sent according to the b_shortpacket setting. For IN endpoint the data is received packet by packet, if the last packet exceeds the buffer size, the overflow data will be discarded. So for IN endpoint it's better to allocate buffer size aligned to endpoint size so no returned data is lost.
About timeout, for BULK endpoint with timeout set to zero, it means that the transfer will never be stopped before transfer done. Since most of USB embedded peripherals do not manage the transfer bandwidth by peripheral hardware, such a BULK transfer will occupy all USB non-periodic transfer bandwidth. In this case, other BULK transfers started later will be pending until this transfer is done and bandwidth released. So it is better to use BULK transfers with none zero timeout.
Returns
1 if function was successfully done, otherwise 0.

References uhd_pipe_job_t::b_shortpacket, uhd_pipe_job_t::buf, uhd_pipe_job_t::buf_size, uhd_pipe_job_t::busy, uhd_pipe_job_t::call_end, cpu_irq_restore(), cpu_irq_save(), uhd_pipe_job_t::nb_trans, uhd_pipe_job_t::timeout, uhd_get_pipe(), UHD_PEP_NB, uhd_pipe_trans_complet(), uhd_udesc_rst_buf0_ctn, and uhd_udesc_rst_buf0_size.

Referenced by uhi_hid_mouse_start_trans_report().

uint16_t uhd_get_frame_number ( void  )

Returns the current Start Of Frame (SOF) number.

Returns
current start of frame number.

References uhd_get_sof_number.

uint16_t uhd_get_microframe_number ( void  )

Returns the current micro start of frame number.

Returns
current micro start of frame number required in high speed mode.

References uhd_get_microsof_number.

static uint8_t uhd_get_pipe ( usb_add_t  add,
usb_ep_t  endp 
)
static

Returns the pipe number matching a USB endpoint.

Parameters
addUSB address
endpEndpoint number
Returns
Pipe number

References Is_uhd_pipe_enabled, uhd_get_pipe_endpoint_address, and uhd_udesc_get_uhaddr.

Referenced by uhd_ep_abort(), and uhd_ep_run().

uhd_speed_t uhd_get_speed ( void  )

Returns the speed of connected device.

Returns
Device speed

References Assert, uhd_get_speed_mode, UHD_SPEED_FULL, and UHD_SPEED_LOW.

Referenced by uhc_enumeration_step4().

bool uhd_is_suspend ( void  )

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

Returns
USB line in SUSPEND state, if true

References Is_uhd_sof_enabled.

Referenced by uhc_is_suspend().

static void uhd_pipe_finish_job ( uint8_t  pipe,
uhd_trans_status_t  status 
)
static

Call the callback linked to the end of pipe transfer.

Parameters
pipePipe number
statusStatus of the transfer

References uhd_pipe_job_t::buf_internal, uhd_pipe_job_t::busy, uhd_pipe_job_t::call_end, dbgp_ep, uhd_pipe_job_t::nb_trans, uhd_get_pipe_endpoint_address, and uhd_udesc_get_uhaddr.

Referenced by uhd_ep_abort_pipe(), uhd_ep_free(), and uhd_pipe_trans_complet().

static void uhd_pipe_interrupt ( uint8_t  pipe)
static
void uhd_resume ( void  )

Enables the IDLE state on the USB line.

The IDLE state is enable when SOF are present on USB line. A Downstream Resume signal can be sent.

References Is_otg_clock_usable, Is_uhd_sof_enabled, otg_unfreeze_clock, uhd_enable_sof, uhd_send_resume, uhd_sleep_mode(), and UHD_STATE_IDLE.

Referenced by uhc_resume().

void uhd_send_reset ( uhd_callback_reset_t  callback)

Enables the Reset state on the USB line.

Parameters
callbackCallback when reset sequence is finished

References uhd_reset_callback, and uhd_start_reset.

Referenced by uhc_enumeration_reset().

bool uhd_setup_request ( usb_add_t  add,
usb_setup_req_t req,
uint8_t *  payload,
uint16_t  payload_size,
uhd_callback_setup_run_t  callback_run,
uhd_callback_setup_end_t  callback_end 
)

Add a setup request in the control endpoint setup queue.

Note: Request timeout is 5s.

Parameters
addUSB address of control endpoint
reqSetup request definition
payloadBuffer to use in setup DATA phase
payload_sizeSize of buffer used in DATA phase
callback_runCallback to call if buffer is empty or full
callback_endCallback to call when request is finish
Returns
true if the request has been accepted, otherwise false. Note: The swap of "req.wValues" from uin16_t to le16_t is done by UHD.

References uhd_ctrl_request_t::add, Assert, uhd_ctrl_request_t::callback_end, uhd_ctrl_request_t::callback_run, cpu_irq_restore(), cpu_irq_save(), uhd_ctrl_request_t::next_request, uhd_ctrl_request_t::payload, uhd_ctrl_request_t::payload_size, uhd_ctrl_request_t::req, and uhd_ctrl_phase_setup().

Referenced by uhc_dev_get_string(), uhc_dev_is_high_speed_support(), uhc_enumeration_step11(), uhc_enumeration_step12(), uhc_enumeration_step13(), uhc_enumeration_step14(), uhc_enumeration_step5(), uhc_enumeration_step9(), and uhc_remotewakeup().

static void uhd_sleep_mode ( enum uhd_usbc_state_enum  new_state)
static
static void uhd_sof_interrupt ( void  )
static

Manages timeouts and actions based on SOF events.

  • Suspend delay
  • Resume delay
  • Setup packet delay
  • Timeout on endpoint control transfer
  • Timeouts on bulk/interrupt/isochronous endpoint transfers
  • UHC user notification
  • SOF user notification

References uhd_pipe_job_t::busy, ctz, dbg_print, dbgp_ctrl, dbgp_ep, Is_uhd_disconnection, Is_uhd_low_speed_mode, Is_uhd_sof_enabled, otg_freeze_clock, otg_get_fsm_drd_state, uhd_pipe_job_t::timeout, uhc_notify_resume(), uhc_notify_sof(), UHC_SOF_EVENT, uhd_ctrl_request_end(), uhd_disable_sof, uhd_enable_out_ready_interrupt, uhd_ep_abort_pipe(), uhd_freeze_pipe, uhd_get_frame_position, UHD_PEP_NB, uhd_raise_pipe_error, uhd_sleep_mode(), UHD_STATE_SUSPEND, UHD_TRANS_TIMEOUT, and uhd_unfreeze_pipe.

Referenced by uhd_interrupt().

void uhd_suspend ( void  )

Enables the suspend state on the USB line.

The SUSPEND state is enable when SOF are disabled on USB line.

References Is_uhd_pipe_frozen, uhd_freeze_pipe, and UHD_PEP_NB.

Referenced by uhc_enumeration_suspend(), uhc_suspend(), and uhd_ctrl_request_end().

bool otg_initialized = false
static

State of USBC OTG initialization.

Referenced by otg_dual_disable(), and otg_dual_enable().

bool uhd_b_suspend_requested
static

Flag to delay a suspend request after all on-going setup request.

bool uhd_b_zlp_out_delayed = false
static

Flag to delay a ZLP OUT on control endpoint.

uint32_t uhd_ctrl_buffer[64/4]

Buffer to store the sent/received data on control endpoint.

Used to avoid a RAM buffer overflow when the payload buffer is smaller than control endpoint size. Also used when payload buffer is not word aligned.

Referenced by uhd_ctrl_phase_data_in(), uhd_ctrl_phase_data_out(), uhd_ctrl_phase_setup(), and uhd_ep0_alloc().

uint16_t uhd_ctrl_nb_trans

Number of transfered byte on DATA phase of current setup request.

struct uhd_ctrl_request_t* uhd_ctrl_request_first

Entry points of setup request list.

Referenced by uhd_ctrl_request_end().

struct uhd_ctrl_request_t* uhd_ctrl_request_last
uhd_ctrl_request_phase_t uhd_ctrl_request_phase
volatile uint16_t uhd_ctrl_request_timeout

Remaining time for on-going setup request (No request on-going if equal 0)

volatile uhd_desc_table_t uhd_g_pipe_table[UHD_PEP_NB]
static

USB SRAM data containing pipe descriptor table The content of the USB SRAM can be :

  • modified by USB hardware interface to update pipe status. Thereby, it is read by software.
  • modified by USB software to control pipe. Thereby, it is read by hardware. This data section is volatile.

Referenced by uhd_enable(), uhd_ep0_alloc(), and uhd_ep_alloc().

uhd_pipe_job_t uhd_pipe_job[UHD_PEP_NB-1]
static

Array to register a job on bulk/interrupt/isochronous endpoint.

uint8_t uhd_pipes_error
static

Variable to manage error on pipes.

uint8_t uhd_pipes_unfreeze
static
uhd_callback_reset_t uhd_reset_callback = NULL
static

Store the callback to be call at the end of reset signal.

Referenced by uhd_interrupt(), and uhd_send_reset().

uint8_t uhd_resume_start
static
uint8_t uhd_suspend_start
static

Variables to manage the suspend/resume sequence.