OpenOCD
usb.h File Reference
Include dependency graph for usb.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  setup_data
 USB Control Setup Data. More...
 
struct  usb_config_descriptor
 USB Configuration Descriptor. More...
 
struct  usb_device_descriptor
 USB Device Descriptor. More...
 
struct  usb_endpoint_descriptor
 USB Endpoint Descriptor. More...
 
struct  usb_interface_descriptor
 USB Interface Descriptor. More...
 
struct  usb_language_descriptor
 USB Language Descriptor. More...
 
struct  usb_string_descriptor
 USB String Descriptor. More...
 

Macros

#define CF_DEVICE   (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
 
#define CF_ENDPOINT   (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)
 
#define CF_INTERFACE   (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
 
#define CLEAR_FEATURE   1
 
#define CLEAR_IRQ()   (EXIF &= ~USBINT)
 
#define DESCRIPTOR_TYPE_CONFIGURATION   0x02
 
#define DESCRIPTOR_TYPE_DEVICE   0x01
 
#define DESCRIPTOR_TYPE_ENDPOINT   0x05
 
#define DESCRIPTOR_TYPE_INTERFACE   0x04
 
#define DESCRIPTOR_TYPE_STRING   0x03
 
#define DEVICE_REMOTE_WAKEUP   1
 
#define ENDPOINT_HALT   0
 
#define GC_DEVICE   (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
 
#define GD_DEVICE   (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
 
#define GET_CONFIGURATION   8
 
#define GET_DESCRIPTOR   6
 
#define GET_INTERFACE   10
 
#define GET_STATUS   0
 
#define GI_INTERFACE   (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
 
#define GS_DEVICE   (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
 
#define GS_ENDPOINT   (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)
 
#define GS_INTERFACE   (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
 
#define HI8(word)   (uint8_t)(((uint16_t)word >> 8) & 0xff)
 
#define LO8(word)   (uint8_t)((uint16_t)word & 0xff)
 
#define NULL   (void *)0;
 
#define SC_DEVICE   (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
 
#define SD_DEVICE   (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
 
#define SET_ADDRESS   5
 
#define SET_CONFIGURATION   9
 
#define SET_DESCRIPTOR   7
 
#define SET_FEATURE   3
 
#define SET_INTERFACE   11
 
#define SF_DEVICE   (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
 
#define SF_ENDPOINT   (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)
 
#define SF_INTERFACE   (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
 
#define SI_INTERFACE   (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
 
#define STALL_EP0()   (EP0CS |= EP0STALL)
 
#define STR_DESCR(len, ...)   { len * 2 + 2, DESCRIPTOR_TYPE_STRING, { __VA_ARGS__ } }
 
#define SY_ENDPOINT   (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)
 
#define SYNCH_FRAME   12
 
#define USB_DIR_IN   0x80
 
#define USB_DIR_OUT   0x00
 
#define USB_RECIP_DEVICE   0x00
 
#define USB_RECIP_ENDPOINT   0x02
 
#define USB_RECIP_INTERFACE   0x01
 
#define USB_RECIP_OTHER   0x03
 
#define USB_REQ_TYPE_CLASS   (0x01 << 5)
 
#define USB_REQ_TYPE_RESERVED   (0x03 << 5)
 
#define USB_REQ_TYPE_STANDARD   (0x00 << 5)
 
#define USB_REQ_TYPE_VENDOR   (0x02 << 5)
 

Enumerations

enum  usb_isr {
  SUDAV_ISR = 13 , SOF_ISR , SUTOK_ISR , SUSPEND_ISR ,
  USBRESET_ISR , IBN_ISR , EP0IN_ISR , EP0OUT_ISR ,
  EP1IN_ISR , EP1OUT_ISR , EP2IN_ISR , EP2OUT_ISR ,
  EP3IN_ISR , EP3OUT_ISR , EP4IN_ISR , EP4OUT_ISR ,
  EP5IN_ISR , EP5OUT_ISR , EP6IN_ISR , EP6OUT_ISR ,
  EP7IN_ISR , EP7OUT_ISR
}
 USB Interrupts. More...
 

