wip: latent usb device enabling/disabling

This commit is contained in:
Dan Halbert 2021-04-13 23:33:44 -04:00
parent 2a58b667aa
commit 4a7e129287
16 changed files with 261 additions and 64 deletions

View File

@ -600,6 +600,14 @@ msgstr ""
msgid "Can't set CCCD on local Characteristic"
msgstr ""
#: shared-bindings/usb_cdc/__init__.c shared-bindings/usb_midi/__init__.c
msgid "Cannot change USB devices now"
msgstr ""
#: shared-bindings/storage/__init__.c
msgid "Cannot change usb devices now"
msgstr ""
#: shared-bindings/_bleio/Adapter.c
msgid "Cannot create a new Adapter; use _bleio.adapter;"
msgstr ""
@ -1080,10 +1088,6 @@ msgstr ""
msgid "I2SOut not available"
msgstr ""
#: ports/esp32s2/common-hal/alarm/pin/__init__.c
msgid "IOs 0, 2 & 4 do not support internal pullup in sleep"
msgstr ""
#: shared-bindings/aesio/aes.c
#, c-format
msgid "IV must be %d bytes long"
@ -3249,10 +3253,6 @@ msgstr ""
msgid "invalid syntax for number"
msgstr ""
#: ports/esp32s2/common-hal/alarm/pin/__init__.c
msgid "io must be rtc io"
msgstr ""
#: py/objtype.c
msgid "issubclass() arg 1 must be a class"
msgstr ""
@ -3712,6 +3712,7 @@ msgstr ""
#: ports/esp32s2/boards/adafruit_funhouse/mpconfigboard.h
#: ports/esp32s2/boards/adafruit_magtag_2.9_grayscale/mpconfigboard.h
#: ports/esp32s2/boards/adafruit_metro_esp32s2/mpconfigboard.h
#: ports/esp32s2/boards/artisense_rd00/mpconfigboard.h
#: ports/esp32s2/boards/electroniccats_bastwifi/mpconfigboard.h
#: ports/esp32s2/boards/espressif_kaluga_1/mpconfigboard.h
#: ports/esp32s2/boards/espressif_saola_1_wroom/mpconfigboard.h
@ -4010,10 +4011,6 @@ msgstr ""
msgid "trapz is defined for 1D arrays of equal length"
msgstr ""
#: ports/esp32s2/common-hal/alarm/pin/__init__.c
msgid "trigger level must be 0 or 1"
msgstr ""
#: py/obj.c
msgid "tuple/list has wrong length"
msgstr ""
@ -4148,10 +4145,6 @@ msgstr ""
msgid "value_count must be > 0"
msgstr ""
#: ports/esp32s2/common-hal/alarm/pin/__init__.c
msgid "wakeup conflict"
msgstr ""
#: ports/esp32s2/common-hal/watchdog/WatchDogTimer.c
msgid "watchdog not initialized"
msgstr ""

22
main.c
View File

