Add support for inverting each input and output UART signal

pull/501/head
Jaap de Wolff 4 years ago
parent cf329e8b84
commit ed634e8e71
  1. 12
      esp-link/cgipins.c
  2. 1
      esp-link/config.h
  3. 2
      esp-link/main.c
  4. 44
      html/home.html
  5. 4
      html/style.css
  6. 104
      html/ui.js
  7. 15
      include/uart_hw.h
  8. 2
      serial/console.c
  9. 12
      serial/serbridge.c
  10. 8
      serial/uart.c
  11. 2
      serial/uart.h

@ -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();

@ -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;

@ -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

@ -96,6 +96,49 @@
TX on gpio1/TX0 and RX on gpio3/RX0, swapped is TX on gpio15 and RX on gpio13.
</div>
</div>
<div class="pure-control-group">
<label for="pin-invert" class="pure-checkbox">pin invert</label>
<input id="pin-invert" type="checkbox">
<div class="popup">Expect inverted serial in signals, and/or invert serial out signals on serial port</div>
</div>
<div id="pins-invert-group" class ="pins-invert-group">
<div class="pin-invert-group pure-control-group">
&NonBreakingSpace;
<label for="pin-rxd-invert" class="pure-checkbox">RXD</label>
<input id="pin-rxd-invert" type="checkbox">
<div class="popup">invert RXD line</div>
</div>
<div class="pin-invert-group pure-control-group">
&NonBreakingSpace;
<label for="pin-txd-invert" class="pure-checkbox">TXD</label>
<input id="pin-txd-invert" type="checkbox">
<div class="popup">invert TXD line</div>
</div>
<div class="pin-invert-group pure-control-group">
&NonBreakingSpace;
<label for="pin-cts-invert" class="pure-checkbox">CTS</label>
<input id="pin-cts-invert" type="checkbox">
<div class="popup">invert CTS line</div>
</div>
<div class="pin-invert-group pure-control-group">
&NonBreakingSpace;
<label for="pin-rts-invert" class="pure-checkbox">RTS</label>
<input id="pin-rts-invert" type="checkbox">
<div class="popup">invert RTS line</div>
</div>
<div class="pin-invert-group pure-control-group">
&NonBreakingSpace;
<label for="pin-dtr-invert" class="pure-checkbox">DTR</label>
<input id="pin-dtr-invert" type="checkbox">
<div class="popup">invert DTR line</div>
</div>
<div class="pin-invert-group pure-control-group">
&NonBreakingSpace;
<label for="pin-dsr-invert" class="pure-checkbox">DSR</label>
<input id="pin-dsr-invert" type="checkbox">
<div class="popup">invert DSR line</div>
</div>
</div>
<div class="pure-control-group">
<label for="pin-rxpup" class="pure-checkbox">RX pull-up</label>
<input id="pin-rxpup" type="checkbox">
@ -156,6 +199,7 @@ onLoad(function() {
getWifiInfo();
getSystemInfo();
bnd($("#pinform"), "submit", setPins);
setInvertBindings();
});
</script>
</body></html>

@ -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;

@ -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");

@ -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,

@ -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;

@ -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);

@ -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);
}

@ -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__ */

Loading…
Cancel
Save