update Python API according to review comments

This commit is contained in:
Dan Halbert 2021-05-03 22:22:29 -04:00
parent 3e2236be8e
commit adc3d7d55e
19 changed files with 221 additions and 132 deletions

View File

@ -40,7 +40,7 @@
#include "shared-bindings/_bleio/Service.h"
#include "shared-bindings/_bleio/UUID.h"
#if CIRCUITPY_REPL_BLE
#if CIRCUITPY_CONSOLE_BLE
static const char default_name[] = "CP-REPL"; // max 8 chars or uuid won't fit in adv data
static const char NUS_UUID[] = "6e400001-b5a3-f393-e0a9-e50e24dcca9e";
@ -188,4 +188,4 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) {
}
}
#endif // CIRCUITPY_REPL_BLE
#endif // CIRCUITPY_CONSOLE_BLE

View File

@ -28,15 +28,15 @@
#include "supervisor/serial.h"
#if CIRCUITPY_REPL_BLE
#if CIRCUITPY_CONSOLE_BLE
#include "ble_uart.h"
#elif CIRCUITPY_REPL_UART
#elif CIRCUITPY_CONSOLE_UART
#include <string.h>
#include "nrf_gpio.h"
#include "nrfx_uarte.h"
#endif
#if CIRCUITPY_REPL_BLE
#if CIRCUITPY_CONSOLE_BLE
void serial_init(void) {
ble_uart_init();
@ -58,7 +58,7 @@ void serial_write(const char *text) {
ble_uart_stdout_tx_str(text);
}
#elif CIRCUITPY_REPL_UART
#elif CIRCUITPY_CONSOLE_UART
uint8_t serial_received_char;
nrfx_uarte_t serial_instance = NRFX_UARTE_INSTANCE(0);
@ -124,4 +124,4 @@ void serial_write_substring(const char *text, uint32_t len) {
}
}
#endif // CIRCUITPY_REPL_UART
#endif // CIRCUITPY_CONSOLE_UART

View File

@ -133,6 +133,12 @@ CFLAGS += -DCIRCUITPY_DIGITALIO=$(CIRCUITPY_DIGITALIO)
CIRCUITPY_COMPUTED_GOTO_SAVE_SPACE ?= 0
CFLAGS += -DCIRCUITPY_COMPUTED_GOTO_SAVE_SPACE=$(CIRCUITPY_COMPUTED_GOTO_SAVE_SPACE)
CIRCUITPY_CONSOLE_BLE ?= 0
CFLAGS += -DCIRCUITPY_CONSOLE_BLE=$(CIRCUITPY_CONSOLE_BLE)
CIRCUITPY_CONSOLE_UART ?= 0
CFLAGS += -DCIRCUITPY_CONSOLE_UART=$(CIRCUITPY_CONSOLE_UART)
CIRCUITPY_COUNTIO ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_COUNTIO=$(CIRCUITPY_COUNTIO)
@ -246,12 +252,6 @@ CFLAGS += -DCIRCUITPY_RE=$(CIRCUITPY_RE)
CIRCUITPY_RE_DEBUG ?= 0
CFLAGS += -DCIRCUITPY_RE_DEBUG=$(CIRCUITPY_RE_DEBUG)
CIRCUITPY_REPL_BLE ?= 0
CFLAGS += -DCIRCUITPY_REPL_BLE=$(CIRCUITPY_REPL_BLE)
CIRCUITPY_REPL_UART ?= 0
CFLAGS += -DCIRCUITPY_REPL_UART=$(CIRCUITPY_REPL_UART)
# Should busio.I2C() check for pullups?
# Some boards in combination with certain peripherals may not want this.
CIRCUITPY_REQUIRE_I2C_PULLUPS ?= 1
@ -337,8 +337,8 @@ CFLAGS += -DCIRCUITPY_USB=$(CIRCUITPY_USB)
CIRCUITPY_USB_CDC ?= 1
CFLAGS += -DCIRCUITPY_USB_CDC=$(CIRCUITPY_USB_CDC)
CIRCUITPY_USB_CDC_REPL_ENABLED_DEFAULT ?= 1
CFLAGS += -DCIRCUITPY_USB_CDC_REPL_ENABLED_DEFAULT=$(CIRCUITPY_USB_CDC_REPL_ENABLED_DEFAULT)
CIRCUITPY_USB_CDC_CONSOLE_ENABLED_DEFAULT ?= 1
CFLAGS += -DCIRCUITPY_USB_CDC_CONSOLE_ENABLED_DEFAULT=$(CIRCUITPY_USB_CDC_CONSOLE_ENABLED_DEFAULT)
CIRCUITPY_USB_CDC_DATA_ENABLED_DEFAULT ?= 0
CFLAGS += -DCIRCUITPY_USB_CDC_DATA_ENABLED_DEFAULT=$(CIRCUITPY_USB_CDC_DATA_ENABLED_DEFAULT)

