Skip to content

Commit b5e40f5

Browse files
committed
Add USB MIDI support for SAMD and nRF.
The API should be identical to using a UART for MIDI. Fixes adafruit#672
1 parent 172311f commit b5e40f5

22 files changed

Lines changed: 754 additions & 43 deletions

File tree

lib/tinyusb

Submodule tinyusb updated 1018 files

ports/atmel-samd/Makefile

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,23 @@ BASE_CFLAGS = \
8989
ifeq ($(CHIP_FAMILY), samd21)
9090
CFLAGS += -Os -DNDEBUG
9191
# 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
92+
CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_SAMD21 -DCFG_TUD_MIDI_RX_BUFSIZE=128 -DCFG_TUD_CDC_RX_BUFSIZE=128 -DCFG_TUD_MIDI_TX_BUFSIZE=128 -DCFG_TUD_CDC_TX_BUFSIZE=128 -DCFG_TUD_MSC_BUFSIZE=512
9393
endif
9494

9595
ifeq ($(CHIP_FAMILY), samd51)
9696
CFLAGS += -Os -DNDEBUG
9797
# 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
98+
CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_SAMD51 -DCFG_TUD_MIDI_RX_BUFSIZE=128 -DCFG_TUD_CDC_RX_BUFSIZE=256 -DCFG_TUD_MIDI_TX_BUFSIZE=128 -DCFG_TUD_CDC_TX_BUFSIZE=256 -DCFG_TUD_MSC_BUFSIZE=1024
9999
endif
100100

101101
#Debugging/Optimization
102102
ifeq ($(DEBUG), 1)
103103
# Turn on Python modules useful for debugging (e.g. uheap, ustack).
104104
CFLAGS += -ggdb
105105
# You may want to disable -flto if it interferes with debugging.
106-
CFLAGS += -flto
106+
# CFLAGS += -flto
107107
# You may want to enable these flags to make setting breakpoints easier.
108-
# CFLAGS += -fno-inline -fno-ipa-sra
108+
CFLAGS += -fno-inline -fno-ipa-sra
109109
ifeq ($(CHIP_FAMILY), samd21)
110110
CFLAGS += -DENABLE_MICRO_TRACE_BUFFER
111111
endif
@@ -266,7 +266,6 @@ SRC_C = \
266266
lib/oofatfs/option/ccsbcs.c \
267267
lib/timeutils/timeutils.c \
268268
lib/tinyusb/src/portable/microchip/$(CHIP_FAMILY)/dcd_$(CHIP_FAMILY).c \
269-
lib/tinyusb/src/portable/microchip/$(CHIP_FAMILY)/hal_$(CHIP_FAMILY).c \
270269
lib/utils/buffer_helper.c \
271270
lib/utils/context_manager_helpers.c \
272271
lib/utils/interrupt_char.c \

ports/atmel-samd/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ extern const struct _mp_obj_module_t gamepad_module;
237237
extern const struct _mp_obj_module_t stage_module;
238238
extern const struct _mp_obj_module_t touchio_module;
239239
extern const struct _mp_obj_module_t usb_hid_module;
240+
extern const struct _mp_obj_module_t usb_midi_module;
240241
extern const struct _mp_obj_module_t network_module;
241242
extern const struct _mp_obj_module_t socket_module;
242243
extern const struct _mp_obj_module_t wiznet_module;
@@ -382,6 +383,7 @@ extern const struct _mp_obj_module_t wiznet_module;
382383
{ MP_OBJ_NEW_QSTR(MP_QSTR_math), (mp_obj_t)&math_module }, \
383384
{ MP_OBJ_NEW_QSTR(MP_QSTR__time), (mp_obj_t)&time_module }, \
384385
{ MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid),(mp_obj_t)&usb_hid_module }, \
386+
{ MP_OBJ_NEW_QSTR(MP_QSTR_usb_midi),(mp_obj_t)&usb_midi_module }, \
385387
TOUCHIO_MODULE \
386388
EXTRA_BUILTIN_MODULES
387389

@@ -410,6 +412,7 @@ extern const struct _mp_obj_module_t wiznet_module;
410412
{ MP_OBJ_NEW_QSTR(MP_QSTR_math), (mp_obj_t)&math_module }, \
411413
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, \
412414
{ MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid),(mp_obj_t)&usb_hid_module }, \
415+
{ MP_OBJ_NEW_QSTR(MP_QSTR_usb_midi),(mp_obj_t)&usb_midi_module }, \
413416
TOUCHIO_MODULE \
414417
EXTRA_BUILTIN_MODULES
415418
#endif

ports/nrf/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ LDFLAGS += -mthumb -mabi=aapcs -T $(LD_FILE) -L boards/
7575
LDFLAGS += -Wl,--gc-sections
7676