Functions

__xdata uint8_t * usb_get_endpoint_cs_reg (uint8_t ep)
 Return the control/status register for an endpoint. More...
 
bool usb_handle_clear_feature (void)
 Handle CLEAR_FEATURE request. More...
 
bool usb_handle_get_descriptor (void)
 Handle GET_DESCRIPTOR request. More...
 
bool usb_handle_get_status (void)
 Handle GET_STATUS request. More...
 
bool usb_handle_set_feature (void)
 Handle SET_FEATURE request. More...
 
void usb_handle_set_interface (void)
 Handle SET_INTERFACE request. More...
 
void usb_handle_setup_data (void)
 Handle the arrival of a USB Control Setup Packet. More...
 
void usb_init (void)
 USB initialization. More...
 
void usb_reset_data_toggle (uint8_t ep)
 

Variables

volatile bool EP2_in
 
volatile bool EP2_out
 
volatile __xdata __at struct setup_data setup_data
 

Macro Definition Documentation

◆ CF_DEVICE

#define CF_DEVICE   (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)

Definition at line 156 of file usb.h.

◆ CF_ENDPOINT

#define CF_ENDPOINT   (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)

Definition at line 158 of file usb.h.

◆ CF_INTERFACE

#define CF_INTERFACE   (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)

Definition at line 157 of file usb.h.

◆ CLEAR_FEATURE

#define CLEAR_FEATURE   1

Definition at line 195 of file usb.h.

◆ CLEAR_IRQ

#define CLEAR_IRQ ( )    (EXIF &= ~USBINT)

Definition at line 24 of file usb.h.

◆ DESCRIPTOR_TYPE_CONFIGURATION

#define DESCRIPTOR_TYPE_CONFIGURATION   0x02

Definition at line 30 of file usb.h.

◆ DESCRIPTOR_TYPE_DEVICE

#define DESCRIPTOR_TYPE_DEVICE   0x01

Definition at line 29 of file usb.h.

◆ DESCRIPTOR_TYPE_ENDPOINT

#define DESCRIPTOR_TYPE_ENDPOINT   0x05

Definition at line 33 of file usb.h.

◆ DESCRIPTOR_TYPE_INTERFACE

#define DESCRIPTOR_TYPE_INTERFACE   0x04

Definition at line 32 of file usb.h.

◆ DESCRIPTOR_TYPE_STRING

#define DESCRIPTOR_TYPE_STRING   0x03

Definition at line 31 of file usb.h.

◆ DEVICE_REMOTE_WAKEUP

#define DEVICE_REMOTE_WAKEUP   1

Definition at line 209 of file usb.h.

◆ ENDPOINT_HALT

#define ENDPOINT_HALT   0

Definition at line 210 of file usb.h.

◆ GC_DEVICE

#define GC_DEVICE   (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)

Definition at line 161 of file usb.h.

◆ GD_DEVICE

#define GD_DEVICE   (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)

Definition at line 164 of file usb.h.

◆ GET_CONFIGURATION

#define GET_CONFIGURATION   8

Definition at line 202 of file usb.h.

◆ GET_DESCRIPTOR

#define GET_DESCRIPTOR   6

Definition at line 200 of file usb.h.

◆ GET_INTERFACE

#define GET_INTERFACE   10

Definition at line 204 of file usb.h.

◆ GET_STATUS

#define GET_STATUS   0

Definition at line 194 of file usb.h.

◆ GI_INTERFACE

#define GI_INTERFACE   (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)

Definition at line 167 of file usb.h.

◆ GS_DEVICE

#define GS_DEVICE   (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)

Definition at line 170 of file usb.h.

◆ GS_ENDPOINT

#define GS_ENDPOINT   (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)

Definition at line 172 of file usb.h.

◆ GS_INTERFACE

#define GS_INTERFACE   (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)

Definition at line 171 of file usb.h.

◆ HI8

#define HI8 (   word)    (uint8_t)(((uint16_t)word >> 8) & 0xff)

Definition at line 19 of file usb.h.

