Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
USB Device Driver Implement (UDD)

USB low-level driver for USB device mode.

Macros

#define dbg_print(...)
 
#define UDC_REMOTEWAKEUP_LPM_DISABLE()
 
#define UDC_REMOTEWAKEUP_LPM_ENABLE()
 
#define UDC_SUSPEND_LPM_EVENT()
 
#define UDD_ENDPOINT_MAX_TRANS   ((8*1024)-1)
 Maximum size of a transfer in multi-packet mode. More...
 

Functions

static void _usb_device_lpm_suspend (struct usb_module *module_inst, void *pointer)
 
static void _usb_ep0_on_setup (struct usb_module *module_inst, void *pointer)
 Endpoint 0 (control) SETUP received callback. More...
 
static void _usb_ep0_on_tansfer_fail (struct usb_module *module_inst, void *pointer)
 Control endpoint transfer fail callback function. More...
 
static void _usb_ep0_on_tansfer_ok (struct usb_module *module_inst, void *pointer)
 Control endpoint transfer complete callback function. More...
 
static void _usb_on_bus_reset (struct usb_module *module_inst, void *pointer)
 Control endpoint Reset callback function. More...
 
static void _usb_on_sof_notify (struct usb_module *module_inst, void *pointer)
 Control endpoint SOF callback function. More...
 
static void _usb_on_suspend (struct usb_module *module_inst, void *pointer)
 Control endpoint Suspend callback function. More...
 
static void _usb_on_wakeup (struct usb_module *module_inst, void *pointer)
 Control endpoint Wakeup callback function. More...
 
void udd_attach (void)
 Attach device to the bus when possible. More...
 
static void udd_ctrl_ep_enable (struct usb_module *module_inst)
 Enable Control Endpoint. More...
 
static void udd_ctrl_fetch_ram (void)
 Control Endpoint translate the data in buffer into Device Request Struct. More...
 
static void udd_ctrl_in_sent (void)
 Process control endpoint IN transaction. More...
 
static void udd_ctrl_out_received (void *pointer)
 Process control endpoint OUT transaction. More...
 
static void udd_ctrl_overflow (void *pointer)
 Control Endpoint Process when overflow condition has occurred. More...
 
static void udd_ctrl_send_zlp_in (void)
 Control Endpoint send out zero length packet. More...
 
static void udd_ctrl_stall_data (void)
 Control Endpoint stall sending data. More...
 
static void udd_ctrl_underflow (void *pointer)
 Control Endpoint Process when underflow condition has occurred. More...
 
void udd_detach (void)
 Detaches the device from the bus. More...
 
void udd_disable (void)
 Disables the USB Device mode. More...
 
void udd_enable (void)
 Enables the USB Device mode. More...
 
void udd_ep_abort (udd_ep_id_t ep)
 
bool udd_ep_alloc (udd_ep_id_t ep, uint8_t bmAttributes, uint16_t MaxEndpointSize)
 
bool udd_ep_clear_halt (udd_ep_id_t ep)
 
void udd_ep_free (udd_ep_id_t ep)
 
static udd_ep_job_tudd_ep_get_job (udd_ep_id_t ep)
 Get the detailed job by endpoint number. More...
 
bool udd_ep_is_halted (udd_ep_id_t ep)
 
bool udd_ep_run (udd_ep_id_t ep, bool b_shortpacket, uint8_t *buf, iram_size_t buf_size, udd_callback_trans_t callback)
 
bool udd_ep_set_halt (udd_ep_id_t ep)
 
static void udd_ep_trans_in_next (void *pointer)
 Endpoint IN process, continue to send packets or zero length packet. More...
 
static void udd_ep_trans_out_next (void *pointer)
 Endpoint OUT process, continue to receive packets or zero length packet. More...
 
static void udd_ep_transfer_process (struct usb_module *module_inst, void *pointer)
 Endpoint Transfer Complete callback function, to do the next transfer depends on the direction(IN or OUT) More...
 
bool udd_ep_wait_stall_clear (udd_ep_id_t ep, udd_callback_halt_cleared_t callback)
 
uint16_t udd_get_frame_number (void)
 Returns the current start of frame number. More...
 
uint16_t udd_get_micro_frame_number (void)
 Returns the current micro start of frame number. More...
 
