Skip to content

Commit 516d7da

Browse files
authored
Merge pull request adafruit#1449 from tannewt/midi
Add USB MIDI support for SAMD and nRF.
2 parents 16b4fa7 + 3dd59c3 commit 516d7da

24 files changed

Lines changed: 765 additions & 44 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ git:
2121
# that SDK is shortest and add it there. In the case of major re-organizations,
2222
# just try to make the builds "about equal in run time"
2323
env:
24-
- TRAVIS_TESTS="unix docs translations website" TRAVIS_BOARDS="feather_huzzah circuitplayground_express mini_sam_m4 grandcentral_m4_express pca10056 pca10059 feather_nrf52832 feather_nrf52840_express makerdiary_nrf52840_mdk particle_boron particle_argon particle_xenon sparkfun_nrf52840_mini" TRAVIS_SDK=arm:nrf:esp8266
24+
- TRAVIS_TESTS="unix docs translations website" TRAVIS_BOARDS="feather_huzzah circuitplayground_express mini_sam_m4 grandcentral_m4_express pca10056 pca10059 feather_nrf52840_express makerdiary_nrf52840_mdk particle_boron particle_argon particle_xenon sparkfun_nrf52840_mini" TRAVIS_SDK=arm:nrf:esp8266
2525
- TRAVIS_BOARDS="metro_m0_express metro_m4_express pirkey_m0 trellis_m4_express trinket_m0" TRAVIS_SDK=arm
2626
- TRAVIS_BOARDS="feather_radiofruit_zigbee gemma_m0 hallowing_m0_express itsybitsy_m0_express itsybitsy_m4_express meowmeow" TRAVIS_SDK=arm
2727
- TRAVIS_BOARDS="feather_m0_express_crickit feather_m0_rfm69 feather_m0_rfm9x feather_m4_express arduino_zero arduino_mkr1300 arduino_mkrzero" TRAVIS_SDK=arm

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: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
//| PortIn objects are constructed for every corresponding entry in the USB descriptor and added
49+
//| to the `usb_midi.ports` tuple.
50+
//|
51+
52+
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) {
53+
return mp_const_none;
54+
}
55+
56+
// These are standard stream methods. Code is in py/stream.c.
57+
//
58+
//| .. method:: read(nbytes=None)
59+
//|
60+
//| Read characters. If ``nbytes`` is specified then read at most that many
61+
//| bytes. Otherwise, read everything that arrives until the connection
62+
//| times out. Providing the number of bytes expected is highly recommended
63+
//| because it will be faster.
64+
//|
65+
//| :return: Data read
66+
//| :rtype: bytes or None
67+
//|
68+
//| .. method:: readinto(buf, nbytes=None)
69+
//|
70+
//| Read bytes into the ``buf``. If ``nbytes`` is specified then read at most
71+
//| that many bytes. Otherwise, read at most ``len(buf)`` bytes.
72+
//|
73+
//| :return: number of bytes read and stored into ``buf``
74+
//| :rtype: bytes or None
75+
//|
76+
77+
// These three methods are used by the shared stream methods.
78+
STATIC mp_uint_t usb_midi_portin_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
79+
usb_midi_portin_obj_t *self = MP_OBJ_TO_PTR(self_in);
80+
byte *buf = buf_in;
81+
82+
// make sure we want at least 1 char
83+
if (size == 0) {
84+
return 0;
85+
}
86+
87+
return common_hal_usb_midi_portin_read(self, buf, size, errcode);
88+
}
89+
90+
STATIC mp_uint_t usb_midi_portin_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
91+
usb_midi_portin_obj_t *self = MP_OBJ_TO_PTR(self_in);
92+
mp_uint_t ret;
93+
if (request == MP_IOCTL_POLL) {
94+
mp_uint_t flags = arg;
95+
ret = 0;
96+
if ((flags & MP_IOCTL_POLL_RD) && common_hal_usb_midi_portin_bytes_available(self) > 0) {
97+
ret |= MP_IOCTL_POLL_RD;
98+
}
99+
} else {
100+
*errcode = MP_EINVAL;
101+
ret = MP_STREAM_ERROR;
102+
}
103+
return ret;
104+
}
105+
106+
STATIC const mp_rom_map_elem_t usb_midi_portin_locals_dict_table[] = {
107+
// Standard stream methods.
108+
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
109+
{ MP_OBJ_NEW_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
110+
};
111+
STATIC MP_DEFINE_CONST_DICT(usb_midi_portin_locals_dict, usb_midi_portin_locals_dict_table);
112+
113+
STATIC const mp_stream_p_t usb_midi_portin_stream_p = {
114+
.read = usb_midi_portin_read,
115+
.write = NULL,
116+
.ioctl = usb_midi_portin_ioctl,
117+
.is_text = false,
118+
};
119+
120+
const mp_obj_type_t usb_midi_portin_type = {
121+
{ &mp_type_type },
122+
.name = MP_QSTR_PortIn,
123+
.make_new = usb_midi_portin_make_new,
124+
.getiter = mp_identity_getiter,
125+
.iternext = mp_stream_unbuffered_iter,
126+
.protocol = &usb_midi_portin_stream_p,
127+
.locals_dict = (mp_obj_dict_t*)&usb_midi_portin_locals_dict,
128+
};

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: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
//| PortOut objects are constructed for every corresponding entry in the USB descriptor and added
49+
//| to the `usb_midi.ports` tuple.
50+
//|
51+
52+
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) {
53+
return mp_const_none;
54+
}
55+
56+
// These are standard stream methods. Code is in py/stream.c.
57+
//
58+
//| .. method:: write(buf)
59+
//|
60+
//| Write the buffer of bytes to the bus.
61+
//|
62+
//| :return: the number of bytes written
63+
//| :rtype: int or None
64+
//|
65+
66+
STATIC mp_uint_t usb_midi_portout_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) {
67+
usb_midi_portout_obj_t *self = MP_OBJ_TO_PTR(self_in);
68+
const byte *buf = buf_in;
69+
70+
return common_hal_usb_midi_portout_write(self, buf, size, errcode);
71+
}
72+
73+
STATIC mp_uint_t usb_midi_portout_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
74+
usb_midi_portout_obj_t *self = MP_OBJ_TO_PTR(self_in);
75+
mp_uint_t ret;
76+
if (request == MP_IOCTL_POLL) {
77+
mp_uint_t flags = arg;
78+
ret = 0;
79+
if ((flags & MP_IOCTL_POLL_WR) && common_hal_usb_midi_portout_ready_to_tx(self)) {
80+
ret |= MP_IOCTL_POLL_WR;
81+
}
82+
} else {
83+
*errcode = MP_EINVAL;
84+
ret = MP_STREAM_ERROR;
85+
}
86+
return ret;
87+
}
88+
89+
STATIC const mp_rom_map_elem_t usb_midi_portout_locals_dict_table[] = {
90+
// Standard stream methods.
91+
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
92+
};
93+
STATIC MP_DEFINE_CONST_DICT(usb_midi_portout_locals_dict, usb_midi_portout_locals_dict_table);
94+
95+
STATIC const mp_stream_p_t usb_midi_portout_stream_p = {
96+
.read = NULL,
97+
.write = usb_midi_portout_write,
98+
.ioctl = usb_midi_portout_ioctl,
99+
.is_text = false,
100+
};
101+
102+
const mp_obj_type_t usb_midi_portout_type = {
103+
{ &mp_type_type },
104+
.name = MP_QSTR_PortOut,
105+
.make_new = usb_midi_portout_make_new,
106+
.getiter = mp_identity_getiter,
107+
.iternext = mp_stream_unbuffered_iter,
108+
.protocol = &usb_midi_portout_stream_p,
109+
.locals_dict = (mp_obj_dict_t*)&usb_midi_portout_locals_dict,
110+
};

0 commit comments

Comments
 (0)