]> mj.ucw.cz Git - home-hw.git/blob - Inc/usb.h
Generated TIM4 setup
[home-hw.git] / Inc / usb.h
1 /*** Configurable parameters ***/
2
3 #define USB_SELF_POWERED
4 #define USB_NUM_CONFIGURATIONS 1
5 #define USB_DEBUG
6
7 /*** USB state structure ***/
8
9 /*
10  *  We have a single buffer for all control transfers.
11  *  It must be able to contain:
12  *
13  *    - 2-byte status replies
14  *    - UTF-16 versions of all string descriptors
15  *
16  *  In addition to that, its length must be even.
17  */
18 #define USB_EP0_BUF_SIZE 256
19
20 struct usb {
21   PCD_HandleTypeDef *hpcd;
22   byte state;                   // USB_STATE_xxx
23   byte pre_suspend_state;
24   byte address;
25   byte config;
26   byte remote_wakeup;
27   byte ep0_state;               // USB_EP0_xxx
28   u16 ep0_setup_data_length;
29   u16 ep0_remaining_length;
30   u16 ep0_total_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;
37   u16 desc_device_len;
38   u16 desc_config_len;
39   u16 desc_string_items;
40   u16 desc_languages_len;
41 };
42
43 enum usb_device_state {
44   USB_STATE_DEFAULT,
45   USB_STATE_ADDRESSED,
46   USB_STATE_CONFIGURED,
47   USB_STATE_SUSPENDED,
48 };
49
50 enum usb_ep0_state {
51   USB_EP0_IDLE,
52   USB_EP0_SETUP,
53   USB_EP0_DATA_IN,
54   USB_EP0_DATA_OUT,
55   USB_EP0_STATUS_IN,
56   USB_EP0_STATUS_OUT,
57   USB_EP0_STALL,
58 };
59
60 #ifdef USB_DEBUG
61 #define usb_debug debug_printf
62 #else
63 static inline void usb_debug(char *msg, ...)
64 { }
65 #endif
66
67 // Parsed setup request
68 struct setup_request {
69   byte bmRequest;
70   byte bRequest;
71   u16 wValue;
72   u16 wIndex;
73   u16 wLength;
74 };
75
76 /*** Functions provided by low-level code ***/
77
78 void usb_init(struct usb *usb, PCD_HandleTypeDef *hpcd);
79 void usb_start(struct usb *usb);
80
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);
87
88 /*** Callbacks to user code ***/
89
90 // Device was reset
91 void usb_dev_reset(struct usb *usb);
92
93 // Configure the device (usb->config is the selected configuration)
94 void usb_dev_configure(struct usb *usb);
95
96 // Un-configure the device (usb->config is the configuration we are leaving)
97 void usb_dev_unconfigure(struct usb *usb);
98
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);
102
103 // Finished receiving control packet data requested by usb_ctl_recv_data()
104 void usb_dev_ctl_recv_done(struct usb *usb);
105
106 // Finished sending control packet data requested by usb_ctl_send_data()
107 void usb_dev_ctl_send_done(struct usb *usb);
108
109 // Finished receiving data on a non-control endpoint
110 void usb_dev_recv_done(struct usb *usb, byte epnum);
111
112 // Finished sending data on a non-control endpoint
113 void usb_dev_send_done(struct usb *usb, byte epnum);
114
115 /*** Constants from USB specs ***/
116
117 #define USB_REQ_DIRECTION 0x80
118
119 enum usb_req_type {
120   USB_REQ_TYPE_STANDARD = 0x00,
121   USB_REQ_TYPE_CLASS = 0x20,
122   USB_REQ_TYPE_VENDOR = 0x40,
123   USB_REQ_TYPE_MASK = 0x60,
124 };
125
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,
131 };
132
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,
145 };
146
147 enum usb_desc_type {
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,
156 };
157
158 enum usb_dev_status {
159   USB_DEV_STATUS_REMOTE_WAKEUP = 2,
160   USB_DEV_STATUS_SELF_POWERED = 1,
161 };
162
163 enum usb_feature {
164   USB_FEATURE_EP_HALT = 0,
165   USB_FEATURE_REMOTE_WAKEUP = 1,
166   USB_FEATURE_TEST_MODE = 2,
167 };
168
169 #define USB_HS_MAX_PACKET_SIZE 512
170 #define USB_FS_MAX_PACKET_SIZE 64
171 #define USB_MAX_EP0_SIZE 64
172
173 enum usb_ep_type {
174   USB_EP_TYPE_CTRL = 0,
175   USB_EP_TYPE_ISOC = 1,
176   USB_EP_TYPE_BULK = 2,
177   USB_EP_TYPE_INTR = 3,
178 };
179
180 /*** Wrappers around HAL routines ***/
181
182 // Wrappers return HAL_OK / HAL_ERROR / HAL_BUSY / HAL_TIMEOUT
183
184 static inline HAL_StatusTypeDef usb_ep_open(struct usb *usb, byte ep_addr, byte ep_type, byte ep_max_size)
185 {
186   return HAL_PCD_EP_Open(usb->hpcd, ep_addr, ep_max_size, ep_type);
187 }
188
189 static inline HAL_StatusTypeDef usb_ep_close(struct usb *usb, byte ep_addr)
190 {
191   return HAL_PCD_EP_Close(usb->hpcd, ep_addr);
192 }
193
194 static inline HAL_StatusTypeDef usb_ep_flush(struct usb *usb, byte ep_addr)
195 {
196   return HAL_PCD_EP_Flush(usb->hpcd, ep_addr);
197 }
198
199 static inline HAL_StatusTypeDef usb_ep_stall(struct usb *usb, byte ep_addr)
200 {
201   return HAL_PCD_EP_SetStall(usb->hpcd, ep_addr);
202 }
203
204 static inline HAL_StatusTypeDef usb_ep_unstall(struct usb *usb, byte ep_addr)
205 {
206   return HAL_PCD_EP_ClrStall(usb->hpcd, ep_addr);
207 }
208
209 static inline int usb_ep_is_stalled(struct usb *usb, byte ep_addr)
210 {
211   return ((ep_addr & 0x80) ? usb->hpcd->IN_ep : usb->hpcd->OUT_ep) [ep_addr & 0x7f].is_stall;
212 }
213
214 static inline HAL_StatusTypeDef usb_ep_send(struct usb *usb, byte ep_addr, const byte *buf, u32 size)
215 {
216   return HAL_PCD_EP_Transmit(usb->hpcd, ep_addr, (byte *) buf, size);
217 }
218
219 static inline HAL_StatusTypeDef usb_ep_receive(struct usb *usb, byte ep_addr, byte *buf, u32 size)
220 {
221   return HAL_PCD_EP_Receive(usb->hpcd, ep_addr, buf, size);
222 }
223
224 static inline u32 usb_ep_received_size(struct usb *usb, byte ep_addr)
225 {
226   return HAL_PCD_EP_GetRxCount(usb->hpcd, ep_addr);
227 }