uint8_t udd_getaddress (void)
 Returns the USB address of device. More...
 
bool udd_include_vbus_monitoring (void)
 Authorizes the VBUS event. More...
 
bool udd_is_high_speed (void)
 Test whether the USB Device Controller is running at high speed or not. More...
 
void udd_send_remotewakeup (void)
 The USB driver sends a resume signal called Upstream Resume. More...
 
void udd_set_address (uint8_t address)
 Changes the USB address of device. More...
 
void udd_set_setup_payload (uint8_t *payload, uint16_t payload_size)
 Load setup payload. More...
 

Variables

struct usb_module usb_device
 USB software device instance structure. More...
 

Clock management

static void udd_wait_clock_ready (void)
 
#define UDD_CLOCK_GEN   GCLK_GENERATOR_0
 
#define UDD_CLOCK_SOURCE   SYSTEM_CLOCK_SOURCE_DFLL
 

Power management

enum  udd_usb_state_enum {
  UDD_STATE_OFF,
  UDD_STATE_SUSPEND,
  UDD_STATE_SUSPEND_LPM,
  UDD_STATE_IDLE
}
 States of USB interface. More...
 
static void udd_sleep_mode (enum udd_usb_state_enum new_state)
 Manages the sleep mode following the USB state. 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  udd_ctrl_ep_state_t {
  UDD_EPCTRL_SETUP = 0,
  UDD_EPCTRL_DATA_OUT = 1,
  UDD_EPCTRL_DATA_IN = 2,
  UDD_EPCTRL_HANDSHAKE_WAIT_IN_ZLP = 3,
  UDD_EPCTRL_HANDSHAKE_WAIT_OUT_ZLP = 4,
  UDD_EPCTRL_STALL_REQ = 5
}
 Bit definitions about endpoint control state machine for udd_ep_control_state. More...
 
uint8_t udd_ctrl_buffer [USB_DEVICE_EP_CTRL_SIZE]
 Buffer to store the data received on control endpoint (SETUP/OUT endpoint 0) More...
 
udd_ctrl_request_t udd_g_ctrlreq
 Global variable to give and record information of the set up request management. More...
 
static udd_ctrl_ep_state_t udd_ep_control_state
 State of the endpoint control management. More...
 
static uint16_t udd_ctrl_prev_payload_nb_trans
 Total number of data received/sent during data packet phase with previous payload buffers. More...
 
static uint16_t udd_ctrl_payload_nb_trans
 Number of data received/sent to/from udd_g_ctrlreq.payload buffer. More...
 

Management of bulk/interrupt/isochronous endpoints

The UDD manages the data transfer on endpoints:

  • Start data transfer on endpoint with USB Device DMA
  • Send a ZLP packet if requested
  • Call callback registered to signal end of transfer The transfer abort and stall feature are supported.
 udd_ep_job_t
 
static udd_ep_job_t udd_ep_job [2 *USB_DEVICE_MAX_EP]
 Array to register a job on bulk/interrupt/isochronous endpoint. More...
 
 UDC_BSS (4)
 Buffer to store the data received on bulk/interrupt endpoints. More...
 

USB VBUS PAD management

static void _uhd_vbus_handler (void)
 USB VBUS pin change handler. More...
 
static void _usb_vbus_config (void)
 USB VBUS pin configuration. More...
 
#define is_usb_vbus_high()   port_pin_get_input_level(USB_VBUS_PIN)
 Check if USB VBus is available. More...
 

#define dbg_print (   ...)

Referenced by _usb_device_lpm_suspend().

#define is_usb_vbus_high ( )    port_pin_get_input_level(USB_VBUS_PIN)

Check if USB VBus is available.

Referenced by _uhd_vbus_handler(), udd_enable(), and uhd_enable().

#define UDC_REMOTEWAKEUP_LPM_DISABLE ( )

Referenced by _usb_device_lpm_suspend().

#define UDC_REMOTEWAKEUP_LPM_ENABLE ( )

Referenced by _usb_device_lpm_suspend().

#define UDC_SUSPEND_LPM_EVENT ( )

Referenced by _usb_device_lpm_suspend().

#define UDD_CLOCK_GEN   GCLK_GENERATOR_0

Referenced by udd_enable().

