Skip to content

Commit 9d91111

Browse files
committed
Move atmel-samd to tinyusb and support nRF flash.
This started while adding USB MIDI support (and descriptor support is in this change.) When seeing that I'd have to implement the MIDI class logic twice, once for atmel-samd and once for nrf, I decided to refactor the USB stack so its shared across ports. This has led to a number of changes that remove items from the ports folder and move them into supervisor. Furthermore, we had external SPI flash support for nrf pending so I factored out the connection between the usb stack and the flash API as well. This PR also includes the QSPI support for nRF.
1 parent d08747d commit 9d91111

File tree

154 files changed

+2193
-3457
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

154 files changed

+2193
-3457
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
url = https://github.com/adafruit/nrfx.git
7979
[submodule "lib/tinyusb"]
8080
path = lib/tinyusb
81-
url = https://github.com/hathach/tinyusb.git
81+
url = https://github.com/tannewt/tinyusb.git
8282
branch = develop
8383
[submodule "tools/huffman"]
8484
path = tools/huffman

lib/tinyusb

Submodule tinyusb updated 76 files

main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "supervisor/shared/autoreload.h"
5252
#include "supervisor/shared/translate.h"
5353
#include "supervisor/shared/rgb_led_status.h"
54+
#include "supervisor/shared/status_leds.h"
5455
#include "supervisor/shared/stack.h"
5556
#include "supervisor/serial.h"
5657

