summaryrefslogtreecommitdiff
path: root/ports
diff options
context:
space:
mode:
authorScott Shawcroft <scott@tannewt.org>2018-12-04 13:47:25 -0800
committerGitHub <noreply@github.com>2018-12-04 13:47:25 -0800
commitdbd3a77908343dd036ab1cd3b76b6fa4f9d85e8c (patch)
tree61c6b527e02a4c663ccfab686cf2049b0ca5a9f8 /ports
parent7f6da78be352ef432d08cfc18f10aa2ffc534196 (diff)
parent8b034b8d1d7303b813a9c0508fdbb74dd19ec1e4 (diff)
Merge pull request #1375 from dhalbert/uart-timeout-interrupt
allow KeyboardInterrupt on UART read; fix nrf UART pin claiming; rename feather 52840 UART pins
Diffstat (limited to 'ports')
-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
4 files changed, 30 insertions, 9 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