From 65977bfbae359b129880810c07cfabd5a4335c74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Tue, 26 Sep 2017 17:41:24 +0200 Subject: [PATCH] Overclock, fix buffer overflow causing total lock-up, small tweaks removed the main make config file from VCS so it can be easier edited added overclocking option to enable 160 MHz mode --- .gitignore | 1 + front-end | 2 +- user/cgi_network.c | 2 ++ user/cgi_system.c | 16 ++++++++++++---- user/cgi_term_cfg.c | 9 +++++++++ user/syscfg.c | 13 +++++++++---- user/syscfg.h | 3 ++- user/uart_buffer.c | 34 ++++++++++++++++++++++------------ user/uart_handler.c | 2 +- 9 files changed, 59 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 675c98f..c013837 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ html_compressed/ ldscript_memspecific.ld eagle.app.sym node_modules +esphttpdconfig.mk # Garbage added by CLion .idea/ diff --git a/front-end b/front-end index c846cff..f5dd70a 160000 --- a/front-end +++ b/front-end @@ -1 +1 @@ -Subproject commit c846cffedbd927433354f4fc2004154cd596916f +Subproject commit f5dd70a6f32ac36f0820badc170832f27242a09d diff --git a/user/cgi_network.c b/user/cgi_network.c index 4cf4cc1..db86874 100644 --- a/user/cgi_network.c +++ b/user/cgi_network.c @@ -181,6 +181,8 @@ httpd_cgi_state ICACHE_FLASH_ATTR cgiNetworkSetParams(HttpdConnData *connData) } } + (void) redir_url; + if (redir_url_buf[strlen(SET_REDIR_ERR)] == 0) { // All was OK cgi_info("Set network params - success, applying in 1000 ms"); diff --git a/user/cgi_system.c b/user/cgi_system.c index 3e26c10..8418c5d 100755 --- a/user/cgi_system.c +++ b/user/cgi_system.c @@ -6,7 +6,6 @@ #include "cgi_system.h" #include "persist.h" #include "syscfg.h" -#include "uart_driver.h" #include "ansi_parser.h" #include "cgi_logging.h" @@ -98,9 +97,7 @@ cgiSystemCfgSetParams(HttpdConnData *connData) do { if (!GET_ARG("pw")) { - warn("Missing admin pw!"); - redir_url += sprintf(redir_url, "pw,"); - break; + break; // if no PW in GET, not trying to configure anything protected } if (!streq(buff, persist.admin.pw)) { @@ -191,6 +188,14 @@ cgiSystemCfgSetParams(HttpdConnData *connData) } } while (0); + if (GET_ARG("overclock")) { + cgi_dbg("overclock = %s", buff); + int enable = atoi(buff); + if (sysconf->overclock != enable) { + sysconf->overclock = (bool)enable; + } + } + (void)redir_url; if (redir_url_buf[strlen(SET_REDIR_ERR)] == 0) { @@ -246,6 +251,9 @@ tplSystemCfg(HttpdConnData *connData, char *token, void **arg) else if (streq(token, "def_admin_pw")) { sprintf(buff, "%s", DEFAULT_ADMIN_PW); } + else if (streq(token, "overclock")) { + sprintf(buff, "%d", sysconf->overclock); + } tplSend(connData, buff, -1); return HTTPD_CGI_DONE; diff --git a/user/cgi_term_cfg.c b/user/cgi_term_cfg.c index 56d06e6..775db58 100644 --- a/user/cgi_term_cfg.c +++ b/user/cgi_term_cfg.c @@ -10,6 +10,7 @@ Cgi/template routines for configuring non-wifi settings #include "helpers.h" #include "cgi_logging.h" #include "uart_driver.h" +#include "serial.h" #define SET_REDIR_SUC "/cfg/term" #define SET_REDIR_ERR SET_REDIR_SUC"?err=" @@ -42,6 +43,7 @@ cgiTermCfgSetParams(HttpdConnData *connData) bool notify_screen_content = 0, notify_screen_labels = 0; bool shall_clear_screen = false; + bool shall_init_uart = false; char *redir_url = redir_url_buf; redir_url += sprintf(redir_url, SET_REDIR_ERR); @@ -317,6 +319,7 @@ cgiTermCfgSetParams(HttpdConnData *connData) baud == BIT_RATE_1843200 || baud == BIT_RATE_3686400) { sysconf->uart_baudrate = (u32) baud; + shall_init_uart = true; } else { cgi_warn("Bad baud rate %s", buff); redir_url += sprintf(redir_url, "uart_baud,"); @@ -328,6 +331,7 @@ cgiTermCfgSetParams(HttpdConnData *connData) int parity = atoi(buff); if (parity >= 0 && parity <= 2) { sysconf->uart_parity = (UartParityMode) parity; + shall_init_uart = true; } else { cgi_warn("Bad parity %s", buff); redir_url += sprintf(redir_url, "uart_parity,"); @@ -339,6 +343,7 @@ cgiTermCfgSetParams(HttpdConnData *connData) int stopbits = atoi(buff); if (stopbits >= 1 && stopbits <= 3) { sysconf->uart_stopbits = (UartStopBitsNum) stopbits; + shall_init_uart = true; } else { cgi_warn("Bad stopbits %s", buff); redir_url += sprintf(redir_url, "uart_stopbits,"); @@ -359,6 +364,10 @@ cgiTermCfgSetParams(HttpdConnData *connData) terminal_apply_settings_noclear(); } + if (shall_init_uart) { + serialInit(); + } + if (notify_screen_content) { screen_notifyChange(CHANGE_CONTENT); } diff --git a/user/syscfg.c b/user/syscfg.c index 418dc85..cd22d77 100644 --- a/user/syscfg.c +++ b/user/syscfg.c @@ -13,10 +13,11 @@ void ICACHE_FLASH_ATTR sysconf_apply_settings(void) { bool changed = false; -// if (sysconf->config_version < 1) { -// dbg("Upgrading syscfg to v 1"); -// changed = true; -// } + if (sysconf->config_version < 1) { + dbg("Upgrading syscfg to v 1"); + sysconf->overclock = false; + changed = true; + } sysconf->config_version = SYSCONF_VERSION; @@ -24,7 +25,10 @@ sysconf_apply_settings(void) persist_store(); } + // uart settings live here, but the CGI handler + form has been moved to the Terminal config page serialInit(); + + system_update_cpu_freq((uint8) (sysconf->overclock ? 160 : 80)); } void ICACHE_FLASH_ATTR @@ -38,4 +42,5 @@ sysconf_restore_defaults(void) sysconf->pwlock = PWLOCK_NONE; strcpy(sysconf->access_pw, DEF_ACCESS_PW); strcpy(sysconf->access_name, DEF_ACCESS_NAME); + sysconf->overclock = false; } diff --git a/user/syscfg.h b/user/syscfg.h index 8f1101d..e316b68 100644 --- a/user/syscfg.h +++ b/user/syscfg.h @@ -10,7 +10,7 @@ // Size designed for the wifi config structure // Must be constant to avoid corrupting user config after upgrade #define SYSCONF_SIZE 300 -#define SYSCONF_VERSION 0 +#define SYSCONF_VERSION 1 #define DEF_ACCESS_PW "1234" #define DEF_ACCESS_NAME "espterm" @@ -32,6 +32,7 @@ typedef struct { enum pwlock pwlock : 8; // page access lock char access_pw[64]; // access password char access_name[32]; // access name + bool overclock; } SystemConfigBundle; extern SystemConfigBundle * const sysconf; diff --git a/user/uart_buffer.c b/user/uart_buffer.c index 6eaf5d3..20bc3c6 100644 --- a/user/uart_buffer.c +++ b/user/uart_buffer.c @@ -9,7 +9,7 @@ #include #define UART_TX_BUFFER_SIZE 512 //Ring buffer length of tx buffer -#define UART_RX_BUFFER_SIZE 512 //Ring buffer length of rx buffer +#define UART_RX_BUFFER_SIZE 600 //Ring buffer length of rx buffer struct UartBuffer { uint32 UartBuffSize; @@ -158,24 +158,34 @@ void UART_RxFifoCollect(void) uint8 fifo_data; fifo_len = (uint8) ((READ_PERI_REG(UART_STATUS(UART0)) >> UART_RXFIFO_CNT_S) & UART_RXFIFO_CNT); if (fifo_len >= pRxBuffer->Space) { - UART_WriteChar(UART1, '%', 100); - } - else { + // try to read at least the bit we can + fifo_len = (uint8) (pRxBuffer->Space - 1); + UART_WriteChar(UART1, '%', 1); + // discard contents of the FIFO - would loop forever buf_idx = 0; while (buf_idx < fifo_len) { buf_idx++; fifo_data = (uint8) (READ_PERI_REG(UART_FIFO(UART0)) & 0xFF); - *(pRxBuffer->pInPos++) = fifo_data; - if (pRxBuffer->pInPos == (pRxBuffer->pUartBuff + pRxBuffer->UartBuffSize)) { - pRxBuffer->pInPos = pRxBuffer->pUartBuff; - } + (void)fifo_data; // pretend we use it } - pRxBuffer->Space -= fifo_len; + return; + } - if (pRxBuffer->Space >= UART_FIFO_LEN) { - uart_rx_intr_enable(UART0); + buf_idx = 0; + while (buf_idx < fifo_len) { + buf_idx++; + fifo_data = (uint8) (READ_PERI_REG(UART_FIFO(UART0)) & 0xFF); + *(pRxBuffer->pInPos++) = fifo_data; + if (pRxBuffer->pInPos == (pRxBuffer->pUartBuff + pRxBuffer->UartBuffSize)) { + pRxBuffer->pInPos = pRxBuffer->pUartBuff; } } + pRxBuffer->Space -= fifo_len; +// + // this is called by the processing routine, no need here +// if (pRxBuffer->Space >= UART_FIFO_LEN) { +// uart_rx_intr_enable(UART0); +// } } u16 ICACHE_FLASH_ATTR UART_AsyncTxGetEmptySpace(void) @@ -209,7 +219,7 @@ UART_SendAsync(const char *pdata, int16_t data_len) UART_WriteToAsyncBuffer(pTxBuffer, pdata, real_len); } else { - UART_WriteChar(UART1, '^', 100); + UART_WriteChar(UART1, '^', 1); } // } diff --git a/user/uart_handler.c b/user/uart_handler.c index a3d0a00..1f96d2f 100755 --- a/user/uart_handler.c +++ b/user/uart_handler.c @@ -25,7 +25,7 @@ static void uart_processTask(os_event_t *events); // Those heavily affect the byte loss ratio #define PROCESS_CHUNK_LEN 1 -#define FIFO_FULL_THRES 4 +#define FIFO_FULL_THRES 32 #define uart_recvTaskPrio 1 #define uart_recvTaskQueueLen 25