Skip to content

Commit 2a18d68

Browse files
authored
[bsp][nrf5x]added the cherryusb adapter for nrf52840 (#9939)
1 parent 4d4c966 commit 2a18d68

File tree

10 files changed

+1579
-2
lines changed

10 files changed

+1579
-2
lines changed

bsp/nrf5x/nrf52840/.ci/attachconfig/ci.attachconfig.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,13 @@ nimble.uart:
9898
segger:
9999
kconfig:
100100
- CONFIG_PKG_USING_SEGGER_RTT=y
101-
- CONFIG_RT_USING_SERIAL_V2=y
101+
- CONFIG_RT_USING_SERIAL_V2=y
102+
# ------ component CI ------
103+
component.cherryusb_hid_keyboard:
104+
kconfig:
105+
- CONFIG_RT_USING_CHERRYUSB=y
106+
- CONFIG_RT_CHERRYUSB_DEVICE=y
107+
- CONFIG_RT_CHERRYUSB_DEVICE_NRF5X=y
108+
- CONFIG_RT_CHERRYUSB_DEVICE_HID=y
109+
- CONFIG_RT_CHERRYUSB_DEVICE_TEMPLATE_HID_KEYBOARD=y
110+
- CONFIG_RT_USING_MESSAGEQUEUE=y

bsp/nrf5x/nrf52840/board/SConscript

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@ src = Glob('*.c')
77
CPPPATH = [cwd]
88

99
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = CPPPATH)
10+
11+
list = os.listdir(cwd)
12+
for item in list:
13+
if os.path.isfile(os.path.join(cwd, item, 'SConscript')):
14+
group = group + SConscript(os.path.join(item, 'SConscript'))
15+
1016
Return('group')
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os
2+
from building import *
3+
4+
cwd = GetCurrentDir()
5+
6+
# add general drivers
7+
src = []
8+
path = []
9+
10+
if GetDepend(['RT_USING_CHERRYUSB']):
11+
src += Glob('cherryusb/cherryusb.c')
12+
path += [cwd + '/cherryusb']
13+
14+
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = path)
15+
16+
list = os.listdir(cwd)
17+
for item in list:
18+
if os.path.isfile(os.path.join(cwd, item, 'SConscript')):
19+
group = group + SConscript(os.path.join(item, 'SConscript'))
20+
21+
Return('group')
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <rtthread.h>
2+
#include <stdint.h>
3+
#include <stdbool.h>
4+
#include <stddef.h>
5+
#include <stdio.h>
6+
#include "nrf.h"
7+
#include "nrfx_usbd.h"
8+
#include "nrfx_clock.h"
9+
#include "nrfx_power.h"
10+
11+
void usb_dc_low_level_post_init(void)
12+
{
13+
/* Enable interrupt globally */
14+
NRFX_IRQ_PRIORITY_SET(USBD_IRQn, NRFX_USBD_CONFIG_IRQ_PRIORITY);
15+
NRFX_IRQ_ENABLE(USBD_IRQn);
16+
}
17+
18+
extern void cherry_usb_hal_nrf_power_event(uint32_t event);
19+
static void power_event_handler(nrfx_power_usb_evt_t event)
20+
{
21+
cherry_usb_hal_nrf_power_event((uint32_t)event);
22+
}
23+
24+
void usb_dc_low_level_pre_init(void)
25+
{
26+
uint32_t usb_reg;
27+
const nrfx_power_usbevt_config_t config = {.handler = power_event_handler};
28+
nrfx_power_usbevt_init(&config);
29+
nrfx_power_usbevt_enable();
30+
usb_reg = NRF_POWER->USBREGSTATUS;
31+
32+
if (usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk)
33+
{
34+
cherry_usb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED);
35+
}
36+
37+
if (usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk)
38+
{
39+
cherry_usb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
40+
}
41+
}
42+
43+
void usb_low_clear_pending_irq(void)
44+
{
45+
NVIC_ClearPendingIRQ(USBD_IRQn);
46+
}
47+
48+
void usb_low_disable_irq(void)
49+
{
50+
NVIC_DisableIRQ(USBD_IRQn);
51+
}
52+
53+
int cherryusb_protocol_stack_init(void)
54+
{
55+
#ifdef RT_CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM
56+
extern void cdc_acm_init(void);
57+
cdc_acm_init();
58+
rt_kprintf("cdc acm example started. \r\n");
59+
#elif defined RT_CHERRYUSB_DEVICE_TEMPLATE_MSC
60+
extern void msc_ram_init(void);
61+
msc_ram_init();
62+
rt_kprintf("msc ram example started. \r\n");
63+
#elif defined RT_CHERRYUSB_DEVICE_TEMPLATE_HID_KEYBOARD
64+
extern void hid_keyboard_init(uint8_t busid, uintptr_t reg_base);
65+
hid_keyboard_init(0,NULL);
66+
rt_kprintf("hid keyboard example started. \r\n");
67+
#endif
68+
}
69+
70+
INIT_APP_EXPORT(cherryusb_protocol_stack_init);
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Copyright (c) 2022, sakumisu
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#ifndef CHERRYUSB_CONFIG_H
7+
#define CHERRYUSB_CONFIG_H
8+
9+
10+
#include <rtthread.h>
11+
12+
/* ================ USB common Configuration ================ */
13+
#define CONFIG_USB_PRINTF(...) rt_kprintf(__VA_ARGS__)
14+
#define usb_malloc(size) malloc(size)
15+
#define usb_free(ptr) free(ptr)
16+
17+
#ifndef CONFIG_USB_DBG_LEVEL
18+
//#define CONFIG_USB_DBG_LEVEL USB_DBG_INFO
19+
#define CONFIG_USB_DBG_LEVEL 3
20+
#endif
21+
22+
/* Enable print with color */
23+
#define CONFIG_USB_PRINTF_COLOR_ENABLE
24+
25+
/* data align size when use dma */
26+
#ifndef CONFIG_USB_ALIGN_SIZE
27+
#define CONFIG_USB_ALIGN_SIZE 4
28+
#endif
29+
30+
/* attribute data into no cache ram */
31+
#define USB_NOCACHE_RAM_SECTION __attribute__((section(".noncacheable")))
32+
33+
/* ================= USB Device Stack Configuration ================ */
34+
35+
/* Ep0 max transfer buffer, specially for receiving data from ep0 out */
36+
#define CONFIG_USBDEV_REQUEST_BUFFER_LEN 256
37+
38+
#ifndef CONFIG_USBDEV_MSC_MAX_LUN
39+
#define CONFIG_USBDEV_MSC_MAX_LUN 1
40+
#endif
41+
42+
#ifndef CONFIG_USBDEV_MSC_MAX_BUFSIZE
43+
#define CONFIG_USBDEV_MSC_MAX_BUFSIZE 512
44+
#endif
45+
46+
#ifndef CONFIG_USBDEV_MSC_MANUFACTURER_STRING
47+
#define CONFIG_USBDEV_MSC_MANUFACTURER_STRING ""
48+
#endif
49+
50+
#ifndef CONFIG_USBDEV_MSC_PRODUCT_STRING
51+
#define CONFIG_USBDEV_MSC_PRODUCT_STRING ""
52+
#endif
53+
54+
#ifndef CONFIG_USBDEV_MSC_VERSION_STRING
55+
#define CONFIG_USBDEV_MSC_VERSION_STRING "0.01"
56+
#endif
57+
58+
#ifndef CONFIG_USBDEV_MSC_PRIO
59+
#define CONFIG_USBDEV_MSC_PRIO 4
60+
#endif
61+
62+
#ifndef CONFIG_USBDEV_MSC_STACKSIZE
63+
#define CONFIG_USBDEV_MSC_STACKSIZE 2048
64+
#endif
65+
66+
#ifndef CONFIG_USBDEV_RNDIS_RESP_BUFFER_SIZE
67+
#define CONFIG_USBDEV_RNDIS_RESP_BUFFER_SIZE 156
68+
#endif
69+
70+
#ifndef CONFIG_USBDEV_RNDIS_ETH_MAX_FRAME_SIZE
71+
#define CONFIG_USBDEV_RNDIS_ETH_MAX_FRAME_SIZE 1536
72+
#endif
73+
74+
#ifndef CONFIG_USBDEV_RNDIS_VENDOR_ID
75+
#define CONFIG_USBDEV_RNDIS_VENDOR_ID 0x0000ffff
76+
#endif
77+
78+
#ifndef CONFIG_USBDEV_RNDIS_VENDOR_DESC
79+
#define CONFIG_USBDEV_RNDIS_VENDOR_DESC "CherryUSB"
80+
#endif
81+
82+
#define CONFIG_USBDEV_RNDIS_USING_LWIP
83+
84+
/* ================ USB HOST Stack Configuration ================== */
85+
86+
#define CONFIG_USBHOST_MAX_RHPORTS 1
87+
#define CONFIG_USBHOST_MAX_EXTHUBS 1
88+
#define CONFIG_USBHOST_MAX_EHPORTS 4
89+
#define CONFIG_USBHOST_MAX_INTERFACES 8
90+
#define CONFIG_USBHOST_MAX_INTF_ALTSETTINGS 8
91+
#define CONFIG_USBHOST_MAX_ENDPOINTS 4
92+
93+
#define CONFIG_USBHOST_MAX_CDC_ACM_CLASS 4
94+
#define CONFIG_USBHOST_MAX_HID_CLASS 4
95+
#define CONFIG_USBHOST_MAX_MSC_CLASS 2
96+
#define CONFIG_USBHOST_MAX_AUDIO_CLASS 1
97+
#define CONFIG_USBHOST_MAX_VIDEO_CLASS 1
98+
99+
#define CONFIG_USBHOST_DEV_NAMELEN 16
100+
101+
#ifndef CONFIG_USBHOST_PSC_PRIO
102+
#define CONFIG_USBHOST_PSC_PRIO 0
103+
#endif
104+
#ifndef CONFIG_USBHOST_PSC_STACKSIZE
105+
#define CONFIG_USBHOST_PSC_STACKSIZE 2048
106+
#endif
107+
108+
#define CONFIG_USBHOST_MSOS_VENDOR_CODE 0x00
109+
110+
/* Ep0 max transfer buffer */
111+
#define CONFIG_USBHOST_REQUEST_BUFFER_LEN 512
112+
113+
#ifndef CONFIG_USBHOST_CONTROL_TRANSFER_TIMEOUT
114+
#define CONFIG_USBHOST_CONTROL_TRANSFER_TIMEOUT 500
115+
#endif
116+
117+
#ifndef CONFIG_USBHOST_MSC_TIMEOUT
118+
#define CONFIG_USBHOST_MSC_TIMEOUT 5000
119+
#endif
120+
121+
/* ================ USB Device Port Configuration ================*/
122+
#define CONFIG_USBDEV_MAX_BUS 1
123+
124+
#define CONFIG_USBDEV_EP_NUM 8
125+
#define CONFIG_USBDEV_FSDEV_PMA_ACCESS 2
126+
127+
/* ================ USB Host Port Configuration ==================*/
128+
129+
#define CONFIG_USBHOST_PIPE_NUM 10
130+
131+
/* ================ EHCI Configuration ================ */
132+
133+
#define CONFIG_USB_EHCI_HCCR_BASE (0x20072000)
134+
#define CONFIG_USB_EHCI_HCOR_BASE (0x20072000 + 0x10)
135+
#define CONFIG_USB_EHCI_FRAME_LIST_SIZE 1024
136+
137+
#endif

