summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2018-12-04 15:05:39 -0500
committerDan Halbert <halbert@halwitz.org>2018-12-04 15:05:39 -0500
commit63cd9209f17c6807e96e5011c5dca9862d67da58 (patch)
treeace56a48990f4a7a6e56953db69dd7133ad8a149
parent7f6da78be352ef432d08cfc18f10aa2ffc534196 (diff)
allow KeyboardInterrupt on UART read; fix nrf UART pin claiming; rename feather 52840 UART pins
-rw-r--r--ports/atmel-samd/common-hal/busio/UART.c14
-rw-r--r--ports/nrf/boards/feather_nrf52840_express/pins.c4
-rw-r--r--ports/nrf/common-hal/busio/UART.c18
-rw-r--r--ports/nrf/common-hal/busio/UART.h3
-rw-r--r--shared-bindings/busio/UART.c12
5 files changed, 39 insertions, 12 deletions
diff --git a/ports/atmel-samd/common-hal/busio/UART.c b/ports/atmel-samd/common-hal/busio/UART.c
index 56a984ebc..bb0666577 100644
--- a/ports/atmel-samd/common-hal/busio/UART.c
+++ b/ports/atmel-samd/common-hal/busio/UART.c
@@ -28,6 +28,7 @@
#include "shared-bindings/busio/UART.h"
#include "mpconfigport.h"
+#include "lib/utils/interrupt_char.h"
#include "py/gc.h"
#include "py/mperrno.h"
#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
start_ticks = ticks_ms;
}
#ifdef MICROPY_VM_HOOK_LOOP
- MICROPY_VM_HOOK_LOOP
+ MICROPY_VM_HOOK_LOOP ;
+ // Allow user to break out of a timeout with a KeyboardInterrupt.
+ if (mp_hal_is_interrupted()) {
+ break;
+ }
#endif
- // If we are zero timeout, make sure we don't loop again (in the event
- // we read in under 1ms)
- if (self->timeout_ms == 0)
+ // If we are zero timeout, make sure we don't loop again (in the event
+ // we read in under 1ms)
+ if (self->timeout_ms == 0) {
break;
+ }
}
if (total_read == 0) {
diff --git a/ports/nrf/boards/feather_nrf52840_express/pins.c b/ports/nrf/boards/feather_nrf52840_express/pins.c
index b85b72af0..56e425374 100644
--- a/ports/nrf/boards/feather_nrf52840_express/pins.c
+++ b/ports/nrf/boards/feather_nrf52840_express/pins.c
@@ -35,8 +35,8 @@ STATIC const mp_rom_map_elem_t board_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_P0_13) },
{ MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_P0_15) },
- { MP_ROM_QSTR(MP_QSTR_TXD), MP_ROM_PTR(&pin_P0_25) },
- { MP_ROM_QSTR(MP_QSTR_RXD), MP_ROM_PTR(&pin_P0_24) },
+ { MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_P0_25) },
+ { MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_P0_24) },
{ MP_ROM_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_P0_11) },
{ MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_P0_12) },
diff --git a/ports/nrf/common-hal/busio/UART.c b/ports/nrf/common-hal/busio/UART.c
index 5d0cb9ed9..70641bd79 100644
--- a/ports/nrf/common-hal/busio/UART.c
+++ b/ports/nrf/common-hal/busio/UART.c
@@ -27,6 +27,7 @@
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/busio/UART.h"
+#include "lib/utils/interrupt_char.h"
#include "py/mpconfig.h"
#include "py/gc.h"
#include "py/mperrno.h"
@@ -116,11 +117,15 @@ void common_hal_busio_uart_construct (busio_uart_obj_t *self,
}
self->bufsize = receiver_buffer_size;
+ self->rx_pin_number = rx->number;
claim_pin(rx);
}
if ( tx != mp_const_none ) {
+ self->tx_pin_number = tx->number;
claim_pin(tx);
+ } else {
+ self->tx_pin_number = NO_PIN;
}
self->baudrate = baudrate;
@@ -132,13 +137,16 @@ void common_hal_busio_uart_construct (busio_uart_obj_t *self,
}
bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) {
- return (nrf_uarte_rx_pin_get(self->uarte.p_reg) == NRF_UARTE_PSEL_DISCONNECTED) &&
- (nrf_uarte_tx_pin_get(self->uarte.p_reg) == NRF_UARTE_PSEL_DISCONNECTED);
+ return self->rx_pin_number == NO_PIN;
}
void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
if ( !common_hal_busio_uart_deinited(self) ) {
nrfx_uarte_uninit(&self->uarte);
+ reset_pin_number(self->tx_pin_number);
+ reset_pin_number(self->rx_pin_number);
+ self->tx_pin_number = NO_PIN;
+ self->rx_pin_number = NO_PIN;
gc_free(self->buffer);
}
}
@@ -156,7 +164,11 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
// Wait for on-going transfer to complete
while ( (self->rx_count == -1) && (ticks_ms - start_ticks < self->timeout_ms) ) {
#ifdef MICROPY_VM_HOOK_LOOP
- MICROPY_VM_HOOK_LOOP
+ MICROPY_VM_HOOK_LOOP;
+ // Allow user to break out of a timeout with a KeyboardInterrupt.
+ if (mp_hal_is_interrupted()) {
+ return 0;
+ }
#endif
}
diff --git a/ports/nrf/common-hal/busio/UART.h b/ports/nrf/common-hal/busio/UART.h
index aef377f14..e7543d6b1 100644
--- a/ports/nrf/common-hal/busio/UART.h
+++ b/ports/nrf/common-hal/busio/UART.h
@@ -44,6 +44,9 @@ typedef struct {
uint8_t* buffer;
uint32_t bufsize;
volatile int32_t rx_count;
+
+ uint8_t tx_pin_number;
+ uint8_t rx_pin_number;
} busio_uart_obj_t;
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BUSIO_UART_H
diff --git a/shared-bindings/busio/UART.c b/shared-bindings/busio/UART.c
index 861b36ff2..1cba386d2 100644
--- a/shared-bindings/busio/UART.c
+++ b/shared-bindings/busio/UART.c
@@ -31,6 +31,7 @@
#include "shared-bindings/util.h"
#include "lib/utils/context_manager_helpers.h"
+#include "lib/utils/interrupt_char.h"
#include "py/ioctl.h"
#include "py/objproperty.h"
@@ -56,10 +57,11 @@
//| :param int bits: the number of bits per byte, 7, 8 or 9.
//| :param Parity parity: the parity used for error checking.
//| :param int stop: the number of stop bits, 1 or 2.
-//| :param int timeout: the timeout in seconds to wait for the first character and between subsequent characters.
+//| :param int timeout: the timeout in seconds to wait for the first character and between subsequent characters. Raises ``ValueError`` if timeout >100 seconds.
//| :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.)
//|
//| *New in CircuitPython 4.0:* ``timeout`` has incompatibly changed units from milliseconds to seconds.
+//| The new upper limit on ``timeout`` is meant to catch mistaken use of milliseconds.
typedef struct {
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
mp_raise_ValueError(translate("stop must be 1 or 2"));
}
+ mp_float_t timeout = mp_obj_get_float(args[ARG_timeout].u_obj);
+ if (timeout > 100.0f) {
+ mp_raise_ValueError(translate("timeout >100 (units are now seconds, not msecs)"));
+ }
+
common_hal_busio_uart_construct(self, tx, rx,
- args[ARG_baudrate].u_int, bits, parity, stop,
- mp_obj_get_float(args[ARG_timeout].u_obj),
+ args[ARG_baudrate].u_int, bits, parity, stop, timeout,
args[ARG_receiver_buffer_size].u_int);
return (mp_obj_t)self;
}