]> mj.ucw.cz Git - home-hw.git/blob - Inc/usb.h
ec7cc232f18cbe7d817638aa6481f87dbbf0b216
[home-hw.git] / Inc / usb.h
1 #define USB_SELF_POWERED
2
3 typedef unsigned int uint;
4 typedef uint8_t byte;
5 typedef uint16_t u16;
6 typedef int16_t s16;
7 typedef uint32_t u32;
8 typedef int32_t s32;
9
10 #define MIN(x,y) ((x) < (y) ? (x) : (y))
11 #define MAX(x,y) ((x) > (y) ? (x) : (y))
12
13 /*** USB state structure ***/
14
15 struct usb_endpoint {
16   u32 max_packet_size;
17   u32 total_length;
18   u32 remaining_length;
19 };
20
21 struct usb {
22   PCD_HandleTypeDef *hpcd;
23   byte state;                   // USB_STATE_xxx
24   byte pre_suspend_state;
25   byte address;
26   byte speed;                   // PCD_SPEED_xxx
27   byte config;
28   byte remote_wakeup;
29   byte ep0_state;               // USB_EP0_xxx
30   byte ep0_data_len;
31   struct usb_endpoint ep_in[15];
32   struct usb_endpoint ep_out[15];
33   byte status_buf[2];
34 };
35
36 void usb_init(struct usb *usb, PCD_HandleTypeDef *hpcd);
37
38 enum usb_device_state {
39   USB_STATE_DEFAULT,
40   USB_STATE_ADDRESSED,
41   USB_STATE_CONFIGURED,
42   USB_STATE_SUSPENDED,
43 };
44
45 enum usb_ep0_state {
46   USB_EP0_IDLE,
47   USB_EP0_SETUP,
48   USB_EP0_DATA_IN,
49   USB_EP0_DATA_OUT,
50   USB_EP0_STATUS_IN,
51   USB_EP0_STATUS_OUT,
52   USB_EP0_STALL,
53 };
54
55 /*** Constants from USB specs ***/
56
57 #define USB_REQ_DIRECTION 0x80
58
59 enum usb_req_type {
60   USB_REQ_TYPE_STANDARD = 0x00,
61   USB_REQ_TYPE_CLASS = 0x20,
62   USB_REQ_TYPE_VENDOR = 0x40,
63   USB_REQ_TYPE_MASK = 0x60,
64 };
65
66 enum usb_req_recipient {
67   USB_REQ_RECIPIENT_DEVICE = 0x00,
68   USB_REQ_RECIPIENT_INTERFACE = 0x01,
69   USB_REQ_RECIPIENT_ENDPOINT = 0x02,
70   USB_REQ_RECIPIENT_MASK = 0x1f,
71 };
72
73 enum usb_req_standard {
74   USB_REQ_GET_STATUS = 0x00,
75   USB_REQ_CLEAR_FEATURE = 0x01,
76   USB_REQ_SET_FEATURE = 0x03,
77   USB_REQ_SET_ADDRESS = 0x05,
78   USB_REQ_GET_DESCRIPTOR = 0x06,
79   USB_REQ_SET_DESCRIPTOR = 0x07,
80   USB_REQ_GET_CONFIGURATION = 0x08,
81   USB_REQ_SET_CONFIGURATION = 0x09,
82   USB_REQ_GET_INTERFACE = 0x0A,
83   USB_REQ_SET_INTERFACE = 0x0B,
84   USB_REQ_SYNCH_FRAME = 0x0C,
85 };
86
87 enum usb_desc_type {
88   USB_DESC_TYPE_DEVICE = 1,
89   USB_DESC_TYPE_CONFIGURATION = 2,
90   USB_DESC_TYPE_STRING = 3,
91   USB_DESC_TYPE_INTERFACE = 4,
92   USB_DESC_TYPE_ENDPOINT = 5,
93   USB_DESC_TYPE_DEVICE_QUALIFIER = 6,
94   USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION = 7,
95   USB_DESC_TYPE_BOS = 0x0F,
96 };
97
98 enum usb_dev_status {
99   USB_DEV_STATUS_REMOTE_WAKEUP = 2,
100   USB_DEV_STATUS_SELF_POWERED = 1,
101 };
102
103 enum usb_feature {
104   USB_FEATURE_EP_HALT = 0,
105   USB_FEATURE_REMOTE_WAKEUP = 1,
106   USB_FEATURE_TEST_MODE = 2,
107 };
108
109 #define USB_HS_MAX_PACKET_SIZE 512
110 #define USB_FS_MAX_PACKET_SIZE 64
111 #define USB_MAX_EP0_SIZE 64
112
113 enum usb_ep_type {
114   USB_EP_TYPE_CTRL = 0,
115   USB_EP_TYPE_ISOC = 1,
116   USB_EP_TYPE_BULK = 2,
117   USB_EP_TYPE_INTR = 3,
118 };
119
120 /*** Wrappers around HAL routines ***/
121
122 // Wrappers return HAL_OK / HAL_ERROR / HAL_BUSY / HAL_TIMEOUT
123
124 static inline HAL_StatusTypeDef usb_ep_open(struct usb *usb, byte ep_addr, byte ep_type, byte ep_max_size)
125 {
126   return HAL_PCD_EP_Open(usb->hpcd, ep_addr, ep_max_size, ep_type);
127 }
128
129 static inline HAL_StatusTypeDef usb_ep_close(struct usb *usb, byte ep_addr)
130 {
131   return HAL_PCD_EP_Close(usb->hpcd, ep_addr);
132 }
133
134 static inline HAL_StatusTypeDef usb_ep_flush(struct usb *usb, byte ep_addr)
135 {
136   return HAL_PCD_EP_Flush(usb->hpcd, ep_addr);
137 }
138
139 static inline HAL_StatusTypeDef usb_ep_stall(struct usb *usb, byte ep_addr)
140 {
141   return HAL_PCD_EP_SetStall(usb->hpcd, ep_addr);
142 }
143
144 static inline HAL_StatusTypeDef usb_ep_unstall(struct usb *usb, byte ep_addr)
145 {
146   return HAL_PCD_EP_ClrStall(usb->hpcd, ep_addr);
147 }
148
149 static inline int usb_ep_is_stalled(struct usb *usb, byte ep_addr)
150 {
151   return ((ep_addr & 0x80) ? usb->hpcd->IN_ep : usb->hpcd->OUT_ep) [ep_addr & 0x7f].is_stall;
152 }
153
154 static inline HAL_StatusTypeDef usb_ep_transmit(struct usb *usb, byte ep_addr, byte *buf, u32 size)
155 {
156   return HAL_PCD_EP_Transmit(usb->hpcd, ep_addr, buf, size);
157 }
158
159 static inline HAL_StatusTypeDef usb_ep_receive(struct usb *usb, byte ep_addr, byte *buf, u32 size)
160 {
161   return HAL_PCD_EP_Receive(usb->hpcd, ep_addr, buf, size);
162 }
163
164 static inline u32 usb_ep_received_size(struct usb *usb, byte ep_addr)
165 {
166   return HAL_PCD_EP_GetRxCount(usb->hpcd, ep_addr);
167 }