contrib/firmware: add new i2c bit-banging feature to angie's firmware
[openocd.git] / contrib / firmware / angie / c / include / usb.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /****************************************************************************
3 File : usb.h *
4 Contents : usb communication handling header file for NanoXplore *
5 USB-JTAG ANGIE adapter hardware. *
6 Based on openULINK project code by: Martin Schmoelzer. *
7 Copyright 2023, Ahmed Errached BOUDJELIDA, NanoXplore SAS. *
8 <aboudjelida@nanoxplore.com> *
9 <ahmederrachedbjld@gmail.com> *
10 *****************************************************************************/
11
12 #ifndef __USB_H
13 #define __USB_H
14
15 #include "reg_ezusb.h"
16 #include <stdint.h>
17 #include <stdbool.h>
18
19 /* High and Low byte of a word (uint16_t) */
20 #define HI8(word) (uint8_t)(((uint16_t)(word) >> 8) & 0xff)
21 #define LO8(word) (uint8_t)((uint16_t)(word) & 0xff)
22
23 /* Convenience functions */
24 #define STALL_EP0() (EP0CS |= EPSTALL)
25 #define CLEAR_IRQ() (USBINT = 0)
26
27 /*********** USB descriptors. See USB 2.0 Spec **********/
28
29 /* USB Descriptor Types. See USB 2.0 Spec */
30 #define DESCRIPTOR_TYPE_DEVICE 0x01
31 #define DESCRIPTOR_TYPE_CONFIGURATION 0x02
32 #define DESCRIPTOR_TYPE_STRING 0x03
33 #define DESCRIPTOR_TYPE_INTERFACE 0x04
34 #define DESCRIPTOR_TYPE_ENDPOINT 0x05
35 #define DESCRIPTOR_TYPE_INTERFACE_ASSOCIATION 0x0B
36
37 #define STR_DESCR(len, ...) { (len) * 2 + 2, DESCRIPTOR_TYPE_STRING, { __VA_ARGS__ } }
38
39 /** USB Device Descriptor. See USB 2.0 Spec */
40 struct usb_device_descriptor {
41 uint8_t blength; /**< Size of this descriptor in bytes. */
42 uint8_t bdescriptortype; /**< DEVICE Descriptor Type. */
43 uint16_t bcdusb; /**< USB specification release number (BCD). */
44 uint8_t bdeviceclass; /**< Class code. */
45 uint8_t bdevicesubclass; /**< Subclass code. */
46 uint8_t bdeviceprotocol; /**< Protocol code. */
47 uint8_t bmaxpacketsize0; /**< Maximum packet size for EP0 (8, 16, 32, 64). */
48 uint16_t idvendor; /**< USB Vendor ID. */
49 uint16_t idproduct; /**< USB Product ID. */
50 uint16_t bcddevice; /**< Device Release Number (BCD). */
51 uint8_t imanufacturer; /**< Index of manufacturer string descriptor. */
52 uint8_t iproduct; /**< Index of product string descriptor. */
53 uint8_t iserialnumber; /**< Index of string descriptor containing serial #. */
54 uint8_t bnumconfigurations; /**< Number of possible configurations. */
55 };
56
57 /** USB Configuration Descriptor. See USB 2.0 Spec */
58 struct usb_config_descriptor {
59 uint8_t blength; /**< Size of this descriptor in bytes. */
60 uint8_t bdescriptortype; /**< CONFIGURATION descriptor type. */
61 uint16_t wtotallength; /**< Combined total length of all descriptors. */
62 uint8_t bnuminterfaces; /**< Number of interfaces in this configuration. */
63 uint8_t bconfigurationvalue; /**< Value used to select this configuration. */
64 uint8_t iconfiguration; /**< Index of configuration string descriptor. */
65 uint8_t bmattributes; /**< Configuration characteristics. */
66 uint8_t maxpower; /**< Maximum power consumption in 2 mA units. */
67 };
68
69 /** USB Interface association Descriptor. See USB 2.0 Spec */
70 struct usb_interface_association_descriptor {
71 uint8_t blength;
72 uint8_t bdescriptortype;
73 uint8_t bfirstinterface;
74 uint8_t binterfacecount;
75 uint8_t bfunctionclass;
76 uint8_t bfunctionsubclass;
77 uint8_t bfunctionprotocol;
78 uint8_t ifunction;
79 };
80
81 /** USB Interface Descriptor. See USB 2.0 Spec */
82 struct usb_interface_descriptor {
83 uint8_t blength; /**< Size of this descriptor in bytes. */
84 uint8_t bdescriptortype; /**< INTERFACE descriptor type. */
85 uint8_t binterfacenumber; /**< Interface number. */
86 uint8_t balternatesetting; /**< Value used to select alternate setting. */
87 uint8_t bnumendpoints; /**< Number of endpoints used by this interface. */
88 uint8_t binterfaceclass; /**< Class code. */
89 uint8_t binterfacesubclass; /**< Subclass code. */
90 uint8_t binterfaceprotocol; /**< Protocol code. */
91 uint8_t iinterface; /**< Index of interface string descriptor. */
92 };
93
94 /** USB Endpoint Descriptor. See USB 2.0 Spec */
95 struct usb_endpoint_descriptor {
96 uint8_t blength; /**< Size of this descriptor in bytes. */
97 uint8_t bdescriptortype; /**< ENDPOINT descriptor type. */
98 uint8_t bendpointaddress; /**< Endpoint Address: IN/OUT + EP number. */
99 uint8_t bmattributes; /**< Endpoint Attributes: BULK/INTR/ISO/CTRL. */
100 uint16_t wmaxpacketsize; /**< Maximum packet size for this endpoint. */
101 uint8_t binterval; /**< Polling interval (in ms) for this endpoint. */
102 };
103
104 /** USB Language Descriptor. See USB 2.0 Spec */
105 struct usb_language_descriptor {
106 uint8_t blength; /**< Size of this descriptor in bytes. */
107 uint8_t bdescriptortype; /**< STRING descriptor type. */
108 uint16_t wlangid[]; /**< LANGID codes. */
109 };
110
111 /** USB String Descriptor. See USB 2.0 Spec */
112 struct usb_string_descriptor {
113 uint8_t blength; /**< Size of this descriptor in bytes. */
114 uint8_t bdescriptortype; /**< STRING descriptor type. */
115 uint16_t bstring[]; /**< UNICODE encoded string. */
116 };
117
118 /********************** USB Control Endpoint 0 related *********************/
119
120 /** USB Control Setup Data. See USB 2.0 Spec */
121 struct setup_data {
122 uint8_t bmrequesttype; /**< Characteristics of a request. */
123 uint8_t brequest; /**< Specific request. */
124 uint16_t wvalue; /**< Field that varies according to request. */
125 uint16_t windex; /**< Field that varies according to request. */
126 uint16_t wlength; /**< Number of bytes to transfer in data stage. */
127 };
128
129 /* External declarations for variables that need to be accessed outside of
130 * the USB module */
131 extern volatile bool ep1_out;
132 extern volatile bool ep1_in;
133
134 extern volatile __xdata __at 0xE6B8 struct setup_data setup_data;
135
136 /*
137 * USB Request Types (bmRequestType): See USB 2.0 Spec
138 *
139 * Bit 7: Data transfer direction
140 * 0 = Host-to-device
141 * 1 = Device-to-host
142 * Bit 6...5: Type
143 * 0 = Standard
144 * 1 = Class
145 * 2 = Vendor
146 * 3 = Reserved
147 * Bit 4...0: Recipient
148 * 0 = Device
149 * 1 = Interface
150 * 2 = Endpoint
151 * 3 = Other
152 * 4...31 = Reserved
153 */
154
155 #define USB_DIR_OUT 0x00
156 #define USB_DIR_IN 0x80
157
158 #define USB_REQ_TYPE_STANDARD (0x00 << 5)
159 #define USB_REQ_TYPE_CLASS (0x01 << 5)
160 #define USB_REQ_TYPE_VENDOR (0x02 << 5)
161 #define USB_REQ_TYPE_RESERVED (0x03 << 5)
162
163 #define USB_RECIP_DEVICE 0x00
164 #define USB_RECIP_INTERFACE 0x01
165 #define USB_RECIP_ENDPOINT 0x02
166 #define USB_RECIP_OTHER 0x03
167
168 /* Clear Interface Request */
169 #define CF_DEVICE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
170 #define CF_INTERFACE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
171 #define CF_ENDPOINT (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)
172
173 /* Get Configuration Request */
174 #define GC_DEVICE (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
175
176 /* Get Descriptor Request */
177 #define GD_DEVICE (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
178
179 /* Get Interface Request */
180 #define GI_INTERFACE (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
181
182 /* Get Status Request: See USB 1.1 spec, page 190 */
183 #define GS_DEVICE (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
184 #define GS_INTERFACE (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
185 #define GS_ENDPOINT (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)
186
187 /* Set Address Request is handled by EZ-USB core */
188
189 /* Set Configuration Request */
190 #define SC_DEVICE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
191
192 /* Set Descriptor Request */
193 #define SD_DEVICE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
194
195 /* Set Feature Request */
196 #define SF_DEVICE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
197 #define SF_INTERFACE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
198 #define SF_ENDPOINT (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)
199
200 /* Set Interface Request */
201 #define SI_INTERFACE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
202
203 /* Synch Frame Request */
204 #define SY_ENDPOINT (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)
205
206 /* USB Requests (bRequest): See USB 2.0 Spec */
207 #define GET_STATUS 0
208 #define CLEAR_FEATURE 1
209 /* Value '2' is reserved for future use */
210 #define SET_FEATURE 3
211 /* Value '4' is reserved for future use */
212 #define SET_ADDRESS 5
213 #define GET_DESCRIPTOR 6
214 #define SET_DESCRIPTOR 7
215 #define GET_CONFIGURATION 8
216 #define SET_CONFIGURATION 9
217 #define GET_INTERFACE 10
218 #define SET_INTERFACE 11
219 #define SYNCH_FRAME 12
220
221 /* Standard Feature Selectors: See USB 2.0 Spec */
222 #define DEVICE_REMOTE_WAKEUP 1
223 #define ENDPOINT_HALT 0
224
225 /************************** EZ-USB specific stuff **************************/
226 /** USB Interrupts. See EZ-USB FX2-TRM, for details */
227 enum usb_isr {
228 SUDAV_ISR = 13,
229 SOF_ISR,
230 SUTOK_ISR,
231 SUSPEND_ISR,
232 USBRESET_ISR,
233 HIGHSPEED_ISR,
234 EP0ACK_ISR,
235 STUB_ISR,
236 EP0IN_ISR,
237 EP0OUT_ISR,
238 EP1IN_ISR,
239 EP1OUT_ISR,
240 EP2_ISR,
241 EP4_ISR,
242 EP6_ISR,
243 EP8_ISR,
244 IBN_ISR,
245 EP0PINGNAK_ISR,
246 EP1PINGNAK_ISR,
247 EP2PINGNAK_ISR,
248 EP4PINGNAK_ISR,
249 EP6PINGNAK_ISR,
250 EP8PINGNAK_ISR,
251 ERRORLIMIT_ISR,
252 EP2PIDERROR_ISR,
253 EP4PIDERROR_ISR,
254 EP6PIDERROR_ISR,
255 EP8PIDERROR_ISR,
256 EP2PFLAG_ISR,
257 EP4PFLAG_ISR,
258 EP6PFLAG_ISR,
259 EP8PFLAG_ISR,
260 EP2EFLAG_ISR,
261 EP4EFLAG_ISR,
262 EP6EFLAG_ISR,
263 EP8EFLAG_ISR,
264 EP2FFLAG_ISR,
265 EP4FFLAG_ISR,
266 EP6FFLAG_ISR,
267 EP8FFLAG_ISR,
268 GPIFCOMPLETE_ISR,
269 GPIFWAVEFORM_ISR
270 };
271
272 /*************************** Function Prototypes ***************************/
273 __xdata uint8_t *usb_get_endpoint_cs_reg(uint8_t ep);
274 void usb_reset_data_toggle(uint8_t ep);
275 bool usb_handle_get_status(void);
276 bool usb_handle_clear_feature(void);
277 bool usb_handle_set_feature(void);
278 bool usb_handle_get_descriptor(void);
279 void usb_handle_set_interface(void);
280 void usb_handle_setup_data(void);
281 void usb_handle_i2c_in(void);
282 void usb_handle_i2c_out(void);
283
284 void i2c_recieve(void);
285 void ep_init(void);
286 void interrupt_init(void);
287 void io_init(void);
288
289 #endif

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)