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