◆ LO8

#define LO8 (   word)    (uint8_t)((uint16_t)word & 0xff)

Definition at line 20 of file usb.h.

◆ NULL

#define NULL   (void *)0;

Definition at line 16 of file usb.h.

◆ SC_DEVICE

#define SC_DEVICE   (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)

Definition at line 177 of file usb.h.

◆ SD_DEVICE

#define SD_DEVICE   (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)

Definition at line 180 of file usb.h.

◆ SET_ADDRESS

#define SET_ADDRESS   5

Definition at line 199 of file usb.h.

◆ SET_CONFIGURATION

#define SET_CONFIGURATION   9

Definition at line 203 of file usb.h.

◆ SET_DESCRIPTOR

#define SET_DESCRIPTOR   7

Definition at line 201 of file usb.h.

◆ SET_FEATURE

#define SET_FEATURE   3

Definition at line 197 of file usb.h.

◆ SET_INTERFACE

#define SET_INTERFACE   11

Definition at line 205 of file usb.h.

◆ SF_DEVICE

#define SF_DEVICE   (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)

Definition at line 183 of file usb.h.

◆ SF_ENDPOINT

#define SF_ENDPOINT   (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)

Definition at line 185 of file usb.h.

◆ SF_INTERFACE

#define SF_INTERFACE   (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)

Definition at line 184 of file usb.h.

◆ SI_INTERFACE

#define SI_INTERFACE   (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)

Definition at line 188 of file usb.h.

◆ STALL_EP0

#define STALL_EP0 ( )    (EP0CS |= EP0STALL)

Definition at line 23 of file usb.h.

◆ STR_DESCR

#define STR_DESCR (   len,
  ... 
)    { len * 2 + 2, DESCRIPTOR_TYPE_STRING, { __VA_ARGS__ } }

Definition at line 35 of file usb.h.

◆ SY_ENDPOINT

#define SY_ENDPOINT   (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)

Definition at line 191 of file usb.h.

◆ SYNCH_FRAME

#define SYNCH_FRAME   12

Definition at line 206 of file usb.h.

◆ USB_DIR_IN

#define USB_DIR_IN   0x80

Definition at line 141 of file usb.h.

◆ USB_DIR_OUT

#define USB_DIR_OUT   0x00

Definition at line 140 of file usb.h.

◆ USB_RECIP_DEVICE

#define USB_RECIP_DEVICE   0x00

Definition at line 148 of file usb.h.

◆ USB_RECIP_ENDPOINT

#define USB_RECIP_ENDPOINT   0x02

Definition at line 150 of file usb.h.

◆ USB_RECIP_INTERFACE

#define USB_RECIP_INTERFACE   0x01

Definition at line 149 of file usb.h.

◆ USB_RECIP_OTHER

#define USB_RECIP_OTHER   0x03

Definition at line 151 of file usb.h.

◆ USB_REQ_TYPE_CLASS

#define USB_REQ_TYPE_CLASS   (0x01 << 5)

Definition at line 144 of file usb.h.

◆ USB_REQ_TYPE_RESERVED

#define USB_REQ_TYPE_RESERVED   (0x03 << 5)

Definition at line 146 of file usb.h.

◆ USB_REQ_TYPE_STANDARD

#define USB_REQ_TYPE_STANDARD   (0x00 << 5)

Definition at line 143 of file usb.h.

◆ USB_REQ_TYPE_VENDOR

#define USB_REQ_TYPE_VENDOR   (0x02 << 5)

Definition at line 145 of file usb.h.

Enumeration Type Documentation

◆ usb_isr

enum usb_isr

USB Interrupts.

See AN2131-TRM, page 9-4 for details

Enumerator
SUDAV_ISR 
SOF_ISR 
SUTOK_ISR 
SUSPEND_ISR 
USBRESET_ISR 
IBN_ISR 
EP0IN_ISR 
EP0OUT_ISR 
EP1IN_ISR 
EP1OUT_ISR 
EP2IN_ISR 
EP2OUT_ISR 
EP3IN_ISR 
EP3OUT_ISR 
EP4IN_ISR 
EP4OUT_ISR 
EP5IN_ISR 
EP5OUT_ISR 
EP6IN_ISR 
EP6OUT_ISR 
EP7IN_ISR 
EP7OUT_ISR 

