From ed634e8e71c9ceac56e04eda3a0acb894e443949 Mon Sep 17 00:00:00 2001 From: Jaap de Wolff Date: Fri, 20 Nov 2020 21:45:34 +0100 Subject: [PATCH] Add support for inverting each input and output UART signal --- esp-link/cgipins.c | 12 +++--- esp-link/config.h | 1 + esp-link/main.c | 2 +- html/home.html | 44 +++++++++++++++++++ html/style.css | 4 ++ html/ui.js | 104 ++++++++++++++++++++++++++++++++++++++++++--- include/uart_hw.h | 15 ++++++- serial/console.c | 2 +- serial/serbridge.c | 12 ++++-- serial/uart.c | 8 ++-- serial/uart.h | 2 +- 11 files changed, 185 insertions(+), 21 deletions(-) diff --git a/esp-link/cgipins.c b/esp-link/cgipins.c index b763d12..c2f0f58 100644 --- a/esp-link/cgipins.c +++ b/esp-link/cgipins.c @@ -32,9 +32,9 @@ int ICACHE_FLASH_ATTR cgiPinsGet(HttpdConnData *connData) { int len; len = os_sprintf(buff, - "{ \"reset\":%d, \"isp\":%d, \"conn\":%d, \"ser\":%d, \"swap\":%d, \"rxpup\":%d }", + "{ \"reset\":%d, \"isp\":%d, \"conn\":%d, \"ser\":%d, \"swap\":%d, \"rxpup\":%d, \"pinvert\":%d }", flashConfig.reset_pin, flashConfig.isp_pin, flashConfig.conn_led_pin, - flashConfig.ser_led_pin, !!flashConfig.swap_uart, !!flashConfig.rx_pullup); + flashConfig.ser_led_pin, !!flashConfig.swap_uart, !!flashConfig.rx_pullup, flashConfig.pin_invert); jsonHeader(connData, 200); httpdSend(connData, buff, len); @@ -49,13 +49,14 @@ int ICACHE_FLASH_ATTR cgiPinsSet(HttpdConnData *connData) { int8_t ok = 0; int8_t reset, isp, conn, ser; - uint8_t swap, rxpup; + uint8_t swap, rxpup, pinvert; ok |= getInt8Arg(connData, "reset", &reset); ok |= getInt8Arg(connData, "isp", &isp); ok |= getInt8Arg(connData, "conn", &conn); ok |= getInt8Arg(connData, "ser", &ser); ok |= getBoolArg(connData, "swap", &swap); ok |= getBoolArg(connData, "rxpup", &rxpup); + ok |= getUInt8Arg(connData, "pinvert", &pinvert); if (ok < 0) return HTTPD_CGI_DONE; char *coll; @@ -90,8 +91,9 @@ int ICACHE_FLASH_ATTR cgiPinsSet(HttpdConnData *connData) { flashConfig.ser_led_pin = ser; flashConfig.swap_uart = swap; flashConfig.rx_pullup = rxpup; - os_printf("Pins changed: reset=%d isp=%d conn=%d ser=%d swap=%d rx-pup=%d\n", - reset, isp, conn, ser, swap, rxpup); + flashConfig.pin_invert = pinvert; + os_printf("Pins changed: reset=%d isp=%d conn=%d ser=%d swap=%d rx-pup=%d pinvert=%X\n", + reset, isp, conn, ser, swap, rxpup, pinvert); // apply the changes serbridgeInitPins(); diff --git a/esp-link/config.h b/esp-link/config.h index c6a4058..45f3b4b 100644 --- a/esp-link/config.h +++ b/esp-link/config.h @@ -43,6 +43,7 @@ typedef struct { int8_t stop_bits; char mqtt_password[70]; // MQTT password, was 32-char mqtt_old_password char mqtt_username[70]; // MQTT username, was 32-char mqtt_old_username + uint8_t pin_invert; // invert serial pins } FlashConfig; extern FlashConfig flashConfig; diff --git a/esp-link/main.c b/esp-link/main.c index 31bcf6e..f32415d 100644 --- a/esp-link/main.c +++ b/esp-link/main.c @@ -173,7 +173,7 @@ user_init(void) { gpio_init(); gpio_output_set(0, 0, 0, (1<<15)); // some people tie it to GND, gotta ensure it's disabled // init UART - uart_init(CALC_UARTMODE(flashConfig.data_bits, flashConfig.parity, flashConfig.stop_bits), + uart_init(CALC_UARTMODE(flashConfig.data_bits, flashConfig.parity, flashConfig.stop_bits, flashConfig.pin_invert), flashConfig.baud_rate, 115200); logInit(); // must come after init of uart // Say hello (leave some time to cause break in TX after boot loader's msg diff --git a/html/home.html b/html/home.html index de9862f..8f7db36 100644 --- a/html/home.html +++ b/html/home.html @@ -96,6 +96,49 @@ TX on gpio1/TX0 and RX on gpio3/RX0, swapped is TX on gpio15 and RX on gpio13. +
+ + + +
+
+
+   + + + +
+
+   + + + +
+
+   + + + +
+
+   + + + +
+
+   + + + +
+
+   + + + +
+
@@ -156,6 +199,7 @@ onLoad(function() { getWifiInfo(); getSystemInfo(); bnd($("#pinform"), "submit", setPins); + setInvertBindings(); }); diff --git a/html/style.css b/html/style.css index 1bb11cd..978edca 100644 --- a/html/style.css +++ b/html/style.css @@ -141,6 +141,10 @@ fieldset fields { text-align: left; width: 6em; } +.pins-invert-group { + display: none; +} + .pure-form-aligned.form-narrow input[type=checkbox], .pure-form-aligned.form-narrow input[type=radio] { float: none; diff --git a/html/ui.js b/html/ui.js index 67361c9..8982b19 100644 --- a/html/ui.js +++ b/html/ui.js @@ -394,11 +394,23 @@ function showNotification(text) { var pinPresets = { // array: reset, isp, conn, ser, swap, rxpup - "esp-01": [ 0, -1, 2, -1, 0, 1 ], - "esp-12": [ 12, 14, 0, 2, 0, 1 ], - "esp-12 swap": [ 1, 3, 0, 2, 1, 1 ], - "esp-bridge": [ 12, 13, 0, 14, 0, 0 ], - "wifi-link-12": [ 1, 3, 0, 2, 1, 0 ], + "esp-01": [ 0, -1, 2, -1, 0, 1, 0 ], + "esp-01-inv": [ 0, -1, 2, -1, 0, 1, 63 ], + "esp-12": [ 12, 14, 0, 2, 0, 1, 0 ], + "esp-12 swap": [ 1, 3, 0, 2, 1, 1, 0 ], + "esp-12-inv": [ 12, 14, 0, 2, 0, 1, 63 ], + "esp-12 swap-inv": [ 1, 3, 0, 2, 1, 1, 63 ], + "esp-bridge": [ 12, 13, 0, 14, 0, 0, 0 ], + "wifi-link-12": [ 1, 3, 0, 2, 1, 0, 0 ], +}; + +var invertPins = { + "rxd" : 1, + "cts" : 2, + "dsr" : 4, + "txd": 8, + "rts": 16, + "dtr": 32 }; function createPresets(sel) { @@ -418,6 +430,16 @@ function createPresets(sel) { setPP("ser", pp[3]); setPP("swap", pp[4]); $("#pin-rxpup").checked = !!pp[5]; + $("#pin-invert").checked = !!pp[6]; + if ($("#pin-invert").checked) + { + $("#pins-invert-group").style.display = "block"; + } + else + { + $("#pins-invert-group").style.display = "none"; + } + pinsInvertApplyConfig(pp[6]); sel.value = 0; }; @@ -453,6 +475,12 @@ function displayPins(resp) { createSelectForPin("ser", resp["ser"]); $("#pin-swap").value = resp["swap"]; $("#pin-rxpup").checked = !!resp["rxpup"]; + $("#pin-invert").checked = !!resp["pinvert"]; + pinsInvertApplyConfig(resp["pinvert"]); + if ($("#pin-invert").checked) + { + $("#pins-invert-group").style.display = "block"; + } createPresets($("#pin-preset")); $("#pin-spinner").setAttribute("hidden", ""); @@ -465,8 +493,65 @@ function fetchPins() { }); } +function pinsInvertApplyConfig(v) +{ + for (var p in invertPins) + { + if (invertPins[p] & v) + { + $("#pin-" + p + "-invert").checked = true; + } + else + { + $("#pin-" + p + "-invert").checked = false; + } + } + +} + +function pinInvertChanged() +{ + var any = false; + for (var p in invertPins) + { + if ($("#pin-" + p + "-invert").checked) + { + any = true; + } + } + if (!any) + { + $("#pin-invert").checked = false; + pinsInvertChanged(); + } +} + +function pinsInvertChanged() +{ + if ($("#pin-invert").checked) + { + pinsInvertApplyConfig(63); + $("#pins-invert-group").style.display = "block"; + } + else + { + pinsInvertApplyConfig(0); + $("#pins-invert-group").style.display = "none"; + } +} + +function setInvertBindings() +{ + bnd($("#pin-invert"),"click", pinsInvertChanged); + for (var p in invertPins) + { + bnd($("#pin-" + p + "-invert"),"click", pinInvertChanged); + } +} + function setPins(ev) { ev.preventDefault(); + setEditToClick() var url = "/pins"; var sep = "?"; ["reset", "isp", "conn", "ser", "swap"].forEach(function(p) { @@ -474,6 +559,15 @@ function setPins(ev) { sep = "&"; }); url += "&rxpup=" + ($("#pin-rxpup").checked ? "1" : "0"); + var invert = 0; + for (var p in invertPins) + { + if ($("#pin-" + p + "-invert").checked) + { + invert += invertPins[p]; + } + } + url += "&pinvert=" + invert; // console.log("set pins: " + url); ajaxSpin("POST", url, function() { showNotification("Pin assignment changed"); diff --git a/include/uart_hw.h b/include/uart_hw.h index 0978011..0622e80 100644 --- a/include/uart_hw.h +++ b/include/uart_hw.h @@ -80,6 +80,15 @@ #define UART_RXFIFO_CNT_S 0 #define UART_CONF0( i ) (REG_UART_BASE( i ) + 0x20) +#define UART_DTR_INV (BIT(24)) +#define UART_RTS_INV (BIT(23)) +#define UART_TXD_INV (BIT(22)) +#define UART_DSR_INV (BIT(21)) +#define UART_CTS_INV (BIT(20)) +#define UART_RXD_INV (BIT(19)) +#define UART_INVERT_BIT_NUM 0x0000003F +#define UART_INVERT_BIT_NUM_S 19 +#define UART_NO_INVERT (0) #define UART_TXFIFO_RST (BIT(18)) #define UART_RXFIFO_RST (BIT(17)) #define UART_IRDA_EN (BIT(16)) @@ -133,10 +142,11 @@ #define UART1 1 //calc bit 0..5 for UART_CONF0 register -#define CALC_UARTMODE(data_bits,parity,stop_bits) \ +#define CALC_UARTMODE(data_bits,parity,stop_bits, invert_bits) \ (((parity == NONE_BITS) ? 0x0 : (UART_PARITY_EN | (parity & UART_PARITY))) | \ ((stop_bits & UART_STOP_BIT_NUM) << UART_STOP_BIT_NUM_S) | \ - ((data_bits & UART_BIT_NUM) << UART_BIT_NUM_S)) + ((data_bits & UART_BIT_NUM) << UART_BIT_NUM_S) | \ + ((invert_bits & UART_INVERT_BIT_NUM) << UART_INVERT_BIT_NUM_S)) typedef enum { FIVE_BITS = 0x0, @@ -162,6 +172,7 @@ typedef enum { STICK_PARITY_EN = BIT3 | BIT5 } UartExistParity; + typedef enum { BIT_RATE_300 = 300, BIT_RATE_600 = 600, diff --git a/serial/console.c b/serial/console.c index 473bcf6..34b0a8d 100644 --- a/serial/console.c +++ b/serial/console.c @@ -106,7 +106,7 @@ ajaxConsoleFormat(HttpdConnData *connData) { if (buff[1] == 'O') flashConfig.parity = ODD_BITS; if (buff[2] == '1') flashConfig.stop_bits = ONE_STOP_BIT; if (buff[2] == '2') flashConfig.stop_bits = TWO_STOP_BIT; - uart0_config(flashConfig.data_bits, flashConfig.parity, flashConfig.stop_bits); + uart0_config(flashConfig.data_bits, flashConfig.parity, flashConfig.stop_bits, UART_NO_INVERT); status = configSave() ? 200 : 400; } else if (connData->requestType == HTTPD_METHOD_GET) { status = 200; diff --git a/serial/serbridge.c b/serial/serbridge.c index 88de652..7d61bab 100644 --- a/serial/serbridge.c +++ b/serial/serbridge.c @@ -203,7 +203,7 @@ telnetUnwrap(serbridgeConnData *conn, uint8_t *inBuf, int len) case TN_setDataSize: if (c >= 5 && c <= 8) { flashConfig.data_bits = c - 5 + FIVE_BITS; - uart0_config(flashConfig.data_bits, flashConfig.parity, flashConfig.stop_bits); + uart0_config(flashConfig.data_bits, flashConfig.parity, flashConfig.stop_bits, flashConfig.pin_invert ); configSave(); os_printf("Telnet: %d bits/char\n", c); } else if (c == 0) { @@ -558,8 +558,8 @@ serbridgeInitPins() mcu_reset_pin = flashConfig.reset_pin; mcu_isp_pin = flashConfig.isp_pin; #ifdef SERBR_DBG - os_printf("Serbridge pins: reset=%d isp=%d swap=%d\n", - mcu_reset_pin, mcu_isp_pin, flashConfig.swap_uart); + os_printf("Serbridge pins: reset=%d isp=%d swap=%d invert=%d\n", + mcu_reset_pin, mcu_isp_pin, flashConfig.swap_uart, flashConfig.pin_invert); #endif if (flashConfig.swap_uart) { @@ -577,6 +577,12 @@ serbridgeInitPins() else PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0RXD_U); system_uart_de_swap(); } + //re-initialise invert pins + + uint32_t conf = READ_PERI_REG(UART_CONF0(0)); + conf &= ~(UART_INVERT_BIT_NUM << UART_INVERT_BIT_NUM_S); + conf |= (flashConfig.pin_invert & UART_INVERT_BIT_NUM) << UART_INVERT_BIT_NUM_S; + WRITE_PERI_REG(UART_CONF0(0), conf); // set both pins to 1 before turning them on so we don't cause a reset if (mcu_isp_pin >= 0) GPIO_OUTPUT_SET(mcu_isp_pin, 1); diff --git a/serial/uart.c b/serial/uart.c index cb44ab0..8c743b9 100644 --- a/serial/uart.c +++ b/serial/uart.c @@ -59,10 +59,12 @@ uart_config(uint8 uart_no, UartBautRate baudrate, uint32 conf0) //PIN_PULLUP_DIS (PERIPHS_IO_MUX_U0RXD_U); } + os_printf("uart config %d: %x\n", uart_no, conf0); + uart_div_modify(uart_no, UART_CLK_FREQ / baudrate); if (uart_no == UART1) //UART 1 always 8 N 1 - conf0 = CALC_UARTMODE(EIGHT_BITS, NONE_BITS, ONE_STOP_BIT); + conf0 = CALC_UARTMODE(EIGHT_BITS, NONE_BITS, ONE_STOP_BIT, UART_NO_INVERT); WRITE_PERI_REG(UART_CONF0(uart_no), conf0); //clear rx and tx fifo,not ready @@ -258,8 +260,8 @@ uart0_baud(int rate) { } void ICACHE_FLASH_ATTR -uart0_config(uint8_t data_bits, uint8_t parity, uint8_t stop_bits) { - uint32_t conf0 = CALC_UARTMODE(data_bits, parity, stop_bits); +uart0_config(uint8_t data_bits, uint8_t parity, uint8_t stop_bits, uint8_t invert) { + uint32_t conf0 = CALC_UARTMODE(data_bits, parity, stop_bits, invert); WRITE_PERI_REG(UART_CONF0(0), conf0); } diff --git a/serial/uart.h b/serial/uart.h index f3fc2de..9fd9d63 100644 --- a/serial/uart.h +++ b/serial/uart.h @@ -27,7 +27,7 @@ void uart_add_recv_cb(UartRecv_cb cb); uint16_t uart0_rx_poll(char *buff, uint16_t nchars, uint32_t timeout_us); void uart0_baud(int rate); -void uart0_config(uint8_t data_bits, uint8_t parity, uint8_t stop_bits); +void uart0_config(uint8_t data_bits, uint8_t parity, uint8_t stop_bits, uint8_t pin_invert); void uart_config(uint8 uart_no, UartBautRate baudrate, uint32 conf0); #endif /* __UART_H__ */