Skip to content

Commit 63cd920

Browse files
committed
allow KeyboardInterrupt on UART read; fix nrf UART pin claiming; rename feather 52840 UART pins
1 parent 7f6da78 commit 63cd920

5 files changed

Lines changed: 39 additions & 12 deletions

File tree

ports/atmel-samd/common-hal/busio/UART.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "shared-bindings/busio/UART.h"
2929

3030
#include "mpconfigport.h"
31+
#include "lib/utils/interrupt_char.h"
3132
#include "py/gc.h"
3233
#include "py/mperrno.h"
3334
#include "py/runtime.h"
@@ -272,12 +273,17 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
272273
start_ticks = ticks_ms;
273274
}
274275
#ifdef MICROPY_VM_HOOK_LOOP
275-
MICROPY_VM_HOOK_LOOP
276+
MICROPY_VM_HOOK_LOOP ;
277+
// Allow user to break out of a timeout with a KeyboardInterrupt.
278+
if (mp_hal_is_interrupted()) {
279+
break;
280+
}
276281
#endif
277-
// If we are zero timeout, make sure we don't loop again (in the event
278-
// we read in under 1ms)
279-
if (self->timeout_ms == 0)
282+
// If we are zero timeout, make sure we don't loop again (in the event
283+
// we read in under 1ms)
284+
if (self->timeout_ms == 0) {
280285
break;
286+
}
281287
}
282288

283289
if (total_read == 0) {

ports/nrf/boards/feather_nrf52840_express/pins.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ STATIC const mp_rom_map_elem_t board_module_globals_table[] = {
3535
{ MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_P0_13) },
3636
{ MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_P0_15) },
3737

38-
{ MP_ROM_QSTR(MP_QSTR_TXD), MP_ROM_PTR(&pin_P0_25) },
39-
{ MP_ROM_QSTR(MP_QSTR_RXD), MP_ROM_PTR(&pin_P0_24) },
38+
{ MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_P0_25) },
39+
{ MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_P0_24) },
4040

4141
{ MP_ROM_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_P0_11) },
4242
{ MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_P0_12) },

ports/nrf/common-hal/busio/UART.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "shared-bindings/microcontroller/__init__.h"
2828
#include "shared-bindings/busio/UART.h"
2929

30+
#include "lib/utils/interrupt_char.h"
3031
#include "py/mpconfig.h"
3132
#include "py/gc.h"
3233
#include "py/mperrno.h"
@@ -116,11 +117,15 @@ void common_hal_busio_uart_construct (busio_uart_obj_t *self,
116117
}
117118
self->bufsize = receiver_buffer_size;
118119

120+
self->rx_pin_number = rx->number;
119121
claim_pin(rx);
120122
}
121123

122124
if ( tx != mp_const_none ) {
125+
self->tx_pin_number = tx->number;
123126
claim_pin(tx);
127+
} else {
128+
self->tx_pin_number = NO_PIN;
124129
}
125130

126131
self->baudrate = baudrate;
@@ -132,13 +137,16 @@ void common_hal_busio_uart_construct (busio_uart_obj_t *self,
132137
}
133138

134139
bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) {
135-
return (nrf_uarte_rx_pin_get(self->uarte.p_reg) == NRF_UARTE_PSEL_DISCONNECTED) &&
136-
(nrf_uarte_tx_pin_get(self->uarte.p_reg) == NRF_UARTE_PSEL_DISCONNECTED);
140+
return self->rx_pin_number == NO_PIN;
137141
}
138142

139143
void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
140144
if ( !common_hal_busio_uart_deinited(self) ) {
141145
nrfx_uarte_uninit(&self->uarte);
146+
reset_pin_number(self->tx_pin_number);
147+
reset_pin_number(self->rx_pin_number);
148+
self->tx_pin_number = NO_PIN;
149+
self->rx_pin_number = NO_PIN;
142150
gc_free(self->buffer);
143151
}
144152
}
@@ -156,7 +164,11 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
156164
// Wait for on-going transfer to complete
157165
while ( (self->rx_count == -1) && (ticks_ms - start_ticks < self->timeout_ms) ) {
158166
#ifdef MICROPY_VM_HOOK_LOOP
159-
MICROPY_VM_HOOK_LOOP
167+
MICROPY_VM_HOOK_LOOP;
168+
// Allow user to break out of a timeout with a KeyboardInterrupt.
169+
if (mp_hal_is_interrupted()) {
170+
return 0;
171+
}
160172
#endif
161173
}
162174

ports/nrf/common-hal/busio/UART.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ typedef struct {
4444
uint8_t* buffer;
4545
uint32_t bufsize;
4646
volatile int32_t rx_count;
47+
48+
uint8_t tx_pin_number;
49+
uint8_t rx_pin_number;
4750
} busio_uart_obj_t;
4851

4952
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BUSIO_UART_H

shared-bindings/busio/UART.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "shared-bindings/util.h"
3232

3333
#include "lib/utils/context_manager_helpers.h"
34+
#include "lib/utils/interrupt_char.h"
3435

3536
#include "py/ioctl.h"
3637
#include "py/objproperty.h"
@@ -56,10 +57,11 @@
5657
//| :param int bits: the number of bits per byte, 7, 8 or 9.
5758
//| :param Parity parity: the parity used for error checking.
5859
//| :param int stop: the number of stop bits, 1 or 2.
59-
//| :param int timeout: the timeout in seconds to wait for the first character and between subsequent characters.
60+
//| :param int timeout: the timeout in seconds to wait for the first character and between subsequent characters. Raises ``ValueError`` if timeout >100 seconds.
6061
//| :param int receiver_buffer_size: the character length of the read buffer (0 to disable). (When a character is 9 bits the buffer will be 2 * receiver_buffer_size bytes.)
6162
//|
6263
//| *New in CircuitPython 4.0:* ``timeout`` has incompatibly changed units from milliseconds to seconds.
64+
//| The new upper limit on ``timeout`` is meant to catch mistaken use of milliseconds.
6365

6466
typedef struct {
6567
mp_obj_base_t base;
@@ -116,9 +118,13 @@ STATIC mp_obj_t busio_uart_make_new(const mp_obj_type_t *type, size_t n_args, si
116118
mp_raise_ValueError(translate("stop must be 1 or 2"));
117119
}
118120

121+
mp_float_t timeout = mp_obj_get_float(args[ARG_timeout].u_obj);
122+
if (timeout > 100.0f) {
123+
mp_raise_ValueError(translate("timeout >100 (units are now seconds, not msecs)"));
124+
}
125+
119126
common_hal_busio_uart_construct(self, tx, rx,
120-
args[ARG_baudrate].u_int, bits, parity, stop,
121-
mp_obj_get_float(args[ARG_timeout].u_obj),
127+
args[ARG_baudrate].u_int, bits, parity, stop, timeout,
122128
args[ARG_receiver_buffer_size].u_int);
123129
return (mp_obj_t)self;
124130
}

0 commit comments

Comments
 (0)