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