#define UDD_CLOCK_SOURCE   SYSTEM_CLOCK_SOURCE_DFLL

Referenced by udd_wait_clock_ready().

#define UDD_ENDPOINT_MAX_TRANS   ((8*1024)-1)

Maximum size of a transfer in multi-packet mode.

Referenced by udd_ep_run(), udd_ep_trans_in_next(), and udd_ep_trans_out_next().

Bit definitions about endpoint control state machine for udd_ep_control_state.

Enumerator
UDD_EPCTRL_SETUP 

Wait a SETUP packet.

UDD_EPCTRL_DATA_OUT 

Wait a OUT data packet.

UDD_EPCTRL_DATA_IN 

Wait a IN data packet.

UDD_EPCTRL_HANDSHAKE_WAIT_IN_ZLP 

Wait a IN ZLP packet.

UDD_EPCTRL_HANDSHAKE_WAIT_OUT_ZLP 

Wait a OUT ZLP packet.

UDD_EPCTRL_STALL_REQ 

STALL enabled on IN & OUT packet.

States of USB interface.

Enumerator
UDD_STATE_OFF 
UDD_STATE_SUSPEND 
UDD_STATE_SUSPEND_LPM 
UDD_STATE_IDLE 

static void _uhd_vbus_handler ( void  )
static
static void _usb_ep0_on_setup ( struct usb_module module_inst,
void *  pointer 
)
static
static void _usb_ep0_on_tansfer_fail ( struct usb_module module_inst,
void *  pointer 
)
static

Control endpoint transfer fail callback function.

Parameters
[in]module_instPointer to USB module instance
[in]pointerPointer to the endpoint transfer status parameter struct from driver layer.

References usb_endpoint_callback_parameter::endpoint_address, ep_callback_para, udd_ctrl_overflow(), udd_ctrl_underflow(), and USB_EP_DIR_IN.

Referenced by udd_ctrl_ep_enable().

static void _usb_ep0_on_tansfer_ok ( struct usb_module module_inst,
void *  pointer 
)
static

Control endpoint transfer complete callback function.

Parameters
[in]module_instPointer to USB module instance
[in]pointerPointer to the endpoint transfer status parameter struct from driver layer.

References udd_ctrl_request_t::callback, NULL, udd_ctrl_in_sent(), udd_ctrl_out_received(), UDD_EPCTRL_DATA_IN, UDD_EPCTRL_DATA_OUT, and UDD_EPCTRL_SETUP.

Referenced by udd_ctrl_ep_enable().

static void _usb_on_bus_reset ( struct usb_module module_inst,
void *  pointer 
)
static

Control endpoint Reset callback function.

Parameters
[in]module_instPointer to USB module instance
[in]pointerPointer to the callback parameter from driver layer.

References udc_reset(), udd_ctrl_ep_enable(), and usb_device_set_address().

Referenced by udd_attach().

static void _usb_on_sof_notify ( struct usb_module module_inst,
void *  pointer 
)
static

Control endpoint SOF callback function.

Parameters
[in]module_instPointer to USB module instance
[in]pointerPointer to the callback parameter from driver layer.

References UDC_SOF_EVENT, and udc_sof_notify().

Referenced by udd_attach().

static void _usb_on_suspend ( struct usb_module module_inst,
void *  pointer 
)
static

Control endpoint Suspend callback function.

Parameters
[in]module_instPointer to USB module instance
[in]pointerPointer to the callback parameter from driver layer.

References UDC_SUSPEND_EVENT, udd_sleep_mode(), UDD_STATE_SUSPEND, usb_device, USB_DEVICE_CALLBACK_SUSPEND, USB_DEVICE_CALLBACK_WAKEUP, usb_device_disable_callback(), and usb_device_enable_callback().

Referenced by udd_attach().

static void _usb_on_wakeup ( struct usb_module module_inst,
void *  pointer 
)
static

Control endpoint Wakeup callback function.

Parameters
[in]module_instPointer to USB module instance
[in]pointerPointer to the callback parameter from driver layer.

References _usb_device_lpm_suspend(), UDC_RESUME_EVENT, udd_sleep_mode(), UDD_STATE_IDLE, udd_wait_clock_ready(), usb_device, USB_DEVICE_CALLBACK_LPMSUSP, USB_DEVICE_CALLBACK_SUSPEND, USB_DEVICE_CALLBACK_WAKEUP, usb_device_disable_callback(), usb_device_enable_callback(), and usb_device_register_callback().

