Skip to content

Commit c0e19ef

Browse files
committed
add tinyusb driver for ch32: usbfs, stm32fsdev, usbhs
1 parent c953968 commit c0e19ef

File tree

8 files changed

+3400
-0
lines changed

8 files changed

+3400
-0
lines changed

src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c

Lines changed: 1227 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2024, hathach (tinyusb.org)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
/** <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
26+
* All rights reserved.</center></h2>
27+
*
28+
* This software component is licensed by ST under BSD 3-Clause license,
29+
* the "License"; You may not use this file except in compliance with the
30+
* License. You may obtain a copy of the License at:
31+
* opensource.org/licenses/BSD-3-Clause
32+
*/
33+
34+
#ifndef TUSB_FSDEV_CH32_H
35+
#define TUSB_FSDEV_CH32_H
36+
37+
#include "common/tusb_compiler.h"
38+
39+
#if CFG_TUSB_MCU == OPT_MCU_CH32V20X
40+
#include <ch32v20x.h>
41+
42+
#elif CFG_TUSB_MCU == OPT_MCU_CH32F20X
43+
#include <ch32f20x.h>
44+
#endif
45+
46+
#define FSDEV_PMA_SIZE (512u)
47+
48+
// volatile 32-bit aligned
49+
#define _va32 volatile TU_ATTR_ALIGNED(4)
50+
51+
typedef struct {
52+
_va32 uint16_t EP0R; // 00: USB Endpoint 0 register
53+
_va32 uint16_t EP1R; // 04: USB Endpoint 1 register
54+
_va32 uint16_t EP2R; // 08: USB Endpoint 2 register
55+
_va32 uint16_t EP3R; // 0C: USB Endpoint 3 register
56+
_va32 uint16_t EP4R; // 10: USB Endpoint 4 register
57+
_va32 uint16_t EP5R; // 14: USB Endpoint 5 register
58+
_va32 uint16_t EP6R; // 18: USB Endpoint 6 register
59+
_va32 uint16_t EP7R; // 1C: USB Endpoint 7 register
60+
_va32 uint16_t RESERVED7[16]; // Reserved
61+
_va32 uint16_t CNTR; // 40: Control register
62+
_va32 uint16_t ISTR; // 44: Interrupt status register
63+
_va32 uint16_t FNR; // 48: Frame number register
64+
_va32 uint16_t DADDR; // 4C: Device address register
65+
_va32 uint16_t BTABLE; // 50: Buffer Table address register
66+
} USB_TypeDef;
67+
68+
TU_VERIFY_STATIC(sizeof(USB_TypeDef) == 0x54, "Size is not correct");
69+
TU_VERIFY_STATIC(offsetof(USB_TypeDef, CNTR) == 0x40, "Wrong offset");
70+
71+
#define USB_BASE (APB1PERIPH_BASE + 0x00005C00UL) /*!< USB_IP Peripheral Registers base address */
72+
#define USB_PMAADDR (APB1PERIPH_BASE + 0x00006000UL) /*!< USB_IP Packet Memory Area base address */
73+
#define USB ((USB_TypeDef *)USB_BASE)
74+
75+
/******************************************************************************/
76+
/* */
77+
/* USB Device General registers */
78+
/* */
79+
/******************************************************************************/
80+
#define USB_CNTR (USB_BASE + 0x40U) /*!< Control register */
81+
#define USB_ISTR (USB_BASE + 0x44U) /*!< Interrupt status register */
82+
#define USB_FNR (USB_BASE + 0x48U) /*!< Frame number register */
83+
#define USB_DADDR (USB_BASE + 0x4CU) /*!< Device address register */
84+
#define USB_BTABLE (USB_BASE + 0x50U) /*!< Buffer Table address register */
85+
86+
/**************************** ISTR interrupt events *************************/
87+
#define USB_ISTR_CTR ((uint16_t)0x8000U) /*!< Correct TRansfer (clear-only bit) */
88+
#define USB_ISTR_PMAOVR ((uint16_t)0x4000U) /*!< DMA OVeR/underrun (clear-only bit) */
89+
#define USB_ISTR_ERR ((uint16_t)0x2000U) /*!< ERRor (clear-only bit) */
90+
#define USB_ISTR_WKUP ((uint16_t)0x1000U) /*!< WaKe UP (clear-only bit) */
91+
#define USB_ISTR_SUSP ((uint16_t)0x0800U) /*!< SUSPend (clear-only bit) */
92+
#define USB_ISTR_RESET ((uint16_t)0x0400U) /*!< RESET (clear-only bit) */
93+
#define USB_ISTR_SOF ((uint16_t)0x0200U) /*!< Start Of Frame (clear-only bit) */
94+
#define USB_ISTR_ESOF ((uint16_t)0x0100U) /*!< Expected Start Of Frame (clear-only bit) */
95+
#define USB_ISTR_DIR ((uint16_t)0x0010U) /*!< DIRection of transaction (read-only bit) */
96+
#define USB_ISTR_EP_ID ((uint16_t)0x000FU) /*!< EndPoint IDentifier (read-only bit) */
97+
98+
/* Legacy defines */
99+
#define USB_ISTR_PMAOVRM USB_ISTR_PMAOVR
100+
101+
#define USB_CLR_CTR (~USB_ISTR_CTR) /*!< clear Correct TRansfer bit */
102+
#define USB_CLR_PMAOVR (~USB_ISTR_PMAOVR) /*!< clear DMA OVeR/underrun bit*/
103+
#define USB_CLR_ERR (~USB_ISTR_ERR) /*!< clear ERRor bit */
104+
#define USB_CLR_WKUP (~USB_ISTR_WKUP) /*!< clear WaKe UP bit */
105+
#define USB_CLR_SUSP (~USB_ISTR_SUSP) /*!< clear SUSPend bit */
106+
#define USB_CLR_RESET (~USB_ISTR_RESET) /*!< clear RESET bit */
107+
#define USB_CLR_SOF (~USB_ISTR_SOF) /*!< clear Start Of Frame bit */
108+
#define USB_CLR_ESOF (~USB_ISTR_ESOF) /*!< clear Expected Start Of Frame bit */
109+
110+
/* Legacy defines */
111+
#define USB_CLR_PMAOVRM USB_CLR_PMAOVR
112+
113+
/************************* CNTR control register bits definitions ***********/
114+
#define USB_CNTR_CTRM ((uint16_t)0x8000U) /*!< Correct TRansfer Mask */
115+
#define USB_CNTR_PMAOVR ((uint16_t)0x4000U) /*!< DMA OVeR/underrun Mask */
116+
#define USB_CNTR_ERRM ((uint16_t)0x2000U) /*!< ERRor Mask */
117+
#define USB_CNTR_WKUPM ((uint16_t)0x1000U) /*!< WaKe UP Mask */
118+
#define USB_CNTR_SUSPM ((uint16_t)0x0800U) /*!< SUSPend Mask */
119+
#define USB_CNTR_RESETM ((uint16_t)0x0400U) /*!< RESET Mask */
120+
#define USB_CNTR_SOFM ((uint16_t)0x0200U) /*!< Start Of Frame Mask */
121+
#define USB_CNTR_ESOFM ((uint16_t)0x0100U) /*!< Expected Start Of Frame Mask */
122+
#define USB_CNTR_RESUME ((uint16_t)0x0010U) /*!< RESUME request */
123+
#define USB_CNTR_FSUSP ((uint16_t)0x0008U) /*!< Force SUSPend */
124+
#define USB_CNTR_LPMODE ((uint16_t)0x0004U) /*!< Low-power MODE */
125+
#define USB_CNTR_PDWN ((uint16_t)0x0002U) /*!< Power DoWN */
126+
#define USB_CNTR_FRES ((uint16_t)0x0001U) /*!< Force USB RESet */
127+
128+
/* Legacy defines */
129+
#define USB_CNTR_PMAOVRM USB_CNTR_PMAOVR
130+
#define USB_CNTR_LP_MODE USB_CNTR_LPMODE
131+
132+
/******************** FNR Frame Number Register bit definitions ************/
133+
#define USB_FNR_RXDP ((uint16_t)0x8000U) /*!< status of D+ data line */
134+
#define USB_FNR_RXDM ((uint16_t)0x4000U) /*!< status of D- data line */
135+
#define USB_FNR_LCK ((uint16_t)0x2000U) /*!< LoCKed */
136+
#define USB_FNR_LSOF ((uint16_t)0x1800U) /*!< Lost SOF */
137+
#define USB_FNR_FN ((uint16_t)0x07FFU) /*!< Frame Number */
138+
139+
/******************** DADDR Device ADDRess bit definitions ****************/
140+
#define USB_DADDR_EF ((uint8_t)0x80U) /*!< USB device address Enable Function */
141+
#define USB_DADDR_ADD ((uint8_t)0x7FU) /*!< USB device address */
142+
143+
/****************************** Endpoint register *************************/
144+
#define USB_EP0R USB_BASE /*!< endpoint 0 register address */
145+
#define USB_EP1R (USB_BASE + 0x04U) /*!< endpoint 1 register address */
146+
#define USB_EP2R (USB_BASE + 0x08U) /*!< endpoint 2 register address */
147+
#define USB_EP3R (USB_BASE + 0x0CU) /*!< endpoint 3 register address */
148+
#define USB_EP4R (USB_BASE + 0x10U) /*!< endpoint 4 register address */
149+
#define USB_EP5R (USB_BASE + 0x14U) /*!< endpoint 5 register address */
150+
#define USB_EP6R (USB_BASE + 0x18U) /*!< endpoint 6 register address */
151+
#define USB_EP7R (USB_BASE + 0x1CU) /*!< endpoint 7 register address */
152+
/* bit positions */
153+
#define USB_EP_CTR_RX ((uint16_t)0x8000U) /*!< EndPoint Correct TRansfer RX */
154+
#define USB_EP_DTOG_RX ((uint16_t)0x4000U) /*!< EndPoint Data TOGGLE RX */
155+
#define USB_EPRX_STAT ((uint16_t)0x3000U) /*!< EndPoint RX STATus bit field */
156+
#define USB_EP_SETUP ((uint16_t)0x0800U) /*!< EndPoint SETUP */
157+
#define USB_EP_T_FIELD ((uint16_t)0x0600U) /*!< EndPoint TYPE */
158+
#define USB_EP_KIND ((uint16_t)0x0100U) /*!< EndPoint KIND */
159+
#define USB_EP_CTR_TX ((uint16_t)0x0080U) /*!< EndPoint Correct TRansfer TX */
160+
#define USB_EP_DTOG_TX ((uint16_t)0x0040U) /*!< EndPoint Data TOGGLE TX */
161+
#define USB_EPTX_STAT ((uint16_t)0x0030U) /*!< EndPoint TX STATus bit field */
162+
#define USB_EPADDR_FIELD ((uint16_t)0x000FU) /*!< EndPoint ADDRess FIELD */
163+
164+
/* EndPoint REGister MASK (no toggle fields) */
165+
#define USB_EPREG_MASK (USB_EP_CTR_RX|USB_EP_SETUP|USB_EP_T_FIELD|USB_EP_KIND|USB_EP_CTR_TX|USB_EPADDR_FIELD)
166+
/*!< EP_TYPE[1:0] EndPoint TYPE */
167+
#define USB_EP_TYPE_MASK ((uint16_t)0x0600U) /*!< EndPoint TYPE Mask */
168+
#define USB_EP_BULK ((uint16_t)0x0000U) /*!< EndPoint BULK */
169+
#define USB_EP_CONTROL ((uint16_t)0x0200U) /*!< EndPoint CONTROL */
170+
#define USB_EP_ISOCHRONOUS ((uint16_t)0x0400U) /*!< EndPoint ISOCHRONOUS */
171+
#define USB_EP_INTERRUPT ((uint16_t)0x0600U) /*!< EndPoint INTERRUPT */
172+
#define USB_EP_T_MASK ((uint16_t) ~USB_EP_T_FIELD & USB_EPREG_MASK)
173+
174+
#define USB_EPKIND_MASK ((uint16_t) ~USB_EP_KIND & USB_EPREG_MASK) /*!< EP_KIND EndPoint KIND */
175+
/*!< STAT_TX[1:0] STATus for TX transfer */
176+
#define USB_EP_TX_DIS ((uint16_t)0x0000U) /*!< EndPoint TX DISabled */
177+
#define USB_EP_TX_STALL ((uint16_t)0x0010U) /*!< EndPoint TX STALLed */
178+
#define USB_EP_TX_NAK ((uint16_t)0x0020U) /*!< EndPoint TX NAKed */
179+
#define USB_EP_TX_VALID ((uint16_t)0x0030U) /*!< EndPoint TX VALID */
180+
#define USB_EPTX_DTOG1 ((uint16_t)0x0010U) /*!< EndPoint TX Data TOGgle bit1 */
181+
#define USB_EPTX_DTOG2 ((uint16_t)0x0020U) /*!< EndPoint TX Data TOGgle bit2 */
182+
#define USB_EPTX_DTOGMASK (USB_EPTX_STAT|USB_EPREG_MASK)
183+
/*!< STAT_RX[1:0] STATus for RX transfer */
184+
#define USB_EP_RX_DIS ((uint16_t)0x0000U) /*!< EndPoint RX DISabled */
185+
#define USB_EP_RX_STALL ((uint16_t)0x1000U) /*!< EndPoint RX STALLed */
186+
#define USB_EP_RX_NAK ((uint16_t)0x2000U) /*!< EndPoint RX NAKed */
187+
#define USB_EP_RX_VALID ((uint16_t)0x3000U) /*!< EndPoint RX VALID */
188+
#define USB_EPRX_DTOG1 ((uint16_t)0x1000U) /*!< EndPoint RX Data TOGgle bit1 */
189+
#define USB_EPRX_DTOG2 ((uint16_t)0x2000U) /*!< EndPoint RX Data TOGgle bit1 */
190+
#define USB_EPRX_DTOGMASK (USB_EPRX_STAT|USB_EPREG_MASK)
191+
192+
193+
//--------------------------------------------------------------------+
194+
//
195+
//--------------------------------------------------------------------+
196+
197+
#if CFG_TUSB_MCU == OPT_MCU_CH32V20X
198+
static const IRQn_Type fsdev_irq[] = {
199+
USB_HP_CAN1_TX_IRQn,
200+
USB_LP_CAN1_RX0_IRQn,
201+
USBWakeUp_IRQn
202+
};
203+
enum { FSDEV_IRQ_NUM = TU_ARRAY_SIZE(fsdev_irq) };
204+
#else
205+
#error "Unsupported MCU"
206+
#endif
207+
208+
void dcd_int_enable(uint8_t rhport) {
209+
(void)rhport;
210+
for(uint8_t i=0; i < FSDEV_IRQ_NUM; i++) {
211+
NVIC_EnableIRQ(fsdev_irq[i]);
212+
}
213+
}
214+
215+
void dcd_int_disable(uint8_t rhport) {
216+
(void)rhport;
217+
for(uint8_t i=0; i < FSDEV_IRQ_NUM; i++) {
218+
NVIC_DisableIRQ(fsdev_irq[i]);
219+
}
220+
}
221+
222+
void dcd_disconnect(uint8_t rhport) {
223+
(void) rhport;
224+
EXTEN->EXTEN_CTR &= ~EXTEN_USBD_PU_EN;
225+
}
226+
227+
void dcd_connect(uint8_t rhport) {
228+
(void) rhport;
229+
EXTEN->EXTEN_CTR |= EXTEN_USBD_PU_EN;
230+
}
231+
232+
#endif

0 commit comments

Comments
 (0)