Open FFBoard
Open source force feedback firmware
ch32_usbhs_reg.h
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2024 Matthew Tran
5 * Copyright (c) 2024 hathach
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 *
25 * This file is part of the TinyUSB stack.
26 */
27
28#ifndef USB_CH32_USBHS_REG_H
29#define USB_CH32_USBHS_REG_H
30
31// https://github.com/openwch/ch32v307/pull/90
32// https://github.com/openwch/ch32v20x/pull/12
33#ifdef __GNUC__
34#pragma GCC diagnostic push
35#pragma GCC diagnostic ignored "-Wstrict-prototypes"
36#endif
37
38#if CFG_TUSB_MCU == OPT_MCU_CH32V307
39 #include <ch32v30x.h>
40#elif CFG_TUSB_MCU == OPT_MCU_CH32F20X
41 #include <ch32f20x.h>
42#endif
43
44#ifdef __GNUC__
45#pragma GCC diagnostic pop
46#endif
47
48
49/******************* GLOBAL ******************/
50
51// USB CONTROL
52#define USBHS_CONTROL_OFFSET 0x00
53#define USBHS_DMA_EN (1 << 0)
54#define USBHS_ALL_CLR (1 << 1)
55#define USBHS_FORCE_RST (1 << 2)
56#define USBHS_INT_BUSY_EN (1 << 3)
57#define USBHS_DEV_PU_EN (1 << 4)
58#define USBHS_SPEED_MASK (3 << 5)
59#define USBHS_FULL_SPEED (0 << 5)
60#define USBHS_HIGH_SPEED (1 << 5)
61#define USBHS_LOW_SPEED (2 << 5)
62#define USBHS_HOST_MODE (1 << 7)
63
64// USB_INT_EN
65#define USBHS_INT_EN_OFFSET 0x02
66#define USBHS_BUS_RST_EN (1 << 0)
67#define USBHS_DETECT_EN (1 << 0)
68#define USBHS_TRANSFER_EN (1 << 1)
69#define USBHS_SUSPEND_EN (1 << 2)
70#define USBHS_SOF_ACT_EN (1 << 3)
71#define USBHS_FIFO_OV_EN (1 << 4)
72#define USBHS_SETUP_ACT_EN (1 << 5)
73#define USBHS_ISO_ACT_EN (1 << 6)
74#define USBHS_DEV_NAK_EN (1 << 7)
75
76// USB DEV AD
77#define USBHS_DEV_AD_OFFSET 0x03
78
79// USB FRAME_NO
80#define USBHS_FRAME_NO_OFFSET 0x04
81#define USBHS_FRAME_NO_NUM_MASK (0x7FF)
82#define USBHS_FRAME_NO_MICROFRAME_SHIFT (11)
83#define USBHS_FRAME_NO_MICROFRAME_MASK (0x7 << USBHS_FRAME_NO_MICROFRAME_SHIFT)
84
85// USB SUSPEND
86#define USBHS_SUSPEND_OFFSET 0x06
87#define USBHS_DEV_REMOTE_WAKEUP (1 << 2)
88#define USBHS_LINESTATE_MASK (2 << 4) /* Read Only */
89
90// RESERVED0
91
92// USB SPEED TYPE
93#define USBHS_SPEED_TYPE_OFFSET 0x08
94#define USBHS_SPEED_TYPE_MASK 0x03
95#define USBHS_SPEED_TYPE_FULL 0
96#define USBHS_SPEED_TYPE_HIGH 1
97#define USBHS_SPEED_TYPE_LOW 2
98
99// USB_MIS_ST
100#define USBHS_MIS_ST_OFFSET 0x09
101#define USBHS_SPLIT_CAN (1 << 0)
102#define USBHS_ATTACH (1 << 1)
103#define USBHS_SUSPEND (1 << 2)
104#define USBHS_BUS_RESET (1 << 3)
105#define USBHS_R_FIFO_RDY (1 << 4)
106#define USBHS_SIE_FREE (1 << 5)
107#define USBHS_SOF_ACT (1 << 6)
108#define USBHS_SOF_PRES (1 << 7)
109
110// INT_FLAG
111#define USBHS_INT_FLAG_OFFSET 0x0A
112#define USBHS_BUS_RST_FLAG (1 << 0)
113#define USBHS_DETECT_FLAG (1 << 0)
114#define USBHS_TRANSFER_FLAG (1 << 1)
115#define USBHS_SUSPEND_FLAG (1 << 2)
116#define USBHS_HST_SOF_FLAG (1 << 3)
117#define USBHS_FIFO_OV_FLAG (1 << 4)
118#define USBHS_SETUP_FLAG (1 << 5)
119#define USBHS_ISO_ACT_FLAG (1 << 6)
120
121// INT_ST
122#define USBHS_INT_ST_OFFSET 0x0B
123#define USBHS_DEV_UIS_IS_NAK (1 << 7)
124#define USBHS_DEV_UIS_TOG_OK (1 << 6)
125#define MASK_UIS_TOKEN (3 << 4)
126#define USBHS_TOKEN_PID_OUT (0 << 4)
127#define USBHS_TOKEN_PID_SOF (1 << 4)
128#define USBHS_TOKEN_PID_IN (2 << 4)
129#define USBHS_TOKEN_PID_SETUP (3 << 4)
130#define MASK_UIS_ENDP (0x0F)
131#define MASK_UIS_H_RES (0x0F)
132
133#define USBHS_TOGGLE_OK (0x40)
134#define USBHS_HOST_RES (0x0f)
135
136//USB_RX_LEN
137#define USBHS_RX_LEN_OFFSET 0x0C
138/******************* DEVICE ******************/
139
140//UEP_CONFIG
141#define USBHS_UEP_CONFIG_OFFSET 0x10
142#define USBHS_EP0_T_EN (1 << 0)
143#define USBHS_EP0_R_EN (1 << 16)
144
145#define USBHS_EP1_T_EN (1 << 1)
146#define USBHS_EP1_R_EN (1 << 17)
147
148#define USBHS_EP2_T_EN (1 << 2)
149#define USBHS_EP2_R_EN (1 << 18)
150
151#define USBHS_EP3_T_EN (1 << 3)
152#define USBHS_EP3_R_EN (1 << 19)
153
154#define USBHS_EP4_T_EN (1 << 4)
155#define USBHS_EP4_R_EN (1 << 20)
156
157#define USBHS_EP5_T_EN (1 << 5)
158#define USBHS_EP5_R_EN (1 << 21)
159
160#define USBHS_EP6_T_EN (1 << 6)
161#define USBHS_EP6_R_EN (1 << 22)
162
163#define USBHS_EP7_T_EN (1 << 7)
164#define USBHS_EP7_R_EN (1 << 23)
165
166#define USBHS_EP8_T_EN (1 << 8)
167#define USBHS_EP8_R_EN (1 << 24)
168
169#define USBHS_EP9_T_EN (1 << 9)
170#define USBHS_EP9_R_EN (1 << 25)
171
172#define USBHS_EP10_T_EN (1 << 10)
173#define USBHS_EP10_R_EN (1 << 26)
174
175#define USBHS_EP11_T_EN (1 << 11)
176#define USBHS_EP11_R_EN (1 << 27)
177
178#define USBHS_EP12_T_EN (1 << 12)
179#define USBHS_EP12_R_EN (1 << 28)
180
181#define USBHS_EP13_T_EN (1 << 13)
182#define USBHS_EP13_R_EN (1 << 29)
183
184#define USBHS_EP14_T_EN (1 << 14)
185#define USBHS_EP14_R_EN (1 << 30)
186
187#define USBHS_EP15_T_EN (1 << 15)
188#define USBHS_EP15_R_EN (1 << 31)
189
190//UEP_TYPE
191#define USBHS_UEP_TYPE_OFFSET 0x14
192#define USBHS_EP0_T_TYP (1 << 0)
193#define USBHS_EP0_R_TYP (1 << 16)
194
195#define USBHS_EP1_T_TYP (1 << 1)
196#define USBHS_EP1_R_TYP (1 << 17)
197
198#define USBHS_EP2_T_TYP (1 << 2)
199#define USBHS_EP2_R_TYP (1 << 18)
200
201#define USBHS_EP3_T_TYP (1 << 3)
202#define USBHS_EP3_R_TYP (1 << 19)
203
204#define USBHS_EP4_T_TYP (1 << 4)
205#define USBHS_EP4_R_TYP (1 << 20)
206
207#define USBHS_EP5_T_TYP (1 << 5)
208#define USBHS_EP5_R_TYP (1 << 21)
209
210#define USBHS_EP6_T_TYP (1 << 6)
211#define USBHS_EP6_R_TYP (1 << 22)
212
213#define USBHS_EP7_T_TYP (1 << 7)
214#define USBHS_EP7_R_TYP (1 << 23)
215
216#define USBHS_EP8_T_TYP (1 << 8)
217#define USBHS_EP8_R_TYP (1 << 24)
218
219#define USBHS_EP9_T_TYP (1 << 8)
220#define USBHS_EP9_R_TYP (1 << 25)
221
222#define USBHS_EP10_T_TYP (1 << 10)
223#define USBHS_EP10_R_TYP (1 << 26)
224
225#define USBHS_EP11_T_TYP (1 << 11)
226#define USBHS_EP11_R_TYP (1 << 27)
227
228#define USBHS_EP12_T_TYP (1 << 12)
229#define USBHS_EP12_R_TYP (1 << 28)
230
231#define USBHS_EP13_T_TYP (1 << 13)
232#define USBHS_EP13_R_TYP (1 << 29)
233
234#define USBHS_EP14_T_TYP (1 << 14)
235#define USBHS_EP14_R_TYP (1 << 30)
236
237#define USBHS_EP15_T_TYP (1 << 15)
238#define USBHS_EP15_R_TYP (1 << 31)
239
240/* BUF_MOD UEP1~15 */
241#define USBHS_BUF_MOD_OFFSET 0x18
242#define USBHS_EP0_BUF_MOD (1 << 0)
243#define USBHS_EP0_ISO_BUF_MOD (1 << 16)
244
245#define USBHS_EP1_BUF_MOD (1 << 1)
246#define USBHS_EP1_ISO_BUF_MOD (1 << 17)
247
248#define USBHS_EP2_BUF_MOD (1 << 2)
249#define USBHS_EP2_ISO_BUF_MOD (1 << 18)
250
251#define USBHS_EP3_BUF_MOD (1 << 3)
252#define USBHS_EP3_ISO_BUF_MOD (1 << 19)
253
254#define USBHS_EP4_BUF_MOD (1 << 4)
255#define USBHS_EP4_ISO_BUF_MOD (1 << 20)
256
257#define USBHS_EP5_BUF_MOD (1 << 5)
258#define USBHS_EP5_ISO_BUF_MOD (1 << 21)
259
260#define USBHS_EP6_BUF_MOD (1 << 6)
261#define USBHS_EP6_ISO_BUF_MOD (1 << 22)
262
263#define USBHS_EP7_BUF_MOD (1 << 7)
264#define USBHS_EP7_ISO_BUF_MOD (1 << 23)
265
266#define USBHS_EP8_BUF_MOD (1 << 8)
267#define USBHS_EP8_ISO_BUF_MOD (1 << 24)
268
269#define USBHS_EP9_BUF_MOD (1 << 9)
270#define USBHS_EP9_ISO_BUF_MOD (1 << 25)
271
272#define USBHS_EP10_BUF_MOD (1 << 10)
273#define USBHS_EP10_ISO_BUF_MOD (1 << 26)
274
275#define USBHS_EP11_BUF_MOD (1 << 11)
276#define USBHS_EP11_ISO_BUF_MOD (1 << 27)
277
278#define USBHS_EP12_BUF_MOD (1 << 12)
279#define USBHS_EP12_ISO_BUF_MOD (1 << 28)
280
281#define USBHS_EP13_BUF_MOD (1 << 13)
282#define USBHS_EP13_ISO_BUF_MOD (1 << 29)
283
284#define USBHS_EP14_BUF_MOD (1 << 14)
285#define USBHS_EP14_ISO_BUF_MOD (1 << 30)
286
287#define USBHS_EP15_BUF_MOD (1 << 15)
288#define USBHS_EP15_ISO_BUF_MOD (1 << 31)
289//USBHS_EPn_T_EN USBHS_EPn_R_EN USBHS_EPn_BUF_MOD Description: Arrange from low to high with UEPn_DMA as the starting address
290// 0 0 x The endpoint is disabled and the UEPn_*_DMA buffers are not used.
291// 1 0 0 The first address of the receive (OUT) buffer is UEPn_RX_DMA
292// 1 0 1 RB_UEPn_RX_TOG[0]=0, use buffer UEPn_RX_DMA RB_UEPn_RX_TOG[0]=1, use buffer UEPn_TX_DMA
293// 0 1 0 The first address of the transmit (IN) buffer is UEPn_TX_DMA.
294// 0 1 1 RB_UEPn_TX_TOG[0]=0, use buffer UEPn_TX_DMA RB_UEPn_TX_TOG[0]=1, use buffer UEPn_RX_DMA
295
296/* USB0_DMA */
297#define USBHS_UEP0_DMA_OFFSET(n) (0x1C) // endpoint 0 DMA buffer address
298
299/* USBX_RX_DMA */
300#define USBHS_UEPx_RX_DMA_OFFSET(n) (0x1C + 4 * (n)) // endpoint x DMA buffer address
301
302#define USBHS_UEPx_TX_DMA_OFFSET(n) (0x58 + 4 * (n)) // endpoint x DMA buffer address
303
304#define USBHS_UEPx_MAX_LEN_OFFSET(n) (0x98 + 4 * (n)) // endpoint x DMA buffer address
305
306#define USBHS_UEPx_T_LEN_OFFSET(n) (0xD8 + 4 * (n)) // endpoint x DMA buffer address
307#define USBHS_UEPx_TX_CTRL_OFFSET(n) (0xD8 + 4 * (n) + 2) // endpoint x DMA buffer address
308#define USBHS_UEPx_RX_CTRL_OFFSET(n) (0xD8 + 4 * (n) + 3) // endpoint x DMA buffer address
309
310// UEPn_T_LEN
311#define USBHS_EP_T_LEN_MASK (0x7FF)
312
313//UEPn_TX_CTRL
314#define USBHS_EP_T_RES_MASK (3 << 0)
315#define USBHS_EP_T_RES_ACK (0 << 0)
316#define USBHS_EP_T_RES_NYET (1 << 0)
317#define USBHS_EP_T_RES_NAK (2 << 0)
318#define USBHS_EP_T_RES_STALL (3 << 0)
319
320#define USBHS_EP_T_TOG_MASK (3 << 3)
321#define USBHS_EP_T_TOG_0 (0 << 3)
322#define USBHS_EP_T_TOG_1 (1 << 3)
323#define USBHS_EP_T_TOG_2 (2 << 3)
324#define USBHS_EP_T_TOG_M (3 << 3)
325
326#define USBHS_EP_T_AUTOTOG (1 << 5)
327
328//UEPn_RX_CTRL
329#define USBHS_EP_R_RES_MASK (3 << 0)
330#define USBHS_EP_R_RES_ACK (0 << 0)
331#define USBHS_EP_R_RES_NYET (1 << 0)
332#define USBHS_EP_R_RES_NAK (2 << 0)
333#define USBHS_EP_R_RES_STALL (3 << 0)
334
335#define USBHS_EP_R_TOG_MASK (3 << 3)
336#define USBHS_EP_R_TOG_0 (0 << 3)
337#define USBHS_EP_R_TOG_1 (1 << 3)
338#define USBHS_EP_R_TOG_2 (2 << 3)
339#define USBHS_EP_R_TOG_M (3 << 3)
340
341#define USBHS_EP_R_AUTOTOG (1 << 5)
342
343#define USBHS_TOG_MATCH (1 << 6)
344
345/******************* HOST ******************/
346// USB HOST_CTRL
347#define USBHS_SEND_BUS_RESET (1 << 0)
348#define USBHS_SEND_BUS_SUSPEND (1 << 1)
349#define USBHS_SEND_BUS_RESUME (1 << 2)
350#define USBHS_REMOTE_WAKE (1 << 3)
351#define USBHS_PHY_SUSPENDM (1 << 4)
352#define USBHS_UH_SOFT_FREE (1 << 6)
353#define USBHS_SEND_SOF_EN (1 << 7)
354
355//UH_CONFIG
356#define USBHS_HOST_TX_EN (1 << 3)
357#define USBHS_HOST_RX_EN (1 << 18)
358
359// HOST_EP_TYPE
360#define USBHS_ENDP_TX_ISO (1 << 3)
361#define USBHS_ENDP_RX_ISO (1 << (16 + 2))
362
363// R32_UH_EP_PID
364#define USBHS_HOST_MASK_TOKEN (0x0f)
365#define USBHS_HOST_MASK_ENDP (0x0f << 4)
366
367//R8_UH_RX_CTRL
368#define USBHS_EP_R_RES_MASK (3 << 0)
369#define USBHS_EP_R_RES_ACK (0 << 0)
370#define USBHS_EP_R_RES_NYET (1 << 0)
371#define USBHS_EP_R_RES_NAK (2 << 0)
372#define USBHS_EP_R_RES_STALL (3 << 0)
373
374#define USBHS_UH_R_RES_NO (1 << 2)
375#define USBHS_UH_R_TOG_1 (1 << 3)
376#define USBHS_UH_R_TOG_2 (2 << 3)
377#define USBHS_UH_R_TOG_3 (3 << 3)
378#define USBHS_UH_R_TOG_AUTO (1 << 5)
379#define USBHS_UH_R_DATA_NO (1 << 6)
380//R8_UH_TX_CTRL
381#define USBHS_UH_T_RES_MASK (3 << 0)
382#define USBHS_UH_T_RES_ACK (0 << 0)
383#define USBHS_UH_T_RES_NYET (1 << 0)
384#define USBHS_UH_T_RES_NAK (2 << 0)
385#define USBHS_UH_T_RES_STALL (3 << 0)
386
387#define USBHS_UH_T_RES_NO (1 << 2)
388#define USBHS_UH_T_TOG_1 (1 << 3)
389#define USBHS_UH_T_TOG_2 (2 << 3)
390#define USBHS_UH_T_TOG_3 (3 << 3)
391#define USBHS_UH_T_TOG_AUTO (1 << 5)
392#define USBHS_UH_T_DATA_NO (1 << 6)
393
394
395#endif