Merge pull request #4215 from dhalbert/secondary-cdc

Second USB CDC (serial) channel
This commit is contained in:
Scott Shawcroft 2021-02-19 18:34:03 -08:00 committed by GitHub
commit 7b6f89a074
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
53 changed files with 1797 additions and 684 deletions

26
main.c
View File

@ -69,6 +69,19 @@
#include "shared-bindings/alarm/__init__.h"
#endif
#if CIRCUITPY_BLEIO
#include "shared-bindings/_bleio/__init__.h"
#include "supervisor/shared/bluetooth.h"
#endif
#if CIRCUITPY_BOARD
#include "shared-module/board/__init__.h"
#endif
#if CIRCUITPY_CANIO
#include "common-hal/canio/CAN.h"
#endif
#if CIRCUITPY_DISPLAYIO
#include "shared-module/displayio/__init__.h"
#endif
@ -81,17 +94,8 @@
#include "shared-module/network/__init__.h"
#endif
#if CIRCUITPY_BOARD
#include "shared-module/board/__init__.h"
#endif
#if CIRCUITPY_BLEIO
#include "shared-bindings/_bleio/__init__.h"
#include "supervisor/shared/bluetooth.h"
#endif
#if CIRCUITPY_CANIO
#include "common-hal/canio/CAN.h"
#if CIRCUITPY_USB_CDC
#include "shared-module/usb_cdc/__init__.h"
#endif
#if CIRCUITPY_WIFI

View File

@ -122,7 +122,7 @@ CFLAGS += -ftree-vrp
$(echo PERIPHERALS_CHIP_FAMILY=$(PERIPHERALS_CHIP_FAMILY))
#Debugging/Optimization
ifeq ($(DEBUG), 1)
CFLAGS += -ggdb3 -Og
CFLAGS += -ggdb3 -Og -Os
# You may want to disable -flto if it interferes with debugging.
CFLAGS += -flto -flto-partition=none
# You may want to enable these flags to make setting breakpoints easier.

View File

@ -11,3 +11,4 @@ LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0
SUPEROPT_GC = 0
SUPEROPT_VM = 0

View File

