summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2019-08-21 21:16:40 +1000
committerDamien George <damien.p.george@gmail.com>2019-08-21 21:16:40 +1000
commit96ace8082e409c582911239ee913a95d955058bf (patch)
tree65bec04436d2b4c7d88cc9da56aa3a32d620c00d
parentfe3c064d42daeb02ee9a6ac91f657d397c0a20fa (diff)
esp8266/machine_uart: Allow remapping UART TX/RX pins from 1/3 to 15/13.
Via the standard tx/rx arguments: UART(0, 115200, tx=Pin(15), rx=Pin(13)). Resolves issue #4718.
-rw-r--r--ports/esp8266/machine_uart.c24
1 files changed, 21 insertions, 3 deletions
diff --git a/ports/esp8266/machine_uart.c b/ports/esp8266/machine_uart.c
index 21336c7fd..9aaa16092 100644
--- a/ports/esp8266/machine_uart.c
+++ b/ports/esp8266/machine_uart.c
@@ -29,11 +29,13 @@
#include <string.h>
#include "ets_sys.h"
+#include "user_interface.h"
#include "uart.h"
#include "py/runtime.h"
#include "py/stream.h"
#include "py/mperrno.h"
+#include "py/mphal.h"
#include "modmachine.h"
// UartDev is defined and initialized in rom code.
@@ -63,14 +65,14 @@ STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k
}
STATIC void pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
- enum { ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_rxbuf, ARG_timeout, ARG_timeout_char };
+ enum { ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_tx, ARG_rx, ARG_rxbuf, ARG_timeout, ARG_timeout_char };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_bits, MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_parity, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_stop, MP_ARG_INT, {.u_int = 0} },
- //{ MP_QSTR_tx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
- //{ MP_QSTR_rx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
+ { MP_QSTR_tx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
+ { MP_QSTR_rx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_rxbuf, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_timeout_char, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
@@ -128,6 +130,22 @@ STATIC void pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const mp_o
}
}
+ // set tx/rx pins
+ mp_hal_pin_obj_t tx = 1, rx = 3;
+ if (args[ARG_tx].u_obj != MP_OBJ_NULL) {
+ tx = mp_hal_get_pin_obj(args[ARG_tx].u_obj);
+ }
+ if (args[ARG_rx].u_obj != MP_OBJ_NULL) {
+ rx = mp_hal_get_pin_obj(args[ARG_rx].u_obj);
+ }
+ if (tx == 1 && rx == 3) {
+ system_uart_de_swap();
+ } else if (tx == 15 && rx == 13) {
+ system_uart_swap();
+ } else {
+ mp_raise_ValueError("invalid tx/rx");
+ }
+
// set stop bits
switch (args[ARG_stop].u_int) {
case 0: