Microchip® Advanced Software Framework

usb_device_udd.c File Reference

USB Device wrapper layer for compliance with common driver UDD.

Copyright (c) 2014-2020 Microchip Technology Inc. and its subsidiaries.

#include <string.h>
#include <stdlib.h>
#include "conf_usb.h"
#include "udd.h"
#include "usb.h"
#include "usb_dual.h"
#include "sleepmgr.h"

Macros

#define dbg_print(...)
 
#define DFLL_READY_FLAG
 
#define DPLL_READY_FLAG   (OSCCTRL_DPLLSTATUS_CLKRDY | OSCCTRL_DPLLSTATUS_LOCK)
 
#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)
 Aborts transfer on going on endpoint. More...
 
bool udd_ep_alloc (udd_ep_id_t ep, uint8_t bmAttributes, uint16_t MaxEndpointSize)
 Configures and enables an endpoint. More...
 
bool udd_ep_clear_halt (udd_ep_id_t ep)
 Clear the halted state of the endpoint ep. More...
 
void udd_ep_free (udd_ep_id_t ep)
 Disables an endpoint. More...
 
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)
 Check if the endpoint ep is halted. More...
 
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)
 Allows to receive or send data on an endpoint. More...
 
bool udd_ep_set_halt (udd_ep_id_t ep)
 Set the halted state of the endpoint ep. More...
 
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)
 Registers a callback to call when endpoint halt is cleared. More...
 
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

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

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

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

#define DFLL_READY_FLAG
Value:
(OSCCTRL_STATUS_DFLLRDY | \
OSCCTRL_STATUS_DFLLLCKF | OSCCTRL_STATUS_DFLLLCKC)
#define DPLL_READY_FLAG   (OSCCTRL_DPLLSTATUS_CLKRDY | OSCCTRL_DPLLSTATUS_LOCK)

Referenced by udd_wait_clock_ready().