Skip to content

Commit d89a060

Browse files
iabdalkaderdpgeorge
authored andcommitted
renesas-ra: Add TinyUSB support.
Signed-off-by: iabdalkader <[email protected]>
1 parent 816b88a commit d89a060

File tree

7 files changed

+357
-18
lines changed

7 files changed

+357
-18
lines changed

ports/renesas-ra/Makefile

+28-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ FROZEN_MANIFEST ?= boards/manifest.py
4444
include $(TOP)/py/py.mk
4545
include $(TOP)/extmod/extmod.mk
4646

47-
GIT_SUBMODULES += lib/fsp
47+
GIT_SUBMODULES += lib/fsp lib/tinyusb
4848

4949
MCU_SERIES_UPPER = $(shell echo $(MCU_SERIES) | tr '[:lower:]' '[:upper:]')
5050
CMSIS_MCU_LOWER = $(shell echo $(CMSIS_MCU) | tr '[:upper:]' '[:lower:]')
@@ -69,6 +69,9 @@ INC += -I$(TOP)/$(HAL_DIR)/ra/fsp/inc
6969
INC += -I$(TOP)/$(HAL_DIR)/ra/fsp/inc/api
7070
INC += -I$(TOP)/$(HAL_DIR)/ra/fsp/inc/instances
7171
INC += -I$(TOP)/$(HAL_DIR)/ra/fsp/src/bsp/cmsis/Device/RENESAS/Include
72+
INC += -I$(TOP)/lib/tinyusb/hw
73+
INC += -I$(TOP)/lib/tinyusb/src
74+
INC += -I$(TOP)/shared/tinyusb
7275
#INC += -Ilwip_inc
7376
ifeq ($(CMSIS_MCU),$(filter $(CMSIS_MCU),RA4M1 RA4W1 RA6M1 RA6M2 RA6M5))
7477
INC += -Ira
@@ -82,6 +85,7 @@ INC += -Idebug
8285
CFLAGS += -D$(CMSIS_MCU)
8386
CFLAGS += -DRA_HAL_H='<$(CMSIS_MCU)_hal.h>'
8487
CFLAGS += -DRA_CFG_H='<$(FSP_BOARD_NAME)_conf.h>'
88+
CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_RAXXX
8589

8690
# Basic Cortex-M flags
8791
CFLAGS_CORTEX_M = -mthumb
@@ -171,6 +175,27 @@ SHARED_SRC_C += $(addprefix shared/,\
171175
runtime/stdout_helpers.c \
172176
runtime/sys_stdio_mphal.c \
173177
timeutils/timeutils.c \
178+
tinyusb/mp_cdc_common.c \
179+
tinyusb/mp_usbd.c \
180+
tinyusb/mp_usbd_descriptor.c \
181+
)
182+
183+
# TinyUSB Stack source
184+
TINYUSB_SRC_C += $(addprefix lib/tinyusb/,\
185+
src/class/cdc/cdc_device.c \
186+
src/class/dfu/dfu_rt_device.c \
187+
src/class/hid/hid_device.c \
188+
src/class/midi/midi_device.c \
189+
src/class/msc/msc_device.c \
190+
src/class/usbtmc/usbtmc_device.c \
191+
src/class/vendor/vendor_device.c \
192+
src/common/tusb_fifo.c \
193+
src/device/usbd.c \
194+
src/device/usbd_control.c \
195+
src/portable/renesas/rusb2/dcd_rusb2.c \
196+
src/portable/renesas/rusb2/hcd_rusb2.c \
197+
src/portable/renesas/rusb2/rusb2_common.c \
198+
src/tusb.c \
174199
)
175200