@ -94,10 +94,18 @@
#include "shared-module/network/__init__.h"
#endif
#if CIRCUITPY_STORAGE
#include "shared-module/storage/__init__.h"
#endif
#if CIRCUITPY_USB_CDC
#include "shared-module/usb_cdc/__init__.h"
#endif
#if CIRCUITPY_USB_MIDI
#include "shared-module/usb_midi/__init__.h"
#endif
#if CIRCUITPY_WIFI
#include "shared-bindings/wifi/__init__.h"
#endif
@ -169,6 +177,20 @@ STATIC void start_mp(supervisor_allocation* heap) {
#if CIRCUITPY_NETWORK
network_module_init();
#endif
// Do before boot.py.
#if CIRCUITPY_STORAGE
storage_init();
#endif
#if CIRCUITPY_USB_CDC
usb_cdc_init();
#endif
#if CIRCUITPY_USB_MIDI
usb_midi_init();
#endif
}
STATIC void stop_mp(void) {

View File

@ -325,10 +325,8 @@ CFLAGS += -DCIRCUITPY_TOUCHIO=$(CIRCUITPY_TOUCHIO)
CIRCUITPY_UHEAP ?= 0
CFLAGS += -DCIRCUITPY_UHEAP=$(CIRCUITPY_UHEAP)
# Disable by default for now, until we have dynamic enabling.
CIRCUITPY_USB_CDC ?= 0
# Secondary CDC is usually available if there are at least 8 endpoints.
#CIRCUITPY_USB_CDC ?= $(shell expr $(USB_NUM_EP) '>=' 8)
CIRCUITPY_USB_CDC ?= $(shell expr $(USB_NUM_EP) '>=' 8)
CFLAGS += -DCIRCUITPY_USB_CDC=$(CIRCUITPY_USB_CDC)
CIRCUITPY_USB_HID ?= 1

View File

@ -158,6 +158,21 @@ mp_obj_t storage_erase_filesystem(void) {
}
MP_DEFINE_CONST_FUN_OBJ_0(storage_erase_filesystem_obj, storage_erase_filesystem);
//| def enable_usb(enabled: True) -> None:
//| """Enable or disable presenting ``CIRCUITPY`` as a USB mass storage device.
//| By default, ``CIRCUITPY`` is visible.
//| Use ``storage.enable_usb(False)`` to hide CIRCUITPY from the host computer.
//| Can be changed in ``boot.py``, before USB is connected."""
//| ...
//|
STATIC mp_obj_t storage_enable_usb(mp_obj_t enabled) {
if (!common_hal_storage_enable_usb(mp_obj_is_true(enabled))) {
mp_raise_RuntimeError(translate("Cannot change usb devices now"));
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(storage_enable_usb_obj, storage_enable_usb);
STATIC const mp_rom_map_elem_t storage_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_storage) },
@ -166,6 +181,7 @@ STATIC const mp_rom_map_elem_t storage_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_remount), MP_ROM_PTR(&storage_remount_obj) },
{ MP_ROM_QSTR(MP_QSTR_getmount), MP_ROM_PTR(&storage_getmount_obj) },
{ MP_ROM_QSTR(MP_QSTR_erase_filesystem), MP_ROM_PTR(&storage_erase_filesystem_obj) },
{ MP_ROM_QSTR(MP_QSTR_enable_usb), MP_ROM_PTR(&storage_enable_usb_obj) },
//| class VfsFat:
//| def __init__(self, block_device: str) -> None:

View File

@ -36,5 +36,6 @@ void common_hal_storage_umount_object(mp_obj_t vfs_obj);
void common_hal_storage_remount(const char *path, bool readonly, bool disable_concurrent_write_protection);
mp_obj_t common_hal_storage_getmount(const char *path);
void common_hal_storage_erase_filesystem(void);
bool common_hal_storage_enable_usb(bool enabled);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_STORAGE___INIT___H

View File

