8000 mods/machuart.c: Support the UART inverted mode · daq-tools/pycom-micropython@9f1d8bb · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f1d8bb

Browse files
robert-hhamotl
authored andcommitted
mods/machuart.c: Support the UART inverted mode
Inverted mode can be set by a keyword argument to init or the constructor, using invert=<option|option...> options are: INV_RX, INV_TX, INV_RTS and INV_CTS Example: from machine import UART uart=UART(1, 9600, invert=UART.INV_TX|UART.INV_TX)
1 parent 7867134 commit 9f1d8bb

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

esp32/mods/machuart.c

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "esp_system.h"
2626
#include "esp_spi_flash.h"
2727
#include "nvs_flash.h"
28+
#include "esp_event.h"
2829

2930
#include "esp_types.h"
3031
#include "esp_attr.h"
@@ -92,6 +93,7 @@ struct _mach_uart_obj_t {
9293
uint8_t rx_timeout;
9394
uint8_t n_pins;
9495
bool init;
96+
uint32_t invert; // lines to invert
9597
};
9698

9799
/******************************************************************************
@@ -293,10 +295,39 @@ STATIC void mach_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_
293295
mp_printf(print, ", parity=UART.%q", (self->config.parity == UART_PARITY_EVEN) ? MP_QSTR_EVEN : MP_QSTR_ODD);
294296
}
295297
if (self->config.stop_bits == UART_STOP_BITS_1_5) {
296-
mp_printf(print, ", stop=1.5)");
298+
mp_printf(print, ", stop=1.5");
297299
} else {
298-
mp_printf(print, ", stop=%u)", (self->config.stop_bits == UART_STOP_BITS_1) ? 1 : 2);
300+
mp_printf(print, ", stop=%u", (self->config.stop_bits == UART_STOP_BITS_1) ? 1 : 2);
299301
}
302+
if (self->invert) {
303+
mp_printf(print, ", invert=");
304+
uint32_t invert_mask = self->invert;
305+
if (invert_mask & UART_INVERSE_TXD) {
306+
mp_print_str(print, "INV_TX");
307+
invert_mask &= ~UART_INVERSE_TXD;
308+
if (invert_mask) {
309+
mp_print_str(print, "|");
310+
}
311+
}
312+
if (invert_mask & UART_INVERSE_RXD) {
313+
mp_print_str(print, "INV_RX");
314+
invert_mask &= ~UART_INVERSE_RXD;
315+
if (invert_mask) {
316+
mp_print_str(print, "|");
317+
}
318+
}
319+
if (invert_mask & UART_INVERSE_RTS) {
320+
mp_print_str(print, "INV_RTS");
321+
invert_mask &= ~UART_INVERSE_RTS;
322+
if (invert_mask) {
323+
mp_print_str(print, "|");
324+
}
325+
}
326+
if (invert_mask & UART_INVERSE_CTS) {
327+
mp_print_str(print, "INV_CTS");
328+
}
329+
}
330+
mp_printf(print, ")");
300331
} else {
301332
mp_printf(print, "UART(%u)", self->uart_id);
302333
}
@@ -432,6 +463,11 @@ STATIC mp_obj_t mach_uart_init_helper(mach_uart_obj_t *self, const mp_arg_val_t
432463
self->config.rx_flow_ctrl_thresh = 64;
433464
uart_param_config(self->uart_id, &self->config);
434465

466+
self->invert = args[7].u_int;
467+
if (self->invert != 0) {
468+
uart_set_line_inverse(self->uart_id, self->invert);
469+
}
470+
435471
// install the UART driver
436472
uart_driver_install(self->uart_id, rx_buffer_size, 0, 0, NULL, 0, UARTRxCallback);
437473

@@ -458,7 +494,8 @@ STATIC const mp_arg_t mach_uart_init_args[] = {
458494
{ MP_QSTR_stop, MP_ARG_OBJ, {.u_obj = mp_const_none} },
459495
{ MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
460496
{ MP_QSTR_timeout_chars, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 2} },
461-
{ MP_QSTR_rx_buffer_size, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MACHUART_RX_BUFFER_LEN} }
497+
{ MP_QSTR_rx_buffer_size, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MACHUART_RX_BUFFER_LEN} },
498+
{ MP_QSTR_invert, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }
462499
};
463500
STATIC mp_obj_t mach_uart_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *all_args) {
464501
// parse args
@@ -560,9 +597,9 @@ STATIC mp_obj_t mach_uart_sendbreak(mp_uint_t n_args, const mp_obj_t *pos_args,
560597
gpio_matrix_out(pin->pin_number, pin->af_out, false, false);
561598
}
562599

563-
WRITE_PERI_REG(UART_CONF0_REG(self->uart_id), READ_PERI_REG(UART_CONF0_REG(self->uart_id)) | UART_TXD_INV);
600+
WRITE_PERI_REG(UART_CONF0_REG(self->uart_id), READ_PERI_REG(UART_CONF0_REG(self->uart_id)) ^ UART_TXD_INV);
564601
ets_delay_us((delay > 0) ? delay : 1);
565-
WRITE_PERI_REG(UART_CONF0_REG(self->uart_id), READ_PERI_REG(UART_CONF0_REG(self->uart_id)) & (~UART_TXD_INV));
602+
WRITE_PERI_REG(UART_CONF0_REG(self->uart_id), READ_PERI_REG(UART_CONF0_REG(self->uart_id)) ^ UART_TXD_INV);
566603

567604
if (self->n_pins == 1) {
568605
// make it an input again
@@ -595,6 +632,11 @@ STATIC const mp_map_elem_t mach_uart_locals_dict_table[] = {
595632
// class constants
596633
{ MP_OBJ_NEW_QSTR(MP_QSTR_EVEN), MP_OBJ_NEW_SMALL_INT(UART_PARITY_EVEN) },
597634
{ MP_OBJ_NEW_QSTR(MP_QSTR_ODD), MP_OBJ_NEW_SMALL_INT(UART_PARITY_ODD) },
635+
{ MP_OBJ_NEW_QSTR(MP_QSTR_INV_RX), MP_OBJ_NEW_SMALL_INT(UART_INVERSE_RXD) },
636+
{ MP_OBJ_NEW_QSTR(MP_QSTR_INV_CTS), MP_OBJ_NEW_SMALL_INT(UART_INVERSE_CTS) },
637+
{ MP_OBJ_NEW_QSTR(MP_QSTR_INV_TX), MP_OBJ_NEW_SMALL_INT(UART_INVERSE_TXD) },
638+
{ MP_OBJ_NEW_QSTR(MP_QSTR_INV_RTS), MP_OBJ_NEW_SMALL_INT(UART_INVERSE_RTS) },
639+
598640
// { MP_OBJ_NEW_QSTR(MP_QSTR_RX_ANY), MP_OBJ_NEW_SMALL_INT(2) },
599641
};
600642
STATIC MP_DEFINE_CONST_DICT(mach_uart_locals_dict, mach_uart_locals_dict_table);

0 commit comments

Comments
 (0)
0