Skip to content

Commit

Permalink
Add support for inverting each input and output UART signal (jeelabs#501
Browse files Browse the repository at this point in the history
)
  • Loading branch information
xstanislav committed Jun 4, 2023
1 parent 15d8ac9 commit 3833685
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 21 deletions.
12 changes: 7 additions & 5 deletions esp-link/cgipins.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions esp-link/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion esp-link/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions html/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,49 @@ <h1>Pin assignment</h1>
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">
Expand Down Expand Up @@ -156,6 +199,7 @@ <h1>System details</h1>
getWifiInfo();
getSystemInfo();
bnd($("#pinform"), "submit", setPins);
setInvertBindings();
});
</script>
</body></html>
4 changes: 4 additions & 0 deletions html/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
104 changes: 99 additions & 5 deletions html/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
};

Expand Down Expand Up @@ -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", "");
Expand All @@ -465,15 +493,81 @@ 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) {
url += sep + p + "=" + $("#pin-"+p).value;
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");
Expand Down
15 changes: 13 additions & 2 deletions include/uart_hw.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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,
Expand All @@ -162,6 +172,7 @@ typedef enum {
STICK_PARITY_EN = BIT3 | BIT5
} UartExistParity;


typedef enum {
BIT_RATE_300 = 300,
BIT_RATE_600 = 600,
Expand Down
2 changes: 1 addition & 1 deletion serial/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 9 additions & 3 deletions serial/serbridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
8 changes: 5 additions & 3 deletions serial/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion serial/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__ */

0 comments on commit 3833685

Please sign in to comment.