@ -36,22 +36,73 @@
//| """USB CDC Serial streams
//|
//| The `usb_cdc` module allows access to USB CDC (serial) communications."""
//| The `usb_cdc` module allows access to USB CDC (serial) communications.
//|
//| serials: Tuple[Serial, ...]
//| """Tuple of all CDC streams. Each item is a `Serial`.
//| ``serials[0]`` is the USB REPL connection.
//| ``serials[1]`` is a second USB serial connection, unconnected to the REPL.
//| On Windows, each `Serial` is visible as a separate COM port. The ports will often
//| be assigned consecutively, REPL first, but this is not always true.
//|
//| On Linux, the ports are typically ``/dev/ttyACM0`` and ``/dev/ttyACM1``. The REPL
//| is usually first.
//|
//| On MacOS, the ports are typically ``/dev/cu.usbmodem<something>``. The something
//| varies based on the USB bus and port used. The REPL is usually first.
//| """
//|
//| repl: Optional[Serial]
//| """The `Serial` object that can be used to communicate over the REPL serial
//| channel. ``None`` if disabled.
//|
//| Note that`sys.stdin` and `sys.stdout` are also connected to the REPL, though
//| they are text-based streams, and the `repl` object is a binary stream."""
//|
//| data: Optional[Serial]
//| """A `Serial` object that can be used to send and receive binary data to and from
//| the host.
//| Note that `data` is *disabled* by default."""
static const mp_map_elem_t usb_cdc_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_usb_cdc) },
{ MP_ROM_QSTR(MP_QSTR_Serial), MP_OBJ_FROM_PTR(&usb_cdc_serial_type) },
{ MP_ROM_QSTR(MP_QSTR_serials), MP_OBJ_FROM_PTR(&usb_cdc_serials_tuple) },
//| def enable_repl(enabled:bool) -> None:
//| """Enable or disable the `repl` USB serial connection. The REPL
//| is enabled by default.
//| Can be changed in ``boot.py``, before USB is connected."""
//| ...
//|
STATIC mp_obj_t usb_cdc_enable_repl(mp_obj_t enabled) {
if (!common_hal_usb_cdc_enable_repl(mp_obj_is_true(enabled))) {
mp_raise_RuntimeError(translate("Cannot change USB devices now"));
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(usb_cdc_enable_repl_obj, usb_cdc_enable_repl);
//| def enable_data(enabled: bool) -> None:
//| """Enable or disable the `data` USB serial connection;n
//| *disabled* by default.
//| Can be changed in ``boot.py``, before USB is connected."""
//| ...
//|
STATIC mp_obj_t usb_cdc_enable_data(mp_obj_t enabled) {
if (!common_hal_usb_cdc_enable_data(mp_obj_is_true(enabled))) {
mp_raise_RuntimeError(translate("Cannot change USB devices now"));
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(usb_cdc_enable_data_obj, usb_cdc_enable_data);
// The usb_cdc module dict is mutable so that .repl and .data may
// be set to a Serial or to None depending on whether they are enabled or not.
static mp_map_elem_t usb_cdc_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_usb_cdc) },
{ MP_ROM_QSTR(MP_QSTR_Serial), MP_OBJ_FROM_PTR(&usb_cdc_serial_type) },
{ MP_ROM_QSTR(MP_QSTR_repl), mp_const_none },
{ MP_ROM_QSTR(MP_QSTR_data), mp_const_none },
{ MP_ROM_QSTR(MP_QSTR_enable_repl), MP_OBJ_FROM_PTR(&usb_cdc_enable_repl_obj) },
{ MP_ROM_QSTR(MP_QSTR_enable_data), MP_OBJ_FROM_PTR(&usb_cdc_enable_data_obj) },
};
static MP_DEFINE_CONST_DICT(usb_cdc_module_globals, usb_cdc_module_globals_table);
static MP_DEFINE_MUTABLE_DICT(usb_cdc_module_globals, usb_cdc_module_globals_table);
const mp_obj_module_t usb_cdc_module = {
.base = { &mp_type_module },

View File

@ -29,4 +29,7 @@
#include "shared-module/usb_cdc/__init__.h"
bool common_hal_usb_cdc_enable_repl(bool enabled);
bool common_hal_usb_cdc_enable_data(bool enabled);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_USB_CDC___INIT___H

View File

@ -43,11 +43,25 @@
//| """Tuple of all MIDI ports. Each item is ether `PortIn` or `PortOut`."""
//|
//| def enable(enabled: bool) -> None:
//| """Enable or disable USB MIDI device. By default, MIDI is enabled.
//| Can be changed in ``boot.py``, before USB is connected."""
//| ...
//|
STATIC mp_obj_t usb_midi_enable(mp_obj_t enabled) {
if (!common_hal_usb_midi_enable(mp_obj_is_true(enabled))) {
mp_raise_RuntimeError(translate("Cannot change USB devices now"));
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(usb_midi_enable_obj, usb_midi_enable);
mp_map_elem_t usb_midi_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_usb_midi) },
{ MP_ROM_QSTR(MP_QSTR_ports), mp_const_empty_tuple },
{ MP_ROM_QSTR(MP_QSTR_enable), MP_OBJ_FROM_PTR(&usb_midi_enable_obj) },
{ MP_ROM_QSTR(MP_QSTR_ports), mp_const_empty_tuple },
{ MP_ROM_QSTR(MP_QSTR_PortIn), MP_OBJ_FROM_PTR(&usb_midi_portin_type) },
{ MP_ROM_QSTR(MP_QSTR_PortOut), MP_OBJ_FROM_PTR(&usb_midi_portout_type) },
{ MP_ROM_QSTR(MP_QSTR_PortOut), MP_OBJ_FROM_PTR(&usb_midi_portout_type) },
};
// This isn't const so we can set ports dynamically.

View File

@ -31,4 +31,6 @@
extern mp_obj_dict_t usb_midi_module_globals;
bool common_hal_usb_midi_enable(bool enabled);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_USB_MIDI___INIT___H

View File

@ -39,6 +39,10 @@
#include "supervisor/filesystem.h"
#include "supervisor/flash.h"
#include "supervisor/usb.h"
#include "tusb.h"
// Is the MSC device enabled?
static bool usb_storage_enabled;
STATIC mp_obj_t mp_vfs_proxy_call(mp_vfs_mount_t *vfs, qstr meth_name, size_t n_args, const mp_obj_t *args) {
if (vfs == MP_VFS_NONE) {
@ -57,6 +61,10 @@ STATIC mp_obj_t mp_vfs_proxy_call(mp_vfs_mount_t *vfs, qstr meth_name, size_t n_
return mp_call_method_n_kw(n_args, 0, meth);
}
void storage_init(void) {
usb_storage_enabled = true;
}
void common_hal_storage_mount(mp_obj_t vfs_obj, const char *mount_path, bool readonly) {
// create new object
mp_vfs_mount_t *vfs = m_new_obj(mp_vfs_mount_t);
@ -166,3 +174,12 @@ void common_hal_storage_erase_filesystem(void) {
common_hal_mcu_reset();
// We won't actually get here, since we're resetting.
}
bool common_hal_storage_enable_usb(bool enabled) {
// We can't change the descriptors once we're connected.
if (!tud_connected()) {
return false;
}
usb_storage_enabled = enabled;
return true;
}

View File

@ -0,0 +1,32 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Dan Halbert for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef SHARED_MODULE_STORAGE___INIT___H
#define SHARED_MODULE_STORAGE___INIT___H
void storage_init(void);
#endif // SHARED_MODULE_STORAGE___INIT___H

View File

@ -38,23 +38,46 @@
#error CFG_TUD_CDC must be exactly 2
#endif
static usb_cdc_serial_obj_t serial_objs[CFG_TUD_CDC] = {
{ .base.type = &usb_cdc_serial_type,
.timeout = -1.0f,
.write_timeout = -1.0f,
.idx = 0,}, {
.base.type = &usb_cdc_serial_type,
.timeout = -1.0f,
.write_timeout = -1.0f,
.idx = 1,
}
static bool usb_cdc_repl_enabled;
static bool usb_cdc_data_enabled;
static usb_cdc_serial_obj_t usb_cdc_repl_obj = {
.base.type = &usb_cdc_serial_type,
.timeout = -1.0f,
.write_timeout = -1.0f,
.idx = 0,
};
const mp_rom_obj_tuple_t usb_cdc_serials_tuple = {
.base.type = &mp_type_tuple,
.len = CFG_TUD_CDC,
.items = {
&serial_objs[0],
&serial_objs[1],
},
static usb_cdc_serial_obj_t usb_cdc_data_obj = {
.base.type = &usb_cdc_serial_type,
.timeout = -1.0f,
.write_timeout = -1.0f,
.idx = 1,
};
void usb_cdc_init(void) {
usb_cdc_repl_enabled = true;
usb_cdc_data_enabled = false;
}
bool common_hal_usb_cdc_enable_repl(bool enabled) {
// We can't change the descriptors once we're connected.
if (!tud_connected()) {
// TODO set entry in dict
return false;
}
usb_cdc_repl_enabled = enabled;
// TODO set entry in dict
return true;
}
bool common_hal_usb_cdc_enable_data(bool enabled) {
// We can't change the descriptors once we're connected.
if (!tud_connected()) {
// TODO set entry in dict
return false;
}
usb_cdc_data_enabled = enabled;
// TODO set entry in dict
return true;
}

View File

@ -31,4 +31,6 @@
extern const mp_rom_obj_tuple_t usb_cdc_serials_tuple;
void usb_cdc_init(void);
#endif /* SHARED_MODULE_USB_CDC___INIT___H */

View File

@ -38,26 +38,48 @@
supervisor_allocation *usb_midi_allocation;
// Is the USB MIDI device enabled?
static bool usb_midi_enabled;
void usb_midi_init(void) {
// TODO(tannewt): Make this dynamic.
size_t tuple_size = align32_size(sizeof(mp_obj_tuple_t) + sizeof(mp_obj_t *) * 2);
size_t portin_size = align32_size(sizeof(usb_midi_portin_obj_t));
size_t portout_size = align32_size(sizeof(usb_midi_portout_obj_t));
usb_midi_enabled = true;
}
// For each embedded MIDI Jack in the descriptor we create a Port
usb_midi_allocation = allocate_memory(tuple_size + portin_size + portout_size, false, false);
void usb_midi_usb_init(void) {
mp_obj_tuple_t *ports;
mp_obj_tuple_t *ports = (mp_obj_tuple_t *)usb_midi_allocation->ptr;
ports->base.type = &mp_type_tuple;
ports->len = 2;
if (usb_midi_enabled) {
// TODO(tannewt): Make this dynamic.
size_t tuple_size = align32_size(sizeof(mp_obj_tuple_t) + sizeof(mp_obj_t *) * 2);
size_t portin_size = align32_size(sizeof(usb_midi_portin_obj_t));
size_t portout_size = align32_size(sizeof(usb_midi_portout_obj_t));
usb_midi_portin_obj_t *in = (usb_midi_portin_obj_t *)(usb_midi_allocation->ptr + tuple_size / 4);
in->base.type = &usb_midi_portin_type;
ports->items[0] = MP_OBJ_FROM_PTR(in);
// For each embedded MIDI Jack in the descriptor we create a Port
usb_midi_allocation = allocate_memory(tuple_size + portin_size + portout_size, false, false);
usb_midi_portout_obj_t *out = (usb_midi_portout_obj_t *)(usb_midi_allocation->ptr + tuple_size / 4 + portin_size / 4);
out->base.type = &usb_midi_portout_type;
ports->items[1] = MP_OBJ_FROM_PTR(out);
ports = (mp_obj_tuple_t *)usb_midi_allocation->ptr;
ports->base.type = &mp_type_tuple;
ports->len = 2;
usb_midi_portin_obj_t *in = (usb_midi_portin_obj_t *)(usb_midi_allocation->ptr + tuple_size / 4);
in->base.type = &usb_midi_portin_type;
ports->items[0] = MP_OBJ_FROM_PTR(in);
usb_midi_portout_obj_t *out = (usb_midi_portout_obj_t *)(usb_midi_allocation->ptr + tuple_size / 4 + portin_size / 4);
out->base.type = &usb_midi_portout_type;
ports->items[1] = MP_OBJ_FROM_PTR(out);
} else {
ports = mp_const_empty_tuple;
}
mp_map_lookup(&usb_midi_module_globals.map, MP_ROM_QSTR(MP_QSTR_ports), MP_MAP_LOOKUP)->value = MP_OBJ_FROM_PTR(ports);
}
bool common_hal_usb_midi_enable(bool enabled) {
// We can't change the descriptors once we're connected.
if (!tud_connected()) {
return false;
}
usb_midi_enabled = enabled;
return true;
}

View File

@ -28,5 +28,6 @@
#define SHARED_MODULE_USB_MIDI___INIT___H
void usb_midi_init(void);
void usb_midi_usb_init(void);
#endif /* SHARED_MODULE_USB_MIDI___INIT___H */

View File

@ -94,7 +94,7 @@ void usb_init(void) {
#endif
#if CIRCUITPY_USB_MIDI
usb_midi_init();
usb_midi_usb_init();
#endif
}