Skip to content

Commit 61bcece

Browse files
committed
[add]Add rtthread script support to better integrate into rtthread projects.
1 parent 50738f2 commit 61bcece

File tree

8 files changed

+553
-1
lines changed

8 files changed

+553
-1
lines changed

SConscript

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# RT-Thread building script for bridge
2+
3+
import os
4+
from building import *
5+
6+
objs = []
7+
cwd = GetCurrentDir()
8+
9+
objs = objs + SConscript(cwd + '/src/osal/rt-thread/SConscript')
10+
11+
Return('objs')

src/osal/osal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ typedef void (*osal_task_func_t)( void * );
6060
#elif CFG_TUSB_OS == OPT_OS_PICO
6161
#include "osal_pico.h"
6262
#elif CFG_TUSB_OS == OPT_OS_RTTHREAD
63-
#include "osal_rtthread.h"
63+
#include "rt-thread/osal_rtthread.h"
6464
#elif CFG_TUSB_OS == OPT_OS_RTX4
6565
#include "osal_rtx4.h"
6666
#elif CFG_TUSB_OS == OPT_OS_CUSTOM

src/osal/rt-thread/SConscript

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import rtconfig
2+
from building import *
3+
4+
cwd = GetCurrentDir()
5+
src = Split("""
6+
../../tusb.c
7+
../../common/tusb_fifo.c
8+
../../device/usbd.c
9+
../../device/usbd_control.c
10+
./tinyusb_port.c
11+
./usb_descriptor.c
12+
""")
13+
path = [cwd, cwd + "/../../../src"]
14+
15+
# BSP
16+
if GetDepend(["SOC_FAMILY_STM32"]):
17+
src += ["../../portable/synopsys/dwc2/dcd_dwc2.c",
18+
"../../portable/st/stm32_fsdev/dcd_stm32_fsdev.c"]
19+
20+
if GetDepend(["SOC_NRF52840"]):
21+
src += ["../../portable/nordic/nrf5x/dcd_nrf5x.c"]
22+
23+
if GetDepend(["SOC_FAMILY_RENESAS"]):
24+
src += ["../../portable/renesas/rusb2/dcd_rusb2.c",
25+
"../../portable/renesas/rusb2/rusb2_common.c"]
26+
27+
# Device class
28+
if GetDepend(["PKG_TINYUSB_DEVICE_CDC"]):
29+
src += ["../../class/cdc/cdc_device.c"]
30+
31+
if GetDepend(["PKG_TINYUSB_DEVICE_MSC"]):
32+
src += ["../../class/msc/msc_device.c", "port/msc_device_port.c"]
33+
34+
if GetDepend(["PKG_TINYUSB_DEVICE_HID"]):
35+
src += ["../../class/hid/hid_device.c", "port/hid_device_port.c"]
36+
37+
LOCAL_CFLAGS = ''
38+
39+
if rtconfig.PLATFORM == 'gcc' or rtconfig.PLATFORM == 'armclang': # GCC or Keil AC6
40+
LOCAL_CFLAGS += ' -std=c99'
41+
elif rtconfig.PLATFORM == 'armcc': # Keil AC5
42+
LOCAL_CFLAGS += ' --c99 --gnu'
43+
44+
group = DefineGroup('TinyUSB', src, depend = ['PKG_USING_TINYUSB'], CPPPATH = path, LOCAL_CFLAGS = LOCAL_CFLAGS)
45+
46+
Return('group')
File renamed without changes.