Referenced by udd_attach().

UDC_BSS ( )

Buffer to store the data received on bulk/interrupt endpoints.

Used to avoid a RAM buffer overflow in case of the user buffer is smaller than endpoint size

Warning
The protected interrupt endpoint size is 512 bytes maximum.
The isochronous and endpoint is not protected by this system and the user must always use a buffer corresponding at endpoint size.Structure definition about job registered on an endpoint

Callback to call at the end of transfer

Callback to call when the endpoint halt is cleared

Buffer located in internal RAM to send or fill during job

Size of buffer to send or fill

Total number of data transferred on endpoint

Endpoint size

A job is registered on this endpoint

A short packet is requested for this job on endpoint IN

The cache buffer is currently used on endpoint OUT

References buf.

void udd_attach ( void  )
static void udd_ctrl_fetch_ram ( void  )
static
static void udd_ctrl_overflow ( void *  pointer)
static

Control Endpoint Process when overflow condition has occurred.

Parameters
[in]pointerPointer to the endpoint transfer status parameter struct from driver layer.

References usb_endpoint_callback_parameter::endpoint_address, ep_callback_para, UDD_EPCTRL_DATA_IN, UDD_EPCTRL_HANDSHAKE_WAIT_IN_ZLP, UDD_EPCTRL_HANDSHAKE_WAIT_OUT_ZLP, usb_device, and usb_device_endpoint_set_halt().

Referenced by _usb_ep0_on_tansfer_fail().

static void udd_ctrl_send_zlp_in ( void  )
static
static void udd_ctrl_stall_data ( void  )
static
static void udd_ctrl_underflow ( void *  pointer)
static

Control Endpoint Process when underflow condition has occurred.

Parameters
[in]pointerPointer to the endpoint transfer status parameter struct from driver layer.

References usb_endpoint_callback_parameter::endpoint_address, ep_callback_para, udd_ctrl_send_zlp_in(), UDD_EPCTRL_DATA_OUT, UDD_EPCTRL_HANDSHAKE_WAIT_OUT_ZLP, usb_device, and usb_device_endpoint_set_halt().

Referenced by _usb_ep0_on_tansfer_fail().

void udd_detach ( void  )

Detaches the device from the bus.

The driver must remove pull-up on USB line D- or D+.

References udd_sleep_mode(), UDD_STATE_SUSPEND, usb_device, and usb_device_detach().

Referenced by _uhd_vbus_handler(), udc_detach(), and udd_disable().

void udd_disable ( void  )

Disables the USB Device mode.

References cpu_irq_restore(), cpu_irq_save(), udd_detach(), udd_sleep_mode(), UDD_STATE_OFF, and usb_dual_disable().

Referenced by udc_stop().

static udd_ep_job_t* udd_ep_get_job ( udd_ep_id_t  ep)
static

Get the detailed job by endpoint number.

Parameters
[in]epEndpoint Address
Return values
pointerto an udd_ep_job_t structure instance

References NULL, USB_EP_ADDR_MASK, and USB_EP_DIR_IN.

Referenced by udd_ep_abort(), udd_ep_alloc(), udd_ep_clear_halt(), udd_ep_run(), udd_ep_trans_in_next(), udd_ep_trans_out_next(), and udd_ep_wait_stall_clear().

bool udd_ep_is_halted ( udd_ep_id_t  ep)

References usb_device, and usb_device_endpoint_is_halted().

Referenced by udd_ep_run().

static void udd_ep_trans_in_next ( void *  pointer)
static

Endpoint IN process, continue to send packets or zero length packet.

Parameters
[in]pointerPointer to the endpoint transfer status parameter struct from driver layer.

References usb_endpoint_callback_parameter::endpoint_address, ep_callback_para, NULL, usb_endpoint_callback_parameter::sent_bytes, UDD_ENDPOINT_MAX_TRANS, udd_ep_get_job(), udd_ep_job_t, UDD_EP_TRANSFER_OK, usb_device, usb_device_endpoint_write_buffer_job(), and USB_EP_ADDR_MASK.

Referenced by udd_ep_transfer_process().

