]> mj.ucw.cz Git - home-hw.git/blob - Inc/usb.h
1d8291ff61e53a580a8be62fcd66bac1e1d1d86c
[home-hw.git] / Inc / usb.h
1 #define USB_SELF_POWERED
2 #define USB_NUM_CONFIGURATIONS 1
3
4 typedef unsigned int uint;
5 typedef uint8_t byte;
6 typedef uint16_t u16;
7 typedef int16_t s16;
8 typedef uint32_t u32;
9 typedef int32_t s32;
10
11 #define MIN(x,y) ((x) < (y) ? (x) : (y))
12 #define MAX(x,y) ((x) > (y) ? (x) : (y))
13
14 /*** USB state structure ***/
15
16 /*
17  *  We have a single buffer for all control transfers.
18  *  It must be able to contain:
19  *
20  *    - 2-byte status replies
21  *    - UTF-16 versions of all string descriptors
22  *
23  *  In addition to that, its length must be even.
24  */
25 #define USB_EP0_BUF_SIZE 256
26
27 struct usb {
28   PCD_HandleTypeDef *hpcd;
29   byte state;                   // USB_STATE_xxx
30   byte pre_suspend_state;
31   byte address;
32   byte config;
33   byte remote_wakeup;
34   byte ep0_state;               // USB_EP0_xxx
35   u16 ep0_setup_data_length;
36   u16 ep0_remaining_length;
37   u16 ep0_total_length;
38   byte ep0_buf[USB_EP0_BUF_SIZE];
39 };
40
41 void usb_init(struct usb *usb, PCD_HandleTypeDef *hpcd);
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 /*** Constants from USB specs ***/
61
62 #define USB_REQ_DIRECTION 0x80
63
64 enum usb_req_type {
65   USB_REQ_TYPE_STANDARD = 0x00,
66   USB_REQ_TYPE_CLASS = 0x20,
67   USB_REQ_TYPE_VENDOR = 0x40,
68   USB_REQ_TYPE_MASK = 0x60,
69 };
70
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,
76 };
77
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,
90 };
91
92 enum usb_desc_type {
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,
101 };
102
103 enum usb_dev_status {
104   USB_DEV_STATUS_REMOTE_WAKEUP = 2,
105   USB_DEV_STATUS_SELF_POWERED = 1,
106 };
107
108 enum usb_feature {
109   USB_FEATURE_EP_HALT = 0,
110   USB_FEATURE_REMOTE_WAKEUP = 1,
111   USB_FEATURE_TEST_MODE = 2,
112 };
113
114 #define USB_HS_MAX_PACKET_SIZE 512
115 #define USB_FS_MAX_PACKET_SIZE 64
116 #define USB_MAX_EP0_SIZE 64
117
118 enum usb_ep_type {
119   USB_EP_TYPE_CTRL = 0,
120   USB_EP_TYPE_ISOC = 1,
121   USB_EP_TYPE_BULK = 2,
122   USB_EP_TYPE_INTR = 3,
123 };
124
125 /*** Wrappers around HAL routines ***/
126
127 // Wrappers return HAL_OK / HAL_ERROR / HAL_BUSY / HAL_TIMEOUT
128
129 static inline HAL_StatusTypeDef usb_ep_open(struct usb *usb, byte ep_addr, byte ep_type, byte ep_max_size)
130 {
131   return HAL_PCD_EP_Open(usb->hpcd, ep_addr, ep_max_size, ep_type);
132 }
133
134 static inline HAL_StatusTypeDef usb_ep_close(struct usb *usb, byte ep_addr)
135 {
136   return HAL_PCD_EP_Close(usb->hpcd, ep_addr);
137 }
138
139 static inline HAL_StatusTypeDef usb_ep_flush(struct usb *usb, byte ep_addr)
140 {
141   return HAL_PCD_EP_Flush(usb->hpcd, ep_addr);
142 }
143
144 static inline HAL_StatusTypeDef usb_ep_stall(struct usb *usb, byte ep_addr)
145 {
146   return HAL_PCD_EP_SetStall(usb->hpcd, ep_addr);
147 }
148
149 static inline HAL_StatusTypeDef usb_ep_unstall(struct usb *usb, byte ep_addr)
150 {
151   return HAL_PCD_EP_ClrStall(usb->hpcd, ep_addr);
152 }
153
154 static inline int usb_ep_is_stalled(struct usb *usb, byte ep_addr)
155 {
156   return ((ep_addr & 0x80) ? usb->hpcd->IN_ep : usb->hpcd->OUT_ep) [ep_addr & 0x7f].is_stall;
157 }
158
159 static inline HAL_StatusTypeDef usb_ep_transmit(struct usb *usb, byte ep_addr, const byte *buf, u32 size)
160 {
161   return HAL_PCD_EP_Transmit(usb->hpcd, ep_addr, (byte *) buf, size);
162 }
163
164 static inline HAL_StatusTypeDef usb_ep_receive(struct usb *usb, byte ep_addr, byte *buf, u32 size)
165 {
166   return HAL_PCD_EP_Receive(usb->hpcd, ep_addr, buf, size);
167 }
168
169 static inline u32 usb_ep_received_size(struct usb *usb, byte ep_addr)
170 {
171   return HAL_PCD_EP_GetRxCount(usb->hpcd, ep_addr);
172 }