src/osal/rt-thread/tinyusb_port.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 Ha Thach (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+
* This file is part of the TinyUSB stack.
25+
*/
26+
#ifdef __RTTHREAD__
27+
#include <rtthread.h>
28+
29+
#define DBG_TAG "TinyUSB"
30+
#define DBG_LVL DBG_INFO
31+
#include <rtdbg.h>
32+
#include <tusb.h>
33+
34+
#ifndef RT_USING_HEAP
35+
/* if there is not enable heap, we should use static thread and stack. */
36+
static rt_uint8_t tusb_stack[PKG_TINYUSB_STACK_SIZE];
37+
static struct rt_thread tusb_thread;
38+
#endif /* RT_USING_HEAP */
39+
40+
extern int tusb_board_init(void);
41+
42+
static void tusb_thread_entry(void *parameter)
43+
{
44+
(void) parameter;
45+
while (1)
46+
{
47+
tud_task();
48+
}
49+
}
50+
51+
static int init_tinyusb(void)
52+
{
53+
rt_thread_t tid;
54+
55+
tusb_board_init();
56+
tusb_init();
57+
58+
#ifdef RT_USING_HEAP
59+
tid = rt_thread_create("tusb", tusb_thread_entry, RT_NULL,
60+
PKG_TINYUSB_STACK_SIZE,
61+
PKG_TINYUSB_THREAD_PRIORITY, 10);
62+
if (tid == RT_NULL)
63+
#else
64+
rt_err_t result;
65+
66+
tid = &tusb_thread;
67+
result = rt_thread_init(tid, "tusb", tusb_thread_entry, RT_NULL,
68+
tusb_stack, sizeof(tusb_stack), 4, 10);
69+
if (result != RT_EOK)
70+
#endif /* RT_USING_HEAP */
71+
{
72+
LOG_E("Fail to create TinyUSB thread");
73+
return -1;
74+
}
75+
76+
rt_thread_startup(tid);
77+
78+
return 0;
79+
}
80+
INIT_APP_EXPORT(init_tinyusb);
81+
#endif /*__RTTHREAD__*/

src/osal/rt-thread/tusb_config.h

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 Ha Thach (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+
* This file is part of the TinyUSB stack.
25+
*/
26+
27+
#ifndef _TUSB_CONFIG_H_
28+
#define _TUSB_CONFIG_H_
29+
30+
#ifdef __RTTHREAD__
31+
#include <rtconfig.h>
32+
33+
#ifdef __cplusplus
34+
extern "C" {
35+
#endif
36+
37+
//--------------------------------------------------------------------
38+
// COMMON CONFIGURATION
39+
//--------------------------------------------------------------------
40+
41+
#if defined(SOC_SERIES_STM32F0)
42+
#define CFG_TUSB_MCU OPT_MCU_STM32F0
43+
#elif defined(SOC_SERIES_STM32F1)
44+
#define CFG_TUSB_MCU OPT_MCU_STM32F1
45+
#elif defined(SOC_SERIES_STM32F2)
46+
#define CFG_TUSB_MCU OPT_MCU_STM32F2
47+
#elif defined(SOC_SERIES_STM32F3)
48+
#define CFG_TUSB_MCU OPT_MCU_STM32F3
49+
#elif defined(SOC_SERIES_STM32F4)
50+
#define CFG_TUSB_MCU OPT_MCU_STM32F4
51+
#elif defined(SOC_SERIES_STM32F7)
52+
#define CFG_TUSB_MCU OPT_MCU_STM32F7
53+
#elif defined(SOC_SERIES_STM32H7)
54+
#define CFG_TUSB_MCU OPT_MCU_STM32H7
55+
#elif defined(SOC_SERIES_STM32L0)
56+
#define CFG_TUSB_MCU OPT_MCU_STM32L0
57+
#elif defined(SOC_SERIES_STM32L1)
58+
#define CFG_TUSB_MCU OPT_MCU_STM32L1
59+
#elif defined(SOC_SERIES_STM32L4)
60+
#define CFG_TUSB_MCU OPT_MCU_STM32L4
61+
#elif defined(SOC_NRF52840)
62+
#define CFG_TUSB_MCU OPT_MCU_NRF5X
63+
#elif defined(SOC_HPM6000)
64+
#define CFG_TUSB_MCU OPT_MCU_HPM
65+
#elif defined(SOC_RP2040)
66+
#define CFG_TUSB_MCU OPT_MCU_RP2040
67+
#elif defined(SOC_FAMILY_RENESAS)
68+
#define CFG_TUSB_MCU OPT_MCU_RAXXX
69+
#else
70+
#error "Not support for current MCU"
71+
#endif
72+
73+
#define CFG_TUSB_OS OPT_OS_RTTHREAD
74+
75+
//--------------------------------------------------------------------
76+
// DEBUG CONFIGURATION
77+
//--------------------------------------------------------------------
78+
#ifdef CFG_TUSB_DEBUG
79+
#define CFG_TUSB_DEBUG_PRINTF rt_kprintf
80+
#endif /* CFG_TUSB_DEBUG */
81+
82+
#ifndef BOARD_DEVICE_RHPORT_NUM
83+
#define BOARD_DEVICE_RHPORT_NUM PKG_TINYUSB_RHPORT_NUM
84+
#endif
85+
86+
#ifndef BOARD_DEVICE_RHPORT_SPEED
87+
#define BOARD_DEVICE_RHPORT_SPEED PKG_TINYUSB_DEVICE_PORT_SPEED
88+
#endif
89+
90+
#if BOARD_DEVICE_RHPORT_NUM == 0
91+
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED)
92+
#elif BOARD_DEVICE_RHPORT_NUM == 1
93+
#define CFG_TUSB_RHPORT1_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED)
94+
#else
95+
#error "Incorrect RHPort configuration"
96+
#endif
97+
98+
/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
99+
* Tinyusb use follows macros to declare transferring memory so that they can be put
100+
* into those specific section.
101+
* e.g
102+
* - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") ))
103+
* - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4)))
104+
*/
105+
#ifndef CFG_TUSB_MEM_SECTION
106+
#define CFG_TUSB_MEM_SECTION rt_section(PKG_TINYUSB_MEM_SECTION)
107+
#endif
108+
109+
#ifndef CFG_TUSB_MEM_ALIGN
110+
#define CFG_TUSB_MEM_ALIGN rt_align(PKG_TINYUSB_MEM_ALIGN)
111+
#endif
112+
113+
//--------------------------------------------------------------------
114+
// DEVICE CONFIGURATION
115+
//--------------------------------------------------------------------
116+
117+
#ifndef CFG_TUD_ENDPOINT0_SIZE
118+
#define CFG_TUD_ENDPOINT0_SIZE PKG_TINYUSB_EDPT0_SIZE
119+
#endif
120+
121+
// CDC FIFO size of TX and RX
122+
#define CFG_TUD_CDC_RX_BUFSIZE PKG_TINYUSB_DEVICE_CDC_RX_BUFSIZE
123+
#define CFG_TUD_CDC_TX_BUFSIZE PKG_TINYUSB_DEVICE_CDC_TX_BUFSIZE
124+
125+
#define CFG_TUD_MSC_EP_BUFSIZE PKG_TINYUSB_DEVICE_MSC_EP_BUFSIZE
126+
127+
#define CFG_TUD_HID_EP_BUFSIZE PKG_TINYUSB_DEVICE_HID_EP_BUFSIZE
128+
129+
#ifndef PKG_TINYUSB_DEVICE_CDC_STRING
130+
#define PKG_TINYUSB_DEVICE_CDC_STRING ""
131+
#endif
132+
133+
#ifndef PKG_TINYUSB_DEVICE_MSC_STRING
134+
#define PKG_TINYUSB_DEVICE_MSC_STRING ""
135+
#endif
136+
137+
#ifndef PKG_TINYUSB_DEVICE_HID_STRING
138+
#define PKG_TINYUSB_DEVICE_HID_STRING ""
139+
#endif
140+
141+
142+
#ifdef __cplusplus
143+
}
144+
#endif
145+
#endif /*__RTTHREAD__*/
146+
147+
#endif /* _TUSB_CONFIG_H_ */

0 commit comments

Comments
 (0)