7777
# TinyUSB defines
78-
CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_NRF5X -DCFG_TUD_CDC_RX_BUFSIZE=1024 -DCFG_TUD_CDC_TX_BUFSIZE=1024 -DCFG_TUD_MSC_BUFSIZE=4096
78+
CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_NRF5X -DCFG_TUD_CDC_RX_BUFSIZE=1024 -DCFG_TUD_CDC_TX_BUFSIZE=1024 -DCFG_TUD_MSC_BUFSIZE=4096 -DCFG_TUD_MIDI_RX_BUFSIZE=128 -DCFG_TUD_MIDI_TX_BUFSIZE=128
7979

8080
#Debugging/Optimization
8181
ifeq ($(DEBUG), 1)

ports/nrf/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ extern const struct _mp_obj_module_t supervisor_module;
175175
extern const struct _mp_obj_module_t gamepad_module;
176176
extern const struct _mp_obj_module_t neopixel_write_module;
177177
extern const struct _mp_obj_module_t usb_hid_module;
178+
extern const struct _mp_obj_module_t usb_midi_module;
178179
extern const struct _mp_obj_module_t bleio_module;
179180

180181
#if MICROPY_PY_BLEIO
@@ -207,6 +208,7 @@ extern const struct _mp_obj_module_t bleio_module;
207208
{ MP_OBJ_NEW_QSTR (MP_QSTR_time ), (mp_obj_t)&time_module }, \
208209
{ MP_OBJ_NEW_QSTR (MP_QSTR_json ), (mp_obj_t)&mp_module_ujson }, \
209210
USBHID_MODULE \
211+
{ MP_OBJ_NEW_QSTR(MP_QSTR_usb_midi),(mp_obj_t)&usb_midi_module }, \
210212
BLEIO_MODULE
211213

212214
// extra built in names to add to the global namespace