View File

@ -158,33 +158,45 @@ mp_obj_t storage_erase_filesystem(void) {
}
MP_DEFINE_CONST_FUN_OBJ_0(storage_erase_filesystem_obj, storage_erase_filesystem);
//| def configure_usb(enabled: bool = True) -> None:
//| """Configure the USB mass storage device.
//| Enable or disable presenting ``CIRCUITPY`` as a USB mass storage device.
//| def disable_usb_drive() -> None:
//| """Disable presenting ``CIRCUITPY`` as a USB mass storage device.
//| By default, the device is enabled and ``CIRCUITPY`` is visible.
//| Can be called in ``boot.py``, before USB is connected.
//|
//| :param enabled bool: Enable or disable the USB mass storage device.
//| True to enable; False to disable. Enabled by default."""
//| Can be called in ``boot.py``, before USB is connected."""
//| ...
//|
STATIC mp_obj_t storage_configure_usb(mp_obj_t enabled) {
if (!common_hal_storage_configure_usb(mp_obj_is_true(enabled))) {
STATIC mp_obj_t storage_disable_usb_drive(void) {
if (!common_hal_storage_disable_usb_drive()) {
mp_raise_RuntimeError(translate("Cannot change usb devices now"));
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(storage_configure_usb_obj, storage_configure_usb);
MP_DEFINE_CONST_FUN_OBJ_0(storage_disable_usb_drive_obj, storage_disable_usb_drive);
//| def enable_usb_drive() -> None:
//| """Enabled presenting ``CIRCUITPY`` as a USB mass storage device.
//| By default, the device is enabled and ``CIRCUITPY`` is visible,
//| so you do not normally need to call this function.
//| Can be called in ``boot.py``, before USB is connected."""
//| ...
//|
STATIC mp_obj_t storage_enable_usb_drive(void) {
if (!common_hal_storage_enable_usb_drive()) {
mp_raise_RuntimeError(translate("Cannot change usb devices now"));
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_0(storage_enable_usb_drive_obj, storage_enable_usb_drive);
STATIC const mp_rom_map_elem_t storage_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_storage) },
{ MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&storage_mount_obj) },
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&storage_umount_obj) },
{ 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_configure_usb), MP_ROM_PTR(&storage_configure_usb_obj) },
{ MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&storage_mount_obj) },
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&storage_umount_obj) },
{ 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_disable_usb_drive), MP_ROM_PTR(&storage_disable_usb_drive_obj) },
{ MP_ROM_QSTR(MP_QSTR_enable_usb_drive), MP_ROM_PTR(&storage_enable_usb_drive_obj) },
//| class VfsFat:
//| def __init__(self, block_device: str) -> None:

View File

@ -38,6 +38,8 @@ 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_configure_usb(bool enabled);
bool common_hal_storage_disable_usb_drive(void);
bool common_hal_storage_enable_usb_drive(void);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_STORAGE___INIT___H

View File

