2021-02-12 19:01:14 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2021 Dan Halbertfor 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
|
|
|
#include "shared-bindings/usb_cdc/__init__.h"
|
|
|
|
#include "shared-bindings/usb_cdc/Serial.h"
|
2022-05-27 15:59:54 -04:00
|
|
|
#include "supervisor/shared/translate/translate.h"
|
2021-02-12 19:01:14 -05:00
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
|
|
|
//| """USB CDC Serial streams
|
|
|
|
//|
|
2021-04-13 23:33:44 -04:00
|
|
|
//| The `usb_cdc` module allows access to USB CDC (serial) communications.
|
2021-02-12 19:01:14 -05:00
|
|
|
//|
|
2021-04-13 23:33:44 -04:00
|
|
|
//| On Windows, each `Serial` is visible as a separate COM port. The ports will often
|
2021-05-03 22:22:29 -04:00
|
|
|
//| be assigned consecutively, `console` first, but this is not always true.
|
2021-04-13 23:33:44 -04:00
|
|
|
//|
|
2021-05-03 22:22:29 -04:00
|
|
|
//| On Linux, the ports are typically ``/dev/ttyACM0`` and ``/dev/ttyACM1``.
|
|
|
|
//| The `console` port will usually be first.
|
2021-04-13 23:33:44 -04:00
|
|
|
//|
|
|
|
|
//| On MacOS, the ports are typically ``/dev/cu.usbmodem<something>``. The something
|
2021-05-03 22:22:29 -04:00
|
|
|
//| varies based on the USB bus and port used. The `console` port will usually be first.
|
2021-02-16 17:11:37 -05:00
|
|
|
//| """
|
2021-02-12 19:01:14 -05:00
|
|
|
//|
|
2021-05-03 22:22:29 -04:00
|
|
|
//| console: Optional[Serial]
|
|
|
|
//| """The `console` `Serial` object is used for the REPL, and for `sys.stdin` and `sys.stdout`.
|
|
|
|
//| `console` is ``None`` if disabled.
|
2021-04-13 23:33:44 -04:00
|
|
|
//|
|
2021-06-01 20:15:51 -04:00
|
|
|
//| However, note that `sys.stdin` and `sys.stdout` are text-based streams,
|
2021-05-03 22:22:29 -04:00
|
|
|
//| and the `console` object is a binary stream.
|
2021-06-01 20:15:51 -04:00
|
|
|
//| You do not normally need to write to `console` unless you want to write binary data.
|
2021-05-03 22:22:29 -04:00
|
|
|
//| """
|
2021-04-13 23:33:44 -04:00
|
|
|
//|
|
|
|
|
//| data: Optional[Serial]
|
|
|
|
//| """A `Serial` object that can be used to send and receive binary data to and from
|
|
|
|
//| the host.
|
2021-05-03 22:22:29 -04:00
|
|
|
//| Note that `data` is *disabled* by default. ``data`` is ``None`` if disabled."""
|
|
|
|
|
|
|
|
//| 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_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_0(usb_cdc_disable_obj, usb_cdc_disable);
|
2021-04-13 23:33:44 -04:00
|
|
|
|
2021-05-03 22:22:29 -04:00
|
|
|
//| 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.
|
2021-04-13 23:33:44 -04:00
|
|
|
//|
|
2021-05-03 22:22:29 -04:00
|
|
|
//| :param console bool: Enable or disable the `console` USB serial device.
|
2021-04-14 22:10:09 -04:00
|
|
|
//| True to enable; False to disable. Enabled by default.
|
2021-05-03 22:22:29 -04:00
|
|
|
//| :param data bool: Enable or disable the `data` USB serial device.
|
2021-05-13 21:49:04 -04:00
|
|
|
//| True to enable; False to disable. *Disabled* by default.
|
|
|
|
//|
|
|
|
|
//| If you enable too many devices at once, you will run out of USB endpoints.
|
|
|
|
//| The number of available endpoints varies by microcontroller.
|
|
|
|
//| CircuitPython will go into safe mode after running boot.py to inform you if
|
|
|
|
//| not enough endpoints are available.
|
|
|
|
//| """
|
2021-04-13 23:33:44 -04:00
|
|
|
//| ...
|
2021-05-03 22:22:29 -04:00
|
|
|
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 };
|
2021-04-29 22:26:38 -04:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
2021-05-03 22:22:29 -04:00
|
|
|
{ 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 } },
|
2021-04-29 22:26:38 -04:00
|
|
|
};
|
|
|
|
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);
|
|
|
|
|
2021-05-03 22:22:29 -04:00
|
|
|
if (!common_hal_usb_cdc_enable(args[ARG_console].u_bool, args[ARG_data].u_bool)) {
|
2021-04-13 23:33:44 -04:00
|
|
|
mp_raise_RuntimeError(translate("Cannot change USB devices now"));
|
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2021-05-03 22:22:29 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(usb_cdc_enable_obj, 0, usb_cdc_enable);
|
2021-02-12 19:01:14 -05:00
|
|
|
|
2021-05-03 22:22:29 -04:00
|
|
|
// The usb_cdc module dict is mutable so that .console and .data may
|
2021-04-13 23:33:44 -04:00
|
|
|
// 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[] = {
|
2021-05-03 22:22:29 -04:00
|
|
|
{ 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) },
|
2021-02-12 19:01:14 -05:00
|
|
|
};
|
|
|
|
|
2021-04-13 23:33:44 -04:00
|
|
|
static MP_DEFINE_MUTABLE_DICT(usb_cdc_module_globals, usb_cdc_module_globals_table);
|
2021-02-12 19:01:14 -05:00
|
|
|
|
|
|
|
const mp_obj_module_t usb_cdc_module = {
|
|
|
|
.base = { &mp_type_module },
|
2021-03-15 09:57:36 -04:00
|
|
|
.globals = (mp_obj_dict_t *)&usb_cdc_module_globals,
|
2021-02-12 19:01:14 -05:00
|
|
|
};
|
2021-04-14 22:10:09 -04:00
|
|
|
|
|
|
|
static void set_module_dict_entry(mp_obj_t key_qstr, mp_obj_t serial_obj) {
|
|
|
|
mp_map_elem_t *elem = mp_map_lookup(&usb_cdc_module_globals.map, key_qstr, MP_MAP_LOOKUP);
|
|
|
|
if (elem) {
|
|
|
|
elem->value = serial_obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-03 22:22:29 -04:00
|
|
|
void usb_cdc_set_console(mp_obj_t serial_obj) {
|
|
|
|
set_module_dict_entry(MP_ROM_QSTR(MP_QSTR_console), serial_obj);
|
2021-04-14 22:10:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void usb_cdc_set_data(mp_obj_t serial_obj) {
|
|
|
|
set_module_dict_entry(MP_ROM_QSTR(MP_QSTR_data), serial_obj);
|
|
|
|
}
|
Convert more modules to use MP_REGISTER_MODULE
Convert neopixel_write, onewireio, ps2io, pulseio, pwmio, rainbowio, random, rgbmatrix, rotaryio, rtc, sdcardio, sharpdisplay, _stage, storage, struct, supervisor, synthio, touchio, traceback, usb_cdc, usb_hid, usb_midi, and vectorio modules to use MP_REGISTER_MODULE.
Related to #5183.
2021-08-30 22:29:51 -04:00
|
|
|
|
|
|
|
MP_REGISTER_MODULE(MP_QSTR_usb_cdc, usb_cdc_module, CIRCUITPY_USB_CDC);
|