1 /*** Configurable parameters ***/
3 #define USB_SELF_POWERED
4 #define USB_NUM_CONFIGURATIONS 1
7 /*** USB state structure ***/
10 * We have a single buffer for all control transfers.
11 * It must be able to contain:
13 * - 2-byte status replies
14 * - UTF-16 versions of all string descriptors
16 * In addition to that, its length must be even.
18 #define USB_EP0_BUF_SIZE 256
21 PCD_HandleTypeDef *hpcd;
22 byte state; // USB_STATE_xxx
23 byte pre_suspend_state;
27 byte ep0_state; // USB_EP0_xxx
28 u16 ep0_setup_data_length;
29 u16 ep0_remaining_length;
31 byte ep0_buf[USB_EP0_BUF_SIZE];
32 // Descriptor data to be filled by the user during usb_dev_reset()
33 const byte *desc_device;
34 const byte *desc_config;
35 const char * const *desc_string;
36 const byte *desc_languages;
39 u16 desc_string_items;
40 u16 desc_languages_len;
43 enum usb_device_state {
61 #define usb_debug debug_printf
63 static inline void usb_debug(char *msg, ...)
67 // Parsed setup request
68 struct setup_request {
76 /*** Functions provided by low-level code ***/
78 void usb_init(struct usb *usb, PCD_HandleTypeDef *hpcd);
79 void usb_start(struct usb *usb);
81 void usb_ctl_send_status(struct usb *usb);
82 void usb_ctl_recv_status(struct usb *usb);
83 void usb_ctl_send_data(struct usb *usb, const byte *data, uint len);
84 void usb_ctl_recv_data(struct usb *usb, byte *data, uint len);
85 void usb_ctl_error(struct usb *usb);
86 void usb_ctl_setup_error(struct usb *usb, struct setup_request *setup);
88 /*** Callbacks to user code ***/
91 void usb_dev_reset(struct usb *usb);
93 // Configure the device (usb->config is the selected configuration)
94 void usb_dev_configure(struct usb *usb);
96 // Un-configure the device (usb->config is the configuration we are leaving)
97 void usb_dev_unconfigure(struct usb *usb);
99 // Intercept a setup packet. Returns true if default processing should be skipped.
100 // Remember to check if usb->state == USB_STATE_CONFIGURED for most requests.
101 bool usb_dev_setup_hook(struct usb *usb, struct setup_request *setup);
103 // Finished receiving control packet data requested by usb_ctl_recv_data()
104 void usb_dev_ctl_recv_done(struct usb *usb);
106 // Finished sending control packet data requested by usb_ctl_send_data()
107 void usb_dev_ctl_send_done(struct usb *usb);
109 // Finished receiving data on a non-control endpoint
110 void usb_dev_recv_done(struct usb *usb, byte epnum);
112 // Finished sending data on a non-control endpoint
113 void usb_dev_send_done(struct usb *usb, byte epnum);
115 /*** Constants from USB specs ***/
117 #define USB_REQ_DIRECTION 0x80
120 USB_REQ_TYPE_STANDARD = 0x00,
121 USB_REQ_TYPE_CLASS = 0x20,
122 USB_REQ_TYPE_VENDOR = 0x40,
123 USB_REQ_TYPE_MASK = 0x60,
126 enum usb_req_recipient {
127 USB_REQ_RECIPIENT_DEVICE = 0x00,
128 USB_REQ_RECIPIENT_INTERFACE = 0x01,
129 USB_REQ_RECIPIENT_ENDPOINT = 0x02,
130 USB_REQ_RECIPIENT_MASK = 0x1f,
133 enum usb_req_standard {
134 USB_REQ_GET_STATUS = 0x00,
135 USB_REQ_CLEAR_FEATURE = 0x01,
136 USB_REQ_SET_FEATURE = 0x03,
137 USB_REQ_SET_ADDRESS = 0x05,
138 USB_REQ_GET_DESCRIPTOR = 0x06,
139 USB_REQ_SET_DESCRIPTOR = 0x07,
140 USB_REQ_GET_CONFIGURATION = 0x08,
141 USB_REQ_SET_CONFIGURATION = 0x09,
142 USB_REQ_GET_INTERFACE = 0x0A,
143 USB_REQ_SET_INTERFACE = 0x0B,
144 USB_REQ_SYNCH_FRAME = 0x0C,
148 USB_DESC_TYPE_DEVICE = 1,
149 USB_DESC_TYPE_CONFIGURATION = 2,
150 USB_DESC_TYPE_STRING = 3,
151 USB_DESC_TYPE_INTERFACE = 4,
152 USB_DESC_TYPE_ENDPOINT = 5,
153 USB_DESC_TYPE_DEVICE_QUALIFIER = 6,
154 USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION = 7,
155 USB_DESC_TYPE_BOS = 0x0F,
158 enum usb_dev_status {
159 USB_DEV_STATUS_REMOTE_WAKEUP = 2,
160 USB_DEV_STATUS_SELF_POWERED = 1,
164 USB_FEATURE_EP_HALT = 0,
165 USB_FEATURE_REMOTE_WAKEUP = 1,
166 USB_FEATURE_TEST_MODE = 2,
169 #define USB_HS_MAX_PACKET_SIZE 512
170 #define USB_FS_MAX_PACKET_SIZE 64
171 #define USB_MAX_EP0_SIZE 64
174 USB_EP_TYPE_CTRL = 0,
175 USB_EP_TYPE_ISOC = 1,
176 USB_EP_TYPE_BULK = 2,
177 USB_EP_TYPE_INTR = 3,
180 /*** Wrappers around HAL routines ***/
182 // Wrappers return HAL_OK / HAL_ERROR / HAL_BUSY / HAL_TIMEOUT
184 static inline HAL_StatusTypeDef usb_ep_open(struct usb *usb, byte ep_addr, byte ep_type, byte ep_max_size)
186 return HAL_PCD_EP_Open(usb->hpcd, ep_addr, ep_max_size, ep_type);
189 static inline HAL_StatusTypeDef usb_ep_close(struct usb *usb, byte ep_addr)
191 return HAL_PCD_EP_Close(usb->hpcd, ep_addr);
194 static inline HAL_StatusTypeDef usb_ep_flush(struct usb *usb, byte ep_addr)
196 return HAL_PCD_EP_Flush(usb->hpcd, ep_addr);
199 static inline HAL_StatusTypeDef usb_ep_stall(struct usb *usb, byte ep_addr)
201 return HAL_PCD_EP_SetStall(usb->hpcd, ep_addr);
204 static inline HAL_StatusTypeDef usb_ep_unstall(struct usb *usb, byte ep_addr)
206 return HAL_PCD_EP_ClrStall(usb->hpcd, ep_addr);
209 static inline int usb_ep_is_stalled(struct usb *usb, byte ep_addr)
211 return ((ep_addr & 0x80) ? usb->hpcd->IN_ep : usb->hpcd->OUT_ep) [ep_addr & 0x7f].is_stall;
214 static inline HAL_StatusTypeDef usb_ep_send(struct usb *usb, byte ep_addr, const byte *buf, u32 size)
216 return HAL_PCD_EP_Transmit(usb->hpcd, ep_addr, (byte *) buf, size);
219 static inline HAL_StatusTypeDef usb_ep_receive(struct usb *usb, byte ep_addr, byte *buf, u32 size)
221 return HAL_PCD_EP_Receive(usb->hpcd, ep_addr, buf, size);
224 static inline u32 usb_ep_received_size(struct usb *usb, byte ep_addr)
226 return HAL_PCD_EP_GetRxCount(usb->hpcd, ep_addr);