shared-bindings/usb_midi/PortIn.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdint.h>
28+
29+
#include "shared-bindings/usb_midi/PortIn.h"
30+
#include "shared-bindings/util.h"
31+
32+
#include "py/ioctl.h"
33+
#include "py/objproperty.h"
34+
#include "py/runtime.h"
35+
#include "py/stream.h"
36+
#include "supervisor/shared/translate.h"
37+
38+
39+
//| .. currentmodule:: usb_midi
40+
//|
41+
//| :class:`PortIn` -- receives midi commands over USB
42+
//| ===================================================
43+
//|
44+
//| .. class:: PortIn()
45+
//|
46+
//| Not currently dynamically supported.
47+
//|
48+
49+
STATIC mp_obj_t usb_midi_portin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) {
50+
return mp_const_none;
51+
}
52+
53+
// These are standard stream methods. Code is in py/stream.c.
54+
//
55+
//| .. method:: read(nbytes=None)
56+
//|
57+
//| Read characters. If ``nbytes`` is specified then read at most that many
58+
//| bytes. Otherwise, read everything that arrives until the connection
59+
//| times out. Providing the number of bytes expected is highly recommended
60+
//| because it will be faster.
61+
//|
62+
//| :return: Data read
63+
//| :rtype: bytes or None
64+
//|
65+
//| .. method:: readinto(buf, nbytes=None)
66+
//|
67+
//| Read bytes into the ``buf``. If ``nbytes`` is specified then read at most
68+
//| that many bytes. Otherwise, read at most ``len(buf)`` bytes.
69+
//|
70+
//| :return: number of bytes read and stored into ``buf``
71+
//| :rtype: bytes or None
72+
//|
73+
74+
// These three methods are used by the shared stream methods.
75+
STATIC mp_uint_t usb_midi_portin_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
76+
usb_midi_portin_obj_t *self = MP_OBJ_TO_PTR(self_in);
77+
byte *buf = buf_in;
78+
79+
// make sure we want at least 1 char
80+
if (size == 0) {
81+
return 0;
82+
}
83+
84+
return common_hal_usb_midi_portin_read(self, buf, size, errcode);
85+
}
86+
87+
STATIC mp_uint_t usb_midi_portin_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
88+
usb_midi_portin_obj_t *self = MP_OBJ_TO_PTR(self_in);
89+
mp_uint_t ret;
90+
if (request == MP_IOCTL_POLL) {
91+
mp_uint_t flags = arg;
92+
ret = 0;
93+
if ((flags & MP_IOCTL_POLL_RD) && common_hal_usb_midi_portin_bytes_available(self) > 0) {
94+
ret |= MP_IOCTL_POLL_RD;
95+
}
96+
} else {
97+
*errcode = MP_EINVAL;
98+
ret = MP_STREAM_ERROR;
99+
}
100+
return ret;
101+
}
102+
103+
STATIC const mp_rom_map_elem_t usb_midi_portin_locals_dict_table[] = {
104+
// Standard stream methods.
105+
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
106+
{ MP_OBJ_NEW_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
107+
};
108+
STATIC MP_DEFINE_CONST_DICT(usb_midi_portin_locals_dict, usb_midi_portin_locals_dict_table);
109+
110+
STATIC const mp_stream_p_t usb_midi_portin_stream_p = {
111+
.read = usb_midi_portin_read,
112+
.write = NULL,
113+
.ioctl = usb_midi_portin_ioctl,
114+
.is_text = false,
115+
};
116+
117+
const mp_obj_type_t usb_midi_portin_type = {
118+
{ &mp_type_type },
119+
.name = MP_QSTR_PortIn,
120+
.make_new = usb_midi_portin_make_new,
121+
.getiter = mp_identity_getiter,
122+
.iternext = mp_stream_unbuffered_iter,
123+
.protocol = &usb_midi_portin_stream_p,
124+
.locals_dict = (mp_obj_dict_t*)&usb_midi_portin_locals_dict,
125+
};

shared-bindings/usb_midi/PortIn.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_USB_MIDI_PORTIN_H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_USB_MIDI_PORTIN_H
29+
30+
#include "shared-module/usb_midi/PortIn.h"
31+
32+
extern const mp_obj_type_t usb_midi_portin_type;
33+
34+
// Construct an underlying UART object.
35+
extern void common_hal_usb_midi_portin_construct(usb_midi_portin_obj_t *self,
36+
uint8_t receiver_buffer_size);
37+
// Read characters.
38+
extern size_t common_hal_usb_midi_portin_read(usb_midi_portin_obj_t *self,
39+
uint8_t *data, size_t len, int *errcode);
40+
41+
extern uint32_t common_hal_usb_midi_portin_bytes_available(usb_midi_portin_obj_t *self);
42+
extern void common_hal_usb_midi_portin_clear_buffer(usb_midi_portin_obj_t *self);
43+
44+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_USB_MIDI_PORTIN_H

shared-bindings/usb_midi/PortOut.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdint.h>
28+
29+
#include "shared-bindings/usb_midi/PortOut.h"
30+
#include "shared-bindings/util.h"
31+
32+
#include "py/ioctl.h"
33+
#include "py/objproperty.h"
34+
#include "py/runtime.h"
35+
#include "py/stream.h"
36+
#include "supervisor/shared/translate.h"
37+
38+
39+
//| .. currentmodule:: usb_midi
40+
//|
41+
//| :class:`PortOut` -- sends midi messages to a computer over USB
42+
//| ==============================================================
43+
//|
44+
//| .. class:: PortOut()
45+
//|
46+
//| Not currently dynamically supported.
47+
//|
48+
49+
STATIC mp_obj_t usb_midi_portout_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) {
50+
return mp_const_none;
51+
}
52+
53+
// These are standard stream methods. Code is in py/stream.c.
54+
//
55+
//| .. method:: write(buf)
56+
//|
57+
//| Write the buffer of bytes to the bus.
58+
//|
59+
//| :return: the number of bytes written
60+
//| :rtype: int or None
61+
//|
62+
63+
STATIC mp_uint_t usb_midi_portout_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) {
64+
usb_midi_portout_obj_t *self = MP_OBJ_TO_PTR(self_in);
65+
const byte *buf = buf_in;
66+
67+
return common_hal_usb_midi_portout_write(self, buf, size, errcode);
68+
}
69+
70+
STATIC mp_uint_t usb_midi_portout_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
71+
usb_midi_portout_obj_t *self = MP_OBJ_TO_PTR(self_in);
72+
mp_uint_t ret;
73+
if (request == MP_IOCTL_POLL) {
74+
mp_uint_t flags = arg;
75+
ret = 0;
76+
if ((flags & MP_IOCTL_POLL_WR) && common_hal_usb_midi_portout_ready_to_tx(self)) {
77+
ret |= MP_IOCTL_POLL_WR;
78+
}
79+
} else {
80+
*errcode = MP_EINVAL;
81+
ret = MP_STREAM_ERROR;
82+
}
83+
return ret;
84+
}
85+
86+
STATIC const mp_rom_map_elem_t usb_midi_portout_locals_dict_table[] = {
87+
// Standard stream methods.
88+
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
89+
};
90+
STATIC MP_DEFINE_CONST_DICT(usb_midi_portout_locals_dict, usb_midi_portout_locals_dict_table);
91+
92+
STATIC const mp_stream_p_t usb_midi_portout_stream_p = {
93+
.read = NULL,
94+
.write = usb_midi_portout_write,
95+
.ioctl = usb_midi_portout_ioctl,
96+
.is_text = false,
97+
};
98+
99+
const mp_obj_type_t usb_midi_portout_type = {
100+
{ &mp_type_type },
101+
.name = MP_QSTR_PortOut,
102+
.make_new = usb_midi_portout_make_new,
103+
.getiter = mp_identity_getiter,
104+
.iternext = mp_stream_unbuffered_iter,
105+
.protocol = &usb_midi_portout_stream_p,
106+
.locals_dict = (mp_obj_dict_t*)&usb_midi_portout_locals_dict,
107+
};

0 commit comments

Comments
 (0)