bsp/nrf5x/nrf52840/board/sdk_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2472,7 +2472,7 @@
24722472
// <e> NRFX_POWER_ENABLED - nrfx_power - POWER peripheral driver
24732473
//==========================================================
24742474
#ifndef NRFX_POWER_ENABLED
2475-
#define NRFX_POWER_ENABLED 0
2475+
#define NRFX_POWER_ENABLED 1
24762476
#endif
24772477
// <o> NRFX_POWER_DEFAULT_CONFIG_IRQ_PRIORITY - Interrupt priority
24782478

components/drivers/usb/cherryusb/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ if RT_USING_CHERRYUSB
6868
bool "aic"
6969
config RT_CHERRYUSB_DEVICE_PUSB2
7070
bool "pusb2"
71+
config RT_CHERRYUSB_DEVICE_NRF5X
72+
bool "nrf5x"
7173
endchoice
7274

7375
config RT_CHERRYUSB_DEVICE_CDC_ACM

components/drivers/usb/cherryusb/SConscript

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ if GetDepend(['RT_CHERRYUSB_DEVICE']):
2828
if GetDepend(['RT_CHERRYUSB_DEVICE_SPEED_HS']):
2929
CPPDEFINES+=['CONFIG_USB_HS']
3030

31+
if GetDepend(['RT_CHERRYUSB_DEVICE_NRF5X']):
32+
src += Glob('port/nrf5x/usb_dc_nrf5x.c')
3133
if GetDepend(['RT_CHERRYUSB_DEVICE_FSDEV']):
3234
src += Glob('port/fsdev/usb_dc_fsdev.c')
3335
if GetDepend(['RT_CHERRYUSB_DEVICE_DWC2_ST']):
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Note
2+
3+
## Support Chip List
4+
5+
- NRF5x
6+
7+
## Before Use
8+
9+
- Your should implement `usb_dc_low_level_pre_init`,`usb_dc_low_level_post_init`,`usb_dc_low_level_deinit`.

0 commit comments

Comments
 (0)