Skip to content

Commit bb16d74

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

File tree

9 files changed

+715
-1
lines changed

9 files changed

+715
-1
lines changed

SConscript

+11
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

+1-1
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

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
LOCAL_CFLAGS = ''
35+
36+
if rtconfig.PLATFORM == 'gcc' or rtconfig.PLATFORM == 'armclang': # GCC or Keil AC6
37+
LOCAL_CFLAGS += ' -std=c99'
38+
elif rtconfig.PLATFORM == 'armcc': # Keil AC5
39+
LOCAL_CFLAGS += ' --c99 --gnu'
40+
41+
group = DefineGroup('TinyUSB', src, depend = ['PKG_USING_TINYUSB'], CPPPATH = path, LOCAL_CFLAGS = LOCAL_CFLAGS)
42+
43+
Return('group')
File renamed without changes.
+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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 <tusb.h>
28+
#include <stdint.h>
29+
30+
#include <board.h>
31+
#include <rtdevice.h>
32+
33+
static bool ejected = false;
34+
static rt_device_t flash_device;
35+
static struct rt_device_blk_geometry blk_geom;
36+
37+
#ifdef __CC_ARM
38+
uint16_t __builtin_bswap16(uint16_t x)
39+
{
40+
return (x << 8) | (x >> 8);
41+
}
42+
#endif
43+
44+
void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4])
45+
{
46+
(void) lun;
47+
48+
const char vid[] = PKG_TINYUSB_DEVICE_MSC_VID;
49+
const char pid[] = PKG_TINYUSB_DEVICE_MSC_PID;
50+
const char rev[] = PKG_TINYUSB_DEVICE_MSC_REV;
51+
52+
memcpy(vendor_id, vid, strlen(vid));
53+
memcpy(product_id, pid, strlen(pid));
54+
memcpy(product_rev, rev, strlen(rev));
55+
}
56+
57+
bool tud_msc_test_unit_ready_cb(uint8_t lun)
58+
{
59+
(void) lun;
60+
61+
if (ejected)
62+
{
63+
tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3a, 0x00);
64+
return false;
65+
}
66+
67+
if (flash_device == NULL)
68+
{
69+
flash_device = rt_device_find(PKG_TINYUSB_DEVICE_MSC_NAME);
70+
}
71+
if (flash_device != NULL)
72+
{
73+
rt_device_open(flash_device, 0);
74+
rt_device_control(flash_device, RT_DEVICE_CTRL_BLK_GETGEOME, &blk_geom);
75+
return true;
76+
}
77+
78+
return false;
79+
}
80+
81+
void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count, uint16_t *block_size)
82+
{
83+
(void) lun;
84+
85+
*block_count = blk_geom.sector_count;
86+
*block_size = blk_geom.bytes_per_sector;
87+
}
88+
89+
bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject)
90+
{
91+
(void) lun;
92+
(void) power_condition;
93+
94+
if (load_eject)
95+
{
96+
if (start)
97+
{
98+
ejected = false;
99+
} else {
100+
// unload disk storage
101+
ejected = true;
102+
}
103+
}
104+
105+
return true;
106+
}
107+
108+
int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize)
109+
{
110+
(void) lun;
111+
(void) offset;
112+
(void) bufsize;
113+
114+
return (int32_t) rt_device_read(flash_device, (rt_off_t) lba, buffer, 1) * blk_geom.bytes_per_sector;
115+
}
116+
117+
int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize)
118+
{
119+
(void) lun;
120+
(void) offset;
121+
(void) bufsize;
122+
123+
return (int32_t) rt_device_write(flash_device, (rt_off_t) lba, buffer, 1) * blk_geom.bytes_per_sector;
124+
}
125+
126+
int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, uint16_t bufsize)
127+
{
128+
void const *response = NULL;
129+
uint16_t resplen = 0;
130+
131+
// most scsi handled is input
132+
bool in_xfer = true;
133+
134+
switch (scsi_cmd[0])
135+
{
136+
case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
137+
// Host is about to read/write etc ... better not to disconnect disk
138+
resplen = 0;
139+
break;
140+
141+
default:
142+
// Set Sense = Invalid Command Operation
143+
tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
144+
145+
// negative means error -> tinyusb could stall and/or response with failed status
146+
resplen = -1;
147+
break;
148+
}
149+
150+
// return resplen must not larger than bufsize
151+
if (resplen > bufsize) resplen = bufsize;
152+
153+
if (response && (resplen > 0))
154+
{
155+
if (in_xfer)
156+
{
157+
memcpy(buffer, response, resplen);
158+
} else {
159+
// SCSI output
160+
}
161+
}
162+
163+
return resplen;
164+
}
165+
#endif /*__RTTHREAD__*/

src/osal/rt-thread/tinyusb_port.c

+81
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__*/

0 commit comments

Comments
 (0)