From 44588702282ced8ef00192f13005e4f61f53b32f Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Mon, 22 Feb 2021 12:04:20 +0100 Subject: [PATCH] 64bit millis() preparations --- CMakeLists.txt | 2 - cores/esp32/Arduino.h | 7 - cores/esp32/HardwareSerial.cpp | 4 +- cores/esp32/HardwareSerial.h | 4 +- cores/esp32/Print.h | 6 + cores/esp32/Stream.cpp | 337 --------------------------------- cores/esp32/Stream.h | 140 -------------- cores/esp32/USBCDC.h | 5 +- cores/esp32/esp32-hal-misc.c | 5 - cores/esp32/esp32-hal-time.c | 97 ---------- cores/esp32/esp32-hal.h | 1 - libraries/FS/src/FS.h | 5 +- libraries/Wire/src/Wire.h | 4 +- 13 files changed, 18 insertions(+), 599 deletions(-) delete mode 100644 cores/esp32/Stream.cpp delete mode 100644 cores/esp32/Stream.h delete mode 100644 cores/esp32/esp32-hal-time.c diff --git a/CMakeLists.txt b/CMakeLists.txt index f67513e3..f6f1e23d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,6 @@ set(CORE_SRCS cores/esp32/esp32-hal-psram.c cores/esp32/esp32-hal-sigmadelta.c cores/esp32/esp32-hal-spi.c - cores/esp32/esp32-hal-time.c cores/esp32/esp32-hal-timer.c cores/esp32/esp32-hal-tinyusb.c cores/esp32/esp32-hal-touch.c @@ -25,7 +24,6 @@ set(CORE_SRCS cores/esp32/libb64/cencode.c cores/esp32/Print.cpp cores/esp32/stdlib_noniso.c - cores/esp32/Stream.cpp cores/esp32/USB.cpp cores/esp32/USBCDC.cpp cores/esp32/wiring_pulse.c diff --git a/cores/esp32/Arduino.h b/cores/esp32/Arduino.h index e66e2ea3..12aa7481 100644 --- a/cores/esp32/Arduino.h +++ b/cores/esp32/Arduino.h @@ -139,7 +139,6 @@ void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val); #include #include "WCharacter.h" -#include "Stream.h" #include "Printable.h" #include "Print.h" #include "IPAddress.h" @@ -160,12 +159,6 @@ uint16_t makeWord(byte h, byte l); unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L); unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L); -extern "C" bool getLocalTime(struct tm * info, uint32_t ms = 5000); -extern "C" void configTime(long gmtOffset_sec, int daylightOffset_sec, - const char* server1, const char* server2 = nullptr, const char* server3 = nullptr); -extern "C" void configTzTime(const char* tz, - const char* server1, const char* server2 = nullptr, const char* server3 = nullptr); - // WMath prototypes long random(long); #endif /* __cplusplus */ diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index b8ae042e..d2de24e3 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -85,9 +85,9 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in if(!baud) { uartStartDetectBaudrate(_uart); - time_t startMillis = millis(); + time_t startMillis = esp_timer_get_time() / 1000ULL; unsigned long detectedBaudRate = 0; - while(millis() - startMillis < timeout_ms && !(detectedBaudRate = uartDetectBaudrate(_uart))) { + while((esp_timer_get_time() / 1000ULL) - startMillis < timeout_ms && !(detectedBaudRate = uartDetectBaudrate(_uart))) { yield(); } diff --git a/cores/esp32/HardwareSerial.h b/cores/esp32/HardwareSerial.h index 0fdb1c41..6ccd9fab 100644 --- a/cores/esp32/HardwareSerial.h +++ b/cores/esp32/HardwareSerial.h @@ -47,10 +47,10 @@ #include -#include "Stream.h" +#include "Print.h" #include "esp32-hal.h" -class HardwareSerial: public Stream +class HardwareSerial: public Print { public: HardwareSerial(int uart_nr); diff --git a/cores/esp32/Print.h b/cores/esp32/Print.h index e5e8abf6..02213da4 100644 --- a/cores/esp32/Print.h +++ b/cores/esp32/Print.h @@ -72,6 +72,12 @@ public: return write((const uint8_t *) buffer, size); } + + virtual int available() = 0; + virtual int read() = 0; + virtual int peek() = 0; + virtual void flush() = 0; + size_t printf(const char * format, ...) __attribute__ ((format (printf, 2, 3))); size_t print(const std::string &); size_t print(const char[]); diff --git a/cores/esp32/Stream.cpp b/cores/esp32/Stream.cpp deleted file mode 100644 index 1885a2d8..00000000 --- a/cores/esp32/Stream.cpp +++ /dev/null @@ -1,337 +0,0 @@ -/* - Stream.cpp - adds parsing methods to Stream class - Copyright (c) 2008 David A. Mellis. All right reserved. - - 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 - - Created July 2011 - parsing functions based on TextFinder library by Michael Margolis - */ - -#include "Arduino.h" -#include "Stream.h" -#include "esp32-hal.h" - -#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait -#define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field - -// private method to read stream with timeout -int Stream::timedRead() -{ - int c; - _startMillis = millis(); - do { - c = read(); - if(c >= 0) { - return c; - } - } while(millis() - _startMillis < _timeout); - return -1; // -1 indicates timeout -} - -// private method to peek stream with timeout -int Stream::timedPeek() -{ - int c; - _startMillis = millis(); - do { - c = peek(); - if(c >= 0) { - return c; - } - } while(millis() - _startMillis < _timeout); - return -1; // -1 indicates timeout -} - -// returns peek of the next digit in the stream or -1 if timeout -// discards non-numeric characters -int Stream::peekNextDigit() -{ - int c; - while(1) { - c = timedPeek(); - if(c < 0) { - return c; // timeout - } - if(c == '-') { - return c; - } - if(c >= '0' && c <= '9') { - return c; - } - read(); // discard non-numeric - } -} - -// Public Methods -////////////////////////////////////////////////////////////// - -void Stream::setTimeout(unsigned long timeout) // sets the maximum number of milliseconds to wait -{ - _timeout = timeout; -} -unsigned long Stream::getTimeout(void) { - return _timeout; -} - -// find returns true if the target string is found -bool Stream::find(const char *target) -{ - return findUntil(target, strlen(target), NULL, 0); -} - -// reads data from the stream until the target string of given length is found -// returns true if target string is found, false if timed out -bool Stream::find(const char *target, size_t length) -{ - return findUntil(target, length, NULL, 0); -} - -// as find but search ends if the terminator string is found -bool Stream::findUntil(const char *target, const char *terminator) -{ - return findUntil(target, strlen(target), terminator, strlen(terminator)); -} - -// reads data from the stream until the target string of the given length is found -// search terminated if the terminator string is found -// returns true if target string is found, false if terminated or timed out -bool Stream::findUntil(const char *target, size_t targetLen, const char *terminator, size_t termLen) -{ - if (terminator == NULL) { - MultiTarget t[1] = {{target, targetLen, 0}}; - return findMulti(t, 1) == 0 ? true : false; - } else { - MultiTarget t[2] = {{target, targetLen, 0}, {terminator, termLen, 0}}; - return findMulti(t, 2) == 0 ? true : false; - } -} - -int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) { - // any zero length target string automatically matches and would make - // a mess of the rest of the algorithm. - for (struct MultiTarget *t = targets; t < targets+tCount; ++t) { - if (t->len <= 0) - return t - targets; - } - - while (1) { - int c = timedRead(); - if (c < 0) - return -1; - - for (struct MultiTarget *t = targets; t < targets+tCount; ++t) { - // the simple case is if we match, deal with that first. - if (c == t->str[t->index]) { - if (++t->index == t->len) - return t - targets; - else - continue; - } - - // if not we need to walk back and see if we could have matched further - // down the stream (ie '1112' doesn't match the first position in '11112' - // but it will match the second position so we can't just reset the current - // index to 0 when we find a mismatch. - if (t->index == 0) - continue; - - int origIndex = t->index; - do { - --t->index; - // first check if current char works against the new current index - if (c != t->str[t->index]) - continue; - - // if it's the only char then we're good, nothing more to check - if (t->index == 0) { - t->index++; - break; - } - - // otherwise we need to check the rest of the found string - int diff = origIndex - t->index; - size_t i; - for (i = 0; i < t->index; ++i) { - if (t->str[i] != t->str[i + diff]) - break; - } - - // if we successfully got through the previous loop then our current - // index is good. - if (i == t->index) { - t->index++; - break; - } - - // otherwise we just try the next index - } while (t->index); - } - } - // unreachable - return -1; -} - -// returns the first valid (long) integer value from the current position. -// initial characters that are not digits (or the minus sign) are skipped -// function is terminated by the first character that is not a digit. -long Stream::parseInt() -{ - return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout) -} - -// as above but a given skipChar is ignored -// this allows format characters (typically commas) in values to be ignored -long Stream::parseInt(char skipChar) -{ - boolean isNegative = false; - long value = 0; - int c; - - c = peekNextDigit(); - // ignore non numeric leading characters - if(c < 0) { - return 0; // zero returned if timeout - } - - do { - if(c == skipChar) { - } // ignore this charactor - else if(c == '-') { - isNegative = true; - } else if(c >= '0' && c <= '9') { // is c a digit? - value = value * 10 + c - '0'; - } - read(); // consume the character we got with peek - c = timedPeek(); - } while((c >= '0' && c <= '9') || c == skipChar); - - if(isNegative) { - value = -value; - } - return value; -} - -// as parseInt but returns a floating point value -float Stream::parseFloat() -{ - return parseFloat(NO_SKIP_CHAR); -} - -// as above but the given skipChar is ignored -// this allows format characters (typically commas) in values to be ignored -float Stream::parseFloat(char skipChar) -{ - boolean isNegative = false; - boolean isFraction = false; - long value = 0; - int c; - float fraction = 1.0; - - c = peekNextDigit(); - // ignore non numeric leading characters - if(c < 0) { - return 0; // zero returned if timeout - } - - do { - if(c == skipChar) { - } // ignore - else if(c == '-') { - isNegative = true; - } else if(c == '.') { - isFraction = true; - } else if(c >= '0' && c <= '9') { // is c a digit? - value = value * 10 + c - '0'; - if(isFraction) { - fraction *= 0.1; - } - } - read(); // consume the character we got with peek - c = timedPeek(); - } while((c >= '0' && c <= '9') || c == '.' || c == skipChar); - - if(isNegative) { - value = -value; - } - if(isFraction) { - return value * fraction; - } else { - return value; - } -} - -// read characters from stream into buffer -// terminates if length characters have been read, or timeout (see setTimeout) -// returns the number of characters placed in the buffer -// the buffer is NOT null terminated. -// -size_t Stream::readBytes(char *buffer, size_t length) -{ - size_t count = 0; - while(count < length) { - int c = timedRead(); - if(c < 0) { - break; - } - *buffer++ = (char) c; - count++; - } - return count; -} - -// as readBytes with terminator character -// terminates if length characters have been read, timeout, or if the terminator character detected -// returns the number of characters placed in the buffer (0 means no valid data found) - -size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length) -{ - if(length < 1) { - return 0; - } - size_t index = 0; - while(index < length) { - int c = timedRead(); - if(c < 0 || c == terminator) { - break; - } - *buffer++ = (char) c; - index++; - } - return index; // return number of characters, not including null terminator -} - -std::string Stream::readString() -{ - std::string ret; - int c = timedRead(); - while(c >= 0) { - ret += (char) c; - c = timedRead(); - } - return ret; -} - -std::string Stream::readStringUntil(char terminator) -{ - std::string ret; - int c = timedRead(); - while(c >= 0 && c != terminator) { - ret += (char) c; - c = timedRead(); - } - return ret; -} - diff --git a/cores/esp32/Stream.h b/cores/esp32/Stream.h deleted file mode 100644 index 5a8cf68b..00000000 --- a/cores/esp32/Stream.h +++ /dev/null @@ -1,140 +0,0 @@ -/* - Stream.h - base class for character-based streams. - Copyright (c) 2010 David A. Mellis. All right reserved. - - 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 - - parsing functions based on TextFinder library by Michael Margolis - */ - -#ifndef Stream_h -#define Stream_h - -#include -#include "Print.h" - -// compatability macros for testing -/* - #define getInt() parseInt() - #define getInt(skipChar) parseInt(skipchar) - #define getFloat() parseFloat() - #define getFloat(skipChar) parseFloat(skipChar) - #define getString( pre_string, post_string, buffer, length) - readBytesBetween( pre_string, terminator, buffer, length) - */ - -class Stream: public Print -{ -protected: - unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read - unsigned long _startMillis; // used for timeout measurement - int timedRead(); // private method to read stream with timeout - int timedPeek(); // private method to peek stream with timeout - int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout - -public: - virtual int available() = 0; - virtual int read() = 0; - virtual int peek() = 0; - virtual void flush() = 0; - - Stream():_startMillis(0) - { - _timeout = 1000; - } - virtual ~Stream() {} - -// parsing methods - - void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second - unsigned long getTimeout(void); - - bool find(const char *target); // reads data from the stream until the target string is found - bool find(uint8_t *target) - { - return find((char *) target); - } - // returns true if target string is found, false if timed out (see setTimeout) - - bool find(const char *target, size_t length); // reads data from the stream until the target string of given length is found - bool find(const uint8_t *target, size_t length) - { - return find((char *) target, length); - } - // returns true if target string is found, false if timed out - - bool find(char target) - { - return find (&target, 1); - } - - bool findUntil(const char *target, const char *terminator); // as find but search ends if the terminator string is found - bool findUntil(const uint8_t *target, const char *terminator) - { - return findUntil((char *) target, terminator); - } - - bool findUntil(const char *target, size_t targetLen, const char *terminate, size_t termLen); // as above but search ends if the terminate string is found - bool findUntil(const uint8_t *target, size_t targetLen, const char *terminate, size_t termLen) - { - return findUntil((char *) target, targetLen, terminate, termLen); - } - - long parseInt(); // returns the first valid (long) integer value from the current position. - // initial characters that are not digits (or the minus sign) are skipped - // integer is terminated by the first character that is not a digit. - - float parseFloat(); // float version of parseInt - - virtual size_t readBytes(char *buffer, size_t length); // read chars from stream into buffer - virtual size_t readBytes(uint8_t *buffer, size_t length) - { - return readBytes((char *) buffer, length); - } - // terminates if length characters have been read or timeout (see setTimeout) - // returns the number of characters placed in the buffer (0 means no valid data found) - - size_t readBytesUntil(char terminator, char *buffer, size_t length); // as readBytes with terminator character - size_t readBytesUntil(char terminator, uint8_t *buffer, size_t length) - { - return readBytesUntil(terminator, (char *) buffer, length); - } - // terminates if length characters have been read, timeout, or if the terminator character detected - // returns the number of characters placed in the buffer (0 means no valid data found) - - // Arduino String functions to be added here - virtual std::string readString(); - std::string readStringUntil(char terminator); - -protected: - long parseInt(char skipChar); // as above but the given skipChar is ignored - // as above but the given skipChar is ignored - // this allows format characters (typically commas) in values to be ignored - - float parseFloat(char skipChar); // as above but the given skipChar is ignored - - struct MultiTarget { - const char *str; // string you're searching for - size_t len; // length of string you're searching for - size_t index; // index used by the search routine. - }; - - // This allows you to search for an arbitrary number of strings. - // Returns index of the target that is found first or -1 if timeout occurs. - int findMulti(struct MultiTarget *targets, int tCount); - -}; - -#endif diff --git a/cores/esp32/USBCDC.h b/cores/esp32/USBCDC.h index 9a6aa3db..59f5902d 100644 --- a/cores/esp32/USBCDC.h +++ b/cores/esp32/USBCDC.h @@ -15,12 +15,13 @@ #include -#include "Stream.h" #include "esp32-hal.h" #if CONFIG_USB_CDC_ENABLED #include "esp_event.h" +#include "Print.h" + ESP_EVENT_DECLARE_BASE(ARDUINO_USB_CDC_EVENTS); typedef enum { @@ -49,7 +50,7 @@ typedef union { } rx; } arduino_usb_cdc_event_data_t; -class USBCDC: public Stream +class USBCDC: public Print { public: USBCDC(uint8_t itf=0); diff --git a/cores/esp32/esp32-hal-misc.c b/cores/esp32/esp32-hal-misc.c index 2602c287..751b739f 100644 --- a/cores/esp32/esp32-hal-misc.c +++ b/cores/esp32/esp32-hal-misc.c @@ -65,11 +65,6 @@ unsigned long ARDUINO_ISR_ATTR micros() return (unsigned long) (esp_timer_get_time()); } -unsigned long ARDUINO_ISR_ATTR millis() -{ - return (unsigned long) (esp_timer_get_time() / 1000ULL); -} - void delay(uint32_t ms) { vTaskDelay(ms / portTICK_PERIOD_MS); diff --git a/cores/esp32/esp32-hal-time.c b/cores/esp32/esp32-hal-time.c deleted file mode 100644 index e7048307..00000000 --- a/cores/esp32/esp32-hal-time.c +++ /dev/null @@ -1,97 +0,0 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "esp32-hal.h" -#include "lwip/apps/sntp.h" -//#include "tcpip_adapter.h" -#include "esp_netif.h" - -static void setTimeZone(long offset, int daylight) -{ - char cst[17] = {0}; - char cdt[17] = "DST"; - char tz[33] = {0}; - - if(offset % 3600){ - sprintf(cst, "UTC%ld:%02u:%02u", offset / 3600, abs((offset % 3600) / 60), abs(offset % 60)); - } else { - sprintf(cst, "UTC%ld", offset / 3600); - } - if(daylight != 3600){ - long tz_dst = offset - daylight; - if(tz_dst % 3600){ - sprintf(cdt, "DST%ld:%02u:%02u", tz_dst / 3600, abs((tz_dst % 3600) / 60), abs(tz_dst % 60)); - } else { - sprintf(cdt, "DST%ld", tz_dst / 3600); - } - } - sprintf(tz, "%s%s", cst, cdt); - setenv("TZ", tz, 1); - tzset(); -} - -/* - * configTime - * Source: https://github.com/esp8266/Arduino/blob/master/cores/esp8266/time.c - * */ -void configTime(long gmtOffset_sec, int daylightOffset_sec, const char* server1, const char* server2, const char* server3) -{ - //tcpip_adapter_init(); // Should not hurt anything if already inited - esp_netif_init(); - if(sntp_enabled()){ - sntp_stop(); - } - sntp_setoperatingmode(SNTP_OPMODE_POLL); - sntp_setservername(0, (char*)server1); - sntp_setservername(1, (char*)server2); - sntp_setservername(2, (char*)server3); - sntp_init(); - setTimeZone(-gmtOffset_sec, daylightOffset_sec); -} - -/* - * configTzTime - * sntp setup using TZ environment variable - * */ -void configTzTime(const char* tz, const char* server1, const char* server2, const char* server3) -{ - //tcpip_adapter_init(); // Should not hurt anything if already inited - esp_netif_init(); - if(sntp_enabled()){ - sntp_stop(); - } - sntp_setoperatingmode(SNTP_OPMODE_POLL); - sntp_setservername(0, (char*)server1); - sntp_setservername(1, (char*)server2); - sntp_setservername(2, (char*)server3); - sntp_init(); - setenv("TZ", tz, 1); - tzset(); -} - -bool getLocalTime(struct tm * info, uint32_t ms) -{ - uint32_t start = millis(); - time_t now; - while((millis()-start) <= ms) { - time(&now); - localtime_r(&now, info); - if(info->tm_year > (2016 - 1900)){ - return true; - } - delay(10); - } - return false; -} - diff --git a/cores/esp32/esp32-hal.h b/cores/esp32/esp32-hal.h index 824e4125..8d9f37db 100644 --- a/cores/esp32/esp32-hal.h +++ b/cores/esp32/esp32-hal.h @@ -89,7 +89,6 @@ void yield(void); float temperatureRead(); unsigned long micros(); -unsigned long millis(); void delay(uint32_t); void delayMicroseconds(uint32_t us); diff --git a/libraries/FS/src/FS.h b/libraries/FS/src/FS.h index 4926f8ed..f9e8d690 100644 --- a/libraries/FS/src/FS.h +++ b/libraries/FS/src/FS.h @@ -23,6 +23,7 @@ #include #include +#include "Print.h" namespace fs { @@ -44,11 +45,11 @@ enum SeekMode { SeekEnd = 2 }; -class File : public Stream +class File : public Print { public: File(FileImplPtr p = FileImplPtr()) : _p(p) { - _timeout = 0; + //_timeout = 0; } size_t write(uint8_t) override; diff --git a/libraries/Wire/src/Wire.h b/libraries/Wire/src/Wire.h index 00f268b7..5cebcd10 100644 --- a/libraries/Wire/src/Wire.h +++ b/libraries/Wire/src/Wire.h @@ -28,14 +28,14 @@ #include #include "freertos/FreeRTOS.h" #include "freertos/queue.h" -#include "Stream.h" +#include "Print.h" #define STICKBREAKER 'V1.1.0' #define I2C_BUFFER_LENGTH 128 typedef void(*user_onRequest)(void); typedef void(*user_onReceive)(uint8_t*, int); -class TwoWire: public Stream +class TwoWire: public Print { protected: uint8_t num;