static void udd_ep_trans_out_next ( void *  pointer)
static

Endpoint OUT process, continue to receive packets or zero length packet.

Parameters
[in]pointerPointer to the endpoint transfer status parameter struct from driver layer.

References usb_endpoint_callback_parameter::endpoint_address, ep_callback_para, NULL, usb_endpoint_callback_parameter::out_buffer_size, usb_endpoint_callback_parameter::received_bytes, UDD_ENDPOINT_MAX_TRANS, udd_ep_get_job(), udd_ep_job_t, UDD_EP_TRANSFER_OK, usb_device, usb_device_endpoint_read_buffer_job(), and USB_EP_ADDR_MASK.

Referenced by udd_ep_transfer_process().

static void udd_ep_transfer_process ( struct usb_module module_inst,
void *  pointer 
)
static

Endpoint Transfer Complete callback function, to do the next transfer depends on the direction(IN or OUT)

Parameters
[in]module_instPointer to USB module instance
[in]pointerPointer to the endpoint transfer status parameter struct from driver layer.

References usb_endpoint_callback_parameter::endpoint_address, ep_callback_para, udd_ep_trans_in_next(), udd_ep_trans_out_next(), and USB_EP_DIR_IN.

Referenced by udd_ep_alloc().

uint16_t udd_get_frame_number ( void  )

Returns the current start of frame number.

Returns
current start of frame number.

References usb_device, and usb_device_get_frame_number().

Referenced by main_sof_action(), and udi_cdc_tx_send().

uint16_t udd_get_micro_frame_number ( void  )

Returns the current micro start of frame number.

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

References usb_device, and usb_device_get_micro_frame_number().

Referenced by udi_cdc_tx_send().

uint8_t udd_getaddress ( void  )

Returns the USB address of device.

Returns
USB address

References usb_device, and usb_device_get_address().

Referenced by udc_req_std_dev_set_configuration().

bool udd_include_vbus_monitoring ( void  )

Authorizes the VBUS event.

Returns
true, if the VBUS monitoring is possible.

Referenced by udc_include_vbus_monitoring().

bool udd_is_high_speed ( void  )

Test whether the USB Device Controller is running at high speed or not.

Returns
true if the Device is running at high speed mode, otherwise false.

Referenced by main_vendor_iso_out_received(), udc_req_std_dev_get_descriptor(), udc_req_std_dev_set_configuration(), udc_req_std_dev_set_feature(), and udi_cdc_tx_send().

void udd_send_remotewakeup ( void  )

The USB driver sends a resume signal called Upstream Resume.

References udd_sleep_mode(), UDD_STATE_IDLE, udd_wait_clock_ready(), usb_device, usb_device_send_remote_wake_up(), and usb_get_state_machine_status().

Referenced by udc_remotewakeup().

void udd_set_address ( uint8_t  address)

Changes the USB address of device.

Parameters
addressNew USB address

References usb_device, and usb_device_set_address().

Referenced by udc_valid_address().

void udd_set_setup_payload ( uint8_t *  payload,
uint16_t  payload_size 
)
static void udd_sleep_mode ( enum udd_usb_state_enum  new_state)
static
static void udd_wait_clock_ready ( void  )
inlinestatic

uint8_t udd_ctrl_buffer[USB_DEVICE_EP_CTRL_SIZE]

Buffer to store the data received on control endpoint (SETUP/OUT endpoint 0)

Used to avoid a RAM buffer overflow in case of the payload buffer is smaller than control endpoint size

Referenced by _usb_ep0_on_setup(), udd_ctrl_ep_enable(), udd_ctrl_fetch_ram(), udd_ctrl_in_sent(), udd_ctrl_out_received(), and udd_ctrl_send_zlp_in().

uint16_t udd_ctrl_payload_nb_trans
static

Number of data received/sent to/from udd_g_ctrlreq.payload buffer.

Referenced by udd_ctrl_in_sent(), and udd_ctrl_out_received().

uint16_t udd_ctrl_prev_payload_nb_trans
static

Total number of data received/sent during data packet phase with previous payload buffers.

udd_ctrl_ep_state_t udd_ep_control_state
static

State of the endpoint control management.

udd_ep_job_t udd_ep_job[2 *USB_DEVICE_MAX_EP]
static

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