Skip to content

Commit 0edbc15

Browse files
authored
[cherryusb][fsdev] add N32H4x USBFS device support #11764
1 parent 3eff2d5 commit 0edbc15

6 files changed

Lines changed: 619 additions & 4 deletions

File tree

‎components/drivers/usb/cherryusb/Kconfig‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ if RT_USING_CHERRYUSB
2828
bool "CUSTOM (Implement it yourself)"
2929
config RT_CHERRYUSB_DEVICE_FSDEV_ST
3030
bool "fsdev_st"
31+
config RT_CHERRYUSB_DEVICE_FSDEV_NATION
32+
bool "fsdev_nation"
3133
config RT_CHERRYUSB_DEVICE_FSDEV_CUSTOM
3234
bool "fsdev_custom"
3335
config RT_CHERRYUSB_DEVICE_DWC2_ST

‎components/drivers/usb/cherryusb/SConscript‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ if GetDepend(['RT_CHERRYUSB_DEVICE']):
3737
if GetDepend(['RT_CHERRYUSB_DEVICE_FSDEV_ST']):
3838
src += Glob('port/fsdev/usb_dc_fsdev.c')
3939
src += Glob('port/fsdev/usb_glue_st.c')
40+
if GetDepend(['RT_CHERRYUSB_DEVICE_FSDEV_NATION']):
41+
src += Glob('port/fsdev/usb_dc_fsdev.c')
42+
src += Glob('port/fsdev/usb_glue_nation.c')
4043
if GetDepend(['RT_CHERRYUSB_DEVICE_FSDEV_CUSTOM']):
4144
src += Glob('port/fsdev/usb_dc_fsdev.c')
4245
if GetDepend(['RT_CHERRYUSB_DEVICE_DWC2_ST']):

‎components/drivers/usb/cherryusb/port/fsdev/README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,11 @@
2828
## CH32
2929

3030
- CH32F10x、CH32V10x
31+
32+
## N32
33+
34+
- N32H473、N32H474、N32H475、N32H481、N32H482、N32H480、N32H487、N32H488
35+
36+
- N32H49X
37+
38+

‎components/drivers/usb/cherryusb/port/fsdev/usb_dc_fsdev.c‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
#include "usbd_core.h"
77

8-
#if (CONFIG_USB_DBG_LEVEL >= USB_DBG_LOG)
8+
#if defined(CONFIG_USB_DBG_LEVEL) && defined(USB_DBG_LOG) && (CONFIG_USB_DBG_LEVEL >= USB_DBG_LOG)
99
#error "fsdev cannot enable USB_DBG_LOG"
1010
#endif
1111

@@ -60,10 +60,30 @@ __WEAK void usb_dc_low_level_deinit(void)
6060
{
6161
}
6262

63+
/**
64+
* @brief Initialize the USB device controller.
65+
* @param busid USB bus index (fsdev supports a single instance, bus 0).
66+
* @retval 0 on success.
67+
*/
6368
int usb_dc_init(uint8_t busid)
6469
{
6570
usb_dc_low_level_init();
6671

72+
#if defined(N32H4X_FSDEV)
73+
/* N32H4x power-on sequence: the USB_CTRL reset value is 0x0300
74+
* -- PD(bit9, power-down) and FRST(bit8, force-reset) are both set.
75+
* 1. Clear PD, wait for the internal analog voltage reference to settle.
76+
* 2. Clear FRST.
77+
* 3. Clear the STS register to drop pending interrupts. */
78+
USB->CNTR &= (uint32_t)~USB_CNTR_PDWN; /* exit power-down */
79+
80+
/* Wait for the internal reference voltage to stabilise. */
81+
for (volatile uint32_t i = 0U; i < 1000U; i++) {
82+
}
83+
84+
USB->CNTR &= (uint32_t)~USB_CNTR_FRES; /* clear force-reset flag */
85+
USB->ISTR = 0U; /* clear pending interrupts */
86+
#else
6787
/* Init Device */
6888
/* CNTR_FRES = 1 */
6989
USB->CNTR = (uint16_t)USB_CNTR_FRES;
@@ -73,6 +93,7 @@ int usb_dc_init(uint8_t busid)
7393

7494
/* Clear pending interrupts */
7595
USB->ISTR = 0U;
96+
#endif
7697

7798
/*Set Btable Address*/
7899
USB->BTABLE = BTABLE_ADDRESS;
@@ -91,8 +112,13 @@ int usb_dc_init(uint8_t busid)
91112
/* Set interrupt mask */
92113
USB->CNTR = (uint16_t)winterruptmask;
93114

115+
#if defined(N32H4X_FSDEV)
116+
/* Enabling DP Pull-UP through USB_CTRL.PU */
117+
USB->CNTR |= (uint32_t)USB_CNTR_PU;
118+
#else
94119
/* Enabling DP Pull-UP bit to Connect internal PU resistor on USB DP line */
95120
USB->BCDR |= (uint16_t)USB_BCDR_DPPU;
121+
#endif
96122

97123
return 0;
98124
}
@@ -325,6 +351,10 @@ int usbd_ep_start_read(uint8_t busid, const uint8_t ep, uint8_t *data, uint32_t
325351
return 0;
326352
}
327353

354+
/**
355+
* @brief USB FS device interrupt handler: dispatches ISTR events.
356+
* @param busid USB bus index (fsdev supports a single instance, bus 0).
357+
*/
328358
void USBD_IRQHandler(uint8_t busid)
329359
{
330360
uint16_t wIstr, wEPVal;
@@ -339,7 +369,13 @@ void USBD_IRQHandler(uint8_t busid)
339369
wIstr = USB->ISTR;
340370

341371
/* extract highest priority endpoint number */
372+
#if defined(N32H4X_FSDEV)
373+
/* EP_ID is at STS bits [11:8] */
374+
ep_idx = (uint8_t)((wIstr & USB_ISTR_EP_ID) >> USB_ISTR_EP_ID_Pos);
375+
#else
376+
/* ST: EP_ID is at ISTR bits [3:0] */
342377
ep_idx = (uint8_t)(wIstr & USB_ISTR_EP_ID);
378+
#endif
343379

344380
if (ep_idx == 0U) {
345381
if ((wIstr & USB_ISTR_DIR) == 0U) {

0 commit comments

Comments
 (0)