@@ -388,6 +389,8 @@ int __attribute__((used)) main(void) {
388389
// initialise the cpu and peripherals
389390
safe_mode_t safe_mode = port_init();
390391

392+
// Turn on LEDs
393+
init_status_leds();
391394
rgb_led_status_init();
392395

393396
stack_init();

ports/atmel-samd/Makefile

Lines changed: 19 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,13 @@ INC += -I. \
4545
-Iasf4/$(CHIP_FAMILY)/hpl/tc \
4646
-Iasf4/$(CHIP_FAMILY)/include \
4747
-Iasf4/$(CHIP_FAMILY)/CMSIS/Include \
48-
-Iasf4/$(CHIP_FAMILY)/usb \
49-
-Iasf4/$(CHIP_FAMILY)/usb/class/cdc \
50-
-Iasf4/$(CHIP_FAMILY)/usb/class/hid \
51-
-Iasf4/$(CHIP_FAMILY)/usb/class/msc \
52-
-Iasf4/$(CHIP_FAMILY)/usb/device \
5348
-Iasf4_conf/$(CHIP_FAMILY) \
5449
-Iboards/$(BOARD) \
5550
-Iboards/ \
5651
-Iperipherals/ \
5752
-Ifreetouch \
53+
-I../../lib/tinyusb/src \
54+
-I../../supervisor/shared/usb \
5855
-I$(BUILD)
5956

6057
BASE_CFLAGS = \
@@ -90,11 +87,15 @@ BASE_CFLAGS = \
9087
# NDEBUG disables assert() statements. This reduces code size pretty dramatically, per tannewt.
9188

9289
ifeq ($(CHIP_FAMILY), samd21)
93-
CFLAGS = -Os -DNDEBUG
90+
CFLAGS += -Os -DNDEBUG
91+
# TinyUSB defines
92+
CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_SAMD21 -DCFG_TUD_CDC_RX_BUFSIZE=128 -DCFG_TUD_CDC_TX_BUFSIZE=128 -DCFG_TUD_MSC_BUFSIZE=512
9493
endif
9594

9695
ifeq ($(CHIP_FAMILY), samd51)
97-
CFLAGS = -Os -DNDEBUG
96+
CFLAGS += -O0 -DNDEBUG
97+
# TinyUSB defines
98+
CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_SAMD51 -DCFG_TUD_CDC_RX_BUFSIZE=256 -DCFG_TUD_CDC_TX_BUFSIZE=256 -DCFG_TUD_MSC_BUFSIZE=1024
9899
endif
99100

100101
#Debugging/Optimization
@@ -104,7 +105,7 @@ ifeq ($(DEBUG), 1)
104105
# You may want to disable -flto if it interferes with debugging.
105106
CFLAGS += -flto
106107
# You may want to enable these flags to make setting breakpoints easier.
107-
## CFLAGS += -fno-inline -fno-ipa-sra
108+
# CFLAGS += -fno-inline -fno-ipa-sra
108109
ifeq ($(CHIP_FAMILY), samd21)
109110
CFLAGS += -DENABLE_MICRO_TRACE_BUFFER
110111
endif
@@ -204,7 +205,6 @@ SRC_ASF := \
204205
hal/src/hal_spi_m_sync.c \
205206
hal/src/hal_timer.c \
206207
hal/src/hal_usart_async.c \
207-
hal/src/hal_usb_device.c \
208208
hpl/adc/hpl_adc.c \
209209
hpl/core/hpl_init.c \
210210
hpl/dac/hpl_dac.c \
@@ -214,12 +214,6 @@ SRC_ASF := \
214214
hpl/rtc/hpl_rtc.c \
215215
hpl/sercom/hpl_sercom.c \
216216
hpl/systick/hpl_systick.c \
217-
hpl/usb/hpl_usb.c \
218-
usb/class/cdc/device/cdcdf_acm.c \
219-
usb/class/hid/device/hiddf_generic.c \
220-
usb/class/msc/device/mscdf.c \
221-
usb/device/usbdc.c \
222-
usb/usb_protocol.c \
223217
hal/utils/src/utils_list.c \
224218
hal/utils/src/utils_ringbuffer.c \
225219

@@ -246,7 +240,6 @@ SRC_C = \
246240
board_busses.c \
247241
background.c \
248242
fatfs_port.c \
249-
flash_api.c \
250243
mphalport.c \
251244
reset.c \
252245
peripherals/samd/clocks.c \
@@ -265,15 +258,15 @@ SRC_C = \
265258
peripherals/samd/$(CHIP_FAMILY)/sercom.c \
266259
peripherals/samd/$(CHIP_FAMILY)/timers.c \
267260
tick.c \
268-
usb.c \
269-
usb_mass_storage.c \
270261
bindings/samd/__init__.c \
271262
bindings/samd/Clock.c \
272263
boards/$(BOARD)/board.c \
273264
boards/$(BOARD)/pins.c \
274265
lib/oofatfs/ff.c \
275266
lib/oofatfs/option/ccsbcs.c \
276267
lib/timeutils/timeutils.c \
268+
lib/tinyusb/src/portable/microchip/$(CHIP_FAMILY)/dcd.c \
269+
lib/tinyusb/src/portable/microchip/$(CHIP_FAMILY)/hal.c \
277270
lib/utils/buffer_helper.c \
278271
lib/utils/context_manager_helpers.c \
279272
lib/utils/interrupt_char.c \
@@ -282,7 +275,6 @@ SRC_C = \
282275
lib/utils/sys_stdio_mphal.c \
283276
lib/libc/string0.c \
284277
lib/mp-readline/readline.c \
285-
$(BUILD)/autogen_usb_descriptor.c \
286278
freetouch/adafruit_ptc.c \
287279
supervisor/shared/memory.c
288280

@@ -306,19 +298,6 @@ SRC_MOD += $(addprefix $(WIZNET5K_DIR)/,\
306298
endif # MICROPY_PY_WIZNET5K
307299
endif # MICROPY_PY_NETWORK
308300

309-
# Choose which flash filesystem impl to use.
310-
# (Right now INTERNAL_FLASH_FILESYSTEM and SPI_FLASH_FILESYSTEM are mutually exclusive.
311-
# But that might not be true in the future.)
312-
ifeq ($(INTERNAL_FLASH_FILESYSTEM),1)
313-
SRC_C += internal_flash.c
314-
endif
315-
ifeq ($(SPI_FLASH_FILESYSTEM),1)
316-
SRC_C += external_flash/external_flash.c external_flash/spi_flash.c
317-
endif
318-
ifeq ($(QSPI_FLASH_FILESYSTEM),1)
319-
SRC_C += external_flash/external_flash.c external_flash/qspi_flash.c
320-
endif
321-
322301
SRC_COMMON_HAL = \
323302
board/__init__.c \
324303
busio/__init__.c \
@@ -339,7 +318,6 @@ SRC_COMMON_HAL = \
339318
rotaryio/IncrementalEncoder.c \
340319
rtc/__init__.c \
341320
rtc/RTC.c \
342-
storage/__init__.c \
343321
supervisor/__init__.c \
344322
supervisor/Runtime.c \
345323
time/__init__.c \
@@ -352,10 +330,8 @@ SRC_COMMON_HAL = \
352330
pulseio/PulseIn.c \
353331
pulseio/PulseOut.c \
354332
pulseio/PWMOut.c \
355-
usb_hid/__init__.c \
356-
usb_hid/Device.c \
357333
touchio/__init__.c \
358-
touchio/TouchIn.c \
334+
touchio/TouchIn.c
359335

360336
ifeq ($(INTERNAL_LIBM),1)
361337
SRC_LIBM = $(addprefix lib/,\
@@ -412,12 +388,17 @@ SRC_SHARED_MODULE = \
412388
_stage/__init__.c \
413389
_stage/Layer.c \
414390
_stage/Text.c \
391+
storage/__init__.c \
415392
os/__init__.c \
416393
random/__init__.c \
417-
storage/__init__.c \
418394
struct/__init__.c \
419395
uheap/__init__.c \
420-
ustack/__init__.c
396+
ustack/__init__.c \
397+
usb_hid/__init__.c \
398+
usb_hid/Device.c
399+
400+
# usb_midi/__init__.c
401+
# usb_midi/Port.c
421402

422403
ifeq ($(MICROPY_PY_NETWORK),1)
423404
SRC_SHARED_MODULE += socket/__init__.c network/__init__.c
@@ -489,21 +470,6 @@ $(BUILD)/firmware.uf2: $(BUILD)/firmware.bin
489470
$(STEPECHO) "Create $@"
490471
$(Q)$(PYTHON3) $(TOP)/tools/uf2/utils/uf2conv.py -b $(BOOTLOADER_SIZE) -c -o $@ $^
491472

492-
$(BUILD)/autogen_usb_descriptor.c $(BUILD)/genhdr/autogen_usb_descriptor.h: autogen_usb_descriptor.intermediate
493-
494-
.INTERMEDIATE: autogen_usb_descriptor.intermediate
495-
496-
autogen_usb_descriptor.intermediate: tools/gen_usb_descriptor.py Makefile | $(HEADER_BUILD)
497-
$(STEPECHO) "GEN $@"
498-
$(Q)install -d $(BUILD)/genhdr
499-
$(Q)$(PYTHON3) tools/gen_usb_descriptor.py \
500-
--manufacturer $(USB_MANUFACTURER)\
501-
--product $(USB_PRODUCT)\
502-
--vid $(USB_VID)\
503-
--pid $(USB_PID)\
504-
--output_c_file $(BUILD)/autogen_usb_descriptor.c\
505-
--output_h_file $(BUILD)/genhdr/autogen_usb_descriptor.h
506-
507473
deploy: $(BUILD)/firmware.bin
508474
$(ECHO) "Writing $< to the board"
509475
$(BOSSAC) -u $<

ports/atmel-samd/background.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727

2828
#include "audio_dma.h"
2929
#include "tick.h"
30-
#include "usb.h"
31-
#include "usb_mass_storage.h"
30+
#include "supervisor/usb.h"
3231

3332
#include "shared-module/displayio/__init__.h"
3433
#include "shared-module/network/__init__.h"
@@ -45,8 +44,8 @@ void run_background_tasks(void) {
4544
#if MICROPY_PY_NETWORK
4645
network_module_background();
4746
#endif
48-
usb_msc_background();
49-
usb_cdc_background();
47+
usb_background();
48+
5049
last_finished_tick = ticks_ms;
5150
}
5251

ports/atmel-samd/boards/arduino_zero/board.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@
3030

3131
void board_init(void)
3232
{
33-
gpio_set_pin_function(MICROPY_HW_LED_TX, GPIO_PIN_FUNCTION_OFF);
34-
gpio_set_pin_direction(MICROPY_HW_LED_TX, GPIO_DIRECTION_OUT);
35-
gpio_set_pin_level(MICROPY_HW_LED_TX, true);
36-
37-
gpio_set_pin_function(MICROPY_HW_LED_RX, GPIO_PIN_FUNCTION_OFF);
38-
gpio_set_pin_direction(MICROPY_HW_LED_RX, GPIO_DIRECTION_OUT);
39-
gpio_set_pin_level(MICROPY_HW_LED_RX, true);
4033
}
4134

4235
bool board_requests_safe_mode(void) {

ports/atmel-samd/boards/arduino_zero/mpconfigboard.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
#define MICROPY_HW_BOARD_NAME "Arduino Zero"
22
#define MICROPY_HW_MCU_NAME "samd21g18"
33

4-
// #define MICROPY_HW_LED_MSC PIN_PA17 // red
5-
#define MICROPY_HW_LED_TX PIN_PA27
6-
#define MICROPY_HW_LED_RX PIN_PB03
4+
// #define MICROPY_HW_LED_MSC &pin_PA17 // red
5+
#define MICROPY_HW_LED_TX &pin_PA27
6+
#define MICROPY_HW_LED_RX &pin_PB03
77

88
#define MICROPY_PORT_A (PORT_PA24 | PORT_PA25 | PORT_PA27)
99
#define MICROPY_PORT_B (PORT_PB03)
1010
#define MICROPY_PORT_C (0)
1111

12-
#include "internal_flash.h"
13-
1412
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
1513

1614
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000)

ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,25 @@
88
#define SPI_FLASH_BAUDRATE (8000000)
99

1010
// On-board flash
11-
#define SPI_FLASH_MOSI_PIN PIN_PA20
12-
#define SPI_FLASH_MISO_PIN PIN_PA16
13-
#define SPI_FLASH_SCK_PIN PIN_PA21
14-
#define SPI_FLASH_CS_PIN PIN_PB22
15-
16-
#define SPI_FLASH_MOSI_PIN_FUNCTION PINMUX_PA20D_SERCOM3_PAD2
17-
#define SPI_FLASH_MISO_PIN_FUNCTION PINMUX_PA16D_SERCOM3_PAD0
18-
#define SPI_FLASH_SCK_PIN_FUNCTION PINMUX_PA21D_SERCOM3_PAD3
19-
#define SPI_FLASH_SERCOM SERCOM3
20-
#define SPI_FLASH_SERCOM_INDEX 3
21-
#define SPI_FLASH_MOSI_PAD 2
22-
#define SPI_FLASH_MISO_PAD 0
23-
#define SPI_FLASH_SCK_PAD 3
24-
25-
// <o> Transmit Data Pinout
26-
// <0x0=>PAD[0,1]_DO_SCK
27-
// <0x1=>PAD[2,3]_DO_SCK
28-
// <0x2=>PAD[3,1]_DO_SCK
29-
// <0x3=>PAD[0,3]_DO_SCK
30-
#define SPI_FLASH_DOPO 1
31-
#define SPI_FLASH_DIPO 0 // same as MISO PAD
11+
#define SPI_FLASH_MOSI_PIN &pin_PA20
12+
#define SPI_FLASH_MISO_PIN &pin_PA16
13+
#define SPI_FLASH_SCK_PIN &pin_PA21
14+
#define SPI_FLASH_CS_PIN &pin_PB22
3215

3316
// These are pins not to reset.
3417
// PA24 and PA25 are USB.
35-
#define MICROPY_PORT_A (PORT_PA16 | PORT_PA20 | PORT_PA21 | PORT_PA24 | PORT_PA25)
36-
#define MICROPY_PORT_B (PORT_PB22)
18+
#define MICROPY_PORT_A (0)
19+
#define MICROPY_PORT_B (0)
3720
#define MICROPY_PORT_C (0)
3821

3922
#define SPEAKER_ENABLE_PIN (&pin_PA30)
4023

41-
#include "external_flash/devices.h"
42-
4324
// If you change this, then make sure to update the linker scripts as well to
4425
// make sure you don't overwrite code.
4526
#define CIRCUITPY_INTERNAL_NVM_SIZE 256
4627

4728
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
4829

49-
#include "external_flash/devices.h"
50-
51-
#define EXTERNAL_FLASH_DEVICE_COUNT 2
52-
#define EXTERNAL_FLASH_DEVICES S25FL216K, \
53-
GD25Q16C
54-
55-
#include "external_flash/external_flash.h"
56-
5730
#define CALIBRATE_CRYSTALLESS 1
5831

5932
// Explanation of how a user got into safe mode.

ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ USB_PRODUCT = "CircuitPlayground Express"
55
USB_MANUFACTURER = "Adafruit Industries LLC"
66

77
SPI_FLASH_FILESYSTEM = 1
8+
EXTERNAL_FLASH_DEVICE_COUNT = 2
9+
EXTERNAL_FLASH_DEVICES = "S25FL216K, GD25Q16C"
810
LONGINT_IMPL = MPZ
911

1012
CHIP_VARIANT = SAMD21G18A

ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.h

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,24 @@
1111
#define SPI_FLASH_BAUDRATE (8000000)
1212

1313
// On-board flash
14-
#define SPI_FLASH_MOSI_PIN PIN_PA20
15-
#define SPI_FLASH_MISO_PIN PIN_PA16
16-
#define SPI_FLASH_SCK_PIN PIN_PA21
17-
#define SPI_FLASH_CS_PIN PIN_PB22
18-
19-
#define SPI_FLASH_MOSI_PIN_FUNCTION PINMUX_PA20D_SERCOM3_PAD2
20-
#define SPI_FLASH_MISO_PIN_FUNCTION PINMUX_PA16D_SERCOM3_PAD0
21-
#define SPI_FLASH_SCK_PIN_FUNCTION PINMUX_PA21D_SERCOM3_PAD3
22-
#define SPI_FLASH_SERCOM SERCOM3
23-
#define SPI_FLASH_SERCOM_INDEX 3
24-
#define SPI_FLASH_MOSI_PAD 2
25-
#define SPI_FLASH_MISO_PAD 0
26-
#define SPI_FLASH_SCK_PAD 3
27-
28-
// <o> Transmit Data Pinout
29-
// <0x0=>PAD[0,1]_DO_SCK
30-
// <0x1=>PAD[2,3]_DO_SCK
31-
// <0x2=>PAD[3,1]_DO_SCK
32-
// <0x3=>PAD[0,3]_DO_SCK
33-
#define SPI_FLASH_DOPO 1
34-
#define SPI_FLASH_DIPO 0 // same as MISO PAD
14+
#define SPI_FLASH_MOSI_PIN &pin_PA20
15+
#define SPI_FLASH_MISO_PIN &pin_PA16
16+
#define SPI_FLASH_SCK_PIN &pin_PA21
17+
#define SPI_FLASH_CS_PIN &pin_PB22
3518

3619
// These are pins not to reset.
37-
// PA24 and PA25 are USB.
38-
#define MICROPY_PORT_A (PORT_PA16 | PORT_PA20 | PORT_PA21 | PORT_PA24 | PORT_PA25)
39-
#define MICROPY_PORT_B (PORT_PB22)
20+
#define MICROPY_PORT_A (0)
21+
#define MICROPY_PORT_B (0)
4022
#define MICROPY_PORT_C (0)
4123

4224
#define SPEAKER_ENABLE_PIN (&pin_PA30)
4325

44-
#include "external_flash/devices.h"
45-
4626
// If you change this, then make sure to update the linker scripts as well to
4727
// make sure you don't overwrite code.
4828
#define CIRCUITPY_INTERNAL_NVM_SIZE 256
4929

5030
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
5131

52-
#include "external_flash/devices.h"
53-
54-
#define EXTERNAL_FLASH_DEVICE_COUNT 2
55-
#define EXTERNAL_FLASH_DEVICES S25FL216K, \
56-
GD25Q16C
57-
58-
#include "external_flash/external_flash.h"
59-
6032
#define CALIBRATE_CRYSTALLESS 1
6133

6234
// Explanation of how a user got into safe mode.

0 commit comments

Comments
 (0)