From c5513d063d070171e6ff7a408e1cc7a812a5219a Mon Sep 17 00:00:00 2001 From: bitdump Date: Thu, 22 Sep 2016 19:33:33 -0700 Subject: [PATCH] Adding the console uart format support (#189) Some of the MCU use format other than 8N1. Adding the support for other format. --- esp-link/config.c | 11 ++++++++++- esp-link/config.h | 3 +++ esp-link/main.c | 4 +++- html/console.html | 26 +++++++++++++++++++++++++- serial/console.c | 32 ++++++++++++++++++++++++++++++++ serial/console.h | 1 + serial/uart.c | 28 ++++++++-------------------- serial/uart.h | 3 ++- 8 files changed, 84 insertions(+), 24 deletions(-) diff --git a/esp-link/config.c b/esp-link/config.c index 1af160a..b670c08 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 { @@ -152,6 +155,12 @@ bool ICACHE_FLASH_ATTR configRestore(void) { os_memset(flashConfig.mqtt_old_host, 0, 32); } else os_printf("mqtt_host is '%s'\n", flashConfig.mqtt_host); + if (flashConfig.data_bits == 0) { + // restore to default 8N1 + flashConfig.data_bits = flashDefault.data_bits; + flashConfig.parity = flashDefault.parity; + flashConfig.stop_bits = flashDefault.stop_bits; + } return true; } 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..fe7f0ec 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. @@ -135,7 +136,8 @@ void 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(flashConfig.baud_rate, 115200); + uart_init(CALC_UARTMODE(flashConfig.data_bits, flashConfig.parity, flashConfig.stop_bits), + 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 os_delay_us(10000L); diff --git a/html/console.html b/html/console.html index d84da72..b2e6597 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..330c870 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; + uint32 conf0; + + len = httpdFindArg(connData->getArgs, "fmt", buff, sizeof(buff)); + if (len >= 3) { + int c = buff[0]; + if (c >= '5' && c <= '8') + flashConfig.data_bits = c - '5' + FIVE_BITS; + if (buff[1] == 'N' || buff[1] == 'E') + flashConfig.parity = buff[1] == 'E' ? EVEN_BITS : NONE_BITS; + if (buff[2] == '1' || buff[2] == '2') + flashConfig.stop_bits = buff[2] == '2' ? TWO_STOP_BIT : ONE_STOP_BIT; + conf0 = CALC_UARTMODE(flashConfig.data_bits, flashConfig.parity, flashConfig.stop_bits); + uart_config(0, flashConfig.baud_rate, conf0); + 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..92233a0 100644 --- a/serial/uart.c +++ b/serial/uart.c @@ -44,8 +44,8 @@ static void uart0_rx_intr_handler(void *para); * Parameters : uart_no, use UART0 or UART1 defined ahead * Returns : NONE *******************************************************************************/ -static void ICACHE_FLASH_ATTR -uart_config(uint8 uart_no) +void ICACHE_FLASH_ATTR +uart_config(uint8 uart_no, UartBautRate baudrate, uint32 conf0) { if (uart_no == UART1) { PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK); @@ -59,14 +59,11 @@ uart_config(uint8 uart_no) //PIN_PULLUP_DIS (PERIPHS_IO_MUX_U0RXD_U); } - uart_div_modify(uart_no, UART_CLK_FREQ / UartDev.baut_rate); + uart_div_modify(uart_no, UART_CLK_FREQ / baudrate); if (uart_no == UART1) //UART 1 always 8 N 1 - WRITE_PERI_REG(UART_CONF0(uart_no), - CALC_UARTMODE(EIGHT_BITS, NONE_BITS, ONE_STOP_BIT)); - else - WRITE_PERI_REG(UART_CONF0(uart_no), - CALC_UARTMODE(UartDev.data_bits, UartDev.parity, UartDev.stop_bits)); + conf0 = CALC_UARTMODE(EIGHT_BITS, NONE_BITS, ONE_STOP_BIT); + WRITE_PERI_REG(UART_CONF0(uart_no), conf0); //clear rx and tx fifo,not ready SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST); @@ -267,13 +264,11 @@ uart0_baud(int rate) { * Returns : NONE *******************************************************************************/ void ICACHE_FLASH_ATTR -uart_init(UartBautRate uart0_br, UartBautRate uart1_br) +uart_init(uint32 conf0, UartBautRate uart0_br, UartBautRate uart1_br) { // rom use 74880 baut_rate, here reinitialize - UartDev.baut_rate = uart0_br; - uart_config(UART0); - UartDev.baut_rate = uart1_br; - uart_config(UART1); + uart_config(UART0, uart0_br, conf0); + uart_config(UART1, uart1_br, conf0); for (int i=0; i<4; i++) uart_tx_one_char(UART1, '\n'); for (int i=0; i<4; i++) uart_tx_one_char(UART0, '\n'); ETS_UART_INTR_ENABLE(); @@ -295,10 +290,3 @@ uart_add_recv_cb(UartRecv_cb cb) { os_printf("UART: max cb count exceeded\n"); } -void ICACHE_FLASH_ATTR -uart_reattach() -{ - uart_init(BIT_RATE_74880, BIT_RATE_74880); -// ETS_UART_INTR_ATTACH(uart_rx_intr_handler_ssc, &(UartDev.rcv_buff)); -// ETS_UART_INTR_ENABLE(); -} diff --git a/serial/uart.h b/serial/uart.h index 24fb2df..d1469c6 100644 --- a/serial/uart.h +++ b/serial/uart.h @@ -8,7 +8,7 @@ typedef void (*UartRecv_cb)(char *buf, short len); // Initialize UARTs to the provided baud rates (115200 recommended). This also makes the os_printf // calls use uart1 for output (for debugging purposes) -void uart_init(UartBautRate uart0_br, UartBautRate uart1_br); +void uart_init(uint32 conf0, UartBautRate uart0_br, UartBautRate uart1_br); // Transmit a buffer of characters on UART0 void uart0_tx_buffer(char *buf, uint16 len); @@ -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, UartBautRate baudrate, uint32 conf0); #endif /* __UART_H__ */