Skip to content

Commit 3833685

Browse files
committed
Add support for inverting each input and output UART signal (jeelabs#501)
1 parent 15d8ac9 commit 3833685

File tree

11 files changed

+185
-21
lines changed

11 files changed

+185
-21
lines changed

esp-link/cgipins.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ int ICACHE_FLASH_ATTR cgiPinsGet(HttpdConnData *connData) {
3232
int len;
3333

3434
len = os_sprintf(buff,
35-
"{ \"reset\":%d, \"isp\":%d, \"conn\":%d, \"ser\":%d, \"swap\":%d, \"rxpup\":%d }",
35+
"{ \"reset\":%d, \"isp\":%d, \"conn\":%d, \"ser\":%d, \"swap\":%d, \"rxpup\":%d, \"pinvert\":%d }",
3636
flashConfig.reset_pin, flashConfig.isp_pin, flashConfig.conn_led_pin,
37-
flashConfig.ser_led_pin, !!flashConfig.swap_uart, !!flashConfig.rx_pullup);
37+
flashConfig.ser_led_pin, !!flashConfig.swap_uart, !!flashConfig.rx_pullup, flashConfig.pin_invert);
3838

3939
jsonHeader(connData, 200);
4040
httpdSend(connData, buff, len);
@@ -49,13 +49,14 @@ int ICACHE_FLASH_ATTR cgiPinsSet(HttpdConnData *connData) {
4949

5050
int8_t ok = 0;
5151
int8_t reset, isp, conn, ser;
52-
uint8_t swap, rxpup;
52+
uint8_t swap, rxpup, pinvert;
5353
ok |= getInt8Arg(connData, "reset", &reset);
5454
ok |= getInt8Arg(connData, "isp", &isp);
5555
ok |= getInt8Arg(connData, "conn", &conn);
5656
ok |= getInt8Arg(connData, "ser", &ser);
5757
ok |= getBoolArg(connData, "swap", &swap);
5858
ok |= getBoolArg(connData, "rxpup", &rxpup);
59+
ok |= getUInt8Arg(connData, "pinvert", &pinvert);
5960
if (ok < 0) return HTTPD_CGI_DONE;
6061

6162
char *coll;
@@ -90,8 +91,9 @@ int ICACHE_FLASH_ATTR cgiPinsSet(HttpdConnData *connData) {
9091
flashConfig.ser_led_pin = ser;
9192
flashConfig.swap_uart = swap;
9293
flashConfig.rx_pullup = rxpup;
93-
os_printf("Pins changed: reset=%d isp=%d conn=%d ser=%d swap=%d rx-pup=%d\n",
94-
reset, isp, conn, ser, swap, rxpup);
94+
flashConfig.pin_invert = pinvert;
95+
os_printf("Pins changed: reset=%d isp=%d conn=%d ser=%d swap=%d rx-pup=%d pinvert=%X\n",
96+
reset, isp, conn, ser, swap, rxpup, pinvert);
9597

9698
// apply the changes
9799
serbridgeInitPins();

esp-link/config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ typedef struct {
4343
int8_t stop_bits;
4444
char mqtt_password[70]; // MQTT password, was 32-char mqtt_old_password
4545
char mqtt_username[70]; // MQTT username, was 32-char mqtt_old_username
46+
uint8_t pin_invert; // invert serial pins
4647
} FlashConfig;
4748
extern FlashConfig flashConfig;
4849

esp-link/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ user_init(void) {
173173
gpio_init();
174174
gpio_output_set(0, 0, 0, (1<<15)); // some people tie it to GND, gotta ensure it's disabled
175175
// init UART
176-
uart_init(CALC_UARTMODE(flashConfig.data_bits, flashConfig.parity, flashConfig.stop_bits),
176+
uart_init(CALC_UARTMODE(flashConfig.data_bits, flashConfig.parity, flashConfig.stop_bits, flashConfig.pin_invert),
177177
flashConfig.baud_rate, 115200);
178178
logInit(); // must come after init of uart
179179
// Say hello (leave some time to cause break in TX after boot loader's msg

html/home.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,49 @@ <h1>Pin assignment</h1>
9696
TX on gpio1/TX0 and RX on gpio3/RX0, swapped is TX on gpio15 and RX on gpio13.
9797
</div>
9898
</div>
99+
<div class="pure-control-group">
100+
<label for="pin-invert" class="pure-checkbox">pin invert</label>
101+
<input id="pin-invert" type="checkbox">
102+
<div class="popup">Expect inverted serial in signals, and/or invert serial out signals on serial port</div>
103+
</div>
104+
<div id="pins-invert-group" class ="pins-invert-group">
105+
<div class="pin-invert-group pure-control-group">
106+
&NonBreakingSpace;
107+
<label for="pin-rxd-invert" class="pure-checkbox">RXD</label>
108+
<input id="pin-rxd-invert" type="checkbox">
109+
<div class="popup">invert RXD line</div>
110+
</div>
111+
<div class="pin-invert-group pure-control-group">
112+
&NonBreakingSpace;
113+
<label for="pin-txd-invert" class="pure-checkbox">TXD</label>
114+
<input id="pin-txd-invert" type="checkbox">
115+
<div class="popup">invert TXD line</div>
116+
</div>
117+
<div class="pin-invert-group pure-control-group">
118+
&NonBreakingSpace;
119+
<label for="pin-cts-invert" class="pure-checkbox">CTS</label>
120+
<input id="pin-cts-invert" type="checkbox">
121+
<div class="popup">invert CTS line</div>
122+
</div>
123+
<div class="pin-invert-group pure-control-group">
124+
&NonBreakingSpace;
125+
<label for="pin-rts-invert" class="pure-checkbox">RTS</label>
126+
<input id="pin-rts-invert" type="checkbox">
127+
<div class="popup">invert RTS line</div>
128+
</div>
129+
<div class="pin-invert-group pure-control-group">
130+
&NonBreakingSpace;
131+
<label for="pin-dtr-invert" class="pure-checkbox">DTR</label>
132+
<input id="pin-dtr-invert" type="checkbox">
133+
<div class="popup">invert DTR line</div>
134+
</div>
135+
<div class="pin-invert-group pure-control-group">
136+
&NonBreakingSpace;
137+
<label for="pin-dsr-invert" class="pure-checkbox">DSR</label>
138+
<input id="pin-dsr-invert" type="checkbox">
139+
<div class="popup">invert DSR line</div>
140+
</div>
141+
</div>
99142
<div class="pure-control-group">
100143
<label for="pin-rxpup" class="pure-checkbox">RX pull-up</label>
101144
<input id="pin-rxpup" type="checkbox">
@@ -156,6 +199,7 @@ <h1>System details</h1>
156199
getWifiInfo();
157200
getSystemInfo();
158201
bnd($("#pinform"), "submit", setPins);
202+
setInvertBindings();
159203
});
160204
</script>
161205
</body></html>

html/style.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ fieldset fields {
141141
text-align: left;
142142
width: 6em;
143143
}
144+
.pins-invert-group {
145+
display: none;
146+
}
147+
144148
.pure-form-aligned.form-narrow input[type=checkbox],
145149
.pure-form-aligned.form-narrow input[type=radio] {
146150
float: none;

html/ui.js

Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,11 +394,23 @@ function showNotification(text) {
394394

395395
var pinPresets = {
396396
// array: reset, isp, conn, ser, swap, rxpup
397-
"esp-01": [ 0, -1, 2, -1, 0, 1 ],
398-
"esp-12": [ 12, 14, 0, 2, 0, 1 ],
399-
"esp-12 swap": [ 1, 3, 0, 2, 1, 1 ],
400-
"esp-bridge": [ 12, 13, 0, 14, 0, 0 ],
401-
"wifi-link-12": [ 1, 3, 0, 2, 1, 0 ],
397+
"esp-01": [ 0, -1, 2, -1, 0, 1, 0 ],
398+
"esp-01-inv": [ 0, -1, 2, -1, 0, 1, 63 ],
399+
"esp-12": [ 12, 14, 0, 2, 0, 1, 0 ],
400+
"esp-12 swap": [ 1, 3, 0, 2, 1, 1, 0 ],
401+
"esp-12-inv": [ 12, 14, 0, 2, 0, 1, 63 ],
402+
"esp-12 swap-inv": [ 1, 3, 0, 2, 1, 1, 63 ],
403+
"esp-bridge": [ 12, 13, 0, 14, 0, 0, 0 ],
404+
"wifi-link-12": [ 1, 3, 0, 2, 1, 0, 0 ],
405+
};
406+
407+
var invertPins = {
408+
"rxd" : 1,
409+
"cts" : 2,
410+
"dsr" : 4,
411+
"txd": 8,
412+
"rts": 16,
413+
"dtr": 32
402414
};
403415

404416
function createPresets(sel) {
@@ -418,6 +430,16 @@ function createPresets(sel) {
418430
setPP("ser", pp[3]);
419431
setPP("swap", pp[4]);
420432
$("#pin-rxpup").checked = !!pp[5];
433+
$("#pin-invert").checked = !!pp[6];
434+
if ($("#pin-invert").checked)
435+
{
436+
$("#pins-invert-group").style.display = "block";
437+
}
438+
else
439+
{
440+
$("#pins-invert-group").style.display = "none";
441+
}
442+
pinsInvertApplyConfig(pp[6]);
421443
sel.value = 0;
422444
};
423445

@@ -453,6 +475,12 @@ function displayPins(resp) {
453475
createSelectForPin("ser", resp["ser"]);
454476
$("#pin-swap").value = resp["swap"];
455477
$("#pin-rxpup").checked = !!resp["rxpup"];
478+
$("#pin-invert").checked = !!resp["pinvert"];
479+
pinsInvertApplyConfig(resp["pinvert"]);
480+
if ($("#pin-invert").checked)
481+
{
482+
$("#pins-invert-group").style.display = "block";
483+
}
456484
createPresets($("#pin-preset"));
457485

458486
$("#pin-spinner").setAttribute("hidden", "");
@@ -465,15 +493,81 @@ function fetchPins() {
465493
});
466494
}
467495

496+
function pinsInvertApplyConfig(v)
497+
{
498+
for (var p in invertPins)
499+
{
500+
if (invertPins[p] & v)
501+
{
502+
$("#pin-" + p + "-invert").checked = true;
503+
}
504+
else
505+
{
506+
$("#pin-" + p + "-invert").checked = false;
507+
}
508+
}
509+
510+
}
511+
512+
function pinInvertChanged()
513+
{
514+
var any = false;
515+
for (var p in invertPins)
516+
{
517+
if ($("#pin-" + p + "-invert").checked)
518+
{
519+
any = true;
520+
}
521+
}
522+
if (!any)
523+
{
524+
$("#pin-invert").checked = false;
525+
pinsInvertChanged();
526+
}
527+
}
528+
529+
function pinsInvertChanged()
530+
{
531+
if ($("#pin-invert").checked)
532+
{
533+
pinsInvertApplyConfig(63);
534+
$("#pins-invert-group").style.display = "block";
535+
}
536+
else
537+
{
538+
pinsInvertApplyConfig(0);
539+
$("#pins-invert-group").style.display = "none";
540+
}
541+
}
542+
543+
function setInvertBindings()
544+
{
545+
bnd($("#pin-invert"),"click", pinsInvertChanged);
546+
for (var p in invertPins)
547+
{
548+
bnd($("#pin-" + p + "-invert"),"click", pinInvertChanged);
549+
}
550+
}
551+
468552
function setPins(ev) {
469553
ev.preventDefault();
554+
setEditToClick()
470555
var url = "/pins";
471556
var sep = "?";
472557
["reset", "isp", "conn", "ser", "swap"].forEach(function(p) {
473558
url += sep + p + "=" + $("#pin-"+p).value;
474559
sep = "&";
475560
});
476561
url += "&rxpup=" + ($("#pin-rxpup").checked ? "1" : "0");
562+
var invert = 0;
563+
for (var p in invertPins)
564+
{
565+
if ($("#pin-" + p + "-invert").checked)
566+
{
567+
invert += invertPins[p];
568+
}
569+
}
570+
url += "&pinvert=" + invert;
477571
// console.log("set pins: " + url);
478572
ajaxSpin("POST", url, function() {
479573
showNotification("Pin assignment changed");

include/uart_hw.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@
8080
#define UART_RXFIFO_CNT_S 0
8181

8282
#define UART_CONF0( i ) (REG_UART_BASE( i ) + 0x20)
83+
#define UART_DTR_INV (BIT(24))
84+
#define UART_RTS_INV (BIT(23))
85+
#define UART_TXD_INV (BIT(22))
86+
#define UART_DSR_INV (BIT(21))
87+
#define UART_CTS_INV (BIT(20))
88+
#define UART_RXD_INV (BIT(19))
89+
#define UART_INVERT_BIT_NUM 0x0000003F
90+
#define UART_INVERT_BIT_NUM_S 19
91+
#define UART_NO_INVERT (0)
8392
#define UART_TXFIFO_RST (BIT(18))
8493
#define UART_RXFIFO_RST (BIT(17))
8594
#define UART_IRDA_EN (BIT(16))
@@ -133,10 +142,11 @@
133142
#define UART1 1
134143

135144
//calc bit 0..5 for UART_CONF0 register
136-
#define CALC_UARTMODE(data_bits,parity,stop_bits) \
145+
#define CALC_UARTMODE(data_bits,parity,stop_bits, invert_bits) \
137146
(((parity == NONE_BITS) ? 0x0 : (UART_PARITY_EN | (parity & UART_PARITY))) | \
138147
((stop_bits & UART_STOP_BIT_NUM) << UART_STOP_BIT_NUM_S) | \
139-
((data_bits & UART_BIT_NUM) << UART_BIT_NUM_S))
148+
((data_bits & UART_BIT_NUM) << UART_BIT_NUM_S) | \
149+
((invert_bits & UART_INVERT_BIT_NUM) << UART_INVERT_BIT_NUM_S))
140150

141151
typedef enum {
142152
FIVE_BITS = 0x0,
@@ -162,6 +172,7 @@ typedef enum {
162172
STICK_PARITY_EN = BIT3 | BIT5
163173
} UartExistParity;
164174

175+
165176
typedef enum {
166177
BIT_RATE_300 = 300,
167178
BIT_RATE_600 = 600,

serial/console.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ ajaxConsoleFormat(HttpdConnData *connData) {
106106
if (buff[1] == 'O') flashConfig.parity = ODD_BITS;
107107
if (buff[2] == '1') flashConfig.stop_bits = ONE_STOP_BIT;
108108
if (buff[2] == '2') flashConfig.stop_bits = TWO_STOP_BIT;
109-
uart0_config(flashConfig.data_bits, flashConfig.parity, flashConfig.stop_bits);
109+
uart0_config(flashConfig.data_bits, flashConfig.parity, flashConfig.stop_bits, UART_NO_INVERT);
110110
status = configSave() ? 200 : 400;
111111
} else if (connData->requestType == HTTPD_METHOD_GET) {
112112
status = 200;

serial/serbridge.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ telnetUnwrap(serbridgeConnData *conn, uint8_t *inBuf, int len)
203203
case TN_setDataSize:
204204
if (c >= 5 && c <= 8) {
205205
flashConfig.data_bits = c - 5 + FIVE_BITS;
206-
uart0_config(flashConfig.data_bits, flashConfig.parity, flashConfig.stop_bits);
206+
uart0_config(flashConfig.data_bits, flashConfig.parity, flashConfig.stop_bits, flashConfig.pin_invert);
207207
configSave();
208208
os_printf("Telnet: %d bits/char\n", c);
209209
} else if (c == 0) {
@@ -558,8 +558,8 @@ serbridgeInitPins()
558558
mcu_reset_pin = flashConfig.reset_pin;
559559
mcu_isp_pin = flashConfig.isp_pin;
560560
#ifdef SERBR_DBG
561-
os_printf("Serbridge pins: reset=%d isp=%d swap=%d\n",
562-
mcu_reset_pin, mcu_isp_pin, flashConfig.swap_uart);
561+
os_printf("Serbridge pins: reset=%d isp=%d swap=%d invert=%d\n",
562+
mcu_reset_pin, mcu_isp_pin, flashConfig.swap_uart, flashConfig.pin_invert);
563563
#endif
564564

565565
if (flashConfig.swap_uart) {
@@ -577,6 +577,12 @@ serbridgeInitPins()
577577
else PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0RXD_U);
578578
system_uart_de_swap();
579579
}
580+
//re-initialise invert pins
581+
582+
uint32_t conf = READ_PERI_REG(UART_CONF0(0));
583+
conf &= ~(UART_INVERT_BIT_NUM << UART_INVERT_BIT_NUM_S);
584+
conf |= (flashConfig.pin_invert & UART_INVERT_BIT_NUM) << UART_INVERT_BIT_NUM_S;
585+
WRITE_PERI_REG(UART_CONF0(0), conf);
580586

581587
// set both pins to 1 before turning them on so we don't cause a reset
582588
if (mcu_isp_pin >= 0) GPIO_OUTPUT_SET(mcu_isp_pin, 1);

serial/uart.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ uart_config(uint8 uart_no, UartBautRate baudrate, uint32 conf0)
5959
//PIN_PULLUP_DIS (PERIPHS_IO_MUX_U0RXD_U);
6060
}
6161

62+
os_printf("uart config %d: %x\n", uart_no, conf0);
63+
6264
uart_div_modify(uart_no, UART_CLK_FREQ / baudrate);
6365

6466
if (uart_no == UART1) //UART 1 always 8 N 1
65-
conf0 = CALC_UARTMODE(EIGHT_BITS, NONE_BITS, ONE_STOP_BIT);
67+
conf0 = CALC_UARTMODE(EIGHT_BITS, NONE_BITS, ONE_STOP_BIT, UART_NO_INVERT);
6668
WRITE_PERI_REG(UART_CONF0(uart_no), conf0);
6769

6870
//clear rx and tx fifo,not ready
@@ -258,8 +260,8 @@ uart0_baud(int rate) {
258260
}
259261

260262
void ICACHE_FLASH_ATTR
261-
uart0_config(uint8_t data_bits, uint8_t parity, uint8_t stop_bits) {
262-
uint32_t conf0 = CALC_UARTMODE(data_bits, parity, stop_bits);
263+
uart0_config(uint8_t data_bits, uint8_t parity, uint8_t stop_bits, uint8_t invert) {
264+
uint32_t conf0 = CALC_UARTMODE(data_bits, parity, stop_bits, invert);
263265
WRITE_PERI_REG(UART_CONF0(0), conf0);
264266
}
265267

0 commit comments

Comments
 (0)