176201
ifeq ($(MICROPY_FLOAT_IMPL),double)
@@ -307,6 +332,7 @@ SRC_C += \
307332
flashbdev.c \
308333
storage.c \
309334
fatfs_port.c \
335+
usbd.c \
310336
$(wildcard $(BOARD_DIR)/*.c)
311337

312338
SRC_C += $(addprefix $(BOARD_DIR)/ra_gen/,\
@@ -401,6 +427,7 @@ OBJ += $(LIBM_O)
401427
OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o))
402428
OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o))
403429
OBJ += $(addprefix $(BUILD)/, $(HAL_SRC_C:.c=.o))
430+
OBJ += $(addprefix $(BUILD)/, $(TINYUSB_SRC_C:.c=.o))
404431
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
405432
OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o))
406433
OBJ += $(addprefix $(BUILD)/, $(SRC_O))

ports/renesas-ra/main.c

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#include "usrsw.h"
6363
#include "rtc.h"
6464
#include "storage.h"
65+
#include "tusb.h"
6566

6667
#define RA_EARLY_PRINT 1 /* for enabling mp_print in boardctrl. */
6768

@@ -259,6 +260,10 @@ int main(void) {
259260
state.reset_mode = 1;
260261
state.log_soft_reset = false;
261262

263+
#if MICROPY_HW_ENABLE_USBDEV
264+
tusb_init();
265+
#endif
266+
262267
MICROPY_BOARD_BEFORE_SOFT_RESET_LOOP(&state);
263268

264269
soft_reset:

ports/renesas-ra/mpconfigport.h

+52
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@
3737
#define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_EXTRA_FEATURES)
3838
#endif
3939

40+
#ifndef MICROPY_HW_ENABLE_USBDEV
41+
#define MICROPY_HW_ENABLE_USBDEV (0)
42+
#endif
43+
#ifndef MICROPY_HW_ENABLE_UART_REPL
44+
#define MICROPY_HW_ENABLE_UART_REPL (1) // useful if there is no USB
45+
#endif
46+
47+
#if MICROPY_HW_ENABLE_USBDEV
48+
// Enable USB-CDC serial port
49+
#ifndef MICROPY_HW_USB_CDC
50+
#define MICROPY_HW_USB_CDC (1)
51+
#endif
52+
// Enable USB Mass Storage with FatFS filesystem.
53+
#ifndef MICROPY_HW_USB_MSC
54+
#define MICROPY_HW_USB_MSC (0)
55+
#endif
56+
// RA unique ID is 16 bytes (hex string == 32)
57+
#ifndef MICROPY_HW_USB_DESC_STR_MAX
58+
#define MICROPY_HW_USB_DESC_STR_MAX (32)
59+
#endif
60+
#endif
61+
4062
// memory allocation policies
4163
#ifndef MICROPY_GC_STACK_ENTRY_TYPE
4264
#if MICROPY_HW_SDRAM_SIZE
@@ -139,6 +161,11 @@
139161
#define MICROPY_FATFS_USE_LABEL (1)
140162
#define MICROPY_FATFS_RPATH (2)
141163
#define MICROPY_FATFS_MULTI_PARTITION (1)
164+
#if MICROPY_HW_USB_MSC
165+
// Set FatFS block size to flash sector size to avoid caching
166+
// the flash sector in memory to support smaller block sizes.
167+
#define MICROPY_FATFS_MAX_SS (FLASH_SECTOR_SIZE)
168+
#endif
142169

143170
#if MICROPY_PY_MACHINE
144171
#define MACHINE_BUILTIN_MODULE_CONSTANTS \
@@ -153,6 +180,15 @@
153180

154181
#define MP_STATE_PORT MP_STATE_VM
155182

183+
// Miscellaneous settings
184+
185+
#ifndef MICROPY_HW_USB_VID
186+
#define MICROPY_HW_USB_VID (0xf055)
187+
#endif
188+
#ifndef MICROPY_HW_USB_PID
189+
#define MICROPY_HW_USB_PID (0x9800)
190+
#endif
191+
156192
// type definitions for the specific machine
157193

158194
#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((uint32_t)(p) | 1))
@@ -190,10 +226,25 @@ static inline mp_uint_t disable_irq(void) {
190226
#define MICROPY_BEGIN_ATOMIC_SECTION() disable_irq()
191227
#define MICROPY_END_ATOMIC_SECTION(state) enable_irq(state)
192228

229+
#if MICROPY_HW_ENABLE_USBDEV
230+
#define MICROPY_HW_USBDEV_TASK_HOOK extern void usbd_task(void); usbd_task();
231+
#define MICROPY_VM_HOOK_COUNT (10)
232+
#define MICROPY_VM_HOOK_INIT static uint vm_hook_divisor = MICROPY_VM_HOOK_COUNT;
233+
#define MICROPY_VM_HOOK_POLL if (--vm_hook_divisor == 0) { \
234+
vm_hook_divisor = MICROPY_VM_HOOK_COUNT; \
235+
MICROPY_HW_USBDEV_TASK_HOOK \
236+
}
237+
#define MICROPY_VM_HOOK_LOOP MICROPY_VM_HOOK_POLL
238+
#define MICROPY_VM_HOOK_RETURN MICROPY_VM_HOOK_POLL
239+
#else
240+
#define MICROPY_HW_USBDEV_TASK_HOOK
241+
#endif
242+
193243
#if MICROPY_PY_THREAD
194244
#define MICROPY_EVENT_POLL_HOOK \
195245
do { \
196246
extern void mp_handle_pending(bool); \
247+
MICROPY_HW_USBDEV_TASK_HOOK \
197248
mp_handle_pending(true); \
198249
if (pyb_thread_enabled) { \
199250
MP_THREAD_GIL_EXIT(); \
@@ -209,6 +260,7 @@ static inline mp_uint_t disable_irq(void) {
209260
#define MICROPY_EVENT_POLL_HOOK \
210261
do { \
211262
extern void mp_handle_pending(bool); \
263+
MICROPY_HW_USBDEV_TASK_HOOK \
212264
mp_handle_pending(true); \
213265
__WFI(); \
214266
} while (0);

0 commit comments

Comments
 (0)