allow KeyboardInterrupt on UART read; fix nrf UART pin claiming; rename feather 52840 UART pins

This commit is contained in:
Dan Halbert 2018-12-04 15:05:39 -05:00
parent 7f6da78be3
commit 63cd9209f1
5 changed files with 40 additions and 13 deletions

View File

@ -28,6 +28,7 @@
#include "shared-bindings/busio/UART.h" #include "shared-bindings/busio/UART.h"
#include "mpconfigport.h" #include "mpconfigport.h"
#include "lib/utils/interrupt_char.h"
#include "py/gc.h" #include "py/gc.h"
#include "py/mperrno.h" #include "py/mperrno.h"
#include "py/runtime.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; start_ticks = ticks_ms;
} }
#ifdef MICROPY_VM_HOOK_LOOP #ifdef MICROPY_VM_HOOK_LOOP
MICROPY_VM_HOOK_LOOP MICROPY_VM_HOOK_LOOP ;
#endif // Allow user to break out of a timeout with a KeyboardInterrupt.
// If we are zero timeout, make sure we don't loop again (in the event if (mp_hal_is_interrupted()) {
// we read in under 1ms)
if (self->timeout_ms == 0)
break; 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) {
break;
}
} }
if (total_read == 0) { if (total_read == 0) {

View File

@ -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_MOSI), MP_ROM_PTR(&pin_P0_13) },
{ MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_P0_15) }, { 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_TX), MP_ROM_PTR(&pin_P0_25) },
{ MP_ROM_QSTR(MP_QSTR_RXD), MP_ROM_PTR(&pin_P0_24) }, { 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_SCL), MP_ROM_PTR(&pin_P0_11) },
{ MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_P0_12) }, { MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_P0_12) },

View File

@ -27,6 +27,7 @@
#include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/busio/UART.h" #include "shared-bindings/busio/UART.h"
#include "lib/utils/interrupt_char.h"
#include "py/mpconfig.h" #include "py/mpconfig.h"
#include "py/gc.h" #include "py/gc.h"
#include "py/mperrno.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->bufsize = receiver_buffer_size;
self->rx_pin_number = rx->number;
claim_pin(rx); claim_pin(rx);
} }
if ( tx != mp_const_none ) { if ( tx != mp_const_none ) {
self->tx_pin_number = tx->number;
claim_pin(tx); claim_pin(tx);
} else {
self->tx_pin_number = NO_PIN;
} }
self->baudrate = baudrate; 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) { 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) && return self->rx_pin_number == NO_PIN;
(nrf_uarte_tx_pin_get(self->uarte.p_reg) == NRF_UARTE_PSEL_DISCONNECTED);
} }
void common_hal_busio_uart_deinit(busio_uart_obj_t *self) { void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
if ( !common_hal_busio_uart_deinited(self) ) { if ( !common_hal_busio_uart_deinited(self) ) {
nrfx_uarte_uninit(&self->uarte); 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); 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 // Wait for on-going transfer to complete
while ( (self->rx_count == -1) && (ticks_ms - start_ticks < self->timeout_ms) ) { while ( (self->rx_count == -1) && (ticks_ms - start_ticks < self->timeout_ms) ) {
#ifdef MICROPY_VM_HOOK_LOOP #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 #endif
} }

View File

@ -44,6 +44,9 @@ typedef struct {
uint8_t* buffer; uint8_t* buffer;
uint32_t bufsize; uint32_t bufsize;
volatile int32_t rx_count; volatile int32_t rx_count;
uint8_t tx_pin_number;
uint8_t rx_pin_number;
} busio_uart_obj_t; } busio_uart_obj_t;
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BUSIO_UART_H #endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BUSIO_UART_H

View File

@ -31,6 +31,7 @@
#include "shared-bindings/util.h" #include "shared-bindings/util.h"
#include "lib/utils/context_manager_helpers.h" #include "lib/utils/context_manager_helpers.h"
#include "lib/utils/interrupt_char.h"
#include "py/ioctl.h" #include "py/ioctl.h"
#include "py/objproperty.h" #include "py/objproperty.h"
@ -56,10 +57,11 @@
//| :param int bits: the number of bits per byte, 7, 8 or 9. //| :param int bits: the number of bits per byte, 7, 8 or 9.
//| :param Parity parity: the parity used for error checking. //| :param Parity parity: the parity used for error checking.
//| :param int stop: the number of stop bits, 1 or 2. //| :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.) //| :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. //| *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 { typedef struct {
mp_obj_base_t base; 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_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, common_hal_busio_uart_construct(self, tx, rx,
args[ARG_baudrate].u_int, bits, parity, stop, args[ARG_baudrate].u_int, bits, parity, stop, timeout,
mp_obj_get_float(args[ARG_timeout].u_obj),
args[ARG_receiver_buffer_size].u_int); args[ARG_receiver_buffer_size].u_int);
return (mp_obj_t)self; return (mp_obj_t)self;
} }