From ef44211eea4cfe08a33a5ecc313e9f2dae09e578 Mon Sep 17 00:00:00 2001 From: Clemens Kirchgatterer Date: Fri, 22 Feb 2019 16:50:55 +0100 Subject: [PATCH] emulation on host: Add full UART driver emulation. (#5785) * emulation on host: Add full UART driver emulation. This PR replaces the high level Serial mock with a more complete UART driver. This way the HardwareSerial works without any modifications. Additionally the driver supports UART0 and UART1 at the same time (UART0 is directed to stdout and UART1 writes to stderr). RX is implemented by switching the terminal into raw non-blocking mode and injecting each key-press directly into the FIFO of UART0. A new command line switch -c was added to ignore CTRL-C and send it via serial as well. The decumentation was updated accordingly. Reading and setting of GPIOs does only write to stderr, when compiled with D=1. But this is subject to be replaced with a proper GPIO emu anyway. Reading from GPIO0 now returns 1 instead of 0 because this is most likely a low active input. * Fixed unused variable. * Remove unused functions, as long as there are no debug macros using them. * Move user_interface.cc from MOCK_CPP_FILES_EMU to MOCK_CPP_FILES. * Move optimistic_yield() from user_interface.cpp to Arduino.cpp * Remove atexit() weak declaration (fixes segfault when calling atexit). * Improve resetting of terminal after program exit, convert \r to \r\n. * Show error, when STDOUT is not a TTY, minor code cleanung. --- tests/host/Makefile | 10 +- tests/host/README.txt | 12 +- tests/host/common/Arduino.cpp | 5 + tests/host/common/Arduino.h | 2 - tests/host/common/ArduinoMain.cpp | 77 ++++- tests/host/common/HostWiring.cpp | 14 +- tests/host/common/MockSerial.cpp | 137 --------- tests/host/common/MockUART.cpp | 433 +++++++++++++++++++++++++++ tests/host/common/mock.h | 9 + tests/host/common/user_interface.cpp | 5 - 10 files changed, 545 insertions(+), 159 deletions(-) delete mode 100644 tests/host/common/MockSerial.cpp create mode 100644 tests/host/common/MockUART.cpp diff --git a/tests/host/Makefile b/tests/host/Makefile index b0749392e..ae082baa3 100644 --- a/tests/host/Makefile +++ b/tests/host/Makefile @@ -65,6 +65,7 @@ CORE_CPP_FILES := $(addprefix $(CORE_PATH)/,\ libb64/cencode.cpp \ libb64/cdecode.cpp \ Schedule.cpp \ + HardwareSerial.cpp \ ) CORE_C_FILES := $(addprefix $(CORE_PATH)/,\ @@ -74,7 +75,7 @@ MOCK_CPP_FILES_COMMON := $(addprefix common/,\ Arduino.cpp \ spiffs_mock.cpp \ WMath.cpp \ - MockSerial.cpp \ + MockUART.cpp \ MockTools.cpp \ MocklwIP.cpp \ ) @@ -95,7 +96,6 @@ MOCK_C_FILES := $(addprefix common/,\ noniso.c \ ) - INC_PATHS += $(addprefix -I, \ . \ common \ @@ -231,7 +231,7 @@ ARDUINO_LIBS := \ WiFiServerSecureBearSSL.cpp \ BearSSLHelpers.cpp \ CertStoreBearSSL.cpp \ - ) \ + ) OPT_ARDUINO_LIBS ?= $(addprefix ../../libraries/,\ $(addprefix ESP8266WebServer/src/,\ @@ -251,7 +251,7 @@ OPT_ARDUINO_LIBS ?= $(addprefix ../../libraries/,\ DNSServer/src/DNSServer.cpp \ ESP8266AVRISP/src/ESP8266AVRISP.cpp \ ESP8266HTTPClient/src/ESP8266HTTPClient.cpp \ -) \ +) MOCK_ARDUINO_LIBS := $(addprefix common/,\ ClientContextSocket.cpp \ @@ -263,7 +263,7 @@ MOCK_ARDUINO_LIBS := $(addprefix common/,\ MockEsp.cpp \ MockEEPROM.cpp \ MockSPI.cpp \ -) \ +) CPP_SOURCES_CORE_EMU = \ $(MOCK_CPP_FILES_EMU) \ diff --git a/tests/host/README.txt b/tests/host/README.txt index 8bc0dac85..4e947a954 100644 --- a/tests/host/README.txt +++ b/tests/host/README.txt @@ -19,6 +19,13 @@ WiFiClient+WiFiServer/ClientContext and WifiUdp/UdpContext using socket posix API. Further work will optionally propose native lwIP library instead. +Serial UART0 and UART1 are emulated via stdin/stdout/stderr. Therefor +stdin is connected to UART0(RX) and stdout is connected to UART0(TX). +UART1(TX) writes to stderr. Reading from stdin happens in non-blocking +raw mode, that means each character is directly injected into the UART +FIFO without any buffering in the console. The command line switch -c +can be used to stop the emulation from intersepting CTRL-C (SIGINT). + How to compile and run a sketch ------------------------------- @@ -73,7 +80,10 @@ Options are available: -h -i eth0 bind servers to this interface (WIP) -l bind Multicast to the above interface (WIP) - -f no throttle (possibly 100%CPU) + -c ignore CTRL-C (send it over serial device) + -f no throttle (possibly 100%CPU) + -S spiffs size in KBytes (default: 1024) + (negative value will force mismatched size) TODO ---- diff --git a/tests/host/common/Arduino.cpp b/tests/host/common/Arduino.cpp index 5f173646a..830ab3480 100644 --- a/tests/host/common/Arduino.cpp +++ b/tests/host/common/Arduino.cpp @@ -37,6 +37,11 @@ extern "C" void yield() { } +extern "C" void optimistic_yield (uint32_t interval_us) +{ + usleep(interval_us); +} + extern "C" void esp_yield() { } diff --git a/tests/host/common/Arduino.h b/tests/host/common/Arduino.h index a1aba082c..9b75fc114 100644 --- a/tests/host/common/Arduino.h +++ b/tests/host/common/Arduino.h @@ -191,8 +191,6 @@ extern "C" { void init(void); void initVariant(void); - int atexit(void (*func)()) __attribute__((weak)); - void pinMode(uint8_t pin, uint8_t mode); void digitalWrite(uint8_t pin, uint8_t val); int digitalRead(uint8_t pin); diff --git a/tests/host/common/ArduinoMain.cpp b/tests/host/common/ArduinoMain.cpp index 54420d9e2..c7b71aac0 100644 --- a/tests/host/common/ArduinoMain.cpp +++ b/tests/host/common/ArduinoMain.cpp @@ -33,12 +33,58 @@ #include // wifi_get_ip_info() #include -#include // usleep +#include #include +#include +#include +#include bool user_exit = false; const char* host_interface = nullptr; size_t spiffs_kb = 1024; +bool ignore_sigint = false; +bool restore_tty = false; + +#define STDIN STDIN_FILENO + +static struct termios initial_settings; + +static int mock_start_uart(void) +{ + struct termios settings; + + if (!isatty(STDIN)) return 0; + if (tcgetattr(STDIN, &initial_settings) < 0) return -1; + settings = initial_settings; + settings.c_lflag &= ~(ignore_sigint ? ISIG : 0); + settings.c_lflag &= ~(ECHO | ICANON); + settings.c_iflag &= ~(ICRNL | INLCR | ISTRIP | IXON); + settings.c_oflag |= (ONLCR); + settings.c_cc[VMIN] = 0; + settings.c_cc[VTIME] = 0; + if (tcsetattr(STDIN, TCSANOW, &settings) < 0) return -2; + tty_restore = true; + return 0; +} + +static int mock_stop_uart(void) +{ + if (!restore_tty) return 0; + if (!isatty(STDIN)) { + perror("isatty(STDIN)"); + //system("stty sane"); <- same error message "Inappropriate ioctl for device" + return 0; + } + if (tcsetattr(STDIN, TCSANOW, &initial_settings) < 0) return -1; + printf("\e[?25h"); // show cursor + return (0); +} + +static uint8_t mock_read_uart(void) +{ + uint8_t ch = 0; + return (read(STDIN, &ch, 1) == 1) ? ch : 0; +} void help (const char* argv0, int exitcode) { @@ -48,9 +94,10 @@ void help (const char* argv0, int exitcode) " -h\n" " -i - use this interface for IP address\n" " -l - bind tcp/udp servers to interface only (not 0.0.0.0)\n" + " -c - ignore CTRL-C (send it via Serial)\n" " -f - no throttle (possibly 100%%CPU)\n" " -S - spiffs size in KBytes (default: %zd)\n" - " (negative value will force mismatched size)\n" + " (negative value will force mismatched size)\n" , argv0, spiffs_kb); exit(exitcode); } @@ -60,13 +107,15 @@ static struct option options[] = { "help", no_argument, NULL, 'h' }, { "fast", no_argument, NULL, 'f' }, { "local", no_argument, NULL, 'l' }, + { "sigint", no_argument, NULL, 'c' }, { "interface", required_argument, NULL, 'i' }, { "spiffskb", required_argument, NULL, 'S' }, }; -void save () +void cleanup () { mock_stop_spiffs(); + mock_stop_uart(); } void control_c (int sig) @@ -76,7 +125,7 @@ void control_c (int sig) if (user_exit) { fprintf(stderr, MOCK "stuck, killing\n"); - save(); + cleanup(); exit(1); } user_exit = true; @@ -90,7 +139,7 @@ int main (int argc, char* const argv []) for (;;) { - int n = getopt_long(argc, argv, "hlfi:S:", options, NULL); + int n = getopt_long(argc, argv, "hlcfi:S:", options, NULL); if (n < 0) break; switch (n) @@ -104,6 +153,9 @@ int main (int argc, char* const argv []) case 'l': global_ipv4_netfmt = NO_GLOBAL_BINDING; break; + case 'c': + ignore_sigint = true; + break; case 'f': fast = true; break; @@ -128,16 +180,25 @@ int main (int argc, char* const argv []) // setup global global_ipv4_netfmt wifi_get_ip_info(0, nullptr); + // set stdin to non blocking mode + mock_start_uart(); + + // install exit handler in case Esp.restart() is called + atexit(cleanup); + setup(); while (!user_exit) { + uint8_t data = mock_read_uart(); + + if (data) + uart_new_data(UART0, data); if (!fast) - usleep(10000); // not 100% cpu + usleep(1000); // not 100% cpu, ~1000 loops per second loop(); check_incoming_udp(); } - - save(); + cleanup(); return 0; } diff --git a/tests/host/common/HostWiring.cpp b/tests/host/common/HostWiring.cpp index b2887c032..d08733a52 100644 --- a/tests/host/common/HostWiring.cpp +++ b/tests/host/common/HostWiring.cpp @@ -46,17 +46,23 @@ void pinMode (uint8_t pin, uint8_t mode) case WAKEUP_PULLDOWN: m="WAKEUP_PULLDOWN"; break; default: m="(special)"; } +#ifdef DEBUG_ESP_CORE fprintf(stderr, MOCK "gpio%d: mode='%s'\n", pin, m); +#endif } void digitalWrite(uint8_t pin, uint8_t val) { +#ifdef DEBUG_ESP_CORE fprintf(stderr, MOCK "digitalWrite(pin=%d val=%d)\n", pin, val); +#endif } void analogWrite(uint8_t pin, int val) { +#ifdef DEBUG_ESP_CORE fprintf(stderr, MOCK "analogWrite(pin=%d, val=%d\n", pin, val); +#endif } int analogRead(uint8_t pin) @@ -67,11 +73,17 @@ int analogRead(uint8_t pin) void analogWriteRange(uint32_t range) { +#ifdef DEBUG_ESP_CORE fprintf(stderr, MOCK "analogWriteRange(range=%d)\n", range); +#endif } int digitalRead(uint8_t pin) { +#ifdef DEBUG_ESP_CORE fprintf(stderr, MOCK "digitalRead(%d)\n", pin); - return 0; +#endif + + // pin 0 is most likely a low active input + return pin ? 0 : 1; } diff --git a/tests/host/common/MockSerial.cpp b/tests/host/common/MockSerial.cpp deleted file mode 100644 index eedcaed11..000000000 --- a/tests/host/common/MockSerial.cpp +++ /dev/null @@ -1,137 +0,0 @@ -/* - Arduino Hardware Serial emulation - Copyright (c) 2018 david gauchard. All rights reserved. - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal with the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - - Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimers. - - - Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimers in the - documentation and/or other materials provided with the distribution. - - - The names of its contributors may not be used to endorse or promote - products derived from this Software without specific prior written - permission. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR - OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS WITH THE SOFTWARE. -*/ - -#include -#include - -#include // write - -HardwareSerial Serial(UART0); - -HardwareSerial::HardwareSerial (int uart_nr) -{ - if (uart_nr != 0) - fprintf(stderr, MOCK "FIXME HardwareSerial::HardwareSerial(%d)\n", uart_nr); - _uart = (decltype(_uart))1; // not used, for 'while (!Serial);' to pass -} - -void HardwareSerial::begin (unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin) -{ - if (config != SERIAL_8N1 || mode != SERIAL_FULL || tx_pin != 1) - fprintf(stderr, MOCK "FIXME HardwareSerial::begin(baud=%ld config=0x%x mode=0x%x)\n", baud, (int)config, (int)mode); -} - -void HardwareSerial::setDebugOutput (bool on) -{ - (void)on; -} - -int HardwareSerial::available (void) -{ - printf(MOCK "TODO HardwareSerial::available\n"); - return 0; -} - -void HardwareSerial::flush () -{ - //XXXTODO - fflush(stdout); -} - -size_t HardwareSerial::readBytes(char* buffer, size_t size) -{ - size_t got = 0; - - while (got < size) - { - esp8266::polledTimeout::oneShot timeOut(_timeout); - size_t avail; - while ((avail = available()) == 0 && !timeOut); - if (avail == 0) - break; - got += read(buffer + got, std::min(size - got, avail)); - } - return got; -} - -// uart.c - -extern "C" -{ - -size_t uart_write_char (uart_t* uart, char c) -{ - //XXXTODO - (void)uart; - return write(1, &c, 1); -} - -int uart_peek_char (uart_t* uart) -{ - ///XXXTODO - static bool notimpl = false; - if (!notimpl) - { - notimpl = true; - fprintf(stderr, MOCK "FIXME uart_peek_char\n"); - } - (void)uart; - return -1; -} - -int uart_read_char (uart_t* uart) -{ - ///XXXTODO - static bool notimpl = false; - if (!notimpl) - { - notimpl = true; - fprintf(stderr, MOCK "FIXME uart_read_char\n"); - } - (void)uart; - return -1; -} - -size_t uart_write (uart_t* uart, const char* buf, size_t size) -{ - ///XXXTODO - (void)uart; - return write(1, buf, size); -} - -size_t uart_read(uart_t* uart, char* userbuffer, size_t usersize) -{ - ///XXXTODO - (void)uart; - return read(0, userbuffer, usersize); -} - -} // extern "C" diff --git a/tests/host/common/MockUART.cpp b/tests/host/common/MockUART.cpp new file mode 100644 index 000000000..0e6ec7615 --- /dev/null +++ b/tests/host/common/MockUART.cpp @@ -0,0 +1,433 @@ +/* + MockUART.cpp - esp8266 UART HAL EMULATION + + Copyright (c) 2019 Clemens Kirchgatterer. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* + This UART driver is directly derived from the ESP8266 UART HAL driver + Copyright (c) 2014 Ivan Grokhotkov. It provides the same API as the + original driver and was striped from all HW dependent interfaces. + + UART0 writes got to stdout, while UART1 writes got to stderr. The user + is responsible for feeding the RX FIFO new data by calling uart_new_data(). + */ + +#include // write + +#include "Arduino.h" +#include "uart.h" + +//#define UART_DISCARD_NEWEST + +extern "C" { + +static int s_uart_debug_nr = UART1; + +static uart_t *UART[2] = { NULL, NULL }; + +struct uart_rx_buffer_ +{ + size_t size; + size_t rpos; + size_t wpos; + uint8_t * buffer; +}; + +struct uart_ +{ + int uart_nr; + int baud_rate; + bool rx_enabled; + bool tx_enabled; + bool rx_overrun; + struct uart_rx_buffer_ * rx_buffer; +}; + +// write one byte to the emulated UART +static void +uart_do_write_char(const int uart_nr, char c) +{ + if (uart_nr >= UART0 && uart_nr <= UART1) + write(uart_nr + 1, &c, 1); +} + +// write a new byte into the RX FIFO buffer +static void +uart_handle_data(uart_t* uart, uint8_t data) +{ + struct uart_rx_buffer_ *rx_buffer = uart->rx_buffer; + + size_t nextPos = (rx_buffer->wpos + 1) % rx_buffer->size; + if(nextPos == rx_buffer->rpos) + { + uart->rx_overrun = true; +#ifdef UART_DISCARD_NEWEST + return; +#else + if (++rx_buffer->rpos == rx_buffer->size) + rx_buffer->rpos = 0; +#endif + } + rx_buffer->buffer[rx_buffer->wpos] = data; + rx_buffer->wpos = nextPos; +} + +// insert a new byte into the RX FIFO nuffer +void +uart_new_data(const int uart_nr, uint8_t data) +{ + uart_t* uart = UART[uart_nr]; + + if(uart == NULL || !uart->rx_enabled) { + return; + } + + uart_handle_data(uart, data); +} + +static size_t +uart_rx_available_unsafe(const struct uart_rx_buffer_ * rx_buffer) +{ + size_t ret = rx_buffer->wpos - rx_buffer->rpos; + + if(rx_buffer->wpos < rx_buffer->rpos) + ret = (rx_buffer->wpos + rx_buffer->size) - rx_buffer->rpos; + + return ret; +} + +// taking data straight from fifo, only needed in uart_resize_rx_buffer() +static int +uart_read_char_unsafe(uart_t* uart) +{ + if (uart_rx_available_unsafe(uart->rx_buffer)) + { + // take oldest sw data + int ret = uart->rx_buffer->buffer[uart->rx_buffer->rpos]; + uart->rx_buffer->rpos = (uart->rx_buffer->rpos + 1) % uart->rx_buffer->size; + return ret; + } + // unavailable + return -1; +} + +/**********************************************************/ +/************ UART API FUNCTIONS **************************/ +/**********************************************************/ + +size_t +uart_rx_available(uart_t* uart) +{ + if(uart == NULL || !uart->rx_enabled) + return 0; + + return uart_rx_available_unsafe(uart->rx_buffer); +} + +int +uart_peek_char(uart_t* uart) +{ + if(uart == NULL || !uart->rx_enabled) + return -1; + + if (!uart_rx_available_unsafe(uart->rx_buffer)) + return -1; + + return uart->rx_buffer->buffer[uart->rx_buffer->rpos]; +} + +int +uart_read_char(uart_t* uart) +{ + uint8_t ret; + return uart_read(uart, (char*)&ret, 1) ? ret : -1; +} + +size_t +uart_read(uart_t* uart, char* userbuffer, size_t usersize) +{ + if(uart == NULL || !uart->rx_enabled) + return 0; + + size_t ret = 0; + while (ret < usersize && uart_rx_available_unsafe(uart->rx_buffer)) + { + // pour sw buffer to user's buffer + // get largest linear length from sw buffer + size_t chunk = uart->rx_buffer->rpos < uart->rx_buffer->wpos ? + uart->rx_buffer->wpos - uart->rx_buffer->rpos : + uart->rx_buffer->size - uart->rx_buffer->rpos; + if (ret + chunk > usersize) + chunk = usersize - ret; + memcpy(userbuffer + ret, uart->rx_buffer->buffer + uart->rx_buffer->rpos, chunk); + uart->rx_buffer->rpos = (uart->rx_buffer->rpos + chunk) % uart->rx_buffer->size; + ret += chunk; + } + return ret; +} + +size_t +uart_resize_rx_buffer(uart_t* uart, size_t new_size) +{ + if(uart == NULL || !uart->rx_enabled) + return 0; + + if(uart->rx_buffer->size == new_size) + return uart->rx_buffer->size; + + uint8_t * new_buf = (uint8_t*)malloc(new_size); + if(!new_buf) + return uart->rx_buffer->size; + + size_t new_wpos = 0; + // if uart_rx_available_unsafe() returns non-0, uart_read_char_unsafe() can't return -1 + while(uart_rx_available_unsafe(uart->rx_buffer) && new_wpos < new_size) + new_buf[new_wpos++] = uart_read_char_unsafe(uart); + if (new_wpos == new_size) + new_wpos = 0; + + uint8_t * old_buf = uart->rx_buffer->buffer; + uart->rx_buffer->rpos = 0; + uart->rx_buffer->wpos = new_wpos; + uart->rx_buffer->size = new_size; + uart->rx_buffer->buffer = new_buf; + free(old_buf); + return uart->rx_buffer->size; +} + +size_t +uart_get_rx_buffer_size(uart_t* uart) +{ + return uart && uart->rx_enabled ? uart->rx_buffer->size : 0; +} + +size_t +uart_write_char(uart_t* uart, char c) +{ + if(uart == NULL || !uart->tx_enabled) + return 0; + + uart_do_write_char(uart->uart_nr, c); + + return 1; +} + +size_t +uart_write(uart_t* uart, const char* buf, size_t size) +{ + if(uart == NULL || !uart->tx_enabled) + return 0; + + size_t ret = size; + const int uart_nr = uart->uart_nr; + while (size--) + uart_do_write_char(uart_nr, *buf++); + + return ret; +} + +size_t +uart_tx_free(uart_t* uart) +{ + if(uart == NULL || !uart->tx_enabled) + return 0; + + return UART_TX_FIFO_SIZE; +} + +void +uart_wait_tx_empty(uart_t* uart) +{ +} + +void +uart_flush(uart_t* uart) +{ + if(uart == NULL) + return; + + if(uart->rx_enabled) + { + uart->rx_buffer->rpos = 0; + uart->rx_buffer->wpos = 0; + } +} + +void +uart_set_baudrate(uart_t* uart, int baud_rate) +{ + if(uart == NULL) + return; + + uart->baud_rate = baud_rate; +} + +int +uart_get_baudrate(uart_t* uart) +{ + if(uart == NULL) + return 0; + + return uart->baud_rate; +} + +uart_t* +uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size) +{ + uart_t* uart = (uart_t*) malloc(sizeof(uart_t)); + if(uart == NULL) + return NULL; + + uart->uart_nr = uart_nr; + uart->rx_overrun = false; + + switch(uart->uart_nr) + { + case UART0: + uart->rx_enabled = (mode != UART_TX_ONLY); + uart->tx_enabled = (mode != UART_RX_ONLY); + if(uart->rx_enabled) + { + struct uart_rx_buffer_ * rx_buffer = (struct uart_rx_buffer_ *)malloc(sizeof(struct uart_rx_buffer_)); + if(rx_buffer == NULL) + { + free(uart); + return NULL; + } + rx_buffer->size = rx_size;//var this + rx_buffer->rpos = 0; + rx_buffer->wpos = 0; + rx_buffer->buffer = (uint8_t *)malloc(rx_buffer->size); + if(rx_buffer->buffer == NULL) + { + free(rx_buffer); + free(uart); + return NULL; + } + uart->rx_buffer = rx_buffer; + } + break; + + case UART1: + // Note: uart_interrupt_handler does not support RX on UART 1. + uart->rx_enabled = false; + uart->tx_enabled = (mode != UART_RX_ONLY); + break; + + case UART_NO: + default: + // big fail! + free(uart); + return NULL; + } + + uart_set_baudrate(uart, baudrate); + + UART[uart_nr] = uart; + + return uart; +} + +void +uart_uninit(uart_t* uart) +{ + if(uart == NULL) + return; + + if(uart->rx_enabled) { + free(uart->rx_buffer->buffer); + free(uart->rx_buffer); + } + free(uart); +} + +void +uart_swap(uart_t* uart, int tx_pin) +{ +} + +void +uart_set_tx(uart_t* uart, int tx_pin) +{ +} + +void +uart_set_pins(uart_t* uart, int tx, int rx) +{ +} + +bool +uart_tx_enabled(uart_t* uart) +{ + if(uart == NULL) + return false; + + return uart->tx_enabled; +} + +bool +uart_rx_enabled(uart_t* uart) +{ + if(uart == NULL) + return false; + + return uart->rx_enabled; +} + +bool +uart_has_overrun(uart_t* uart) +{ + if(uart == NULL || !uart->rx_overrun) + return false; + + // clear flag + uart->rx_overrun = false; + return true; +} + +bool +uart_has_rx_error(uart_t* uart) +{ + return false; +} + +void +uart_set_debug(int uart_nr) +{ + (void)uart_nr; +} + +int +uart_get_debug() +{ + return s_uart_debug_nr; +} + +void +uart_start_detect_baudrate(int uart_nr) +{ +} + +int +uart_detect_baudrate(int uart_nr) +{ + return 115200; +} + +}; diff --git a/tests/host/common/mock.h b/tests/host/common/mock.h index 23b5a675d..6cfafc5ff 100644 --- a/tests/host/common/mock.h +++ b/tests/host/common/mock.h @@ -94,6 +94,15 @@ extern uint32_t global_ipv4_netfmt; // selected interface addresse to bind to #define CCBUFSIZE 65536 #endif +// uart +#ifdef __cplusplus +extern "C" { +#endif +void uart_new_data(const int uart_nr, uint8_t data); +#ifdef __cplusplus +} +#endif + // tcp int mockSockSetup (int sock); int mockConnect (uint32_t addr, int& sock, int port); diff --git a/tests/host/common/user_interface.cpp b/tests/host/common/user_interface.cpp index 936eb075f..13d53ff06 100644 --- a/tests/host/common/user_interface.cpp +++ b/tests/host/common/user_interface.cpp @@ -434,11 +434,6 @@ void esp_schedule (void) { } -void optimistic_yield (uint32_t interval_us) -{ - usleep(interval_us); -} - void dns_setserver (u8_t numdns, ip_addr_t *dnsserver) { (void)numdns;