Skip to content

Commit d54b586

Browse files
committed
wip
1 parent f0564b4 commit d54b586

File tree

19 files changed

+566
-124
lines changed

19 files changed

+566
-124
lines changed

ports/atmel-samd/mpconfigport.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ CIRCUITPY_SDCARDIO ?= 0
4949
CIRCUITPY_FRAMEBUFFERIO ?= 0
5050

5151
# SAMD21 needs separate endpoint pairs for MSC BULK IN and BULK OUT, otherwise it's erratic.
52-
# Because of that, there aren't enough endpoints for serial2.
52+
# Because of that, there aren't enough endpoints for a secondary CDC serial connection.
5353
USB_MSC_EP_NUM_OUT = 1
54-
CIRCUITPY_USB_SERIAL2 = 0
54+
CIRCUITPY_USB_CDC = 0
5555

5656
CIRCUITPY_ULAB = 0
5757

ports/nrf/bluetooth/ble_uart.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#include "shared-bindings/_bleio/Service.h"
4141
#include "shared-bindings/_bleio/UUID.h"
4242

43-
#if CIRCUITPY_SERIAL_BLE
43+
#if CIRCUITPY_REPL_BLE
4444

4545
static const char default_name[] = "CP-REPL"; // max 8 chars or uuid won't fit in adv data
4646
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) {
190190
}
191191
}
192192

193-
#endif // CIRCUITPY_SERIAL_BLE
193+
#endif // CIRCUITPY_REPL_BLE

