Open FFBoard
Open source force feedback firmware
fsdev_stm32.h
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright(c) N Conrad
5 * Copyright (c) 2019 Ha Thach (tinyusb.org)
6 *
7 * Redistribution and use in source and binary forms, with or without modification,
8 * are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * This file is part of the TinyUSB stack.
30 */
31
32#ifndef TUSB_FSDEV_STM32_H
33#define TUSB_FSDEV_STM32_H
34
35#if CFG_TUSB_MCU == OPT_MCU_STM32F0
36 #include "stm32f0xx.h"
37 #define FSDEV_PMA_SIZE (1024u)
38 #define FSDEV_REG_BASE USB_BASE
39 // F0x2 models are crystal-less
40 // All have internal D+ pull-up
41 // 070RB: 2 x 16 bits/word memory LPM Support, BCD Support
42 // PMA dedicated to USB (no sharing with CAN)
43
44#elif CFG_TUSB_MCU == OPT_MCU_STM32F1
45 #include "stm32f1xx.h"
46 #define FSDEV_PMA_SIZE (512u)
47 // NO internal Pull-ups
48 // *B, and *C: 2 x 16 bits/word
49
50 // F1 names this differently from the rest
51 #define USB_CNTR_LPMODE USB_CNTR_LP_MODE
52
53#elif defined(STM32F302xB) || defined(STM32F302xC) || \
54 defined(STM32F303xB) || defined(STM32F303xC) || \
55 defined(STM32F373xC)
56 #include "stm32f3xx.h"
57 #define FSDEV_PMA_SIZE (512u)
58 // NO internal Pull-ups
59 // *B, and *C: 1 x 16 bits/word
60 // PMA dedicated to USB (no sharing with CAN)
61
62#elif defined(STM32F302x6) || defined(STM32F302x8) || \
63 defined(STM32F302xD) || defined(STM32F302xE) || \
64 defined(STM32F303xD) || defined(STM32F303xE)
65 #include "stm32f3xx.h"
66 #define FSDEV_PMA_SIZE (1024u)
67 // NO internal Pull-ups
68 // *6, *8, *D, and *E: 2 x 16 bits/word LPM Support
69 // When CAN clock is enabled, USB can use first 768 bytes ONLY.
70
71#elif CFG_TUSB_MCU == OPT_MCU_STM32L0
72 #include "stm32l0xx.h"
73 #define FSDEV_PMA_SIZE (1024u)
74
75#elif CFG_TUSB_MCU == OPT_MCU_STM32L1
76 #include "stm32l1xx.h"
77 #define FSDEV_PMA_SIZE (512u)
78
79#elif CFG_TUSB_MCU == OPT_MCU_STM32G4
80 #include "stm32g4xx.h"
81 #define FSDEV_PMA_SIZE (1024u)
82
83#elif CFG_TUSB_MCU == OPT_MCU_STM32G0
84 #include "stm32g0xx.h"
85 #define FSDEV_PMA_SIZE (2048u)
86 #define USB USB_DRD_FS
87
88 #define USB_EP_CTR_RX USB_EP_VTRX
89 #define USB_EP_CTR_TX USB_EP_VTTX
90 #define USB_EP_T_FIELD USB_CHEP_UTYPE
91 #define USB_EPREG_MASK USB_CHEP_REG_MASK
92 #define USB_EPTX_DTOGMASK USB_CHEP_TX_DTOGMASK
93 #define USB_EPRX_DTOGMASK USB_CHEP_RX_DTOGMASK
94 #define USB_EPTX_DTOG1 USB_CHEP_TX_DTOG1
95 #define USB_EPTX_DTOG2 USB_CHEP_TX_DTOG2
96 #define USB_EPRX_DTOG1 USB_CHEP_RX_DTOG1
97 #define USB_EPRX_DTOG2 USB_CHEP_RX_DTOG2
98 #define USB_EPRX_STAT USB_CH_RX_VALID
99 #define USB_EPKIND_MASK USB_EP_KIND_MASK
100 #define USB_CNTR_FRES USB_CNTR_USBRST
101 #define USB_CNTR_RESUME USB_CNTR_L2RES
102 #define USB_ISTR_EP_ID USB_ISTR_IDN
103 #define USB_EPADDR_FIELD USB_CHEP_ADDR
104 #define USB_CNTR_LPMODE USB_CNTR_SUSPRDY
105 #define USB_CNTR_FSUSP USB_CNTR_SUSPEN
106
107#elif CFG_TUSB_MCU == OPT_MCU_STM32H5
108 #include "stm32h5xx.h"
109 #define FSDEV_PMA_SIZE (2048u)
110 #define USB USB_DRD_FS
111
112 #define USB_EP_CTR_RX USB_EP_VTRX
113 #define USB_EP_CTR_TX USB_EP_VTTX
114 #define USB_EP_T_FIELD USB_CHEP_UTYPE
115 #define USB_EPREG_MASK USB_CHEP_REG_MASK
116 #define USB_EPTX_DTOGMASK USB_CHEP_TX_DTOGMASK
117 #define USB_EPRX_DTOGMASK USB_CHEP_RX_DTOGMASK
118 #define USB_EPTX_DTOG1 USB_CHEP_TX_DTOG1
119 #define USB_EPTX_DTOG2 USB_CHEP_TX_DTOG2
120 #define USB_EPRX_DTOG1 USB_CHEP_RX_DTOG1
121 #define USB_EPRX_DTOG2 USB_CHEP_RX_DTOG2
122 #define USB_EPRX_STAT USB_CH_RX_VALID
123 #define USB_EPKIND_MASK USB_EP_KIND_MASK
124 #define USB_CNTR_FRES USB_CNTR_USBRST
125 #define USB_CNTR_RESUME USB_CNTR_L2RES
126 #define USB_ISTR_EP_ID USB_ISTR_IDN
127 #define USB_EPADDR_FIELD USB_CHEP_ADDR
128 #define USB_CNTR_LPMODE USB_CNTR_SUSPRDY
129 #define USB_CNTR_FSUSP USB_CNTR_SUSPEN
130
131#elif CFG_TUSB_MCU == OPT_MCU_STM32WB
132 #include "stm32wbxx.h"
133 #define FSDEV_PMA_SIZE (1024u)
134 /* ST provided header has incorrect value of USB_PMAADDR */
135 #define FSDEV_PMA_BASE USB1_PMAADDR
136
137#elif CFG_TUSB_MCU == OPT_MCU_STM32L4
138 #include "stm32l4xx.h"
139 #define FSDEV_PMA_SIZE (1024u)
140
141#elif CFG_TUSB_MCU == OPT_MCU_STM32L5
142 #include "stm32l5xx.h"
143 #define FSDEV_PMA_SIZE (1024u)
144
145 #ifndef USB_PMAADDR
146 #define USB_PMAADDR (USB_BASE + (USB_PMAADDR_NS - USB_BASE_NS))
147 #endif
148
149#elif CFG_TUSB_MCU == OPT_MCU_STM32U5
150 #include "stm32u5xx.h"
151 #define FSDEV_PMA_SIZE (2048u)
152 #define USB USB_DRD_FS
153
154 #define USB_EP_CTR_RX USB_EP_VTRX
155 #define USB_EP_CTR_TX USB_EP_VTTX
156 #define USB_EP_T_FIELD USB_CHEP_UTYPE
157 #define USB_EPREG_MASK USB_CHEP_REG_MASK
158 #define USB_EPTX_DTOGMASK USB_CHEP_TX_DTOGMASK
159 #define USB_EPRX_DTOGMASK USB_CHEP_RX_DTOGMASK
160 #define USB_EPTX_DTOG1 USB_CHEP_TX_DTOG1
161 #define USB_EPTX_DTOG2 USB_CHEP_TX_DTOG2
162 #define USB_EPRX_DTOG1 USB_CHEP_RX_DTOG1
163 #define USB_EPRX_DTOG2 USB_CHEP_RX_DTOG2
164 #define USB_EPRX_STAT USB_CH_RX_VALID
165 #define USB_EPKIND_MASK USB_EP_KIND_MASK
166 #define USB_CNTR_FRES USB_CNTR_USBRST
167 #define USB_CNTR_RESUME USB_CNTR_L2RES
168 #define USB_ISTR_EP_ID USB_ISTR_IDN
169 #define USB_EPADDR_FIELD USB_CHEP_ADDR
170 #define USB_CNTR_LPMODE USB_CNTR_SUSPRDY
171 #define USB_CNTR_FSUSP USB_CNTR_SUSPEN
172
173#elif CFG_TUSB_MCU == OPT_MCU_STM32U0
174 #include "stm32u0xx.h"
175 #define FSDEV_PMA_SIZE (2048u)
176 #define USB USB_DRD_FS
177
178 #define USB_EP_CTR_RX USB_EP_VTRX
179 #define USB_EP_CTR_TX USB_EP_VTTX
180 #define USB_EP_T_FIELD USB_CHEP_UTYPE
181 #define USB_EPREG_MASK USB_CHEP_REG_MASK
182 #define USB_EPTX_DTOGMASK USB_CHEP_TX_DTOGMASK
183 #define USB_EPRX_DTOGMASK USB_CHEP_RX_DTOGMASK
184 #define USB_EPTX_DTOG1 USB_CHEP_TX_DTOG1
185 #define USB_EPTX_DTOG2 USB_CHEP_TX_DTOG2
186 #define USB_EPRX_DTOG1 USB_CHEP_RX_DTOG1
187 #define USB_EPRX_DTOG2 USB_CHEP_RX_DTOG2
188 #define USB_EPRX_STAT USB_CH_RX_VALID
189 #define USB_EPKIND_MASK USB_EP_KIND_MASK
190 #define USB_CNTR_FRES USB_CNTR_USBRST
191 #define USB_CNTR_RESUME USB_CNTR_L2RES
192 #define USB_ISTR_EP_ID USB_ISTR_IDN
193 #define USB_EPADDR_FIELD USB_CHEP_ADDR
194 #define USB_CNTR_LPMODE USB_CNTR_SUSPRDY
195 #define USB_CNTR_FSUSP USB_CNTR_SUSPEN
196
197#else
198 #error You are using an untested or unimplemented STM32 variant. Please update the driver.
199 // This includes U0
200#endif
201
202//--------------------------------------------------------------------+
203// Register and PMA Base Address
204//--------------------------------------------------------------------+
205#ifndef FSDEV_REG_BASE
206#if defined(USB_BASE)
207 #define FSDEV_REG_BASE USB_BASE
208#elif defined(USB_DRD_BASE)
209 #define FSDEV_REG_BASE USB_DRD_BASE
210#elif defined(USB_DRD_FS_BASE)
211 #define FSDEV_REG_BASE USB_DRD_FS_BASE
212#else
213 #error "FSDEV_REG_BASE not defined"
214#endif
215#endif
216
217#ifndef FSDEV_PMA_BASE
218#if defined(USB_PMAADDR)
219 #define FSDEV_PMA_BASE USB_PMAADDR
220#elif defined(USB_DRD_PMAADDR)
221 #define FSDEV_PMA_BASE USB_DRD_PMAADDR
222#else
223 #error "FSDEV_PMA_BASE not defined"
224#endif
225#endif
226
227// This checks if the device has "LPM"
228#if defined(USB_ISTR_L1REQ)
229#define USB_ISTR_L1REQ_FORCED (USB_ISTR_L1REQ)
230#else
231#define USB_ISTR_L1REQ_FORCED ((uint16_t)0x0000U)
232#endif
233
234#define USB_ISTR_ALL_EVENTS (USB_ISTR_PMAOVR | USB_ISTR_ERR | USB_ISTR_WKUP | USB_ISTR_SUSP | \
235 USB_ISTR_RESET | USB_ISTR_SOF | USB_ISTR_ESOF | USB_ISTR_L1REQ_FORCED )
236
237//--------------------------------------------------------------------+
238//
239//--------------------------------------------------------------------+
240
241#if TU_CHECK_MCU(OPT_MCU_STM32L1) && !defined(USBWakeUp_IRQn)
242 #define USBWakeUp_IRQn USB_FS_WKUP_IRQn
243#endif
244
245static const IRQn_Type fsdev_irq[] = {
246 #if TU_CHECK_MCU(OPT_MCU_STM32F0, OPT_MCU_STM32L0, OPT_MCU_STM32L4)
247 USB_IRQn,
248 #elif CFG_TUSB_MCU == OPT_MCU_STM32F1
249 USB_HP_CAN1_TX_IRQn,
250 USB_LP_CAN1_RX0_IRQn,
251 USBWakeUp_IRQn,
252 #elif CFG_TUSB_MCU == OPT_MCU_STM32F3
253 // USB remap handles dcd functions
254 USB_HP_CAN_TX_IRQn,
255 USB_LP_CAN_RX0_IRQn,
256 USBWakeUp_IRQn,
257 #elif CFG_TUSB_MCU == OPT_MCU_STM32G0
258 #ifdef STM32G0B0xx
259 USB_IRQn,
260 #else
261 USB_UCPD1_2_IRQn,
262 #endif
263 #elif TU_CHECK_MCU(OPT_MCU_STM32G4, OPT_MCU_STM32L1)
264 USB_HP_IRQn,
265 USB_LP_IRQn,
266 USBWakeUp_IRQn,
267 #elif CFG_TUSB_MCU == OPT_MCU_STM32H5
268 USB_DRD_FS_IRQn,
269 #elif CFG_TUSB_MCU == OPT_MCU_STM32L5
270 USB_FS_IRQn,
271 #elif CFG_TUSB_MCU == OPT_MCU_STM32WB
272 USB_HP_IRQn,
273 USB_LP_IRQn,
274 #elif CFG_TUSB_MCU == OPT_MCU_STM32U5
275 USB_IRQn,
276 #elif CFG_TUSB_MCU == OPT_MCU_STM32U0
277 USB_DRD_FS_IRQn,
278 #else
279 #error Unknown arch in USB driver
280 #endif
281};
282enum { FSDEV_IRQ_NUM = TU_ARRAY_SIZE(fsdev_irq) };
283
284void dcd_int_enable(uint8_t rhport) {
285 (void)rhport;
286
287 // forces write to RAM before allowing ISR to execute
288 __DSB(); __ISB();
289
290 #if CFG_TUSB_MCU == OPT_MCU_STM32F3 && defined(SYSCFG_CFGR1_USB_IT_RMP)
291 // Some STM32F302/F303 devices allow to remap the USB interrupt vectors from
292 // shared USB/CAN IRQs to separate CAN and USB IRQs.
293 // This dynamically checks if this remap is active to enable the right IRQs.
294 if (SYSCFG->CFGR1 & SYSCFG_CFGR1_USB_IT_RMP) {
295 NVIC_EnableIRQ(USB_HP_IRQn);
296 NVIC_EnableIRQ(USB_LP_IRQn);
297 NVIC_EnableIRQ(USBWakeUp_RMP_IRQn);
298 } else
299 #endif
300 {
301 for (uint8_t i = 0; i < FSDEV_IRQ_NUM; i++) {
302 NVIC_EnableIRQ(fsdev_irq[i]);
303 }
304 }
305}
306
307void dcd_int_disable(uint8_t rhport) {
308 (void)rhport;
309
310 #if CFG_TUSB_MCU == OPT_MCU_STM32F3 && defined(SYSCFG_CFGR1_USB_IT_RMP)
311 // Some STM32F302/F303 devices allow to remap the USB interrupt vectors from
312 // shared USB/CAN IRQs to separate CAN and USB IRQs.
313 // This dynamically checks if this remap is active to enable the right IRQs.
314 if (SYSCFG->CFGR1 & SYSCFG_CFGR1_USB_IT_RMP) {
315 NVIC_DisableIRQ(USB_HP_IRQn);
316 NVIC_DisableIRQ(USB_LP_IRQn);
317 NVIC_DisableIRQ(USBWakeUp_RMP_IRQn);
318 } else
319 #endif
320 {
321 for (uint8_t i = 0; i < FSDEV_IRQ_NUM; i++) {
322 NVIC_DisableIRQ(fsdev_irq[i]);
323 }
324 }
325
326 // CMSIS has a membar after disabling interrupts
327}
328
329// Define only on MCU with internal pull-up. BSP can define on MCU without internal PU.
330#if defined(USB_BCDR_DPPU)
331
332void dcd_disconnect(uint8_t rhport) {
333 (void)rhport;
334 USB->BCDR &= ~(USB_BCDR_DPPU);
335}
336
337void dcd_connect(uint8_t rhport) {
338 (void)rhport;
339 USB->BCDR |= USB_BCDR_DPPU;
340}
341
342#elif defined(SYSCFG_PMC_USB_PU) // works e.g. on STM32L151
343
344void dcd_disconnect(uint8_t rhport) {
345 (void)rhport;
346 SYSCFG->PMC &= ~(SYSCFG_PMC_USB_PU);
347}
348
349void dcd_connect(uint8_t rhport) {
350 (void)rhport;
351 SYSCFG->PMC |= SYSCFG_PMC_USB_PU;
352}
353#endif
354
355
356#endif /* TUSB_FSDEV_STM32_H */
static const IRQn_Type fsdev_irq[]
Definition: fsdev_stm32.h:245
void dcd_disconnect(uint8_t rhport)
Definition: fsdev_stm32.h:332
void dcd_int_disable(uint8_t rhport)
Definition: fsdev_stm32.h:307
void dcd_connect(uint8_t rhport)
Definition: fsdev_stm32.h:337
@ FSDEV_IRQ_NUM
Definition: fsdev_stm32.h:282
void dcd_int_enable(uint8_t rhport)
Definition: fsdev_stm32.h:284