@ -28,10 +28,10 @@
#include "shared-bindings/supervisor/Runtime.h"
#include "supervisor/serial.h"
bool common_hal_get_serial_connected(void) {
bool common_hal_supervisor_runtime_get_serial_connected(void) {
return (bool) serial_connected();
}
bool common_hal_get_serial_bytes_available(void) {
bool common_hal_supervisor_runtime_get_serial_bytes_available(void) {
return (bool) serial_bytes_available();
}

View File

@ -18,7 +18,16 @@ ifeq ($(LONGINT_IMPL),LONGLONG)
MPY_TOOL_LONGINT_IMPL = -mlongint-impl=longlong
endif
INTERNAL_LIBM = 1
USB_SERIAL_NUMBER_LENGTH = 32
# Number of USB endpoint pairs.
USB_NUM_EP = 8
######################################################################
# Put samd21-only choices here.
ifeq ($(CHIP_FAMILY),samd21)
# The ?='s allow overriding in mpconfigboard.mk.
@ -40,7 +49,9 @@ CIRCUITPY_SDCARDIO ?= 0
CIRCUITPY_FRAMEBUFFERIO ?= 0
# SAMD21 needs separate endpoint pairs for MSC BULK IN and BULK OUT, otherwise it's erratic.
# Because of that, there aren't enough endpoints for a secondary CDC serial connection.
USB_MSC_EP_NUM_OUT = 1
CIRCUITPY_USB_CDC = 0
CIRCUITPY_ULAB = 0
@ -55,9 +66,13 @@ CIRCUITPY_TERMINALIO = 0
endif
endif # samd21
######################################################################
######################################################################
# Put samd51-only choices here.
ifeq ($(CHIP_FAMILY),samd51)
# No native touchio on SAMD51.
CIRCUITPY_TOUCHIO_USE_NATIVE = 0
@ -70,9 +85,4 @@ CIRCUITPY_RGBMATRIX ?= $(CIRCUITPY_FULL_BUILD)
CIRCUITPY_FRAMEBUFFERIO ?= $(CIRCUITPY_FULL_BUILD)
endif # samd51
INTERNAL_LIBM = 1
USB_SERIAL_NUMBER_LENGTH = 32
USB_NUM_EP = 8
######################################################################

View File

@ -27,10 +27,10 @@
#include "shared-bindings/supervisor/Runtime.h"
#include "supervisor/serial.h"
bool common_hal_get_serial_connected(void) {
bool common_hal_supervisor_runtime_get_serial_connected(void) {
return (bool) serial_connected();
}
bool common_hal_get_serial_bytes_available(void) {
bool common_hal_supervisor_runtime_get_serial_bytes_available(void) {
return (bool) serial_bytes_available();
}

View File

@ -7,6 +7,9 @@ USB_CDC_EP_NUM_DATA_IN = 1
USB_MSC_EP_NUM_OUT = 5
USB_MSC_EP_NUM_IN = 4
# Number of USB endpoint pairs.
USB_NUM_EP = 6
MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz
CIRCUITPY_AUDIOBUSIO = 0

View File

@ -2,7 +2,6 @@ USB_VID = 0x303a
USB_PID = 0x8007
USB_PRODUCT = "TTGO T8 ESP32-S2"
USB_MANUFACTURER = "LILYGO"
USB_DEVICES = "CDC,MSC,HID"
INTERNAL_FLASH_FILESYSTEM = 1
LONGINT_IMPL = MPZ

View File

@ -2,7 +2,6 @@ USB_VID = 0x239A
USB_PID = 0x80C6
USB_PRODUCT = "microS2"
USB_MANUFACTURER = "microDev"
USB_DEVICES = "CDC,MSC,HID"
INTERNAL_FLASH_FILESYSTEM = 1
LONGINT_IMPL = MPZ

View File

@ -2,7 +2,6 @@ USB_VID = 0x239A
USB_PID = 0x80AC
USB_PRODUCT = "FeatherS2"
USB_MANUFACTURER = "UnexpectedMaker"
USB_DEVICES = "CDC,MSC,HID"
INTERNAL_FLASH_FILESYSTEM = 1
LONGINT_IMPL = MPZ

View File

@ -2,7 +2,6 @@ USB_VID = 0x239A
USB_PID = 0x80AC
USB_PRODUCT = "FeatherS2"
USB_MANUFACTURER = "UnexpectedMaker"
USB_DEVICES = "CDC,MSC,HID"
INTERNAL_FLASH_FILESYSTEM = 1
LONGINT_IMPL = MPZ

View File

@ -28,10 +28,10 @@
#include "shared-bindings/supervisor/Runtime.h"
#include "supervisor/serial.h"
bool common_hal_get_serial_connected(void) {
bool common_hal_supervisor_runtime_get_serial_connected(void) {
return (bool) serial_connected();
}
bool common_hal_get_serial_bytes_available(void) {
return (bool) serial_bytes_available();
bool common_hal_supervisor_runtime_get_serial_bytes_available(void) {
return (bool) serial_bytes_available();
}

View File

@ -28,6 +28,8 @@ CIRCUITPY_FREQUENCYIO = 1
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_ROTARYIO = 1
CIRCUITPY_NVM = 1
CIRCUITPY_PS2IO ?= 1
CIRCUITPY_TOUCHIO_USE_NATIVE ?= 1
# We don't have enough endpoints to include MIDI.
CIRCUITPY_USB_MIDI ?= 0
CIRCUITPY_USB_HID ?= 1
@ -35,14 +37,9 @@ CIRCUITPY_USB_HID ?= 1
CIRCUITPY_USB_VENDOR ?= 0
CIRCUITPY_WIFI = 1
CIRCUITPY_WATCHDOG ?= 1
CIRCUITPY_ESPIDF = 1
ifndef CIRCUITPY_PS2IO
CIRCUITPY_PS2IO = 1
endif
ifndef CIRCUITPY_TOUCHIO_USE_NATIVE
CIRCUITPY_TOUCHIO_USE_NATIVE = 1
endif
CIRCUITPY_MODULE ?= none
USB_NUM_EP = 5

View File

@ -28,10 +28,10 @@
#include "shared-bindings/supervisor/Runtime.h"
#include "supervisor/serial.h"
bool common_hal_get_serial_connected(void) {
bool common_hal_supervisor_runtime_get_serial_connected(void) {
return (bool) serial_connected();
}
bool common_hal_get_serial_bytes_available(void) {
return (bool) serial_bytes_available();
bool common_hal_supervisor_runtime_get_serial_bytes_available(void) {
return (bool) serial_bytes_available();
}

View File

@ -6,6 +6,9 @@ MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz
# Internal math library is substantially smaller than toolchain one
INTERNAL_LIBM = 1
# Number of USB endpoint pairs.
USB_NUM_EP = 16
# Chip supplied serial number, in bytes
USB_SERIAL_NUMBER_LENGTH = 30

View File

@ -28,10 +28,10 @@
#include "shared-bindings/supervisor/Runtime.h"
#include "supervisor/serial.h"
bool common_hal_get_serial_connected(void) {
bool common_hal_supervisor_runtime_get_serial_connected(void) {
return (bool) serial_connected();
}
bool common_hal_get_serial_bytes_available(void) {
bool common_hal_supervisor_runtime_get_serial_bytes_available(void) {
return (bool) serial_bytes_available();
}

View File

@ -17,6 +17,9 @@ INTERNAL_LIBM = 1
USB_SERIAL_NUMBER_LENGTH = 32
USB_HIGHSPEED = 1
# Number of USB endpoint pairs.
USB_NUM_EP = 8
INTERNAL_FLASH_FILESYSTEM = 1
CIRCUITPY_AUDIOIO = 0

View File

@ -40,7 +40,7 @@
#include "shared-bindings/_bleio/Service.h"
#include "shared-bindings/_bleio/UUID.h"
#if CIRCUITPY_SERIAL_BLE
#if CIRCUITPY_REPL_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";
@ -190,4 +190,4 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) {
}
}
#endif // CIRCUITPY_SERIAL_BLE
#endif // CIRCUITPY_REPL_BLE

View File

@ -29,6 +29,7 @@ CIRCUITPY_BUSDEVICE = 0
MICROPY_PY_ASYNC_AWAIT = 0
SUPEROPT_GC = 0
SUPEROPT_VM = 0
# Override optimization to keep binary small
OPTIMIZATION_FLAGS = -Os

View File

@ -3,8 +3,6 @@ USB_PID = 0xc051
USB_PRODUCT = "Simmel"
USB_MANUFACTURER = "Betrusted"
CIRCUITPY_DEVICES="CDC,MSC,HID"
MCU_CHIP = nrf52833
# SPI_FLASH_FILESYSTEM = 1
@ -31,6 +29,7 @@ CIRCUITPY_RTC = 1
CIRCUITPY_SDCARDIO = 0
CIRCUITPY_TOUCHIO = 0
CIRCUITPY_ULAB = 0
CIRCUITPY_USB_CDC = 0
CIRCUITPY_USB_MIDI = 0
CIRCUITPY_WATCHDOG = 1

View File

@ -28,10 +28,10 @@
#include "shared-bindings/supervisor/Runtime.h"
#include "supervisor/serial.h"
bool common_hal_get_serial_connected(void) {
bool common_hal_supervisor_runtime_get_serial_connected(void) {
return (bool) serial_connected();
}
bool common_hal_get_serial_bytes_available(void) {
bool common_hal_supervisor_runtime_get_serial_bytes_available(void) {
return (bool) serial_bytes_available();
}

View File

@ -11,6 +11,9 @@ INTERNAL_LIBM = 1
USB_SERIAL_NUMBER_LENGTH = 16
# Number of USB endpoint pairs.
USB_NUM_EP = 8
# All nRF ports have longints.
LONGINT_IMPL = MPZ

View File

@ -28,15 +28,15 @@
#include "supervisor/serial.h"
#if CIRCUITPY_SERIAL_BLE
#if CIRCUITPY_REPL_BLE
#include "ble_uart.h"
#elif CIRCUITPY_SERIAL_UART
#elif CIRCUITPY_REPL_UART
#include <string.h>
#include "nrf_gpio.h"
#include "nrfx_uarte.h"
#endif
#if CIRCUITPY_SERIAL_BLE
#if CIRCUITPY_REPL_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_SERIAL_UART
#elif CIRCUITPY_REPL_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_SERIAL_UART
#endif // CIRCUITPY_REPL_UART

View File

@ -28,10 +28,10 @@
#include "shared-bindings/supervisor/Runtime.h"
#include "supervisor/serial.h"
bool common_hal_get_serial_connected(void) {
bool common_hal_supervisor_runtime_get_serial_connected(void) {
return (bool) serial_connected();
}
bool common_hal_get_serial_bytes_available(void) {
bool common_hal_supervisor_runtime_get_serial_bytes_available(void) {
return (bool) serial_bytes_available();
}

View File

@ -50,4 +50,5 @@ INTERNAL_LIBM = 1
USB_SERIAL_NUMBER_LENGTH = 16
# Number of USB endpoint pairs.
USB_NUM_EP = 8

View File

@ -16,5 +16,3 @@ MCU_PACKAGE = LQFP144
LD_COMMON = boards/common_default.ld
LD_FILE = boards/STM32F412_fs.ld
CIRCUITPY_USB_HID = 1

View File

@ -2,7 +2,9 @@ USB_VID = 0x239A
USB_PID = 0x8071
USB_PRODUCT = "Thunderpack STM32F411"
USB_MANUFACTURER = "Jeremy Gillick"
USB_DEVICES = "CDC,MSC"
# Turn off HID devices
CIRCUITPY_USB_HID = 0
LONGINT_IMPL = NONE

View File

@ -28,10 +28,10 @@
#include "shared-bindings/supervisor/Runtime.h"
#include "supervisor/serial.h"
bool common_hal_get_serial_connected(void) {
bool common_hal_supervisor_runtime_get_serial_connected(void) {
return (bool) serial_connected();
}
bool common_hal_get_serial_bytes_available(void) {
return (bool) serial_bytes_available();
bool common_hal_supervisor_runtime_get_serial_bytes_available(void) {
return (bool) serial_bytes_available();
}

View File

@ -8,6 +8,7 @@ ifeq ($(MCU_VARIANT),STM32F405xx)
CIRCUITPY_FRAMEBUFFERIO ?= 1
CIRCUITPY_RGBMATRIX ?= 1
CIRCUITPY_SDIOIO ?= 1
# Number of USB endpoint pairs.
USB_NUM_EP = 4
endif
@ -23,6 +24,8 @@ ifeq ($(MCU_SERIES),F4)
CIRCUITPY_RTC ?= 0
CIRCUITPY_USB_MIDI ?= 0
CIRCUITPY_USB_HID ?= 0
USB_NUM_EP = 4
endif
ifeq ($(MCU_SERIES),H7)
@ -40,6 +43,8 @@ ifeq ($(MCU_SERIES),H7)
CIRCUITPY_RTC ?= 0
CIRCUITPY_USB_HID ?= 0
CIRCUITPY_USB_MIDI ?= 0
USB_NUM_EP = 9
endif
ifeq ($(MCU_SERIES),F7)
@ -56,4 +61,6 @@ ifeq ($(MCU_SERIES),F7)
CIRCUITPY_RTC ?= 0
CIRCUITPY_USB_HID ?= 0
CIRCUITPY_USB_MIDI ?= 0
USB_NUM_EP = 6
endif

View File

@ -132,9 +132,6 @@ endif
ifeq ($(CIRCUITPY_AUDIOMP3),1)
SRC_PATTERNS += audiomp3/%
endif
ifeq ($(CIRCUITPY_BITOPS),1)
SRC_PATTERNS += bitops/%
endif
ifeq ($(CIRCUITPY_BITBANGIO),1)
SRC_PATTERNS += bitbangio/%
endif
@ -142,6 +139,9 @@ endif
ifeq ($(CIRCUITPY_BITBANG_APA102),1)
SRC_PATTERNS += bitbangio/SPI%
endif
ifeq ($(CIRCUITPY_BITOPS),1)
SRC_PATTERNS += bitops/%
endif
ifeq ($(CIRCUITPY_BLEIO),1)
SRC_PATTERNS += _bleio/%
endif
@ -289,6 +289,9 @@ endif
ifeq ($(CIRCUITPY_UHEAP),1)
SRC_PATTERNS += uheap/%
endif
ifeq ($(CIRCUITPY_USB_CDC),1)
SRC_PATTERNS += usb_cdc/%
endif
ifeq ($(CIRCUITPY_USB_HID),1)
SRC_PATTERNS += usb_hid/%
endif

View File

@ -299,6 +299,13 @@ extern const struct _mp_obj_module_t audiopwmio_module;
#define BINASCII_MODULE
#endif
#if CIRCUITPY_BITBANGIO
#define BITBANGIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_bitbangio), (mp_obj_t)&bitbangio_module },
extern const struct _mp_obj_module_t bitbangio_module;
#else
#define BITBANGIO_MODULE
#endif
#if CIRCUITPY_BITOPS
extern const struct _mp_obj_module_t bitops_module;
#define BITOPS_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_bitops),(mp_obj_t)&bitops_module },
@ -307,13 +314,6 @@ extern const struct _mp_obj_module_t bitops_module;
#endif
#if CIRCUITPY_BITBANGIO
#define BITBANGIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_bitbangio), (mp_obj_t)&bitbangio_module },
extern const struct _mp_obj_module_t bitbangio_module;
#else
#define BITBANGIO_MODULE
#endif
#if CIRCUITPY_BLEIO
#define BLEIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR__bleio), (mp_obj_t)&bleio_module },
extern const struct _mp_obj_module_t bleio_module;
@ -744,6 +744,13 @@ extern const struct _mp_obj_module_t uheap_module;
#define UHEAP_MODULE
#endif
#if CIRCUITPY_USB_CDC
extern const struct _mp_obj_module_t usb_cdc_module;
#define USB_CDC_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_usb_cdc),(mp_obj_t)&usb_cdc_module },
#else
#define USB_CDC_MODULE
#endif
#if CIRCUITPY_USB_HID
extern const struct _mp_obj_module_t usb_hid_module;
#define USB_HID_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid),(mp_obj_t)&usb_hid_module },
@ -827,8 +834,8 @@ extern const struct _mp_obj_module_t msgpack_module;
AUDIOMP3_MODULE \
AUDIOPWMIO_MODULE \
BINASCII_MODULE \
BITOPS_MODULE \
BITBANGIO_MODULE \
BITOPS_MODULE \
BLEIO_MODULE \
BOARD_MODULE \
BUSDEVICE_MODULE \
@ -884,6 +891,7 @@ extern const struct _mp_obj_module_t msgpack_module;
SUPERVISOR_MODULE \
TOUCHIO_MODULE \
UHEAP_MODULE \
USB_CDC_MODULE \
USB_HID_MODULE \
USB_MIDI_MODULE \
USTACK_MODULE \

View File

@ -89,12 +89,15 @@ CFLAGS += -DCIRCUITPY_AUDIOMP3=$(CIRCUITPY_AUDIOMP3)
CIRCUITPY_BINASCII ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_BINASCII=$(CIRCUITPY_BINASCII)
CIRCUITPY_BITOPS ?= 0
CFLAGS += -DCIRCUITPY_BITOPS=$(CIRCUITPY_BITOPS)
CIRCUITPY_BITBANG_APA102 ?= 0
CFLAGS += -DCIRCUITPY_BITBANG_APA102=$(CIRCUITPY_BITBANG_APA102)
CIRCUITPY_BITBANGIO ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_BITBANGIO=$(CIRCUITPY_BITBANGIO)
CIRCUITPY_BITOPS ?= 0
CFLAGS += -DCIRCUITPY_BITOPS=$(CIRCUITPY_BITOPS)
# _bleio can be supported on most any board via HCI
CIRCUITPY_BLEIO_HCI ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_BLEIO_HCI=$(CIRCUITPY_BLEIO_HCI)
@ -103,6 +106,9 @@ CFLAGS += -DCIRCUITPY_BLEIO_HCI=$(CIRCUITPY_BLEIO_HCI)
CIRCUITPY_BLEIO ?= $(CIRCUITPY_BLEIO_HCI)
CFLAGS += -DCIRCUITPY_BLEIO=$(CIRCUITPY_BLEIO)
CIRCUITPY_BLE_FILE_SERVICE ?= 0
CFLAGS += -DCIRCUITPY_BLE_FILE_SERVICE=$(CIRCUITPY_BLE_FILE_SERVICE)
CIRCUITPY_BOARD ?= 1
CFLAGS += -DCIRCUITPY_BOARD=$(CIRCUITPY_BOARD)
@ -130,6 +136,13 @@ CFLAGS += -DCIRCUITPY_COUNTIO=$(CIRCUITPY_COUNTIO)
CIRCUITPY_DISPLAYIO ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_DISPLAYIO=$(CIRCUITPY_DISPLAYIO)
CIRCUITPY_DUALBANK ?= 0
CFLAGS += -DCIRCUITPY_DUALBANK=$(CIRCUITPY_DUALBANK)
# Enabled micropython.native decorator (experimental)
CIRCUITPY_ENABLE_MPY_NATIVE ?= 0
CFLAGS += -DCIRCUITPY_ENABLE_MPY_NATIVE=$(CIRCUITPY_ENABLE_MPY_NATIVE)
CIRCUITPY_ERRNO ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_ERRNO=$(CIRCUITPY_ERRNO)
@ -182,6 +195,9 @@ CFLAGS += -DCIRCUITPY_MEMORYMONITOR=$(CIRCUITPY_MEMORYMONITOR)
CIRCUITPY_MICROCONTROLLER ?= 1
CFLAGS += -DCIRCUITPY_MICROCONTROLLER=$(CIRCUITPY_MICROCONTROLLER)
CIRCUITPY_MSGPACK ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_MSGPACK=$(CIRCUITPY_MSGPACK)
CIRCUITPY_NEOPIXEL_WRITE ?= 1
CFLAGS += -DCIRCUITPY_NEOPIXEL_WRITE=$(CIRCUITPY_NEOPIXEL_WRITE)
@ -195,8 +211,8 @@ CFLAGS += -DCIRCUITPY_NVM=$(CIRCUITPY_NVM)
CIRCUITPY_OS ?= 1
CFLAGS += -DCIRCUITPY_OS=$(CIRCUITPY_OS)
CIRCUITPY_DUALBANK ?= 0
CFLAGS += -DCIRCUITPY_DUALBANK=$(CIRCUITPY_DUALBANK)
CIRCUITPY_PEW ?= 0
CFLAGS += -DCIRCUITPY_PEW=$(CIRCUITPY_PEW)
CIRCUITPY_PIXELBUF ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_PIXELBUF=$(CIRCUITPY_PIXELBUF)
@ -219,6 +235,20 @@ CFLAGS += -DCIRCUITPY_RANDOM=$(CIRCUITPY_RANDOM)
CIRCUITPY_RE ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_RE=$(CIRCUITPY_RE)
CIRCUITPY_REPL_BLE ?= 0
CFLAGS += -DCIRCUITPY_REPL_BLE=$(CIRCUITPY_REPL_BLE)
CIRCUITPY_REPL_UART ?= 0
CFLAGS += -DCIRCUITPY_REPL_UART=$(CIRCUITPY_REPL_UART)
CIRCUITPY_REPL_USB ?= 1
CFLAGS += -DCIRCUITPY_REPL_USB=$(CIRCUITPY_REPL_USB)
# Should busio.I2C() check for pullups?
# Some boards in combination with certain peripherals may not want this.
CIRCUITPY_REQUIRE_I2C_PULLUPS ?= 1
CFLAGS += -DCIRCUITPY_REQUIRE_I2C_PULLUPS=$(CIRCUITPY_REQUIRE_I2C_PULLUPS)
# CIRCUITPY_RP2PIO is handled in the raspberrypi tree.
# Only for rp2 chips.
# Assume not a rp2 build.
@ -285,46 +315,54 @@ CFLAGS += -DCIRCUITPY_TOUCHIO=$(CIRCUITPY_TOUCHIO)
CIRCUITPY_UHEAP ?= 0
CFLAGS += -DCIRCUITPY_UHEAP=$(CIRCUITPY_UHEAP)
# Secondary CDC is usually available if there are at least 8 endpoints.
CIRCUITPY_USB_CDC ?= $(shell expr $(USB_NUM_EP) '>=' 8)
CFLAGS += -DCIRCUITPY_USB_CDC=$(CIRCUITPY_USB_CDC)
CIRCUITPY_USB_HID ?= 1
CFLAGS += -DCIRCUITPY_USB_HID=$(CIRCUITPY_USB_HID)
CIRCUITPY_USB_HID_CONSUMER ?= 1
CFLAGS += -DCIRCUITPY_USB_HID_CONSUMER=$(CIRCUITPY_USB_HID_CONSUMER)
CIRCUITPY_USB_HID_DIGITIZER ?= 0
CFLAGS += -DCIRCUITPY_USB_HID_DIGITIZER=$(CIRCUITPY_USB_HID_DIGITIZER)
CIRCUITPY_USB_HID_GAMEPAD ?= 1
CFLAGS += -DCIRCUITPY_USB_HID_GAMEPAD=$(CIRCUITPY_USB_HID_GAMEPAD)
CIRCUITPY_USB_HID_KEYBOARD ?= 1
CFLAGS += -DCIRCUITPY_USB_HID_KEYBOARD=$(CIRCUITPY_USB_HID_KEYBOARD)
CIRCUITPY_USB_HID_MOUSE ?= 1
CFLAGS += -DCIRCUITPY_USB_HID_MOUSE=$(CIRCUITPY_USB_HID_MOUSE)
CIRCUITPY_USB_HID_SYS_CONTROL ?= 0
CFLAGS += -DCIRCUITPY_USB_HID_CONTROL=$(CIRCUITPY_USB_HID_CONTROL)
CIRCUITPY_USB_HID_XAC_COMPATIBLE_GAMEPAD ?= 0
CFLAGS += -DCIRCUITPY_USB_HID_XAC_COMPATIBLE_GAMEPAD=$(CIRCUITPY_USB_HID_XAC_COMPATIBLE_GAMEPAD)
CIRCUITPY_USB_MIDI ?= 1
CFLAGS += -DCIRCUITPY_USB_MIDI=$(CIRCUITPY_USB_MIDI)
CIRCUITPY_USB_MSC ?= 1
CFLAGS += -DCIRCUITPY_USB_MSC=$(CIRCUITPY_USB_MSC)
# Defaulting this to OFF initially because it has only been tested on a
# limited number of platforms, and the other platforms do not have this
# setting in their mpconfigport.mk and/or mpconfigboard.mk files yet.
CIRCUITPY_USB_VENDOR ?= 0
CFLAGS += -DCIRCUITPY_USB_VENDOR=$(CIRCUITPY_USB_VENDOR)
CIRCUITPY_PEW ?= 0
CFLAGS += -DCIRCUITPY_PEW=$(CIRCUITPY_PEW)
ifndef USB_NUM_EP
$(error "USB_NUM_EP (number of USB endpoint pairs)must be defined")
endif
# For debugging.
CIRCUITPY_USTACK ?= 0
CFLAGS += -DCIRCUITPY_USTACK=$(CIRCUITPY_USTACK)
# Non-module conditionals
CIRCUITPY_BITBANG_APA102 ?= 0
CFLAGS += -DCIRCUITPY_BITBANG_APA102=$(CIRCUITPY_BITBANG_APA102)
# Should busio.I2C() check for pullups?
# Some boards in combination with certain peripherals may not want this.
CIRCUITPY_REQUIRE_I2C_PULLUPS ?= 1
CFLAGS += -DCIRCUITPY_REQUIRE_I2C_PULLUPS=$(CIRCUITPY_REQUIRE_I2C_PULLUPS)
# REPL over BLE
CIRCUITPY_SERIAL_BLE ?= 0
CFLAGS += -DCIRCUITPY_SERIAL_BLE=$(CIRCUITPY_SERIAL_BLE)
CIRCUITPY_BLE_FILE_SERVICE ?= 0
CFLAGS += -DCIRCUITPY_BLE_FILE_SERVICE=$(CIRCUITPY_BLE_FILE_SERVICE)
# REPL over UART
CIRCUITPY_SERIAL_UART ?= 0
CFLAGS += -DCIRCUITPY_SERIAL_UART=$(CIRCUITPY_SERIAL_UART)
# ulab numerics library
CIRCUITPY_ULAB ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_ULAB=$(CIRCUITPY_ULAB)
@ -335,10 +373,3 @@ CFLAGS += -DCIRCUITPY_WATCHDOG=$(CIRCUITPY_WATCHDOG)
CIRCUITPY_WIFI ?= 0
CFLAGS += -DCIRCUITPY_WIFI=$(CIRCUITPY_WIFI)
# Enabled micropython.native decorator (experimental)
CIRCUITPY_ENABLE_MPY_NATIVE ?= 0
CFLAGS += -DCIRCUITPY_ENABLE_MPY_NATIVE=$(CIRCUITPY_ENABLE_MPY_NATIVE)
CIRCUITPY_MSGPACK ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_MSGPACK=$(CIRCUITPY_MSGPACK)

View File

@ -99,14 +99,22 @@ const mp_stream_p_t *mp_get_stream_raise(mp_obj_t self_in, int flags) {
STATIC mp_obj_t stream_read_generic(size_t n_args, const mp_obj_t *args, byte flags) {
// What to do if sz < -1? Python docs don't specify this case.
// CPython does a readall, but here we silently let negatives through,
// and they will cause a MemoryError.
// CPython does a readall, let's do the same.
mp_int_t sz;
if (n_args == 1 || args[1] == mp_const_none || ((sz = mp_obj_get_int(args[1])) == -1)) {
return stream_readall(args[0]);
}
const mp_stream_p_t *stream_p = mp_get_stream(args[0]);
if (stream_p->pyserial_read_compatibility) {
// Pyserial defaults to sz=1 if not specified.
if (n_args == 1) {
sz = 1;
} else {
// Pyserial treats negative size as 0.
sz = MAX(0, mp_obj_get_int(args[1]));
}
} else {
if (n_args == 1 || args[1] == mp_const_none || (sz = mp_obj_get_int(args[1])) <= -1) {
return stream_readall(args[0]);
}
}
#if MICROPY_PY_BUILTINS_STR_UNICODE
if (stream_p->is_text) {
@ -284,7 +292,7 @@ STATIC mp_obj_t stream_readinto(size_t n_args, const mp_obj_t *args) {
// https://docs.python.org/3/library/socket.html#socket.socket.recv_into
mp_uint_t len = bufinfo.len;
if (n_args > 2) {
if (mp_get_stream(args[0])->pyserial_compatibility) {
if (mp_get_stream(args[0])->pyserial_readinto_compatibility) {
mp_raise_ValueError(translate("length argument not allowed for this type"));
}
len = mp_obj_get_int(args[2]);
@ -297,7 +305,10 @@ STATIC mp_obj_t stream_readinto(size_t n_args, const mp_obj_t *args) {
mp_uint_t out_sz = mp_stream_read_exactly(args[0], bufinfo.buf, len, &error);
if (error != 0) {
if (mp_is_nonblocking_error(error)) {
return mp_const_none;
// pyserial readinto never returns None, just 0.
return mp_get_stream(args[0])->pyserial_dont_return_none_compatibility
? MP_OBJ_NEW_SMALL_INT(0)
: mp_const_none;
}
mp_raise_OSError(error);
} else {
@ -323,7 +334,10 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
// If we read nothing, return None, just like read().
// Otherwise, return data read so far.
if (total_size == 0) {
return mp_const_none;
// pyserial read() never returns None, just b''.
return stream_p->pyserial_dont_return_none_compatibility
? mp_const_empty_bytes
: mp_const_none;
}
break;
}

View File

@ -72,7 +72,9 @@ typedef struct _mp_stream_p_t {
mp_uint_t (*write)(mp_obj_t obj, const void *buf, mp_uint_t size, int *errcode);
mp_uint_t (*ioctl)(mp_obj_t obj, mp_uint_t request, uintptr_t arg, int *errcode);
mp_uint_t is_text : 1; // default is bytes, set this for text stream
bool pyserial_compatibility: 1; // adjust API to match pyserial more closely
bool pyserial_readinto_compatibility: 1; // Disallow size parameter in readinto()
bool pyserial_read_compatibility: 1; // Disallow omitting read(size) size parameter
bool pyserial_dont_return_none_compatibility: 1; // Don't return None for read() or readinto()
} mp_stream_p_t;
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj);

View File

@ -231,8 +231,8 @@ STATIC const mp_stream_p_t characteristic_buffer_stream_p = {
.write = bleio_characteristic_buffer_write,
.ioctl = bleio_characteristic_buffer_ioctl,
.is_text = false,
// Match PySerial when possible, such as disallowing optional length argument for .readinto()
.pyserial_compatibility = true,
// Disallow readinto() size parameter.
.pyserial_readinto_compatibility = true,
};

View File

@ -415,8 +415,8 @@ STATIC const mp_stream_p_t uart_stream_p = {
.write = busio_uart_write,
.ioctl = busio_uart_ioctl,
.is_text = false,
// Match PySerial when possible, such as disallowing optional length argument for .readinto()
.pyserial_compatibility = true,
// Disallow optional length argument for .readinto()
.pyserial_readinto_compatibility = true,
};
const mp_obj_type_t busio_uart_type = {

View File

@ -55,59 +55,46 @@ STATIC supervisor_run_reason_t _run_reason;
//| serial_connected: bool
//| """Returns the USB serial communication status (read-only)."""
//|
STATIC mp_obj_t supervisor_get_serial_connected(mp_obj_t self){
if (!common_hal_get_serial_connected()) {
return mp_const_false;
}
else {
return mp_const_true;
}
STATIC mp_obj_t supervisor_runtime_get_serial_connected(mp_obj_t self){
return mp_obj_new_bool(common_hal_supervisor_runtime_get_serial_connected());
}
MP_DEFINE_CONST_FUN_OBJ_1(supervisor_get_serial_connected_obj, supervisor_get_serial_connected);
MP_DEFINE_CONST_FUN_OBJ_1(supervisor_runtime_get_serial_connected_obj, supervisor_runtime_get_serial_connected);
const mp_obj_property_t supervisor_serial_connected_obj = {
const mp_obj_property_t supervisor_runtime_serial_connected_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&supervisor_get_serial_connected_obj,
.proxy = {(mp_obj_t)&supervisor_runtime_get_serial_connected_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| serial_bytes_available: int
//| """Returns the whether any bytes are available to read
//| on the USB serial input. Allows for polling to see whether
//| to call the built-in input() or wait. (read-only)"""
//|
STATIC mp_obj_t supervisor_get_serial_bytes_available(mp_obj_t self){
if (!common_hal_get_serial_bytes_available()) {
return mp_const_false;
}
else {
return mp_const_true;
}
STATIC mp_obj_t supervisor_runtime_get_serial_bytes_available(mp_obj_t self){
return mp_obj_new_bool(common_hal_supervisor_runtime_get_serial_bytes_available());
}
MP_DEFINE_CONST_FUN_OBJ_1(supervisor_get_serial_bytes_available_obj, supervisor_get_serial_bytes_available);
MP_DEFINE_CONST_FUN_OBJ_1(supervisor_runtime_get_serial_bytes_available_obj, supervisor_runtime_get_serial_bytes_available);
const mp_obj_property_t supervisor_serial_bytes_available_obj = {
const mp_obj_property_t supervisor_runtime_serial_bytes_available_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&supervisor_get_serial_bytes_available_obj,
.proxy = {(mp_obj_t)&supervisor_runtime_get_serial_bytes_available_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| run_reason: RunReason
//| """Returns why CircuitPython started running this particular time."""
//|
STATIC mp_obj_t supervisor_get_run_reason(mp_obj_t self) {
STATIC mp_obj_t supervisor_runtime_get_run_reason(mp_obj_t self) {
return cp_enum_find(&supervisor_run_reason_type, _run_reason);
}
MP_DEFINE_CONST_FUN_OBJ_1(supervisor_get_run_reason_obj, supervisor_get_run_reason);
MP_DEFINE_CONST_FUN_OBJ_1(supervisor_runtime_get_run_reason_obj, supervisor_runtime_get_run_reason);
const mp_obj_property_t supervisor_run_reason_obj = {
const mp_obj_property_t supervisor_runtime_run_reason_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&supervisor_get_run_reason_obj,
.proxy = {(mp_obj_t)&supervisor_runtime_get_run_reason_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
@ -117,9 +104,9 @@ void supervisor_set_run_reason(supervisor_run_reason_t run_reason) {
}
STATIC const mp_rom_map_elem_t supervisor_runtime_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_serial_connected), MP_ROM_PTR(&supervisor_serial_connected_obj) },
{ MP_ROM_QSTR(MP_QSTR_serial_bytes_available), MP_ROM_PTR(&supervisor_serial_bytes_available_obj) },
{ MP_ROM_QSTR(MP_QSTR_run_reason), MP_ROM_PTR(&supervisor_run_reason_obj) },
{ MP_ROM_QSTR(MP_QSTR_serial_connected), MP_ROM_PTR(&supervisor_runtime_serial_connected_obj) },
{ MP_ROM_QSTR(MP_QSTR_serial_bytes_available), MP_ROM_PTR(&supervisor_runtime_serial_bytes_available_obj) },
{ MP_ROM_QSTR(MP_QSTR_run_reason), MP_ROM_PTR(&supervisor_runtime_run_reason_obj) },
};
STATIC MP_DEFINE_CONST_DICT(supervisor_runtime_locals_dict, supervisor_runtime_locals_dict_table);

View File

@ -36,12 +36,12 @@ extern const mp_obj_type_t supervisor_runtime_type;
void supervisor_set_run_reason(supervisor_run_reason_t run_reason);
bool common_hal_get_serial_connected(void);
bool common_hal_supervisor_runtime_get_serial_connected(void);
bool common_hal_get_serial_bytes_available(void);
bool common_hal_supervisor_runtime_get_serial_bytes_available(void);
//TODO: placeholders for future functions
//bool common_hal_get_repl_active(void);
//bool common_hal_get_usb_enumerated(void);
//bool common_hal_get_supervisor_runtime_repl_active(void);
//bool common_hal_get_supervisor_runtime_usb_enumerated(void);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_SUPERVISOR_RUNTIME_H

View File

@ -0,0 +1,293 @@
/*
* This file is part of the Micro Python 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.
*/
#include <stdint.h>
#include "shared-bindings/usb_cdc/Serial.h"
#include "shared-bindings/util.h"
#include "py/ioctl.h"
#include "py/objproperty.h"
#include "py/runtime.h"
#include "py/stream.h"
#include "supervisor/shared/translate.h"
//| class Serial:
//| """Receives cdc commands over USB"""
//|
//| def __init__(self) -> None:
//| """You cannot create an instance of `usb_cdc.Serial`.
//|
//| Serial objects are pre-constructed for each CDC device in the USB
//| descriptor and added to the ``usb_cdc.ports`` tuple."""
//| ...
//|
//| def read(self, size: int = 1) -> bytes:
//| """Read at most ``size`` bytes. If ``size`` exceeds the internal buffer size
//| only the bytes in the buffer will be read. If `timeout` is > 0 or ``None``,
//| and fewer than ``size`` bytes are available, keep waiting until the timeout
//| expires or ``size`` bytes are available.
//|
//| :return: Data read
//| :rtype: bytes"""
//| ...
//|
//| def readinto(self, buf: WriteableBuffer) -> int:
//| """Read bytes into the ``buf``. If ``nbytes`` is specified then read at most
//| that many bytes, subject to `timeout`. Otherwise, read at most ``len(buf)`` bytes.
//|
//| :return: number of bytes read and stored into ``buf``
//| :rtype: bytes"""
//| ...
//|
//| def write(self, buf: ReadableBuffer) -> int:
//| """Write as many bytes as possible from the buffer of bytes.
//|
//| :return: the number of bytes written
//| :rtype: int"""
//| ...
//|
//| def flush(self) -> None:
//| """Force out any unwritten bytes, waiting until they are written."""
//| ...
//|
// These three methods are used by the shared stream methods.
STATIC mp_uint_t usb_cdc_serial_read_stream(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
usb_cdc_serial_obj_t *self = MP_OBJ_TO_PTR(self_in);
byte *buf = buf_in;
// make sure we want at least 1 char
if (size == 0) {
return 0;
}
return common_hal_usb_cdc_serial_read(self, buf, size, errcode);
}
STATIC mp_uint_t usb_cdc_serial_write_stream(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) {
usb_cdc_serial_obj_t *self = MP_OBJ_TO_PTR(self_in);
const byte *buf = buf_in;
return common_hal_usb_cdc_serial_write(self, buf, size, errcode);
}
STATIC mp_uint_t usb_cdc_serial_ioctl_stream(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
usb_cdc_serial_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_uint_t ret = 0;
switch (request) {
case MP_IOCTL_POLL: {
mp_uint_t flags = arg;
ret = 0;
if ((flags & MP_IOCTL_POLL_RD) && common_hal_usb_cdc_serial_get_in_waiting(self) > 0) {
ret |= MP_IOCTL_POLL_RD;
}
if ((flags & MP_IOCTL_POLL_WR) && common_hal_usb_cdc_serial_get_out_waiting(self) == 0) {
ret |= MP_IOCTL_POLL_WR;
}
break;
}
case MP_STREAM_FLUSH:
common_hal_usb_cdc_serial_flush(self);
break;
default:
*errcode = MP_EINVAL;
ret = MP_STREAM_ERROR;
}
return ret;
}
//| connected: bool
//| """True if this Serial is connected to a host. (read-only)"""
//|
STATIC mp_obj_t usb_cdc_serial_get_connected(mp_obj_t self_in) {
usb_cdc_serial_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_bool(common_hal_usb_cdc_serial_get_connected(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(usb_cdc_serial_get_connected_obj, usb_cdc_serial_get_connected);
const mp_obj_property_t usb_cdc_serial_connected_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&usb_cdc_serial_get_connected_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| in_waiting: int
//| """Returns the number of bytes waiting to be read on the USB serial input. (read-only)"""
//|
STATIC mp_obj_t usb_cdc_serial_get_in_waiting(mp_obj_t self_in) {
usb_cdc_serial_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_int(common_hal_usb_cdc_serial_get_in_waiting(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(usb_cdc_serial_get_in_waiting_obj, usb_cdc_serial_get_in_waiting);
const mp_obj_property_t usb_cdc_serial_in_waiting_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&usb_cdc_serial_get_in_waiting_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| out_waiting: int
//| """Returns the number of bytes waiting to be written on the USB serial output. (read-only)"""
//|
STATIC mp_obj_t usb_cdc_serial_get_out_waiting(mp_obj_t self_in) {
usb_cdc_serial_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_int(common_hal_usb_cdc_serial_get_out_waiting(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(usb_cdc_serial_get_out_waiting_obj, usb_cdc_serial_get_out_waiting);
const mp_obj_property_t usb_cdc_serial_out_waiting_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&usb_cdc_serial_get_out_waiting_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| def reset_input_buffer(self) -> None:
//| """Clears any unread bytes."""
//| ...
//|
STATIC mp_obj_t usb_cdc_serial_reset_input_buffer(mp_obj_t self_in) {
usb_cdc_serial_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_usb_cdc_serial_reset_input_buffer(self);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(usb_cdc_serial_reset_input_buffer_obj, usb_cdc_serial_reset_input_buffer);
//| def reset_output_buffer(self) -> None:
//| """Clears any unwritten bytes."""
//| ...
//|
STATIC mp_obj_t usb_cdc_serial_reset_output_buffer(mp_obj_t self_in) {
usb_cdc_serial_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_usb_cdc_serial_reset_output_buffer(self);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(usb_cdc_serial_reset_output_buffer_obj, usb_cdc_serial_reset_output_buffer);
//| timeout: Optional[float]
//| """The initial value of `timeout` is ``None``. If ``None``, wait indefinitely to satisfy
//| the conditions of a read operation. If 0, do not wait. If > 0, wait only ``timeout`` seconds."""
//|
STATIC mp_obj_t usb_cdc_serial_get_timeout(mp_obj_t self_in) {
usb_cdc_serial_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_float_t timeout = common_hal_usb_cdc_serial_get_timeout(self);
return (timeout < 0.0f) ? mp_const_none : mp_obj_new_float(self->timeout);
}
MP_DEFINE_CONST_FUN_OBJ_1(usb_cdc_serial_get_timeout_obj, usb_cdc_serial_get_timeout);
STATIC mp_obj_t usb_cdc_serial_set_timeout(mp_obj_t self_in, mp_obj_t timeout_in) {
usb_cdc_serial_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_usb_cdc_serial_set_timeout(self,
timeout_in == mp_const_none ? -1.0f : mp_obj_get_float(timeout_in));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(usb_cdc_serial_set_timeout_obj, usb_cdc_serial_set_timeout);
const mp_obj_property_t usb_cdc_serial_timeout_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&usb_cdc_serial_get_timeout_obj,
(mp_obj_t)&usb_cdc_serial_set_timeout_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| write_timeout: Optional[float]
//| """The initial value of `write_timeout` is ``None``. If ``None``, wait indefinitely to finish
//| writing all the bytes passed to ``write()``.If 0, do not wait.
//| If > 0, wait only ``write_timeout`` seconds."""
//|
STATIC mp_obj_t usb_cdc_serial_get_write_timeout(mp_obj_t self_in) {
usb_cdc_serial_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_float_t write_timeout = common_hal_usb_cdc_serial_get_write_timeout(self);
return (write_timeout < 0.0f) ? mp_const_none : mp_obj_new_float(self->write_timeout);
}
MP_DEFINE_CONST_FUN_OBJ_1(usb_cdc_serial_get_write_timeout_obj, usb_cdc_serial_get_write_timeout);
STATIC mp_obj_t usb_cdc_serial_set_write_timeout(mp_obj_t self_in, mp_obj_t write_timeout_in) {
usb_cdc_serial_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_usb_cdc_serial_set_write_timeout(self,
write_timeout_in == mp_const_none ? -1.0f : mp_obj_get_float(write_timeout_in));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(usb_cdc_serial_set_write_timeout_obj, usb_cdc_serial_set_write_timeout);
const mp_obj_property_t usb_cdc_serial_write_timeout_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&usb_cdc_serial_get_write_timeout_obj,
(mp_obj_t)&usb_cdc_serial_set_write_timeout_obj,
(mp_obj_t)&mp_const_none_obj},
};
STATIC const mp_rom_map_elem_t usb_cdc_serial_locals_dict_table[] = {
// Standard stream methods.
{ MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
{ MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj)},
{ MP_ROM_QSTR(MP_QSTR_readlines), MP_ROM_PTR(&mp_stream_unbuffered_readlines_obj)},
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
// Other pyserial-inspired attributes.
{ MP_OBJ_NEW_QSTR(MP_QSTR_in_waiting), MP_ROM_PTR(&usb_cdc_serial_in_waiting_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_out_waiting), MP_ROM_PTR(&usb_cdc_serial_out_waiting_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_reset_input_buffer), MP_ROM_PTR(&usb_cdc_serial_reset_input_buffer_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_reset_output_buffer), MP_ROM_PTR(&usb_cdc_serial_reset_output_buffer_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_timeout), MP_ROM_PTR(&usb_cdc_serial_timeout_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_write_timeout), MP_ROM_PTR(&usb_cdc_serial_write_timeout_obj) },
// Not in pyserial protocol.
{ MP_OBJ_NEW_QSTR(MP_QSTR_connected), MP_ROM_PTR(&usb_cdc_serial_connected_obj) },
};
STATIC MP_DEFINE_CONST_DICT(usb_cdc_serial_locals_dict, usb_cdc_serial_locals_dict_table);
STATIC const mp_stream_p_t usb_cdc_serial_stream_p = {
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_stream)
.read = usb_cdc_serial_read_stream,
.write = usb_cdc_serial_write_stream,
.ioctl = usb_cdc_serial_ioctl_stream,
.is_text = false,
.pyserial_read_compatibility = true,
.pyserial_readinto_compatibility = true,
.pyserial_dont_return_none_compatibility = true,
};
const mp_obj_type_t usb_cdc_serial_type = {
{ &mp_type_type },
.name = MP_QSTR_Serial,
.getiter = mp_identity_getiter,
.iternext = mp_stream_unbuffered_iter,
.protocol = &usb_cdc_serial_stream_p,
.locals_dict = (mp_obj_dict_t*)&usb_cdc_serial_locals_dict,
};

View File

@ -0,0 +1,53 @@
/*
* This file is part of the Micro Python 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 MICROPY_INCLUDED_SHARED_BINDINGS_USB_CDC_SERIAL_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_USB_CDC_SERIAL_H
#include "shared-module/usb_cdc/Serial.h"
extern const mp_obj_type_t usb_cdc_serial_type;
extern size_t common_hal_usb_cdc_serial_read(usb_cdc_serial_obj_t *self, uint8_t *data, size_t len, int *errcode);
extern size_t common_hal_usb_cdc_serial_write(usb_cdc_serial_obj_t *self, const uint8_t *data, size_t len, int *errcode);
extern uint32_t common_hal_usb_cdc_serial_get_in_waiting(usb_cdc_serial_obj_t *self);
extern uint32_t common_hal_usb_cdc_serial_get_out_waiting(usb_cdc_serial_obj_t *self);
extern void common_hal_usb_cdc_serial_reset_input_buffer(usb_cdc_serial_obj_t *self);
extern uint32_t common_hal_usb_cdc_serial_reset_output_buffer(usb_cdc_serial_obj_t *self);
extern uint32_t common_hal_usb_cdc_serial_flush(usb_cdc_serial_obj_t *self);
extern bool common_hal_usb_cdc_serial_get_connected(usb_cdc_serial_obj_t *self);
extern mp_float_t common_hal_usb_cdc_serial_get_timeout(usb_cdc_serial_obj_t *self);
extern void common_hal_usb_cdc_serial_set_timeout(usb_cdc_serial_obj_t *self, mp_float_t timeout);
extern mp_float_t common_hal_usb_cdc_serial_get_write_timeout(usb_cdc_serial_obj_t *self);
extern void common_hal_usb_cdc_serial_set_write_timeout(usb_cdc_serial_obj_t *self, mp_float_t write_timeout);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_USB_CDC_SERIAL_H

View File

@ -0,0 +1,59 @@
/*
* 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"
#include "py/runtime.h"
//| """USB CDC Serial streams
//|
//| 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.
//| """
//|
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) },
};
static MP_DEFINE_CONST_DICT(usb_cdc_module_globals, usb_cdc_module_globals_table);
const mp_obj_module_t usb_cdc_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&usb_cdc_module_globals,
};

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 MICROPY_INCLUDED_SHARED_BINDINGS_USB_CDC___INIT___H
#define MICROPY_INCLUDED_SHARED_BINDINGS_USB_CDC___INIT___H
#include "shared-module/usb_cdc/__init__.h"
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_USB_CDC___INIT___H

View File

@ -0,0 +1,147 @@
/*
* 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.
*/
#include "lib/utils/interrupt_char.h"
#include "shared-module/usb_cdc/Serial.h"
#include "supervisor/shared/tick.h"
#include "tusb.h"
size_t common_hal_usb_cdc_serial_read(usb_cdc_serial_obj_t *self, uint8_t *data, size_t len, int *errcode) {
const bool wait_forever = self->timeout < 0.0f;
const bool wait_for_timeout = self->timeout > 0.0f;
// Read up to len bytes immediately.
// The number of bytes read will not be larger than what is already in the TinyUSB FIFO.
uint32_t total_num_read = tud_cdc_n_read(self->idx, data, len);
if (wait_forever || wait_for_timeout) {
// Read more if we have time.
uint64_t timeout_ms = self->timeout * 1000; // Junk value if timeout < 0.
uint64_t start_ticks = supervisor_ticks_ms64();
uint32_t num_read = 0;
while (total_num_read < len &&
(wait_forever || supervisor_ticks_ms64() - start_ticks <= timeout_ms)) {
// Wait for a bit, and check for ctrl-C.
RUN_BACKGROUND_TASKS;
if (mp_hal_is_interrupted()) {
return 0;
}
// Advance buffer pointer and reduce number of bytes that need to be read.
len -= num_read;
data += num_read;
// Try to read another batch of bytes.
num_read = tud_cdc_n_read(self->idx, data, len);
total_num_read += num_read;
}
}
return total_num_read;
}
size_t common_hal_usb_cdc_serial_write(usb_cdc_serial_obj_t *self, const uint8_t *data, size_t len, int *errcode) {
const bool wait_forever = self->write_timeout < 0.0f;
const bool wait_for_timeout = self->write_timeout > 0.0f;
// Write as many bytes as possible immediately.
// The number of bytes written at once will not be larger than what can fit in the TinyUSB FIFO.
uint32_t total_num_written = tud_cdc_n_write(self->idx, data, len);
tud_cdc_n_write_flush(self->idx);
if (wait_forever || wait_for_timeout) {
// Write more if we have time.
uint64_t timeout_ms = self->write_timeout * 1000; // Junk value if write_timeout < 0.
uint64_t start_ticks = supervisor_ticks_ms64();
uint32_t num_written = 0;
while (total_num_written < len &&
(wait_forever || supervisor_ticks_ms64() - start_ticks <= timeout_ms)) {
// Wait for a bit, and check for ctrl-C.
RUN_BACKGROUND_TASKS;
if (mp_hal_is_interrupted()) {
return 0;
}
// Advance buffer pointer and reduce number of bytes that need to be written.
len -= num_written;
data += num_written;
// Try to write another batch of bytes.
num_written = tud_cdc_n_write(self->idx, data, len);
tud_cdc_n_write_flush(self->idx);
total_num_written += num_written;
}
}
return total_num_written;
}
uint32_t common_hal_usb_cdc_serial_get_in_waiting(usb_cdc_serial_obj_t *self) {
return tud_cdc_n_available(self->idx);
}
uint32_t common_hal_usb_cdc_serial_get_out_waiting(usb_cdc_serial_obj_t *self) {
// Return number of FIFO bytes currently occupied.
return CFG_TUD_CDC_TX_BUFSIZE - tud_cdc_n_write_available(self->idx);
}
void common_hal_usb_cdc_serial_reset_input_buffer(usb_cdc_serial_obj_t *self) {
tud_cdc_n_read_flush(self->idx);
}
uint32_t common_hal_usb_cdc_serial_reset_output_buffer(usb_cdc_serial_obj_t *self) {
return tud_cdc_n_write_clear(self->idx);
}
uint32_t common_hal_usb_cdc_serial_flush(usb_cdc_serial_obj_t *self) {
return tud_cdc_n_write_flush(self->idx);
}
bool common_hal_usb_cdc_serial_get_connected(usb_cdc_serial_obj_t *self) {
return tud_cdc_n_connected(self->idx);
}
mp_float_t common_hal_usb_cdc_serial_get_timeout(usb_cdc_serial_obj_t *self) {
return self->timeout;
}
void common_hal_usb_cdc_serial_set_timeout(usb_cdc_serial_obj_t *self, mp_float_t timeout) {
self->timeout = timeout;
}
mp_float_t common_hal_usb_cdc_serial_get_write_timeout(usb_cdc_serial_obj_t *self) {
return self->write_timeout;
}
void common_hal_usb_cdc_serial_set_write_timeout(usb_cdc_serial_obj_t *self, mp_float_t write_timeout) {
self->write_timeout = write_timeout;
}

View File

@ -0,0 +1,39 @@
/*
* 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_USB_CDC_SERIAL_H
#define SHARED_MODULE_USB_CDC_SERIAL_H
#include "py/obj.h"
typedef struct {
mp_obj_base_t base;
mp_float_t timeout; // if negative, wait forever.
mp_float_t write_timeout; // if negative, wait forever.
uint8_t idx; // which CDC device?
} usb_cdc_serial_obj_t;
#endif // SHARED_MODULE_USB_CDC_SERIAL_H

View File

@ -0,0 +1,61 @@
/*
* 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.
*/
#include "genhdr/autogen_usb_descriptor.h"
#include "py/gc.h"
#include "py/obj.h"
#include "py/mphal.h"
#include "py/runtime.h"
#include "py/objtuple.h"
#include "shared-bindings/usb_cdc/__init__.h"
#include "shared-bindings/usb_cdc/Serial.h"
#include "tusb.h"
#if CFG_TUD_CDC != 2
#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,
}
};
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],
},
};

View File

@ -0,0 +1,34 @@
/*
* 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_USB_CDC___INIT___H
#define SHARED_MODULE_USB_CDC___INIT___H
#include "py/objtuple.h"
extern const mp_rom_obj_tuple_t usb_cdc_serials_tuple;
#endif /* SHARED_MODULE_USB_CDC___INIT___H */

View File

@ -118,7 +118,6 @@ bool serial_bytes_available(void) {
return tud_cdc_available() > 0;
#endif
}
void serial_write_substring(const char* text, uint32_t length) {
if (length == 0) {
return;

View File

@ -61,10 +61,15 @@
// DEVICE CONFIGURATION
//--------------------------------------------------------------------+
#define CFG_TUD_ENDOINT0_SIZE 64
#define CFG_TUD_ENDPOINT0_SIZE 64
//------------- CLASS -------------//
// Could be 2 if secondary CDC channel requested.
#ifndef CFG_TUD_CDC
#define CFG_TUD_CDC 1
#endif
#define CFG_TUD_MSC 1
#define CFG_TUD_HID CIRCUITPY_USB_HID
#define CFG_TUD_MIDI CIRCUITPY_USB_MIDI

View File

@ -26,7 +26,6 @@
#include "py/objstr.h"
#include "shared-bindings/microcontroller/Processor.h"
#include "shared-module/usb_midi/__init__.h"
#include "supervisor/background_callback.h"
#include "supervisor/port.h"
#include "supervisor/serial.h"
@ -35,6 +34,10 @@
#include "lib/utils/interrupt_char.h"
#include "lib/mp-readline/readline.h"
#if CIRCUITPY_USB_MIDI
#include "shared-module/usb_midi/__init__.h"
#endif
#include "tusb.h"
#if CIRCUITPY_USB_VENDOR
@ -216,7 +219,7 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ
return true;
}
#endif CIRCUITPY_USB_VENDOR
#endif // CIRCUITPY_USB_VENDOR
#if MICROPY_KBD_EXCEPTION

View File

@ -43,12 +43,14 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index) {
return usb_desc_cfg;
}
#if CIRCUITPY_USB_HID
// Invoked when received GET HID REPORT DESCRIPTOR
// Application return pointer to descriptor
// Descriptor contents must exist long enough for transfer to complete
uint8_t const * tud_hid_descriptor_report_cb(void) {
return hid_report_descriptor;
}
#endif
// Invoked when received GET STRING DESCRIPTOR request
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete

View File

@ -14,23 +14,15 @@ SRC_SUPERVISOR = \
supervisor/shared/tick.c \
supervisor/shared/translate.c
ifndef $(NO_USB)
NO_USB = $(wildcard supervisor/usb.c)
endif
NO_USB ?= $(wildcard supervisor/usb.c)
ifndef INTERNAL_FLASH_FILESYSTEM
INTERNAL_FLASH_FILESYSTEM = 0
endif
INTERNAL_FLASH_FILESYSTEM ?= 0
CFLAGS += -DINTERNAL_FLASH_FILESYSTEM=$(INTERNAL_FLASH_FILESYSTEM)
ifndef QSPI_FLASH_FILESYSTEM
QSPI_FLASH_FILESYSTEM = 0
endif
QSPI_FLASH_FILESYSTEM ?= 0
CFLAGS += -DQSPI_FLASH_FILESYSTEM=$(QSPI_FLASH_FILESYSTEM)
ifndef SPI_FLASH_FILESYSTEM
SPI_FLASH_FILESYSTEM = 0
endif
SPI_FLASH_FILESYSTEM ?= 0
CFLAGS += -DSPI_FLASH_FILESYSTEM=$(SPI_FLASH_FILESYSTEM)
ifeq ($(CIRCUITPY_BLEIO),1)
@ -41,154 +33,180 @@ endif
# (Right now INTERNAL_FLASH_FILESYSTEM and (Q)SPI_FLASH_FILESYSTEM are mutually exclusive.
# But that might not be true in the future.)
ifdef EXTERNAL_FLASH_DEVICES
CFLAGS += -DEXTERNAL_FLASH_DEVICES=$(EXTERNAL_FLASH_DEVICES) \
-DEXTERNAL_FLASH_DEVICE_COUNT=$(EXTERNAL_FLASH_DEVICE_COUNT)
CFLAGS += -DEXTERNAL_FLASH_DEVICES=$(EXTERNAL_FLASH_DEVICES) \
-DEXTERNAL_FLASH_DEVICE_COUNT=$(EXTERNAL_FLASH_DEVICE_COUNT)
SRC_SUPERVISOR += supervisor/shared/external_flash/external_flash.c
ifeq ($(SPI_FLASH_FILESYSTEM),1)
SRC_SUPERVISOR += supervisor/shared/external_flash/spi_flash.c
else
endif
ifeq ($(QSPI_FLASH_FILESYSTEM),1)
SRC_SUPERVISOR += supervisor/qspi_flash.c supervisor/shared/external_flash/qspi_flash.c
endif
SRC_SUPERVISOR += supervisor/shared/external_flash/external_flash.c
ifeq ($(SPI_FLASH_FILESYSTEM),1)
SRC_SUPERVISOR += supervisor/shared/external_flash/spi_flash.c
endif
ifeq ($(QSPI_FLASH_FILESYSTEM),1)
SRC_SUPERVISOR += supervisor/qspi_flash.c supervisor/shared/external_flash/qspi_flash.c
endif
else
ifeq ($(DISABLE_FILESYSTEM),1)
SRC_SUPERVISOR += supervisor/stub/internal_flash.c
else
SRC_SUPERVISOR += supervisor/internal_flash.c
endif
ifeq ($(DISABLE_FILESYSTEM),1)
SRC_SUPERVISOR += supervisor/stub/internal_flash.c
else
SRC_SUPERVISOR += supervisor/internal_flash.c
endif
endif
ifeq ($(USB),FALSE)
ifeq ($(wildcard supervisor/serial.c),)
SRC_SUPERVISOR += supervisor/stub/serial.c
else
SRC_SUPERVISOR += supervisor/serial.c
endif
ifeq ($(wildcard supervisor/serial.c),)
SRC_SUPERVISOR += supervisor/stub/serial.c
else
SRC_SUPERVISOR += supervisor/serial.c
endif
else
SRC_SUPERVISOR += \
lib/tinyusb/src/common/tusb_fifo.c \
lib/tinyusb/src/device/usbd.c \
lib/tinyusb/src/device/usbd_control.c \
lib/tinyusb/src/class/msc/msc_device.c \
lib/tinyusb/src/class/cdc/cdc_device.c \
lib/tinyusb/src/tusb.c \
supervisor/shared/serial.c \
supervisor/shared/workflow.c \
supervisor/usb.c \
supervisor/shared/usb/usb_desc.c \
supervisor/shared/usb/usb.c \
supervisor/shared/usb/usb_msc_flash.c \
$(BUILD)/autogen_usb_descriptor.c
SRC_SUPERVISOR += \
lib/tinyusb/src/class/cdc/cdc_device.c \
lib/tinyusb/src/common/tusb_fifo.c \
lib/tinyusb/src/device/usbd.c \
lib/tinyusb/src/device/usbd_control.c \
lib/tinyusb/src/tusb.c \
supervisor/shared/serial.c \
supervisor/shared/workflow.c \
supervisor/usb.c \
supervisor/shared/usb/usb_desc.c \
supervisor/shared/usb/usb.c \
$(BUILD)/autogen_usb_descriptor.c \
ifeq ($(CIRCUITPY_USB_HID), 1)
SRC_SUPERVISOR += \
lib/tinyusb/src/class/hid/hid_device.c \
shared-bindings/usb_hid/__init__.c \
shared-bindings/usb_hid/Device.c \
shared-module/usb_hid/__init__.c \
shared-module/usb_hid/Device.c
endif
ifeq ($(CIRCUITPY_USB_CDC), 1)
SRC_SUPERVISOR += \
shared-bindings/usb_cdc/__init__.c \
shared-bindings/usb_cdc/Serial.c \
shared-module/usb_cdc/__init__.c \
shared-module/usb_cdc/Serial.c \
ifeq ($(CIRCUITPY_USB_MIDI), 1)
SRC_SUPERVISOR += \
lib/tinyusb/src/class/midi/midi_device.c \
shared-bindings/usb_midi/__init__.c \
shared-bindings/usb_midi/PortIn.c \
shared-bindings/usb_midi/PortOut.c \
shared-module/usb_midi/__init__.c \
shared-module/usb_midi/PortIn.c \
shared-module/usb_midi/PortOut.c
endif
endif
ifeq ($(CIRCUITPY_USB_VENDOR), 1)
SRC_SUPERVISOR += \
lib/tinyusb/src/class/vendor/vendor_device.c
endif
ifeq ($(CIRCUITPY_USB_HID), 1)
SRC_SUPERVISOR += \
lib/tinyusb/src/class/hid/hid_device.c \
shared-bindings/usb_hid/__init__.c \
shared-bindings/usb_hid/Device.c \
shared-module/usb_hid/__init__.c \
shared-module/usb_hid/Device.c \
CFLAGS += -DUSB_AVAILABLE
endif
ifeq ($(CIRCUITPY_USB_MIDI), 1)
SRC_SUPERVISOR += \
lib/tinyusb/src/class/midi/midi_device.c \
shared-bindings/usb_midi/__init__.c \
shared-bindings/usb_midi/PortIn.c \
shared-bindings/usb_midi/PortOut.c \
shared-module/usb_midi/__init__.c \
shared-module/usb_midi/PortIn.c \
shared-module/usb_midi/PortOut.c \
endif
ifeq ($(CIRCUITPY_USB_MSC), 1)
SRC_SUPERVISOR += \
lib/tinyusb/src/class/msc/msc_device.c \
supervisor/shared/usb/usb_msc_flash.c \
endif
ifeq ($(CIRCUITPY_USB_VENDOR), 1)
SRC_SUPERVISOR += \
lib/tinyusb/src/class/vendor/vendor_device.c \
endif
CFLAGS += -DUSB_AVAILABLE
endif
SUPERVISOR_O = $(addprefix $(BUILD)/, $(SRC_SUPERVISOR:.c=.o))
ifeq ($(CIRCUITPY_DISPLAYIO), 1)
SRC_SUPERVISOR += \
supervisor/shared/display.c
SRC_SUPERVISOR += \
supervisor/shared/display.c
ifeq ($(CIRCUITPY_TERMINALIO), 1)
SUPERVISOR_O += $(BUILD)/autogen_display_resources.o
endif
ifeq ($(CIRCUITPY_TERMINALIO), 1)
SUPERVISOR_O += $(BUILD)/autogen_display_resources.o
endif
endif
ifndef USB_INTERFACE_NAME
USB_INTERFACE_NAME = "CircuitPython"
USB_INTERFACE_NAME ?= "CircuitPython"
ifneq ($(USB_VID),)
CFLAGS += -DUSB_VID=$(USB_VID)
CFLAGS += -DSUB_PID=$(USB_PID)
CFLAGS += -DUSB_MANUFACTURER=$(USB_MANUFACTURER)
CFLAGS += -DUSB_PRODUCT=$(USB_PRODUCT)
endif
# In the following URL, don't include the https:// prefix.
# It gets added automatically.
ifndef USB_WEBUSB_URL
USB_WEBUSB_URL = "circuitpython.org"
USB_WEBUSB_URL ?= "circuitpython.org"
ifeq ($(CIRCUITPY_REPL_USB),1)
USB_DEVICES += CDC
endif
USB_DEVICES_COMPUTED := CDC,MSC
ifeq ($(CIRCUITPY_USB_MIDI),1)
USB_DEVICES_COMPUTED := $(USB_DEVICES_COMPUTED),AUDIO
endif
ifeq ($(CIRCUITPY_USB_HID),1)
USB_DEVICES_COMPUTED := $(USB_DEVICES_COMPUTED),HID
USB_DEVICES += HID
endif
ifeq ($(CIRCUITPY_USB_MIDI),1)
USB_DEVICES += AUDIO
endif
ifeq ($(CIRCUITPY_USB_MSC),1)
USB_DEVICES += MSC
endif
ifeq ($(CIRCUITPY_USB_CDC),1)
# Inform TinyUSB there are two CDC devices.
CFLAGS += -DCFG_TUD_CDC=2
USB_DEVICES += CDC2
endif
ifeq ($(CIRCUITPY_USB_VENDOR),1)
USB_DEVICES_COMPUTED := $(USB_DEVICES_COMPUTED),VENDOR
endif
USB_DEVICES ?= "$(USB_DEVICES_COMPUTED)"
ifndef USB_HID_DEVICES
USB_HID_DEVICES = "KEYBOARD,MOUSE,CONSUMER,GAMEPAD"
USB_DEVICES += VENDOR
endif
ifndef USB_HIGHSPEED
USB_HIGHSPEED = 0
USB_HID_DEVICES =
ifeq ($(CIRCUITPY_USB_HID_CONSUMER),1)
USB_HID_DEVICES += CONSUMER
endif
ifeq ($(CIRCUITPY_USB_HID_DIGITIZER),1)
USB_HID_DEVICES += DIGITIZER
endif
ifeq ($(CIRCUITPY_USB_HID_GAMEPAD),1)
USB_HID_DEVICES += GAMEPAD
endif
ifeq ($(CIRCUITPY_USB_HID_KEYBOARD),1)
USB_HID_DEVICES += KEYBOARD
endif
ifeq ($(CIRCUITPY_USB_HID_MOUSE),1)
USB_HID_DEVICES += MOUSE
endif
ifeq ($(CIRCUITPY_USB_HID_SYS_CONTROL),1)
USB_HID_DEVICES += SYS_CONTROL
endif
ifeq ($(CIRCUITPY_USB_HID_XAC_COMPATIBLE_GAMEPAD),1)
USB_HID_DEVICES += XAC_COMPATIBLE_GAMEPAD
endif
ifndef USB_CDC_EP_NUM_NOTIFICATION
USB_CDC_EP_NUM_NOTIFICATION = 0
# RAW is not compatible with other HID devices.
ifeq ($(CIRCUITPY_USB_HID_RAW),1)
ifneq ($(CIRCUITPY_USB_HID_DEVICES,)
$(error HID RAW must not be combined with other HID devices)
endif
USB_HID_DEVICES += MOUSE
endif
ifndef USB_CDC_EP_NUM_DATA_OUT
USB_CDC_EP_NUM_DATA_OUT = 0
endif
USB_HIGHSPEED ?= 0
ifndef USB_CDC_EP_NUM_DATA_IN
USB_CDC_EP_NUM_DATA_IN = 0
endif
ifndef USB_MSC_EP_NUM_OUT
USB_MSC_EP_NUM_OUT = 0
endif
ifndef USB_MSC_EP_NUM_IN
USB_MSC_EP_NUM_IN = 0
endif
ifndef USB_HID_EP_NUM_OUT
USB_HID_EP_NUM_OUT = 0
endif
ifndef USB_HID_EP_NUM_IN
USB_HID_EP_NUM_IN = 0
endif
ifndef USB_MIDI_EP_NUM_OUT
USB_MIDI_EP_NUM_OUT = 0
endif
ifndef USB_MIDI_EP_NUM_IN
USB_MIDI_EP_NUM_IN = 0
endif
ifndef USB_NUM_EP
USB_NUM_EP = 0
endif
USB_CDC_EP_NUM_NOTIFICATION ?= 0
USB_CDC_EP_NUM_DATA_OUT ?= 0
USB_CDC_EP_NUM_DATA_IN ?= 0
USB_MSC_EP_NUM_OUT ?= 0
USB_MSC_EP_NUM_IN ?= 0
USB_HID_EP_NUM_OUT ?= 0
USB_HID_EP_NUM_IN ?= 0
USB_MIDI_EP_NUM_OUT ?= 0
USB_MIDI_EP_NUM_IN ?= 0
USB_NUM_EP ?= 0
USB_DESCRIPTOR_ARGS = \
--manufacturer $(USB_MANUFACTURER)\
@ -197,8 +215,8 @@ USB_DESCRIPTOR_ARGS = \
--pid $(USB_PID)\
--serial_number_length $(USB_SERIAL_NUMBER_LENGTH)\
--interface_name $(USB_INTERFACE_NAME)\
--devices $(USB_DEVICES)\
--hid_devices $(USB_HID_DEVICES)\
--devices "$(USB_DEVICES)"\
--hid_devices "$(USB_HID_DEVICES)"\
--max_ep $(USB_NUM_EP) \
--cdc_ep_num_notification $(USB_CDC_EP_NUM_NOTIFICATION)\
--cdc_ep_num_data_out $(USB_CDC_EP_NUM_DATA_OUT)\

File diff suppressed because it is too large Load Diff

@ -1 +1 @@
Subproject commit 701cafc50e2e574dccaf7a340eedbd64a0b41a42
Subproject commit 2eaa6114b209fe7f0a795eda8d6a7b3b93d76d2e