Skip to content

Commit ca4ab65

Browse files
committed
[bsp/stm32f407-rt-spark/l496zg] add cherryusb support
1 parent 4f40a74 commit ca4ab65

File tree

10 files changed

+843
-6
lines changed

10 files changed

+843
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ ncscope.*
4343
tags
4444

4545
.idea
46+
**/.cache/
4647
.vscode
4748
*.code-workspace
4849
*.eide.*

bsp/stm32/stm32f407-rt-spark/.ci/attachconfig/ci.attachconfig.yml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
scons.args: &scons
22
scons_arg:
33
- '--strict'
4+
# ------ nano CI ------
5+
nano:
6+
<<: *scons
7+
kconfig:
8+
- CONFIG_RT_USING_NANO=y
49
# ------ kernel CI ------
510
kernel.klibc-stdlib:
611
<<: *scons
@@ -182,8 +187,11 @@ peripheral.sram:
182187
peripheral.usb_mouse:
183188
kconfig:
184189
- CONFIG_BSP_USING_USB_MOUSE=y
185-
# ------ nano CI ------
186-
nano:
187-
<<: *scons
188-
kconfig:
189-
- CONFIG_RT_USING_NANO=y
190+
# ------ component CI ------
191+
component.cherryusb_cdc:
192+
kconfig:
193+
- CONFIG_RT_USING_CHERRYUSB=y
194+
- CONFIG_RT_CHERRYUSB_DEVICE=y
195+
- CONFIG_RT_CHERRYUSB_DEVICE_DWC2_ST=y
196+
- CONFIG_RT_CHERRYUSB_DEVICE_CDC_ACM=y
197+
- CONFIG_RT_CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM=y
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from building import *
2+
import os
3+
4+
cwd = GetCurrentDir()
5+
group = []
6+
src = Glob('*.c')
7+
CPPPATH = [cwd]
8+
9+
list = os.listdir(cwd)
10+
for d in list:
11+
path = os.path.join(cwd, d)
12+
if os.path.isfile(os.path.join(path, 'SConscript')):
13+
group = group + SConscript(os.path.join(d, 'SConscript'))
14+
15+
group = group + DefineGroup('cherryusb-port', src, depend = ['RT_CHERRYUSB_DEVICE'], CPPPATH = CPPPATH)
16+
Return('group')
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2025, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2025-01-17 Supperthomas first version
9+
*/
10+
#include <stm32f4xx.h>
11+
#include "rtthread.h"
12+
#include "drv_config.h"
13+
14+
void usb_dc_low_level_init(uint8_t busid)
15+
{
16+
GPIO_InitTypeDef GPIO_InitStruct = {0};
17+
18+
__HAL_RCC_GPIOA_CLK_ENABLE();
19+
/**USB_OTG_FS GPIO Configuration
20+
PA11 ------> USB_OTG_FS_DM
21+
PA12 ------> USB_OTG_FS_DP
22+
*/
23+
GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
24+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
25+
GPIO_InitStruct.Pull = GPIO_NOPULL;
26+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
27+
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
28+
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
29+
30+
/* Peripheral clock enable */
31+
__HAL_RCC_USB_OTG_FS_CLK_ENABLE();
32+
/* USB_OTG_FS interrupt Init */
33+
HAL_NVIC_SetPriority(OTG_FS_IRQn, 0, 0);
34+
HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
35+
}
36+
37+
void usb_dc_low_level_deinit(uint8_t busid)
38+
{
39+
/* Peripheral clock disable */
40+
__HAL_RCC_USB_OTG_FS_CLK_DISABLE();
41+
42+
/**USB_OTG_FS GPIO Configuration
43+
PA11 ------> USB_OTG_FS_DM
44+
PA12 ------> USB_OTG_FS_DP
45+
*/
46+
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);
47+
48+
/* USB_OTG_FS interrupt DeInit */
49+
HAL_NVIC_DisableIRQ(OTG_FS_IRQn);
50+
}
51+
52+
#ifdef RT_CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM
53+
/* Register the EMAC device */
54+
static int rt_hw_stm32_cherryusb_cdc_init(void)
55+
{
56+
extern void cdc_acm_init(uint8_t busid, uintptr_t reg_base);
57+
cdc_acm_init(0, USB_OTG_FS_PERIPH_BASE);
58+
59+
return 0;
60+
}
61+
INIT_COMPONENT_EXPORT(rt_hw_stm32_cherryusb_cdc_init);
62+
static int cherry_usb_cdc_send(int argc, char **argv)
63+
{
64+
extern void cdc_acm_data_send_with_dtr_test(uint8_t busid);
65+
cdc_acm_data_send_with_dtr_test(0);
66+
return 0;
67+
}
68+
MSH_CMD_EXPORT(cherry_usb_cdc_send, send the cdc data for test)
69+
#endif
70+
71+
#ifdef USBD_IRQ_HANDLER
72+
void USBD_IRQ_HANDLER(void)
73+
{
74+
extern void USBD_IRQHandler(uint8_t busid);
75+
USBD_IRQHandler(0);
76+
}
77+
#else
78+
#error USBD_IRQ_HANDLER need to USB IRQ like #define USBD_IRQ_HANDLER OTG_HS_IRQHandler
79+
#endif
80+
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
/*
2+
* Copyright (c) 2025, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2025-01-17 Supperthomas first version
9+
*/
10+
#ifndef CHERRYUSB_CONFIG_H
11+
#define CHERRYUSB_CONFIG_H
12+
13+
/* ================ USB common Configuration ================ */
14+
15+
#define CONFIG_USB_PRINTF(...) printf(__VA_ARGS__)
16+
17+
#ifndef CONFIG_USB_DBG_LEVEL
18+
#define CONFIG_USB_DBG_LEVEL USB_DBG_INFO
19+
#endif
20+
21+
/* Enable print with color */
22+
#define CONFIG_USB_PRINTF_COLOR_ENABLE
23+
24+
/* data align size when use dma */
25+
#ifndef CONFIG_USB_ALIGN_SIZE
26+
#define CONFIG_USB_ALIGN_SIZE 4
27+
#endif
28+
29+
/* attribute data into no cache ram */
30+
#define USB_NOCACHE_RAM_SECTION __attribute__((section(".noncacheable")))
31+
32+
/* ================= USB Device Stack Configuration ================ */
33+
34+
/* Ep0 in and out transfer buffer */
35+
#ifndef CONFIG_USBDEV_REQUEST_BUFFER_LEN
36+
#define CONFIG_USBDEV_REQUEST_BUFFER_LEN 512
37+
#endif
38+
39+
/* Setup packet log for debug */
40+
// #define CONFIG_USBDEV_SETUP_LOG_PRINT
41+
42+
/* Check if the input descriptor is correct */
43+
// #define CONFIG_USBDEV_DESC_CHECK
44+
45+
/* Enable test mode */
46+
// #define CONFIG_USBDEV_TEST_MODE
47+
48+
#ifndef CONFIG_USBDEV_MSC_MAX_LUN
49+
#define CONFIG_USBDEV_MSC_MAX_LUN 1
50+
#endif
51+
52+
#ifndef CONFIG_USBDEV_MSC_MAX_BUFSIZE
53+
#define CONFIG_USBDEV_MSC_MAX_BUFSIZE 512
54+
#endif
55+
56+
#ifndef CONFIG_USBDEV_MSC_MANUFACTURER_STRING
57+
#define CONFIG_USBDEV_MSC_MANUFACTURER_STRING ""
58+
#endif
59+
60+
#ifndef CONFIG_USBDEV_MSC_PRODUCT_STRING
61+
#define CONFIG_USBDEV_MSC_PRODUCT_STRING ""
62+
#endif
63+
64+
#ifndef CONFIG_USBDEV_MSC_VERSION_STRING
65+
#define CONFIG_USBDEV_MSC_VERSION_STRING "0.01"
66+
#endif
67+
68+
// #define CONFIG_USBDEV_MSC_THREAD
69+
70+
#ifndef CONFIG_USBDEV_MSC_PRIO
71+
#define CONFIG_USBDEV_MSC_PRIO 4
72+
#endif
73+
74+
#ifndef CONFIG_USBDEV_MSC_STACKSIZE
75+
#define CONFIG_USBDEV_MSC_STACKSIZE 2048
76+
#endif
77+
78+
#ifndef CONFIG_USBDEV_RNDIS_RESP_BUFFER_SIZE
79+
#define CONFIG_USBDEV_RNDIS_RESP_BUFFER_SIZE 156
80+
#endif
81+
82+
#ifndef CONFIG_USBDEV_RNDIS_ETH_MAX_FRAME_SIZE
83+
#define CONFIG_USBDEV_RNDIS_ETH_MAX_FRAME_SIZE 2048
84+
#endif
85+
86+
#ifndef CONFIG_USBDEV_RNDIS_VENDOR_ID
87+
#define CONFIG_USBDEV_RNDIS_VENDOR_ID 0x0000ffff
88+
#endif
89+
90+
#ifndef CONFIG_USBDEV_RNDIS_VENDOR_DESC
91+
#define CONFIG_USBDEV_RNDIS_VENDOR_DESC "CherryUSB"
92+
#endif
93+
94+
#define CONFIG_USBDEV_RNDIS_USING_LWIP
95+
96+
/* ================ USB HOST Stack Configuration ================== */
97+
98+
#define CONFIG_USBHOST_MAX_RHPORTS 1
99+
#define CONFIG_USBHOST_MAX_EXTHUBS 1
100+
#define CONFIG_USBHOST_MAX_EHPORTS 4
101+
#define CONFIG_USBHOST_MAX_INTERFACES 8
102+
#define CONFIG_USBHOST_MAX_INTF_ALTSETTINGS 8
103+
#define CONFIG_USBHOST_MAX_ENDPOINTS 4
104+
105+
#define CONFIG_USBHOST_MAX_CDC_ACM_CLASS 4
106+
#define CONFIG_USBHOST_MAX_HID_CLASS 4
107+
#define CONFIG_USBHOST_MAX_MSC_CLASS 2
108+
#define CONFIG_USBHOST_MAX_AUDIO_CLASS 1
109+
#define CONFIG_USBHOST_MAX_VIDEO_CLASS 1
110+
111+
#define CONFIG_USBHOST_DEV_NAMELEN 16
112+
113+
#ifndef CONFIG_USBHOST_PSC_PRIO
114+
#define CONFIG_USBHOST_PSC_PRIO 0
115+
#endif
116+
#ifndef CONFIG_USBHOST_PSC_STACKSIZE
117+
#define CONFIG_USBHOST_PSC_STACKSIZE 2048
118+
#endif
119+
120+
//#define CONFIG_USBHOST_GET_STRING_DESC
121+
122+
// #define CONFIG_USBHOST_MSOS_ENABLE
123+
#ifndef CONFIG_USBHOST_MSOS_VENDOR_CODE
124+
#define CONFIG_USBHOST_MSOS_VENDOR_CODE 0x00
125+
#endif
126+
127+
/* Ep0 max transfer buffer */
128+
#ifndef CONFIG_USBHOST_REQUEST_BUFFER_LEN
129+
#define CONFIG_USBHOST_REQUEST_BUFFER_LEN 512
130+
#endif
131+
132+
#ifndef CONFIG_USBHOST_CONTROL_TRANSFER_TIMEOUT
133+
#define CONFIG_USBHOST_CONTROL_TRANSFER_TIMEOUT 500
134+
#endif
135+
136+
#ifndef CONFIG_USBHOST_MSC_TIMEOUT
137+
#define CONFIG_USBHOST_MSC_TIMEOUT 5000
138+
#endif
139+
140+
/* This parameter affects usb performance, and depends on (TCP_WND)tcp eceive windows size,
141+
* you can change with 2K,4K,8K,16K,default is 2K to get one TCP_MSS
142+
*/
143+
#ifndef CONFIG_USBHOST_RNDIS_ETH_MAX_RX_SIZE
144+
#define CONFIG_USBHOST_RNDIS_ETH_MAX_RX_SIZE (2048)
145+
#endif
146+
#ifndef CONFIG_USBHOST_RNDIS_ETH_MAX_TX_SIZE
147+
#define CONFIG_USBHOST_RNDIS_ETH_MAX_TX_SIZE (2048)
148+
#endif
149+
150+
/* This parameter affects usb performance, and depends on (TCP_WND)tcp eceive windows size,
151+
* you can change with 2K,4K,8K,16K,default is 2K to get one TCP_MSS
152+
*/
153+
#ifndef CONFIG_USBHOST_CDC_NCM_ETH_MAX_RX_SIZE
154+
#define CONFIG_USBHOST_CDC_NCM_ETH_MAX_RX_SIZE (2048)
155+
#endif
156+
#ifndef CONFIG_USBHOST_CDC_NCM_ETH_MAX_TX_SIZE
157+
#define CONFIG_USBHOST_CDC_NCM_ETH_MAX_TX_SIZE (2048)
158+
#endif
159+
160+
#define CONFIG_USBHOST_BLUETOOTH_HCI_H4
161+
// #define CONFIG_USBHOST_BLUETOOTH_HCI_LOG
162+
163+
#ifndef CONFIG_USBHOST_BLUETOOTH_TX_SIZE
164+
#define CONFIG_USBHOST_BLUETOOTH_TX_SIZE 2048
165+
#endif
166+
#ifndef CONFIG_USBHOST_BLUETOOTH_RX_SIZE
167+
#define CONFIG_USBHOST_BLUETOOTH_RX_SIZE 2048
168+
#endif
169+
170+
/* ================ USB Device Port Configuration ================*/
171+
172+
#ifndef CONFIG_USBDEV_MAX_BUS
173+
#define CONFIG_USBDEV_MAX_BUS 1 // for now, bus num must be 1 except hpm ip
174+
#endif
175+
176+
#ifndef CONFIG_USBDEV_EP_NUM
177+
#define CONFIG_USBDEV_EP_NUM 4
178+
#endif
179+
180+
/* ---------------- FSDEV Configuration ---------------- */
181+
//#define CONFIG_USBDEV_FSDEV_PMA_ACCESS 2 // maybe 1 or 2, many chips may have a difference
182+
183+
/* ---------------- DWC2 Configuration ---------------- */
184+
// #define CONFIG_USB_DWC2_RXALL_FIFO_SIZE (1024 / 4)
185+
#define CONFIG_USB_DWC2_TX0_FIFO_SIZE (64 / 4)
186+
#define CONFIG_USB_DWC2_TX1_FIFO_SIZE (64 / 4)
187+
#define CONFIG_USB_DWC2_TX2_FIFO_SIZE (64 / 4)
188+
#define CONFIG_USB_DWC2_TX3_FIFO_SIZE (64 / 4)
189+
// #define CONFIG_USB_DWC2_TX4_FIFO_SIZE (0 / 4)
190+
// #define CONFIG_USB_DWC2_TX5_FIFO_SIZE (0 / 4)
191+
// #define CONFIG_USB_DWC2_TX6_FIFO_SIZE (0 / 4)
192+
// #define CONFIG_USB_DWC2_TX7_FIFO_SIZE (0 / 4)
193+
// #define CONFIG_USB_DWC2_TX8_FIFO_SIZE (0 / 4)
194+
195+
/* ---------------- MUSB Configuration ---------------- */
196+
// #define CONFIG_USB_MUSB_SUNXI
197+
198+
/* ================ USB Host Port Configuration ==================*/
199+
#ifndef CONFIG_USBHOST_MAX_BUS
200+
#define CONFIG_USBHOST_MAX_BUS 1
201+
#endif
202+
203+
#ifndef CONFIG_USBHOST_PIPE_NUM
204+
#define CONFIG_USBHOST_PIPE_NUM 12
205+
#endif
206+
207+
/* ---------------- EHCI Configuration ---------------- */
208+
209+
#define CONFIG_USB_EHCI_HCCR_OFFSET (0x0)
210+
#define CONFIG_USB_EHCI_FRAME_LIST_SIZE 1024
211+
#define CONFIG_USB_EHCI_QH_NUM CONFIG_USBHOST_PIPE_NUM
212+
#define CONFIG_USB_EHCI_QTD_NUM 3
213+
#define CONFIG_USB_EHCI_ITD_NUM 20
214+
// #define CONFIG_USB_EHCI_HCOR_RESERVED_DISABLE
215+
// #define CONFIG_USB_EHCI_CONFIGFLAG
216+
// #define CONFIG_USB_EHCI_ISO
217+
// #define CONFIG_USB_EHCI_WITH_OHCI
218+
219+
/* ---------------- OHCI Configuration ---------------- */
220+
#define CONFIG_USB_OHCI_HCOR_OFFSET (0x0)
221+
222+
/* ---------------- XHCI Configuration ---------------- */
223+
#define CONFIG_USB_XHCI_HCCR_OFFSET (0x0)
224+
225+
/* ---------------- DWC2 Configuration ---------------- */
226+
/* largest non-periodic USB packet used / 4 */
227+
// #define CONFIG_USB_DWC2_NPTX_FIFO_SIZE (512 / 4)
228+
/* largest periodic USB packet used / 4 */
229+
// #define CONFIG_USB_DWC2_PTX_FIFO_SIZE (1024 / 4)
230+
/*
231+
* (largest USB packet used / 4) + 1 for status information + 1 transfer complete +
232+
* 1 location each for Bulk/Control endpoint for handling NAK/NYET scenario
233+
*/
234+
// #define CONFIG_USB_DWC2_RX_FIFO_SIZE ((1012 - CONFIG_USB_DWC2_NPTX_FIFO_SIZE - CONFIG_USB_DWC2_PTX_FIFO_SIZE))
235+
236+
/* ---------------- MUSB Configuration ---------------- */
237+
// #define CONFIG_USB_MUSB_SUNXI
238+
239+
#endif

0 commit comments

Comments
 (0)