Definition at line 215 of file usb.h.

Function Documentation

◆ usb_get_endpoint_cs_reg()

__xdata uint8_t* usb_get_endpoint_cs_reg ( uint8_t  ep)

Return the control/status register for an endpoint.

Parameters
ependpoint address
Returns
on success: pointer to Control & Status register for endpoint specified in ep
on failure: NULL

Definition at line 221 of file usb.c.

References NULL.

Referenced by usb_handle_clear_feature(), usb_handle_get_status(), and usb_handle_set_feature().

◆ usb_handle_clear_feature()

bool usb_handle_clear_feature ( void  )

Handle CLEAR_FEATURE request.

Returns
on success: true
on failure: false

Definition at line 334 of file usb.c.

References setup_data::bmRequestType, CF_DEVICE, CF_ENDPOINT, EPSTALL, STALL_EP0, usb_get_endpoint_cs_reg(), setup_data::wIndex, and setup_data::wValue.

Referenced by usb_handle_setup_data().

◆ usb_handle_get_descriptor()

bool usb_handle_get_descriptor ( void  )

Handle GET_DESCRIPTOR request.

Returns
on success: true
on failure: false

Definition at line 403 of file usb.c.

References config_descriptor, DESCRIPTOR_TYPE_CONFIGURATION, DESCRIPTOR_TYPE_DEVICE, DESCRIPTOR_TYPE_STRING, device_descriptor, en_string_descriptors, HI8, language_descriptor, LO8, setup_data::wIndex, and setup_data::wValue.

Referenced by usb_handle_setup_data().

◆ usb_handle_get_status()

bool usb_handle_get_status ( void  )

Handle GET_STATUS request.

Returns
on success: true
on failure: false

Definition at line 283 of file usb.c.

References setup_data::bmRequestType, EPSTALL, GS_DEVICE, GS_ENDPOINT, GS_INTERFACE, usb_get_endpoint_cs_reg(), and setup_data::wIndex.

Referenced by usb_handle_setup_data().

◆ usb_handle_set_feature()

bool usb_handle_set_feature ( void  )

Handle SET_FEATURE request.

Returns
on success: true
on failure: false

Definition at line 368 of file usb.c.

References setup_data::bmRequestType, EPSTALL, SF_DEVICE, SF_ENDPOINT, usb_get_endpoint_cs_reg(), setup_data::wIndex, and setup_data::wValue.

Referenced by usb_handle_setup_data().

◆ usb_handle_set_interface()

void usb_handle_set_interface ( void  )

Handle SET_INTERFACE request.

Definition at line 444 of file usb.c.

References EPBSY, USB_DIR_IN, USB_DIR_OUT, and usb_reset_data_toggle().

Referenced by usb_handle_setup_data().

◆ usb_handle_setup_data()

◆ usb_init()

void usb_init ( void  )

USB initialization.

Configures USB interrupts, endpoints and performs ReNumeration.

Definition at line 520 of file usb.c.

References AVEN, delay_ms(), DISCOE, DISCON, IN2IEN, IN2VAL, ISODISAB, OUT2IEN, OUT2VAL, RENUM, and SUDAVIE.

Referenced by main().

◆ usb_reset_data_toggle()

void usb_reset_data_toggle ( uint8_t  ep)

Definition at line 256 of file usb.c.

References TOG_R.

Referenced by usb_handle_set_interface().

Variable Documentation

◆ EP2_in

volatile bool EP2_in
extern

Definition at line 25 of file usb.c.

Referenced by command_loop(), and ep2in_isr().

◆ EP2_out

volatile bool EP2_out
extern

Definition at line 24 of file usb.c.

Referenced by command_loop(), and ep2out_isr().

◆ setup_data

volatile __xdata __at struct setup_data setup_data
extern

Definition at line 27 of file usb.c.