Skip to content

Commit d9cc3b0

Browse files
committed
[bsp][nrf5x]added the cherryusb adapter for nrf52840
1 parent ed3222c commit d9cc3b0

File tree

11 files changed

+1611
-2
lines changed

11 files changed

+1611
-2
lines changed

bsp/nrf5x/nrf52840/.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ CONFIG_RT_USING_SEMAPHORE=y
4848
CONFIG_RT_USING_MUTEX=y
4949
CONFIG_RT_USING_EVENT=y
5050
# CONFIG_RT_USING_MAILBOX is not set
51-
# CONFIG_RT_USING_MESSAGEQUEUE is not set
51+
CONFIG_RT_USING_MESSAGEQUEUE=y
5252
# CONFIG_RT_USING_SIGNALS is not set
5353

5454
#

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_usbd_compostite_init.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: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
3+
*
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without modification,
7+
* are permitted provided that the following conditions are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright notice, this
10+
* list of conditions and the following disclaimer.
11+
*
12+
* 2. Redistributions in binary form, except as embedded into a Nordic
13+
* Semiconductor ASA integrated circuit in a product or a software update for
14+
* such product, must reproduce the above copyright notice, this list of
15+
* conditions and the following disclaimer in the documentation and/or other
16+
* materials provided with the distribution.
17+
*
18+
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
19+
* contributors may be used to endorse or promote products derived from this
20+
* software without specific prior written permission.
21+
*
22+
* 4. This software, with or without modification, must only be used with a
23+
* Nordic Semiconductor ASA integrated circuit.
24+
*
25+
* 5. Any software provided in binary form under this license must not be reverse
26+
* engineered, decompiled, modified and/or disassembled.
27+
*
28+
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
29+
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30+
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
31+
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
32+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
34+
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
37+
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38+
*
39+
*/
40+
#include <rtthread.h>
41+
#include <stdint.h>
42+
#include <stdbool.h>
43+
#include <stddef.h>
44+
#include <stdio.h>
45+
#include "nrf.h"
46+
#include "nrfx_usbd.h"
47+
#include "nrfx_clock.h"
48+
#include "nrfx_power.h"
49+
50+
void usb_dc_low_level_post_init(void)
51+
{
52+
/* Enable interrupt globally */
53+
NRFX_IRQ_PRIORITY_SET(USBD_IRQn, NRFX_USBD_CONFIG_IRQ_PRIORITY);
54+
NRFX_IRQ_ENABLE(USBD_IRQn);
55+
}
56+
57+
extern void cherry_usb_hal_nrf_power_event(uint32_t event);
58+
static void power_event_handler(nrfx_power_usb_evt_t event)
59+
{
60+
cherry_usb_hal_nrf_power_event((uint32_t)event);
61+
}
62+
63+
void usb_dc_low_level_pre_init(void)
64+
{
65+
uint32_t usb_reg;
66+
const nrfx_power_usbevt_config_t config = {.handler = power_event_handler};
67+
nrfx_power_usbevt_init(&config);
68+
nrfx_power_usbevt_enable();
69+
usb_reg = NRF_POWER->USBREGSTATUS;
70+
71+
if (usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk)
72+
{
73+
cherry_usb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED);
74+
}
75+
76+
if (usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk)
77+
{
78+
cherry_usb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
79+
}
80+
}
81+
82+
void usb_low_clear_pending_irq(void)
83+
{
84+
NVIC_ClearPendingIRQ(USBD_IRQn);
85+
}
86+
87+
void usb_low_disable_irq(void)
88+
{
89+
NVIC_DisableIRQ(USBD_IRQn);
90+
}
91+
int cherryusb_protocol_stack_init(void)
92+
{
93+
#ifdef RT_CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM
94+
extern void cdc_acm_init(void);
95+
cdc_acm_init();
96+
rt_kprintf("cdc acm example started. \r\n");
97+
nrf_delay_ms(5000);
98+
#elif defined RT_CHERRYUSB_DEVICE_TEMPLATE_MSC
99+
extern void msc_ram_init(void);
100+
msc_ram_init();
101+
rt_kprintf("msc ram example started. \r\n");
102+
nrf_delay_ms(5000);
103+
#elif defined RT_CHERRYUSB_DEVICE_TEMPLATE_HID_KEYBOARD
104+
extern void hid_keyboard_init(uint8_t busid, uintptr_t reg_base);
105+
hid_keyboard_init(0,NULL);
106+
rt_kprintf("hid keyboard example started. \r\n");
107+
// nrf_delay_ms(5000);
108+
#endif
109+
}
110+
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

bsp/nrf5x/nrf52840/rtconfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#define RT_USING_SEMAPHORE
3333
#define RT_USING_MUTEX
3434
#define RT_USING_EVENT
35+
#define RT_USING_MESSAGEQUEUE
3536

3637
/* Memory Management */
3738

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)