Microchip® Advanced Software Framework

usbhs_device.c File Reference

USB Device Driver for USBHS.

Compliant with common UDD driver.

Copyright (c) 2015-2018 Microchip Technology Inc. and its subsidiaries.

#include "conf_usb.h"
#include "sysclk.h"
#include "udd.h"
#include "usbhs_otg.h"
#include "usbhs_device.h"
#include <string.h>
#include "sleep.h"
#include "sleepmgr.h"

Data Structures

struct  udd_ep_job_t
 Structure definition about job registered on an endpoint. More...
 

Macros

#define dbg_print(...)
 
#define UDD_BULK_NB_BANK(ep)   2
 
#define UDD_BULK_NB_BANK_ERROR
 
#define UDD_EP_BULK_NB_BANK_ERROR(ep)   (UDD_EP_USED(ep) && UDD_EP_ISO_NBANK_ERROR(ep))
 
#define UDD_EP_BULK_NBANK_ERROR(ep)   ( (UDD_BULK_NB_BANK(ep) < 1) || (UDD_BULK_NB_BANK(ep) > 2) )
 
#define UDD_EP_DMA_SUPPORTED
 
#define UDD_EP_INT_NB_BANK_ERROR(ep)   (UDD_EP_USED(ep) && UDD_EP_ISO_NBANK_ERROR(ep))
 
#define UDD_EP_INT_NBANK_ERROR(ep)   ( (UDD_INTERRUPT_NB_BANK(ep) < 1) || (UDD_INTERRUPT_NB_BANK(ep) > 2) )
 
#define UDD_EP_ISO_NB_BANK_ERROR(ep)   (UDD_EP_USED(ep) && UDD_EP_ISO_NBANK_ERROR(ep))
 
#define UDD_EP_ISO_NBANK_ERROR(ep)
 
#define UDD_EP_NB_BANK_ERROR(ep, type)   (ATPASTE3(UDD_EP_, type, _NB_BANK_ERROR(ep)))
 
#define UDD_EP_USED(ep)   (USB_DEVICE_MAX_EP >= ep)
 
#define UDD_INTERRUPT_NB_BANK(ep)   1
 
#define UDD_INTERRUPT_NB_BANK_ERROR
 
#define UDD_ISO_NB_BANK_ERROR
 
#define UDD_ISOCHRONOUS_NB_BANK(ep)   2
 
#define UDD_USB_INT_FUN   USBHS_Handler
 
#define UDD_USB_INT_LEVEL   5
 

Functions

static void _dcache_flush (void *addr, uint32_t dsize)
 Flush content in DCache to physical memory. More...
 
static void _dcache_invalidate (void *addr, uint32_t dsize)
 Invalidate DCache of physical memory. More...
 
static void _dcache_invalidate_prepare (void *addr, uint32_t dsize)
 Preparation for unaligned buffer invalidation. More...
 
 if (udd_ctrl_interrupt())
 Function called by USBHS interrupt to manage USB Device interrupts. More...
 
 if (udd_ep_interrupt())
 
 if (Is_udd_reset())
 
 if (Is_udd_suspend_interrupt_enabled()&&Is_udd_suspend())
 
 if (Is_udd_wake_up_interrupt_enabled()&&Is_udd_wake_up())
 
void udd_attach (void)
 Attach device to the bus when possible. 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...
 
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...
 
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...
 
void udd_test_mode_j (void)
 
void udd_test_mode_k (void)
 
void udd_test_mode_packet (void)
 
void udd_test_mode_se0_nak (void)
 

Variables

udd_interrupt_end __pad0__
 
udd_interrupt_sof_end __pad1__
 

Power management routine.

#define USBHS_SLEEP_MODE_USB_SUSPEND   SLEEPMGR_WAIT_FAST
 Definition of sleep levels. More...
 
#define USBHS_SLEEP_MODE_USB_IDLE   SLEEPMGR_SLEEP_WFI
 
static bool udd_b_idle
 State of USB line. More...
 
static bool udd_b_sleep_initialized = false
 State of sleep manager. More...
 