ports/nrf/common-hal/supervisor/Runtime.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,3 @@ bool common_hal_supervisor_runtime_get_serial_connected(void) {
3535
bool common_hal_get_supervisor_runtime_serial_bytes_available(void) {
3636
return (bool) serial_bytes_available();
3737
}
38-
39-
#if CIRCUITPY_USB_SERIAL2
40-
mp_obj_t common_hal_supervisor_runtime_get_serial2(void) {
41-
return (bool) serial_connected();
42-
}
43-
44-
bool common_hal_supervisor_runtime_get_serial2_connected(void) {
45-
return (bool) serial_connected();
46-
}
47-
48-
bool common_hal_get_supervisor_runtime_serial2_bytes_available(void) {
49-
return (bool) serial_bytes_available();
50-
}
51-
#endif

ports/nrf/supervisor/serial.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@
2828

2929
#include "supervisor/serial.h"
3030

31-
#if CIRCUITPY_SERIAL_BLE
31+
#if CIRCUITPY_REPL_BLE
3232
#include "ble_uart.h"
33-
#elif CIRCUITPY_SERIAL_UART
33+
#elif CIRCUITPY_REPL_UART
3434
#include <string.h>
3535
#include "nrf_gpio.h"
3636
#include "nrfx_uarte.h"
3737
#endif
3838

39-
#if CIRCUITPY_SERIAL_BLE
39+
#if CIRCUITPY_REPL_BLE
4040

4141
void serial_init(void) {
4242
ble_uart_init();
@@ -58,7 +58,7 @@ void serial_write(const char *text) {
5858
ble_uart_stdout_tx_str(text);
5959
}
6060

61-
#elif CIRCUITPY_SERIAL_UART
61+
#elif CIRCUITPY_REPL_UART
6262

6363
uint8_t serial_received_char;
6464
nrfx_uarte_t serial_instance = NRFX_UARTE_INSTANCE(0);
@@ -124,4 +124,4 @@ void serial_write_substring(const char *text, uint32_t len) {
124124
}
125125
}
126126

127-
#endif // CIRCUITPY_SERIAL_UART
127+
#endif // CIRCUITPY_REPL_UART

ports/raspberrypi/common-hal/pwmio/PWMOut.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ void common_hal_pwmio_pwmout_deinit(pwmio_pwmout_obj_t* self) {
164164
extern void common_hal_pwmio_pwmout_set_duty_cycle(pwmio_pwmout_obj_t* self, uint16_t duty) {
165165
self->duty_cycle = duty;
166166
uint16_t actual_duty = duty * self->top / ((1 << 16) - 1);
167+
mp_printf(&mp_plat_print, "actual_duty: %d, self->top: %d\n", actual_duty, top); /// ***
167168
pwm_set_chan_level(self->slice, self->channel, actual_duty);
168169
}
169170

@@ -200,6 +201,7 @@ void common_hal_pwmio_pwmout_set_frequency(pwmio_pwmout_obj_t* self, uint32_t fr
200201
} else {
201202
uint32_t top = common_hal_mcu_processor_get_frequency() / frequency;
202203
self->actual_frequency = common_hal_mcu_processor_get_frequency() / top;
204+
mp_printf(&mp_plat_print, "high speed self->top: %d\n", top); /// ***
203205
self->top = top;
204206
pwm_set_clkdiv_int_frac(self->slice, 1, 0);
205207
pwm_set_wrap(self->slice, self->top - 1);

py/circuitpy_defns.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ endif
286286
ifeq ($(CIRCUITPY_UHEAP),1)
287287
SRC_PATTERNS += uheap/%
288288
endif
289+
ifeq ($(CIRCUITPY_USB_CDC),1)
290+
SRC_PATTERNS += usb_cdc/%
291+
endif
289292
ifeq ($(CIRCUITPY_USB_HID),1)
290293
SRC_PATTERNS += usb_hid/%
291294
endif

py/circuitpy_mpconfig.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,13 @@ extern const struct _mp_obj_module_t uheap_module;
736736
#define UHEAP_MODULE
737737
#endif
738738

739+
#if CIRCUITPY_USB_CDC
740+
extern const struct _mp_obj_module_t usb_cdc_module;
741+
#define USB_CDC_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_usb_cdc),(mp_obj_t)&usb_cdc_module },
742+
#else
743+
#define USB_CDC_MODULE
744+
#endif
745+
739746
#if CIRCUITPY_USB_HID
740747
extern const struct _mp_obj_module_t usb_hid_module;
741748
#define USB_HID_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid),(mp_obj_t)&usb_hid_module },
@@ -875,6 +882,7 @@ extern const struct _mp_obj_module_t msgpack_module;
875882
SUPERVISOR_MODULE \
876883
TOUCHIO_MODULE \
877884
UHEAP_MODULE \
885+
USB_CDC_MODULE \
878886
USB_HID_MODULE \
879887
USB_MIDI_MODULE \
880888
USTACK_MODULE \

py/circuitpy_mpconfig.mk

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,15 @@ CFLAGS += -DCIRCUITPY_RANDOM=$(CIRCUITPY_RANDOM)
232232
CIRCUITPY_RE ?= $(CIRCUITPY_FULL_BUILD)
233233
CFLAGS += -DCIRCUITPY_RE=$(CIRCUITPY_RE)
234234

235+
CIRCUITPY_REPL_BLE ?= 0
236+
CFLAGS += -DCIRCUITPY_REPL_BLE=$(CIRCUITPY_REPL_BLE)
237+
238+
CIRCUITPY_REPL_UART ?= 0
239+
CFLAGS += -DCIRCUITPY_REPL_UART=$(CIRCUITPY_REPL_UART)
240+
241+
CIRCUITPY_REPL_USB ?= 1
242+
CFLAGS += -DCIRCUITPY_REPL_USB=$(CIRCUITPY_REPL_USB)
243+
235244
# Should busio.I2C() check for pullups?
236245
# Some boards in combination with certain peripherals may not want this.
237246
CIRCUITPY_REQUIRE_I2C_PULLUPS ?= 1
@@ -264,18 +273,6 @@ CFLAGS += -DCIRCUITPY_SDCARDIO=$(CIRCUITPY_SDCARDIO)
264273
CIRCUITPY_SDIOIO ?= 0
265274
CFLAGS += -DCIRCUITPY_SDIOIO=$(CIRCUITPY_SDIOIO)
266275

267-
# Second USB CDC serial channel.
268-
CIRCUITPY_SERIAL2 ?=
269-
CFLAGS += -DCIRCUITPY_SERIAL2=$(CIRCUITPY_SERIAL2)
270-
271-
# REPL over BLE
272-
CIRCUITPY_SERIAL_BLE ?= 0
273-
CFLAGS += -DCIRCUITPY_SERIAL_BLE=$(CIRCUITPY_SERIAL_BLE)
274-
275-
# REPL over UART
276-
CIRCUITPY_SERIAL_UART ?= 0
277-
CFLAGS += -DCIRCUITPY_SERIAL_UART=$(CIRCUITPY_SERIAL_UART)
278-
279276
CIRCUITPY_SHARPDISPLAY ?= $(CIRCUITPY_FRAMEBUFFERIO)
280277
CFLAGS += -DCIRCUITPY_SHARPDISPLAY=$(CIRCUITPY_SHARPDISPLAY)
281278

@@ -315,6 +312,10 @@ CFLAGS += -DCIRCUITPY_TOUCHIO=$(CIRCUITPY_TOUCHIO)
315312
CIRCUITPY_UHEAP ?= 0
316313
CFLAGS += -DCIRCUITPY_UHEAP=$(CIRCUITPY_UHEAP)
317314

315+
# Secondary CDC is usually available if there are at least 8 endpoints.
316+
CIRCUITPY_USB_CDC ?= $(shell expr $(USB_NUM_EP) '>=' 8)
317+
CFLAGS += -DCIRCUITPY_USB_CDC=$(CIRCUITPY_USB_CDC)
318+
318319
CIRCUITPY_USB_HID ?= 1
319320
CFLAGS += -DCIRCUITPY_USB_HID=$(CIRCUITPY_USB_HID)
320321

@@ -345,20 +346,13 @@ CFLAGS += -DCIRCUITPY_USB_MIDI=$(CIRCUITPY_USB_MIDI)
345346
CIRCUITPY_USB_MSC ?= 1
346347
CFLAGS += -DCIRCUITPY_USB_MSC=$(CIRCUITPY_USB_MSC)
347348

348-
CIRCUITPY_USB_SERIAL ?= 1
349-
CFLAGS += -DCIRCUITPY_USB_SERIAL=$(CIRCUITPY_USB_SERIAL)
350-
351349
CIRCUITPY_USB_VENDOR ?= 0
352350
CFLAGS += -DCIRCUITPY_USB_VENDOR=$(CIRCUITPY_USB_VENDOR)
353351

354352
ifndef USB_NUM_EP
355353
$(error "USB_NUM_EP (number of USB endpoint pairs)must be defined")
356354
endif
357355

358-
# Secondary CDC is usually available if there are at least 8 endpoints.
359-
CIRCUITPY_USB_SERIAL2 ?= $(shell expr $(USB_NUM_EP) '>=' 8)
360-
CFLAGS += -DCIRCUITPY_USB_SERIAL2=$(CIRCUITPY_USB_SERIAL2)
361-
362356
# For debugging.
363357
CIRCUITPY_USTACK ?= 0
364358
CFLAGS += -DCIRCUITPY_USTACK=$(CIRCUITPY_USTACK)

shared-bindings/usb_cdc/Serial.c

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Dan Halbert for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdint.h>
28+
29+
#include "shared-bindings/usb_cdc/Serial.h"
30+
#include "shared-bindings/util.h"
31+
32+
#include "py/ioctl.h"
33+
#include "py/objproperty.h"
34+
#include "py/runtime.h"
35+
#include "py/stream.h"
36+
#include "supervisor/shared/translate.h"
37+
38+
//| class Serial:
39+
//| """Receives cdc commands over USB"""
40+
//|
41+
//| def __init__(self) -> None:
42+
//| """You cannot create an instance of `usb_cdc.Serial`.
43+
//|
44+
//| Serial objects are constructed for every corresponding entry in the USB
45+
//| descriptor and added to the ``usb_cdc.ports`` tuple."""
46+
//| ...
47+
//|
48+
49+
// These are standard stream methods. Code is in py/stream.c.
50+
//
51+
//| def read(self, nbytes: Optional[int] = None) -> Optional[bytes]:
52+
//| """Read characters. If ``nbytes`` is specified then read at most that many
53+
//| bytes. Otherwise, read everything that arrives until the connection
54+
//| times out. Providing the number of bytes expected is highly recommended
55+
//| because it will be faster.
56+
//|
57+
//| :return: Data read
58+
//| :rtype: bytes or None"""
59+
//| ...
60+
//|
61+
//| def readinto(self, buf: WriteableBuffer, nbytes: Optional[int] = None) -> Optional[bytes]:
62+
//| """Read bytes into the ``buf``. If ``nbytes`` is specified then read at most
63+
//| that many bytes. Otherwise, read at most ``len(buf)`` bytes.
64+
//|
65+
//| :return: number of bytes read and stored into ``buf``
66+
//| :rtype: bytes or None"""
67+
//| ...
68+
//|
69+
//| def write(self, buf: ReadableBuffer) -> Optional[int]:
70+
//| """Write the buffer of bytes to the bus.
71+
//|
72+
//| :return: the number of bytes written
73+
//| :rtype: int or None"""
74+
//| ...
75+
//|
76+
77+
// These three methods are used by the shared stream methods.
78+
STATIC mp_uint_t usb_cdc_serial_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
79+
usb_cdc_serial_obj_t *self = MP_OBJ_TO_PTR(self_in);
80+
byte *buf = buf_in;
81+
82+
// make sure we want at least 1 char
83+
if (size == 0) {
84+
return 0;
85+
}
86+
87+
return common_hal_usb_cdc_serial_read(self, buf, size, errcode);
88+
}
89+
90+
STATIC mp_uint_t usb_cdc_serial_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) {
91+
usb_cdc_serial_obj_t *self = MP_OBJ_TO_PTR(self_in);
92+
const byte *buf = buf_in;
93+
94+
return common_hal_usb_cdc_serial_write(self, buf, size, errcode);
95+
}
96+
97+
STATIC mp_uint_t usb_cdc_serial_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
98+
usb_cdc_serial_obj_t *self = MP_OBJ_TO_PTR(self_in);
99+
mp_uint_t ret;
100+
if (request == MP_IOCTL_POLL) {
101+
mp_uint_t flags = arg;
102+
ret = 0;
103+
if ((flags & MP_IOCTL_POLL_RD) && common_hal_usb_cdc_serial_bytes_available(self) > 0) {
104+
ret |= MP_IOCTL_POLL_RD;
105+
}
106+
if ((flags & MP_IOCTL_POLL_WR) && common_hal_usb_cdc_serial_ready_to_tx(self)) {
107+
ret |= MP_IOCTL_POLL_WR;
108+
}
109+
} else {
110+
*errcode = MP_EINVAL;
111+
ret = MP_STREAM_ERROR;
112+
}
113+
return ret;
114+
}
115+
116+
STATIC const mp_rom_map_elem_t usb_cdc_serial_locals_dict_table[] = {
117+
// Standard stream methods.
118+
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
119+
{ MP_OBJ_NEW_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
120+
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
121+
};
122+
STATIC MP_DEFINE_CONST_DICT(usb_cdc_serial_locals_dict, usb_cdc_serial_locals_dict_table);
123+
124+
STATIC const mp_stream_p_t usb_cdc_serial_stream_p = {
125+
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_stream)
126+
.read = usb_cdc_serial_read,
127+
.write = usb_cdc_serial_write,
128+
.ioctl = usb_cdc_serial_ioctl,
129+
.is_text = false,
130+
};
131+
132+
const mp_obj_type_t usb_cdc_serial_type = {
133+
{ &mp_type_type },
134+
.name = MP_QSTR_Serial,
135+
.getiter = mp_identity_getiter,
136+
.iternext = mp_stream_unbuffered_iter,
137+
.protocol = &usb_cdc_serial_stream_p,
138+
.locals_dict = (mp_obj_dict_t*)&usb_cdc_serial_locals_dict,
139+
};

shared-bindings/usb_cdc/Serial.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Dan Halbert for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_USB_CDC_SERIAL_H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_USB_CDC_SERIAL_H
29+
30+
#include "shared-module/usb_cdc/Serial.h"
31+
32+
extern const mp_obj_type_t usb_cdc_serial_type;
33+
34+
extern size_t common_hal_usb_cdc_serial_read(usb_cdc_serial_obj_t *self, uint8_t *data, size_t len, int *errcode);
35+
extern uint32_t common_hal_usb_cdc_serial_bytes_available(usb_cdc_serial_obj_t *self);
36+
extern void common_hal_usb_cdc_serial_clear_buffer(usb_cdc_serial_obj_t *self);
37+
extern size_t common_hal_usb_cdc_serial_write(usb_cdc_serial_obj_t *self, const uint8_t *data, size_t len, int *errcode);
38+
extern bool common_hal_usb_cdc_serial_ready_to_tx(usb_cdc_serial_obj_t *self);
39+
40+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_USB_CDC_SERIAL_H

0 commit comments

Comments
 (0)