1 #define USB_SELF_POWERED
2 #define USB_NUM_CONFIGURATIONS 1
4 typedef unsigned int uint;
11 #define MIN(x,y) ((x) < (y) ? (x) : (y))
12 #define MAX(x,y) ((x) > (y) ? (x) : (y))
14 /*** USB state structure ***/
17 * We have a single buffer for all control transfers.
18 * It must be able to contain:
20 * - 2-byte status replies
21 * - UTF-16 versions of all string descriptors
23 * In addition to that, its length must be even.
25 #define USB_EP0_BUF_SIZE 256
28 PCD_HandleTypeDef *hpcd;
29 byte state; // USB_STATE_xxx
30 byte pre_suspend_state;
34 byte ep0_state; // USB_EP0_xxx
35 u16 ep0_setup_data_length;
36 u16 ep0_remaining_length;
38 byte ep0_buf[USB_EP0_BUF_SIZE];
41 void usb_init(struct usb *usb, PCD_HandleTypeDef *hpcd);
43 enum usb_device_state {
60 /*** Constants from USB specs ***/
62 #define USB_REQ_DIRECTION 0x80
65 USB_REQ_TYPE_STANDARD = 0x00,
66 USB_REQ_TYPE_CLASS = 0x20,
67 USB_REQ_TYPE_VENDOR = 0x40,
68 USB_REQ_TYPE_MASK = 0x60,
71 enum usb_req_recipient {
72 USB_REQ_RECIPIENT_DEVICE = 0x00,
73 USB_REQ_RECIPIENT_INTERFACE = 0x01,
74 USB_REQ_RECIPIENT_ENDPOINT = 0x02,
75 USB_REQ_RECIPIENT_MASK = 0x1f,
78 enum usb_req_standard {
79 USB_REQ_GET_STATUS = 0x00,
80 USB_REQ_CLEAR_FEATURE = 0x01,
81 USB_REQ_SET_FEATURE = 0x03,
82 USB_REQ_SET_ADDRESS = 0x05,
83 USB_REQ_GET_DESCRIPTOR = 0x06,
84 USB_REQ_SET_DESCRIPTOR = 0x07,
85 USB_REQ_GET_CONFIGURATION = 0x08,
86 USB_REQ_SET_CONFIGURATION = 0x09,
87 USB_REQ_GET_INTERFACE = 0x0A,
88 USB_REQ_SET_INTERFACE = 0x0B,
89 USB_REQ_SYNCH_FRAME = 0x0C,
93 USB_DESC_TYPE_DEVICE = 1,
94 USB_DESC_TYPE_CONFIGURATION = 2,
95 USB_DESC_TYPE_STRING = 3,
96 USB_DESC_TYPE_INTERFACE = 4,
97 USB_DESC_TYPE_ENDPOINT = 5,
98 USB_DESC_TYPE_DEVICE_QUALIFIER = 6,
99 USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION = 7,
100 USB_DESC_TYPE_BOS = 0x0F,
103 enum usb_dev_status {
104 USB_DEV_STATUS_REMOTE_WAKEUP = 2,
105 USB_DEV_STATUS_SELF_POWERED = 1,
109 USB_FEATURE_EP_HALT = 0,
110 USB_FEATURE_REMOTE_WAKEUP = 1,
111 USB_FEATURE_TEST_MODE = 2,
114 #define USB_HS_MAX_PACKET_SIZE 512
115 #define USB_FS_MAX_PACKET_SIZE 64
116 #define USB_MAX_EP0_SIZE 64
119 USB_EP_TYPE_CTRL = 0,
120 USB_EP_TYPE_ISOC = 1,
121 USB_EP_TYPE_BULK = 2,
122 USB_EP_TYPE_INTR = 3,
125 /*** Wrappers around HAL routines ***/
127 // Wrappers return HAL_OK / HAL_ERROR / HAL_BUSY / HAL_TIMEOUT
129 static inline HAL_StatusTypeDef usb_ep_open(struct usb *usb, byte ep_addr, byte ep_type, byte ep_max_size)
131 return HAL_PCD_EP_Open(usb->hpcd, ep_addr, ep_max_size, ep_type);
134 static inline HAL_StatusTypeDef usb_ep_close(struct usb *usb, byte ep_addr)
136 return HAL_PCD_EP_Close(usb->hpcd, ep_addr);
139 static inline HAL_StatusTypeDef usb_ep_flush(struct usb *usb, byte ep_addr)
141 return HAL_PCD_EP_Flush(usb->hpcd, ep_addr);
144 static inline HAL_StatusTypeDef usb_ep_stall(struct usb *usb, byte ep_addr)
146 return HAL_PCD_EP_SetStall(usb->hpcd, ep_addr);
149 static inline HAL_StatusTypeDef usb_ep_unstall(struct usb *usb, byte ep_addr)
151 return HAL_PCD_EP_ClrStall(usb->hpcd, ep_addr);
154 static inline int usb_ep_is_stalled(struct usb *usb, byte ep_addr)
156 return ((ep_addr & 0x80) ? usb->hpcd->IN_ep : usb->hpcd->OUT_ep) [ep_addr & 0x7f].is_stall;
159 static inline HAL_StatusTypeDef usb_ep_transmit(struct usb *usb, byte ep_addr, const byte *buf, u32 size)
161 return HAL_PCD_EP_Transmit(usb->hpcd, ep_addr, (byte *) buf, size);
164 static inline HAL_StatusTypeDef usb_ep_receive(struct usb *usb, byte ep_addr, byte *buf, u32 size)
166 return HAL_PCD_EP_Receive(usb->hpcd, ep_addr, buf, size);
169 static inline u32 usb_ep_received_size(struct usb *usb, byte ep_addr)
171 return HAL_PCD_EP_GetRxCount(usb->hpcd, ep_addr);