static void udd_sleep_mode (bool b_idle)
 Authorize or not the CPU powerdown mode. More...
 

VBus monitor routine

#define udd_vbus_monitor_sleep_mode(lock)
 
static void udd_vbus_handler (uint32_t id, uint32_t mask)
 USB VBus pin change handler. More...
 

Control endpoint low level management routine.

This function performs control endpoint mangement.

It handle 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...
 
COMPILER_WORD_ALIGNED
udd_ctrl_request_t 
udd_g_ctrlreq
 Global variable to give and record information about setup 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_buf_cnt
 Total number of data received/sent during data packet phase with previous payload buffers. More...
 
static uint16_t udd_ctrl_payload_buf_cnt
 Number of data received/sent to/from udd_g_ctrlreq.payload buffer. More...
 
static void udd_reset_ep_ctrl (void)
 Reset control endpoint. More...
 
static void udd_ctrl_init (void)
 Reset control endpoint management. More...
 
static void udd_ctrl_setup_received (void)
 Managed reception of SETUP packet on control endpoint. More...
 
static void udd_ctrl_in_sent (void)
 Managed reception of IN packet on control endpoint. More...
 
static void udd_ctrl_out_received (void)
 Managed reception of OUT packet on control endpoint. More...
 
static void udd_ctrl_underflow (void)
 Managed underflow event of IN packet on control endpoint. More...
 
static void udd_ctrl_overflow (void)
 Managed overflow event of OUT packet on control endpoint. More...
 
static void udd_ctrl_stall_data (void)
 Managed stall event of IN/OUT packet on control endpoint. More...
 
static void udd_ctrl_send_zlp_in (void)
 Send a ZLP IN on control endpoint. More...
 
static void udd_ctrl_send_zlp_out (void)
 Send a ZLP OUT on control endpoint. More...
 
static void udd_ctrl_endofrequest (void)
 Call callback associated to setup request. More...
 
static bool udd_ctrl_interrupt (void)
 Main interrupt routine for control endpoint. More...
 

Management of bulk/interrupt/isochronous endpoints

The UDD manages the data transfer on endpoints:

  • Start data tranfer 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.
static udd_ep_job_t udd_ep_job [USB_DEVICE_MAX_EP]
 Array to register a job on bulk/interrupt/isochronous endpoint. More...
 
static void udd_ep_job_table_reset (void)
 Reset all job table. More...
 
static void udd_ep_job_table_kill (void)
 Abort all endpoint jobs on going. More...
 
static void udd_ep_abort_job (udd_ep_id_t ep)
 Abort endpoint job on going. More...
 
static void udd_ep_finish_job (udd_ep_job_t *ptr_job, bool b_abort, uint8_t ep_num)
 Call the callback associated to the job which is finished. More...
 
static void udd_ep_trans_done (udd_ep_id_t ep)
 Start the next transfer if necessary or complet the job associated. More...
 
static bool udd_ep_interrupt (void)
 Main interrupt routine for bulk/interrupt/isochronous endpoints. More...
 

#define UDD_EP_DMA_SUPPORTED
#define UDD_EP_USED (   ep)    (USB_DEVICE_MAX_EP >= ep)
#define UDD_USB_INT_FUN   USBHS_Handler
#define UDD_USB_INT_LEVEL   5

Referenced by udd_enable().

static void _dcache_flush ( void *  addr,
uint32_t  dsize 
)
static

Flush content in DCache to physical memory.

Parameters
addrThe memory address to flush
dsizeThe size of the memory

Referenced by udd_ep_run().

static void _dcache_invalidate ( void *  addr,
uint32_t  dsize 
)
static

Invalidate DCache of physical memory.

Parameters
addrThe memory address to invalidate
dsizeThe size of the memory

Referenced by udd_ep_trans_done().

static void _dcache_invalidate_prepare ( void *  addr,
uint32_t  dsize 
)
static

Preparation for unaligned buffer invalidation.

Parameters
addrThe memory address to invalidate
dsizeThe size of the memory

Referenced by udd_ep_run().