From 84157b221b13f5c55f979519611b056097fd337f Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 26 Jun 2016 09:40:14 +0800 Subject: [PATCH] Adding the console uart format support Some of the MCU use format other than 8N1. Adding the support for other format. --- esp-link/config.c | 5 ++++- esp-link/config.h | 3 +++ esp-link/main.c | 1 + html/console.html | 26 +++++++++++++++++++++++++- serial/console.c | 32 ++++++++++++++++++++++++++++++++ serial/console.h | 1 + serial/uart.c | 2 +- serial/uart.h | 1 + 8 files changed, 68 insertions(+), 3 deletions(-) diff --git a/esp-link/config.c b/esp-link/config.c index 1af160a..dfbfbc6 100644 --- a/esp-link/config.c +++ b/esp-link/config.c @@ -31,7 +31,10 @@ FlashConfig flashDefault = { .rx_pullup = 1, .sntp_server = "us.pool.ntp.org\0", .syslog_host = "\0", .syslog_minheap = 8192, .syslog_filter = 7, .syslog_showtick = 1, .syslog_showdate = 0, - .mdns_enable = 1, .mdns_servername = "http\0", .timezone_offset = 0 + .mdns_enable = 1, .mdns_servername = "http\0", .timezone_offset = 0, + .data_bits = EIGHT_BITS, + .parity = NONE_BITS, + .stop_bits = ONE_STOP_BIT, }; typedef union { diff --git a/esp-link/config.h b/esp-link/config.h index 341d7e1..37c82e4 100644 --- a/esp-link/config.h +++ b/esp-link/config.h @@ -38,6 +38,9 @@ typedef struct { char mdns_servername[32]; int8_t timezone_offset; char mqtt_host[64]; // MQTT host we connect to, was 32-char mqtt_old_host + int8_t data_bits; + int8_t parity; + int8_t stop_bits; } FlashConfig; extern FlashConfig flashConfig; diff --git a/esp-link/main.c b/esp-link/main.c index 49a228a..d23e7ea 100644 --- a/esp-link/main.c +++ b/esp-link/main.c @@ -74,6 +74,7 @@ HttpdBuiltInUrl builtInUrls[] = { { "/log/reset", cgiReset, NULL }, { "/console/reset", ajaxConsoleReset, NULL }, { "/console/baud", ajaxConsoleBaud, NULL }, + { "/console/fmt", ajaxConsoleFormat, NULL }, { "/console/text", ajaxConsole, NULL }, { "/console/send", ajaxConsoleSend, NULL }, //Enable the line below to protect the WiFi configuration with an username/password combo. diff --git a/html/console.html b/html/console.html index d84da72..fdda148 100644 --- a/html/console.html +++ b/html/console.html @@ -18,7 +18,17 @@ -   Fmt: 8N1 +   Fmt: +

Console
@@ -93,6 +103,20 @@ ); }); + ajaxJson('GET', "/console/fmt", + function(data) { $("#fmt-sel").value = data.fmt; }, + function(s, st) { showNotification(st); } + ); + + bnd($("#fmt-sel"), "change", function(ev) { + ev.preventDefault(); + var fmt = $("#fmt-sel").value; + ajaxSpin('POST', "/console/fmt?fmt="+fmt, + function(resp) { showNotification("" + fmt + " format set"); }, + function(s, st) { showWarning("Error setting format: " + st); } + ); + }); + consoleSendInit(); addClass($('html')[0], "height100"); diff --git a/serial/console.c b/serial/console.c index 44e860a..597d80d 100644 --- a/serial/console.c +++ b/serial/console.c @@ -80,6 +80,38 @@ ajaxConsoleBaud(HttpdConnData *connData) { httpdSend(connData, buff, -1); return HTTPD_CGI_DONE; } +int ICACHE_FLASH_ATTR +ajaxConsoleFormat(HttpdConnData *connData) { + if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. + char buff[16]; + int len, status = 400; + extern UartDevice UartDev; + + len = httpdFindArg(connData->getArgs, "fmt", buff, sizeof(buff)); + if (len >= 3) { + int c = buff[0]; + if (c >= '5' && c <= '8') + flashConfig.data_bits = UartDev.data_bits = c - '5' + FIVE_BITS; + if (buff[1] == 'N' || buff[1] == 'E') + flashConfig.parity = UartDev.parity = buff[1] == 'E' ? EVEN_BITS : NONE_BITS; + if (buff[2] == '1' || buff[2] == '2') + flashConfig.stop_bits = UartDev.stop_bits = buff[2] == '2' ? TWO_STOP_BIT : ONE_STOP_BIT; + UartDev.baut_rate = flashConfig.baud_rate; + uart_config(0); + status = configSave() ? 200 : 400; + } else if (connData->requestType == HTTPD_METHOD_GET) { + status = 200; + } + + jsonHeader(connData, status); + os_sprintf(buff, "{\"fmt\": \"%c%c%c\"}", + flashConfig.data_bits + '5', + flashConfig.parity ? 'E' : 'N', + flashConfig.stop_bits ? '2': '1'); + httpdSend(connData, buff, -1); + return HTTPD_CGI_DONE; +} + int ICACHE_FLASH_ATTR ajaxConsoleSend(HttpdConnData *connData) { diff --git a/serial/console.h b/serial/console.h index 6225c0a..3020903 100644 --- a/serial/console.h +++ b/serial/console.h @@ -8,6 +8,7 @@ void ICACHE_FLASH_ATTR console_write_char(char c); int ajaxConsole(HttpdConnData *connData); int ajaxConsoleReset(HttpdConnData *connData); int ajaxConsoleBaud(HttpdConnData *connData); +int ajaxConsoleFormat(HttpdConnData *connData); int ajaxConsoleSend(HttpdConnData *connData); int tplConsole(HttpdConnData *connData, char *token, void **arg); diff --git a/serial/uart.c b/serial/uart.c index f7f07fa..cb44b22 100644 --- a/serial/uart.c +++ b/serial/uart.c @@ -44,7 +44,7 @@ static void uart0_rx_intr_handler(void *para); * Parameters : uart_no, use UART0 or UART1 defined ahead * Returns : NONE *******************************************************************************/ -static void ICACHE_FLASH_ATTR +void ICACHE_FLASH_ATTR uart_config(uint8 uart_no) { if (uart_no == UART1) { diff --git a/serial/uart.h b/serial/uart.h index 24fb2df..dc5b5ff 100644 --- a/serial/uart.h +++ b/serial/uart.h @@ -27,5 +27,6 @@ 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 uart_config(uint8 uart_no); #endif /* __UART_H__ */