@ -39,60 +39,77 @@
//| The `usb_cdc` module allows access to USB CDC (serial) communications.
//|
//| 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.
//| be assigned consecutively, `console` first, but this is not always true.
//|
//| On Linux, the ports are typically ``/dev/ttyACM0`` and ``/dev/ttyACM1``. The REPL
//| is usually first.
//| On Linux, the ports are typically ``/dev/ttyACM0`` and ``/dev/ttyACM1``.
//| The `console` port will usually be 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.
//| varies based on the USB bus and port used. The `console` port will usually be first.
//| """
//|
//| repl: Optional[Serial]
//| """The `Serial` object that can be used to communicate over the REPL serial
//| channel. ``None`` if disabled.
//| console: Optional[Serial]
//| """The `console` `Serial` object is used for the REPL, and for `sys.stdin` and `sys.stdout`.
//| `console` is ``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."""
//| However, note that`sys.stdin` and `sys.stdout` are text-based streams,
//| and the `console` object is a binary stream.
//| You do not normally need to write to `console` unless you wnat to write binary data.
//| """
//|
//| 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."""
//| Note that `data` is *disabled* by default. ``data`` is ``None`` if disabled."""
//| def configure_usb(*, repl_enabled: bool = True, data_enabled: bool = False) -> None:
//| """Configure the USB CDC devices. Can be called in ``boot.py``, before USB is connected.
//|
//| :param repl_enabled bool: Enable or disable the `repl` USB serial device.
//| True to enable; False to disable. Enabled by default.
//| :param data_enabled bool: Enable or disable the `data` USB serial device.
//| True to enable; False to disable. *Disabled* by default."""
//| def disable() -> None:
//| """Do not present any USB CDC device to the host.
//| Can be called in ``boot.py``, before USB is connected.
//| Equivalent to ``usb_cdc.enable(console=False, data=False)``."""
//| ...
//|
STATIC mp_obj_t usb_cdc_configure_usb(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_repl_enabled, ARG_data_enabled };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_repl_enabled, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true } },
{ MP_QSTR_data_enabled, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false } },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
if (!common_hal_usb_cdc_configure_usb(args[ARG_repl_enabled].u_bool, args[ARG_data_enabled].u_bool)) {
STATIC mp_obj_t usb_cdc_disable(void) {
if (!common_hal_usb_cdc_disable()) {
mp_raise_RuntimeError(translate("Cannot change USB devices now"));
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(usb_cdc_configure_usb_obj, 0, usb_cdc_configure_usb);
MP_DEFINE_CONST_FUN_OBJ_0(usb_cdc_disable_obj, usb_cdc_disable);
// The usb_cdc module dict is mutable so that .repl and .data may
//| def enable(*, console: bool = True, data: bool = False) -> None:
//| """Enable or disable each CDC device. Can be called in ``boot.py``, before USB is connected.
//|
//| :param console bool: Enable or disable the `console` USB serial device.
//| True to enable; False to disable. Enabled by default.
//| :param data bool: Enable or disable the `data` USB serial device.
//| True to enable; False to disable. *Disabled* by default."""
//| ...
//|
STATIC mp_obj_t usb_cdc_enable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_console, ARG_data };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_console, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true } },
{ MP_QSTR_data, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false } },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
if (!common_hal_usb_cdc_enable(args[ARG_console].u_bool, args[ARG_data].u_bool)) {
mp_raise_RuntimeError(translate("Cannot change USB devices now"));
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(usb_cdc_enable_obj, 0, usb_cdc_enable);
// The usb_cdc module dict is mutable so that .console 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_configure_usb), MP_OBJ_FROM_PTR(&usb_cdc_configure_usb_obj) },
{ 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_console), mp_const_none },
{ MP_ROM_QSTR(MP_QSTR_data), mp_const_none },
{ MP_ROM_QSTR(MP_QSTR_disable), MP_OBJ_FROM_PTR(&usb_cdc_disable_obj) },
{ MP_ROM_QSTR(MP_QSTR_enable), MP_OBJ_FROM_PTR(&usb_cdc_enable_obj) },
};
static MP_DEFINE_MUTABLE_DICT(usb_cdc_module_globals, usb_cdc_module_globals_table);
@ -109,8 +126,8 @@ static void set_module_dict_entry(mp_obj_t key_qstr, mp_obj_t serial_obj) {
}
}
void usb_cdc_set_repl(mp_obj_t serial_obj) {
set_module_dict_entry(MP_ROM_QSTR(MP_QSTR_repl), serial_obj);
void usb_cdc_set_console(mp_obj_t serial_obj) {
set_module_dict_entry(MP_ROM_QSTR(MP_QSTR_console), serial_obj);
}
void usb_cdc_set_data(mp_obj_t serial_obj) {

View File

@ -30,9 +30,10 @@
#include "shared-module/usb_cdc/__init__.h"
// Set the module dict entries.
void usb_cdc_set_repl(mp_obj_t serial_obj);
void usb_cdc_set_console(mp_obj_t serial_obj);
void usb_cdc_set_data(mp_obj_t serial_obj);
extern bool common_hal_usb_cdc_configure_usb(bool repl_enabled, bool data_enabled);
extern bool common_hal_usb_cdc_disable(void);
extern bool common_hal_usb_cdc_enable(bool console, bool data);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_USB_CDC___INIT___H

View File

@ -40,8 +40,8 @@
//|
//| def __init__(self, *, descriptor: ReadableBuffer, usage_page: int, usage: int, in_report_length: int, out_report_length: int = 0, report_id_index: Optional[int]) -> None:
//| """Create a description of a USB HID device. To create an actual device,
//| pass a `Device` to `usb_hid.configure_usb()`.
//| """Create a description of a USB HID device. The actual device is created when you
//| pass a `Device` to `usb_hid.enable()`.
//|
//| :param ReadableBuffer report_descriptor: The USB HID Report descriptor bytes. The descriptor is not
//| not verified for correctness; it is up to you to make sure it is not malformed.

View File

@ -40,8 +40,19 @@
//| """Tuple of all active HID device interfaces."""
//|
//| def configure_usb(devices: Optional[Sequence[Device]]) -> None:
//| """Configure the USB HID devices that will be available.
//| def disable() -> None:
//| """Do not present any USB HID devices to the host computer.
//| Can be called in ``boot.py``, before USB is connected."""
STATIC mp_obj_t usb_hid_disable(void) {
if (!common_hal_usb_hid_disable()) {
mp_raise_RuntimeError(translate("Cannot change USB devices now"));
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_0(usb_hid_disable_obj, usb_hid_disable);
//| def enable(devices: Optional[Sequence[Device]]) -> None:
//| """Specify which USB HID devices that will be available.
//| Can be called in ``boot.py``, before USB is connected.
//|
//| :param Sequence devices: `Device` objects.
@ -51,7 +62,7 @@
//| """
//| ...
//|
STATIC mp_obj_t usb_hid_configure_usb(mp_obj_t devices) {
STATIC mp_obj_t usb_hid_enable(mp_obj_t devices) {
const mp_int_t len = mp_obj_get_int(mp_obj_len(devices));
for (mp_int_t i = 0; i < len; i++) {
mp_obj_t item = mp_obj_subscr(devices, MP_OBJ_NEW_SMALL_INT(i), MP_OBJ_SENTINEL);
@ -60,20 +71,21 @@ STATIC mp_obj_t usb_hid_configure_usb(mp_obj_t devices) {
}
}
if (!common_hal_usb_hid_configure_usb(devices)) {
if (!common_hal_usb_hid_enable(devices)) {
mp_raise_RuntimeError(translate("Cannot change USB devices now"));
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(usb_hid_configure_usb_obj, usb_hid_configure_usb);
MP_DEFINE_CONST_FUN_OBJ_1(usb_hid_enable_obj, usb_hid_enable);
// usb_hid.devices is set once the usb devices are determined, after boot.py runs.
STATIC mp_map_elem_t usb_hid_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid) },
{ MP_ROM_QSTR(MP_QSTR_configure_usb), MP_OBJ_FROM_PTR(&usb_hid_configure_usb_obj) },
{ MP_ROM_QSTR(MP_QSTR_devices), mp_const_none },
{ MP_ROM_QSTR(MP_QSTR_Device), MP_OBJ_FROM_PTR(&usb_hid_device_type) },
{ MP_ROM_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid) },
{ MP_ROM_QSTR(MP_QSTR_Device), MP_OBJ_FROM_PTR(&usb_hid_device_type) },
{ MP_ROM_QSTR(MP_QSTR_devices), mp_const_none },
{ MP_ROM_QSTR(MP_QSTR_disable), MP_OBJ_FROM_PTR(&usb_hid_disable_obj) },
{ MP_ROM_QSTR(MP_QSTR_enable), MP_OBJ_FROM_PTR(&usb_hid_enable_obj) },
};
STATIC MP_DEFINE_MUTABLE_DICT(usb_hid_module_globals, usb_hid_module_globals_table);

View File

@ -35,6 +35,7 @@ extern mp_obj_tuple_t common_hal_usb_hid_devices;
void usb_hid_set_devices(mp_obj_t devices);
bool common_hal_usb_hid_configure_usb(const mp_obj_t devices_seq);
bool common_hal_usb_hid_disable(void);
bool common_hal_usb_hid_enable(const mp_obj_t devices_seq);
#endif // SHARED_BINDINGS_USB_HID_H

View File

@ -43,28 +43,41 @@
//| """Tuple of all MIDI ports. Each item is ether `PortIn` or `PortOut`."""
//|
//| def configure_usb(enabled: bool = True) -> None:
//| """Configure the USB MIDI device.
//| Can be called in ``boot.py``, before USB is connected.
//|
//| :param enabled bool: Enable or disable the USB MIDI Device.
//| True to enable; False to disable. Enabled by default."""
//| def disable() -> None:
//| """Disable presenting a USB MIDI device to the host.
//| The device is normally enabled by default.
//| Can be called in ``boot.py``, before USB is connected."""
//| ...
//|
STATIC mp_obj_t usb_midi_configure_usb(mp_obj_t enabled) {
if (!common_hal_usb_midi_configure_usb(mp_obj_is_true(enabled))) {
STATIC mp_obj_t usb_midi_disable(void) {
if (!common_hal_usb_midi_disable()) {
mp_raise_RuntimeError(translate("Cannot change USB devices now"));
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(usb_midi_configure_usb_obj, usb_midi_configure_usb);
MP_DEFINE_CONST_FUN_OBJ_0(usb_midi_disable_obj, usb_midi_disable);
//| def enable() -> None:
//| """Enable presenting a USB MIDI device to the host.
//| The device is enabled by default, so you do not normally need to call this function.
//| Can be called in ``boot.py``, before USB is connected."""
//| ...
//|
STATIC mp_obj_t usb_midi_enable(void) {
if (!common_hal_usb_midi_enable()) {
mp_raise_RuntimeError(translate("Cannot change USB devices now"));
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_0(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_configure_usb), MP_OBJ_FROM_PTR(&usb_midi_configure_usb_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___name__), MP_ROM_QSTR(MP_QSTR_usb_midi) },
{ MP_ROM_QSTR(MP_QSTR_disable), MP_OBJ_FROM_PTR(&usb_midi_disable_obj) },
{ 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) },
};
// This isn't const so we can set ports dynamically.

View File

@ -32,6 +32,7 @@
extern mp_obj_dict_t usb_midi_module_globals;
bool common_hal_usb_midi_configure_usb(bool enabled);
bool common_hal_usb_midi_disable(void);
bool common_hal_usb_midi_enable(void);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_USB_MIDI___INIT___H

View File

@ -109,7 +109,7 @@ size_t storage_usb_add_descriptor(uint8_t *descriptor_buf, uint8_t *current_inte
return sizeof(usb_msc_descriptor_template);
}
bool common_hal_storage_configure_usb(bool enabled) {
static bool usb_drive_set_enabled(bool enabled) {
// We can't change the descriptors once we're connected.
if (tud_connected()) {
return false;
@ -117,6 +117,14 @@ bool common_hal_storage_configure_usb(bool enabled) {
storage_usb_is_enabled = enabled;
return true;
}
bool common_hal_storage_disable_usb_drive(void) {
return usb_drive_set_enabled(false);
}
bool common_hal_storage_enable_usb_drive(void) {
return usb_drive_set_enabled(true);
}
#endif // CIRCUITPY_USB_MSC
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) {

View File

@ -134,12 +134,12 @@ static const uint8_t usb_cdc_descriptor_template[] = {
0x00, // 65 bInterval 0 (unit depends on device speed)
};
static const char repl_cdc_comm_interface_name[] = USB_INTERFACE_NAME " CDC control";
static const char console_cdc_comm_interface_name[] = USB_INTERFACE_NAME " CDC control";
static const char data_cdc_comm_interface_name[] = USB_INTERFACE_NAME " CDC2 control";
static const char repl_cdc_data_interface_name[] = USB_INTERFACE_NAME " CDC data";
static const char console_cdc_data_interface_name[] = USB_INTERFACE_NAME " CDC data";
static const char data_cdc_data_interface_name[] = USB_INTERFACE_NAME " CDC2 data";
static usb_cdc_serial_obj_t usb_cdc_repl_obj = {
static usb_cdc_serial_obj_t usb_cdc_console_obj = {
.base.type = &usb_cdc_serial_type,
.timeout = -1.0f,
.write_timeout = -1.0f,
@ -153,16 +153,16 @@ static usb_cdc_serial_obj_t usb_cdc_data_obj = {
.idx = 1,
};
static bool usb_cdc_repl_is_enabled;
static bool usb_cdc_console_is_enabled;
static bool usb_cdc_data_is_enabled;
void usb_cdc_set_defaults(void) {
usb_cdc_repl_is_enabled = CIRCUITPY_USB_CDC_REPL_ENABLED_DEFAULT;
usb_cdc_console_is_enabled = CIRCUITPY_USB_CDC_CONSOLE_ENABLED_DEFAULT;
usb_cdc_data_is_enabled = CIRCUITPY_USB_CDC_DATA_ENABLED_DEFAULT;
}
bool usb_cdc_repl_enabled(void) {
return usb_cdc_repl_is_enabled;
bool usb_cdc_console_enabled(void) {
return usb_cdc_console_is_enabled;
}
bool usb_cdc_data_enabled(void) {
@ -173,7 +173,7 @@ size_t usb_cdc_descriptor_length(void) {
return sizeof(usb_cdc_descriptor_template);
}
size_t usb_cdc_add_descriptor(uint8_t *descriptor_buf, uint8_t *current_interface, uint8_t *current_endpoint, uint8_t *current_interface_string, bool repl) {
size_t usb_cdc_add_descriptor(uint8_t *descriptor_buf, uint8_t *current_interface, uint8_t *current_endpoint, uint8_t *current_interface_string, bool console) {
memcpy(descriptor_buf, usb_cdc_descriptor_template, sizeof(usb_cdc_descriptor_template));
// Store comm interface number.
@ -189,36 +189,40 @@ size_t usb_cdc_add_descriptor(uint8_t *descriptor_buf, uint8_t *current_interfac
(*current_interface)++;
descriptor_buf[CDC_CONTROL_IN_ENDPOINT_INDEX] = 0x80 | (
repl
console
? (USB_CDC_EP_NUM_NOTIFICATION ? USB_CDC_EP_NUM_NOTIFICATION : *current_endpoint)
: (USB_CDC2_EP_NUM_NOTIFICATION ? USB_CDC2_EP_NUM_NOTIFICATION : *current_endpoint));
(*current_endpoint)++;
descriptor_buf[CDC_DATA_OUT_ENDPOINT_INDEX] =
repl
console
? (USB_CDC_EP_NUM_DATA_OUT ? USB_CDC_EP_NUM_DATA_OUT : *current_endpoint)
: (USB_CDC2_EP_NUM_DATA_OUT ? USB_CDC2_EP_NUM_DATA_OUT : *current_endpoint);
descriptor_buf[CDC_DATA_IN_ENDPOINT_INDEX] = 0x80 | (
repl
console
? (USB_CDC_EP_NUM_DATA_IN ? USB_CDC_EP_NUM_DATA_IN : *current_endpoint)
: (USB_CDC2_EP_NUM_DATA_IN ? USB_CDC2_EP_NUM_DATA_IN : *current_endpoint));
(*current_endpoint)++;
usb_add_interface_string(*current_interface_string,
repl ? repl_cdc_comm_interface_name : data_cdc_comm_interface_name);
console ? console_cdc_comm_interface_name : data_cdc_comm_interface_name);
descriptor_buf[CDC_COMM_INTERFACE_STRING_INDEX] = *current_interface_string;
(*current_interface_string)++;
usb_add_interface_string(*current_interface_string,
repl ? repl_cdc_data_interface_name : data_cdc_data_interface_name);
console ? console_cdc_data_interface_name : data_cdc_data_interface_name);
descriptor_buf[CDC_DATA_INTERFACE_STRING_INDEX] = *current_interface_string;
(*current_interface_string)++;
return sizeof(usb_cdc_descriptor_template);
}
bool common_hal_usb_cdc_configure_usb(bool repl_enabled, bool data_enabled) {
bool common_hal_usb_cdc_disable(void) {
return common_hal_usb_cdc_enable(false, false);
}
bool common_hal_usb_cdc_enable(bool console, bool data) {
// We can't change the descriptors once we're connected.
if (tud_connected()) {
return false;
@ -227,11 +231,11 @@ bool common_hal_usb_cdc_configure_usb(bool repl_enabled, bool data_enabled) {
// Right now these objects contain no heap objects, but if that changes,
// they will need to be protected against gc.
usb_cdc_repl_is_enabled = repl_enabled;
usb_cdc_set_repl(repl_enabled ? MP_OBJ_FROM_PTR(&usb_cdc_repl_obj) : mp_const_none);
usb_cdc_console_is_enabled = console;
usb_cdc_set_console(console ? MP_OBJ_FROM_PTR(&usb_cdc_console_obj) : mp_const_none);
usb_cdc_data_is_enabled = data_enabled;
usb_cdc_set_data(data_enabled ? MP_OBJ_FROM_PTR(&usb_cdc_data_obj) : mp_const_none);
usb_cdc_data_is_enabled = data;
usb_cdc_set_data(data ? MP_OBJ_FROM_PTR(&usb_cdc_data_obj) : mp_const_none);
return true;
}

View File

@ -30,7 +30,7 @@
#include "py/mpconfig.h"
#include "py/objtuple.h"
bool usb_cdc_repl_enabled(void);
bool usb_cdc_console_enabled(void);
bool usb_cdc_data_enabled(void);
void usb_cdc_set_defaults(void);

View File

@ -74,13 +74,11 @@ static const uint8_t usb_hid_descriptor_template[] = {
0x08, // 31 bInterval 8 (unit depends on device speed)
};
// Is the HID device enabled?
static bool usb_hid_is_enabled;
#define MAX_HID_DEVICES 8
static supervisor_allocation *hid_report_descriptor_allocation;
static usb_hid_device_obj_t hid_devices[MAX_HID_DEVICES];
// If 0, USB HID is disabled.
static mp_int_t hid_devices_num;
// This tuple is store in usb_hid.devices.
@ -99,12 +97,12 @@ static mp_obj_tuple_t default_hid_devices_tuple = {
};
bool usb_hid_enabled(void) {
return usb_hid_is_enabled;
return hid_devices_num == 0;
}
void usb_hid_set_defaults(void) {
usb_hid_is_enabled = CIRCUITPY_USB_HID_ENABLED_DEFAULT;
common_hal_usb_hid_configure_usb(usb_hid_is_enabled ? &default_hid_devices_tuple : mp_const_empty_tuple);
common_hal_usb_hid_enable(
CIRCUITPY_USB_HID_ENABLED_DEFAULT ? &default_hid_devices_tuple : mp_const_empty_tuple);
}
// This is the interface descriptor, not the report descriptor.
@ -148,7 +146,11 @@ static void usb_hid_set_devices_from_hid_devices(void) {
usb_hid_set_devices(hid_devices_tuple);
}
bool common_hal_usb_hid_configure_usb(const mp_obj_t devices) {
bool common_hal_usb_hid_disable(void) {
return common_hal_usb_hid_enable(mp_const_empty_tuple);
}
bool common_hal_usb_hid_enable(const mp_obj_t devices) {
// We can't change the devices once we're connected.
if (tud_connected()) {
return false;
@ -198,6 +200,10 @@ size_t usb_hid_report_descriptor_length(void) {
// Build the combined HID report descriptor in the given space.
void usb_hid_build_report_descriptor(uint8_t *report_descriptor_space, size_t report_descriptor_length) {
if (!usb_hid_enabled()) {
return;
}
uint8_t *report_descriptor_start = report_descriptor_space;
for (mp_int_t i = 0; i < hid_devices_num; i++) {
@ -229,7 +235,11 @@ void usb_hid_build_report_descriptor(uint8_t *report_descriptor_space, size_t re
// Call this after the heap and VM are finished.
void usb_hid_save_report_descriptor(uint8_t *report_descriptor_space, size_t report_descriptor_length) {
// Allocate storage that persists across VMs to hold the combind report descriptor.
if (!usb_hid_enabled()) {
return;
}
// Allocate storage that persists across VMs to hold the combined report descriptor.
// and to remember the device details.
// Copy the descriptor from the temporary area to a supervisor storage allocation that

View File

@ -242,7 +242,7 @@ void usb_midi_setup_ports(void) {
MP_OBJ_FROM_PTR(ports);
}
bool common_hal_usb_midi_configure_usb(bool enabled) {
static bool usb_midi_set_enabled(bool enabled) {
// We can't change the descriptors once we're connected.
if (tud_connected()) {
return false;
@ -250,3 +250,11 @@ bool common_hal_usb_midi_configure_usb(bool enabled) {
usb_midi_is_enabled = enabled;
return true;
}
bool common_hal_usb_midi_disable(void) {
return usb_midi_set_enabled(false);
}
bool common_hal_usb_midi_enable(void) {
return usb_midi_set_enabled(true);
}

View File

@ -94,7 +94,7 @@ bool serial_connected(void) {
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
return true;
#elif CIRCUITPY_USB_CDC
return usb_cdc_repl_enabled() && tud_cdc_connected();
return usb_cdc_console_enabled() && tud_cdc_connected();
#else
return tud_cdc_connected();
#endif
@ -118,7 +118,7 @@ char serial_read(void) {
common_hal_busio_uart_read(&debug_uart, (uint8_t *)&text, 1, &uart_errcode);
return text;
#elif CIRCUITPY_USB_CDC
if (!usb_cdc_repl_enabled()) {
if (!usb_cdc_console_enabled()) {
return -1;
}
#endif
@ -135,7 +135,7 @@ bool serial_bytes_available(void) {
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
return common_hal_busio_uart_rx_characters_available(&debug_uart) || (tud_cdc_available() > 0);
#elif CIRCUITPY_USB_CDC
if (!usb_cdc_repl_enabled()) {
if (!usb_cdc_console_enabled()) {
return 0;
}
#endif
@ -157,7 +157,7 @@ void serial_write_substring(const char *text, uint32_t length) {
#endif
#if CIRCUITPY_USB_CDC
if (!usb_cdc_repl_enabled()) {
if (!usb_cdc_console_enabled()) {
return;
}
#endif

View File

@ -143,7 +143,7 @@ static void usb_build_configuration_descriptor(void) {
// CDC should be first, for compatibility with Adafruit Windows 7 drivers.
// In the past, the order has been CDC, MSC, MIDI, HID, so preserve that order.
#if CIRCUITPY_USB_CDC
if (usb_cdc_repl_enabled()) {
if (usb_cdc_console_enabled()) {
total_descriptor_length += usb_cdc_descriptor_length();
}
if (usb_cdc_data_enabled()) {
@ -190,7 +190,7 @@ static void usb_build_configuration_descriptor(void) {
uint8_t *descriptor_buf_remaining = configuration_descriptor + sizeof(configuration_descriptor_template);
#if CIRCUITPY_USB_CDC
if (usb_cdc_repl_enabled()) {
if (usb_cdc_console_enabled()) {
// Concatenate and fix up the CDC REPL descriptor.
descriptor_buf_remaining += usb_cdc_add_descriptor(
descriptor_buf_remaining, &current_interface, &current_endpoint, &current_interface_string, true);