mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-19 23:22:16 +03:00
This reverts commit 98125f88605cd7e46e9be4e1b3ad0600dd5d2b51.
This commit is contained in:
parent
98125f8860
commit
eea9999dc5
@ -1,27 +1,27 @@
|
||||
/*
|
||||
AddrList.h - cycle through lwIP netif's ip addresses like a c++ list
|
||||
Copyright (c) 2018 david gauchard. All right reserved.
|
||||
AddrList.h - cycle through lwIP netif's ip addresses like a c++ list
|
||||
Copyright (c) 2018 david gauchard. 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 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.
|
||||
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
|
||||
*/
|
||||
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 class allows to explore all configured IP addresses
|
||||
in lwIP netifs, with that kind of c++ loop:
|
||||
This class allows to explore all configured IP addresses
|
||||
in lwIP netifs, with that kind of c++ loop:
|
||||
|
||||
for (auto a: addrList)
|
||||
for (auto a: addrList)
|
||||
out.printf("IF='%s' index=%d legacy=%d IPv4=%d local=%d hostname='%s' addr= %s\n",
|
||||
a.iface().c_str(),
|
||||
a.ifnumber(),
|
||||
@ -31,14 +31,14 @@
|
||||
a.hostname().c_str(),
|
||||
a.addr().toString().c_str());
|
||||
|
||||
This loop:
|
||||
This loop:
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED()) {
|
||||
Serial.print('.');
|
||||
delay(500);
|
||||
}
|
||||
|
||||
can be replaced by:
|
||||
can be replaced by:
|
||||
|
||||
for (bool configured = false; !configured; ) {
|
||||
for (auto iface: addrList)
|
||||
@ -48,7 +48,7 @@
|
||||
delay(500);
|
||||
}
|
||||
|
||||
waiting for an IPv6 global address:
|
||||
waiting for an IPv6 global address:
|
||||
|
||||
for (bool configured = false; !configured; ) {
|
||||
for (auto iface: addrList)
|
||||
@ -59,7 +59,7 @@
|
||||
delay(500);
|
||||
}
|
||||
|
||||
waiting for an IPv6 global address, on a specific interface:
|
||||
waiting for an IPv6 global address, on a specific interface:
|
||||
|
||||
for (bool configured = false; !configured; ) {
|
||||
for (auto iface: addrList)
|
||||
@ -94,8 +94,8 @@ namespace AddressListImplementation
|
||||
|
||||
struct netifWrapper
|
||||
{
|
||||
netifWrapper(netif* netif) : _netif(netif), _num(-1) {}
|
||||
netifWrapper(const netifWrapper& o) : _netif(o._netif), _num(o._num) {}
|
||||
netifWrapper (netif* netif) : _netif(netif), _num(-1) {}
|
||||
netifWrapper (const netifWrapper& o) : _netif(o._netif), _num(o._num) {}
|
||||
|
||||
netifWrapper& operator= (const netifWrapper& o)
|
||||
{
|
||||
@ -110,64 +110,25 @@ struct netifWrapper
|
||||
}
|
||||
|
||||
// address properties
|
||||
IPAddress addr() const
|
||||
{
|
||||
return ipFromNetifNum();
|
||||
}
|
||||
bool isLegacy() const
|
||||
{
|
||||
return _num == 0;
|
||||
}
|
||||
bool isLocal() const
|
||||
{
|
||||
return addr().isLocal();
|
||||
}
|
||||
bool isV4() const
|
||||
{
|
||||
return addr().isV4();
|
||||
}
|
||||
bool isV6() const
|
||||
{
|
||||
return !addr().isV4();
|
||||
}
|
||||
String toString() const
|
||||
{
|
||||
return addr().toString();
|
||||
}
|
||||
IPAddress addr () const { return ipFromNetifNum(); }
|
||||
bool isLegacy () const { return _num == 0; }
|
||||
bool isLocal () const { return addr().isLocal(); }
|
||||
bool isV4 () const { return addr().isV4(); }
|
||||
bool isV6 () const { return !addr().isV4(); }
|
||||
String toString() const { return addr().toString(); }
|
||||
|
||||
// related to legacy address (_num=0, ipv4)
|
||||
IPAddress ipv4() const
|
||||
{
|
||||
return _netif->ip_addr;
|
||||
}
|
||||
IPAddress netmask() const
|
||||
{
|
||||
return _netif->netmask;
|
||||
}
|
||||
IPAddress gw() const
|
||||
{
|
||||
return _netif->gw;
|
||||
}
|
||||
IPAddress ipv4 () const { return _netif->ip_addr; }
|
||||
IPAddress netmask () const { return _netif->netmask; }
|
||||
IPAddress gw () const { return _netif->gw; }
|
||||
|
||||
// common to all addresses of this interface
|
||||
String ifname() const
|
||||
{
|
||||
return String(_netif->name[0]) + _netif->name[1];
|
||||
}
|
||||
const char* ifhostname() const
|
||||
{
|
||||
return _netif->hostname ? : emptyString.c_str();
|
||||
}
|
||||
const char* ifmac() const
|
||||
{
|
||||
return (const char*)_netif->hwaddr;
|
||||
}
|
||||
int ifnumber() const
|
||||
{
|
||||
return _netif->num;
|
||||
}
|
||||
String ifname () const { return String(_netif->name[0]) + _netif->name[1]; }
|
||||
const char* ifhostname () const { return _netif->hostname?: emptyString.c_str(); }
|
||||
const char* ifmac () const { return (const char*)_netif->hwaddr; }
|
||||
int ifnumber () const { return _netif->num; }
|
||||
|
||||
const ip_addr_t* ipFromNetifNum() const
|
||||
const ip_addr_t* ipFromNetifNum () const
|
||||
{
|
||||
#if LWIP_IPV6
|
||||
return _num ? &_netif->ip6_addr[_num - 1] : &_netif->ip_addr;
|
||||
@ -189,8 +150,8 @@ struct netifWrapper
|
||||
class AddressListIterator
|
||||
{
|
||||
public:
|
||||
AddressListIterator(const netifWrapper& o) : netIf(o) {}
|
||||
AddressListIterator(netif* netif) : netIf(netif)
|
||||
AddressListIterator (const netifWrapper& o) : netIf(o) {}
|
||||
AddressListIterator (netif* netif) : netIf(netif)
|
||||
{
|
||||
// This constructor is called with lwIP's global netif_list, or
|
||||
// nullptr. operator++() is designed to loop through _configured_
|
||||
@ -199,29 +160,13 @@ public:
|
||||
(void)operator++();
|
||||
}
|
||||
|
||||
const netifWrapper& operator* () const
|
||||
{
|
||||
return netIf;
|
||||
}
|
||||
const netifWrapper* operator-> () const
|
||||
{
|
||||
return &netIf;
|
||||
}
|
||||
const netifWrapper& operator* () const { return netIf; }
|
||||
const netifWrapper* operator-> () const { return &netIf; }
|
||||
|
||||
bool operator== (AddressListIterator& o)
|
||||
{
|
||||
return netIf.equal(*o);
|
||||
}
|
||||
bool operator!= (AddressListIterator& o)
|
||||
{
|
||||
return !netIf.equal(*o);
|
||||
}
|
||||
bool operator== (AddressListIterator& o) { return netIf.equal(*o); }
|
||||
bool operator!= (AddressListIterator& o) { return !netIf.equal(*o); }
|
||||
|
||||
AddressListIterator& operator= (const AddressListIterator& o)
|
||||
{
|
||||
netIf = o.netIf;
|
||||
return *this;
|
||||
}
|
||||
AddressListIterator& operator= (const AddressListIterator& o) { netIf = o.netIf; return *this; }
|
||||
|
||||
AddressListIterator operator++ (int)
|
||||
{
|
||||
@ -243,9 +188,7 @@ public:
|
||||
}
|
||||
if (!ip_addr_isany(netIf.ipFromNetifNum()))
|
||||
// found an initialized address
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
@ -257,27 +200,15 @@ public:
|
||||
class AddressList
|
||||
{
|
||||
public:
|
||||
using const_iterator = const AddressListIterator;
|
||||
using const_iterator = const AddressListIterator;
|
||||
|
||||
const_iterator begin() const
|
||||
{
|
||||
return const_iterator(netif_list);
|
||||
}
|
||||
const_iterator end() const
|
||||
{
|
||||
return const_iterator(nullptr);
|
||||
}
|
||||
const_iterator begin () const { return const_iterator(netif_list); }
|
||||
const_iterator end () const { return const_iterator(nullptr); }
|
||||
|
||||
};
|
||||
|
||||
inline AddressList::const_iterator begin(const AddressList& a)
|
||||
{
|
||||
return a.begin();
|
||||
}
|
||||
inline AddressList::const_iterator end(const AddressList& a)
|
||||
{
|
||||
return a.end();
|
||||
}
|
||||
inline AddressList::const_iterator begin (const AddressList& a) { return a.begin(); }
|
||||
inline AddressList::const_iterator end (const AddressList& a) { return a.end(); }
|
||||
|
||||
|
||||
} // AddressListImplementation
|
||||
|
@ -1,21 +1,21 @@
|
||||
/*
|
||||
Arduino.h - Main include file for the Arduino SDK
|
||||
Copyright (c) 2005-2013 Arduino Team. All right reserved.
|
||||
Arduino.h - Main include file for the Arduino SDK
|
||||
Copyright (c) 2005-2013 Arduino Team. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef Arduino_h
|
||||
#define Arduino_h
|
||||
@ -86,11 +86,10 @@ extern "C" {
|
||||
#define EXTERNAL 0
|
||||
|
||||
//timer dividers
|
||||
enum TIM_DIV_ENUM
|
||||
{
|
||||
TIM_DIV1 = 0, //80MHz (80 ticks/us - 104857.588 us max)
|
||||
TIM_DIV16 = 1, //5MHz (5 ticks/us - 1677721.4 us max)
|
||||
TIM_DIV256 = 3 //312.5Khz (1 tick = 3.2us - 26843542.4 us max)
|
||||
enum TIM_DIV_ENUM {
|
||||
TIM_DIV1 = 0, //80MHz (80 ticks/us - 104857.588 us max)
|
||||
TIM_DIV16 = 1, //5MHz (5 ticks/us - 1677721.4 us max)
|
||||
TIM_DIV256 = 3 //312.5Khz (1 tick = 3.2us - 26843542.4 us max)
|
||||
};
|
||||
|
||||
|
||||
@ -296,7 +295,7 @@ long secureRandom(long, long);
|
||||
long map(long, long, long, long, long);
|
||||
|
||||
extern "C" void configTime(long timezone, int daylightOffset_sec,
|
||||
const char* server1, const char* server2 = nullptr, const char* server3 = nullptr);
|
||||
const char* server1, const char* server2 = nullptr, const char* server3 = nullptr);
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -1,21 +1,21 @@
|
||||
/*
|
||||
Client.h - Base class that provides Client
|
||||
Copyright (c) 2011 Adrian McEwen. All right reserved.
|
||||
Client.h - Base class that provides Client
|
||||
Copyright (c) 2011 Adrian McEwen. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef client_h
|
||||
#define client_h
|
||||
@ -23,32 +23,29 @@
|
||||
#include "Stream.h"
|
||||
#include "IPAddress.h"
|
||||
|
||||
class Client: public Stream
|
||||
{
|
||||
class Client: public Stream {
|
||||
|
||||
public:
|
||||
virtual int connect(IPAddress ip, uint16_t port) = 0;
|
||||
virtual int connect(const char *host, uint16_t port) = 0;
|
||||
virtual size_t write(uint8_t) = 0;
|
||||
virtual size_t write(const uint8_t *buf, size_t size) = 0;
|
||||
virtual int available() = 0;
|
||||
virtual int read() = 0;
|
||||
virtual int read(uint8_t *buf, size_t size) = 0;
|
||||
virtual int peek() = 0;
|
||||
virtual void flush() = 0;
|
||||
virtual void stop() = 0;
|
||||
virtual uint8_t connected() = 0;
|
||||
virtual operator bool() = 0;
|
||||
protected:
|
||||
uint8_t* rawIPAddress(IPAddress& addr)
|
||||
{
|
||||
return addr.raw_address();
|
||||
}
|
||||
public:
|
||||
virtual int connect(IPAddress ip, uint16_t port) =0;
|
||||
virtual int connect(const char *host, uint16_t port) =0;
|
||||
virtual size_t write(uint8_t) =0;
|
||||
virtual size_t write(const uint8_t *buf, size_t size) =0;
|
||||
virtual int available() = 0;
|
||||
virtual int read() = 0;
|
||||
virtual int read(uint8_t *buf, size_t size) = 0;
|
||||
virtual int peek() = 0;
|
||||
virtual void flush() = 0;
|
||||
virtual void stop() = 0;
|
||||
virtual uint8_t connected() = 0;
|
||||
virtual operator bool() = 0;
|
||||
protected:
|
||||
uint8_t* rawIPAddress(IPAddress& addr) {
|
||||
return addr.raw_address();
|
||||
}
|
||||
#if LWIP_VERSION_MAJOR != 1
|
||||
const uint8_t* rawIPAddress(const IPAddress& addr)
|
||||
{
|
||||
return addr.raw_address();
|
||||
}
|
||||
const uint8_t* rawIPAddress(const IPAddress& addr) {
|
||||
return addr.raw_address();
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
Esp.cpp - ESP8266-specific APIs
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Esp.cpp - ESP8266-specific APIs
|
||||
Copyright (c) 2015 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#include "umm_malloc/umm_malloc.h"
|
||||
#include "umm_malloc/umm_malloc_cfg.h"
|
||||
@ -33,17 +33,11 @@ void EspClass::getHeapStats(uint32_t* hfree, uint16_t* hmax, uint8_t* hfrag)
|
||||
uint8_t block_size = umm_block_size();
|
||||
uint32_t fh = ummHeapInfo.freeBlocks * block_size;
|
||||
if (hfree)
|
||||
{
|
||||
*hfree = fh;
|
||||
}
|
||||
if (hmax)
|
||||
{
|
||||
*hmax = ummHeapInfo.maxFreeContiguousBlocks * block_size;
|
||||
}
|
||||
if (hfrag)
|
||||
{
|
||||
*hfrag = 100 - (sqrt32(ummHeapInfo.freeSize2) * 100) / fh;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t EspClass::getHeapFragmentation()
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
Esp.cpp - ESP8266-specific APIs
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Esp.cpp - ESP8266-specific APIs
|
||||
Copyright (c) 2015 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <user_interface.h>
|
||||
@ -34,10 +34,10 @@ static const char bearssl_version [] PROGMEM = "/BearSSL:" STR(BEARSSL_GIT);
|
||||
String EspClass::getFullVersion()
|
||||
{
|
||||
return String(F("SDK:")) + system_get_sdk_version()
|
||||
+ F("/Core:") + FPSTR(arduino_esp8266_git_ver)
|
||||
+ F("=") + String(esp8266::coreVersionNumeric())
|
||||
+ F("/Core:") + FPSTR(arduino_esp8266_git_ver)
|
||||
+ F("=") + String(esp8266::coreVersionNumeric())
|
||||
#if LWIP_VERSION_MAJOR == 1
|
||||
+ F("/lwIP:") + String(LWIP_VERSION_MAJOR) + "." + String(LWIP_VERSION_MINOR) + "." + String(LWIP_VERSION_REVISION)
|
||||
+ F("/lwIP:") + String(LWIP_VERSION_MAJOR) + "." + String(LWIP_VERSION_MINOR) + "." + String(LWIP_VERSION_REVISION)
|
||||
#if LWIP_VERSION_IS_DEVELOPMENT
|
||||
+ F("-dev")
|
||||
#endif
|
||||
@ -52,5 +52,5 @@ String EspClass::getFullVersion()
|
||||
+ F(LWIP_HASH_STR)
|
||||
#endif // LWIP_VERSION_MAJOR != 1
|
||||
+ FPSTR(bearssl_version)
|
||||
;
|
||||
;
|
||||
}
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
Esp.cpp - ESP8266-specific APIs
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Esp.cpp - ESP8266-specific APIs
|
||||
Copyright (c) 2015 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "flash_utils.h"
|
||||
@ -30,7 +30,7 @@
|
||||
extern "C" {
|
||||
#include "user_interface.h"
|
||||
|
||||
extern struct rst_info resetInfo;
|
||||
extern struct rst_info resetInfo;
|
||||
}
|
||||
|
||||
|
||||
@ -38,54 +38,45 @@ extern "C" {
|
||||
|
||||
|
||||
/**
|
||||
User-defined Literals
|
||||
usage:
|
||||
* User-defined Literals
|
||||
* usage:
|
||||
*
|
||||
* uint32_t = test = 10_MHz; // --> 10000000
|
||||
*/
|
||||
|
||||
uint32_t = test = 10_MHz; // --> 10000000
|
||||
*/
|
||||
|
||||
unsigned long long operator"" _kHz(unsigned long long x)
|
||||
{
|
||||
unsigned long long operator"" _kHz(unsigned long long x) {
|
||||
return x * 1000;
|
||||
}
|
||||
|
||||
unsigned long long operator"" _MHz(unsigned long long x)
|
||||
{
|
||||
unsigned long long operator"" _MHz(unsigned long long x) {
|
||||
return x * 1000 * 1000;
|
||||
}
|
||||
|
||||
unsigned long long operator"" _GHz(unsigned long long x)
|
||||
{
|
||||
unsigned long long operator"" _GHz(unsigned long long x) {
|
||||
return x * 1000 * 1000 * 1000;
|
||||
}
|
||||
|
||||
unsigned long long operator"" _kBit(unsigned long long x)
|
||||
{
|
||||
unsigned long long operator"" _kBit(unsigned long long x) {
|
||||
return x * 1024;
|
||||
}
|
||||
|
||||
unsigned long long operator"" _MBit(unsigned long long x)
|
||||
{
|
||||
unsigned long long operator"" _MBit(unsigned long long x) {
|
||||
return x * 1024 * 1024;
|
||||
}
|
||||
|
||||
unsigned long long operator"" _GBit(unsigned long long x)
|
||||
{
|
||||
unsigned long long operator"" _GBit(unsigned long long x) {
|
||||
return x * 1024 * 1024 * 1024;
|
||||
}
|
||||
|
||||
unsigned long long operator"" _kB(unsigned long long x)
|
||||
{
|
||||
unsigned long long operator"" _kB(unsigned long long x) {
|
||||
return x * 1024;
|
||||
}
|
||||
|
||||
unsigned long long operator"" _MB(unsigned long long x)
|
||||
{
|
||||
unsigned long long operator"" _MB(unsigned long long x) {
|
||||
return x * 1024 * 1024;
|
||||
}
|
||||
|
||||
unsigned long long operator"" _GB(unsigned long long x)
|
||||
{
|
||||
unsigned long long operator"" _GB(unsigned long long x) {
|
||||
return x * 1024 * 1024 * 1024;
|
||||
}
|
||||
|
||||
@ -136,65 +127,59 @@ void EspClass::deepSleepInstant(uint64_t time_us, WakeMode mode)
|
||||
//Note: system_rtc_clock_cali_proc() returns a uint32_t, even though system_deep_sleep() takes a uint64_t.
|
||||
uint64_t EspClass::deepSleepMax()
|
||||
{
|
||||
//cali*(2^31-1)/(2^12)
|
||||
return (uint64_t)system_rtc_clock_cali_proc() * (0x80000000 - 1) / (0x1000);
|
||||
//cali*(2^31-1)/(2^12)
|
||||
return (uint64_t)system_rtc_clock_cali_proc()*(0x80000000-1)/(0x1000);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
Layout of RTC Memory is as follows:
|
||||
Ref: Espressif doc 2C-ESP8266_Non_OS_SDK_API_Reference, section 3.3.23 (system_rtc_mem_write)
|
||||
Layout of RTC Memory is as follows:
|
||||
Ref: Espressif doc 2C-ESP8266_Non_OS_SDK_API_Reference, section 3.3.23 (system_rtc_mem_write)
|
||||
|
||||
|<------system data (256 bytes)------->|<-----------------user data (512 bytes)--------------->|
|
||||
|<------system data (256 bytes)------->|<-----------------user data (512 bytes)--------------->|
|
||||
|
||||
SDK function signature:
|
||||
bool system_rtc_mem_read (
|
||||
SDK function signature:
|
||||
bool system_rtc_mem_read (
|
||||
uint32 des_addr,
|
||||
void * src_addr,
|
||||
uint32 save_size
|
||||
)
|
||||
)
|
||||
|
||||
The system data section can't be used by the user, so:
|
||||
des_addr must be >=64 (i.e.: 256/4) and <192 (i.e.: 768/4)
|
||||
src_addr is a pointer to data
|
||||
save_size is the number of bytes to write
|
||||
The system data section can't be used by the user, so:
|
||||
des_addr must be >=64 (i.e.: 256/4) and <192 (i.e.: 768/4)
|
||||
src_addr is a pointer to data
|
||||
save_size is the number of bytes to write
|
||||
|
||||
For the method interface:
|
||||
offset is the user block number (block size is 4 bytes) must be >= 0 and <128
|
||||
data is a pointer to data, 4-byte aligned
|
||||
size is number of bytes in the block pointed to by data
|
||||
For the method interface:
|
||||
offset is the user block number (block size is 4 bytes) must be >= 0 and <128
|
||||
data is a pointer to data, 4-byte aligned
|
||||
size is number of bytes in the block pointed to by data
|
||||
|
||||
Same for write
|
||||
Same for write
|
||||
|
||||
Note: If the Updater class is in play, e.g.: the application uses OTA, the eboot
|
||||
command will be stored into the first 128 bytes of user data, then it will be
|
||||
retrieved by eboot on boot. That means that user data present there will be lost.
|
||||
Ref:
|
||||
- discussion in PR #5330.
|
||||
- https://github.com/esp8266/esp8266-wiki/wiki/Memory-Map#memmory-mapped-io-registers
|
||||
- Arduino/bootloaders/eboot/eboot_command.h RTC_MEM definition
|
||||
Note: If the Updater class is in play, e.g.: the application uses OTA, the eboot
|
||||
command will be stored into the first 128 bytes of user data, then it will be
|
||||
retrieved by eboot on boot. That means that user data present there will be lost.
|
||||
Ref:
|
||||
- discussion in PR #5330.
|
||||
- https://github.com/esp8266/esp8266-wiki/wiki/Memory-Map#memmory-mapped-io-registers
|
||||
- Arduino/bootloaders/eboot/eboot_command.h RTC_MEM definition
|
||||
*/
|
||||
|
||||
bool EspClass::rtcUserMemoryRead(uint32_t offset, uint32_t *data, size_t size)
|
||||
{
|
||||
if (offset * 4 + size > 512 || size == 0)
|
||||
{
|
||||
if (offset * 4 + size > 512 || size == 0) {
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
return system_rtc_mem_read(64 + offset, data, size);
|
||||
}
|
||||
}
|
||||
|
||||
bool EspClass::rtcUserMemoryWrite(uint32_t offset, uint32_t *data, size_t size)
|
||||
{
|
||||
if (offset * 4 + size > 512 || size == 0)
|
||||
{
|
||||
if (offset * 4 + size > 512 || size == 0) {
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
return system_rtc_mem_write(64 + offset, data, size);
|
||||
}
|
||||
}
|
||||
@ -250,8 +235,7 @@ extern "C" const char* core_release;
|
||||
|
||||
String EspClass::getCoreVersion()
|
||||
{
|
||||
if (core_release != NULL)
|
||||
{
|
||||
if (core_release != NULL) {
|
||||
return String(core_release);
|
||||
}
|
||||
char buf[12];
|
||||
@ -283,8 +267,7 @@ uint8_t EspClass::getCpuFreqMHz(void)
|
||||
uint32_t EspClass::getFlashChipId(void)
|
||||
{
|
||||
static uint32_t flash_chip_id = 0;
|
||||
if (flash_chip_id == 0)
|
||||
{
|
||||
if (flash_chip_id == 0) {
|
||||
flash_chip_id = spi_flash_get_id();
|
||||
}
|
||||
return flash_chip_id;
|
||||
@ -305,8 +288,7 @@ uint32_t EspClass::getFlashChipSize(void)
|
||||
uint32_t data;
|
||||
uint8_t * bytes = (uint8_t *) &data;
|
||||
// read first 4 byte (magic byte + flash config)
|
||||
if (spi_flash_read(0x0000, &data, 4) == SPI_FLASH_RESULT_OK)
|
||||
{
|
||||
if(spi_flash_read(0x0000, &data, 4) == SPI_FLASH_RESULT_OK) {
|
||||
return magicFlashChipSize((bytes[3] & 0xf0) >> 4);
|
||||
}
|
||||
return 0;
|
||||
@ -317,8 +299,7 @@ uint32_t EspClass::getFlashChipSpeed(void)
|
||||
uint32_t data;
|
||||
uint8_t * bytes = (uint8_t *) &data;
|
||||
// read first 4 byte (magic byte + flash config)
|
||||
if (spi_flash_read(0x0000, &data, 4) == SPI_FLASH_RESULT_OK)
|
||||
{
|
||||
if(spi_flash_read(0x0000, &data, 4) == SPI_FLASH_RESULT_OK) {
|
||||
return magicFlashChipSpeed(bytes[3] & 0x0F);
|
||||
}
|
||||
return 0;
|
||||
@ -330,191 +311,158 @@ FlashMode_t EspClass::getFlashChipMode(void)
|
||||
uint32_t data;
|
||||
uint8_t * bytes = (uint8_t *) &data;
|
||||
// read first 4 byte (magic byte + flash config)
|
||||
if (spi_flash_read(0x0000, &data, 4) == SPI_FLASH_RESULT_OK)
|
||||
{
|
||||
if(spi_flash_read(0x0000, &data, 4) == SPI_FLASH_RESULT_OK) {
|
||||
mode = magicFlashChipMode(bytes[2]);
|
||||
}
|
||||
return mode;
|
||||
}
|
||||
|
||||
uint32_t EspClass::magicFlashChipSize(uint8_t byte)
|
||||
{
|
||||
switch (byte & 0x0F)
|
||||
{
|
||||
case 0x0: // 4 Mbit (512KB)
|
||||
return (512_kB);
|
||||
case 0x1: // 2 MBit (256KB)
|
||||
return (256_kB);
|
||||
case 0x2: // 8 MBit (1MB)
|
||||
return (1_MB);
|
||||
case 0x3: // 16 MBit (2MB)
|
||||
return (2_MB);
|
||||
case 0x4: // 32 MBit (4MB)
|
||||
return (4_MB);
|
||||
case 0x8: // 64 MBit (8MB)
|
||||
return (8_MB);
|
||||
case 0x9: // 128 MBit (16MB)
|
||||
return (16_MB);
|
||||
default: // fail?
|
||||
return 0;
|
||||
uint32_t EspClass::magicFlashChipSize(uint8_t byte) {
|
||||
switch(byte & 0x0F) {
|
||||
case 0x0: // 4 Mbit (512KB)
|
||||
return (512_kB);
|
||||
case 0x1: // 2 MBit (256KB)
|
||||
return (256_kB);
|
||||
case 0x2: // 8 MBit (1MB)
|
||||
return (1_MB);
|
||||
case 0x3: // 16 MBit (2MB)
|
||||
return (2_MB);
|
||||
case 0x4: // 32 MBit (4MB)
|
||||
return (4_MB);
|
||||
case 0x8: // 64 MBit (8MB)
|
||||
return (8_MB);
|
||||
case 0x9: // 128 MBit (16MB)
|
||||
return (16_MB);
|
||||
default: // fail?
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t EspClass::magicFlashChipSpeed(uint8_t byte)
|
||||
{
|
||||
switch (byte & 0x0F)
|
||||
{
|
||||
case 0x0: // 40 MHz
|
||||
return (40_MHz);
|
||||
case 0x1: // 26 MHz
|
||||
return (26_MHz);
|
||||
case 0x2: // 20 MHz
|
||||
return (20_MHz);
|
||||
case 0xf: // 80 MHz
|
||||
return (80_MHz);
|
||||
default: // fail?
|
||||
return 0;
|
||||
uint32_t EspClass::magicFlashChipSpeed(uint8_t byte) {
|
||||
switch(byte & 0x0F) {
|
||||
case 0x0: // 40 MHz
|
||||
return (40_MHz);
|
||||
case 0x1: // 26 MHz
|
||||
return (26_MHz);
|
||||
case 0x2: // 20 MHz
|
||||
return (20_MHz);
|
||||
case 0xf: // 80 MHz
|
||||
return (80_MHz);
|
||||
default: // fail?
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
FlashMode_t EspClass::magicFlashChipMode(uint8_t byte)
|
||||
{
|
||||
FlashMode_t EspClass::magicFlashChipMode(uint8_t byte) {
|
||||
FlashMode_t mode = (FlashMode_t) byte;
|
||||
if (mode > FM_DOUT)
|
||||
{
|
||||
if(mode > FM_DOUT) {
|
||||
mode = FM_UNKNOWN;
|
||||
}
|
||||
return mode;
|
||||
}
|
||||
|
||||
/**
|
||||
Infos from
|
||||
http://www.wlxmall.com/images/stock_item/att/A1010004.pdf
|
||||
http://www.gigadevice.com/product-series/5.html?locale=en_US
|
||||
http://www.elinux.org/images/f/f5/Winbond-w25q32.pdf
|
||||
*/
|
||||
uint32_t EspClass::getFlashChipSizeByChipId(void)
|
||||
{
|
||||
* Infos from
|
||||
* http://www.wlxmall.com/images/stock_item/att/A1010004.pdf
|
||||
* http://www.gigadevice.com/product-series/5.html?locale=en_US
|
||||
* http://www.elinux.org/images/f/f5/Winbond-w25q32.pdf
|
||||
*/
|
||||
uint32_t EspClass::getFlashChipSizeByChipId(void) {
|
||||
uint32_t chipId = getFlashChipId();
|
||||
/**
|
||||
Chip ID
|
||||
00 - always 00 (Chip ID use only 3 byte)
|
||||
17 - ? looks like 2^xx is size in Byte ? //todo: find docu to this
|
||||
40 - ? may be Speed ? //todo: find docu to this
|
||||
C8 - manufacturer ID
|
||||
*/
|
||||
switch (chipId)
|
||||
{
|
||||
* Chip ID
|
||||
* 00 - always 00 (Chip ID use only 3 byte)
|
||||
* 17 - ? looks like 2^xx is size in Byte ? //todo: find docu to this
|
||||
* 40 - ? may be Speed ? //todo: find docu to this
|
||||
* C8 - manufacturer ID
|
||||
*/
|
||||
switch(chipId) {
|
||||
|
||||
// GigaDevice
|
||||
case 0x1740C8: // GD25Q64B
|
||||
return (8_MB);
|
||||
case 0x1640C8: // GD25Q32B
|
||||
return (4_MB);
|
||||
case 0x1540C8: // GD25Q16B
|
||||
return (2_MB);
|
||||
case 0x1440C8: // GD25Q80
|
||||
return (1_MB);
|
||||
case 0x1340C8: // GD25Q40
|
||||
return (512_kB);
|
||||
case 0x1240C8: // GD25Q20
|
||||
return (256_kB);
|
||||
case 0x1140C8: // GD25Q10
|
||||
return (128_kB);
|
||||
case 0x1040C8: // GD25Q12
|
||||
return (64_kB);
|
||||
// GigaDevice
|
||||
case 0x1740C8: // GD25Q64B
|
||||
return (8_MB);
|
||||
case 0x1640C8: // GD25Q32B
|
||||
return (4_MB);
|
||||
case 0x1540C8: // GD25Q16B
|
||||
return (2_MB);
|
||||
case 0x1440C8: // GD25Q80
|
||||
return (1_MB);
|
||||
case 0x1340C8: // GD25Q40
|
||||
return (512_kB);
|
||||
case 0x1240C8: // GD25Q20
|
||||
return (256_kB);
|
||||
case 0x1140C8: // GD25Q10
|
||||
return (128_kB);
|
||||
case 0x1040C8: // GD25Q12
|
||||
return (64_kB);
|
||||
|
||||
// Winbond
|
||||
case 0x1640EF: // W25Q32
|
||||
return (4_MB);
|
||||
case 0x1540EF: // W25Q16
|
||||
return (2_MB);
|
||||
case 0x1440EF: // W25Q80
|
||||
return (1_MB);
|
||||
case 0x1340EF: // W25Q40
|
||||
return (512_kB);
|
||||
// Winbond
|
||||
case 0x1640EF: // W25Q32
|
||||
return (4_MB);
|
||||
case 0x1540EF: // W25Q16
|
||||
return (2_MB);
|
||||
case 0x1440EF: // W25Q80
|
||||
return (1_MB);
|
||||
case 0x1340EF: // W25Q40
|
||||
return (512_kB);
|
||||
|
||||
// BergMicro
|
||||
case 0x1640E0: // BG25Q32
|
||||
return (4_MB);
|
||||
case 0x1540E0: // BG25Q16
|
||||
return (2_MB);
|
||||
case 0x1440E0: // BG25Q80
|
||||
return (1_MB);
|
||||
case 0x1340E0: // BG25Q40
|
||||
return (512_kB);
|
||||
// BergMicro
|
||||
case 0x1640E0: // BG25Q32
|
||||
return (4_MB);
|
||||
case 0x1540E0: // BG25Q16
|
||||
return (2_MB);
|
||||
case 0x1440E0: // BG25Q80
|
||||
return (1_MB);
|
||||
case 0x1340E0: // BG25Q40
|
||||
return (512_kB);
|
||||
|
||||
default:
|
||||
return 0;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
check the Flash settings from IDE against the Real flash size
|
||||
@param needsEquals (return only true it equals)
|
||||
@return ok or not
|
||||
*/
|
||||
bool EspClass::checkFlashConfig(bool needsEquals)
|
||||
{
|
||||
if (needsEquals)
|
||||
{
|
||||
if (getFlashChipRealSize() == getFlashChipSize())
|
||||
{
|
||||
* check the Flash settings from IDE against the Real flash size
|
||||
* @param needsEquals (return only true it equals)
|
||||
* @return ok or not
|
||||
*/
|
||||
bool EspClass::checkFlashConfig(bool needsEquals) {
|
||||
if(needsEquals) {
|
||||
if(getFlashChipRealSize() == getFlashChipSize()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (getFlashChipRealSize() >= getFlashChipSize())
|
||||
{
|
||||
} else {
|
||||
if(getFlashChipRealSize() >= getFlashChipSize()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
String EspClass::getResetReason(void)
|
||||
{
|
||||
String EspClass::getResetReason(void) {
|
||||
char buff[32];
|
||||
if (resetInfo.reason == REASON_DEFAULT_RST) // normal startup by power on
|
||||
{
|
||||
strcpy_P(buff, PSTR("Power on"));
|
||||
}
|
||||
else if (resetInfo.reason == REASON_WDT_RST) // hardware watch dog reset
|
||||
{
|
||||
strcpy_P(buff, PSTR("Hardware Watchdog"));
|
||||
}
|
||||
else if (resetInfo.reason == REASON_EXCEPTION_RST) // exception reset, GPIO status won’t change
|
||||
{
|
||||
strcpy_P(buff, PSTR("Exception"));
|
||||
}
|
||||
else if (resetInfo.reason == REASON_SOFT_WDT_RST) // software watch dog reset, GPIO status won’t change
|
||||
{
|
||||
strcpy_P(buff, PSTR("Software Watchdog"));
|
||||
}
|
||||
else if (resetInfo.reason == REASON_SOFT_RESTART) // software restart ,system_restart , GPIO status won’t change
|
||||
{
|
||||
strcpy_P(buff, PSTR("Software/System restart"));
|
||||
}
|
||||
else if (resetInfo.reason == REASON_DEEP_SLEEP_AWAKE) // wake up from deep-sleep
|
||||
{
|
||||
strcpy_P(buff, PSTR("Deep-Sleep Wake"));
|
||||
}
|
||||
else if (resetInfo.reason == REASON_EXT_SYS_RST) // external system reset
|
||||
{
|
||||
strcpy_P(buff, PSTR("External System"));
|
||||
}
|
||||
else
|
||||
{
|
||||
strcpy_P(buff, PSTR("Unknown"));
|
||||
if (resetInfo.reason == REASON_DEFAULT_RST) { // normal startup by power on
|
||||
strcpy_P(buff, PSTR("Power on"));
|
||||
} else if (resetInfo.reason == REASON_WDT_RST) { // hardware watch dog reset
|
||||
strcpy_P(buff, PSTR("Hardware Watchdog"));
|
||||
} else if (resetInfo.reason == REASON_EXCEPTION_RST) { // exception reset, GPIO status won’t change
|
||||
strcpy_P(buff, PSTR("Exception"));
|
||||
} else if (resetInfo.reason == REASON_SOFT_WDT_RST) { // software watch dog reset, GPIO status won’t change
|
||||
strcpy_P(buff, PSTR("Software Watchdog"));
|
||||
} else if (resetInfo.reason == REASON_SOFT_RESTART) { // software restart ,system_restart , GPIO status won’t change
|
||||
strcpy_P(buff, PSTR("Software/System restart"));
|
||||
} else if (resetInfo.reason == REASON_DEEP_SLEEP_AWAKE) { // wake up from deep-sleep
|
||||
strcpy_P(buff, PSTR("Deep-Sleep Wake"));
|
||||
} else if (resetInfo.reason == REASON_EXT_SYS_RST) { // external system reset
|
||||
strcpy_P(buff, PSTR("External System"));
|
||||
} else {
|
||||
strcpy_P(buff, PSTR("Unknown"));
|
||||
}
|
||||
return String(buff);
|
||||
}
|
||||
|
||||
String EspClass::getResetInfo(void)
|
||||
{
|
||||
if (resetInfo.reason != 0)
|
||||
{
|
||||
String EspClass::getResetInfo(void) {
|
||||
if(resetInfo.reason != 0) {
|
||||
char buff[200];
|
||||
sprintf(&buff[0], "Fatal exception:%d flag:%d (%s) epc1:0x%08x epc2:0x%08x epc3:0x%08x excvaddr:0x%08x depc:0x%08x", resetInfo.exccause, resetInfo.reason, (resetInfo.reason == 0 ? "DEFAULT" : resetInfo.reason == 1 ? "WDT" : resetInfo.reason == 2 ? "EXCEPTION" : resetInfo.reason == 3 ? "SOFT_WDT" : resetInfo.reason == 4 ? "SOFT_RESTART" : resetInfo.reason == 5 ? "DEEP_SLEEP_AWAKE" : resetInfo.reason == 6 ? "EXT_SYS_RST" : "???"), resetInfo.epc1, resetInfo.epc2, resetInfo.epc3, resetInfo.excvaddr, resetInfo.depc);
|
||||
return String(buff);
|
||||
@ -522,20 +470,16 @@ String EspClass::getResetInfo(void)
|
||||
return String("flag: 0");
|
||||
}
|
||||
|
||||
struct rst_info * EspClass::getResetInfoPtr(void)
|
||||
{
|
||||
struct rst_info * EspClass::getResetInfoPtr(void) {
|
||||
return &resetInfo;
|
||||
}
|
||||
|
||||
bool EspClass::eraseConfig(void)
|
||||
{
|
||||
bool EspClass::eraseConfig(void) {
|
||||
const size_t cfgSize = 0x4000;
|
||||
size_t cfgAddr = ESP.getFlashChipSize() - cfgSize;
|
||||
|
||||
for (size_t offset = 0; offset < cfgSize; offset += SPI_FLASH_SEC_SIZE)
|
||||
{
|
||||
if (!flashEraseSector((cfgAddr + offset) / SPI_FLASH_SEC_SIZE))
|
||||
{
|
||||
for (size_t offset = 0; offset < cfgSize; offset += SPI_FLASH_SEC_SIZE) {
|
||||
if (!flashEraseSector((cfgAddr + offset) / SPI_FLASH_SEC_SIZE)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -543,18 +487,14 @@ bool EspClass::eraseConfig(void)
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t EspClass::getSketchSize()
|
||||
{
|
||||
uint32_t EspClass::getSketchSize() {
|
||||
static uint32_t result = 0;
|
||||
if (result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
image_header_t image_header;
|
||||
uint32_t pos = APP_START_OFFSET;
|
||||
if (spi_flash_read(pos, (uint32_t*) &image_header, sizeof(image_header)))
|
||||
{
|
||||
if (spi_flash_read(pos, (uint32_t*) &image_header, sizeof(image_header))) {
|
||||
return 0;
|
||||
}
|
||||
pos += sizeof(image_header);
|
||||
@ -562,12 +502,11 @@ uint32_t EspClass::getSketchSize()
|
||||
DEBUG_SERIAL.printf("num_segments=%u\r\n", image_header.num_segments);
|
||||
#endif
|
||||
for (uint32_t section_index = 0;
|
||||
section_index < image_header.num_segments;
|
||||
++section_index)
|
||||
section_index < image_header.num_segments;
|
||||
++section_index)
|
||||
{
|
||||
section_header_t section_header = {0, 0};
|
||||
if (spi_flash_read(pos, (uint32_t*) §ion_header, sizeof(section_header)))
|
||||
{
|
||||
if (spi_flash_read(pos, (uint32_t*) §ion_header, sizeof(section_header))) {
|
||||
return 0;
|
||||
}
|
||||
pos += sizeof(section_header);
|
||||
@ -582,8 +521,7 @@ uint32_t EspClass::getSketchSize()
|
||||
|
||||
extern "C" uint32_t _SPIFFS_start;
|
||||
|
||||
uint32_t EspClass::getFreeSketchSpace()
|
||||
{
|
||||
uint32_t EspClass::getFreeSketchSpace() {
|
||||
|
||||
uint32_t usedSize = getSketchSize();
|
||||
// round one sector up
|
||||
@ -596,108 +534,81 @@ uint32_t EspClass::getFreeSketchSpace()
|
||||
return freeSpaceEnd - freeSpaceStart;
|
||||
}
|
||||
|
||||
bool EspClass::updateSketch(Stream& in, uint32_t size, bool restartOnFail, bool restartOnSuccess)
|
||||
{
|
||||
if (!Update.begin(size))
|
||||
{
|
||||
bool EspClass::updateSketch(Stream& in, uint32_t size, bool restartOnFail, bool restartOnSuccess) {
|
||||
if(!Update.begin(size)){
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.print("Update ");
|
||||
Update.printError(DEBUG_SERIAL);
|
||||
DEBUG_SERIAL.print("Update ");
|
||||
Update.printError(DEBUG_SERIAL);
|
||||
#endif
|
||||
if (restartOnFail)
|
||||
{
|
||||
ESP.restart();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if(restartOnFail) ESP.restart();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Update.writeStream(in) != size)
|
||||
{
|
||||
if(Update.writeStream(in) != size){
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.print("Update ");
|
||||
Update.printError(DEBUG_SERIAL);
|
||||
DEBUG_SERIAL.print("Update ");
|
||||
Update.printError(DEBUG_SERIAL);
|
||||
#endif
|
||||
if (restartOnFail)
|
||||
{
|
||||
ESP.restart();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if(restartOnFail) ESP.restart();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Update.end())
|
||||
{
|
||||
if(!Update.end()){
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.print("Update ");
|
||||
Update.printError(DEBUG_SERIAL);
|
||||
DEBUG_SERIAL.print("Update ");
|
||||
Update.printError(DEBUG_SERIAL);
|
||||
#endif
|
||||
if (restartOnFail)
|
||||
{
|
||||
ESP.restart();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if(restartOnFail) ESP.restart();
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.println("Update SUCCESS");
|
||||
#endif
|
||||
if (restartOnSuccess)
|
||||
{
|
||||
ESP.restart();
|
||||
}
|
||||
if(restartOnSuccess) ESP.restart();
|
||||
return true;
|
||||
}
|
||||
|
||||
static const int FLASH_INT_MASK = ((B10 << 8) | B00111010);
|
||||
|
||||
bool EspClass::flashEraseSector(uint32_t sector)
|
||||
{
|
||||
bool EspClass::flashEraseSector(uint32_t sector) {
|
||||
int rc = spi_flash_erase_sector(sector);
|
||||
return rc == 0;
|
||||
}
|
||||
|
||||
#if PUYA_SUPPORT
|
||||
static int spi_flash_write_puya(uint32_t offset, uint32_t *data, size_t size)
|
||||
{
|
||||
if (data == nullptr)
|
||||
{
|
||||
return 1; // SPI_FLASH_RESULT_ERR
|
||||
static int spi_flash_write_puya(uint32_t offset, uint32_t *data, size_t size) {
|
||||
if (data == nullptr) {
|
||||
return 1; // SPI_FLASH_RESULT_ERR
|
||||
}
|
||||
// PUYA flash chips need to read existing data, update in memory and write modified data again.
|
||||
static uint32_t *flash_write_puya_buf = nullptr;
|
||||
int rc = 0;
|
||||
uint32_t* ptr = data;
|
||||
|
||||
if (flash_write_puya_buf == nullptr)
|
||||
{
|
||||
if (flash_write_puya_buf == nullptr) {
|
||||
flash_write_puya_buf = (uint32_t*) malloc(PUYA_BUFFER_SIZE);
|
||||
// No need to ever free this, since the flash chip will never change at runtime.
|
||||
if (flash_write_puya_buf == nullptr)
|
||||
{
|
||||
if (flash_write_puya_buf == nullptr) {
|
||||
// Memory could not be allocated.
|
||||
return 1; // SPI_FLASH_RESULT_ERR
|
||||
}
|
||||
}
|
||||
size_t bytesLeft = size;
|
||||
uint32_t pos = offset;
|
||||
while (bytesLeft > 0 && rc == 0)
|
||||
{
|
||||
while (bytesLeft > 0 && rc == 0) {
|
||||
size_t bytesNow = bytesLeft;
|
||||
if (bytesNow > PUYA_BUFFER_SIZE)
|
||||
{
|
||||
if (bytesNow > PUYA_BUFFER_SIZE) {
|
||||
bytesNow = PUYA_BUFFER_SIZE;
|
||||
bytesLeft -= PUYA_BUFFER_SIZE;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
bytesLeft = 0;
|
||||
}
|
||||
rc = spi_flash_read(pos, flash_write_puya_buf, bytesNow);
|
||||
if (rc != 0)
|
||||
{
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
for (size_t i = 0; i < bytesNow / 4; ++i)
|
||||
{
|
||||
for (size_t i = 0; i < bytesNow / 4; ++i) {
|
||||
flash_write_puya_buf[i] &= *ptr;
|
||||
++ptr;
|
||||
}
|
||||
@ -708,12 +619,10 @@ static int spi_flash_write_puya(uint32_t offset, uint32_t *data, size_t size)
|
||||
}
|
||||
#endif
|
||||
|
||||
bool EspClass::flashWrite(uint32_t offset, uint32_t *data, size_t size)
|
||||
{
|
||||
bool EspClass::flashWrite(uint32_t offset, uint32_t *data, size_t size) {
|
||||
int rc = 0;
|
||||
#if PUYA_SUPPORT
|
||||
if (getFlashChipVendorId() == SPI_FLASH_VENDOR_PUYA)
|
||||
{
|
||||
if (getFlashChipVendorId() == SPI_FLASH_VENDOR_PUYA) {
|
||||
rc = spi_flash_write_puya(offset, data, size);
|
||||
}
|
||||
else
|
||||
@ -724,8 +633,7 @@ bool EspClass::flashWrite(uint32_t offset, uint32_t *data, size_t size)
|
||||
return rc == 0;
|
||||
}
|
||||
|
||||
bool EspClass::flashRead(uint32_t offset, uint32_t *data, size_t size)
|
||||
{
|
||||
bool EspClass::flashRead(uint32_t offset, uint32_t *data, size_t size) {
|
||||
int rc = spi_flash_read(offset, (uint32_t*) data, size);
|
||||
return rc == 0;
|
||||
}
|
||||
@ -733,25 +641,21 @@ bool EspClass::flashRead(uint32_t offset, uint32_t *data, size_t size)
|
||||
String EspClass::getSketchMD5()
|
||||
{
|
||||
static String result;
|
||||
if (result.length())
|
||||
{
|
||||
if (result.length()) {
|
||||
return result;
|
||||
}
|
||||
uint32_t lengthLeft = getSketchSize();
|
||||
const size_t bufSize = 512;
|
||||
std::unique_ptr<uint8_t[]> buf(new uint8_t[bufSize]);
|
||||
uint32_t offset = 0;
|
||||
if (!buf.get())
|
||||
{
|
||||
if(!buf.get()) {
|
||||
return String();
|
||||
}
|
||||
MD5Builder md5;
|
||||
md5.begin();
|
||||
while (lengthLeft > 0)
|
||||
{
|
||||
while( lengthLeft > 0) {
|
||||
size_t readBytes = (lengthLeft < bufSize) ? lengthLeft : bufSize;
|
||||
if (!flashRead(offset, reinterpret_cast<uint32_t*>(buf.get()), (readBytes + 3) & ~3))
|
||||
{
|
||||
if (!flashRead(offset, reinterpret_cast<uint32_t*>(buf.get()), (readBytes + 3) & ~3)) {
|
||||
return String();
|
||||
}
|
||||
md5.add(buf.get(), readBytes);
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
Esp.h - ESP8266-specific APIs
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Esp.h - ESP8266-specific APIs
|
||||
Copyright (c) 2015 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef ESP_H
|
||||
#define ESP_H
|
||||
@ -24,18 +24,17 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#ifndef PUYA_SUPPORT
|
||||
#define PUYA_SUPPORT 0
|
||||
#define PUYA_SUPPORT 0
|
||||
#endif
|
||||
#ifndef PUYA_BUFFER_SIZE
|
||||
// Good alternative for buffer size is: SPI_FLASH_SEC_SIZE (= 4k)
|
||||
// Always use a multiple of flash page size (256 bytes)
|
||||
#define PUYA_BUFFER_SIZE 256
|
||||
// Good alternative for buffer size is: SPI_FLASH_SEC_SIZE (= 4k)
|
||||
// Always use a multiple of flash page size (256 bytes)
|
||||
#define PUYA_BUFFER_SIZE 256
|
||||
#endif
|
||||
|
||||
// Vendor IDs taken from Flashrom project
|
||||
// https://review.coreboot.org/cgit/flashrom.git/tree/flashchips.h?h=1.0.x
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
SPI_FLASH_VENDOR_ALLIANCE = 0x52, /* Alliance Semiconductor */
|
||||
SPI_FLASH_VENDOR_AMD = 0x01, /* AMD */
|
||||
SPI_FLASH_VENDOR_AMIC = 0x37, /* AMIC */
|
||||
@ -71,10 +70,9 @@ typedef enum
|
||||
} SPI_FLASH_VENDOR_t;
|
||||
|
||||
/**
|
||||
AVR macros for WDT managment
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
* AVR macros for WDT managment
|
||||
*/
|
||||
typedef enum {
|
||||
WDTO_0MS = 0, //!< WDTO_0MS
|
||||
WDTO_15MS = 15, //!< WDTO_15MS
|
||||
WDTO_30MS = 30, //!< WDTO_30MS
|
||||
@ -96,8 +94,7 @@ typedef enum
|
||||
#define cli() ets_intr_lock() // IRQ Disable
|
||||
#define sei() ets_intr_unlock() // IRQ Enable
|
||||
|
||||
enum RFMode
|
||||
{
|
||||
enum RFMode {
|
||||
RF_DEFAULT = 0, // RF_CAL or not after deep-sleep wake up, depends on init data byte 108.
|
||||
RF_CAL = 1, // RF_CAL after deep-sleep wake up, there will be large current.
|
||||
RF_NO_CAL = 2, // no RF_CAL after deep-sleep wake up, there will only be small current.
|
||||
@ -114,8 +111,7 @@ enum RFMode
|
||||
#define WAKE_NO_RFCAL RF_NO_CAL
|
||||
#define WAKE_RF_DISABLED RF_DISABLED
|
||||
|
||||
enum ADCMode
|
||||
{
|
||||
enum ADCMode {
|
||||
ADC_TOUT = 33,
|
||||
ADC_TOUT_3V3 = 33,
|
||||
ADC_VCC = 255,
|
||||
@ -124,99 +120,97 @@ enum ADCMode
|
||||
|
||||
#define ADC_MODE(mode) int __get_adc_mode(void) { return (int) (mode); }
|
||||
|
||||
typedef enum
|
||||
{
|
||||
FM_QIO = 0x00,
|
||||
FM_QOUT = 0x01,
|
||||
FM_DIO = 0x02,
|
||||
FM_DOUT = 0x03,
|
||||
FM_UNKNOWN = 0xff
|
||||
typedef enum {
|
||||
FM_QIO = 0x00,
|
||||
FM_QOUT = 0x01,
|
||||
FM_DIO = 0x02,
|
||||
FM_DOUT = 0x03,
|
||||
FM_UNKNOWN = 0xff
|
||||
} FlashMode_t;
|
||||
|
||||
class EspClass
|
||||
{
|
||||
public:
|
||||
// TODO: figure out how to set WDT timeout
|
||||
void wdtEnable(uint32_t timeout_ms = 0);
|
||||
// note: setting the timeout value is not implemented at the moment
|
||||
void wdtEnable(WDTO_t timeout_ms = WDTO_0MS);
|
||||
class EspClass {
|
||||
public:
|
||||
// TODO: figure out how to set WDT timeout
|
||||
void wdtEnable(uint32_t timeout_ms = 0);
|
||||
// note: setting the timeout value is not implemented at the moment
|
||||
void wdtEnable(WDTO_t timeout_ms = WDTO_0MS);
|
||||
|
||||
void wdtDisable();
|
||||
void wdtFeed();
|
||||
void wdtDisable();
|
||||
void wdtFeed();
|
||||
|
||||
void deepSleep(uint64_t time_us, RFMode mode = RF_DEFAULT);
|
||||
void deepSleepInstant(uint64_t time_us, RFMode mode = RF_DEFAULT);
|
||||
uint64_t deepSleepMax();
|
||||
void deepSleep(uint64_t time_us, RFMode mode = RF_DEFAULT);
|
||||
void deepSleepInstant(uint64_t time_us, RFMode mode = RF_DEFAULT);
|
||||
uint64_t deepSleepMax();
|
||||
|
||||
bool rtcUserMemoryRead(uint32_t offset, uint32_t *data, size_t size);
|
||||
bool rtcUserMemoryWrite(uint32_t offset, uint32_t *data, size_t size);
|
||||
bool rtcUserMemoryRead(uint32_t offset, uint32_t *data, size_t size);
|
||||
bool rtcUserMemoryWrite(uint32_t offset, uint32_t *data, size_t size);
|
||||
|
||||
void reset();
|
||||
void restart();
|
||||
void reset();
|
||||
void restart();
|
||||
|
||||
uint16_t getVcc();
|
||||
uint32_t getChipId();
|
||||
uint16_t getVcc();
|
||||
uint32_t getChipId();
|
||||
|
||||
uint32_t getFreeHeap();
|
||||
uint16_t getMaxFreeBlockSize();
|
||||
uint8_t getHeapFragmentation(); // in %
|
||||
void getHeapStats(uint32_t* free = nullptr, uint16_t* max = nullptr, uint8_t* frag = nullptr);
|
||||
uint32_t getFreeHeap();
|
||||
uint16_t getMaxFreeBlockSize();
|
||||
uint8_t getHeapFragmentation(); // in %
|
||||
void getHeapStats(uint32_t* free = nullptr, uint16_t* max = nullptr, uint8_t* frag = nullptr);
|
||||
|
||||
uint32_t getFreeContStack();
|
||||
void resetFreeContStack();
|
||||
uint32_t getFreeContStack();
|
||||
void resetFreeContStack();
|
||||
|
||||
const char * getSdkVersion();
|
||||
String getCoreVersion();
|
||||
String getFullVersion();
|
||||
const char * getSdkVersion();
|
||||
String getCoreVersion();
|
||||
String getFullVersion();
|
||||
|
||||
uint8_t getBootVersion();
|
||||
uint8_t getBootMode();
|
||||
uint8_t getBootVersion();
|
||||
uint8_t getBootMode();
|
||||
|
||||
uint8_t getCpuFreqMHz();
|
||||
uint8_t getCpuFreqMHz();
|
||||
|
||||
uint32_t getFlashChipId();
|
||||
uint8_t getFlashChipVendorId();
|
||||
uint32_t getFlashChipId();
|
||||
uint8_t getFlashChipVendorId();
|
||||
|
||||
//gets the actual chip size based on the flash id
|
||||
uint32_t getFlashChipRealSize();
|
||||
//gets the size of the flash as set by the compiler
|
||||
uint32_t getFlashChipSize();
|
||||
uint32_t getFlashChipSpeed();
|
||||
FlashMode_t getFlashChipMode();
|
||||
uint32_t getFlashChipSizeByChipId();
|
||||
//gets the actual chip size based on the flash id
|
||||
uint32_t getFlashChipRealSize();
|
||||
//gets the size of the flash as set by the compiler
|
||||
uint32_t getFlashChipSize();
|
||||
uint32_t getFlashChipSpeed();
|
||||
FlashMode_t getFlashChipMode();
|
||||
uint32_t getFlashChipSizeByChipId();
|
||||
|
||||
uint32_t magicFlashChipSize(uint8_t byte);
|
||||
uint32_t magicFlashChipSpeed(uint8_t byte);
|
||||
FlashMode_t magicFlashChipMode(uint8_t byte);
|
||||
uint32_t magicFlashChipSize(uint8_t byte);
|
||||
uint32_t magicFlashChipSpeed(uint8_t byte);
|
||||
FlashMode_t magicFlashChipMode(uint8_t byte);
|
||||
|
||||
bool checkFlashConfig(bool needsEquals = false);
|
||||
bool checkFlashConfig(bool needsEquals = false);
|
||||
|
||||
bool flashEraseSector(uint32_t sector);
|
||||
bool flashWrite(uint32_t offset, uint32_t *data, size_t size);
|
||||
bool flashRead(uint32_t offset, uint32_t *data, size_t size);
|
||||
bool flashEraseSector(uint32_t sector);
|
||||
bool flashWrite(uint32_t offset, uint32_t *data, size_t size);
|
||||
bool flashRead(uint32_t offset, uint32_t *data, size_t size);
|
||||
|
||||
uint32_t getSketchSize();
|
||||
String getSketchMD5();
|
||||
uint32_t getFreeSketchSpace();
|
||||
bool updateSketch(Stream& in, uint32_t size, bool restartOnFail = false, bool restartOnSuccess = true);
|
||||
uint32_t getSketchSize();
|
||||
String getSketchMD5();
|
||||
uint32_t getFreeSketchSpace();
|
||||
bool updateSketch(Stream& in, uint32_t size, bool restartOnFail = false, bool restartOnSuccess = true);
|
||||
|
||||
String getResetReason();
|
||||
String getResetInfo();
|
||||
struct rst_info * getResetInfoPtr();
|
||||
String getResetReason();
|
||||
String getResetInfo();
|
||||
struct rst_info * getResetInfoPtr();
|
||||
|
||||
bool eraseConfig();
|
||||
bool eraseConfig();
|
||||
|
||||
#ifndef CORE_MOCK
|
||||
inline
|
||||
inline
|
||||
#endif
|
||||
uint32_t getCycleCount();
|
||||
uint32_t getCycleCount();
|
||||
};
|
||||
|
||||
#ifndef CORE_MOCK
|
||||
uint32_t EspClass::getCycleCount()
|
||||
{
|
||||
uint32_t ccount;
|
||||
__asm__ __volatile__("esync; rsr %0,ccount":"=a"(ccount));
|
||||
__asm__ __volatile__("esync; rsr %0,ccount":"=a" (ccount));
|
||||
return ccount;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
FS.cpp - file system wrapper
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
FS.cpp - file system wrapper
|
||||
Copyright (c) 2015 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#include "FS.h"
|
||||
#include "FSImpl.h"
|
||||
@ -25,68 +25,49 @@ using namespace fs;
|
||||
|
||||
static bool sflags(const char* mode, OpenMode& om, AccessMode& am);
|
||||
|
||||
size_t File::write(uint8_t c)
|
||||
{
|
||||
size_t File::write(uint8_t c) {
|
||||
if (!_p)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _p->write(&c, 1);
|
||||
}
|
||||
|
||||
size_t File::write(const uint8_t *buf, size_t size)
|
||||
{
|
||||
size_t File::write(const uint8_t *buf, size_t size) {
|
||||
if (!_p)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _p->write(buf, size);
|
||||
}
|
||||
|
||||
int File::available()
|
||||
{
|
||||
int File::available() {
|
||||
if (!_p)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _p->size() - _p->position();
|
||||
}
|
||||
|
||||
int File::read()
|
||||
{
|
||||
int File::read() {
|
||||
if (!_p)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint8_t result;
|
||||
if (_p->read(&result, 1) != 1)
|
||||
{
|
||||
if (_p->read(&result, 1) != 1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t File::read(uint8_t* buf, size_t size)
|
||||
{
|
||||
size_t File::read(uint8_t* buf, size_t size) {
|
||||
if (!_p)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return _p->read(buf, size);
|
||||
}
|
||||
|
||||
int File::peek()
|
||||
{
|
||||
int File::peek() {
|
||||
if (!_p)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t curPos = _p->position();
|
||||
int result = read();
|
||||
@ -94,126 +75,90 @@ int File::peek()
|
||||
return result;
|
||||
}
|
||||
|
||||
void File::flush()
|
||||
{
|
||||
void File::flush() {
|
||||
if (!_p)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_p->flush();
|
||||
}
|
||||
|
||||
bool File::seek(uint32_t pos, SeekMode mode)
|
||||
{
|
||||
bool File::seek(uint32_t pos, SeekMode mode) {
|
||||
if (!_p)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _p->seek(pos, mode);
|
||||
}
|
||||
|
||||
size_t File::position() const
|
||||
{
|
||||
size_t File::position() const {
|
||||
if (!_p)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _p->position();
|
||||
}
|
||||
|
||||
size_t File::size() const
|
||||
{
|
||||
size_t File::size() const {
|
||||
if (!_p)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _p->size();
|
||||
}
|
||||
|
||||
void File::close()
|
||||
{
|
||||
if (_p)
|
||||
{
|
||||
void File::close() {
|
||||
if (_p) {
|
||||
_p->close();
|
||||
_p = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
File::operator bool() const
|
||||
{
|
||||
File::operator bool() const {
|
||||
return !!_p;
|
||||
}
|
||||
|
||||
bool File::truncate(uint32_t size)
|
||||
{
|
||||
bool File::truncate(uint32_t size) {
|
||||
if (!_p)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _p->truncate(size);
|
||||
}
|
||||
|
||||
const char* File::name() const
|
||||
{
|
||||
const char* File::name() const {
|
||||
if (!_p)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return _p->name();
|
||||
}
|
||||
|
||||
const char* File::fullName() const
|
||||
{
|
||||
const char* File::fullName() const {
|
||||
if (!_p)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return _p->fullName();
|
||||
}
|
||||
|
||||
bool File::isFile() const
|
||||
{
|
||||
bool File::isFile() const {
|
||||
if (!_p)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _p->isFile();
|
||||
}
|
||||
|
||||
bool File::isDirectory() const
|
||||
{
|
||||
bool File::isDirectory() const {
|
||||
if (!_p)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _p->isDirectory();
|
||||
}
|
||||
|
||||
void File::rewindDirectory()
|
||||
{
|
||||
if (!_fakeDir)
|
||||
{
|
||||
void File::rewindDirectory() {
|
||||
if (!_fakeDir) {
|
||||
_fakeDir = std::make_shared<Dir>(_baseFS->openDir(fullName()));
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
_fakeDir->rewind();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
File File::openNextFile()
|
||||
{
|
||||
if (!_fakeDir)
|
||||
{
|
||||
File File::openNextFile() {
|
||||
if (!_fakeDir) {
|
||||
_fakeDir = std::make_shared<Dir>(_baseFS->openDir(fullName()));
|
||||
}
|
||||
_fakeDir->next();
|
||||
@ -224,28 +169,25 @@ String File::readString()
|
||||
{
|
||||
String ret;
|
||||
ret.reserve(size() - position());
|
||||
char temp[256 + 1];
|
||||
int countRead = readBytes(temp, sizeof(temp) - 1);
|
||||
char temp[256+1];
|
||||
int countRead = readBytes(temp, sizeof(temp)-1);
|
||||
while (countRead > 0)
|
||||
{
|
||||
temp[countRead] = 0;
|
||||
ret += temp;
|
||||
countRead = readBytes(temp, sizeof(temp) - 1);
|
||||
countRead = readBytes(temp, sizeof(temp)-1);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
File Dir::openFile(const char* mode)
|
||||
{
|
||||
if (!_impl)
|
||||
{
|
||||
File Dir::openFile(const char* mode) {
|
||||
if (!_impl) {
|
||||
return File();
|
||||
}
|
||||
|
||||
OpenMode om;
|
||||
AccessMode am;
|
||||
if (!sflags(mode, om, am))
|
||||
{
|
||||
if (!sflags(mode, om, am)) {
|
||||
DEBUGV("Dir::openFile: invalid mode `%s`\r\n", mode);
|
||||
return File();
|
||||
}
|
||||
@ -253,257 +195,206 @@ File Dir::openFile(const char* mode)
|
||||
return File(_impl->openFile(om, am), _baseFS);
|
||||
}
|
||||
|
||||
String Dir::fileName()
|
||||
{
|
||||
if (!_impl)
|
||||
{
|
||||
String Dir::fileName() {
|
||||
if (!_impl) {
|
||||
return String();
|
||||
}
|
||||
|
||||
return _impl->fileName();
|
||||
}
|
||||
|
||||
size_t Dir::fileSize()
|
||||
{
|
||||
if (!_impl)
|
||||
{
|
||||
size_t Dir::fileSize() {
|
||||
if (!_impl) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _impl->fileSize();
|
||||
}
|
||||
|
||||
bool Dir::isFile() const
|
||||
{
|
||||
bool Dir::isFile() const {
|
||||
if (!_impl)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _impl->isFile();
|
||||
}
|
||||
|
||||
bool Dir::isDirectory() const
|
||||
{
|
||||
bool Dir::isDirectory() const {
|
||||
if (!_impl)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _impl->isDirectory();
|
||||
}
|
||||
|
||||
bool Dir::next()
|
||||
{
|
||||
if (!_impl)
|
||||
{
|
||||
bool Dir::next() {
|
||||
if (!_impl) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return _impl->next();
|
||||
}
|
||||
|
||||
bool Dir::rewind()
|
||||
{
|
||||
if (!_impl)
|
||||
{
|
||||
bool Dir::rewind() {
|
||||
if (!_impl) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return _impl->rewind();
|
||||
}
|
||||
|
||||
bool FS::setConfig(const FSConfig &cfg)
|
||||
{
|
||||
if (!_impl)
|
||||
{
|
||||
bool FS::setConfig(const FSConfig &cfg) {
|
||||
if (!_impl) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return _impl->setConfig(cfg);
|
||||
}
|
||||
|
||||
bool FS::begin()
|
||||
{
|
||||
if (!_impl)
|
||||
{
|
||||
bool FS::begin() {
|
||||
if (!_impl) {
|
||||
return false;
|
||||
}
|
||||
return _impl->begin();
|
||||
}
|
||||
|
||||
void FS::end()
|
||||
{
|
||||
if (_impl)
|
||||
{
|
||||
void FS::end() {
|
||||
if (_impl) {
|
||||
_impl->end();
|
||||
}
|
||||
}
|
||||
|
||||
bool FS::gc()
|
||||
{
|
||||
if (!_impl)
|
||||
{
|
||||
bool FS::gc() {
|
||||
if (!_impl) {
|
||||
return false;
|
||||
}
|
||||
return _impl->gc();
|
||||
}
|
||||
|
||||
bool FS::format()
|
||||
{
|
||||
if (!_impl)
|
||||
{
|
||||
bool FS::format() {
|
||||
if (!_impl) {
|
||||
return false;
|
||||
}
|
||||
return _impl->format();
|
||||
}
|
||||
|
||||
bool FS::info(FSInfo& info)
|
||||
{
|
||||
if (!_impl)
|
||||
{
|
||||
bool FS::info(FSInfo& info){
|
||||
if (!_impl) {
|
||||
return false;
|
||||
}
|
||||
return _impl->info(info);
|
||||
}
|
||||
|
||||
File FS::open(const String& path, const char* mode)
|
||||
{
|
||||
File FS::open(const String& path, const char* mode) {
|
||||
return open(path.c_str(), mode);
|
||||
}
|
||||
|
||||
File FS::open(const char* path, const char* mode)
|
||||
{
|
||||
if (!_impl)
|
||||
{
|
||||
File FS::open(const char* path, const char* mode) {
|
||||
if (!_impl) {
|
||||
return File();
|
||||
}
|
||||
|
||||
OpenMode om;
|
||||
AccessMode am;
|
||||
if (!sflags(mode, om, am))
|
||||
{
|
||||
if (!sflags(mode, om, am)) {
|
||||
DEBUGV("FS::open: invalid mode `%s`\r\n", mode);
|
||||
return File();
|
||||
}
|
||||
return File(_impl->open(path, om, am), this);
|
||||
}
|
||||
|
||||
bool FS::exists(const char* path)
|
||||
{
|
||||
if (!_impl)
|
||||
{
|
||||
bool FS::exists(const char* path) {
|
||||
if (!_impl) {
|
||||
return false;
|
||||
}
|
||||
return _impl->exists(path);
|
||||
}
|
||||
|
||||
bool FS::exists(const String& path)
|
||||
{
|
||||
bool FS::exists(const String& path) {
|
||||
return exists(path.c_str());
|
||||
}
|
||||
|
||||
Dir FS::openDir(const char* path)
|
||||
{
|
||||
if (!_impl)
|
||||
{
|
||||
Dir FS::openDir(const char* path) {
|
||||
if (!_impl) {
|
||||
return Dir();
|
||||
}
|
||||
DirImplPtr p = _impl->openDir(path);
|
||||
return Dir(p, this);
|
||||
}
|
||||
|
||||
Dir FS::openDir(const String& path)
|
||||
{
|
||||
Dir FS::openDir(const String& path) {
|
||||
return openDir(path.c_str());
|
||||
}
|
||||
|
||||
bool FS::remove(const char* path)
|
||||
{
|
||||
if (!_impl)
|
||||
{
|
||||
bool FS::remove(const char* path) {
|
||||
if (!_impl) {
|
||||
return false;
|
||||
}
|
||||
return _impl->remove(path);
|
||||
}
|
||||
|
||||
bool FS::remove(const String& path)
|
||||
{
|
||||
bool FS::remove(const String& path) {
|
||||
return remove(path.c_str());
|
||||
}
|
||||
|
||||
bool FS::rmdir(const char* path)
|
||||
{
|
||||
if (!_impl)
|
||||
{
|
||||
bool FS::rmdir(const char* path) {
|
||||
if (!_impl) {
|
||||
return false;
|
||||
}
|
||||
return _impl->rmdir(path);
|
||||
}
|
||||
|
||||
bool FS::rmdir(const String& path)
|
||||
{
|
||||
bool FS::rmdir(const String& path) {
|
||||
return rmdir(path.c_str());
|
||||
}
|
||||
|
||||
bool FS::mkdir(const char* path)
|
||||
{
|
||||
if (!_impl)
|
||||
{
|
||||
bool FS::mkdir(const char* path) {
|
||||
if (!_impl) {
|
||||
return false;
|
||||
}
|
||||
return _impl->mkdir(path);
|
||||
}
|
||||
|
||||
bool FS::mkdir(const String& path)
|
||||
{
|
||||
bool FS::mkdir(const String& path) {
|
||||
return mkdir(path.c_str());
|
||||
}
|
||||
|
||||
bool FS::rename(const char* pathFrom, const char* pathTo)
|
||||
{
|
||||
if (!_impl)
|
||||
{
|
||||
bool FS::rename(const char* pathFrom, const char* pathTo) {
|
||||
if (!_impl) {
|
||||
return false;
|
||||
}
|
||||
return _impl->rename(pathFrom, pathTo);
|
||||
}
|
||||
|
||||
bool FS::rename(const String& pathFrom, const String& pathTo)
|
||||
{
|
||||
bool FS::rename(const String& pathFrom, const String& pathTo) {
|
||||
return rename(pathFrom.c_str(), pathTo.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
static bool sflags(const char* mode, OpenMode& om, AccessMode& am)
|
||||
{
|
||||
switch (mode[0])
|
||||
{
|
||||
case 'r':
|
||||
am = AM_READ;
|
||||
om = OM_DEFAULT;
|
||||
break;
|
||||
case 'w':
|
||||
am = AM_WRITE;
|
||||
om = (OpenMode)(OM_CREATE | OM_TRUNCATE);
|
||||
break;
|
||||
case 'a':
|
||||
am = AM_WRITE;
|
||||
om = (OpenMode)(OM_CREATE | OM_APPEND);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
static bool sflags(const char* mode, OpenMode& om, AccessMode& am) {
|
||||
switch (mode[0]) {
|
||||
case 'r':
|
||||
am = AM_READ;
|
||||
om = OM_DEFAULT;
|
||||
break;
|
||||
case 'w':
|
||||
am = AM_WRITE;
|
||||
om = (OpenMode) (OM_CREATE | OM_TRUNCATE);
|
||||
break;
|
||||
case 'a':
|
||||
am = AM_WRITE;
|
||||
om = (OpenMode) (OM_CREATE | OM_APPEND);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
switch (mode[1])
|
||||
{
|
||||
case '+':
|
||||
am = (AccessMode)(AM_WRITE | AM_READ);
|
||||
break;
|
||||
case 0:
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
switch(mode[1]) {
|
||||
case '+':
|
||||
am = (AccessMode) (AM_WRITE | AM_READ);
|
||||
break;
|
||||
case 0:
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -512,7 +403,7 @@ static bool sflags(const char* mode, OpenMode& om, AccessMode& am)
|
||||
#if defined(FS_FREESTANDING_FUNCTIONS)
|
||||
|
||||
/*
|
||||
TODO: move these functions to public API:
|
||||
TODO: move these functions to public API:
|
||||
*/
|
||||
File open(const char* path, const char* mode);
|
||||
File open(String& path, const char* mode);
|
||||
@ -526,8 +417,7 @@ bool mount<FS>(FS& fs, const char* mountPoint);
|
||||
*/
|
||||
|
||||
|
||||
struct MountEntry
|
||||
{
|
||||
struct MountEntry {
|
||||
FSImplPtr fs;
|
||||
String path;
|
||||
MountEntry* next;
|
||||
@ -536,11 +426,9 @@ struct MountEntry
|
||||
static MountEntry* s_mounted = nullptr;
|
||||
|
||||
template<>
|
||||
bool mount<FS>(FS& fs, const char* mountPoint)
|
||||
{
|
||||
bool mount<FS>(FS& fs, const char* mountPoint) {
|
||||
FSImplPtr p = fs._impl;
|
||||
if (!p || !p->mount())
|
||||
{
|
||||
if (!p || !p->mount()) {
|
||||
DEBUGV("FSImpl mount failed\r\n");
|
||||
return false;
|
||||
}
|
||||
@ -559,39 +447,31 @@ bool mount<FS>(FS& fs, const char* mountPoint)
|
||||
/*
|
||||
iterate over MountEntries and look for the ones which match the path
|
||||
*/
|
||||
File open(const char* path, const char* mode)
|
||||
{
|
||||
File open(const char* path, const char* mode) {
|
||||
OpenMode om;
|
||||
AccessMode am;
|
||||
if (!sflags(mode, om, am))
|
||||
{
|
||||
if (!sflags(mode, om, am)) {
|
||||
DEBUGV("open: invalid mode `%s`\r\n", mode);
|
||||
return File();
|
||||
}
|
||||
|
||||
for (MountEntry* entry = s_mounted; entry; entry = entry->next)
|
||||
{
|
||||
for (MountEntry* entry = s_mounted; entry; entry = entry->next) {
|
||||
size_t offset = entry->path.length();
|
||||
if (strstr(path, entry->path.c_str()))
|
||||
{
|
||||
if (strstr(path, entry->path.c_str())) {
|
||||
File result = entry->fs->open(path + offset);
|
||||
if (result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return File();
|
||||
}
|
||||
|
||||
File open(const String& path, const char* mode)
|
||||
{
|
||||
File open(const String& path, const char* mode) {
|
||||
return FS::open(path.c_str(), mode);
|
||||
}
|
||||
|
||||
Dir openDir(const String& path)
|
||||
{
|
||||
Dir openDir(const String& path) {
|
||||
return openDir(path.c_str());
|
||||
}
|
||||
#endif
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
FS.h - file system wrapper
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
FS.h - file system wrapper
|
||||
Copyright (c) 2015 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef FS_H
|
||||
#define FS_H
|
||||
@ -24,8 +24,7 @@
|
||||
#include <memory>
|
||||
#include <Arduino.h>
|
||||
|
||||
namespace fs
|
||||
{
|
||||
namespace fs {
|
||||
|
||||
class File;
|
||||
class Dir;
|
||||
@ -41,8 +40,7 @@ typedef std::shared_ptr<DirImpl> DirImplPtr;
|
||||
template <typename Tfs>
|
||||
bool mount(Tfs& fs, const char* mountPoint);
|
||||
|
||||
enum SeekMode
|
||||
{
|
||||
enum SeekMode {
|
||||
SeekSet = 0,
|
||||
SeekCur = 1,
|
||||
SeekEnd = 2
|
||||
@ -62,14 +60,12 @@ public:
|
||||
int read() override;
|
||||
int peek() override;
|
||||
void flush() override;
|
||||
size_t readBytes(char *buffer, size_t length) override
|
||||
{
|
||||
size_t readBytes(char *buffer, size_t length) override {
|
||||
return read((uint8_t*)buffer, length);
|
||||
}
|
||||
size_t read(uint8_t* buf, size_t size);
|
||||
bool seek(uint32_t pos, SeekMode mode);
|
||||
bool seek(uint32_t pos)
|
||||
{
|
||||
bool seek(uint32_t pos) {
|
||||
return seek(pos, SeekSet);
|
||||
}
|
||||
size_t position() const;
|
||||
@ -84,29 +80,26 @@ public:
|
||||
bool isDirectory() const;
|
||||
|
||||
// Arduino "class SD" methods for compatibility
|
||||
template<typename T> size_t write(T &src)
|
||||
{
|
||||
uint8_t obuf[256];
|
||||
size_t doneLen = 0;
|
||||
size_t sentLen;
|
||||
int i;
|
||||
template<typename T> size_t write(T &src){
|
||||
uint8_t obuf[256];
|
||||
size_t doneLen = 0;
|
||||
size_t sentLen;
|
||||
int i;
|
||||
|
||||
while (src.available() > sizeof(obuf))
|
||||
{
|
||||
src.read(obuf, sizeof(obuf));
|
||||
sentLen = write(obuf, sizeof(obuf));
|
||||
doneLen = doneLen + sentLen;
|
||||
if (sentLen != sizeof(obuf))
|
||||
{
|
||||
return doneLen;
|
||||
}
|
||||
}
|
||||
|
||||
size_t leftLen = src.available();
|
||||
src.read(obuf, leftLen);
|
||||
sentLen = write(obuf, leftLen);
|
||||
while (src.available() > sizeof(obuf)){
|
||||
src.read(obuf, sizeof(obuf));
|
||||
sentLen = write(obuf, sizeof(obuf));
|
||||
doneLen = doneLen + sentLen;
|
||||
return doneLen;
|
||||
if(sentLen != sizeof(obuf)){
|
||||
return doneLen;
|
||||
}
|
||||
}
|
||||
|
||||
size_t leftLen = src.available();
|
||||
src.read(obuf, leftLen);
|
||||
sentLen = write(obuf, leftLen);
|
||||
doneLen = doneLen + sentLen;
|
||||
return doneLen;
|
||||
}
|
||||
using Print::write;
|
||||
|
||||
@ -123,8 +116,7 @@ protected:
|
||||
FS *_baseFS;
|
||||
};
|
||||
|
||||
class Dir
|
||||
{
|
||||
class Dir {
|
||||
public:
|
||||
Dir(DirImplPtr impl = DirImplPtr(), FS *baseFS = nullptr): _impl(impl), _baseFS(baseFS) { }
|
||||
|
||||
@ -143,8 +135,7 @@ protected:
|
||||
FS *_baseFS;
|
||||
};
|
||||
|
||||
struct FSInfo
|
||||
{
|
||||
struct FSInfo {
|
||||
size_t totalBytes;
|
||||
size_t usedBytes;
|
||||
size_t blockSize;
|
||||
@ -156,15 +147,13 @@ struct FSInfo
|
||||
class FSConfig
|
||||
{
|
||||
public:
|
||||
FSConfig(bool autoFormat = true)
|
||||
{
|
||||
FSConfig(bool autoFormat = true) {
|
||||
_type = FSConfig::fsid::FSId;
|
||||
_autoFormat = autoFormat;
|
||||
_autoFormat = autoFormat;
|
||||
}
|
||||
|
||||
enum fsid { FSId = 0x00000000 };
|
||||
FSConfig setAutoFormat(bool val = true)
|
||||
{
|
||||
FSConfig setAutoFormat(bool val = true) {
|
||||
_autoFormat = val;
|
||||
return *this;
|
||||
}
|
||||
@ -176,10 +165,9 @@ public:
|
||||
class SPIFFSConfig : public FSConfig
|
||||
{
|
||||
public:
|
||||
SPIFFSConfig(bool autoFormat = true)
|
||||
{
|
||||
SPIFFSConfig(bool autoFormat = true) {
|
||||
_type = SPIFFSConfig::fsid::FSId;
|
||||
_autoFormat = autoFormat;
|
||||
_autoFormat = autoFormat;
|
||||
}
|
||||
enum fsid { FSId = 0x53504946 };
|
||||
};
|
||||
|
@ -1,33 +1,31 @@
|
||||
/*
|
||||
FSImpl.h - base file system interface
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
FSImpl.h - base file system interface
|
||||
Copyright (c) 2015 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
#ifndef FSIMPL_H
|
||||
#define FSIMPL_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
namespace fs
|
||||
{
|
||||
namespace fs {
|
||||
|
||||
class FileImpl
|
||||
{
|
||||
class FileImpl {
|
||||
public:
|
||||
virtual ~FileImpl() { }
|
||||
virtual size_t write(const uint8_t *buf, size_t size) = 0;
|
||||
@ -44,23 +42,20 @@ public:
|
||||
virtual bool isDirectory() const = 0;
|
||||
};
|
||||
|
||||
enum OpenMode
|
||||
{
|
||||
enum OpenMode {
|
||||
OM_DEFAULT = 0,
|
||||
OM_CREATE = 1,
|
||||
OM_APPEND = 2,
|
||||
OM_TRUNCATE = 4
|
||||
};
|
||||
|
||||
enum AccessMode
|
||||
{
|
||||
enum AccessMode {
|
||||
AM_READ = 1,
|
||||
AM_WRITE = 2,
|
||||
AM_RW = AM_READ | AM_WRITE
|
||||
};
|
||||
|
||||
class DirImpl
|
||||
{
|
||||
class DirImpl {
|
||||
public:
|
||||
virtual ~DirImpl() { }
|
||||
virtual FileImplPtr openFile(OpenMode openMode, AccessMode accessMode) = 0;
|
||||
@ -72,10 +67,9 @@ public:
|
||||
virtual bool rewind() = 0;
|
||||
};
|
||||
|
||||
class FSImpl
|
||||
{
|
||||
class FSImpl {
|
||||
public:
|
||||
virtual ~FSImpl() { }
|
||||
virtual ~FSImpl () { }
|
||||
virtual bool setConfig(const FSConfig &cfg) = 0;
|
||||
virtual bool begin() = 0;
|
||||
virtual void end() = 0;
|
||||
@ -88,10 +82,7 @@ public:
|
||||
virtual bool remove(const char* path) = 0;
|
||||
virtual bool mkdir(const char* path) = 0;
|
||||
virtual bool rmdir(const char* path) = 0;
|
||||
virtual bool gc()
|
||||
{
|
||||
return true; // May not be implemented in all file systems.
|
||||
}
|
||||
virtual bool gc() { return true; } // May not be implemented in all file systems.
|
||||
};
|
||||
|
||||
} // namespace fs
|
||||
|
@ -8,64 +8,64 @@ typedef void (*voidFuncPtr)(void);
|
||||
typedef void (*voidFuncPtrArg)(void*);
|
||||
|
||||
// Helper functions for Functional interrupt routines
|
||||
extern "C" void ICACHE_RAM_ATTR __attachInterruptArg(uint8_t pin, voidFuncPtr userFunc, void*fp, int mode);
|
||||
extern "C" void ICACHE_RAM_ATTR __attachInterruptArg(uint8_t pin, voidFuncPtr userFunc, void*fp , int mode);
|
||||
|
||||
|
||||
void ICACHE_RAM_ATTR interruptFunctional(void* arg)
|
||||
{
|
||||
ArgStructure* localArg = (ArgStructure*)arg;
|
||||
if (localArg->functionInfo->reqScheduledFunction)
|
||||
{
|
||||
schedule_function(std::bind(localArg->functionInfo->reqScheduledFunction, InterruptInfo(*(localArg->interruptInfo))));
|
||||
// scheduledInterrupts->scheduleFunctionReg(std::bind(localArg->functionInfo->reqScheduledFunction,InterruptInfo(*(localArg->interruptInfo))), false, true);
|
||||
}
|
||||
if (localArg->functionInfo->reqFunction)
|
||||
{
|
||||
localArg->functionInfo->reqFunction();
|
||||
}
|
||||
if (localArg->functionInfo->reqScheduledFunction)
|
||||
{
|
||||
schedule_function(std::bind(localArg->functionInfo->reqScheduledFunction,InterruptInfo(*(localArg->interruptInfo))));
|
||||
// scheduledInterrupts->scheduleFunctionReg(std::bind(localArg->functionInfo->reqScheduledFunction,InterruptInfo(*(localArg->interruptInfo))), false, true);
|
||||
}
|
||||
if (localArg->functionInfo->reqFunction)
|
||||
{
|
||||
localArg->functionInfo->reqFunction();
|
||||
}
|
||||
}
|
||||
|
||||
extern "C"
|
||||
{
|
||||
void cleanupFunctional(void* arg)
|
||||
{
|
||||
ArgStructure* localArg = (ArgStructure*)arg;
|
||||
delete (FunctionInfo*)localArg->functionInfo;
|
||||
delete (InterruptInfo*)localArg->interruptInfo;
|
||||
delete localArg;
|
||||
}
|
||||
void cleanupFunctional(void* arg)
|
||||
{
|
||||
ArgStructure* localArg = (ArgStructure*)arg;
|
||||
delete (FunctionInfo*)localArg->functionInfo;
|
||||
delete (InterruptInfo*)localArg->interruptInfo;
|
||||
delete localArg;
|
||||
}
|
||||
}
|
||||
|
||||
void attachInterrupt(uint8_t pin, std::function<void(void)> intRoutine, int mode)
|
||||
{
|
||||
// use the local interrupt routine which takes the ArgStructure as argument
|
||||
// use the local interrupt routine which takes the ArgStructure as argument
|
||||
|
||||
InterruptInfo* ii = nullptr;
|
||||
InterruptInfo* ii = nullptr;
|
||||
|
||||
FunctionInfo* fi = new FunctionInfo;
|
||||
fi->reqFunction = intRoutine;
|
||||
FunctionInfo* fi = new FunctionInfo;
|
||||
fi->reqFunction = intRoutine;
|
||||
|
||||
ArgStructure* as = new ArgStructure;
|
||||
as->interruptInfo = ii;
|
||||
as->functionInfo = fi;
|
||||
ArgStructure* as = new ArgStructure;
|
||||
as->interruptInfo = ii;
|
||||
as->functionInfo = fi;
|
||||
|
||||
__attachInterruptArg(pin, (voidFuncPtr)interruptFunctional, as, mode);
|
||||
__attachInterruptArg (pin, (voidFuncPtr)interruptFunctional, as, mode);
|
||||
}
|
||||
|
||||
void attachScheduledInterrupt(uint8_t pin, std::function<void(InterruptInfo)> scheduledIntRoutine, int mode)
|
||||
{
|
||||
if (!scheduledInterrupts)
|
||||
{
|
||||
scheduledInterrupts = new ScheduledFunctions(32);
|
||||
}
|
||||
InterruptInfo* ii = new InterruptInfo;
|
||||
if (!scheduledInterrupts)
|
||||
{
|
||||
scheduledInterrupts = new ScheduledFunctions(32);
|
||||
}
|
||||
InterruptInfo* ii = new InterruptInfo;
|
||||
|
||||
FunctionInfo* fi = new FunctionInfo;
|
||||
fi->reqScheduledFunction = scheduledIntRoutine;
|
||||
FunctionInfo* fi = new FunctionInfo;
|
||||
fi->reqScheduledFunction = scheduledIntRoutine;
|
||||
|
||||
ArgStructure* as = new ArgStructure;
|
||||
as->interruptInfo = ii;
|
||||
as->functionInfo = fi;
|
||||
ArgStructure* as = new ArgStructure;
|
||||
as->interruptInfo = ii;
|
||||
as->functionInfo = fi;
|
||||
|
||||
__attachInterruptArg(pin, (voidFuncPtr)interruptFunctional, as, mode);
|
||||
__attachInterruptArg (pin, (voidFuncPtr)interruptFunctional, as, mode);
|
||||
}
|
||||
|
@ -13,23 +13,20 @@ extern "C" {
|
||||
|
||||
// Structures for communication
|
||||
|
||||
struct InterruptInfo
|
||||
{
|
||||
uint8_t pin = 0;
|
||||
uint8_t value = 0;
|
||||
uint32_t micro = 0;
|
||||
struct InterruptInfo {
|
||||
uint8_t pin = 0;
|
||||
uint8_t value = 0;
|
||||
uint32_t micro = 0;
|
||||
};
|
||||
|
||||
struct FunctionInfo
|
||||
{
|
||||
struct FunctionInfo {
|
||||
std::function<void(void)> reqFunction = nullptr;
|
||||
std::function<void(InterruptInfo)> reqScheduledFunction = nullptr;
|
||||
std::function<void(InterruptInfo)> reqScheduledFunction = nullptr;
|
||||
};
|
||||
|
||||
struct ArgStructure
|
||||
{
|
||||
InterruptInfo* interruptInfo = nullptr;
|
||||
FunctionInfo* functionInfo = nullptr;
|
||||
struct ArgStructure {
|
||||
InterruptInfo* interruptInfo = nullptr;
|
||||
FunctionInfo* functionInfo = nullptr;
|
||||
};
|
||||
|
||||
static ScheduledFunctions* scheduledInterrupts;
|
||||
|
@ -1,27 +1,27 @@
|
||||
/*
|
||||
HardwareSerial.cpp - esp8266 UART support
|
||||
HardwareSerial.cpp - esp8266 UART support
|
||||
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2014 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
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
|
||||
|
||||
Modified 31 March 2015 by Markus Sattler (rewrite the code for UART0 + UART1 support in ESP8266)
|
||||
Modified 25 April 2015 by Thomas Flayols (add configuration different from 8N1 in ESP8266)
|
||||
Modified 3 May 2015 by Hristo Gochkov (change register access methods)
|
||||
*/
|
||||
Modified 31 March 2015 by Markus Sattler (rewrite the code for UART0 + UART1 support in ESP8266)
|
||||
Modified 25 April 2015 by Thomas Flayols (add configuration different from 8N1 in ESP8266)
|
||||
Modified 3 May 2015 by Hristo Gochkov (change register access methods)
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
@ -52,8 +52,7 @@ void HardwareSerial::begin(unsigned long baud, SerialConfig config, SerialMode m
|
||||
|
||||
void HardwareSerial::end()
|
||||
{
|
||||
if (uart_get_debug() == _uart_nr)
|
||||
{
|
||||
if(uart_get_debug() == _uart_nr) {
|
||||
uart_set_debug(UART_NO);
|
||||
}
|
||||
|
||||
@ -61,14 +60,10 @@ void HardwareSerial::end()
|
||||
_uart = NULL;
|
||||
}
|
||||
|
||||
size_t HardwareSerial::setRxBufferSize(size_t size)
|
||||
{
|
||||
if (_uart)
|
||||
{
|
||||
size_t HardwareSerial::setRxBufferSize(size_t size){
|
||||
if(_uart) {
|
||||
_rx_size = uart_resize_rx_buffer(_uart, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
_rx_size = size;
|
||||
}
|
||||
return _rx_size;
|
||||
@ -76,26 +71,18 @@ size_t HardwareSerial::setRxBufferSize(size_t size)
|
||||
|
||||
void HardwareSerial::setDebugOutput(bool en)
|
||||
{
|
||||
if (!_uart)
|
||||
{
|
||||
if(!_uart) {
|
||||
return;
|
||||
}
|
||||
if (en)
|
||||
{
|
||||
if (uart_tx_enabled(_uart))
|
||||
{
|
||||
if(en) {
|
||||
if(uart_tx_enabled(_uart)) {
|
||||
uart_set_debug(_uart_nr);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
uart_set_debug(UART_NO);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// disable debug for this interface
|
||||
if (uart_get_debug() == _uart_nr)
|
||||
{
|
||||
if(uart_get_debug() == _uart_nr) {
|
||||
uart_set_debug(UART_NO);
|
||||
}
|
||||
}
|
||||
@ -104,8 +91,7 @@ void HardwareSerial::setDebugOutput(bool en)
|
||||
int HardwareSerial::available(void)
|
||||
{
|
||||
int result = static_cast<int>(uart_rx_available(_uart));
|
||||
if (!result)
|
||||
{
|
||||
if (!result) {
|
||||
optimistic_yield(10000);
|
||||
}
|
||||
return result;
|
||||
@ -113,8 +99,7 @@ int HardwareSerial::available(void)
|
||||
|
||||
void HardwareSerial::flush()
|
||||
{
|
||||
if (!_uart || !uart_tx_enabled(_uart))
|
||||
{
|
||||
if(!_uart || !uart_tx_enabled(_uart)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -138,11 +123,9 @@ unsigned long HardwareSerial::detectBaudrate(time_t timeoutMillis)
|
||||
{
|
||||
time_t startMillis = millis();
|
||||
unsigned long detectedBaudrate;
|
||||
while ((time_t) millis() - startMillis < timeoutMillis)
|
||||
{
|
||||
if ((detectedBaudrate = testBaudrate()))
|
||||
{
|
||||
break;
|
||||
while ((time_t) millis() - startMillis < timeoutMillis) {
|
||||
if ((detectedBaudrate = testBaudrate())) {
|
||||
break;
|
||||
}
|
||||
yield();
|
||||
delay(100);
|
||||
@ -160,9 +143,7 @@ size_t HardwareSerial::readBytes(char* buffer, size_t size)
|
||||
size_t avail;
|
||||
while ((avail = available()) == 0 && !timeOut);
|
||||
if (avail == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
got += read(buffer + got, std::min(size - got, avail));
|
||||
}
|
||||
return got;
|
||||
|
@ -1,28 +1,28 @@
|
||||
/*
|
||||
HardwareSerial.h - Hardware serial library for Wiring
|
||||
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
||||
HardwareSerial.h - Hardware serial library for Wiring
|
||||
Copyright (c) 2006 Nicholas Zambetti. 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 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.
|
||||
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
|
||||
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
|
||||
|
||||
Modified 28 September 2010 by Mark Sproul
|
||||
Modified 14 August 2012 by Alarus
|
||||
Modified 3 December 2013 by Matthijs Kooijman
|
||||
Modified 18 December 2014 by Ivan Grokhotkov (esp8266 platform support)
|
||||
Modified 31 March 2015 by Markus Sattler (rewrite the code for UART0 + UART1 support in ESP8266)
|
||||
Modified 25 April 2015 by Thomas Flayols (add configuration different from 8N1 in ESP8266)
|
||||
*/
|
||||
Modified 28 September 2010 by Mark Sproul
|
||||
Modified 14 August 2012 by Alarus
|
||||
Modified 3 December 2013 by Matthijs Kooijman
|
||||
Modified 18 December 2014 by Ivan Grokhotkov (esp8266 platform support)
|
||||
Modified 31 March 2015 by Markus Sattler (rewrite the code for UART0 + UART1 support in ESP8266)
|
||||
Modified 25 April 2015 by Thomas Flayols (add configuration different from 8N1 in ESP8266)
|
||||
*/
|
||||
|
||||
#ifndef HardwareSerial_h
|
||||
#define HardwareSerial_h
|
||||
@ -32,8 +32,7 @@
|
||||
#include "Stream.h"
|
||||
#include "uart.h"
|
||||
|
||||
enum SerialConfig
|
||||
{
|
||||
enum SerialConfig {
|
||||
SERIAL_5N1 = UART_5N1,
|
||||
SERIAL_6N1 = UART_6N1,
|
||||
SERIAL_7N1 = UART_7N1,
|
||||
@ -60,8 +59,7 @@ enum SerialConfig
|
||||
SERIAL_8O2 = UART_8O2,
|
||||
};
|
||||
|
||||
enum SerialMode
|
||||
{
|
||||
enum SerialMode {
|
||||
SERIAL_FULL = UART_FULL,
|
||||
SERIAL_RX_ONLY = UART_RX_ONLY,
|
||||
SERIAL_TX_ONLY = UART_TX_ONLY
|
||||
@ -106,18 +104,18 @@ public:
|
||||
}
|
||||
|
||||
/*
|
||||
Toggle between use of GPIO1 and GPIO2 as TX on UART 0.
|
||||
Note: UART 1 can't be used if GPIO2 is used with UART 0!
|
||||
*/
|
||||
* Toggle between use of GPIO1 and GPIO2 as TX on UART 0.
|
||||
* Note: UART 1 can't be used if GPIO2 is used with UART 0!
|
||||
*/
|
||||
void set_tx(uint8_t tx_pin)
|
||||
{
|
||||
uart_set_tx(_uart, tx_pin);
|
||||
}
|
||||
|
||||
/*
|
||||
UART 0 possible options are (1, 3), (2, 3) or (15, 13)
|
||||
UART 1 allows only TX on 2 if UART 0 is not (2, 3)
|
||||
*/
|
||||
* UART 0 possible options are (1, 3), (2, 3) or (15, 13)
|
||||
* UART 1 allows only TX on 2 if UART 0 is not (2, 3)
|
||||
*/
|
||||
void pins(uint8_t tx, uint8_t rx)
|
||||
{
|
||||
uart_set_pins(_uart, tx, rx);
|
||||
|
@ -1,21 +1,21 @@
|
||||
/*
|
||||
IPAddress.cpp - Base class that provides IPAddress
|
||||
Copyright (c) 2011 Adrian McEwen. All right reserved.
|
||||
IPAddress.cpp - Base class that provides IPAddress
|
||||
Copyright (c) 2011 Adrian McEwen. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <IPAddress.h>
|
||||
@ -27,18 +27,15 @@ IPAddress::IPAddress(const IPAddress& from)
|
||||
ip_addr_copy(_ip, from._ip);
|
||||
}
|
||||
|
||||
IPAddress::IPAddress()
|
||||
{
|
||||
IPAddress::IPAddress() {
|
||||
_ip = *IP_ANY_TYPE; // lwIP's v4-or-v6 generic address
|
||||
}
|
||||
|
||||
bool IPAddress::isSet() const
|
||||
{
|
||||
bool IPAddress::isSet () const {
|
||||
return !ip_addr_isany(&_ip);
|
||||
}
|
||||
|
||||
IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
|
||||
{
|
||||
IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) {
|
||||
setV4();
|
||||
(*this)[0] = first_octet;
|
||||
(*this)[1] = second_octet;
|
||||
@ -46,14 +43,12 @@ IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_oc
|
||||
(*this)[3] = fourth_octet;
|
||||
}
|
||||
|
||||
void IPAddress::ctor32(uint32_t address)
|
||||
{
|
||||
void IPAddress::ctor32(uint32_t address) {
|
||||
setV4();
|
||||
v4() = address;
|
||||
}
|
||||
|
||||
IPAddress::IPAddress(const uint8_t *address)
|
||||
{
|
||||
IPAddress::IPAddress(const uint8_t *address) {
|
||||
setV4();
|
||||
(*this)[0] = address[0];
|
||||
(*this)[1] = address[1];
|
||||
@ -61,10 +56,8 @@ IPAddress::IPAddress(const uint8_t *address)
|
||||
(*this)[3] = address[3];
|
||||
}
|
||||
|
||||
bool IPAddress::fromString(const char *address)
|
||||
{
|
||||
if (!fromString4(address))
|
||||
{
|
||||
bool IPAddress::fromString(const char *address) {
|
||||
if (!fromString4(address)) {
|
||||
#if LWIP_IPV6
|
||||
return fromString6(address);
|
||||
#else
|
||||
@ -74,8 +67,7 @@ bool IPAddress::fromString(const char *address)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IPAddress::fromString4(const char *address)
|
||||
{
|
||||
bool IPAddress::fromString4(const char *address) {
|
||||
// TODO: (IPv4) add support for "a", "a.b", "a.b.c" formats
|
||||
|
||||
uint16_t acc = 0; // Accumulator
|
||||
@ -87,16 +79,14 @@ bool IPAddress::fromString4(const char *address)
|
||||
if (c >= '0' && c <= '9')
|
||||
{
|
||||
acc = acc * 10 + (c - '0');
|
||||
if (acc > 255)
|
||||
{
|
||||
if (acc > 255) {
|
||||
// Value out of [0..255] range
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (c == '.')
|
||||
{
|
||||
if (dots == 3)
|
||||
{
|
||||
if (dots == 3) {
|
||||
// Too much dots (there must be 3 dots)
|
||||
return false;
|
||||
}
|
||||
@ -110,8 +100,7 @@ bool IPAddress::fromString4(const char *address)
|
||||
}
|
||||
}
|
||||
|
||||
if (dots != 3)
|
||||
{
|
||||
if (dots != 3) {
|
||||
// Too few dots (there must be 3 dots)
|
||||
return false;
|
||||
}
|
||||
@ -121,70 +110,51 @@ bool IPAddress::fromString4(const char *address)
|
||||
return true;
|
||||
}
|
||||
|
||||
IPAddress& IPAddress::operator=(const uint8_t *address)
|
||||
{
|
||||
IPAddress& IPAddress::operator=(const uint8_t *address) {
|
||||
setV4();
|
||||
v4() = *reinterpret_cast<const uint32_t*>(address);
|
||||
return *this;
|
||||
}
|
||||
|
||||
IPAddress& IPAddress::operator=(uint32_t address)
|
||||
{
|
||||
IPAddress& IPAddress::operator=(uint32_t address) {
|
||||
setV4();
|
||||
v4() = address;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool IPAddress::operator==(const uint8_t* addr) const
|
||||
{
|
||||
bool IPAddress::operator==(const uint8_t* addr) const {
|
||||
return isV4() && v4() == *reinterpret_cast<const uint32_t*>(addr);
|
||||
}
|
||||
|
||||
size_t IPAddress::printTo(Print& p) const
|
||||
{
|
||||
size_t IPAddress::printTo(Print& p) const {
|
||||
size_t n = 0;
|
||||
|
||||
if (!isSet())
|
||||
{
|
||||
return p.print(F("(IP unset)"));
|
||||
}
|
||||
|
||||
#if LWIP_IPV6
|
||||
if (isV6())
|
||||
{
|
||||
if (isV6()) {
|
||||
int count0 = 0;
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
for (int i = 0; i < 8; i++) {
|
||||
uint16_t bit = PP_NTOHS(raw6()[i]);
|
||||
if (bit || count0 < 0)
|
||||
{
|
||||
if (bit || count0 < 0) {
|
||||
n += p.printf("%x", bit);
|
||||
if (count0 > 0)
|
||||
// no more hiding 0
|
||||
{
|
||||
count0 = -8;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else
|
||||
count0++;
|
||||
}
|
||||
if ((i != 7 && count0 < 2) || count0 == 7)
|
||||
{
|
||||
n += p.print(':');
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
#endif
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
for(int i = 0; i < 4; i++) {
|
||||
n += p.print((*this)[i], DEC);
|
||||
if (i != 3)
|
||||
{
|
||||
n += p.print('.');
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
@ -194,9 +164,7 @@ String IPAddress::toString() const
|
||||
StreamString sstr;
|
||||
#if LWIP_IPV6
|
||||
if (isV6())
|
||||
{
|
||||
sstr.reserve(40); // 8 shorts x 4 chars each + 7 colons + nullterm
|
||||
}
|
||||
sstr.reserve(40); // 8 shorts x 4 chars each + 7 colons + nullterm
|
||||
else
|
||||
#endif
|
||||
sstr.reserve(16); // 4 bytes with 3 chars max + 3 dots + nullterm, or '(IP unset)'
|
||||
@ -204,25 +172,22 @@ String IPAddress::toString() const
|
||||
return sstr;
|
||||
}
|
||||
|
||||
bool IPAddress::isValid(const String& arg)
|
||||
{
|
||||
return IPAddress().fromString(arg);
|
||||
bool IPAddress::isValid(const String& arg) {
|
||||
return IPAddress().fromString(arg);
|
||||
}
|
||||
|
||||
bool IPAddress::isValid(const char* arg)
|
||||
{
|
||||
return IPAddress().fromString(arg);
|
||||
bool IPAddress::isValid(const char* arg) {
|
||||
return IPAddress().fromString(arg);
|
||||
}
|
||||
|
||||
CONST IPAddress INADDR_ANY; // generic "0.0.0.0" for IPv4 & IPv6
|
||||
const IPAddress INADDR_NONE(255, 255, 255, 255);
|
||||
const IPAddress INADDR_NONE(255,255,255,255);
|
||||
|
||||
/**************************************/
|
||||
|
||||
#if LWIP_IPV6
|
||||
|
||||
bool IPAddress::fromString6(const char *address)
|
||||
{
|
||||
bool IPAddress::fromString6(const char *address) {
|
||||
// TODO: test test test
|
||||
|
||||
uint32_t acc = 0; // Accumulator
|
||||
@ -231,64 +196,44 @@ bool IPAddress::fromString6(const char *address)
|
||||
while (*address)
|
||||
{
|
||||
char c = tolower(*address++);
|
||||
if (isalnum(c))
|
||||
{
|
||||
if (isalnum(c)) {
|
||||
if (c >= 'a')
|
||||
{
|
||||
c -= 'a' - '0' - 10;
|
||||
}
|
||||
acc = acc * 16 + (c - '0');
|
||||
if (acc > 0xffff)
|
||||
// Value out of range
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (c == ':')
|
||||
{
|
||||
if (*address == ':')
|
||||
{
|
||||
else if (c == ':') {
|
||||
if (*address == ':') {
|
||||
if (doubledots >= 0)
|
||||
// :: allowed once
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// remember location
|
||||
doubledots = dots + !!acc;
|
||||
address++;
|
||||
}
|
||||
if (dots == 7)
|
||||
// too many separators
|
||||
{
|
||||
return false;
|
||||
}
|
||||
raw6()[dots++] = PP_HTONS(acc);
|
||||
acc = 0;
|
||||
}
|
||||
else
|
||||
// Invalid char
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (doubledots == -1 && dots != 7)
|
||||
// Too few separators
|
||||
{
|
||||
return false;
|
||||
}
|
||||
raw6()[dots++] = PP_HTONS(acc);
|
||||
|
||||
if (doubledots != -1)
|
||||
{
|
||||
if (doubledots != -1) {
|
||||
for (int i = dots - doubledots - 1; i >= 0; i--)
|
||||
{
|
||||
raw6()[8 - dots + doubledots + i] = raw6()[doubledots + i];
|
||||
}
|
||||
for (int i = doubledots; i < 8 - dots + doubledots; i++)
|
||||
{
|
||||
raw6()[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
setV6();
|
||||
|
@ -1,21 +1,21 @@
|
||||
/*
|
||||
IPAddress.h - Base class that provides IPAddress
|
||||
Copyright (c) 2011 Adrian McEwen. All right reserved.
|
||||
IPAddress.h - Base class that provides IPAddress
|
||||
Copyright (c) 2011 Adrian McEwen. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef IPAddress_h
|
||||
#define IPAddress_h
|
||||
@ -54,283 +54,167 @@ struct ip_addr: ipv4_addr { };
|
||||
// fully backward compatible with legacy IPv4-only Arduino's
|
||||
// with unchanged footprint when IPv6 is disabled
|
||||
|
||||
class IPAddress: public Printable
|
||||
{
|
||||
private:
|
||||
class IPAddress: public Printable {
|
||||
private:
|
||||
|
||||
ip_addr_t _ip;
|
||||
ip_addr_t _ip;
|
||||
|
||||
// Access the raw byte array containing the address. Because this returns a pointer
|
||||
// to the internal structure rather than a copy of the address this function should only
|
||||
// be used when you know that the usage of the returned uint8_t* will be transient and not
|
||||
// stored.
|
||||
uint8_t* raw_address()
|
||||
{
|
||||
return reinterpret_cast<uint8_t*>(&v4());
|
||||
}
|
||||
const uint8_t* raw_address() const
|
||||
{
|
||||
return reinterpret_cast<const uint8_t*>(&v4());
|
||||
}
|
||||
// Access the raw byte array containing the address. Because this returns a pointer
|
||||
// to the internal structure rather than a copy of the address this function should only
|
||||
// be used when you know that the usage of the returned uint8_t* will be transient and not
|
||||
// stored.
|
||||
uint8_t* raw_address() {
|
||||
return reinterpret_cast<uint8_t*>(&v4());
|
||||
}
|
||||
const uint8_t* raw_address() const {
|
||||
return reinterpret_cast<const uint8_t*>(&v4());
|
||||
}
|
||||
|
||||
void ctor32(uint32_t);
|
||||
void ctor32 (uint32_t);
|
||||
|
||||
public:
|
||||
// Constructors
|
||||
IPAddress();
|
||||
IPAddress(const IPAddress& from);
|
||||
IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
|
||||
IPAddress(uint32_t address)
|
||||
{
|
||||
ctor32(address);
|
||||
}
|
||||
IPAddress(u32_t address)
|
||||
{
|
||||
ctor32(address);
|
||||
}
|
||||
IPAddress(int address)
|
||||
{
|
||||
ctor32(address);
|
||||
}
|
||||
IPAddress(const uint8_t *address);
|
||||
public:
|
||||
// Constructors
|
||||
IPAddress();
|
||||
IPAddress(const IPAddress& from);
|
||||
IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
|
||||
IPAddress(uint32_t address) { ctor32(address); }
|
||||
IPAddress(u32_t address) { ctor32(address); }
|
||||
IPAddress(int address) { ctor32(address); }
|
||||
IPAddress(const uint8_t *address);
|
||||
|
||||
bool fromString(const char *address);
|
||||
bool fromString(const String &address)
|
||||
{
|
||||
return fromString(address.c_str());
|
||||
}
|
||||
bool fromString(const char *address);
|
||||
bool fromString(const String &address) { return fromString(address.c_str()); }
|
||||
|
||||
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
|
||||
// to a four-byte uint8_t array is expected
|
||||
operator uint32_t() const
|
||||
{
|
||||
return isV4() ? v4() : (uint32_t)0;
|
||||
}
|
||||
operator uint32_t()
|
||||
{
|
||||
return isV4() ? v4() : (uint32_t)0;
|
||||
}
|
||||
operator u32_t() const
|
||||
{
|
||||
return isV4() ? v4() : (u32_t)0;
|
||||
}
|
||||
operator u32_t()
|
||||
{
|
||||
return isV4() ? v4() : (u32_t)0;
|
||||
}
|
||||
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
|
||||
// to a four-byte uint8_t array is expected
|
||||
operator uint32_t() const { return isV4()? v4(): (uint32_t)0; }
|
||||
operator uint32_t() { return isV4()? v4(): (uint32_t)0; }
|
||||
operator u32_t() const { return isV4()? v4(): (u32_t)0; }
|
||||
operator u32_t() { return isV4()? v4(): (u32_t)0; }
|
||||
|
||||
bool isSet() const;
|
||||
operator bool () const
|
||||
{
|
||||
return isSet(); // <-
|
||||
}
|
||||
operator bool ()
|
||||
{
|
||||
return isSet(); // <- both are needed
|
||||
}
|
||||
bool isSet () const;
|
||||
operator bool () const { return isSet(); } // <-
|
||||
operator bool () { return isSet(); } // <- both are needed
|
||||
|
||||
// generic IPv4 wrapper to uint32-view like arduino loves to see it
|
||||
const u32_t& v4() const
|
||||
{
|
||||
return ip_2_ip4(&_ip)->addr; // for raw_address(const)
|
||||
}
|
||||
u32_t& v4()
|
||||
{
|
||||
return ip_2_ip4(&_ip)->addr;
|
||||
}
|
||||
// generic IPv4 wrapper to uint32-view like arduino loves to see it
|
||||
const u32_t& v4() const { return ip_2_ip4(&_ip)->addr; } // for raw_address(const)
|
||||
u32_t& v4() { return ip_2_ip4(&_ip)->addr; }
|
||||
|
||||
bool operator==(const IPAddress& addr) const
|
||||
{
|
||||
return ip_addr_cmp(&_ip, &addr._ip);
|
||||
}
|
||||
bool operator!=(const IPAddress& addr) const
|
||||
{
|
||||
return !ip_addr_cmp(&_ip, &addr._ip);
|
||||
}
|
||||
bool operator==(uint32_t addr) const
|
||||
{
|
||||
return isV4() && v4() == addr;
|
||||
}
|
||||
bool operator==(u32_t addr) const
|
||||
{
|
||||
return isV4() && v4() == addr;
|
||||
}
|
||||
bool operator!=(uint32_t addr) const
|
||||
{
|
||||
return !(isV4() && v4() == addr);
|
||||
}
|
||||
bool operator!=(u32_t addr) const
|
||||
{
|
||||
return !(isV4() && v4() == addr);
|
||||
}
|
||||
bool operator==(const uint8_t* addr) const;
|
||||
bool operator==(const IPAddress& addr) const {
|
||||
return ip_addr_cmp(&_ip, &addr._ip);
|
||||
}
|
||||
bool operator!=(const IPAddress& addr) const {
|
||||
return !ip_addr_cmp(&_ip, &addr._ip);
|
||||
}
|
||||
bool operator==(uint32_t addr) const {
|
||||
return isV4() && v4() == addr;
|
||||
}
|
||||
bool operator==(u32_t addr) const {
|
||||
return isV4() && v4() == addr;
|
||||
}
|
||||
bool operator!=(uint32_t addr) const {
|
||||
return !(isV4() && v4() == addr);
|
||||
}
|
||||
bool operator!=(u32_t addr) const {
|
||||
return !(isV4() && v4() == addr);
|
||||
}
|
||||
bool operator==(const uint8_t* addr) const;
|
||||
|
||||
int operator>>(int n) const
|
||||
{
|
||||
return isV4() ? v4() >> n : 0;
|
||||
}
|
||||
int operator>>(int n) const {
|
||||
return isV4()? v4() >> n: 0;
|
||||
}
|
||||
|
||||
// Overloaded index operator to allow getting and setting individual octets of the address
|
||||
uint8_t operator[](int index) const
|
||||
{
|
||||
return isV4() ? *(raw_address() + index) : 0;
|
||||
}
|
||||
uint8_t& operator[](int index)
|
||||
{
|
||||
setV4();
|
||||
return *(raw_address() + index);
|
||||
}
|
||||
// Overloaded index operator to allow getting and setting individual octets of the address
|
||||
uint8_t operator[](int index) const {
|
||||
return isV4()? *(raw_address() + index): 0;
|
||||
}
|
||||
uint8_t& operator[](int index) {
|
||||
setV4();
|
||||
return *(raw_address() + index);
|
||||
}
|
||||
|
||||
// Overloaded copy operators to allow initialisation of IPAddress objects from other types
|
||||
IPAddress& operator=(const uint8_t *address);
|
||||
IPAddress& operator=(uint32_t address);
|
||||
// Overloaded copy operators to allow initialisation of IPAddress objects from other types
|
||||
IPAddress& operator=(const uint8_t *address);
|
||||
IPAddress& operator=(uint32_t address);
|
||||
|
||||
virtual size_t printTo(Print& p) const;
|
||||
String toString() const;
|
||||
virtual size_t printTo(Print& p) const;
|
||||
String toString() const;
|
||||
|
||||
/*
|
||||
check if input string(arg) is a valid IPV4 address or not.
|
||||
return true on valid.
|
||||
return false on invalid.
|
||||
*/
|
||||
static bool isValid(const String& arg);
|
||||
static bool isValid(const char* arg);
|
||||
/*
|
||||
check if input string(arg) is a valid IPV4 address or not.
|
||||
return true on valid.
|
||||
return false on invalid.
|
||||
*/
|
||||
static bool isValid(const String& arg);
|
||||
static bool isValid(const char* arg);
|
||||
|
||||
friend class EthernetClass;
|
||||
friend class UDP;
|
||||
friend class Client;
|
||||
friend class Server;
|
||||
friend class DhcpClass;
|
||||
friend class DNSClient;
|
||||
friend class EthernetClass;
|
||||
friend class UDP;
|
||||
friend class Client;
|
||||
friend class Server;
|
||||
friend class DhcpClass;
|
||||
friend class DNSClient;
|
||||
|
||||
/*
|
||||
lwIP address compatibility
|
||||
*/
|
||||
IPAddress(const ipv4_addr& fw_addr)
|
||||
{
|
||||
setV4();
|
||||
v4() = fw_addr.addr;
|
||||
}
|
||||
IPAddress(const ipv4_addr* fw_addr)
|
||||
{
|
||||
setV4();
|
||||
v4() = fw_addr->addr;
|
||||
}
|
||||
/*
|
||||
lwIP address compatibility
|
||||
*/
|
||||
IPAddress(const ipv4_addr& fw_addr) { setV4(); v4() = fw_addr.addr; }
|
||||
IPAddress(const ipv4_addr* fw_addr) { setV4(); v4() = fw_addr->addr; }
|
||||
|
||||
IPAddress& operator=(const ipv4_addr& fw_addr)
|
||||
{
|
||||
setV4();
|
||||
v4() = fw_addr.addr;
|
||||
return *this;
|
||||
}
|
||||
IPAddress& operator=(const ipv4_addr* fw_addr)
|
||||
{
|
||||
setV4();
|
||||
v4() = fw_addr->addr;
|
||||
return *this;
|
||||
}
|
||||
IPAddress& operator=(const ipv4_addr& fw_addr) { setV4(); v4() = fw_addr.addr; return *this; }
|
||||
IPAddress& operator=(const ipv4_addr* fw_addr) { setV4(); v4() = fw_addr->addr; return *this; }
|
||||
|
||||
operator ip_addr_t () const
|
||||
{
|
||||
return _ip;
|
||||
}
|
||||
operator const ip_addr_t*() const
|
||||
{
|
||||
return &_ip;
|
||||
}
|
||||
operator ip_addr_t*()
|
||||
{
|
||||
return &_ip;
|
||||
}
|
||||
operator ip_addr_t () const { return _ip; }
|
||||
operator const ip_addr_t*() const { return &_ip; }
|
||||
operator ip_addr_t*() { return &_ip; }
|
||||
|
||||
bool isV4() const
|
||||
{
|
||||
return IP_IS_V4_VAL(_ip);
|
||||
}
|
||||
void setV4()
|
||||
{
|
||||
IP_SET_TYPE_VAL(_ip, IPADDR_TYPE_V4);
|
||||
}
|
||||
bool isV4() const { return IP_IS_V4_VAL(_ip); }
|
||||
void setV4() { IP_SET_TYPE_VAL(_ip, IPADDR_TYPE_V4); }
|
||||
|
||||
bool isLocal() const
|
||||
{
|
||||
return ip_addr_islinklocal(&_ip);
|
||||
}
|
||||
bool isLocal () const { return ip_addr_islinklocal(&_ip); }
|
||||
|
||||
#if LWIP_IPV6
|
||||
|
||||
IPAddress(const ip_addr_t& lwip_addr)
|
||||
{
|
||||
ip_addr_copy(_ip, lwip_addr);
|
||||
}
|
||||
IPAddress(const ip_addr_t* lwip_addr)
|
||||
{
|
||||
ip_addr_copy(_ip, *lwip_addr);
|
||||
}
|
||||
IPAddress(const ip_addr_t& lwip_addr) { ip_addr_copy(_ip, lwip_addr); }
|
||||
IPAddress(const ip_addr_t* lwip_addr) { ip_addr_copy(_ip, *lwip_addr); }
|
||||
|
||||
IPAddress& operator=(const ip_addr_t& lwip_addr)
|
||||
{
|
||||
ip_addr_copy(_ip, lwip_addr);
|
||||
return *this;
|
||||
}
|
||||
IPAddress& operator=(const ip_addr_t* lwip_addr)
|
||||
{
|
||||
ip_addr_copy(_ip, *lwip_addr);
|
||||
return *this;
|
||||
}
|
||||
IPAddress& operator=(const ip_addr_t& lwip_addr) { ip_addr_copy(_ip, lwip_addr); return *this; }
|
||||
IPAddress& operator=(const ip_addr_t* lwip_addr) { ip_addr_copy(_ip, *lwip_addr); return *this; }
|
||||
|
||||
uint16_t* raw6()
|
||||
{
|
||||
setV6();
|
||||
return reinterpret_cast<uint16_t*>(ip_2_ip6(&_ip));
|
||||
}
|
||||
uint16_t* raw6()
|
||||
{
|
||||
setV6();
|
||||
return reinterpret_cast<uint16_t*>(ip_2_ip6(&_ip));
|
||||
}
|
||||
|
||||
const uint16_t* raw6() const
|
||||
{
|
||||
return isV6() ? reinterpret_cast<const uint16_t*>(ip_2_ip6(&_ip)) : nullptr;
|
||||
}
|
||||
const uint16_t* raw6() const
|
||||
{
|
||||
return isV6()? reinterpret_cast<const uint16_t*>(ip_2_ip6(&_ip)): nullptr;
|
||||
}
|
||||
|
||||
// when not IPv6, ip_addr_t == ip4_addr_t so this one would be ambiguous
|
||||
// required otherwise
|
||||
operator const ip4_addr_t*() const
|
||||
{
|
||||
return isV4() ? ip_2_ip4(&_ip) : nullptr;
|
||||
}
|
||||
// when not IPv6, ip_addr_t == ip4_addr_t so this one would be ambiguous
|
||||
// required otherwise
|
||||
operator const ip4_addr_t*() const { return isV4()? ip_2_ip4(&_ip): nullptr; }
|
||||
|
||||
bool isV6() const
|
||||
{
|
||||
return IP_IS_V6_VAL(_ip);
|
||||
}
|
||||
void setV6()
|
||||
{
|
||||
IP_SET_TYPE_VAL(_ip, IPADDR_TYPE_V6);
|
||||
}
|
||||
bool isV6() const { return IP_IS_V6_VAL(_ip); }
|
||||
void setV6() { IP_SET_TYPE_VAL(_ip, IPADDR_TYPE_V6); }
|
||||
|
||||
protected:
|
||||
bool fromString6(const char *address);
|
||||
protected:
|
||||
bool fromString6(const char *address);
|
||||
|
||||
#else
|
||||
|
||||
// allow portable code when IPv6 is not enabled
|
||||
// allow portable code when IPv6 is not enabled
|
||||
|
||||
uint16_t* raw6()
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
const uint16_t* raw6() const
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
bool isV6() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
void setV6() { }
|
||||
uint16_t* raw6() { return nullptr; }
|
||||
const uint16_t* raw6() const { return nullptr; }
|
||||
bool isV6() const { return false; }
|
||||
void setV6() { }
|
||||
|
||||
#endif
|
||||
|
||||
protected:
|
||||
bool fromString4(const char *address);
|
||||
protected:
|
||||
bool fromString4(const char *address);
|
||||
|
||||
};
|
||||
|
||||
|
@ -1,72 +1,60 @@
|
||||
#include <Arduino.h>
|
||||
#include <MD5Builder.h>
|
||||
|
||||
uint8_t hex_char_to_byte(uint8_t c)
|
||||
{
|
||||
return (c >= 'a' && c <= 'f') ? (c - ((uint8_t)'a' - 0xa)) :
|
||||
(c >= 'A' && c <= 'F') ? (c - ((uint8_t)'A' - 0xA)) :
|
||||
(c >= '0' && c <= '9') ? (c - (uint8_t)'0') : 0;
|
||||
uint8_t hex_char_to_byte(uint8_t c){
|
||||
return (c >= 'a' && c <= 'f') ? (c - ((uint8_t)'a' - 0xa)) :
|
||||
(c >= 'A' && c <= 'F') ? (c - ((uint8_t)'A' - 0xA)) :
|
||||
(c >= '0' && c<= '9') ? (c - (uint8_t)'0') : 0;
|
||||
}
|
||||
|
||||
void MD5Builder::begin(void)
|
||||
{
|
||||
void MD5Builder::begin(void){
|
||||
memset(_buf, 0x00, 16);
|
||||
MD5Init(&_ctx);
|
||||
}
|
||||
|
||||
void MD5Builder::add(const uint8_t * data, const uint16_t len)
|
||||
{
|
||||
void MD5Builder::add(const uint8_t * data, const uint16_t len){
|
||||
MD5Update(&_ctx, data, len);
|
||||
}
|
||||
|
||||
void MD5Builder::addHexString(const char * data)
|
||||
{
|
||||
void MD5Builder::addHexString(const char * data){
|
||||
uint16_t i, len = strlen(data);
|
||||
uint8_t * tmp = (uint8_t*)malloc(len / 2);
|
||||
if (tmp == NULL)
|
||||
{
|
||||
uint8_t * tmp = (uint8_t*)malloc(len/2);
|
||||
if(tmp == NULL) {
|
||||
return;
|
||||
}
|
||||
for (i = 0; i < len; i += 2)
|
||||
{
|
||||
for(i=0; i<len; i+=2) {
|
||||
uint8_t high = hex_char_to_byte(data[i]);
|
||||
uint8_t low = hex_char_to_byte(data[i + 1]);
|
||||
tmp[i / 2] = (high & 0x0F) << 4 | (low & 0x0F);
|
||||
uint8_t low = hex_char_to_byte(data[i+1]);
|
||||
tmp[i/2] = (high & 0x0F) << 4 | (low & 0x0F);
|
||||
}
|
||||
add(tmp, len / 2);
|
||||
add(tmp, len/2);
|
||||
free(tmp);
|
||||
}
|
||||
|
||||
bool MD5Builder::addStream(Stream & stream, const size_t maxLen)
|
||||
{
|
||||
bool MD5Builder::addStream(Stream & stream, const size_t maxLen){
|
||||
const int buf_size = 512;
|
||||
int maxLengthLeft = maxLen;
|
||||
uint8_t * buf = (uint8_t*) malloc(buf_size);
|
||||
|
||||
if (!buf)
|
||||
{
|
||||
if(!buf) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int bytesAvailable = stream.available();
|
||||
while ((bytesAvailable > 0) && (maxLengthLeft > 0))
|
||||
{
|
||||
while((bytesAvailable > 0) && (maxLengthLeft > 0)) {
|
||||
|
||||
// determine number of bytes to read
|
||||
int readBytes = bytesAvailable;
|
||||
if (readBytes > maxLengthLeft)
|
||||
{
|
||||
if(readBytes > maxLengthLeft) {
|
||||
readBytes = maxLengthLeft ; // read only until max_len
|
||||
}
|
||||
if (readBytes > buf_size)
|
||||
{
|
||||
if(readBytes > buf_size) {
|
||||
readBytes = buf_size; // not read more the buffer can handle
|
||||
}
|
||||
|
||||
// read data and check if we got something
|
||||
int numBytesRead = stream.readBytes(buf, readBytes);
|
||||
if (numBytesRead < 1)
|
||||
{
|
||||
if(numBytesRead< 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -83,26 +71,21 @@ bool MD5Builder::addStream(Stream & stream, const size_t maxLen)
|
||||
return true;
|
||||
}
|
||||
|
||||
void MD5Builder::calculate(void)
|
||||
{
|
||||
void MD5Builder::calculate(void){
|
||||
MD5Final(_buf, &_ctx);
|
||||
}
|
||||
|
||||
void MD5Builder::getBytes(uint8_t * output)
|
||||
{
|
||||
void MD5Builder::getBytes(uint8_t * output){
|
||||
memcpy(output, _buf, 16);
|
||||
}
|
||||
|
||||
void MD5Builder::getChars(char * output)
|
||||
{
|
||||
for (uint8_t i = 0; i < 16; i++)
|
||||
{
|
||||
void MD5Builder::getChars(char * output){
|
||||
for(uint8_t i = 0; i < 16; i++) {
|
||||
sprintf(output + (i * 2), "%02x", _buf[i]);
|
||||
}
|
||||
}
|
||||
|
||||
String MD5Builder::toString(void)
|
||||
{
|
||||
String MD5Builder::toString(void){
|
||||
char out[33];
|
||||
getChars(out);
|
||||
return String(out);
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
md5.h - exposed md5 ROM functions for esp8266
|
||||
md5.h - exposed md5 ROM functions for esp8266
|
||||
|
||||
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2015 Hristo Gochkov. 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 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.
|
||||
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
|
||||
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
|
||||
*/
|
||||
#ifndef __ESP8266_MD5_BUILDER__
|
||||
#define __ESP8266_MD5_BUILDER__
|
||||
@ -25,35 +25,19 @@
|
||||
#include <Stream.h>
|
||||
#include "md5.h"
|
||||
|
||||
class MD5Builder
|
||||
{
|
||||
private:
|
||||
class MD5Builder {
|
||||
private:
|
||||
md5_context_t _ctx;
|
||||
uint8_t _buf[16];
|
||||
public:
|
||||
public:
|
||||
void begin(void);
|
||||
void add(const uint8_t * data, const uint16_t len);
|
||||
void add(const char * data)
|
||||
{
|
||||
add((const uint8_t*)data, strlen(data));
|
||||
}
|
||||
void add(char * data)
|
||||
{
|
||||
add((const char*)data);
|
||||
}
|
||||
void add(const String& data)
|
||||
{
|
||||
add(data.c_str());
|
||||
}
|
||||
void add(const char * data){ add((const uint8_t*)data, strlen(data)); }
|
||||
void add(char * data){ add((const char*)data); }
|
||||
void add(const String& data){ add(data.c_str()); }
|
||||
void addHexString(const char * data);
|
||||
void addHexString(char * data)
|
||||
{
|
||||
addHexString((const char*)data);
|
||||
}
|
||||
void addHexString(const String& data)
|
||||
{
|
||||
addHexString(data.c_str());
|
||||
}
|
||||
void addHexString(char * data){ addHexString((const char*)data); }
|
||||
void addHexString(const String& data){ addHexString(data.c_str()); }
|
||||
bool addStream(Stream & stream, const size_t maxLen);
|
||||
void calculate(void);
|
||||
void getBytes(uint8_t * output);
|
||||
|
@ -3,25 +3,25 @@
|
||||
|
||||
|
||||
/*
|
||||
PolledTimeout.h - Encapsulation of a polled Timeout
|
||||
PolledTimeout.h - Encapsulation of a polled Timeout
|
||||
|
||||
Copyright (c) 2018 Daniel Salazar. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2018 Daniel Salazar. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#include <limits>
|
||||
|
||||
@ -39,24 +39,18 @@ namespace YieldPolicy
|
||||
|
||||
struct DoNothing
|
||||
{
|
||||
static void execute() {}
|
||||
static void execute() {}
|
||||
};
|
||||
|
||||
struct YieldOrSkip
|
||||
{
|
||||
static void execute()
|
||||
{
|
||||
delay(0);
|
||||
}
|
||||
static void execute() {delay(0);}
|
||||
};
|
||||
|
||||
template <unsigned long delayMs>
|
||||
struct YieldAndDelayMs
|
||||
{
|
||||
static void execute()
|
||||
{
|
||||
delay(delayMs);
|
||||
}
|
||||
static void execute() {delay(delayMs);}
|
||||
};
|
||||
|
||||
} //YieldPolicy
|
||||
@ -66,91 +60,74 @@ namespace TimePolicy
|
||||
|
||||
struct TimeSourceMillis
|
||||
{
|
||||
// time policy in milli-seconds based on millis()
|
||||
// time policy in milli-seconds based on millis()
|
||||
|
||||
using timeType = decltype(millis());
|
||||
static timeType time()
|
||||
{
|
||||
return millis();
|
||||
}
|
||||
static constexpr timeType ticksPerSecond = 1000;
|
||||
static constexpr timeType ticksPerSecondMax = 1000;
|
||||
using timeType = decltype(millis());
|
||||
static timeType time() {return millis();}
|
||||
static constexpr timeType ticksPerSecond = 1000;
|
||||
static constexpr timeType ticksPerSecondMax = 1000;
|
||||
};
|
||||
|
||||
struct TimeSourceCycles
|
||||
{
|
||||
// time policy based on ESP.getCycleCount()
|
||||
// this particular time measurement is intended to be called very often
|
||||
// (every loop, every yield)
|
||||
// time policy based on ESP.getCycleCount()
|
||||
// this particular time measurement is intended to be called very often
|
||||
// (every loop, every yield)
|
||||
|
||||
using timeType = decltype(ESP.getCycleCount());
|
||||
static timeType time()
|
||||
{
|
||||
return ESP.getCycleCount();
|
||||
}
|
||||
static constexpr timeType ticksPerSecond = F_CPU; // 80'000'000 or 160'000'000 Hz
|
||||
static constexpr timeType ticksPerSecondMax = 160000000; // 160MHz
|
||||
using timeType = decltype(ESP.getCycleCount());
|
||||
static timeType time() {return ESP.getCycleCount();}
|
||||
static constexpr timeType ticksPerSecond = F_CPU; // 80'000'000 or 160'000'000 Hz
|
||||
static constexpr timeType ticksPerSecondMax = 160000000; // 160MHz
|
||||
};
|
||||
|
||||
template <typename TimeSourceType, unsigned long long second_th>
|
||||
// "second_th" units of timeType for one second
|
||||
// "second_th" units of timeType for one second
|
||||
struct TimeUnit
|
||||
{
|
||||
using timeType = typename TimeSourceType::timeType;
|
||||
using timeType = typename TimeSourceType::timeType;
|
||||
|
||||
#if __GNUC__ < 5
|
||||
// gcc-4.8 cannot compile the constexpr-only version of this function
|
||||
// using #defines instead luckily works
|
||||
static constexpr timeType computeRangeCompensation()
|
||||
{
|
||||
#define number_of_secondTh_in_one_tick ((1.0 * second_th) / ticksPerSecond)
|
||||
#define fractional (number_of_secondTh_in_one_tick - (long)number_of_secondTh_in_one_tick)
|
||||
// gcc-4.8 cannot compile the constexpr-only version of this function
|
||||
// using #defines instead luckily works
|
||||
static constexpr timeType computeRangeCompensation ()
|
||||
{
|
||||
#define number_of_secondTh_in_one_tick ((1.0 * second_th) / ticksPerSecond)
|
||||
#define fractional (number_of_secondTh_in_one_tick - (long)number_of_secondTh_in_one_tick)
|
||||
|
||||
return (
|
||||
{
|
||||
fractional == 0 ?
|
||||
1 : // no need for compensation
|
||||
(number_of_secondTh_in_one_tick / fractional) + 0.5; // scalar multiplier allowing exact division
|
||||
});
|
||||
return ({
|
||||
fractional == 0?
|
||||
1: // no need for compensation
|
||||
(number_of_secondTh_in_one_tick / fractional) + 0.5; // scalar multiplier allowing exact division
|
||||
});
|
||||
|
||||
#undef number_of_secondTh_in_one_tick
|
||||
#undef fractional
|
||||
}
|
||||
#undef number_of_secondTh_in_one_tick
|
||||
#undef fractional
|
||||
}
|
||||
#else
|
||||
static constexpr timeType computeRangeCompensation()
|
||||
{
|
||||
return (
|
||||
{
|
||||
constexpr double number_of_secondTh_in_one_tick = (1.0 * second_th) / ticksPerSecond;
|
||||
constexpr double fractional = number_of_secondTh_in_one_tick - (long)number_of_secondTh_in_one_tick;
|
||||
fractional == 0 ?
|
||||
1 : // no need for compensation
|
||||
(number_of_secondTh_in_one_tick / fractional) + 0.5; // scalar multiplier allowing exact division
|
||||
});
|
||||
}
|
||||
static constexpr timeType computeRangeCompensation ()
|
||||
{
|
||||
return ({
|
||||
constexpr double number_of_secondTh_in_one_tick = (1.0 * second_th) / ticksPerSecond;
|
||||
constexpr double fractional = number_of_secondTh_in_one_tick - (long)number_of_secondTh_in_one_tick;
|
||||
fractional == 0?
|
||||
1: // no need for compensation
|
||||
(number_of_secondTh_in_one_tick / fractional) + 0.5; // scalar multiplier allowing exact division
|
||||
});
|
||||
}
|
||||
#endif
|
||||
|
||||
static constexpr timeType ticksPerSecond = TimeSourceType::ticksPerSecond;
|
||||
static constexpr timeType ticksPerSecondMax = TimeSourceType::ticksPerSecondMax;
|
||||
static constexpr timeType rangeCompensate = computeRangeCompensation();
|
||||
static constexpr timeType user2UnitMultiplierMax = (ticksPerSecondMax * rangeCompensate) / second_th;
|
||||
static constexpr timeType user2UnitMultiplier = (ticksPerSecond * rangeCompensate) / second_th;
|
||||
static constexpr timeType user2UnitDivider = rangeCompensate;
|
||||
// std::numeric_limits<timeType>::max() is reserved
|
||||
static constexpr timeType timeMax = (std::numeric_limits<timeType>::max() - 1) / user2UnitMultiplierMax;
|
||||
static constexpr timeType ticksPerSecond = TimeSourceType::ticksPerSecond;
|
||||
static constexpr timeType ticksPerSecondMax = TimeSourceType::ticksPerSecondMax;
|
||||
static constexpr timeType rangeCompensate = computeRangeCompensation();
|
||||
static constexpr timeType user2UnitMultiplierMax = (ticksPerSecondMax * rangeCompensate) / second_th;
|
||||
static constexpr timeType user2UnitMultiplier = (ticksPerSecond * rangeCompensate) / second_th;
|
||||
static constexpr timeType user2UnitDivider = rangeCompensate;
|
||||
// std::numeric_limits<timeType>::max() is reserved
|
||||
static constexpr timeType timeMax = (std::numeric_limits<timeType>::max() - 1) / user2UnitMultiplierMax;
|
||||
|
||||
static timeType toTimeTypeUnit(const timeType userUnit)
|
||||
{
|
||||
return (userUnit * user2UnitMultiplier) / user2UnitDivider;
|
||||
}
|
||||
static timeType toUserUnit(const timeType internalUnit)
|
||||
{
|
||||
return (internalUnit * user2UnitDivider) / user2UnitMultiplier;
|
||||
}
|
||||
static timeType time()
|
||||
{
|
||||
return TimeSourceType::time();
|
||||
}
|
||||
static timeType toTimeTypeUnit (const timeType userUnit) {return (userUnit * user2UnitMultiplier) / user2UnitDivider;}
|
||||
static timeType toUserUnit (const timeType internalUnit) {return (internalUnit * user2UnitDivider) / user2UnitMultiplier;}
|
||||
static timeType time () {return TimeSourceType::time();}
|
||||
};
|
||||
|
||||
using TimeMillis = TimeUnit< TimeSourceMillis, 1000 >;
|
||||
@ -164,113 +141,109 @@ template <bool PeriodicT, typename YieldPolicyT = YieldPolicy::DoNothing, typena
|
||||
class timeoutTemplate
|
||||
{
|
||||
public:
|
||||
using timeType = typename TimePolicyT::timeType;
|
||||
static_assert(std::is_unsigned<timeType>::value == true, "timeType must be unsigned");
|
||||
using timeType = typename TimePolicyT::timeType;
|
||||
static_assert(std::is_unsigned<timeType>::value == true, "timeType must be unsigned");
|
||||
|
||||
static constexpr timeType alwaysExpired = 0;
|
||||
static constexpr timeType neverExpires = std::numeric_limits<timeType>::max();
|
||||
static constexpr timeType rangeCompensate = TimePolicyT::rangeCompensate; //debug
|
||||
static constexpr timeType alwaysExpired = 0;
|
||||
static constexpr timeType neverExpires = std::numeric_limits<timeType>::max();
|
||||
static constexpr timeType rangeCompensate = TimePolicyT::rangeCompensate; //debug
|
||||
|
||||
timeoutTemplate(const timeType userTimeout)
|
||||
{
|
||||
reset(userTimeout);
|
||||
}
|
||||
timeoutTemplate(const timeType userTimeout)
|
||||
{
|
||||
reset(userTimeout);
|
||||
}
|
||||
|
||||
ICACHE_RAM_ATTR
|
||||
bool expired()
|
||||
{
|
||||
YieldPolicyT::execute(); //in case of DoNothing: gets optimized away
|
||||
if (PeriodicT) //in case of false: gets optimized away
|
||||
{
|
||||
return expiredRetrigger();
|
||||
}
|
||||
return expiredOneShot();
|
||||
}
|
||||
ICACHE_RAM_ATTR
|
||||
bool expired()
|
||||
{
|
||||
YieldPolicyT::execute(); //in case of DoNothing: gets optimized away
|
||||
if(PeriodicT) //in case of false: gets optimized away
|
||||
return expiredRetrigger();
|
||||
return expiredOneShot();
|
||||
}
|
||||
|
||||
ICACHE_RAM_ATTR
|
||||
operator bool()
|
||||
{
|
||||
return expired();
|
||||
}
|
||||
ICACHE_RAM_ATTR
|
||||
operator bool()
|
||||
{
|
||||
return expired();
|
||||
}
|
||||
|
||||
bool canExpire() const
|
||||
{
|
||||
return !_neverExpires;
|
||||
}
|
||||
bool canExpire () const
|
||||
{
|
||||
return !_neverExpires;
|
||||
}
|
||||
|
||||
bool canWait() const
|
||||
{
|
||||
return _timeout != alwaysExpired;
|
||||
}
|
||||
bool canWait () const
|
||||
{
|
||||
return _timeout != alwaysExpired;
|
||||
}
|
||||
|
||||
void reset(const timeType newUserTimeout)
|
||||
{
|
||||
reset();
|
||||
_timeout = TimePolicyT::toTimeTypeUnit(newUserTimeout);
|
||||
_neverExpires = (newUserTimeout < 0) || (newUserTimeout > timeMax());
|
||||
}
|
||||
void reset(const timeType newUserTimeout)
|
||||
{
|
||||
reset();
|
||||
_timeout = TimePolicyT::toTimeTypeUnit(newUserTimeout);
|
||||
_neverExpires = (newUserTimeout < 0) || (newUserTimeout > timeMax());
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
_start = TimePolicyT::time();
|
||||
}
|
||||
void reset()
|
||||
{
|
||||
_start = TimePolicyT::time();
|
||||
}
|
||||
|
||||
void resetToNeverExpires()
|
||||
{
|
||||
_timeout = alwaysExpired + 1; // because canWait() has precedence
|
||||
_neverExpires = true;
|
||||
}
|
||||
void resetToNeverExpires ()
|
||||
{
|
||||
_timeout = alwaysExpired + 1; // because canWait() has precedence
|
||||
_neverExpires = true;
|
||||
}
|
||||
|
||||
timeType getTimeout() const
|
||||
{
|
||||
return TimePolicyT::toUserUnit(_timeout);
|
||||
}
|
||||
timeType getTimeout() const
|
||||
{
|
||||
return TimePolicyT::toUserUnit(_timeout);
|
||||
}
|
||||
|
||||
static constexpr timeType timeMax()
|
||||
{
|
||||
return TimePolicyT::timeMax;
|
||||
}
|
||||
static constexpr timeType timeMax()
|
||||
{
|
||||
return TimePolicyT::timeMax;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
ICACHE_RAM_ATTR
|
||||
bool checkExpired(const timeType internalUnit) const
|
||||
{
|
||||
// canWait() is not checked here
|
||||
// returns "can expire" and "time expired"
|
||||
return (!_neverExpires) && ((internalUnit - _start) >= _timeout);
|
||||
}
|
||||
ICACHE_RAM_ATTR
|
||||
bool checkExpired(const timeType internalUnit) const
|
||||
{
|
||||
// canWait() is not checked here
|
||||
// returns "can expire" and "time expired"
|
||||
return (!_neverExpires) && ((internalUnit - _start) >= _timeout);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
ICACHE_RAM_ATTR
|
||||
bool expiredRetrigger()
|
||||
ICACHE_RAM_ATTR
|
||||
bool expiredRetrigger()
|
||||
{
|
||||
if (!canWait())
|
||||
return true;
|
||||
|
||||
timeType current = TimePolicyT::time();
|
||||
if(checkExpired(current))
|
||||
{
|
||||
if (!canWait())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
timeType current = TimePolicyT::time();
|
||||
if (checkExpired(current))
|
||||
{
|
||||
unsigned long n = (current - _start) / _timeout; //how many _timeouts periods have elapsed, will usually be 1 (current - _start >= _timeout)
|
||||
_start += n * _timeout;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
unsigned long n = (current - _start) / _timeout; //how many _timeouts periods have elapsed, will usually be 1 (current - _start >= _timeout)
|
||||
_start += n * _timeout;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
ICACHE_RAM_ATTR
|
||||
bool expiredOneShot() const
|
||||
{
|
||||
// returns "always expired" or "has expired"
|
||||
return !canWait() || checkExpired(TimePolicyT::time());
|
||||
}
|
||||
ICACHE_RAM_ATTR
|
||||
bool expiredOneShot() const
|
||||
{
|
||||
// returns "always expired" or "has expired"
|
||||
return !canWait() || checkExpired(TimePolicyT::time());
|
||||
}
|
||||
|
||||
timeType _timeout;
|
||||
timeType _start;
|
||||
bool _neverExpires;
|
||||
timeType _timeout;
|
||||
timeType _start;
|
||||
bool _neverExpires;
|
||||
};
|
||||
|
||||
// legacy type names, deprecated (unit is milliseconds)
|
||||
@ -303,11 +276,11 @@ using periodicFastNs = polledTimeout::timeoutTemplate<true, YieldPolicy::DoNothi
|
||||
} //polledTimeout
|
||||
|
||||
|
||||
/* A 1-shot timeout that auto-yields when in CONT can be built as follows:
|
||||
using oneShotYieldMs = esp8266::polledTimeout::timeoutTemplate<false, esp8266::polledTimeout::YieldPolicy::YieldOrSkip>;
|
||||
|
||||
Other policies can be implemented by the user, e.g.: simple yield that panics in SYS, and the polledTimeout types built as needed as shown above, without modifying this file.
|
||||
*/
|
||||
/* A 1-shot timeout that auto-yields when in CONT can be built as follows:
|
||||
* using oneShotYieldMs = esp8266::polledTimeout::timeoutTemplate<false, esp8266::polledTimeout::YieldPolicy::YieldOrSkip>;
|
||||
*
|
||||
* Other policies can be implemented by the user, e.g.: simple yield that panics in SYS, and the polledTimeout types built as needed as shown above, without modifying this file.
|
||||
*/
|
||||
|
||||
}//esp8266
|
||||
|
||||
|
@ -1,25 +1,25 @@
|
||||
/*
|
||||
Print.cpp - Base class that provides print() and println()
|
||||
Copyright (c) 2008 David A. Mellis. All right reserved.
|
||||
Print.cpp - Base class that provides print() and println()
|
||||
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 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.
|
||||
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
|
||||
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
|
||||
|
||||
Modified 23 November 2006 by David A. Mellis
|
||||
Modified December 2014 by Ivan Grokhotkov
|
||||
Modified May 2015 by Michael C. Miller - esp8266 progmem support
|
||||
*/
|
||||
Modified 23 November 2006 by David A. Mellis
|
||||
Modified December 2014 by Ivan Grokhotkov
|
||||
Modified May 2015 by Michael C. Miller - esp8266 progmem support
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
@ -32,25 +32,21 @@
|
||||
// Public Methods //////////////////////////////////////////////////////////////
|
||||
|
||||
/* default implementation: may be overridden */
|
||||
size_t Print::write(const uint8_t *buffer, size_t size)
|
||||
{
|
||||
size_t Print::write(const uint8_t *buffer, size_t size) {
|
||||
|
||||
#ifdef DEBUG_ESP_CORE
|
||||
static char not_the_best_way [] PROGMEM STORE_ATTR = "Print::write(data,len) should be overridden for better efficiency\r\n";
|
||||
static bool once = false;
|
||||
if (!once)
|
||||
{
|
||||
if (!once) {
|
||||
once = true;
|
||||
os_printf_plus(not_the_best_way);
|
||||
}
|
||||
#endif
|
||||
|
||||
size_t n = 0;
|
||||
while (size--)
|
||||
{
|
||||
while (size--) {
|
||||
size_t ret = write(*buffer++);
|
||||
if (ret == 0)
|
||||
{
|
||||
if (ret == 0) {
|
||||
// Write of last byte didn't complete, abort additional processing
|
||||
break;
|
||||
}
|
||||
@ -59,19 +55,16 @@ size_t Print::write(const uint8_t *buffer, size_t size)
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::printf(const char *format, ...)
|
||||
{
|
||||
size_t Print::printf(const char *format, ...) {
|
||||
va_list arg;
|
||||
va_start(arg, format);
|
||||
char temp[64];
|
||||
char* buffer = temp;
|
||||
size_t len = vsnprintf(temp, sizeof(temp), format, arg);
|
||||
va_end(arg);
|
||||
if (len > sizeof(temp) - 1)
|
||||
{
|
||||
if (len > sizeof(temp) - 1) {
|
||||
buffer = new char[len + 1];
|
||||
if (!buffer)
|
||||
{
|
||||
if (!buffer) {
|
||||
return 0;
|
||||
}
|
||||
va_start(arg, format);
|
||||
@ -79,26 +72,22 @@ size_t Print::printf(const char *format, ...)
|
||||
va_end(arg);
|
||||
}
|
||||
len = write((const uint8_t*) buffer, len);
|
||||
if (buffer != temp)
|
||||
{
|
||||
if (buffer != temp) {
|
||||
delete[] buffer;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
size_t Print::printf_P(PGM_P format, ...)
|
||||
{
|
||||
size_t Print::printf_P(PGM_P format, ...) {
|
||||
va_list arg;
|
||||
va_start(arg, format);
|
||||
char temp[64];
|
||||
char* buffer = temp;
|
||||
size_t len = vsnprintf_P(temp, sizeof(temp), format, arg);
|
||||
va_end(arg);
|
||||
if (len > sizeof(temp) - 1)
|
||||
{
|
||||
if (len > sizeof(temp) - 1) {
|
||||
buffer = new char[len + 1];
|
||||
if (!buffer)
|
||||
{
|
||||
if (!buffer) {
|
||||
return 0;
|
||||
}
|
||||
va_start(arg, format);
|
||||
@ -106,181 +95,143 @@ size_t Print::printf_P(PGM_P format, ...)
|
||||
va_end(arg);
|
||||
}
|
||||
len = write((const uint8_t*) buffer, len);
|
||||
if (buffer != temp)
|
||||
{
|
||||
if (buffer != temp) {
|
||||
delete[] buffer;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
size_t Print::print(const __FlashStringHelper *ifsh)
|
||||
{
|
||||
size_t Print::print(const __FlashStringHelper *ifsh) {
|
||||
PGM_P p = reinterpret_cast<PGM_P>(ifsh);
|
||||
|
||||
size_t n = 0;
|
||||
while (1)
|
||||
{
|
||||
while (1) {
|
||||
uint8_t c = pgm_read_byte(p++);
|
||||
if (c == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (c == 0) break;
|
||||
n += write(c);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::print(const String &s)
|
||||
{
|
||||
size_t Print::print(const String &s) {
|
||||
return write(s.c_str(), s.length());
|
||||
}
|
||||
|
||||
size_t Print::print(const char str[])
|
||||
{
|
||||
size_t Print::print(const char str[]) {
|
||||
return write(str);
|
||||
}
|
||||
|
||||
size_t Print::print(char c)
|
||||
{
|
||||
size_t Print::print(char c) {
|
||||
return write(c);
|
||||
}
|
||||
|
||||
size_t Print::print(unsigned char b, int base)
|
||||
{
|
||||
size_t Print::print(unsigned char b, int base) {
|
||||
return print((unsigned long) b, base);
|
||||
}
|
||||
|
||||
size_t Print::print(int n, int base)
|
||||
{
|
||||
size_t Print::print(int n, int base) {
|
||||
return print((long) n, base);
|
||||
}
|
||||
|
||||
size_t Print::print(unsigned int n, int base)
|
||||
{
|
||||
size_t Print::print(unsigned int n, int base) {
|
||||
return print((unsigned long) n, base);
|
||||
}
|
||||
|
||||
size_t Print::print(long n, int base)
|
||||
{
|
||||
if (base == 0)
|
||||
{
|
||||
size_t Print::print(long n, int base) {
|
||||
if(base == 0) {
|
||||
return write(n);
|
||||
}
|
||||
else if (base == 10)
|
||||
{
|
||||
if (n < 0)
|
||||
{
|
||||
} else if(base == 10) {
|
||||
if(n < 0) {
|
||||
int t = print('-');
|
||||
n = -n;
|
||||
return printNumber(n, 10) + t;
|
||||
}
|
||||
return printNumber(n, 10);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
return printNumber(n, base);
|
||||
}
|
||||
}
|
||||
|
||||
size_t Print::print(unsigned long n, int base)
|
||||
{
|
||||
if (base == 0)
|
||||
{
|
||||
size_t Print::print(unsigned long n, int base) {
|
||||
if(base == 0)
|
||||
return write(n);
|
||||
}
|
||||
else
|
||||
{
|
||||
return printNumber(n, base);
|
||||
}
|
||||
}
|
||||
|
||||
size_t Print::print(double n, int digits)
|
||||
{
|
||||
size_t Print::print(double n, int digits) {
|
||||
return printFloat(n, digits);
|
||||
}
|
||||
|
||||
size_t Print::println(const __FlashStringHelper *ifsh)
|
||||
{
|
||||
size_t Print::println(const __FlashStringHelper *ifsh) {
|
||||
size_t n = print(ifsh);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::print(const Printable& x)
|
||||
{
|
||||
size_t Print::print(const Printable& x) {
|
||||
return x.printTo(*this);
|
||||
}
|
||||
|
||||
size_t Print::println(void)
|
||||
{
|
||||
size_t Print::println(void) {
|
||||
return print("\r\n");
|
||||
}
|
||||
|
||||
size_t Print::println(const String &s)
|
||||
{
|
||||
size_t Print::println(const String &s) {
|
||||
size_t n = print(s);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(const char c[])
|
||||
{
|
||||
size_t Print::println(const char c[]) {
|
||||
size_t n = print(c);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(char c)
|
||||
{
|
||||
size_t Print::println(char c) {
|
||||
size_t n = print(c);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(unsigned char b, int base)
|
||||
{
|
||||
size_t Print::println(unsigned char b, int base) {
|
||||
size_t n = print(b, base);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(int num, int base)
|
||||
{
|
||||
size_t Print::println(int num, int base) {
|
||||
size_t n = print(num, base);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(unsigned int num, int base)
|
||||
{
|
||||
size_t Print::println(unsigned int num, int base) {
|
||||
size_t n = print(num, base);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(long num, int base)
|
||||
{
|
||||
size_t Print::println(long num, int base) {
|
||||
size_t n = print(num, base);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(unsigned long num, int base)
|
||||
{
|
||||
size_t Print::println(unsigned long num, int base) {
|
||||
size_t n = print(num, base);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(double num, int digits)
|
||||
{
|
||||
size_t Print::println(double num, int digits) {
|
||||
size_t n = print(num, digits);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(const Printable& x)
|
||||
{
|
||||
size_t Print::println(const Printable& x) {
|
||||
size_t n = print(x);
|
||||
n += println();
|
||||
return n;
|
||||
@ -288,64 +239,48 @@ size_t Print::println(const Printable& x)
|
||||
|
||||
// Private Methods /////////////////////////////////////////////////////////////
|
||||
|
||||
size_t Print::printNumber(unsigned long n, uint8_t base)
|
||||
{
|
||||
size_t Print::printNumber(unsigned long n, uint8_t base) {
|
||||
char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
|
||||
char *str = &buf[sizeof(buf) - 1];
|
||||
|
||||
*str = '\0';
|
||||
|
||||
// prevent crash if called with base == 1
|
||||
if (base < 2)
|
||||
{
|
||||
if(base < 2)
|
||||
base = 10;
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
do {
|
||||
unsigned long m = n;
|
||||
n /= base;
|
||||
char c = m - base * n;
|
||||
*--str = c < 10 ? c + '0' : c + 'A' - 10;
|
||||
} while (n);
|
||||
} while(n);
|
||||
|
||||
return write(str);
|
||||
}
|
||||
|
||||
size_t Print::printFloat(double number, uint8_t digits)
|
||||
{
|
||||
size_t Print::printFloat(double number, uint8_t digits) {
|
||||
size_t n = 0;
|
||||
|
||||
if (isnan(number))
|
||||
{
|
||||
if(isnan(number))
|
||||
return print("nan");
|
||||
}
|
||||
if (isinf(number))
|
||||
{
|
||||
if(isinf(number))
|
||||
return print("inf");
|
||||
}
|
||||
if (number > 4294967040.0)
|
||||
{
|
||||
return print("ovf"); // constant determined empirically
|
||||
}
|
||||
if (number < -4294967040.0)
|
||||
{
|
||||
return print("ovf"); // constant determined empirically
|
||||
}
|
||||
if(number > 4294967040.0)
|
||||
return print("ovf"); // constant determined empirically
|
||||
if(number < -4294967040.0)
|
||||
return print("ovf"); // constant determined empirically
|
||||
|
||||
// Handle negative numbers
|
||||
if (number < 0.0)
|
||||
{
|
||||
if(number < 0.0) {
|
||||
n += print('-');
|
||||
number = -number;
|
||||
}
|
||||
|
||||
// Round correctly so that print(1.999, 2) prints as "2.00"
|
||||
double rounding = 0.5;
|
||||
for (uint8_t i = 0; i < digits; ++i)
|
||||
{
|
||||
for(uint8_t i = 0; i < digits; ++i)
|
||||
rounding /= 10.0;
|
||||
}
|
||||
|
||||
number += rounding;
|
||||
|
||||
@ -355,14 +290,12 @@ size_t Print::printFloat(double number, uint8_t digits)
|
||||
n += print(int_part);
|
||||
|
||||
// Print the decimal point, but only if there are digits beyond
|
||||
if (digits > 0)
|
||||
{
|
||||
if(digits > 0) {
|
||||
n += print(".");
|
||||
}
|
||||
|
||||
// Extract digits from the remainder one at a time
|
||||
while (digits-- > 0)
|
||||
{
|
||||
while(digits-- > 0) {
|
||||
remainder *= 10.0;
|
||||
int toPrint = int(remainder);
|
||||
n += print(toPrint);
|
||||
|
@ -1,21 +1,21 @@
|
||||
/*
|
||||
Print.h - Base class that provides print() and println()
|
||||
Copyright (c) 2008 David A. Mellis. All right reserved.
|
||||
Print.h - Base class that provides print() and println()
|
||||
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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef Print_h
|
||||
#define Print_h
|
||||
@ -31,100 +31,73 @@
|
||||
#define OCT 8
|
||||
#define BIN 2
|
||||
|
||||
class Print
|
||||
{
|
||||
private:
|
||||
int write_error;
|
||||
size_t printNumber(unsigned long, uint8_t);
|
||||
size_t printFloat(double, uint8_t);
|
||||
protected:
|
||||
void setWriteError(int err = 1)
|
||||
{
|
||||
write_error = err;
|
||||
}
|
||||
public:
|
||||
Print() :
|
||||
write_error(0)
|
||||
{
|
||||
}
|
||||
|
||||
int getWriteError()
|
||||
{
|
||||
return write_error;
|
||||
}
|
||||
void clearWriteError()
|
||||
{
|
||||
setWriteError(0);
|
||||
}
|
||||
|
||||
virtual size_t write(uint8_t) = 0;
|
||||
size_t write(const char *str)
|
||||
{
|
||||
if (str == NULL)
|
||||
{
|
||||
return 0;
|
||||
class Print {
|
||||
private:
|
||||
int write_error;
|
||||
size_t printNumber(unsigned long, uint8_t);
|
||||
size_t printFloat(double, uint8_t);
|
||||
protected:
|
||||
void setWriteError(int err = 1) {
|
||||
write_error = err;
|
||||
}
|
||||
public:
|
||||
Print() :
|
||||
write_error(0) {
|
||||
}
|
||||
return write((const uint8_t *) str, strlen(str));
|
||||
}
|
||||
virtual size_t write(const uint8_t *buffer, size_t size);
|
||||
size_t write(const char *buffer, size_t size)
|
||||
{
|
||||
return write((const uint8_t *) buffer, size);
|
||||
}
|
||||
// These handle ambiguity for write(0) case, because (0) can be a pointer or an integer
|
||||
size_t write(short t)
|
||||
{
|
||||
return write((uint8_t)t);
|
||||
}
|
||||
size_t write(unsigned short t)
|
||||
{
|
||||
return write((uint8_t)t);
|
||||
}
|
||||
size_t write(int t)
|
||||
{
|
||||
return write((uint8_t)t);
|
||||
}
|
||||
size_t write(unsigned int t)
|
||||
{
|
||||
return write((uint8_t)t);
|
||||
}
|
||||
size_t write(long t)
|
||||
{
|
||||
return write((uint8_t)t);
|
||||
}
|
||||
size_t write(unsigned long t)
|
||||
{
|
||||
return write((uint8_t)t);
|
||||
}
|
||||
|
||||
size_t printf(const char * format, ...) __attribute__((format(printf, 2, 3)));
|
||||
size_t printf_P(PGM_P format, ...) __attribute__((format(printf, 2, 3)));
|
||||
size_t print(const __FlashStringHelper *);
|
||||
size_t print(const String &);
|
||||
size_t print(const char[]);
|
||||
size_t print(char);
|
||||
size_t print(unsigned char, int = DEC);
|
||||
size_t print(int, int = DEC);
|
||||
size_t print(unsigned int, int = DEC);
|
||||
size_t print(long, int = DEC);
|
||||
size_t print(unsigned long, int = DEC);
|
||||
size_t print(double, int = 2);
|
||||
size_t print(const Printable&);
|
||||
int getWriteError() {
|
||||
return write_error;
|
||||
}
|
||||
void clearWriteError() {
|
||||
setWriteError(0);
|
||||
}
|
||||
|
||||
size_t println(const __FlashStringHelper *);
|
||||
size_t println(const String &s);
|
||||
size_t println(const char[]);
|
||||
size_t println(char);
|
||||
size_t println(unsigned char, int = DEC);
|
||||
size_t println(int, int = DEC);
|
||||
size_t println(unsigned int, int = DEC);
|
||||
size_t println(long, int = DEC);
|
||||
size_t println(unsigned long, int = DEC);
|
||||
size_t println(double, int = 2);
|
||||
size_t println(const Printable&);
|
||||
size_t println(void);
|
||||
virtual size_t write(uint8_t) = 0;
|
||||
size_t write(const char *str) {
|
||||
if(str == NULL)
|
||||
return 0;
|
||||
return write((const uint8_t *) str, strlen(str));
|
||||
}
|
||||
virtual size_t write(const uint8_t *buffer, size_t size);
|
||||
size_t write(const char *buffer, size_t size) {
|
||||
return write((const uint8_t *) buffer, size);
|
||||
}
|
||||
// These handle ambiguity for write(0) case, because (0) can be a pointer or an integer
|
||||
size_t write(short t) { return write((uint8_t)t); }
|
||||
size_t write(unsigned short t) { return write((uint8_t)t); }
|
||||
size_t write(int t) { return write((uint8_t)t); }
|
||||
size_t write(unsigned int t) { return write((uint8_t)t); }
|
||||
size_t write(long t) { return write((uint8_t)t); }
|
||||
size_t write(unsigned long t) { return write((uint8_t)t); }
|
||||
|
||||
virtual void flush() { /* Empty implementation for backward compatibility */ }
|
||||
size_t printf(const char * format, ...) __attribute__ ((format (printf, 2, 3)));
|
||||
size_t printf_P(PGM_P format, ...) __attribute__((format(printf, 2, 3)));
|
||||
size_t print(const __FlashStringHelper *);
|
||||
size_t print(const String &);
|
||||
size_t print(const char[]);
|
||||
size_t print(char);
|
||||
size_t print(unsigned char, int = DEC);
|
||||
size_t print(int, int = DEC);
|
||||
size_t print(unsigned int, int = DEC);
|
||||
size_t print(long, int = DEC);
|
||||
size_t print(unsigned long, int = DEC);
|
||||
size_t print(double, int = 2);
|
||||
size_t print(const Printable&);
|
||||
|
||||
size_t println(const __FlashStringHelper *);
|
||||
size_t println(const String &s);
|
||||
size_t println(const char[]);
|
||||
size_t println(char);
|
||||
size_t println(unsigned char, int = DEC);
|
||||
size_t println(int, int = DEC);
|
||||
size_t println(unsigned int, int = DEC);
|
||||
size_t println(long, int = DEC);
|
||||
size_t println(unsigned long, int = DEC);
|
||||
size_t println(double, int = 2);
|
||||
size_t println(const Printable&);
|
||||
size_t println(void);
|
||||
|
||||
virtual void flush() { /* Empty implementation for backward compatibility */ }
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -1,21 +1,21 @@
|
||||
/*
|
||||
Printable.h - Interface class that allows printing of complex types
|
||||
Copyright (c) 2011 Adrian McEwen. All right reserved.
|
||||
Printable.h - Interface class that allows printing of complex types
|
||||
Copyright (c) 2011 Adrian McEwen. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef Printable_h
|
||||
#define Printable_h
|
||||
@ -25,15 +25,14 @@
|
||||
class Print;
|
||||
|
||||
/** The Printable class provides a way for new classes to allow themselves to be printed.
|
||||
By deriving from Printable and implementing the printTo method, it will then be possible
|
||||
for users to print out instances of this class by passing them into the usual
|
||||
Print::print and Print::println methods.
|
||||
*/
|
||||
By deriving from Printable and implementing the printTo method, it will then be possible
|
||||
for users to print out instances of this class by passing them into the usual
|
||||
Print::print and Print::println methods.
|
||||
*/
|
||||
|
||||
class Printable
|
||||
{
|
||||
public:
|
||||
virtual size_t printTo(Print& p) const = 0;
|
||||
class Printable {
|
||||
public:
|
||||
virtual size_t printTo(Print& p) const = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -14,22 +14,18 @@ static scheduled_fn_t* sLastUnused = 0;
|
||||
|
||||
static int sCount = 0;
|
||||
|
||||
static scheduled_fn_t* get_fn()
|
||||
{
|
||||
static scheduled_fn_t* get_fn() {
|
||||
scheduled_fn_t* result = NULL;
|
||||
// try to get an item from unused items list
|
||||
if (sFirstUnused)
|
||||
{
|
||||
if (sFirstUnused) {
|
||||
result = sFirstUnused;
|
||||
sFirstUnused = result->mNext;
|
||||
if (sFirstUnused == NULL)
|
||||
{
|
||||
if (sFirstUnused == NULL) {
|
||||
sLastUnused = NULL;
|
||||
}
|
||||
}
|
||||
// if no unused items, and count not too high, allocate a new one
|
||||
else if (sCount != SCHEDULED_FN_MAX_COUNT)
|
||||
{
|
||||
else if (sCount != SCHEDULED_FN_MAX_COUNT) {
|
||||
result = new scheduled_fn_t;
|
||||
result->mNext = NULL;
|
||||
++sCount;
|
||||
@ -39,12 +35,10 @@ static scheduled_fn_t* get_fn()
|
||||
|
||||
static void recycle_fn(scheduled_fn_t* fn)
|
||||
{
|
||||
if (!sLastUnused)
|
||||
{
|
||||
if (!sLastUnused) {
|
||||
sFirstUnused = fn;
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
sLastUnused->mNext = fn;
|
||||
}
|
||||
fn->mNext = NULL;
|
||||
@ -54,18 +48,15 @@ static void recycle_fn(scheduled_fn_t* fn)
|
||||
bool schedule_function(std::function<void(void)> fn)
|
||||
{
|
||||
scheduled_fn_t* item = get_fn();
|
||||
if (!item)
|
||||
{
|
||||
if (!item) {
|
||||
return false;
|
||||
}
|
||||
item->mFunc = fn;
|
||||
item->mNext = NULL;
|
||||
if (!sFirst)
|
||||
{
|
||||
if (!sFirst) {
|
||||
sFirst = item;
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
sLast->mNext = item;
|
||||
}
|
||||
sLast = item;
|
||||
@ -74,11 +65,10 @@ bool schedule_function(std::function<void(void)> fn)
|
||||
|
||||
void run_scheduled_functions()
|
||||
{
|
||||
scheduled_fn_t* rFirst = sFirst;
|
||||
sFirst = NULL;
|
||||
sLast = NULL;
|
||||
while (rFirst)
|
||||
{
|
||||
scheduled_fn_t* rFirst = sFirst;
|
||||
sFirst = NULL;
|
||||
sLast = NULL;
|
||||
while (rFirst) {
|
||||
scheduled_fn_t* item = rFirst;
|
||||
rFirst = item->mNext;
|
||||
item->mFunc();
|
||||
|
@ -1,118 +1,117 @@
|
||||
/*
|
||||
ScheduledFunctions.cpp
|
||||
|
||||
Created on: 27 apr. 2018
|
||||
Author: Herman
|
||||
*/
|
||||
* ScheduledFunctions.cpp
|
||||
*
|
||||
* Created on: 27 apr. 2018
|
||||
* Author: Herman
|
||||
*/
|
||||
#include "ScheduledFunctions.h"
|
||||
|
||||
std::list<ScheduledFunctions::ScheduledElement> ScheduledFunctions::scheduledFunctions;
|
||||
|
||||
ScheduledFunctions::ScheduledFunctions()
|
||||
: ScheduledFunctions(UINT_MAX)
|
||||
:ScheduledFunctions(UINT_MAX)
|
||||
{
|
||||
}
|
||||
|
||||
ScheduledFunctions::ScheduledFunctions(unsigned int reqMax)
|
||||
{
|
||||
maxElements = reqMax;
|
||||
maxElements = reqMax;
|
||||
}
|
||||
|
||||
ScheduledFunctions::~ScheduledFunctions()
|
||||
{
|
||||
ScheduledFunctions::~ScheduledFunctions() {
|
||||
}
|
||||
|
||||
ScheduledRegistration ScheduledFunctions::insertElement(ScheduledElement se, bool front)
|
||||
{
|
||||
if (countElements >= maxElements)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
countElements++;
|
||||
if (front)
|
||||
{
|
||||
scheduledFunctions.push_front(se);
|
||||
return scheduledFunctions.begin()->registration;
|
||||
}
|
||||
else
|
||||
{
|
||||
scheduledFunctions.push_back(se);
|
||||
return scheduledFunctions.rbegin()->registration;
|
||||
}
|
||||
}
|
||||
if (countElements >= maxElements)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
countElements++;
|
||||
if (front)
|
||||
{
|
||||
scheduledFunctions.push_front(se);
|
||||
return scheduledFunctions.begin()->registration;
|
||||
}
|
||||
else
|
||||
{
|
||||
scheduledFunctions.push_back(se);
|
||||
return scheduledFunctions.rbegin()->registration;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::list<ScheduledFunctions::ScheduledElement>::iterator ScheduledFunctions::eraseElement(std::list<ScheduledFunctions::ScheduledElement>::iterator it)
|
||||
{
|
||||
countElements--;
|
||||
return scheduledFunctions.erase(it);
|
||||
countElements--;
|
||||
return scheduledFunctions.erase(it);
|
||||
}
|
||||
|
||||
bool ScheduledFunctions::scheduleFunction(ScheduledFunction sf, bool continuous, bool front)
|
||||
{
|
||||
return (insertElement({this, continuous, nullptr, sf}, front) == nullptr);
|
||||
return (insertElement({this,continuous,nullptr,sf}, front) == nullptr);
|
||||
}
|
||||
|
||||
bool ScheduledFunctions::scheduleFunction(ScheduledFunction sf)
|
||||
{
|
||||
return scheduleFunction(sf, false, false);
|
||||
return scheduleFunction(sf, false, false);
|
||||
}
|
||||
|
||||
ScheduledRegistration ScheduledFunctions::scheduleFunctionReg(ScheduledFunction sf, bool continuous, bool front)
|
||||
ScheduledRegistration ScheduledFunctions::scheduleFunctionReg (ScheduledFunction sf, bool continuous, bool front)
|
||||
{
|
||||
return insertElement({this, continuous, std::make_shared<int>(1), sf}, front);
|
||||
return insertElement({this,continuous,std::make_shared<int>(1),sf},front);
|
||||
}
|
||||
|
||||
void ScheduledFunctions::runScheduledFunctions()
|
||||
{
|
||||
auto lastElement = scheduledFunctions.end(); // do not execute elements added during runScheduledFunctions
|
||||
auto it = scheduledFunctions.begin();
|
||||
while (it != lastElement)
|
||||
{
|
||||
bool erase = false;
|
||||
if (it->registration == nullptr)
|
||||
{
|
||||
it->function();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (it->registration.use_count() > 1)
|
||||
{
|
||||
it->function();
|
||||
}
|
||||
else
|
||||
{
|
||||
erase = true;
|
||||
}
|
||||
}
|
||||
if ((!it->continuous) || (erase))
|
||||
{
|
||||
it = it->_this->eraseElement(it);
|
||||
}
|
||||
else
|
||||
{
|
||||
it++;
|
||||
}
|
||||
}
|
||||
auto lastElement = scheduledFunctions.end(); // do not execute elements added during runScheduledFunctions
|
||||
auto it = scheduledFunctions.begin();
|
||||
while (it != lastElement)
|
||||
{
|
||||
bool erase = false;
|
||||
if (it->registration == nullptr)
|
||||
{
|
||||
it->function();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (it->registration.use_count() > 1)
|
||||
{
|
||||
it->function();
|
||||
}
|
||||
else
|
||||
{
|
||||
erase = true;
|
||||
}
|
||||
}
|
||||
if ((!it->continuous) || (erase))
|
||||
{
|
||||
it = it->_this->eraseElement(it);
|
||||
}
|
||||
else
|
||||
{
|
||||
it++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ScheduledFunctions::removeFunction(ScheduledRegistration sr)
|
||||
{
|
||||
auto it = scheduledFunctions.begin();
|
||||
bool removed = false;
|
||||
while ((!removed) && (it != scheduledFunctions.end()))
|
||||
{
|
||||
if (it->registration == sr)
|
||||
{
|
||||
it = eraseElement(it);
|
||||
removed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
it++;
|
||||
}
|
||||
}
|
||||
auto it = scheduledFunctions.begin();
|
||||
bool removed = false;
|
||||
while ((!removed) && (it != scheduledFunctions.end()))
|
||||
{
|
||||
if (it->registration == sr)
|
||||
{
|
||||
it = eraseElement(it);
|
||||
removed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
it++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,9 @@
|
||||
/*
|
||||
ScheduledFunctions.h
|
||||
|
||||
Created on: 27 apr. 2018
|
||||
Author: Herman
|
||||
*/
|
||||
* ScheduledFunctions.h
|
||||
*
|
||||
* Created on: 27 apr. 2018
|
||||
* Author: Herman
|
||||
*/
|
||||
#include "Arduino.h"
|
||||
#include "Schedule.h"
|
||||
|
||||
@ -18,34 +18,33 @@
|
||||
typedef std::function<void(void)> ScheduledFunction;
|
||||
typedef std::shared_ptr<void> ScheduledRegistration;
|
||||
|
||||
class ScheduledFunctions
|
||||
{
|
||||
class ScheduledFunctions {
|
||||
|
||||
public:
|
||||
ScheduledFunctions();
|
||||
ScheduledFunctions(unsigned int reqMax);
|
||||
virtual ~ScheduledFunctions();
|
||||
ScheduledFunctions();
|
||||
ScheduledFunctions(unsigned int reqMax);
|
||||
virtual ~ScheduledFunctions();
|
||||
|
||||
struct ScheduledElement
|
||||
{
|
||||
ScheduledFunctions* _this;
|
||||
bool continuous;
|
||||
ScheduledRegistration registration;
|
||||
ScheduledFunction function;
|
||||
};
|
||||
struct ScheduledElement
|
||||
{
|
||||
ScheduledFunctions* _this;
|
||||
bool continuous;
|
||||
ScheduledRegistration registration;
|
||||
ScheduledFunction function;
|
||||
};
|
||||
|
||||
ScheduledRegistration insertElement(ScheduledElement se, bool front);
|
||||
std::list<ScheduledElement>::iterator eraseElement(std::list<ScheduledElement>::iterator);
|
||||
bool scheduleFunction(ScheduledFunction sf, bool continuous, bool front);
|
||||
bool scheduleFunction(ScheduledFunction sf);
|
||||
ScheduledRegistration scheduleFunctionReg(ScheduledFunction sf, bool continuous, bool front);
|
||||
static void runScheduledFunctions();
|
||||
void removeFunction(ScheduledRegistration sr);
|
||||
ScheduledRegistration insertElement(ScheduledElement se, bool front);
|
||||
std::list<ScheduledElement>::iterator eraseElement(std::list<ScheduledElement>::iterator);
|
||||
bool scheduleFunction(ScheduledFunction sf, bool continuous, bool front);
|
||||
bool scheduleFunction(ScheduledFunction sf);
|
||||
ScheduledRegistration scheduleFunctionReg (ScheduledFunction sf, bool continuous, bool front);
|
||||
static void runScheduledFunctions();
|
||||
void removeFunction(ScheduledRegistration sr);
|
||||
|
||||
|
||||
static std::list<ScheduledElement> scheduledFunctions;
|
||||
unsigned int maxElements;
|
||||
unsigned int countElements = 0;
|
||||
static std::list<ScheduledElement> scheduledFunctions;
|
||||
unsigned int maxElements;
|
||||
unsigned int countElements = 0;
|
||||
|
||||
};
|
||||
|
||||
|
@ -1,31 +1,30 @@
|
||||
/*
|
||||
Server.h - Base class that provides Server
|
||||
Copyright (c) 2011 Adrian McEwen. All right reserved.
|
||||
Server.h - Base class that provides Server
|
||||
Copyright (c) 2011 Adrian McEwen. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef server_h
|
||||
#define server_h
|
||||
|
||||
#include "Print.h"
|
||||
|
||||
class Server: public Print
|
||||
{
|
||||
public:
|
||||
virtual void begin() = 0;
|
||||
class Server: public Print {
|
||||
public:
|
||||
virtual void begin() =0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -1,27 +1,27 @@
|
||||
/*
|
||||
StackThunk.c - Allow use second stack for BearSSL calls
|
||||
StackThunk.c - Allow use second stack for BearSSL calls
|
||||
|
||||
BearSSL uses a significant amount of stack space, much larger than
|
||||
the default Arduino core stack. These routines handle swapping
|
||||
between a secondary, user-allocated stack on the heap and the real
|
||||
stack.
|
||||
BearSSL uses a significant amount of stack space, much larger than
|
||||
the default Arduino core stack. These routines handle swapping
|
||||
between a secondary, user-allocated stack on the heap and the real
|
||||
stack.
|
||||
|
||||
Copyright (c) 2017 Earle F. Philhower, III. All rights reserved.
|
||||
Copyright (c) 2017 Earle F. Philhower, III. All rights 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 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.
|
||||
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
|
||||
Modified 8 May 2015 by Hristo Gochkov (proper post and file upload handling)
|
||||
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
|
||||
Modified 8 May 2015 by Hristo Gochkov (proper post and file upload handling)
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
@ -31,111 +31,97 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
uint32_t *stack_thunk_ptr = NULL;
|
||||
uint32_t *stack_thunk_top = NULL;
|
||||
uint32_t *stack_thunk_save = NULL; /* Saved A1 while in BearSSL */
|
||||
uint32_t stack_thunk_refcnt = 0;
|
||||
uint32_t *stack_thunk_ptr = NULL;
|
||||
uint32_t *stack_thunk_top = NULL;
|
||||
uint32_t *stack_thunk_save = NULL; /* Saved A1 while in BearSSL */
|
||||
uint32_t stack_thunk_refcnt = 0;
|
||||
|
||||
#define _stackSize (5600/4)
|
||||
#define _stackPaint 0xdeadbeef
|
||||
|
||||
/* Add a reference, and allocate the stack if necessary */
|
||||
void stack_thunk_add_ref()
|
||||
{
|
||||
stack_thunk_refcnt++;
|
||||
if (stack_thunk_refcnt == 1)
|
||||
{
|
||||
stack_thunk_ptr = (uint32_t *)malloc(_stackSize * sizeof(uint32_t));
|
||||
stack_thunk_top = stack_thunk_ptr + _stackSize - 1;
|
||||
stack_thunk_save = NULL;
|
||||
stack_thunk_repaint();
|
||||
}
|
||||
}
|
||||
/* Add a reference, and allocate the stack if necessary */
|
||||
void stack_thunk_add_ref()
|
||||
{
|
||||
stack_thunk_refcnt++;
|
||||
if (stack_thunk_refcnt == 1) {
|
||||
stack_thunk_ptr = (uint32_t *)malloc(_stackSize * sizeof(uint32_t));
|
||||
stack_thunk_top = stack_thunk_ptr + _stackSize - 1;
|
||||
stack_thunk_save = NULL;
|
||||
stack_thunk_repaint();
|
||||
}
|
||||
}
|
||||
|
||||
/* Drop a reference, and free stack if no more in use */
|
||||
void stack_thunk_del_ref()
|
||||
{
|
||||
if (stack_thunk_refcnt == 0)
|
||||
{
|
||||
/* Error! */
|
||||
return;
|
||||
}
|
||||
stack_thunk_refcnt--;
|
||||
if (!stack_thunk_refcnt)
|
||||
{
|
||||
free(stack_thunk_ptr);
|
||||
stack_thunk_ptr = NULL;
|
||||
stack_thunk_top = NULL;
|
||||
stack_thunk_save = NULL;
|
||||
}
|
||||
}
|
||||
/* Drop a reference, and free stack if no more in use */
|
||||
void stack_thunk_del_ref()
|
||||
{
|
||||
if (stack_thunk_refcnt == 0) {
|
||||
/* Error! */
|
||||
return;
|
||||
}
|
||||
stack_thunk_refcnt--;
|
||||
if (!stack_thunk_refcnt) {
|
||||
free(stack_thunk_ptr);
|
||||
stack_thunk_ptr = NULL;
|
||||
stack_thunk_top = NULL;
|
||||
stack_thunk_save = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void stack_thunk_repaint()
|
||||
{
|
||||
for (int i = 0; i < _stackSize; i++)
|
||||
{
|
||||
stack_thunk_ptr[i] = _stackPaint;
|
||||
}
|
||||
}
|
||||
void stack_thunk_repaint()
|
||||
{
|
||||
for (int i=0; i < _stackSize; i++) {
|
||||
stack_thunk_ptr[i] = _stackPaint;
|
||||
}
|
||||
}
|
||||
|
||||
/* Simple accessor functions used by postmortem */
|
||||
uint32_t stack_thunk_get_refcnt()
|
||||
{
|
||||
return stack_thunk_refcnt;
|
||||
}
|
||||
/* Simple accessor functions used by postmortem */
|
||||
uint32_t stack_thunk_get_refcnt() {
|
||||
return stack_thunk_refcnt;
|
||||
}
|
||||
|
||||
uint32_t stack_thunk_get_stack_top()
|
||||
{
|
||||
return (uint32_t)stack_thunk_top;
|
||||
}
|
||||
uint32_t stack_thunk_get_stack_top() {
|
||||
return (uint32_t)stack_thunk_top;
|
||||
}
|
||||
|
||||
uint32_t stack_thunk_get_stack_bot()
|
||||
{
|
||||
return (uint32_t)stack_thunk_ptr;
|
||||
}
|
||||
uint32_t stack_thunk_get_stack_bot() {
|
||||
return (uint32_t)stack_thunk_ptr;
|
||||
}
|
||||
|
||||
uint32_t stack_thunk_get_cont_sp()
|
||||
{
|
||||
return (uint32_t)stack_thunk_save;
|
||||
}
|
||||
uint32_t stack_thunk_get_cont_sp() {
|
||||
return (uint32_t)stack_thunk_save;
|
||||
}
|
||||
|
||||
/* Return the number of bytes ever used since the stack was created */
|
||||
uint32_t stack_thunk_get_max_usage()
|
||||
{
|
||||
uint32_t cnt = 0;
|
||||
/* Return the number of bytes ever used since the stack was created */
|
||||
uint32_t stack_thunk_get_max_usage()
|
||||
{
|
||||
uint32_t cnt = 0;
|
||||
|
||||
/* No stack == no usage by definition! */
|
||||
if (!stack_thunk_ptr)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
/* No stack == no usage by definition! */
|
||||
if (!stack_thunk_ptr) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (cnt = 0; (cnt < _stackSize) && (stack_thunk_ptr[cnt] == _stackPaint); cnt++)
|
||||
{
|
||||
/* Noop, all work done in for() */
|
||||
}
|
||||
return 4 * (_stackSize - cnt);
|
||||
}
|
||||
for (cnt=0; (cnt < _stackSize) && (stack_thunk_ptr[cnt] == _stackPaint); cnt++) {
|
||||
/* Noop, all work done in for() */
|
||||
}
|
||||
return 4 * (_stackSize - cnt);
|
||||
}
|
||||
|
||||
/* Print the stack from the first used 16-byte chunk to the top, decodable by the exception decoder */
|
||||
void stack_thunk_dump_stack()
|
||||
{
|
||||
uint32_t *pos = stack_thunk_top;
|
||||
while (pos < stack_thunk_ptr)
|
||||
{
|
||||
if ((pos[0] != _stackPaint) || (pos[1] != _stackPaint) || (pos[2] != _stackPaint) || (pos[3] != _stackPaint))
|
||||
{
|
||||
break;
|
||||
}
|
||||
pos += 4;
|
||||
}
|
||||
ets_printf(">>>stack>>>\n");
|
||||
while (pos < stack_thunk_ptr)
|
||||
{
|
||||
ets_printf("%08x: %08x %08x %08x %08x\n", (int32_t)pos, pos[0], pos[1], pos[2], pos[3]);
|
||||
pos += 4;
|
||||
}
|
||||
ets_printf("<<<stack<<<\n");
|
||||
}
|
||||
/* Print the stack from the first used 16-byte chunk to the top, decodable by the exception decoder */
|
||||
void stack_thunk_dump_stack()
|
||||
{
|
||||
uint32_t *pos = stack_thunk_top;
|
||||
while (pos < stack_thunk_ptr) {
|
||||
if ((pos[0] != _stackPaint) || (pos[1] != _stackPaint) || (pos[2] != _stackPaint) || (pos[3] != _stackPaint))
|
||||
break;
|
||||
pos += 4;
|
||||
}
|
||||
ets_printf(">>>stack>>>\n");
|
||||
while (pos < stack_thunk_ptr) {
|
||||
ets_printf("%08x: %08x %08x %08x %08x\n", (int32_t)pos, pos[0], pos[1], pos[2], pos[3]);
|
||||
pos += 4;
|
||||
}
|
||||
ets_printf("<<<stack<<<\n");
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -1,27 +1,27 @@
|
||||
/*
|
||||
StackThunk.h - Allow use second stack for BearSSL calls
|
||||
StackThunk.h - Allow use second stack for BearSSL calls
|
||||
|
||||
BearSSL uses a significant amount of stack space, much larger than
|
||||
the default Arduino core stack. These routines handle swapping
|
||||
between a secondary, user-allocated stack on the heap and the real
|
||||
stack.
|
||||
BearSSL uses a significant amount of stack space, much larger than
|
||||
the default Arduino core stack. These routines handle swapping
|
||||
between a secondary, user-allocated stack on the heap and the real
|
||||
stack.
|
||||
|
||||
Copyright (c) 2017 Earle F. Philhower, III. All rights reserved.
|
||||
Copyright (c) 2017 Earle F. Philhower, III. All rights 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 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.
|
||||
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
|
||||
Modified 8 May 2015 by Hristo Gochkov (proper post and file upload handling)
|
||||
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
|
||||
Modified 8 May 2015 by Hristo Gochkov (proper post and file upload handling)
|
||||
*/
|
||||
|
||||
#ifndef _STACKTHUNK_H
|
||||
|
@ -1,24 +1,24 @@
|
||||
/*
|
||||
Stream.cpp - adds parsing methods to Stream class
|
||||
Copyright (c) 2008 David A. Mellis. All right reserved.
|
||||
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 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.
|
||||
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
|
||||
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
|
||||
*/
|
||||
Created July 2011
|
||||
parsing functions based on TextFinder library by Michael Margolis
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <Stream.h>
|
||||
@ -26,59 +26,43 @@
|
||||
#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 Stream::timedRead() {
|
||||
int c;
|
||||
_startMillis = millis();
|
||||
do
|
||||
{
|
||||
do {
|
||||
c = read();
|
||||
if (c >= 0)
|
||||
{
|
||||
if(c >= 0)
|
||||
return c;
|
||||
}
|
||||
yield();
|
||||
} while (millis() - _startMillis < _timeout);
|
||||
} while(millis() - _startMillis < _timeout);
|
||||
return -1; // -1 indicates timeout
|
||||
}
|
||||
|
||||
// private method to peek stream with timeout
|
||||
int Stream::timedPeek()
|
||||
{
|
||||
int Stream::timedPeek() {
|
||||
int c;
|
||||
_startMillis = millis();
|
||||
do
|
||||
{
|
||||
do {
|
||||
c = peek();
|
||||
if (c >= 0)
|
||||
{
|
||||
if(c >= 0)
|
||||
return c;
|
||||
}
|
||||
yield();
|
||||
} while (millis() - _startMillis < _timeout);
|
||||
} 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 Stream::peekNextDigit() {
|
||||
int c;
|
||||
while (1)
|
||||
{
|
||||
while(1) {
|
||||
c = timedPeek();
|
||||
if (c < 0)
|
||||
{
|
||||
return c; // timeout
|
||||
}
|
||||
if (c == '-')
|
||||
{
|
||||
if(c < 0)
|
||||
return c; // timeout
|
||||
if(c == '-')
|
||||
return c;
|
||||
}
|
||||
if (c >= '0' && c <= '9')
|
||||
{
|
||||
if(c >= '0' && c <= '9')
|
||||
return c;
|
||||
}
|
||||
read(); // discard non-numeric
|
||||
}
|
||||
}
|
||||
@ -92,65 +76,48 @@ void Stream::setTimeout(unsigned long timeout) // sets the maximum number of mi
|
||||
}
|
||||
|
||||
// find returns true if the target string is found
|
||||
bool Stream::find(const char *target)
|
||||
{
|
||||
bool Stream::find(const char *target) {
|
||||
return findUntil(target, (char*) "");
|
||||
}
|
||||
|
||||
// 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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
bool Stream::findUntil(const char *target, size_t targetLen, const char *terminator, size_t termLen) {
|
||||
size_t index = 0; // maximum target string length is 64k bytes!
|
||||
size_t termIndex = 0;
|
||||
int c;
|
||||
|
||||
if (*target == 0)
|
||||
{
|
||||
return true; // return true if target is a null string
|
||||
}
|
||||
while ((c = timedRead()) > 0)
|
||||
{
|
||||
if(*target == 0)
|
||||
return true; // return true if target is a null string
|
||||
while((c = timedRead()) > 0) {
|
||||
|
||||
if (c != target[index])
|
||||
{
|
||||
index = 0; // reset index if any char does not match
|
||||
}
|
||||
if(c != target[index])
|
||||
index = 0; // reset index if any char does not match
|
||||
|
||||
if (c == target[index])
|
||||
{
|
||||
if(c == target[index]) {
|
||||
//////Serial.print("found "); Serial.write(c); Serial.print("index now"); Serial.println(index+1);
|
||||
if (++index >= targetLen) // return true if all chars in the target match
|
||||
{
|
||||
if(++index >= targetLen) { // return true if all chars in the target match
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (termLen > 0 && c == terminator[termIndex])
|
||||
{
|
||||
if (++termIndex >= termLen)
|
||||
{
|
||||
return false; // return false if terminate string found before target string
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(termLen > 0 && c == terminator[termIndex]) {
|
||||
if(++termIndex >= termLen)
|
||||
return false; // return false if terminate string found before target string
|
||||
} else
|
||||
termIndex = 0;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -158,59 +125,46 @@ bool Stream::findUntil(const char *target, size_t targetLen, const char *termina
|
||||
// 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()
|
||||
{
|
||||
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)
|
||||
{
|
||||
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
|
||||
}
|
||||
if(c < 0)
|
||||
return 0; // zero returned if timeout
|
||||
|
||||
do
|
||||
{
|
||||
if (c == skipChar)
|
||||
do {
|
||||
if(c == skipChar)
|
||||
; // ignore this charactor
|
||||
else if (c == '-')
|
||||
{
|
||||
else if(c == '-')
|
||||
isNegative = true;
|
||||
}
|
||||
else if (c >= '0' && c <= '9') // is c a digit?
|
||||
{
|
||||
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);
|
||||
} while((c >= '0' && c <= '9') || c == skipChar);
|
||||
|
||||
if (isNegative)
|
||||
{
|
||||
if(isNegative)
|
||||
value = -value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
// as parseInt but returns a floating point value
|
||||
float Stream::parseFloat()
|
||||
{
|
||||
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)
|
||||
{
|
||||
float Stream::parseFloat(char skipChar) {
|
||||
boolean isNegative = false;
|
||||
boolean isFraction = false;
|
||||
long value = 0;
|
||||
@ -219,47 +173,31 @@ float Stream::parseFloat(char skipChar)
|
||||
|
||||
c = peekNextDigit();
|
||||
// ignore non numeric leading characters
|
||||
if (c < 0)
|
||||
{
|
||||
return 0; // zero returned if timeout
|
||||
}
|
||||
if(c < 0)
|
||||
return 0; // zero returned if timeout
|
||||
|
||||
do
|
||||
{
|
||||
if (c == skipChar)
|
||||
do {
|
||||
if(c == skipChar)
|
||||
; // ignore
|
||||
else if (c == '-')
|
||||
{
|
||||
else if(c == '-')
|
||||
isNegative = true;
|
||||
}
|
||||
else if (c == '.')
|
||||
{
|
||||
else if(c == '.')
|
||||
isFraction = true;
|
||||
}
|
||||
else if (c >= '0' && c <= '9') // is c a digit?
|
||||
{
|
||||
else if(c >= '0' && c <= '9') { // is c a digit?
|
||||
value = value * 10 + c - '0';
|
||||
if (isFraction)
|
||||
{
|
||||
if(isFraction)
|
||||
fraction *= 0.1;
|
||||
}
|
||||
}
|
||||
read(); // consume the character we got with peek
|
||||
c = timedPeek();
|
||||
} while ((c >= '0' && c <= '9') || c == '.' || c == skipChar);
|
||||
} while((c >= '0' && c <= '9') || c == '.' || c == skipChar);
|
||||
|
||||
if (isNegative)
|
||||
{
|
||||
if(isNegative)
|
||||
value = -value;
|
||||
}
|
||||
if (isFraction)
|
||||
{
|
||||
if(isFraction)
|
||||
return value * fraction;
|
||||
}
|
||||
else
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
// read characters from stream into buffer
|
||||
@ -267,16 +205,12 @@ float Stream::parseFloat(char skipChar)
|
||||
// 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 Stream::readBytes(char *buffer, size_t length) {
|
||||
size_t count = 0;
|
||||
while (count < length)
|
||||
{
|
||||
while(count < length) {
|
||||
int c = timedRead();
|
||||
if (c < 0)
|
||||
{
|
||||
if(c < 0)
|
||||
break;
|
||||
}
|
||||
*buffer++ = (char) c;
|
||||
count++;
|
||||
}
|
||||
@ -287,44 +221,34 @@ size_t Stream::readBytes(char *buffer, size_t 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)
|
||||
|
||||
size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
|
||||
{
|
||||
if (length < 1)
|
||||
{
|
||||
size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length) {
|
||||
if(length < 1)
|
||||
return 0;
|
||||
}
|
||||
size_t index = 0;
|
||||
while (index < length)
|
||||
{
|
||||
while(index < length) {
|
||||
int c = timedRead();
|
||||
if (c < 0 || c == terminator)
|
||||
{
|
||||
if(c < 0 || c == terminator)
|
||||
break;
|
||||
}
|
||||
*buffer++ = (char) c;
|
||||
index++;
|
||||
}
|
||||
return index; // return number of characters, not including null terminator
|
||||
}
|
||||
|
||||
String Stream::readString()
|
||||
{
|
||||
String Stream::readString() {
|
||||
String ret;
|
||||
int c = timedRead();
|
||||
while (c >= 0)
|
||||
{
|
||||
while(c >= 0) {
|
||||
ret += (char) c;
|
||||
c = timedRead();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
String Stream::readStringUntil(char terminator)
|
||||
{
|
||||
String Stream::readStringUntil(char terminator) {
|
||||
String ret;
|
||||
int c = timedRead();
|
||||
while (c >= 0 && c != terminator)
|
||||
{
|
||||
while(c >= 0 && c != terminator) {
|
||||
ret += (char) c;
|
||||
c = timedRead();
|
||||
}
|
||||
|
@ -1,23 +1,23 @@
|
||||
/*
|
||||
Stream.h - base class for character-based streams.
|
||||
Copyright (c) 2010 David A. Mellis. All right reserved.
|
||||
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 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.
|
||||
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
|
||||
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
|
||||
*/
|
||||
parsing functions based on TextFinder library by Michael Margolis
|
||||
*/
|
||||
|
||||
#ifndef Stream_h
|
||||
#define Stream_h
|
||||
@ -27,100 +27,89 @@
|
||||
|
||||
// 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)
|
||||
*/
|
||||
#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
|
||||
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;
|
||||
public:
|
||||
virtual int available() = 0;
|
||||
virtual int read() = 0;
|
||||
virtual int peek() = 0;
|
||||
|
||||
Stream()
|
||||
{
|
||||
_timeout = 1000;
|
||||
}
|
||||
Stream() {
|
||||
_timeout = 1000;
|
||||
}
|
||||
|
||||
// parsing methods
|
||||
// parsing methods
|
||||
|
||||
void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
|
||||
void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
|
||||
|
||||
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); // 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(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 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, 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);
|
||||
}
|
||||
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.
|
||||
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
|
||||
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)
|
||||
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)
|
||||
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 String readString();
|
||||
String readStringUntil(char terminator);
|
||||
// Arduino String functions to be added here
|
||||
virtual String readString();
|
||||
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
|
||||
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
|
||||
float parseFloat(char skipChar); // as above but the given skipChar is ignored
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -1,36 +1,33 @@
|
||||
/**
|
||||
StreamString.cpp
|
||||
StreamString.cpp
|
||||
|
||||
Copyright (c) 2015 Markus Sattler. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2015 Markus Sattler. 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 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.
|
||||
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
|
||||
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
|
||||
|
||||
*/
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "StreamString.h"
|
||||
|
||||
size_t StreamString::write(const uint8_t *data, size_t size)
|
||||
{
|
||||
if (size && data)
|
||||
{
|
||||
size_t StreamString::write(const uint8_t *data, size_t size) {
|
||||
if(size && data) {
|
||||
const unsigned int newlen = length() + size;
|
||||
if (reserve(newlen + 1))
|
||||
{
|
||||
memcpy((void *)(wbuffer() + len()), (const void *) data, size);
|
||||
if(reserve(newlen + 1)) {
|
||||
memcpy((void *) (wbuffer() + len()), (const void *) data, size);
|
||||
setLen(newlen);
|
||||
*(wbuffer() + newlen) = 0x00; // add null for string end
|
||||
return size;
|
||||
@ -39,20 +36,16 @@ size_t StreamString::write(const uint8_t *data, size_t size)
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t StreamString::write(uint8_t data)
|
||||
{
|
||||
size_t StreamString::write(uint8_t data) {
|
||||
return concat((char) data);
|
||||
}
|
||||
|
||||
int StreamString::available()
|
||||
{
|
||||
int StreamString::available() {
|
||||
return length();
|
||||
}
|
||||
|
||||
int StreamString::read()
|
||||
{
|
||||
if (length())
|
||||
{
|
||||
int StreamString::read() {
|
||||
if(length()) {
|
||||
char c = charAt(0);
|
||||
remove(0, 1);
|
||||
return c;
|
||||
@ -61,17 +54,14 @@ int StreamString::read()
|
||||
return -1;
|
||||
}
|
||||
|
||||
int StreamString::peek()
|
||||
{
|
||||
if (length())
|
||||
{
|
||||
int StreamString::peek() {
|
||||
if(length()) {
|
||||
char c = charAt(0);
|
||||
return c;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void StreamString::flush()
|
||||
{
|
||||
void StreamString::flush() {
|
||||
}
|
||||
|
||||
|
@ -1,22 +1,22 @@
|
||||
/**
|
||||
StreamString.h
|
||||
StreamString.h
|
||||
|
||||
Copyright (c) 2015 Markus Sattler. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2015 Markus Sattler. 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 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.
|
||||
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
|
||||
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
|
||||
|
||||
*/
|
||||
|
||||
@ -24,8 +24,7 @@
|
||||
#define STREAMSTRING_H_
|
||||
|
||||
|
||||
class StreamString: public Stream, public String
|
||||
{
|
||||
class StreamString: public Stream, public String {
|
||||
public:
|
||||
size_t write(const uint8_t *buffer, size_t size) override;
|
||||
size_t write(uint8_t data) override;
|
||||
|
@ -1,24 +1,24 @@
|
||||
/*
|
||||
Tone.cpp
|
||||
Tone.cpp
|
||||
|
||||
A Tone Generator Library for the ESP8266
|
||||
A Tone Generator Library for the ESP8266
|
||||
|
||||
Original Copyright (c) 2016 Ben Pirt. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Original Copyright (c) 2016 Ben Pirt. 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 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.
|
||||
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
|
||||
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
|
||||
*/
|
||||
|
||||
#include "Arduino.h"
|
||||
@ -28,74 +28,60 @@
|
||||
static uint32_t _toneMap = 0;
|
||||
|
||||
|
||||
static void _startTone(uint8_t _pin, uint32_t high, uint32_t low, unsigned long duration)
|
||||
{
|
||||
if (_pin > 16)
|
||||
{
|
||||
return;
|
||||
}
|
||||
static void _startTone(uint8_t _pin, uint32_t high, uint32_t low, unsigned long duration) {
|
||||
if (_pin > 16) {
|
||||
return;
|
||||
}
|
||||
|
||||
pinMode(_pin, OUTPUT);
|
||||
pinMode(_pin, OUTPUT);
|
||||
|
||||
high = std::max(high, (uint32_t)100);
|
||||
low = std::max(low, (uint32_t)100);
|
||||
high = std::max(high, (uint32_t)100);
|
||||
low = std::max(low, (uint32_t)100);
|
||||
|
||||
if (startWaveform(_pin, high, low, (uint32_t) duration * 1000))
|
||||
{
|
||||
_toneMap |= 1 << _pin;
|
||||
}
|
||||
if (startWaveform(_pin, high, low, (uint32_t) duration * 1000)) {
|
||||
_toneMap |= 1 << _pin;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
|
||||
{
|
||||
if (frequency == 0)
|
||||
{
|
||||
noTone(_pin);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint32_t period = 1000000L / frequency;
|
||||
uint32_t high = period / 2;
|
||||
uint32_t low = period - high;
|
||||
_startTone(_pin, high, low, duration);
|
||||
}
|
||||
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) {
|
||||
if (frequency == 0) {
|
||||
noTone(_pin);
|
||||
} else {
|
||||
uint32_t period = 1000000L / frequency;
|
||||
uint32_t high = period / 2;
|
||||
uint32_t low = period - high;
|
||||
_startTone(_pin, high, low, duration);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Separate tone(float) to hopefully not pull in floating point libs unless
|
||||
// it's called with a float.
|
||||
void tone(uint8_t _pin, double frequency, unsigned long duration)
|
||||
{
|
||||
if (frequency < 1.0) // FP means no exact comparisons
|
||||
{
|
||||
noTone(_pin);
|
||||
}
|
||||
else
|
||||
{
|
||||
double period = 1000000.0 / frequency;
|
||||
uint32_t high = (uint32_t)((period / 2.0) + 0.5);
|
||||
uint32_t low = (uint32_t)(period + 0.5) - high;
|
||||
_startTone(_pin, high, low, duration);
|
||||
}
|
||||
void tone(uint8_t _pin, double frequency, unsigned long duration) {
|
||||
if (frequency < 1.0) { // FP means no exact comparisons
|
||||
noTone(_pin);
|
||||
} else {
|
||||
double period = 1000000.0 / frequency;
|
||||
uint32_t high = (uint32_t)((period / 2.0) + 0.5);
|
||||
uint32_t low = (uint32_t)(period + 0.5) - high;
|
||||
_startTone(_pin, high, low, duration);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Fix ambiguous tone() binding when adding in a duration
|
||||
void tone(uint8_t _pin, int frequency, unsigned long duration)
|
||||
{
|
||||
// Call the unsigned int version of the function explicitly
|
||||
tone(_pin, (unsigned int)frequency, duration);
|
||||
void tone(uint8_t _pin, int frequency, unsigned long duration) {
|
||||
// Call the unsigned int version of the function explicitly
|
||||
tone(_pin, (unsigned int)frequency, duration);
|
||||
}
|
||||
|
||||
|
||||
void noTone(uint8_t _pin)
|
||||
{
|
||||
if (_pin > 16)
|
||||
{
|
||||
return;
|
||||
}
|
||||
stopWaveform(_pin);
|
||||
_toneMap &= ~(1 << _pin);
|
||||
digitalWrite(_pin, 0);
|
||||
void noTone(uint8_t _pin) {
|
||||
if (_pin > 16) {
|
||||
return;
|
||||
}
|
||||
stopWaveform(_pin);
|
||||
_toneMap &= ~(1 << _pin);
|
||||
digitalWrite(_pin, 0);
|
||||
}
|
||||
|
@ -1,36 +1,36 @@
|
||||
/*
|
||||
Udp.cpp: Library to send/receive UDP packets.
|
||||
|
||||
NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
|
||||
1) UDP does not guarantee the order in which assembled UDP packets are received. This
|
||||
might not happen often in practice, but in larger network topologies, a UDP
|
||||
packet can be received out of sequence.
|
||||
2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
|
||||
aware of it. Again, this may not be a concern in practice on small local networks.
|
||||
For more information, see http://www.cafeaulait.org/course/week12/35.html
|
||||
|
||||
MIT License:
|
||||
Copyright (c) 2008 Bjoern Hartmann
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in 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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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
|
||||
AUTHORS 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 IN
|
||||
THE SOFTWARE.
|
||||
|
||||
bjoern@cs.stanford.edu 12/30/2008
|
||||
*/
|
||||
* Udp.cpp: Library to send/receive UDP packets.
|
||||
*
|
||||
* NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
|
||||
* 1) UDP does not guarantee the order in which assembled UDP packets are received. This
|
||||
* might not happen often in practice, but in larger network topologies, a UDP
|
||||
* packet can be received out of sequence.
|
||||
* 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
|
||||
* aware of it. Again, this may not be a concern in practice on small local networks.
|
||||
* For more information, see http://www.cafeaulait.org/course/week12/35.html
|
||||
*
|
||||
* MIT License:
|
||||
* Copyright (c) 2008 Bjoern Hartmann
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in 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:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* 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
|
||||
* AUTHORS 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 IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* bjoern@cs.stanford.edu 12/30/2008
|
||||
*/
|
||||
|
||||
#ifndef udp_h
|
||||
#define udp_h
|
||||
@ -38,61 +38,58 @@
|
||||
#include <Stream.h>
|
||||
#include <IPAddress.h>
|
||||
|
||||
class UDP: public Stream
|
||||
{
|
||||
class UDP: public Stream {
|
||||
|
||||
public:
|
||||
virtual ~UDP() {};
|
||||
virtual uint8_t begin(uint16_t) = 0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
|
||||
virtual void stop() = 0; // Finish with the UDP socket
|
||||
public:
|
||||
virtual ~UDP() {};
|
||||
virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
|
||||
virtual void stop() =0; // Finish with the UDP socket
|
||||
|
||||
// Sending UDP packets
|
||||
// Sending UDP packets
|
||||
|
||||
// Start building up a packet to send to the remote host specific in ip and port
|
||||
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
|
||||
virtual int beginPacket(IPAddress ip, uint16_t port) = 0;
|
||||
// Start building up a packet to send to the remote host specific in host and port
|
||||
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
|
||||
virtual int beginPacket(const char *host, uint16_t port) = 0;
|
||||
// Finish off this packet and send it
|
||||
// Returns 1 if the packet was sent successfully, 0 if there was an error
|
||||
virtual int endPacket() = 0;
|
||||
// Write a single byte into the packet
|
||||
virtual size_t write(uint8_t) = 0;
|
||||
// Write size bytes from buffer into the packet
|
||||
virtual size_t write(const uint8_t *buffer, size_t size) = 0;
|
||||
// Start building up a packet to send to the remote host specific in ip and port
|
||||
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
|
||||
virtual int beginPacket(IPAddress ip, uint16_t port) =0;
|
||||
// Start building up a packet to send to the remote host specific in host and port
|
||||
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
|
||||
virtual int beginPacket(const char *host, uint16_t port) =0;
|
||||
// Finish off this packet and send it
|
||||
// Returns 1 if the packet was sent successfully, 0 if there was an error
|
||||
virtual int endPacket() =0;
|
||||
// Write a single byte into the packet
|
||||
virtual size_t write(uint8_t) =0;
|
||||
// Write size bytes from buffer into the packet
|
||||
virtual size_t write(const uint8_t *buffer, size_t size) =0;
|
||||
|
||||
// Start processing the next available incoming packet
|
||||
// Returns the size of the packet in bytes, or 0 if no packets are available
|
||||
virtual int parsePacket() = 0;
|
||||
// Number of bytes remaining in the current packet
|
||||
virtual int available() = 0;
|
||||
// Read a single byte from the current packet
|
||||
virtual int read() = 0;
|
||||
// Read up to len bytes from the current packet and place them into buffer
|
||||
// Returns the number of bytes read, or 0 if none are available
|
||||
virtual int read(unsigned char* buffer, size_t len) = 0;
|
||||
// Read up to len characters from the current packet and place them into buffer
|
||||
// Returns the number of characters read, or 0 if none are available
|
||||
virtual int read(char* buffer, size_t len) = 0;
|
||||
// Return the next byte from the current packet without moving on to the next byte
|
||||
virtual int peek() = 0;
|
||||
virtual void flush() = 0; // Finish reading the current packet
|
||||
// Start processing the next available incoming packet
|
||||
// Returns the size of the packet in bytes, or 0 if no packets are available
|
||||
virtual int parsePacket() =0;
|
||||
// Number of bytes remaining in the current packet
|
||||
virtual int available() =0;
|
||||
// Read a single byte from the current packet
|
||||
virtual int read() =0;
|
||||
// Read up to len bytes from the current packet and place them into buffer
|
||||
// Returns the number of bytes read, or 0 if none are available
|
||||
virtual int read(unsigned char* buffer, size_t len) =0;
|
||||
// Read up to len characters from the current packet and place them into buffer
|
||||
// Returns the number of characters read, or 0 if none are available
|
||||
virtual int read(char* buffer, size_t len) =0;
|
||||
// Return the next byte from the current packet without moving on to the next byte
|
||||
virtual int peek() =0;
|
||||
virtual void flush() =0; // Finish reading the current packet
|
||||
|
||||
// Return the IP address of the host who sent the current incoming packet
|
||||
virtual IPAddress remoteIP() = 0;
|
||||
// Return the port of the host who sent the current incoming packet
|
||||
virtual uint16_t remotePort() = 0;
|
||||
protected:
|
||||
uint8_t* rawIPAddress(IPAddress& addr)
|
||||
{
|
||||
return addr.raw_address();
|
||||
}
|
||||
// Return the IP address of the host who sent the current incoming packet
|
||||
virtual IPAddress remoteIP() =0;
|
||||
// Return the port of the host who sent the current incoming packet
|
||||
virtual uint16_t remotePort() =0;
|
||||
protected:
|
||||
uint8_t* rawIPAddress(IPAddress& addr) {
|
||||
return addr.raw_address();
|
||||
}
|
||||
#if LWIP_VERSION_MAJOR != 1
|
||||
const uint8_t* rawIPAddress(const IPAddress& addr)
|
||||
{
|
||||
return addr.raw_address();
|
||||
}
|
||||
const uint8_t* rawIPAddress(const IPAddress& addr) {
|
||||
return addr.raw_address();
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -31,9 +31,8 @@
|
||||
#endif
|
||||
|
||||
// Abstract class to implement whatever signing hash desired
|
||||
class UpdaterHashClass
|
||||
{
|
||||
public:
|
||||
class UpdaterHashClass {
|
||||
public:
|
||||
virtual void begin() = 0;
|
||||
virtual void add(const void *data, uint32_t len) = 0;
|
||||
virtual void end() = 0;
|
||||
@ -42,190 +41,137 @@ public:
|
||||
};
|
||||
|
||||
// Abstract class to implement a signature verifier
|
||||
class UpdaterVerifyClass
|
||||
{
|
||||
public:
|
||||
class UpdaterVerifyClass {
|
||||
public:
|
||||
virtual uint32_t length() = 0; // How many bytes of signature are expected
|
||||
virtual bool verify(UpdaterHashClass *hash, const void *signature, uint32_t signatureLen) = 0; // Verify, return "true" on success
|
||||
};
|
||||
|
||||
class UpdaterClass
|
||||
{
|
||||
public:
|
||||
class UpdaterClass {
|
||||
public:
|
||||
typedef std::function<void(size_t, size_t)> THandlerFunction_Progress;
|
||||
|
||||
UpdaterClass();
|
||||
|
||||
/* Optionally add a cryptographic signature verification hash and method */
|
||||
void installSignature(UpdaterHashClass *hash, UpdaterVerifyClass *verify)
|
||||
{
|
||||
_hash = hash;
|
||||
_verify = verify;
|
||||
}
|
||||
void installSignature(UpdaterHashClass *hash, UpdaterVerifyClass *verify) { _hash = hash; _verify = verify; }
|
||||
|
||||
/*
|
||||
Call this to check the space needed for the update
|
||||
Will return false if there is not enough space
|
||||
Call this to check the space needed for the update
|
||||
Will return false if there is not enough space
|
||||
*/
|
||||
bool begin(size_t size, int command = U_FLASH, int ledPin = -1, uint8_t ledOn = LOW);
|
||||
|
||||
/*
|
||||
Run Updater from asynchronous callbacs
|
||||
Run Updater from asynchronous callbacs
|
||||
*/
|
||||
void runAsync(bool async)
|
||||
{
|
||||
_async = async;
|
||||
}
|
||||
void runAsync(bool async){ _async = async; }
|
||||
|
||||
/*
|
||||
Writes a buffer to the flash and increments the address
|
||||
Returns the amount written
|
||||
Writes a buffer to the flash and increments the address
|
||||
Returns the amount written
|
||||
*/
|
||||
size_t write(uint8_t *data, size_t len);
|
||||
|
||||
/*
|
||||
Writes the remaining bytes from the Stream to the flash
|
||||
Uses readBytes() and sets UPDATE_ERROR_STREAM on timeout
|
||||
Returns the bytes written
|
||||
Should be equal to the remaining bytes when called
|
||||
Usable for slow streams like Serial
|
||||
Writes the remaining bytes from the Stream to the flash
|
||||
Uses readBytes() and sets UPDATE_ERROR_STREAM on timeout
|
||||
Returns the bytes written
|
||||
Should be equal to the remaining bytes when called
|
||||
Usable for slow streams like Serial
|
||||
*/
|
||||
size_t writeStream(Stream &data);
|
||||
|
||||
/*
|
||||
If all bytes are written
|
||||
this call will write the config to eboot
|
||||
and return true
|
||||
If there is already an update running but is not finished and !evenIfRemaining
|
||||
or there is an error
|
||||
this will clear everything and return false
|
||||
the last error is available through getError()
|
||||
evenIfRemaining is helpful when you update without knowing the final size first
|
||||
If all bytes are written
|
||||
this call will write the config to eboot
|
||||
and return true
|
||||
If there is already an update running but is not finished and !evenIfRemaining
|
||||
or there is an error
|
||||
this will clear everything and return false
|
||||
the last error is available through getError()
|
||||
evenIfRemaining is helpful when you update without knowing the final size first
|
||||
*/
|
||||
bool end(bool evenIfRemaining = false);
|
||||
|
||||
/*
|
||||
Prints the last error to an output stream
|
||||
Prints the last error to an output stream
|
||||
*/
|
||||
void printError(Print &out);
|
||||
|
||||
/*
|
||||
sets the expected MD5 for the firmware (hexString)
|
||||
sets the expected MD5 for the firmware (hexString)
|
||||
*/
|
||||
bool setMD5(const char * expected_md5);
|
||||
|
||||
/*
|
||||
returns the MD5 String of the sucessfully ended firmware
|
||||
returns the MD5 String of the sucessfully ended firmware
|
||||
*/
|
||||
String md5String(void)
|
||||
{
|
||||
return _md5.toString();
|
||||
}
|
||||
String md5String(void){ return _md5.toString(); }
|
||||
|
||||
/*
|
||||
populated the result with the md5 bytes of the sucessfully ended firmware
|
||||
populated the result with the md5 bytes of the sucessfully ended firmware
|
||||
*/
|
||||
void md5(uint8_t * result)
|
||||
{
|
||||
return _md5.getBytes(result);
|
||||
}
|
||||
void md5(uint8_t * result){ return _md5.getBytes(result); }
|
||||
|
||||
/*
|
||||
This callback will be called when Updater is receiving data
|
||||
This callback will be called when Updater is receiving data
|
||||
*/
|
||||
UpdaterClass& onProgress(THandlerFunction_Progress fn);
|
||||
|
||||
//Helpers
|
||||
uint8_t getError()
|
||||
{
|
||||
return _error;
|
||||
}
|
||||
void clearError()
|
||||
{
|
||||
_error = UPDATE_ERROR_OK;
|
||||
}
|
||||
bool hasError()
|
||||
{
|
||||
return _error != UPDATE_ERROR_OK;
|
||||
}
|
||||
bool isRunning()
|
||||
{
|
||||
return _size > 0;
|
||||
}
|
||||
bool isFinished()
|
||||
{
|
||||
return _currentAddress == (_startAddress + _size);
|
||||
}
|
||||
size_t size()
|
||||
{
|
||||
return _size;
|
||||
}
|
||||
size_t progress()
|
||||
{
|
||||
return _currentAddress - _startAddress;
|
||||
}
|
||||
size_t remaining()
|
||||
{
|
||||
return _size - (_currentAddress - _startAddress);
|
||||
}
|
||||
uint8_t getError(){ return _error; }
|
||||
void clearError(){ _error = UPDATE_ERROR_OK; }
|
||||
bool hasError(){ return _error != UPDATE_ERROR_OK; }
|
||||
bool isRunning(){ return _size > 0; }
|
||||
bool isFinished(){ return _currentAddress == (_startAddress + _size); }
|
||||
size_t size(){ return _size; }
|
||||
size_t progress(){ return _currentAddress - _startAddress; }
|
||||
size_t remaining(){ return _size - (_currentAddress - _startAddress); }
|
||||
|
||||
/*
|
||||
Template to write from objects that expose
|
||||
available() and read(uint8_t*, size_t) methods
|
||||
faster than the writeStream method
|
||||
writes only what is available
|
||||
Template to write from objects that expose
|
||||
available() and read(uint8_t*, size_t) methods
|
||||
faster than the writeStream method
|
||||
writes only what is available
|
||||
*/
|
||||
template<typename T>
|
||||
size_t write(T &data)
|
||||
{
|
||||
size_t written = 0;
|
||||
if (hasError() || !isRunning())
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
size_t write(T &data){
|
||||
size_t written = 0;
|
||||
if (hasError() || !isRunning())
|
||||
return 0;
|
||||
|
||||
size_t available = data.available();
|
||||
while (available)
|
||||
{
|
||||
if (_bufferLen + available > remaining())
|
||||
{
|
||||
available = remaining() - _bufferLen;
|
||||
}
|
||||
if (_bufferLen + available > _bufferSize)
|
||||
{
|
||||
size_t toBuff = _bufferSize - _bufferLen;
|
||||
data.read(_buffer + _bufferLen, toBuff);
|
||||
_bufferLen += toBuff;
|
||||
if (!_writeBuffer())
|
||||
{
|
||||
return written;
|
||||
}
|
||||
written += toBuff;
|
||||
}
|
||||
else
|
||||
{
|
||||
data.read(_buffer + _bufferLen, available);
|
||||
_bufferLen += available;
|
||||
written += available;
|
||||
if (_bufferLen == remaining())
|
||||
{
|
||||
if (!_writeBuffer())
|
||||
{
|
||||
return written;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (remaining() == 0)
|
||||
{
|
||||
return written;
|
||||
}
|
||||
delay(1);
|
||||
available = data.available();
|
||||
size_t available = data.available();
|
||||
while(available) {
|
||||
if(_bufferLen + available > remaining()){
|
||||
available = remaining() - _bufferLen;
|
||||
}
|
||||
return written;
|
||||
if(_bufferLen + available > _bufferSize) {
|
||||
size_t toBuff = _bufferSize - _bufferLen;
|
||||
data.read(_buffer + _bufferLen, toBuff);
|
||||
_bufferLen += toBuff;
|
||||
if(!_writeBuffer())
|
||||
return written;
|
||||
written += toBuff;
|
||||
} else {
|
||||
data.read(_buffer + _bufferLen, available);
|
||||
_bufferLen += available;
|
||||
written += available;
|
||||
if(_bufferLen == remaining()) {
|
||||
if(!_writeBuffer()) {
|
||||
return written;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(remaining() == 0)
|
||||
return written;
|
||||
delay(1);
|
||||
available = data.available();
|
||||
}
|
||||
return written;
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
void _reset();
|
||||
bool _writeBuffer();
|
||||
|
||||
|
@ -1,21 +1,21 @@
|
||||
/*
|
||||
WCharacter.h - Character utility functions for Wiring & Arduino
|
||||
Copyright (c) 2010 Hernando Barragan. All right reserved.
|
||||
WCharacter.h - Character utility functions for Wiring & Arduino
|
||||
Copyright (c) 2010 Hernando Barragan. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef Character_h
|
||||
#define Character_h
|
||||
@ -46,93 +46,79 @@ inline int toUpperCase(int c) __attribute__((always_inline));
|
||||
|
||||
// Checks for an alphanumeric character.
|
||||
// It is equivalent to (isalpha(c) || isdigit(c)).
|
||||
inline boolean isAlphaNumeric(int c)
|
||||
{
|
||||
inline boolean isAlphaNumeric(int c) {
|
||||
return (isalnum(c) == 0 ? false : true);
|
||||
}
|
||||
|
||||
// Checks for an alphabetic character.
|
||||
// It is equivalent to (isupper(c) || islower(c)).
|
||||
inline boolean isAlpha(int c)
|
||||
{
|
||||
inline boolean isAlpha(int c) {
|
||||
return (isalpha(c) == 0 ? false : true);
|
||||
}
|
||||
|
||||
// Checks whether c is a 7-bit unsigned char value
|
||||
// that fits into the ASCII character set.
|
||||
inline boolean isAscii(int c)
|
||||
{
|
||||
return (isascii(c) == 0 ? false : true);
|
||||
inline boolean isAscii(int c) {
|
||||
return ( isascii (c) == 0 ? false : true);
|
||||
}
|
||||
|
||||
// Checks for a blank character, that is, a space or a tab.
|
||||
inline boolean isWhitespace(int c)
|
||||
{
|
||||
inline boolean isWhitespace(int c) {
|
||||
return (isblank(c) == 0 ? false : true);
|
||||
}
|
||||
|
||||
// Checks for a control character.
|
||||
inline boolean isControl(int c)
|
||||
{
|
||||
inline boolean isControl(int c) {
|
||||
return (iscntrl(c) == 0 ? false : true);
|
||||
}
|
||||
|
||||
// Checks for a digit (0 through 9).
|
||||
inline boolean isDigit(int c)
|
||||
{
|
||||
inline boolean isDigit(int c) {
|
||||
return (isdigit(c) == 0 ? false : true);
|
||||
}
|
||||
|
||||
// Checks for any printable character except space.
|
||||
inline boolean isGraph(int c)
|
||||
{
|
||||
inline boolean isGraph(int c) {
|
||||
return (isgraph(c) == 0 ? false : true);
|
||||
}
|
||||
|
||||
// Checks for a lower-case character.
|
||||
inline boolean isLowerCase(int c)
|
||||
{
|
||||
inline boolean isLowerCase(int c) {
|
||||
return (islower(c) == 0 ? false : true);
|
||||
}
|
||||
|
||||
// Checks for any printable character including space.
|
||||
inline boolean isPrintable(int c)
|
||||
{
|
||||
inline boolean isPrintable(int c) {
|
||||
return (isprint(c) == 0 ? false : true);
|
||||
}
|
||||
|
||||
// Checks for any printable character which is not a space
|
||||
// or an alphanumeric character.
|
||||
inline boolean isPunct(int c)
|
||||
{
|
||||
inline boolean isPunct(int c) {
|
||||
return (ispunct(c) == 0 ? false : true);
|
||||
}
|
||||
|
||||
// Checks for white-space characters. For the avr-libc library,
|
||||
// these are: space, formfeed ('\f'), newline ('\n'), carriage
|
||||
// return ('\r'), horizontal tab ('\t'), and vertical tab ('\v').
|
||||
inline boolean isSpace(int c)
|
||||
{
|
||||
inline boolean isSpace(int c) {
|
||||
return (isspace(c) == 0 ? false : true);
|
||||
}
|
||||
|
||||
// Checks for an uppercase letter.
|
||||
inline boolean isUpperCase(int c)
|
||||
{
|
||||
inline boolean isUpperCase(int c) {
|
||||
return (isupper(c) == 0 ? false : true);
|
||||
}
|
||||
|
||||
// Checks for a hexadecimal digits, i.e. one of 0 1 2 3 4 5 6 7
|
||||
// 8 9 a b c d e f A B C D E F.
|
||||
inline boolean isHexadecimalDigit(int c)
|
||||
{
|
||||
inline boolean isHexadecimalDigit(int c) {
|
||||
return (isxdigit(c) == 0 ? false : true);
|
||||
}
|
||||
|
||||
// Converts c to a 7-bit unsigned char value that fits into the
|
||||
// ASCII character set, by clearing the high-order bits.
|
||||
inline int toAscii(int c)
|
||||
{
|
||||
inline int toAscii(int c) {
|
||||
return toascii(c);
|
||||
}
|
||||
|
||||
@ -142,14 +128,12 @@ inline int toAscii(int c)
|
||||
// characters.
|
||||
|
||||
// Converts the letter c to lower case, if possible.
|
||||
inline int toLowerCase(int c)
|
||||
{
|
||||
inline int toLowerCase(int c) {
|
||||
return tolower(c);
|
||||
}
|
||||
|
||||
// Converts the letter c to upper case, if possible.
|
||||
inline int toUpperCase(int c)
|
||||
{
|
||||
inline int toUpperCase(int c) {
|
||||
return toupper(c);
|
||||
}
|
||||
|
||||
|
@ -1,27 +1,27 @@
|
||||
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
||||
|
||||
/*
|
||||
Part of the Wiring project - http://wiring.org.co
|
||||
Copyright (c) 2004-06 Hernando Barragan
|
||||
Modified 13 August 2006, David A. Mellis for Arduino - http://www.arduino.cc/
|
||||
Part of the Wiring project - http://wiring.org.co
|
||||
Copyright (c) 2004-06 Hernando Barragan
|
||||
Modified 13 August 2006, David A. Mellis for Arduino - http://www.arduino.cc/
|
||||
|
||||
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 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.
|
||||
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., 59 Temple Place, Suite 330,
|
||||
Boston, MA 02111-1307 USA
|
||||
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., 59 Temple Place, Suite 330,
|
||||
Boston, MA 02111-1307 USA
|
||||
|
||||
$Id$
|
||||
*/
|
||||
$Id$
|
||||
*/
|
||||
|
||||
extern "C" {
|
||||
#include <stdlib.h>
|
||||
@ -30,19 +30,15 @@ extern "C" {
|
||||
|
||||
static bool s_randomSeedCalled = false;
|
||||
|
||||
void randomSeed(unsigned long seed)
|
||||
{
|
||||
if (seed != 0)
|
||||
{
|
||||
void randomSeed(unsigned long seed) {
|
||||
if(seed != 0) {
|
||||
srand(seed);
|
||||
s_randomSeedCalled = true;
|
||||
}
|
||||
}
|
||||
|
||||
long random(long howbig)
|
||||
{
|
||||
if (howbig == 0)
|
||||
{
|
||||
long random(long howbig) {
|
||||
if(howbig == 0) {
|
||||
return 0;
|
||||
}
|
||||
// if randomSeed was called, fall back to software PRNG
|
||||
@ -50,51 +46,41 @@ long random(long howbig)
|
||||
return val % howbig;
|
||||
}
|
||||
|
||||
long random(long howsmall, long howbig)
|
||||
{
|
||||
if (howsmall >= howbig)
|
||||
{
|
||||
long random(long howsmall, long howbig) {
|
||||
if(howsmall >= howbig) {
|
||||
return howsmall;
|
||||
}
|
||||
long diff = howbig - howsmall;
|
||||
return random(diff) + howsmall;
|
||||
}
|
||||
|
||||
long secureRandom(long howbig)
|
||||
{
|
||||
if (howbig == 0)
|
||||
{
|
||||
long secureRandom(long howbig) {
|
||||
if(howbig == 0) {
|
||||
return 0;
|
||||
}
|
||||
return RANDOM_REG32 % howbig;
|
||||
}
|
||||
|
||||
long secureRandom(long howsmall, long howbig)
|
||||
{
|
||||
if (howsmall >= howbig)
|
||||
{
|
||||
long secureRandom(long howsmall, long howbig) {
|
||||
if(howsmall >= howbig) {
|
||||
return howsmall;
|
||||
}
|
||||
long diff = howbig - howsmall;
|
||||
return secureRandom(diff) + howsmall;
|
||||
}
|
||||
|
||||
long map(long x, long in_min, long in_max, long out_min, long out_max)
|
||||
{
|
||||
long map(long x, long in_min, long in_max, long out_min, long out_max) {
|
||||
long divisor = (in_max - in_min);
|
||||
if (divisor == 0)
|
||||
{
|
||||
if(divisor == 0){
|
||||
return -1; //AVR returns -1, SAM returns 0
|
||||
}
|
||||
return (x - in_min) * (out_max - out_min) / divisor + out_min;
|
||||
}
|
||||
|
||||
unsigned int makeWord(unsigned int w)
|
||||
{
|
||||
unsigned int makeWord(unsigned int w) {
|
||||
return w;
|
||||
}
|
||||
|
||||
unsigned int makeWord(unsigned char h, unsigned char l)
|
||||
{
|
||||
unsigned int makeWord(unsigned char h, unsigned char l) {
|
||||
return (h << 8) | l;
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,23 +1,23 @@
|
||||
/*
|
||||
WString.h - String library for Wiring & Arduino
|
||||
...mostly rewritten by Paul Stoffregen...
|
||||
Copyright (c) 2009-10 Hernando Barragan. All right reserved.
|
||||
Copyright 2011, Paul Stoffregen, paul@pjrc.com
|
||||
WString.h - String library for Wiring & Arduino
|
||||
...mostly rewritten by Paul Stoffregen...
|
||||
Copyright (c) 2009-10 Hernando Barragan. All right reserved.
|
||||
Copyright 2011, Paul Stoffregen, paul@pjrc.com
|
||||
|
||||
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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef String_class_h
|
||||
#define String_class_h
|
||||
@ -39,373 +39,285 @@ class __FlashStringHelper;
|
||||
#define F(string_literal) (FPSTR(PSTR(string_literal)))
|
||||
|
||||
// The string class
|
||||
class String
|
||||
{
|
||||
// use a function pointer to allow for "if (s)" without the
|
||||
// complications of an operator bool(). for more information, see:
|
||||
// http://www.artima.com/cppsource/safebool.html
|
||||
typedef void (String::*StringIfHelperType)() const;
|
||||
void StringIfHelper() const
|
||||
{
|
||||
}
|
||||
class String {
|
||||
// use a function pointer to allow for "if (s)" without the
|
||||
// complications of an operator bool(). for more information, see:
|
||||
// http://www.artima.com/cppsource/safebool.html
|
||||
typedef void (String::*StringIfHelperType)() const;
|
||||
void StringIfHelper() const {
|
||||
}
|
||||
|
||||
public:
|
||||
// constructors
|
||||
// creates a copy of the initial value.
|
||||
// if the initial value is null or invalid, or if memory allocation
|
||||
// fails, the string will be marked as invalid (i.e. "if (s)" will
|
||||
// be false).
|
||||
String(const char *cstr = "");
|
||||
String(const String &str);
|
||||
String(const __FlashStringHelper *str);
|
||||
public:
|
||||
// constructors
|
||||
// creates a copy of the initial value.
|
||||
// if the initial value is null or invalid, or if memory allocation
|
||||
// fails, the string will be marked as invalid (i.e. "if (s)" will
|
||||
// be false).
|
||||
String(const char *cstr = "");
|
||||
String(const String &str);
|
||||
String(const __FlashStringHelper *str);
|
||||
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
||||
String(String &&rval);
|
||||
String(StringSumHelper &&rval);
|
||||
String(String &&rval);
|
||||
String(StringSumHelper &&rval);
|
||||
#endif
|
||||
explicit String(char c);
|
||||
explicit String(unsigned char, unsigned char base = 10);
|
||||
explicit String(int, unsigned char base = 10);
|
||||
explicit String(unsigned int, unsigned char base = 10);
|
||||
explicit String(long, unsigned char base = 10);
|
||||
explicit String(unsigned long, unsigned char base = 10);
|
||||
explicit String(float, unsigned char decimalPlaces = 2);
|
||||
explicit String(double, unsigned char decimalPlaces = 2);
|
||||
~String(void);
|
||||
explicit String(char c);
|
||||
explicit String(unsigned char, unsigned char base = 10);
|
||||
explicit String(int, unsigned char base = 10);
|
||||
explicit String(unsigned int, unsigned char base = 10);
|
||||
explicit String(long, unsigned char base = 10);
|
||||
explicit String(unsigned long, unsigned char base = 10);
|
||||
explicit String(float, unsigned char decimalPlaces = 2);
|
||||
explicit String(double, unsigned char decimalPlaces = 2);
|
||||
~String(void);
|
||||
|
||||
// memory management
|
||||
// return true on success, false on failure (in which case, the string
|
||||
// is left unchanged). reserve(0), if successful, will validate an
|
||||
// invalid string (i.e., "if (s)" will be true afterwards)
|
||||
unsigned char reserve(unsigned int size);
|
||||
inline unsigned int length(void) const
|
||||
{
|
||||
if (buffer())
|
||||
{
|
||||
return len();
|
||||
// memory management
|
||||
// return true on success, false on failure (in which case, the string
|
||||
// is left unchanged). reserve(0), if successful, will validate an
|
||||
// invalid string (i.e., "if (s)" will be true afterwards)
|
||||
unsigned char reserve(unsigned int size);
|
||||
inline unsigned int length(void) const {
|
||||
if(buffer()) {
|
||||
return len();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// creates a copy of the assigned value. if the value is null or
|
||||
// invalid, or if the memory allocation fails, the string will be
|
||||
// marked as invalid ("if (s)" will be false).
|
||||
String & operator =(const String &rhs);
|
||||
String & operator =(const char *cstr);
|
||||
String & operator = (const __FlashStringHelper *str);
|
||||
// creates a copy of the assigned value. if the value is null or
|
||||
// invalid, or if the memory allocation fails, the string will be
|
||||
// marked as invalid ("if (s)" will be false).
|
||||
String & operator =(const String &rhs);
|
||||
String & operator =(const char *cstr);
|
||||
String & operator = (const __FlashStringHelper *str);
|
||||
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
||||
String & operator =(String &&rval);
|
||||
String & operator =(StringSumHelper &&rval);
|
||||
String & operator =(String &&rval);
|
||||
String & operator =(StringSumHelper &&rval);
|
||||
#endif
|
||||
|
||||
// concatenate (works w/ built-in types)
|
||||
// concatenate (works w/ built-in types)
|
||||
|
||||
// returns true on success, false on failure (in which case, the string
|
||||
// is left unchanged). if the argument is null or invalid, the
|
||||
// concatenation is considered unsuccessful.
|
||||
unsigned char concat(const String &str);
|
||||
unsigned char concat(const char *cstr);
|
||||
unsigned char concat(char c);
|
||||
unsigned char concat(unsigned char c);
|
||||
unsigned char concat(int num);
|
||||
unsigned char concat(unsigned int num);
|
||||
unsigned char concat(long num);
|
||||
unsigned char concat(unsigned long num);
|
||||
unsigned char concat(float num);
|
||||
unsigned char concat(double num);
|
||||
unsigned char concat(const __FlashStringHelper * str);
|
||||
// returns true on success, false on failure (in which case, the string
|
||||
// is left unchanged). if the argument is null or invalid, the
|
||||
// concatenation is considered unsuccessful.
|
||||
unsigned char concat(const String &str);
|
||||
unsigned char concat(const char *cstr);
|
||||
unsigned char concat(char c);
|
||||
unsigned char concat(unsigned char c);
|
||||
unsigned char concat(int num);
|
||||
unsigned char concat(unsigned int num);
|
||||
unsigned char concat(long num);
|
||||
unsigned char concat(unsigned long num);
|
||||
unsigned char concat(float num);
|
||||
unsigned char concat(double num);
|
||||
unsigned char concat(const __FlashStringHelper * str);
|
||||
|
||||
// if there's not enough memory for the concatenated value, the string
|
||||
// will be left unchanged (but this isn't signalled in any way)
|
||||
String & operator +=(const String &rhs)
|
||||
{
|
||||
concat(rhs);
|
||||
return (*this);
|
||||
}
|
||||
String & operator +=(const char *cstr)
|
||||
{
|
||||
concat(cstr);
|
||||
return (*this);
|
||||
}
|
||||
String & operator +=(char c)
|
||||
{
|
||||
concat(c);
|
||||
return (*this);
|
||||
}
|
||||
String & operator +=(unsigned char num)
|
||||
{
|
||||
concat(num);
|
||||
return (*this);
|
||||
}
|
||||
String & operator +=(int num)
|
||||
{
|
||||
concat(num);
|
||||
return (*this);
|
||||
}
|
||||
String & operator +=(unsigned int num)
|
||||
{
|
||||
concat(num);
|
||||
return (*this);
|
||||
}
|
||||
String & operator +=(long num)
|
||||
{
|
||||
concat(num);
|
||||
return (*this);
|
||||
}
|
||||
String & operator +=(unsigned long num)
|
||||
{
|
||||
concat(num);
|
||||
return (*this);
|
||||
}
|
||||
String & operator +=(float num)
|
||||
{
|
||||
concat(num);
|
||||
return (*this);
|
||||
}
|
||||
String & operator +=(double num)
|
||||
{
|
||||
concat(num);
|
||||
return (*this);
|
||||
}
|
||||
String & operator += (const __FlashStringHelper *str)
|
||||
{
|
||||
concat(str);
|
||||
return (*this);
|
||||
}
|
||||
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, const String &rhs);
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, const char *cstr);
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, char c);
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned char num);
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, int num);
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned int num);
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, long num);
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned long num);
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, float num);
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, double num);
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs);
|
||||
|
||||
// comparison (only works w/ Strings and "strings")
|
||||
operator StringIfHelperType() const
|
||||
{
|
||||
return buffer() ? &String::StringIfHelper : 0;
|
||||
}
|
||||
int compareTo(const String &s) const;
|
||||
unsigned char equals(const String &s) const;
|
||||
unsigned char equals(const char *cstr) const;
|
||||
unsigned char operator ==(const String &rhs) const
|
||||
{
|
||||
return equals(rhs);
|
||||
}
|
||||
unsigned char operator ==(const char *cstr) const
|
||||
{
|
||||
return equals(cstr);
|
||||
}
|
||||
unsigned char operator !=(const String &rhs) const
|
||||
{
|
||||
return !equals(rhs);
|
||||
}
|
||||
unsigned char operator !=(const char *cstr) const
|
||||
{
|
||||
return !equals(cstr);
|
||||
}
|
||||
unsigned char operator <(const String &rhs) const;
|
||||
unsigned char operator >(const String &rhs) const;
|
||||
unsigned char operator <=(const String &rhs) const;
|
||||
unsigned char operator >=(const String &rhs) const;
|
||||
unsigned char equalsIgnoreCase(const String &s) const;
|
||||
unsigned char equalsConstantTime(const String &s) const;
|
||||
unsigned char startsWith(const String &prefix) const;
|
||||
unsigned char startsWith(const String &prefix, unsigned int offset) const;
|
||||
unsigned char endsWith(const String &suffix) const;
|
||||
|
||||
// character access
|
||||
char charAt(unsigned int index) const;
|
||||
void setCharAt(unsigned int index, char c);
|
||||
char operator [](unsigned int index) const;
|
||||
char& operator [](unsigned int index);
|
||||
void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index = 0) const;
|
||||
void toCharArray(char *buf, unsigned int bufsize, unsigned int index = 0) const
|
||||
{
|
||||
getBytes((unsigned char *) buf, bufsize, index);
|
||||
}
|
||||
const char* c_str() const
|
||||
{
|
||||
return buffer();
|
||||
}
|
||||
char* begin()
|
||||
{
|
||||
return wbuffer();
|
||||
}
|
||||
char* end()
|
||||
{
|
||||
return wbuffer() + length();
|
||||
}
|
||||
const char* begin() const
|
||||
{
|
||||
return c_str();
|
||||
}
|
||||
const char* end() const
|
||||
{
|
||||
return c_str() + length();
|
||||
}
|
||||
|
||||
// search
|
||||
int indexOf(char ch) const;
|
||||
int indexOf(char ch, unsigned int fromIndex) const;
|
||||
int indexOf(const String &str) const;
|
||||
int indexOf(const String &str, unsigned int fromIndex) const;
|
||||
int lastIndexOf(char ch) const;
|
||||
int lastIndexOf(char ch, unsigned int fromIndex) const;
|
||||
int lastIndexOf(const String &str) const;
|
||||
int lastIndexOf(const String &str, unsigned int fromIndex) const;
|
||||
String substring(unsigned int beginIndex) const
|
||||
{
|
||||
return substring(beginIndex, len());
|
||||
}
|
||||
;
|
||||
String substring(unsigned int beginIndex, unsigned int endIndex) const;
|
||||
|
||||
// modification
|
||||
void replace(char find, char replace);
|
||||
void replace(const String& find, const String& replace);
|
||||
void remove(unsigned int index);
|
||||
void remove(unsigned int index, unsigned int count);
|
||||
void toLowerCase(void);
|
||||
void toUpperCase(void);
|
||||
void trim(void);
|
||||
|
||||
// parsing/conversion
|
||||
long toInt(void) const;
|
||||
float toFloat(void) const;
|
||||
double toDouble(void) const;
|
||||
|
||||
protected:
|
||||
// Contains the string info when we're not in SSO mode
|
||||
struct _ptr
|
||||
{
|
||||
char * buff;
|
||||
uint16_t cap;
|
||||
uint16_t len;
|
||||
};
|
||||
|
||||
// SSO is handled by checking the last byte of sso_buff.
|
||||
// When not in SSO mode, that byte is set to 0xff, while when in SSO mode it is always 0x00 (so it can serve as the string terminator as well as a flag)
|
||||
// This allows strings up up to 12 (11 + \0 termination) without any extra space.
|
||||
enum { SSOSIZE = sizeof(struct _ptr) + 4 }; // Characters to allocate space for SSO, must be 12 or more
|
||||
enum { CAPACITY_MAX = 65535 }; // If size of capacity changed, be sure to update this enum
|
||||
union
|
||||
{
|
||||
struct _ptr ptr;
|
||||
char sso_buf[SSOSIZE];
|
||||
};
|
||||
// Accessor functions
|
||||
inline bool sso() const
|
||||
{
|
||||
return sso_buf[SSOSIZE - 1] == 0;
|
||||
}
|
||||
inline unsigned int len() const
|
||||
{
|
||||
return sso() ? strlen(sso_buf) : ptr.len;
|
||||
}
|
||||
inline unsigned int capacity() const
|
||||
{
|
||||
return sso() ? SSOSIZE - 1 : ptr.cap;
|
||||
}
|
||||
inline void setSSO(bool sso)
|
||||
{
|
||||
sso_buf[SSOSIZE - 1] = sso ? 0x00 : 0xff;
|
||||
}
|
||||
inline void setLen(int len)
|
||||
{
|
||||
if (!sso())
|
||||
{
|
||||
ptr.len = len;
|
||||
// if there's not enough memory for the concatenated value, the string
|
||||
// will be left unchanged (but this isn't signalled in any way)
|
||||
String & operator +=(const String &rhs) {
|
||||
concat(rhs);
|
||||
return (*this);
|
||||
}
|
||||
}
|
||||
inline void setCapacity(int cap)
|
||||
{
|
||||
if (!sso())
|
||||
{
|
||||
ptr.cap = cap;
|
||||
String & operator +=(const char *cstr) {
|
||||
concat(cstr);
|
||||
return (*this);
|
||||
}
|
||||
}
|
||||
inline void setBuffer(char *buff)
|
||||
{
|
||||
if (!sso())
|
||||
{
|
||||
ptr.buff = buff;
|
||||
String & operator +=(char c) {
|
||||
concat(c);
|
||||
return (*this);
|
||||
}
|
||||
String & operator +=(unsigned char num) {
|
||||
concat(num);
|
||||
return (*this);
|
||||
}
|
||||
String & operator +=(int num) {
|
||||
concat(num);
|
||||
return (*this);
|
||||
}
|
||||
String & operator +=(unsigned int num) {
|
||||
concat(num);
|
||||
return (*this);
|
||||
}
|
||||
String & operator +=(long num) {
|
||||
concat(num);
|
||||
return (*this);
|
||||
}
|
||||
String & operator +=(unsigned long num) {
|
||||
concat(num);
|
||||
return (*this);
|
||||
}
|
||||
String & operator +=(float num) {
|
||||
concat(num);
|
||||
return (*this);
|
||||
}
|
||||
String & operator +=(double num) {
|
||||
concat(num);
|
||||
return (*this);
|
||||
}
|
||||
String & operator += (const __FlashStringHelper *str){
|
||||
concat(str);
|
||||
return (*this);
|
||||
}
|
||||
}
|
||||
// Buffer accessor functions
|
||||
inline const char *buffer() const
|
||||
{
|
||||
return (const char *)(sso() ? sso_buf : ptr.buff);
|
||||
}
|
||||
inline char *wbuffer() const
|
||||
{
|
||||
return sso() ? const_cast<char *>(sso_buf) : ptr.buff; // Writable version of buffer
|
||||
}
|
||||
|
||||
protected:
|
||||
void init(void);
|
||||
void invalidate(void);
|
||||
unsigned char changeBuffer(unsigned int maxStrLen);
|
||||
unsigned char concat(const char *cstr, unsigned int length);
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, const String &rhs);
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, const char *cstr);
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, char c);
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned char num);
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, int num);
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned int num);
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, long num);
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned long num);
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, float num);
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, double num);
|
||||
friend StringSumHelper & operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs);
|
||||
|
||||
// copy and move
|
||||
String & copy(const char *cstr, unsigned int length);
|
||||
String & copy(const __FlashStringHelper *pstr, unsigned int length);
|
||||
// comparison (only works w/ Strings and "strings")
|
||||
operator StringIfHelperType() const {
|
||||
return buffer() ? &String::StringIfHelper : 0;
|
||||
}
|
||||
int compareTo(const String &s) const;
|
||||
unsigned char equals(const String &s) const;
|
||||
unsigned char equals(const char *cstr) const;
|
||||
unsigned char operator ==(const String &rhs) const {
|
||||
return equals(rhs);
|
||||
}
|
||||
unsigned char operator ==(const char *cstr) const {
|
||||
return equals(cstr);
|
||||
}
|
||||
unsigned char operator !=(const String &rhs) const {
|
||||
return !equals(rhs);
|
||||
}
|
||||
unsigned char operator !=(const char *cstr) const {
|
||||
return !equals(cstr);
|
||||
}
|
||||
unsigned char operator <(const String &rhs) const;
|
||||
unsigned char operator >(const String &rhs) const;
|
||||
unsigned char operator <=(const String &rhs) const;
|
||||
unsigned char operator >=(const String &rhs) const;
|
||||
unsigned char equalsIgnoreCase(const String &s) const;
|
||||
unsigned char equalsConstantTime(const String &s) const;
|
||||
unsigned char startsWith(const String &prefix) const;
|
||||
unsigned char startsWith(const String &prefix, unsigned int offset) const;
|
||||
unsigned char endsWith(const String &suffix) const;
|
||||
|
||||
// character access
|
||||
char charAt(unsigned int index) const;
|
||||
void setCharAt(unsigned int index, char c);
|
||||
char operator [](unsigned int index) const;
|
||||
char& operator [](unsigned int index);
|
||||
void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index = 0) const;
|
||||
void toCharArray(char *buf, unsigned int bufsize, unsigned int index = 0) const {
|
||||
getBytes((unsigned char *) buf, bufsize, index);
|
||||
}
|
||||
const char* c_str() const { return buffer(); }
|
||||
char* begin() { return wbuffer(); }
|
||||
char* end() { return wbuffer() + length(); }
|
||||
const char* begin() const { return c_str(); }
|
||||
const char* end() const { return c_str() + length(); }
|
||||
|
||||
// search
|
||||
int indexOf(char ch) const;
|
||||
int indexOf(char ch, unsigned int fromIndex) const;
|
||||
int indexOf(const String &str) const;
|
||||
int indexOf(const String &str, unsigned int fromIndex) const;
|
||||
int lastIndexOf(char ch) const;
|
||||
int lastIndexOf(char ch, unsigned int fromIndex) const;
|
||||
int lastIndexOf(const String &str) const;
|
||||
int lastIndexOf(const String &str, unsigned int fromIndex) const;
|
||||
String substring(unsigned int beginIndex) const {
|
||||
return substring(beginIndex, len());
|
||||
}
|
||||
;
|
||||
String substring(unsigned int beginIndex, unsigned int endIndex) const;
|
||||
|
||||
// modification
|
||||
void replace(char find, char replace);
|
||||
void replace(const String& find, const String& replace);
|
||||
void remove(unsigned int index);
|
||||
void remove(unsigned int index, unsigned int count);
|
||||
void toLowerCase(void);
|
||||
void toUpperCase(void);
|
||||
void trim(void);
|
||||
|
||||
// parsing/conversion
|
||||
long toInt(void) const;
|
||||
float toFloat(void) const;
|
||||
double toDouble(void) const;
|
||||
|
||||
protected:
|
||||
// Contains the string info when we're not in SSO mode
|
||||
struct _ptr {
|
||||
char * buff;
|
||||
uint16_t cap;
|
||||
uint16_t len;
|
||||
};
|
||||
|
||||
// SSO is handled by checking the last byte of sso_buff.
|
||||
// When not in SSO mode, that byte is set to 0xff, while when in SSO mode it is always 0x00 (so it can serve as the string terminator as well as a flag)
|
||||
// This allows strings up up to 12 (11 + \0 termination) without any extra space.
|
||||
enum { SSOSIZE = sizeof(struct _ptr) + 4 }; // Characters to allocate space for SSO, must be 12 or more
|
||||
enum { CAPACITY_MAX = 65535 }; // If size of capacity changed, be sure to update this enum
|
||||
union {
|
||||
struct _ptr ptr;
|
||||
char sso_buf[SSOSIZE];
|
||||
};
|
||||
// Accessor functions
|
||||
inline bool sso() const { return sso_buf[SSOSIZE - 1] == 0; }
|
||||
inline unsigned int len() const { return sso() ? strlen(sso_buf) : ptr.len; }
|
||||
inline unsigned int capacity() const { return sso() ? SSOSIZE - 1 : ptr.cap; }
|
||||
inline void setSSO(bool sso) { sso_buf[SSOSIZE - 1] = sso ? 0x00 : 0xff; }
|
||||
inline void setLen(int len) { if (!sso()) ptr.len = len; }
|
||||
inline void setCapacity(int cap) { if (!sso()) ptr.cap = cap; }
|
||||
inline void setBuffer(char *buff) { if (!sso()) ptr.buff = buff; }
|
||||
// Buffer accessor functions
|
||||
inline const char *buffer() const { return (const char *)(sso() ? sso_buf : ptr.buff); }
|
||||
inline char *wbuffer() const { return sso() ? const_cast<char *>(sso_buf) : ptr.buff; } // Writable version of buffer
|
||||
|
||||
protected:
|
||||
void init(void);
|
||||
void invalidate(void);
|
||||
unsigned char changeBuffer(unsigned int maxStrLen);
|
||||
unsigned char concat(const char *cstr, unsigned int length);
|
||||
|
||||
// copy and move
|
||||
String & copy(const char *cstr, unsigned int length);
|
||||
String & copy(const __FlashStringHelper *pstr, unsigned int length);
|
||||
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
||||
void move(String &rhs);
|
||||
void move(String &rhs);
|
||||
#endif
|
||||
};
|
||||
|
||||
class StringSumHelper: public String
|
||||
{
|
||||
public:
|
||||
StringSumHelper(const String &s) :
|
||||
String(s)
|
||||
{
|
||||
}
|
||||
StringSumHelper(const char *p) :
|
||||
String(p)
|
||||
{
|
||||
}
|
||||
StringSumHelper(char c) :
|
||||
String(c)
|
||||
{
|
||||
}
|
||||
StringSumHelper(unsigned char num) :
|
||||
String(num)
|
||||
{
|
||||
}
|
||||
StringSumHelper(int num) :
|
||||
String(num)
|
||||
{
|
||||
}
|
||||
StringSumHelper(unsigned int num) :
|
||||
String(num)
|
||||
{
|
||||
}
|
||||
StringSumHelper(long num) :
|
||||
String(num)
|
||||
{
|
||||
}
|
||||
StringSumHelper(unsigned long num) :
|
||||
String(num)
|
||||
{
|
||||
}
|
||||
StringSumHelper(float num) :
|
||||
String(num)
|
||||
{
|
||||
}
|
||||
StringSumHelper(double num) :
|
||||
String(num)
|
||||
{
|
||||
}
|
||||
class StringSumHelper: public String {
|
||||
public:
|
||||
StringSumHelper(const String &s) :
|
||||
String(s) {
|
||||
}
|
||||
StringSumHelper(const char *p) :
|
||||
String(p) {
|
||||
}
|
||||
StringSumHelper(char c) :
|
||||
String(c) {
|
||||
}
|
||||
StringSumHelper(unsigned char num) :
|
||||
String(num) {
|
||||
}
|
||||
StringSumHelper(int num) :
|
||||
String(num) {
|
||||
}
|
||||
StringSumHelper(unsigned int num) :
|
||||
String(num) {
|
||||
}
|
||||
StringSumHelper(long num) :
|
||||
String(num) {
|
||||
}
|
||||
StringSumHelper(unsigned long num) :
|
||||
String(num) {
|
||||
}
|
||||
StringSumHelper(float num) :
|
||||
String(num) {
|
||||
}
|
||||
StringSumHelper(double num) :
|
||||
String(num) {
|
||||
}
|
||||
};
|
||||
|
||||
extern const String emptyString;
|
||||
|
@ -1,20 +1,20 @@
|
||||
/*
|
||||
Copyright (c) 2014 Arduino. All right reserved.
|
||||
Copyright (c) 2014 Arduino. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
@ -28,8 +28,8 @@ using __cxxabiv1::__guard;
|
||||
extern void *umm_last_fail_alloc_addr;
|
||||
extern int umm_last_fail_alloc_size;
|
||||
|
||||
extern "C" void __cxa_pure_virtual(void) __attribute__((__noreturn__));
|
||||
extern "C" void __cxa_deleted_virtual(void) __attribute__((__noreturn__));
|
||||
extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__));
|
||||
extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__));
|
||||
|
||||
void __cxa_pure_virtual(void)
|
||||
{
|
||||
@ -41,8 +41,7 @@ void __cxa_deleted_virtual(void)
|
||||
panic();
|
||||
}
|
||||
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
uint8_t guard;
|
||||
uint8_t ps;
|
||||
} guard_t;
|
||||
@ -50,8 +49,7 @@ typedef struct
|
||||
extern "C" int __cxa_guard_acquire(__guard* pg)
|
||||
{
|
||||
uint8_t ps = xt_rsil(15);
|
||||
if (reinterpret_cast<guard_t*>(pg)->guard)
|
||||
{
|
||||
if (reinterpret_cast<guard_t*>(pg)->guard) {
|
||||
xt_wsr_ps(ps);
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,26 +1,26 @@
|
||||
/**
|
||||
base64.cpp
|
||||
|
||||
Created on: 09.12.2015
|
||||
|
||||
Copyright (c) 2015 Markus Sattler. All rights reserved.
|
||||
This file is part of the ESP8266 core for Arduino.
|
||||
|
||||
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
|
||||
|
||||
*/
|
||||
* base64.cpp
|
||||
*
|
||||
* Created on: 09.12.2015
|
||||
*
|
||||
* Copyright (c) 2015 Markus Sattler. All rights reserved.
|
||||
* This file is part of the ESP8266 core for Arduino.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
*/
|
||||
|
||||
#include "Arduino.h"
|
||||
extern "C" {
|
||||
@ -30,21 +30,19 @@ extern "C" {
|
||||
#include "base64.h"
|
||||
|
||||
/**
|
||||
convert input data to base64
|
||||
@param data uint8_t
|
||||
@param length size_t
|
||||
@return String
|
||||
*/
|
||||
String base64::encode(uint8_t * data, size_t length, bool doNewLines)
|
||||
{
|
||||
* convert input data to base64
|
||||
* @param data uint8_t *
|
||||
* @param length size_t
|
||||
* @return String
|
||||
*/
|
||||
String base64::encode(uint8_t * data, size_t length, bool doNewLines) {
|
||||
// base64 needs more size then the source data, use cencode.h macros
|
||||
size_t size = ((doNewLines ? base64_encode_expected_len(length)
|
||||
: base64_encode_expected_len_nonewlines(length)) + 1);
|
||||
: base64_encode_expected_len_nonewlines(length)) + 1);
|
||||
char * buffer = (char *) malloc(size);
|
||||
if (buffer)
|
||||
{
|
||||
if(buffer) {
|
||||
base64_encodestate _state;
|
||||
if (doNewLines)
|
||||
if(doNewLines)
|
||||
{
|
||||
base64_init_encodestate(&_state);
|
||||
}
|
||||
@ -63,12 +61,11 @@ String base64::encode(uint8_t * data, size_t length, bool doNewLines)
|
||||
}
|
||||
|
||||
/**
|
||||
convert input data to base64
|
||||
@param text String
|
||||
@return String
|
||||
*/
|
||||
String base64::encode(String text, bool doNewLines)
|
||||
{
|
||||
* convert input data to base64
|
||||
* @param text String
|
||||
* @return String
|
||||
*/
|
||||
String base64::encode(String text, bool doNewLines) {
|
||||
return base64::encode((uint8_t *) text.c_str(), text.length(), doNewLines);
|
||||
}
|
||||
|
||||
|
@ -1,39 +1,38 @@
|
||||
/**
|
||||
base64.h
|
||||
|
||||
Created on: 09.12.2015
|
||||
|
||||
Copyright (c) 2015 Markus Sattler. All rights reserved.
|
||||
This file is part of the ESP8266 core for Arduino.
|
||||
|
||||
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
|
||||
|
||||
*/
|
||||
* base64.h
|
||||
*
|
||||
* Created on: 09.12.2015
|
||||
*
|
||||
* Copyright (c) 2015 Markus Sattler. All rights reserved.
|
||||
* This file is part of the ESP8266 core for Arduino.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CORE_BASE64_H_
|
||||
#define CORE_BASE64_H_
|
||||
|
||||
class base64
|
||||
{
|
||||
public:
|
||||
// NOTE: The default behaviour of backend (lib64)
|
||||
// is to add a newline every 72 (encoded) characters output.
|
||||
// This may 'break' longer uris and json variables
|
||||
static String encode(uint8_t * data, size_t length, bool doNewLines = true);
|
||||
static String encode(String text, bool doNewLines = true);
|
||||
private:
|
||||
class base64 {
|
||||
public:
|
||||
// NOTE: The default behaviour of backend (lib64)
|
||||
// is to add a newline every 72 (encoded) characters output.
|
||||
// This may 'break' longer uris and json variables
|
||||
static String encode(uint8_t * data, size_t length, bool doNewLines = true);
|
||||
static String encode(String text, bool doNewLines = true);
|
||||
private:
|
||||
};
|
||||
|
||||
|
||||
|
@ -1,21 +1,21 @@
|
||||
/*
|
||||
binary.h - Definitions for binary constants
|
||||
Copyright (c) 2006 David A. Mellis. All right reserved.
|
||||
binary.h - Definitions for binary constants
|
||||
Copyright (c) 2006 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef Binary_h
|
||||
#define Binary_h
|
||||
|
@ -1,63 +1,56 @@
|
||||
/*
|
||||
cbuf.cpp - Circular buffer implementation
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
cbuf.cpp - Circular buffer implementation
|
||||
Copyright (c) 2014 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#include "cbuf.h"
|
||||
#include "c_types.h"
|
||||
|
||||
cbuf::cbuf(size_t size) :
|
||||
next(NULL), _size(size), _buf(new char[size]), _bufend(_buf + size), _begin(_buf), _end(_begin)
|
||||
{
|
||||
next(NULL), _size(size), _buf(new char[size]), _bufend(_buf + size), _begin(_buf), _end(_begin) {
|
||||
}
|
||||
|
||||
cbuf::~cbuf()
|
||||
{
|
||||
cbuf::~cbuf() {
|
||||
delete[] _buf;
|
||||
}
|
||||
|
||||
size_t cbuf::resizeAdd(size_t addSize)
|
||||
{
|
||||
size_t cbuf::resizeAdd(size_t addSize) {
|
||||
return resize(_size + addSize);
|
||||
}
|
||||
|
||||
size_t cbuf::resize(size_t newSize)
|
||||
{
|
||||
size_t cbuf::resize(size_t newSize) {
|
||||
|
||||
size_t bytes_available = available();
|
||||
|
||||
// not lose any data
|
||||
// if data can be lost use remove or flush before resize
|
||||
if ((newSize <= bytes_available) || (newSize == _size))
|
||||
{
|
||||
// not lose any data
|
||||
// if data can be lost use remove or flush before resize
|
||||
if((newSize <= bytes_available) || (newSize == _size)) {
|
||||
return _size;
|
||||
}
|
||||
|
||||
char *newbuf = new char[newSize];
|
||||
char *oldbuf = _buf;
|
||||
|
||||
if (!newbuf)
|
||||
{
|
||||
if(!newbuf) {
|
||||
return _size;
|
||||
}
|
||||
|
||||
if (_buf)
|
||||
{
|
||||
if(_buf) {
|
||||
read(newbuf, bytes_available);
|
||||
memset((newbuf + bytes_available), 0x00, (newSize - bytes_available));
|
||||
}
|
||||
@ -73,47 +66,37 @@ size_t cbuf::resize(size_t newSize)
|
||||
return _size;
|
||||
}
|
||||
|
||||
size_t ICACHE_RAM_ATTR cbuf::available() const
|
||||
{
|
||||
if (_end >= _begin)
|
||||
{
|
||||
size_t ICACHE_RAM_ATTR cbuf::available() const {
|
||||
if(_end >= _begin) {
|
||||
return _end - _begin;
|
||||
}
|
||||
return _size - (_begin - _end);
|
||||
}
|
||||
|
||||
size_t cbuf::size()
|
||||
{
|
||||
size_t cbuf::size() {
|
||||
return _size;
|
||||
}
|
||||
|
||||
size_t cbuf::room() const
|
||||
{
|
||||
if (_end >= _begin)
|
||||
{
|
||||
size_t cbuf::room() const {
|
||||
if(_end >= _begin) {
|
||||
return _size - (_end - _begin) - 1;
|
||||
}
|
||||
return _begin - _end - 1;
|
||||
}
|
||||
|
||||
int cbuf::peek()
|
||||
{
|
||||
if (empty())
|
||||
{
|
||||
int cbuf::peek() {
|
||||
if(empty())
|
||||
return -1;
|
||||
}
|
||||
|
||||
return static_cast<int>(*_begin);
|
||||
}
|
||||
|
||||
size_t cbuf::peek(char *dst, size_t size)
|
||||
{
|
||||
size_t cbuf::peek(char *dst, size_t size) {
|
||||
size_t bytes_available = available();
|
||||
size_t size_to_read = (size < bytes_available) ? size : bytes_available;
|
||||
size_t size_read = size_to_read;
|
||||
char * begin = _begin;
|
||||
if (_end < _begin && size_to_read > (size_t)(_bufend - _begin))
|
||||
{
|
||||
if(_end < _begin && size_to_read > (size_t) (_bufend - _begin)) {
|
||||
size_t top_size = _bufend - _begin;
|
||||
memcpy(dst, _begin, top_size);
|
||||
begin = _buf;
|
||||
@ -124,25 +107,20 @@ size_t cbuf::peek(char *dst, size_t size)
|
||||
return size_read;
|
||||
}
|
||||
|
||||
int ICACHE_RAM_ATTR cbuf::read()
|
||||
{
|
||||
if (empty())
|
||||
{
|
||||
int ICACHE_RAM_ATTR cbuf::read() {
|
||||
if(empty())
|
||||
return -1;
|
||||
}
|
||||
|
||||
char result = *_begin;
|
||||
_begin = wrap_if_bufend(_begin + 1);
|
||||
return static_cast<int>(result);
|
||||
}
|
||||
|
||||
size_t cbuf::read(char* dst, size_t size)
|
||||
{
|
||||
size_t cbuf::read(char* dst, size_t size) {
|
||||
size_t bytes_available = available();
|
||||
size_t size_to_read = (size < bytes_available) ? size : bytes_available;
|
||||
size_t size_read = size_to_read;
|
||||
if (_end < _begin && size_to_read > (size_t)(_bufend - _begin))
|
||||
{
|
||||
if(_end < _begin && size_to_read > (size_t) (_bufend - _begin)) {
|
||||
size_t top_size = _bufend - _begin;
|
||||
memcpy(dst, _begin, top_size);
|
||||
_begin = _buf;
|
||||
@ -154,25 +132,20 @@ size_t cbuf::read(char* dst, size_t size)
|
||||
return size_read;
|
||||
}
|
||||
|
||||
size_t ICACHE_RAM_ATTR cbuf::write(char c)
|
||||
{
|
||||
if (full())
|
||||
{
|
||||
size_t ICACHE_RAM_ATTR cbuf::write(char c) {
|
||||
if(full())
|
||||
return 0;
|
||||
}
|
||||
|
||||
*_end = c;
|
||||
_end = wrap_if_bufend(_end + 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t cbuf::write(const char* src, size_t size)
|
||||
{
|
||||
size_t cbuf::write(const char* src, size_t size) {
|
||||
size_t bytes_available = room();
|
||||
size_t size_to_write = (size < bytes_available) ? size : bytes_available;
|
||||
size_t size_written = size_to_write;
|
||||
if (_end >= _begin && size_to_write > (size_t)(_bufend - _end))
|
||||
{
|
||||
if(_end >= _begin && size_to_write > (size_t) (_bufend - _end)) {
|
||||
size_t top_size = _bufend - _end;
|
||||
memcpy(_end, src, top_size);
|
||||
_end = _buf;
|
||||
@ -184,23 +157,19 @@ size_t cbuf::write(const char* src, size_t size)
|
||||
return size_written;
|
||||
}
|
||||
|
||||
void cbuf::flush()
|
||||
{
|
||||
void cbuf::flush() {
|
||||
_begin = _buf;
|
||||
_end = _buf;
|
||||
}
|
||||
|
||||
size_t cbuf::remove(size_t size)
|
||||
{
|
||||
size_t cbuf::remove(size_t size) {
|
||||
size_t bytes_available = available();
|
||||
if (size >= bytes_available)
|
||||
{
|
||||
if(size >= bytes_available) {
|
||||
flush();
|
||||
return 0;
|
||||
}
|
||||
size_t size_to_remove = (size < bytes_available) ? size : bytes_available;
|
||||
if (_end < _begin && size_to_remove > (size_t)(_bufend - _begin))
|
||||
{
|
||||
if(_end < _begin && size_to_remove > (size_t) (_bufend - _begin)) {
|
||||
size_t top_size = _bufend - _begin;
|
||||
_begin = _buf;
|
||||
size_to_remove -= top_size;
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
cbuf.h - Circular buffer implementation
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
cbuf.h - Circular buffer implementation
|
||||
Copyright (c) 2014 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef __cbuf_h
|
||||
#define __cbuf_h
|
||||
@ -25,54 +25,50 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
class cbuf
|
||||
{
|
||||
public:
|
||||
cbuf(size_t size);
|
||||
~cbuf();
|
||||
class cbuf {
|
||||
public:
|
||||
cbuf(size_t size);
|
||||
~cbuf();
|
||||
|
||||
size_t resizeAdd(size_t addSize);
|
||||
size_t resize(size_t newSize);
|
||||
size_t available() const;
|
||||
size_t size();
|
||||
size_t resizeAdd(size_t addSize);
|
||||
size_t resize(size_t newSize);
|
||||
size_t available() const;
|
||||
size_t size();
|
||||
|
||||
size_t room() const;
|
||||
size_t room() const;
|
||||
|
||||
inline bool empty() const
|
||||
{
|
||||
return _begin == _end;
|
||||
}
|
||||
inline bool empty() const {
|
||||
return _begin == _end;
|
||||
}
|
||||
|
||||
inline bool full() const
|
||||
{
|
||||
return wrap_if_bufend(_end + 1) == _begin;
|
||||
}
|
||||
inline bool full() const {
|
||||
return wrap_if_bufend(_end + 1) == _begin;
|
||||
}
|
||||
|
||||
int peek();
|
||||
size_t peek(char *dst, size_t size);
|
||||
int peek();
|
||||
size_t peek(char *dst, size_t size);
|
||||
|
||||
int read();
|
||||
size_t read(char* dst, size_t size);
|
||||
int read();
|
||||
size_t read(char* dst, size_t size);
|
||||
|
||||
size_t write(char c);
|
||||
size_t write(const char* src, size_t size);
|
||||
size_t write(char c);
|
||||
size_t write(const char* src, size_t size);
|
||||
|
||||
void flush();
|
||||
size_t remove(size_t size);
|
||||
void flush();
|
||||
size_t remove(size_t size);
|
||||
|
||||
cbuf *next;
|
||||
cbuf *next;
|
||||
|
||||
private:
|
||||
inline char* wrap_if_bufend(char* ptr) const
|
||||
{
|
||||
return (ptr == _bufend) ? _buf : ptr;
|
||||
}
|
||||
private:
|
||||
inline char* wrap_if_bufend(char* ptr) const {
|
||||
return (ptr == _bufend) ? _buf : ptr;
|
||||
}
|
||||
|
||||
size_t _size;
|
||||
char* _buf;
|
||||
const char* _bufend;
|
||||
char* _begin;
|
||||
char* _end;
|
||||
size_t _size;
|
||||
char* _buf;
|
||||
const char* _bufend;
|
||||
char* _begin;
|
||||
char* _end;
|
||||
|
||||
};
|
||||
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
cont.h - continuations support for Xtensa call0 ABI
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
cont.h - continuations support for Xtensa call0 ABI
|
||||
Copyright (c) 2014 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef CONT_H_
|
||||
#define CONT_H_
|
||||
@ -31,23 +31,22 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct cont_
|
||||
{
|
||||
void (*pc_ret)(void);
|
||||
unsigned* sp_ret;
|
||||
typedef struct cont_ {
|
||||
void (*pc_ret)(void);
|
||||
unsigned* sp_ret;
|
||||
|
||||
void (*pc_yield)(void);
|
||||
unsigned* sp_yield;
|
||||
void (*pc_yield)(void);
|
||||
unsigned* sp_yield;
|
||||
|
||||
unsigned* stack_end;
|
||||
unsigned unused1;
|
||||
unsigned unused2;
|
||||
unsigned stack_guard1;
|
||||
unsigned* stack_end;
|
||||
unsigned unused1;
|
||||
unsigned unused2;
|
||||
unsigned stack_guard1;
|
||||
|
||||
unsigned stack[CONT_STACKSIZE / 4];
|
||||
unsigned stack[CONT_STACKSIZE / 4];
|
||||
|
||||
unsigned stack_guard2;
|
||||
unsigned* struct_start;
|
||||
unsigned stack_guard2;
|
||||
unsigned* struct_start;
|
||||
} cont_t;
|
||||
|
||||
extern cont_t* g_pcont;
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
cont_util.s - continuations support for Xtensa call0 ABI
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
cont_util.s - continuations support for Xtensa call0 ABI
|
||||
Copyright (c) 2014 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#include "cont.h"
|
||||
#include <stddef.h>
|
||||
@ -27,64 +27,57 @@ extern "C" {
|
||||
|
||||
#define CONT_STACKGUARD 0xfeefeffe
|
||||
|
||||
void cont_init(cont_t* cont)
|
||||
void cont_init(cont_t* cont) {
|
||||
memset(cont, 0, sizeof(cont_t));
|
||||
|
||||
cont->stack_guard1 = CONT_STACKGUARD;
|
||||
cont->stack_guard2 = CONT_STACKGUARD;
|
||||
cont->stack_end = cont->stack + (sizeof(cont->stack) / 4);
|
||||
cont->struct_start = (unsigned*) cont;
|
||||
|
||||
// fill stack with magic values to check high water mark
|
||||
for(int pos = 0; pos < (int)(sizeof(cont->stack) / 4); pos++)
|
||||
{
|
||||
memset(cont, 0, sizeof(cont_t));
|
||||
cont->stack[pos] = CONT_STACKGUARD;
|
||||
}
|
||||
}
|
||||
|
||||
cont->stack_guard1 = CONT_STACKGUARD;
|
||||
cont->stack_guard2 = CONT_STACKGUARD;
|
||||
cont->stack_end = cont->stack + (sizeof(cont->stack) / 4);
|
||||
cont->struct_start = (unsigned*) cont;
|
||||
int ICACHE_RAM_ATTR cont_check(cont_t* cont) {
|
||||
if(cont->stack_guard1 != CONT_STACKGUARD || cont->stack_guard2 != CONT_STACKGUARD) return 1;
|
||||
|
||||
// fill stack with magic values to check high water mark
|
||||
for (int pos = 0; pos < (int)(sizeof(cont->stack) / 4); pos++)
|
||||
{
|
||||
cont->stack[pos] = CONT_STACKGUARD;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// No need for this to be in IRAM, not expected to be IRQ called
|
||||
int cont_get_free_stack(cont_t* cont) {
|
||||
uint32_t *head = cont->stack;
|
||||
int freeWords = 0;
|
||||
|
||||
while(*head == CONT_STACKGUARD)
|
||||
{
|
||||
head++;
|
||||
freeWords++;
|
||||
}
|
||||
|
||||
int ICACHE_RAM_ATTR cont_check(cont_t* cont)
|
||||
return freeWords * 4;
|
||||
}
|
||||
|
||||
bool ICACHE_RAM_ATTR cont_can_yield(cont_t* cont) {
|
||||
return !ETS_INTR_WITHINISR() &&
|
||||
cont->pc_ret != 0 && cont->pc_yield == 0;
|
||||
}
|
||||
|
||||
// No need for this to be in IRAM, not expected to be IRQ called
|
||||
void cont_repaint_stack(cont_t *cont)
|
||||
{
|
||||
register uint32_t *sp asm("a1");
|
||||
// Ensure 64 bytes adjacent to the current SP don't get touched to endure
|
||||
// we don't accidentally trounce over locals or IRQ temps.
|
||||
// Fill stack with magic values
|
||||
for ( uint32_t *pos = sp - 16; pos >= &cont->stack[0]; pos-- )
|
||||
{
|
||||
if (cont->stack_guard1 != CONT_STACKGUARD || cont->stack_guard2 != CONT_STACKGUARD)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// No need for this to be in IRAM, not expected to be IRQ called
|
||||
int cont_get_free_stack(cont_t* cont)
|
||||
{
|
||||
uint32_t *head = cont->stack;
|
||||
int freeWords = 0;
|
||||
|
||||
while (*head == CONT_STACKGUARD)
|
||||
{
|
||||
head++;
|
||||
freeWords++;
|
||||
}
|
||||
|
||||
return freeWords * 4;
|
||||
}
|
||||
|
||||
bool ICACHE_RAM_ATTR cont_can_yield(cont_t* cont)
|
||||
{
|
||||
return !ETS_INTR_WITHINISR() &&
|
||||
cont->pc_ret != 0 && cont->pc_yield == 0;
|
||||
}
|
||||
|
||||
// No need for this to be in IRAM, not expected to be IRQ called
|
||||
void cont_repaint_stack(cont_t *cont)
|
||||
{
|
||||
register uint32_t *sp asm("a1");
|
||||
// Ensure 64 bytes adjacent to the current SP don't get touched to endure
|
||||
// we don't accidentally trounce over locals or IRQ temps.
|
||||
// Fill stack with magic values
|
||||
for (uint32_t *pos = sp - 16; pos >= &cont->stack[0]; pos--)
|
||||
{
|
||||
*pos = CONT_STACKGUARD;
|
||||
}
|
||||
*pos = CONT_STACKGUARD;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -1,23 +1,23 @@
|
||||
/*
|
||||
This is the original app_entry() not providing extra 4K heap, but allowing
|
||||
the use of WPS.
|
||||
|
||||
see comments in core_esp8266_main.cpp's app_entry()
|
||||
|
||||
*/
|
||||
* This is the original app_entry() not providing extra 4K heap, but allowing
|
||||
* the use of WPS.
|
||||
*
|
||||
* see comments in core_esp8266_main.cpp's app_entry()
|
||||
*
|
||||
*/
|
||||
|
||||
#include <c_types.h>
|
||||
#include "cont.h"
|
||||
#include "coredecls.h"
|
||||
|
||||
void disable_extra4k_at_link_time(void)
|
||||
void disable_extra4k_at_link_time (void)
|
||||
{
|
||||
/*
|
||||
does nothing
|
||||
allows overriding the core_esp8266_main.cpp's app_entry()
|
||||
by this one below, at link time
|
||||
|
||||
*/
|
||||
* does nothing
|
||||
* allows overriding the core_esp8266_main.cpp's app_entry()
|
||||
* by this one below, at link time
|
||||
*
|
||||
*/
|
||||
}
|
||||
|
||||
/* the following code is linked only if a call to the above function is made somewhere */
|
||||
@ -25,7 +25,7 @@ void disable_extra4k_at_link_time(void)
|
||||
extern "C" void call_user_start();
|
||||
|
||||
/* this is the default NONOS-SDK user's heap location */
|
||||
static cont_t g_cont __attribute__((aligned(16)));
|
||||
static cont_t g_cont __attribute__ ((aligned (16)));
|
||||
|
||||
extern "C" void ICACHE_RAM_ATTR app_entry_redefinable(void)
|
||||
{
|
||||
|
@ -1,23 +1,23 @@
|
||||
/*
|
||||
core_esp8266_eboot_command.c - interface to the eboot bootloader
|
||||
core_esp8266_eboot_command.c - interface to the eboot bootloader
|
||||
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2015 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
@ -25,74 +25,67 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
static uint32_t crc_update(uint32_t crc, const uint8_t *data, size_t length)
|
||||
{
|
||||
uint32_t i;
|
||||
bool bit;
|
||||
uint8_t c;
|
||||
static uint32_t crc_update(uint32_t crc, const uint8_t *data, size_t length)
|
||||
{
|
||||
uint32_t i;
|
||||
bool bit;
|
||||
uint8_t c;
|
||||
|
||||
while (length--)
|
||||
{
|
||||
c = *data++;
|
||||
for (i = 0x80; i > 0; i >>= 1)
|
||||
{
|
||||
bit = crc & 0x80000000;
|
||||
if (c & i)
|
||||
{
|
||||
bit = !bit;
|
||||
}
|
||||
crc <<= 1;
|
||||
if (bit)
|
||||
{
|
||||
crc ^= 0x04c11db7;
|
||||
}
|
||||
while (length--) {
|
||||
c = *data++;
|
||||
for (i = 0x80; i > 0; i >>= 1) {
|
||||
bit = crc & 0x80000000;
|
||||
if (c & i) {
|
||||
bit = !bit;
|
||||
}
|
||||
crc <<= 1;
|
||||
if (bit) {
|
||||
crc ^= 0x04c11db7;
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
static uint32_t eboot_command_calculate_crc32(const struct eboot_command* cmd)
|
||||
{
|
||||
return crc_update(0xffffffff, (const uint8_t*) cmd,
|
||||
offsetof(struct eboot_command, crc32));
|
||||
}
|
||||
|
||||
int eboot_command_read(struct eboot_command* cmd)
|
||||
{
|
||||
const uint32_t dw_count = sizeof(struct eboot_command) / sizeof(uint32_t);
|
||||
uint32_t* dst = (uint32_t *) cmd;
|
||||
for (uint32_t i = 0; i < dw_count; ++i) {
|
||||
dst[i] = RTC_MEM[i];
|
||||
}
|
||||
|
||||
static uint32_t eboot_command_calculate_crc32(const struct eboot_command* cmd)
|
||||
{
|
||||
return crc_update(0xffffffff, (const uint8_t*) cmd,
|
||||
offsetof(struct eboot_command, crc32));
|
||||
uint32_t crc32 = eboot_command_calculate_crc32(cmd);
|
||||
if ((cmd->magic & EBOOT_MAGIC_MASK) != EBOOT_MAGIC ||
|
||||
cmd->crc32 != crc32) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int eboot_command_read(struct eboot_command* cmd)
|
||||
{
|
||||
const uint32_t dw_count = sizeof(struct eboot_command) / sizeof(uint32_t);
|
||||
uint32_t* dst = (uint32_t *) cmd;
|
||||
for (uint32_t i = 0; i < dw_count; ++i)
|
||||
{
|
||||
dst[i] = RTC_MEM[i];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t crc32 = eboot_command_calculate_crc32(cmd);
|
||||
if ((cmd->magic & EBOOT_MAGIC_MASK) != EBOOT_MAGIC ||
|
||||
cmd->crc32 != crc32)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
void eboot_command_write(struct eboot_command* cmd)
|
||||
{
|
||||
cmd->magic = EBOOT_MAGIC;
|
||||
cmd->crc32 = eboot_command_calculate_crc32(cmd);
|
||||
|
||||
return 0;
|
||||
const uint32_t dw_count = sizeof(struct eboot_command) / sizeof(uint32_t);
|
||||
const uint32_t* src = (const uint32_t *) cmd;
|
||||
for (uint32_t i = 0; i < dw_count; ++i) {
|
||||
RTC_MEM[i] = src[i];
|
||||
}
|
||||
}
|
||||
|
||||
void eboot_command_write(struct eboot_command* cmd)
|
||||
{
|
||||
cmd->magic = EBOOT_MAGIC;
|
||||
cmd->crc32 = eboot_command_calculate_crc32(cmd);
|
||||
|
||||
const uint32_t dw_count = sizeof(struct eboot_command) / sizeof(uint32_t);
|
||||
const uint32_t* src = (const uint32_t *) cmd;
|
||||
for (uint32_t i = 0; i < dw_count; ++i)
|
||||
{
|
||||
RTC_MEM[i] = src[i];
|
||||
}
|
||||
}
|
||||
|
||||
void eboot_command_clear()
|
||||
{
|
||||
RTC_MEM[offsetof(struct eboot_command, magic) / sizeof(uint32_t)] = 0;
|
||||
RTC_MEM[offsetof(struct eboot_command, crc32) / sizeof(uint32_t)] = 0;
|
||||
}
|
||||
void eboot_command_clear()
|
||||
{
|
||||
RTC_MEM[offsetof(struct eboot_command, magic) / sizeof(uint32_t)] = 0;
|
||||
RTC_MEM[offsetof(struct eboot_command, crc32) / sizeof(uint32_t)] = 0;
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -1,24 +1,24 @@
|
||||
/*
|
||||
core_esp8266_features.h - list of features integrated in to ESP8266 core
|
||||
core_esp8266_features.h - list of features integrated in to ESP8266 core
|
||||
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2014 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
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
|
||||
|
||||
*/
|
||||
*/
|
||||
|
||||
|
||||
#ifndef CORE_ESP8266_FEATURES_H
|
||||
|
@ -1,23 +1,23 @@
|
||||
/*
|
||||
core_esp8266_flash_utils.c - flash and binary image helpers
|
||||
core_esp8266_flash_utils.c - flash and binary image helpers
|
||||
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2015 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
@ -27,47 +27,40 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
int SPIEraseAreaEx(const uint32_t start, const uint32_t size)
|
||||
{
|
||||
if ((start & (FLASH_SECTOR_SIZE - 1)) != 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
const uint32_t sectors_per_block = FLASH_BLOCK_SIZE / FLASH_SECTOR_SIZE;
|
||||
uint32_t current_sector = start / FLASH_SECTOR_SIZE;
|
||||
uint32_t sector_count = (size + FLASH_SECTOR_SIZE - 1) / FLASH_SECTOR_SIZE;
|
||||
const uint32_t end = current_sector + sector_count;
|
||||
|
||||
for (; current_sector < end && (current_sector & (sectors_per_block - 1));
|
||||
++current_sector, --sector_count)
|
||||
{
|
||||
if (SPIEraseSector(current_sector))
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
for (; current_sector + sectors_per_block <= end;
|
||||
current_sector += sectors_per_block,
|
||||
sector_count -= sectors_per_block)
|
||||
{
|
||||
if (SPIEraseBlock(current_sector / sectors_per_block))
|
||||
{
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
for (; current_sector < end;
|
||||
++current_sector, --sector_count)
|
||||
{
|
||||
if (SPIEraseSector(current_sector))
|
||||
{
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
int SPIEraseAreaEx(const uint32_t start, const uint32_t size)
|
||||
{
|
||||
if ((start & (FLASH_SECTOR_SIZE - 1)) != 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
const uint32_t sectors_per_block = FLASH_BLOCK_SIZE / FLASH_SECTOR_SIZE;
|
||||
uint32_t current_sector = start / FLASH_SECTOR_SIZE;
|
||||
uint32_t sector_count = (size + FLASH_SECTOR_SIZE - 1) / FLASH_SECTOR_SIZE;
|
||||
const uint32_t end = current_sector + sector_count;
|
||||
|
||||
for (; current_sector < end && (current_sector & (sectors_per_block-1));
|
||||
++current_sector, --sector_count) {
|
||||
if (SPIEraseSector(current_sector)) {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
for (;current_sector + sectors_per_block <= end;
|
||||
current_sector += sectors_per_block,
|
||||
sector_count -= sectors_per_block) {
|
||||
if (SPIEraseBlock(current_sector / sectors_per_block)) {
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
for (; current_sector < end;
|
||||
++current_sector, --sector_count) {
|
||||
if (SPIEraseSector(current_sector)) {
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
};
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,24 +1,24 @@
|
||||
/*
|
||||
main.cpp - platform initialization and context switching
|
||||
emulation
|
||||
main.cpp - platform initialization and context switching
|
||||
emulation
|
||||
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2014 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
*/
|
||||
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 may be used to change user task stack size:
|
||||
//#define CONT_STACKSIZE 4096
|
||||
@ -48,10 +48,10 @@ extern void (*__init_array_end)(void);
|
||||
/* Not static, used in Esp.cpp */
|
||||
struct rst_info resetInfo;
|
||||
|
||||
/* Not static, used in core_esp8266_postmortem.c and other places.
|
||||
Placed into noinit section because we assign value to this variable
|
||||
before .bss is zero-filled, and need to preserve the value.
|
||||
*/
|
||||
/* Not static, used in core_esp8266_postmortem.c and other places.
|
||||
* Placed into noinit section because we assign value to this variable
|
||||
* before .bss is zero-filled, and need to preserve the value.
|
||||
*/
|
||||
cont_t* g_pcont __attribute__((section(".noinit")));
|
||||
|
||||
/* Event queue used by the main (arduino) task */
|
||||
@ -62,23 +62,21 @@ static uint32_t s_micros_at_task_start;
|
||||
|
||||
|
||||
extern "C" {
|
||||
extern const uint32_t __attribute__((section(".ver_number"))) core_version = ARDUINO_ESP8266_GIT_VER;
|
||||
const char* core_release =
|
||||
extern const uint32_t __attribute__((section(".ver_number"))) core_version = ARDUINO_ESP8266_GIT_VER;
|
||||
const char* core_release =
|
||||
#ifdef ARDUINO_ESP8266_RELEASE
|
||||
ARDUINO_ESP8266_RELEASE;
|
||||
ARDUINO_ESP8266_RELEASE;
|
||||
#else
|
||||
NULL;
|
||||
NULL;
|
||||
#endif
|
||||
} // extern "C"
|
||||
|
||||
void initVariant() __attribute__((weak));
|
||||
void initVariant()
|
||||
{
|
||||
void initVariant() {
|
||||
}
|
||||
|
||||
void preloop_update_frequency() __attribute__((weak));
|
||||
void preloop_update_frequency()
|
||||
{
|
||||
void preloop_update_frequency() {
|
||||
#if defined(F_CPU) && (F_CPU == 160000000L)
|
||||
REG_SET_BIT(0x3ff00014, BIT(0));
|
||||
ets_update_cpu_frequency(160);
|
||||
@ -86,49 +84,40 @@ void preloop_update_frequency()
|
||||
}
|
||||
|
||||
|
||||
extern "C" void esp_yield()
|
||||
{
|
||||
if (cont_can_yield(g_pcont))
|
||||
{
|
||||
extern "C" void esp_yield() {
|
||||
if (cont_can_yield(g_pcont)) {
|
||||
cont_yield(g_pcont);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void esp_schedule()
|
||||
{
|
||||
extern "C" void esp_schedule() {
|
||||
ets_post(LOOP_TASK_PRIORITY, 0, 0);
|
||||
}
|
||||
|
||||
extern "C" void __yield()
|
||||
{
|
||||
if (cont_can_yield(g_pcont))
|
||||
{
|
||||
extern "C" void __yield() {
|
||||
if (cont_can_yield(g_pcont)) {
|
||||
esp_schedule();
|
||||
esp_yield();
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
panic();
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void yield(void) __attribute__((weak, alias("__yield")));
|
||||
extern "C" void yield(void) __attribute__ ((weak, alias("__yield")));
|
||||
|
||||
extern "C" void optimistic_yield(uint32_t interval_us)
|
||||
{
|
||||
extern "C" void optimistic_yield(uint32_t interval_us) {
|
||||
if (cont_can_yield(g_pcont) &&
|
||||
(system_get_time() - s_micros_at_task_start) > interval_us)
|
||||
(system_get_time() - s_micros_at_task_start) > interval_us)
|
||||
{
|
||||
yield();
|
||||
}
|
||||
}
|
||||
|
||||
static void loop_wrapper()
|
||||
{
|
||||
static void loop_wrapper() {
|
||||
static bool setup_done = false;
|
||||
preloop_update_frequency();
|
||||
if (!setup_done)
|
||||
{
|
||||
if(!setup_done) {
|
||||
setup();
|
||||
setup_done = true;
|
||||
}
|
||||
@ -137,72 +126,56 @@ static void loop_wrapper()
|
||||
esp_schedule();
|
||||
}
|
||||
|
||||
static void loop_task(os_event_t *events)
|
||||
{
|
||||
static void loop_task(os_event_t *events) {
|
||||
(void) events;
|
||||
s_micros_at_task_start = system_get_time();
|
||||
cont_run(g_pcont, &loop_wrapper);
|
||||
if (cont_check(g_pcont) != 0)
|
||||
{
|
||||
if (cont_check(g_pcont) != 0) {
|
||||
panic();
|
||||
}
|
||||
}
|
||||
extern "C" {
|
||||
|
||||
struct object
|
||||
{
|
||||
long placeholder[ 10 ];
|
||||
};
|
||||
void __register_frame_info(const void *begin, struct object *ob);
|
||||
extern char __eh_frame[];
|
||||
struct object { long placeholder[ 10 ]; };
|
||||
void __register_frame_info (const void *begin, struct object *ob);
|
||||
extern char __eh_frame[];
|
||||
}
|
||||
|
||||
static void do_global_ctors(void)
|
||||
{
|
||||
static void do_global_ctors(void) {
|
||||
static struct object ob;
|
||||
__register_frame_info(__eh_frame, &ob);
|
||||
__register_frame_info( __eh_frame, &ob );
|
||||
|
||||
void (**p)(void) = &__init_array_end;
|
||||
while (p != &__init_array_start)
|
||||
{
|
||||
(*--p)();
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
extern void __unhandled_exception(const char *str);
|
||||
extern void __unhandled_exception(const char *str);
|
||||
|
||||
static void __unhandled_exception_cpp()
|
||||
{
|
||||
static void __unhandled_exception_cpp()
|
||||
{
|
||||
#ifndef __EXCEPTIONS
|
||||
abort();
|
||||
abort();
|
||||
#else
|
||||
static bool terminating;
|
||||
if (terminating)
|
||||
{
|
||||
abort();
|
||||
}
|
||||
terminating = true;
|
||||
/* Use a trick from vterminate.cc to get any std::exception what() */
|
||||
try
|
||||
{
|
||||
__throw_exception_again;
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
__unhandled_exception(e.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
__unhandled_exception("");
|
||||
}
|
||||
#endif
|
||||
static bool terminating;
|
||||
if (terminating)
|
||||
abort();
|
||||
terminating = true;
|
||||
/* Use a trick from vterminate.cc to get any std::exception what() */
|
||||
try {
|
||||
__throw_exception_again;
|
||||
} catch (const std::exception& e) {
|
||||
__unhandled_exception( e.what() );
|
||||
} catch (...) {
|
||||
__unhandled_exception( "" );
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void init_done()
|
||||
{
|
||||
void init_done() {
|
||||
system_set_os_print(1);
|
||||
gdb_init();
|
||||
std::set_terminate(__unhandled_exception_cpp);
|
||||
@ -210,56 +183,56 @@ void init_done()
|
||||
esp_schedule();
|
||||
}
|
||||
|
||||
/* This is the entry point of the application.
|
||||
It gets called on the default stack, which grows down from the top
|
||||
of DRAM area.
|
||||
.bss has not been zeroed out yet, but .data and .rodata are in place.
|
||||
Cache is not enabled, so only ROM and IRAM functions can be called.
|
||||
Peripherals (except for SPI0 and UART0) are not initialized.
|
||||
This function does not return.
|
||||
*/
|
||||
/* This is the entry point of the application.
|
||||
* It gets called on the default stack, which grows down from the top
|
||||
* of DRAM area.
|
||||
* .bss has not been zeroed out yet, but .data and .rodata are in place.
|
||||
* Cache is not enabled, so only ROM and IRAM functions can be called.
|
||||
* Peripherals (except for SPI0 and UART0) are not initialized.
|
||||
* This function does not return.
|
||||
*/
|
||||
/*
|
||||
A bit of explanation for this entry point:
|
||||
A bit of explanation for this entry point:
|
||||
|
||||
SYS is the SDK task/context used by the upperlying system to run its
|
||||
administrative tasks (at least WLAN and lwip's receive callbacks and
|
||||
Ticker). NONOS-SDK is designed to run user's non-threaded code in
|
||||
another specific task/context with its own stack in BSS.
|
||||
SYS is the SDK task/context used by the upperlying system to run its
|
||||
administrative tasks (at least WLAN and lwip's receive callbacks and
|
||||
Ticker). NONOS-SDK is designed to run user's non-threaded code in
|
||||
another specific task/context with its own stack in BSS.
|
||||
|
||||
Some clever fellows found that the SYS stack was a large and quite unused
|
||||
piece of ram that we could use for the user's stack instead of using user's
|
||||
main memory, thus saving around 4KB on ram/heap.
|
||||
Some clever fellows found that the SYS stack was a large and quite unused
|
||||
piece of ram that we could use for the user's stack instead of using user's
|
||||
main memory, thus saving around 4KB on ram/heap.
|
||||
|
||||
A problem arose later, which is that this stack can heavily be used by
|
||||
the SDK for some features. One of these features is WPS. We still don't
|
||||
know if other features are using this, or if this memory is going to be
|
||||
used in future SDK releases.
|
||||
A problem arose later, which is that this stack can heavily be used by
|
||||
the SDK for some features. One of these features is WPS. We still don't
|
||||
know if other features are using this, or if this memory is going to be
|
||||
used in future SDK releases.
|
||||
|
||||
WPS beeing flawed by its poor security, or not beeing used by lots of
|
||||
users, it has been decided that we are still going to use that memory for
|
||||
user's stack and disable the use of WPS.
|
||||
WPS beeing flawed by its poor security, or not beeing used by lots of
|
||||
users, it has been decided that we are still going to use that memory for
|
||||
user's stack and disable the use of WPS.
|
||||
|
||||
app_entry() jumps to app_entry_custom() defined as "weakref" calling
|
||||
itself a weak customizable function, allowing to use another one when
|
||||
this is required (see core_esp8266_app_entry_noextra4k.cpp, used by WPS).
|
||||
app_entry() jumps to app_entry_custom() defined as "weakref" calling
|
||||
itself a weak customizable function, allowing to use another one when
|
||||
this is required (see core_esp8266_app_entry_noextra4k.cpp, used by WPS).
|
||||
|
||||
(note: setting app_entry() itself as "weak" is not sufficient and always
|
||||
(note: setting app_entry() itself as "weak" is not sufficient and always
|
||||
ends up with the other "noextra4k" one linked, maybe because it has a
|
||||
default ENTRY(app_entry) value in linker scripts).
|
||||
|
||||
References:
|
||||
https://github.com/esp8266/Arduino/pull/4553
|
||||
https://github.com/esp8266/Arduino/pull/4622
|
||||
https://github.com/esp8266/Arduino/issues/4779
|
||||
https://github.com/esp8266/Arduino/pull/4889
|
||||
References:
|
||||
https://github.com/esp8266/Arduino/pull/4553
|
||||
https://github.com/esp8266/Arduino/pull/4622
|
||||
https://github.com/esp8266/Arduino/issues/4779
|
||||
https://github.com/esp8266/Arduino/pull/4889
|
||||
|
||||
*/
|
||||
|
||||
extern "C" void ICACHE_RAM_ATTR app_entry_redefinable(void) __attribute__((weak));
|
||||
extern "C" void ICACHE_RAM_ATTR app_entry_redefinable(void)
|
||||
{
|
||||
/* Allocate continuation context on this SYS stack,
|
||||
and save pointer to it. */
|
||||
/* Allocate continuation context on this SYS stack,
|
||||
and save pointer to it. */
|
||||
cont_t s_cont __attribute__((aligned(16)));
|
||||
g_pcont = &s_cont;
|
||||
|
||||
@ -267,21 +240,20 @@ extern "C" void ICACHE_RAM_ATTR app_entry_redefinable(void)
|
||||
call_user_start();
|
||||
}
|
||||
|
||||
static void ICACHE_RAM_ATTR app_entry_custom(void) __attribute__((weakref("app_entry_redefinable")));
|
||||
static void ICACHE_RAM_ATTR app_entry_custom (void) __attribute__((weakref("app_entry_redefinable")));
|
||||
|
||||
extern "C" void ICACHE_RAM_ATTR app_entry(void)
|
||||
extern "C" void ICACHE_RAM_ATTR app_entry (void)
|
||||
{
|
||||
return app_entry_custom();
|
||||
}
|
||||
|
||||
extern "C" void preinit(void) __attribute__((weak));
|
||||
extern "C" void preinit(void)
|
||||
extern "C" void preinit (void) __attribute__((weak));
|
||||
extern "C" void preinit (void)
|
||||
{
|
||||
/* do nothing by default */
|
||||
}
|
||||
|
||||
extern "C" void user_init(void)
|
||||
{
|
||||
extern "C" void user_init(void) {
|
||||
struct rst_info *rtc_info_ptr = system_get_rst_info();
|
||||
memcpy((void *) &resetInfo, (void *) rtc_info_ptr, sizeof(resetInfo));
|
||||
|
||||
@ -296,8 +268,8 @@ extern "C" void user_init(void)
|
||||
preinit(); // Prior to C++ Dynamic Init (not related to above init() ). Meant to be user redefinable.
|
||||
|
||||
ets_task(loop_task,
|
||||
LOOP_TASK_PRIORITY, s_loop_queue,
|
||||
LOOP_QUEUE_SIZE);
|
||||
LOOP_TASK_PRIORITY, s_loop_queue,
|
||||
LOOP_QUEUE_SIZE);
|
||||
|
||||
system_init_done_cb(&init_done);
|
||||
}
|
||||
|
@ -1,26 +1,26 @@
|
||||
/*
|
||||
core_esp8266_noniso.c - nonstandard (but usefull) conversion functions
|
||||
core_esp8266_noniso.c - nonstandard (but usefull) conversion functions
|
||||
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2014 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
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
|
||||
|
||||
Modified 03 April 2015 by Markus Sattler
|
||||
Modified 03 April 2015 by Markus Sattler
|
||||
|
||||
*/
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@ -31,104 +31,85 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
char* ltoa(long value, char* result, int base)
|
||||
{
|
||||
return itoa((int)value, result, base);
|
||||
char* ltoa(long value, char* result, int base) {
|
||||
return itoa((int)value, result, base);
|
||||
}
|
||||
|
||||
char* ultoa(unsigned long value, char* result, int base) {
|
||||
return utoa((unsigned int)value, result, base);
|
||||
}
|
||||
|
||||
char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
|
||||
bool negative = false;
|
||||
|
||||
if (isnan(number)) {
|
||||
strcpy(s, "nan");
|
||||
return s;
|
||||
}
|
||||
|
||||
char* ultoa(unsigned long value, char* result, int base)
|
||||
{
|
||||
return utoa((unsigned int)value, result, base);
|
||||
}
|
||||
|
||||
char * dtostrf(double number, signed char width, unsigned char prec, char *s)
|
||||
{
|
||||
bool negative = false;
|
||||
|
||||
if (isnan(number))
|
||||
{
|
||||
strcpy(s, "nan");
|
||||
return s;
|
||||
}
|
||||
if (isinf(number))
|
||||
{
|
||||
strcpy(s, "inf");
|
||||
return s;
|
||||
}
|
||||
|
||||
char* out = s;
|
||||
|
||||
int fillme = width; // how many cells to fill for the integer part
|
||||
if (prec > 0)
|
||||
{
|
||||
fillme -= (prec + 1);
|
||||
}
|
||||
|
||||
// Handle negative numbers
|
||||
if (number < 0.0)
|
||||
{
|
||||
negative = true;
|
||||
fillme--;
|
||||
number = -number;
|
||||
}
|
||||
|
||||
// Round correctly so that print(1.999, 2) prints as "2.00"
|
||||
// I optimized out most of the divisions
|
||||
double rounding = 2.0;
|
||||
for (uint8_t i = 0; i < prec; ++i)
|
||||
{
|
||||
rounding *= 10.0;
|
||||
}
|
||||
rounding = 1.0 / rounding;
|
||||
|
||||
number += rounding;
|
||||
|
||||
// Figure out how big our number really is
|
||||
double tenpow = 1.0;
|
||||
int digitcount = 1;
|
||||
while (number >= 10.0 * tenpow)
|
||||
{
|
||||
tenpow *= 10.0;
|
||||
digitcount++;
|
||||
}
|
||||
|
||||
number /= tenpow;
|
||||
fillme -= digitcount;
|
||||
|
||||
// Pad unused cells with spaces
|
||||
while (fillme-- > 0)
|
||||
{
|
||||
*out++ = ' ';
|
||||
}
|
||||
|
||||
// Handle negative sign
|
||||
if (negative)
|
||||
{
|
||||
*out++ = '-';
|
||||
}
|
||||
|
||||
// Print the digits, and if necessary, the decimal point
|
||||
digitcount += prec;
|
||||
int8_t digit = 0;
|
||||
while (digitcount-- > 0)
|
||||
{
|
||||
digit = (int8_t)number;
|
||||
if (digit > 9)
|
||||
{
|
||||
digit = 9; // insurance
|
||||
}
|
||||
*out++ = (char)('0' | digit);
|
||||
if ((digitcount == prec) && (prec > 0))
|
||||
{
|
||||
*out++ = '.';
|
||||
}
|
||||
number -= digit;
|
||||
number *= 10.0;
|
||||
}
|
||||
|
||||
// make sure the string is terminated
|
||||
*out = 0;
|
||||
if (isinf(number)) {
|
||||
strcpy(s, "inf");
|
||||
return s;
|
||||
}
|
||||
|
||||
char* out = s;
|
||||
|
||||
int fillme = width; // how many cells to fill for the integer part
|
||||
if (prec > 0) {
|
||||
fillme -= (prec+1);
|
||||
}
|
||||
|
||||
// Handle negative numbers
|
||||
if (number < 0.0) {
|
||||
negative = true;
|
||||
fillme--;
|
||||
number = -number;
|
||||
}
|
||||
|
||||
// Round correctly so that print(1.999, 2) prints as "2.00"
|
||||
// I optimized out most of the divisions
|
||||
double rounding = 2.0;
|
||||
for (uint8_t i = 0; i < prec; ++i)
|
||||
rounding *= 10.0;
|
||||
rounding = 1.0 / rounding;
|
||||
|
||||
number += rounding;
|
||||
|
||||
// Figure out how big our number really is
|
||||
double tenpow = 1.0;
|
||||
int digitcount = 1;
|
||||
while (number >= 10.0 * tenpow) {
|
||||
tenpow *= 10.0;
|
||||
digitcount++;
|
||||
}
|
||||
|
||||
number /= tenpow;
|
||||
fillme -= digitcount;
|
||||
|
||||
// Pad unused cells with spaces
|
||||
while (fillme-- > 0) {
|
||||
*out++ = ' ';
|
||||
}
|
||||
|
||||
// Handle negative sign
|
||||
if (negative) *out++ = '-';
|
||||
|
||||
// Print the digits, and if necessary, the decimal point
|
||||
digitcount += prec;
|
||||
int8_t digit = 0;
|
||||
while (digitcount-- > 0) {
|
||||
digit = (int8_t)number;
|
||||
if (digit > 9) digit = 9; // insurance
|
||||
*out++ = (char)('0' | digit);
|
||||
if ((digitcount == prec) && (prec > 0)) {
|
||||
*out++ = '.';
|
||||
}
|
||||
number -= digit;
|
||||
number *= 10.0;
|
||||
}
|
||||
|
||||
// make sure the string is terminated
|
||||
*out = 0;
|
||||
return s;
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -1,23 +1,23 @@
|
||||
/*
|
||||
phy.c - ESP8266 PHY initialization data
|
||||
phy.c - ESP8266 PHY initialization data
|
||||
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2015 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
@ -30,332 +30,330 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
static const uint8_t ICACHE_FLASH_ATTR phy_init_data[128] =
|
||||
{
|
||||
/*[0] =*/ 5, // Reserved, do not change
|
||||
/*[1] =*/ 8, // Reserved, do not change
|
||||
/*[2] =*/ 4, // Reserved, do not change
|
||||
/*[3] =*/ 2, // Reserved, do not change
|
||||
/*[4] =*/ 5, // Reserved, do not change
|
||||
/*[5] =*/ 5, // Reserved, do not change
|
||||
/*[6] =*/ 5, // Reserved, do not change
|
||||
/*[7] =*/ 2, // Reserved, do not change
|
||||
/*[8] =*/ 5, // Reserved, do not change
|
||||
/*[9] =*/ 0, // Reserved, do not change
|
||||
/*[10] =*/ 4, // Reserved, do not change
|
||||
/*[11] =*/ 5, // Reserved, do not change
|
||||
/*[12] =*/ 5, // Reserved, do not change
|
||||
/*[13] =*/ 4, // Reserved, do not change
|
||||
/*[14] =*/ 5, // Reserved, do not change
|
||||
/*[15] =*/ 5, // Reserved, do not change
|
||||
/*[16] =*/ 4, // Reserved, do not change
|
||||
/*[17] =*/ (uint8_t) -2, // Reserved, do not change
|
||||
/*[18] =*/ (uint8_t) -3, // Reserved, do not change
|
||||
/*[19] =*/ (uint8_t) -1, // Reserved, do not change
|
||||
/*[20] =*/ (uint8_t) -16, // Reserved, do not change
|
||||
/*[21] =*/ (uint8_t) -16, // Reserved, do not change
|
||||
/*[22] =*/ (uint8_t) -16, // Reserved, do not change
|
||||
/*[23] =*/ (uint8_t) -32, // Reserved, do not change
|
||||
/*[24] =*/ (uint8_t) -32, // Reserved, do not change
|
||||
/*[25] =*/ (uint8_t) -32, // Reserved, do not change
|
||||
static const uint8_t ICACHE_FLASH_ATTR phy_init_data[128] =
|
||||
{
|
||||
/*[0] =*/ 5, // Reserved, do not change
|
||||
/*[1] =*/ 8, // Reserved, do not change
|
||||
/*[2] =*/ 4, // Reserved, do not change
|
||||
/*[3] =*/ 2, // Reserved, do not change
|
||||
/*[4] =*/ 5, // Reserved, do not change
|
||||
/*[5] =*/ 5, // Reserved, do not change
|
||||
/*[6] =*/ 5, // Reserved, do not change
|
||||
/*[7] =*/ 2, // Reserved, do not change
|
||||
/*[8] =*/ 5, // Reserved, do not change
|
||||
/*[9] =*/ 0, // Reserved, do not change
|
||||
/*[10] =*/ 4, // Reserved, do not change
|
||||
/*[11] =*/ 5, // Reserved, do not change
|
||||
/*[12] =*/ 5, // Reserved, do not change
|
||||
/*[13] =*/ 4, // Reserved, do not change
|
||||
/*[14] =*/ 5, // Reserved, do not change
|
||||
/*[15] =*/ 5, // Reserved, do not change
|
||||
/*[16] =*/ 4, // Reserved, do not change
|
||||
/*[17] =*/ (uint8_t)-2, // Reserved, do not change
|
||||
/*[18] =*/ (uint8_t)-3, // Reserved, do not change
|
||||
/*[19] =*/ (uint8_t)-1, // Reserved, do not change
|
||||
/*[20] =*/ (uint8_t)-16, // Reserved, do not change
|
||||
/*[21] =*/ (uint8_t)-16, // Reserved, do not change
|
||||
/*[22] =*/ (uint8_t)-16, // Reserved, do not change
|
||||
/*[23] =*/ (uint8_t)-32, // Reserved, do not change
|
||||
/*[24] =*/ (uint8_t)-32, // Reserved, do not change
|
||||
/*[25] =*/ (uint8_t)-32, // Reserved, do not change
|
||||
|
||||
/*[26] =*/ 225, // spur_freq_cfg, spur_freq=spur_freq_cfg/spur_freq_cfg_div
|
||||
/*[27] =*/ 10, // spur_freq_cfg_div
|
||||
// each bit for 1 channel, 1 to select the spur_freq if in band, else 40
|
||||
/*[28] =*/ 0xff, // spur_freq_en_h
|
||||
/*[29] =*/ 0xff, // spur_freq_en_l
|
||||
/*[26] =*/ 225, // spur_freq_cfg, spur_freq=spur_freq_cfg/spur_freq_cfg_div
|
||||
/*[27] =*/ 10, // spur_freq_cfg_div
|
||||
// each bit for 1 channel, 1 to select the spur_freq if in band, else 40
|
||||
/*[28] =*/ 0xff, // spur_freq_en_h
|
||||
/*[29] =*/ 0xff, // spur_freq_en_l
|
||||
|
||||
/*[30] =*/ 0xf8, // Reserved, do not change
|
||||
/*[31] =*/ 0, // Reserved, do not change
|
||||
/*[32] =*/ 0xf8, // Reserved, do not change
|
||||
/*[33] =*/ 0xf8, // Reserved, do not change
|
||||
/*[30] =*/ 0xf8, // Reserved, do not change
|
||||
/*[31] =*/ 0, // Reserved, do not change
|
||||
/*[32] =*/ 0xf8, // Reserved, do not change
|
||||
/*[33] =*/ 0xf8, // Reserved, do not change
|
||||
|
||||
/*[34] =*/ 78, // target_power_qdb_0, target power is 78/4=19.5dbm
|
||||
/*[35] =*/ 74, // target_power_qdb_1, target power is 74/4=18.5dbm
|
||||
/*[36] =*/ 70, // target_power_qdb_2, target power is 70/4=17.5dbm
|
||||
/*[37] =*/ 64, // target_power_qdb_3, target power is 64/4=16dbm
|
||||
/*[38] =*/ 60, // target_power_qdb_4, target power is 60/4=15dbm
|
||||
/*[39] =*/ 56, // target_power_qdb_5, target power is 56/4=14dbm
|
||||
/*[34] =*/ 78, // target_power_qdb_0, target power is 78/4=19.5dbm
|
||||
/*[35] =*/ 74, // target_power_qdb_1, target power is 74/4=18.5dbm
|
||||
/*[36] =*/ 70, // target_power_qdb_2, target power is 70/4=17.5dbm
|
||||
/*[37] =*/ 64, // target_power_qdb_3, target power is 64/4=16dbm
|
||||
/*[38] =*/ 60, // target_power_qdb_4, target power is 60/4=15dbm
|
||||
/*[39] =*/ 56, // target_power_qdb_5, target power is 56/4=14dbm
|
||||
|
||||
/*[40] =*/ 0, // target_power_index_mcs0
|
||||
/*[41] =*/ 0, // target_power_index_mcs1
|
||||
/*[42] =*/ 1, // target_power_index_mcs2
|
||||
/*[43] =*/ 1, // target_power_index_mcs3
|
||||
/*[44] =*/ 2, // target_power_index_mcs4
|
||||
/*[45] =*/ 3, // target_power_index_mcs5
|
||||
/*[46] =*/ 4, // target_power_index_mcs6
|
||||
/*[47] =*/ 5, // target_power_index_mcs7
|
||||
/*[40] =*/ 0, // target_power_index_mcs0
|
||||
/*[41] =*/ 0, // target_power_index_mcs1
|
||||
/*[42] =*/ 1, // target_power_index_mcs2
|
||||
/*[43] =*/ 1, // target_power_index_mcs3
|
||||
/*[44] =*/ 2, // target_power_index_mcs4
|
||||
/*[45] =*/ 3, // target_power_index_mcs5
|
||||
/*[46] =*/ 4, // target_power_index_mcs6
|
||||
/*[47] =*/ 5, // target_power_index_mcs7
|
||||
|
||||
// crystal_26m_en
|
||||
// 0: 40MHz
|
||||
// 1: 26MHz
|
||||
// 2: 24MHz
|
||||
#if F_CRYSTAL == 40000000
|
||||
/*[48] =*/ 0,
|
||||
#else
|
||||
/*[48] =*/ 1,
|
||||
#endif
|
||||
// crystal_26m_en
|
||||
// 0: 40MHz
|
||||
// 1: 26MHz
|
||||
// 2: 24MHz
|
||||
#if F_CRYSTAL == 40000000
|
||||
/*[48] =*/ 0,
|
||||
#else
|
||||
/*[48] =*/ 1,
|
||||
#endif
|
||||
|
||||
/*[49] =*/ 0,
|
||||
/*[49] =*/ 0,
|
||||
|
||||
// sdio_configure
|
||||
// 0: Auto by pin strapping
|
||||
// 1: SDIO dataoutput is at negative edges (SDIO V1.1)
|
||||
// 2: SDIO dataoutput is at positive edges (SDIO V2.0)
|
||||
/*[50] =*/ 0,
|
||||
// sdio_configure
|
||||
// 0: Auto by pin strapping
|
||||
// 1: SDIO dataoutput is at negative edges (SDIO V1.1)
|
||||
// 2: SDIO dataoutput is at positive edges (SDIO V2.0)
|
||||
/*[50] =*/ 0,
|
||||
|
||||
// bt_configure
|
||||
// 0: None,no bluetooth
|
||||
// 1: GPIO0 -> WLAN_ACTIVE/ANT_SEL_WIFI
|
||||
// MTMS -> BT_ACTIVE
|
||||
// MTCK -> BT_PRIORITY
|
||||
// U0RXD -> ANT_SEL_BT
|
||||
// 2: None, have bluetooth
|
||||
// 3: GPIO0 -> WLAN_ACTIVE/ANT_SEL_WIFI
|
||||
// MTMS -> BT_PRIORITY
|
||||
// MTCK -> BT_ACTIVE
|
||||
// U0RXD -> ANT_SEL_BT
|
||||
/*[51] =*/ 0,
|
||||
// bt_configure
|
||||
// 0: None,no bluetooth
|
||||
// 1: GPIO0 -> WLAN_ACTIVE/ANT_SEL_WIFI
|
||||
// MTMS -> BT_ACTIVE
|
||||
// MTCK -> BT_PRIORITY
|
||||
// U0RXD -> ANT_SEL_BT
|
||||
// 2: None, have bluetooth
|
||||
// 3: GPIO0 -> WLAN_ACTIVE/ANT_SEL_WIFI
|
||||
// MTMS -> BT_PRIORITY
|
||||
// MTCK -> BT_ACTIVE
|
||||
// U0RXD -> ANT_SEL_BT
|
||||
/*[51] =*/ 0,
|
||||
|
||||
// bt_protocol
|
||||
// 0: WiFi-BT are not enabled. Antenna is for WiFi
|
||||
// 1: WiFi-BT are not enabled. Antenna is for BT
|
||||
// 2: WiFi-BT 2-wire are enabled, (only use BT_ACTIVE), independent ant
|
||||
// 3: WiFi-BT 3-wire are enabled, (when BT_ACTIVE = 0, BT_PRIORITY must be 0), independent ant
|
||||
// 4: WiFi-BT 2-wire are enabled, (only use BT_ACTIVE), share ant
|
||||
// 5: WiFi-BT 3-wire are enabled, (when BT_ACTIVE = 0, BT_PRIORITY must be 0), share ant
|
||||
/*[52] =*/ 0,
|
||||
// bt_protocol
|
||||
// 0: WiFi-BT are not enabled. Antenna is for WiFi
|
||||
// 1: WiFi-BT are not enabled. Antenna is for BT
|
||||
// 2: WiFi-BT 2-wire are enabled, (only use BT_ACTIVE), independent ant
|
||||
// 3: WiFi-BT 3-wire are enabled, (when BT_ACTIVE = 0, BT_PRIORITY must be 0), independent ant
|
||||
// 4: WiFi-BT 2-wire are enabled, (only use BT_ACTIVE), share ant
|
||||
// 5: WiFi-BT 3-wire are enabled, (when BT_ACTIVE = 0, BT_PRIORITY must be 0), share ant
|
||||
/*[52] =*/ 0,
|
||||
|
||||
// dual_ant_configure
|
||||
// 0: None
|
||||
// 1: dual_ant (antenna diversity for WiFi-only): GPIO0 + U0RXD
|
||||
// 2: T/R switch for External PA/LNA: GPIO0 is high and U0RXD is low during Tx
|
||||
// 3: T/R switch for External PA/LNA: GPIO0 is low and U0RXD is high during Tx
|
||||
/*[53] =*/ 0,
|
||||
// dual_ant_configure
|
||||
// 0: None
|
||||
// 1: dual_ant (antenna diversity for WiFi-only): GPIO0 + U0RXD
|
||||
// 2: T/R switch for External PA/LNA: GPIO0 is high and U0RXD is low during Tx
|
||||
// 3: T/R switch for External PA/LNA: GPIO0 is low and U0RXD is high during Tx
|
||||
/*[53] =*/ 0,
|
||||
|
||||
/*[54] =*/ 2, // Reserved, do not change
|
||||
/*[54] =*/ 2, // Reserved, do not change
|
||||
|
||||
// share_xtal
|
||||
// This option is to share crystal clock for BT
|
||||
// The state of Crystal during sleeping
|
||||
// 0: Off
|
||||
// 1: Forcely On
|
||||
// 2: Automatically On according to XPD_DCDC
|
||||
// 3: Automatically On according to GPIO2
|
||||
/*[55] =*/ 0,
|
||||
// share_xtal
|
||||
// This option is to share crystal clock for BT
|
||||
// The state of Crystal during sleeping
|
||||
// 0: Off
|
||||
// 1: Forcely On
|
||||
// 2: Automatically On according to XPD_DCDC
|
||||
// 3: Automatically On according to GPIO2
|
||||
/*[55] =*/ 0,
|
||||
|
||||
/*[56] =*/ 0,
|
||||
/*[57] =*/ 0,
|
||||
/*[58] =*/ 0,
|
||||
/*[59] =*/ 0,
|
||||
/*[60] =*/ 0,
|
||||
/*[61] =*/ 0,
|
||||
/*[62] =*/ 0,
|
||||
/*[63] =*/ 0,
|
||||
/*[56] =*/ 0,
|
||||
/*[57] =*/ 0,
|
||||
/*[58] =*/ 0,
|
||||
/*[59] =*/ 0,
|
||||
/*[60] =*/ 0,
|
||||
/*[61] =*/ 0,
|
||||
/*[62] =*/ 0,
|
||||
/*[63] =*/ 0,
|
||||
|
||||
/*[64] =*/ 225, // spur_freq_cfg_2, spur_freq_2=spur_freq_cfg_2/spur_freq_cfg_div_2
|
||||
/*[65] =*/ 10, // spur_freq_cfg_div_2
|
||||
/*[66] =*/ 0, // spur_freq_en_h_2
|
||||
/*[67] =*/ 0, // spur_freq_en_l_2
|
||||
/*[68] =*/ 0, // spur_freq_cfg_msb
|
||||
/*[69] =*/ 0, // spur_freq_cfg_2_msb
|
||||
/*[70] =*/ 0, // spur_freq_cfg_3_low
|
||||
/*[71] =*/ 0, // spur_freq_cfg_3_high
|
||||
/*[72] =*/ 0, // spur_freq_cfg_4_low
|
||||
/*[73] =*/ 0, // spur_freq_cfg_4_high
|
||||
/*[64] =*/ 225, // spur_freq_cfg_2, spur_freq_2=spur_freq_cfg_2/spur_freq_cfg_div_2
|
||||
/*[65] =*/ 10, // spur_freq_cfg_div_2
|
||||
/*[66] =*/ 0, // spur_freq_en_h_2
|
||||
/*[67] =*/ 0, // spur_freq_en_l_2
|
||||
/*[68] =*/ 0, // spur_freq_cfg_msb
|
||||
/*[69] =*/ 0, // spur_freq_cfg_2_msb
|
||||
/*[70] =*/ 0, // spur_freq_cfg_3_low
|
||||
/*[71] =*/ 0, // spur_freq_cfg_3_high
|
||||
/*[72] =*/ 0, // spur_freq_cfg_4_low
|
||||
/*[73] =*/ 0, // spur_freq_cfg_4_high
|
||||
|
||||
/*[74] =*/ 1, // Reserved, do not change
|
||||
/*[75] =*/ 0x93, // Reserved, do not change
|
||||
/*[76] =*/ 0x43, // Reserved, do not change
|
||||
/*[77] =*/ 0x00, // Reserved, do not change
|
||||
/*[74] =*/ 1, // Reserved, do not change
|
||||
/*[75] =*/ 0x93, // Reserved, do not change
|
||||
/*[76] =*/ 0x43, // Reserved, do not change
|
||||
/*[77] =*/ 0x00, // Reserved, do not change
|
||||
|
||||
/*[78] =*/ 0,
|
||||
/*[79] =*/ 0,
|
||||
/*[80] =*/ 0,
|
||||
/*[81] =*/ 0,
|
||||
/*[82] =*/ 0,
|
||||
/*[83] =*/ 0,
|
||||
/*[84] =*/ 0,
|
||||
/*[85] =*/ 0,
|
||||
/*[86] =*/ 0,
|
||||
/*[87] =*/ 0,
|
||||
/*[88] =*/ 0,
|
||||
/*[89] =*/ 0,
|
||||
/*[90] =*/ 0,
|
||||
/*[91] =*/ 0,
|
||||
/*[92] =*/ 0,
|
||||
/*[78] =*/ 0,
|
||||
/*[79] =*/ 0,
|
||||
/*[80] =*/ 0,
|
||||
/*[81] =*/ 0,
|
||||
/*[82] =*/ 0,
|
||||
/*[83] =*/ 0,
|
||||
/*[84] =*/ 0,
|
||||
/*[85] =*/ 0,
|
||||
/*[86] =*/ 0,
|
||||
/*[87] =*/ 0,
|
||||
/*[88] =*/ 0,
|
||||
/*[89] =*/ 0,
|
||||
/*[90] =*/ 0,
|
||||
/*[91] =*/ 0,
|
||||
/*[92] =*/ 0,
|
||||
|
||||
// low_power_en
|
||||
// 0: disable low power mode
|
||||
// 1: enable low power mode
|
||||
/*[93] =*/ 0,
|
||||
// low_power_en
|
||||
// 0: disable low power mode
|
||||
// 1: enable low power mode
|
||||
/*[93] =*/ 0,
|
||||
|
||||
// lp_rf_stg10
|
||||
// the attenuation of RF gain stage 0 and 1,
|
||||
// 0xf: 0db,
|
||||
// 0xe: -2.5db,
|
||||
// 0xd: -6db,
|
||||
// 0x9: -8.5db,
|
||||
// 0xc: -11.5db,
|
||||
// 0x8: -14db,
|
||||
// 0x4: -17.5,
|
||||
// 0x0: -23
|
||||
/*[94] =*/ 0x00,
|
||||
// lp_rf_stg10
|
||||
// the attenuation of RF gain stage 0 and 1,
|
||||
// 0xf: 0db,
|
||||
// 0xe: -2.5db,
|
||||
// 0xd: -6db,
|
||||
// 0x9: -8.5db,
|
||||
// 0xc: -11.5db,
|
||||
// 0x8: -14db,
|
||||
// 0x4: -17.5,
|
||||
// 0x0: -23
|
||||
/*[94] =*/ 0x00,
|
||||
|
||||
|
||||
// lp_bb_att_ext
|
||||
// the attenuation of BB gain,
|
||||
// 0: 0db,
|
||||
// 1: -0.25db,
|
||||
// 2: -0.5db,
|
||||
// 3: -0.75db,
|
||||
// 4: -1db,
|
||||
// 5: -1.25db,
|
||||
// 6: -1.5db,
|
||||
// 7: -1.75db,
|
||||
// 8: -2db
|
||||
// max valve is 24(-6db)
|
||||
/*[95] =*/ 0,
|
||||
// lp_bb_att_ext
|
||||
// the attenuation of BB gain,
|
||||
// 0: 0db,
|
||||
// 1: -0.25db,
|
||||
// 2: -0.5db,
|
||||
// 3: -0.75db,
|
||||
// 4: -1db,
|
||||
// 5: -1.25db,
|
||||
// 6: -1.5db,
|
||||
// 7: -1.75db,
|
||||
// 8: -2db
|
||||
// max valve is 24(-6db)
|
||||
/*[95] =*/ 0,
|
||||
|
||||
// pwr_ind_11b_en
|
||||
// 0: 11b power is same as mcs0 and 6m
|
||||
// 1: enable 11b power different with ofdm
|
||||
/*[96] =*/ 0,
|
||||
// pwr_ind_11b_en
|
||||
// 0: 11b power is same as mcs0 and 6m
|
||||
// 1: enable 11b power different with ofdm
|
||||
/*[96] =*/ 0,
|
||||
|
||||
// pwr_ind_11b_0
|
||||
// 1m, 2m power index [0~5]
|
||||
/*[97] =*/ 0,
|
||||
// pwr_ind_11b_0
|
||||
// 1m, 2m power index [0~5]
|
||||
/*[97] =*/ 0,
|
||||
|
||||
// pwr_ind_11b_1
|
||||
// 5.5m, 11m power index [0~5]
|
||||
/*[98] =*/ 0,
|
||||
// pwr_ind_11b_1
|
||||
// 5.5m, 11m power index [0~5]
|
||||
/*[98] =*/ 0,
|
||||
|
||||
/*[99] =*/ 0,
|
||||
/*[100] =*/ 0,
|
||||
/*[101] =*/ 0,
|
||||
/*[102] =*/ 0,
|
||||
/*[103] =*/ 0,
|
||||
/*[104] =*/ 0,
|
||||
/*[105] =*/ 0,
|
||||
/*[106] =*/ 0,
|
||||
/*[99] =*/ 0,
|
||||
/*[100] =*/ 0,
|
||||
/*[101] =*/ 0,
|
||||
/*[102] =*/ 0,
|
||||
/*[103] =*/ 0,
|
||||
/*[104] =*/ 0,
|
||||
/*[105] =*/ 0,
|
||||
/*[106] =*/ 0,
|
||||
|
||||
// vdd33_const
|
||||
// the voltage of PA_VDD
|
||||
// x=0xff: it can measure VDD33,
|
||||
// 18<=x<=36: use input voltage,
|
||||
// the value is voltage*10, 33 is 3.3V, 30 is 3.0V,
|
||||
// x<18 or x>36: default voltage is 3.3V
|
||||
//
|
||||
// the value of this byte depend from the TOUT pin usage (1 or 2):
|
||||
// 1)
|
||||
// analogRead function (system_adc_read()):
|
||||
// is only available when wire TOUT pin17 to external circuitry, Input Voltage Range restricted to 0 ~ 1.0V.
|
||||
// For this function the vdd33_const must be set as real power voltage of VDD3P3 pin 3 and 4
|
||||
// The range of operating voltage of ESP8266 is 1.8V~3.6V,the unit of vdd33_const is 0.1V,so effective value range of vdd33_const is [18,36]
|
||||
// 2)
|
||||
// getVcc function (system_get_vdd33):
|
||||
// is only available when TOUT pin17 is suspended (floating), this function measure the power voltage of VDD3P3 pin 3 and 4
|
||||
// For this function the vdd33_const must be set to 255 (0xFF).
|
||||
/*[107] =*/ 33,
|
||||
// vdd33_const
|
||||
// the voltage of PA_VDD
|
||||
// x=0xff: it can measure VDD33,
|
||||
// 18<=x<=36: use input voltage,
|
||||
// the value is voltage*10, 33 is 3.3V, 30 is 3.0V,
|
||||
// x<18 or x>36: default voltage is 3.3V
|
||||
//
|
||||
// the value of this byte depend from the TOUT pin usage (1 or 2):
|
||||
// 1)
|
||||
// analogRead function (system_adc_read()):
|
||||
// is only available when wire TOUT pin17 to external circuitry, Input Voltage Range restricted to 0 ~ 1.0V.
|
||||
// For this function the vdd33_const must be set as real power voltage of VDD3P3 pin 3 and 4
|
||||
// The range of operating voltage of ESP8266 is 1.8V~3.6V,the unit of vdd33_const is 0.1V,so effective value range of vdd33_const is [18,36]
|
||||
// 2)
|
||||
// getVcc function (system_get_vdd33):
|
||||
// is only available when TOUT pin17 is suspended (floating), this function measure the power voltage of VDD3P3 pin 3 and 4
|
||||
// For this function the vdd33_const must be set to 255 (0xFF).
|
||||
/*[107] =*/ 33,
|
||||
|
||||
// disable RF calibration for certain number of times
|
||||
/*[108] =*/ 0,
|
||||
// disable RF calibration for certain number of times
|
||||
/*[108] =*/ 0,
|
||||
|
||||
/*[109] =*/ 0,
|
||||
/*[110] =*/ 0,
|
||||
/*[111] =*/ 0,
|
||||
/*[109] =*/ 0,
|
||||
/*[110] =*/ 0,
|
||||
/*[111] =*/ 0,
|
||||
|
||||
// freq_correct_en
|
||||
// bit[0]:0->do not correct frequency offset, 1->correct frequency offset.
|
||||
// bit[1]:0->bbpll is 168M, it can correct + and - frequency offset, 1->bbpll is 160M, it only can correct + frequency offset
|
||||
// bit[2]:0->auto measure frequency offset and correct it, 1->use 113 byte force_freq_offset to correct frequency offset.
|
||||
// 0: do not correct frequency offset.
|
||||
// 1: auto measure frequency offset and correct it, bbpll is 168M, it can correct + and - frequency offset.
|
||||
// 3: auto measure frequency offset and correct it, bbpll is 160M, it only can correct + frequency offset.
|
||||
// 5: use 113 byte force_freq_offset to correct frequency offset, bbpll is 168M, it can correct + and - frequency offset.
|
||||
// 7: use 113 byte force_freq_offset to correct frequency offset, bbpll is 160M , it only can correct + frequency offset.
|
||||
/*[112] =*/ 0,
|
||||
// freq_correct_en
|
||||
// bit[0]:0->do not correct frequency offset, 1->correct frequency offset.
|
||||
// bit[1]:0->bbpll is 168M, it can correct + and - frequency offset, 1->bbpll is 160M, it only can correct + frequency offset
|
||||
// bit[2]:0->auto measure frequency offset and correct it, 1->use 113 byte force_freq_offset to correct frequency offset.
|
||||
// 0: do not correct frequency offset.
|
||||
// 1: auto measure frequency offset and correct it, bbpll is 168M, it can correct + and - frequency offset.
|
||||
// 3: auto measure frequency offset and correct it, bbpll is 160M, it only can correct + frequency offset.
|
||||
// 5: use 113 byte force_freq_offset to correct frequency offset, bbpll is 168M, it can correct + and - frequency offset.
|
||||
// 7: use 113 byte force_freq_offset to correct frequency offset, bbpll is 160M , it only can correct + frequency offset.
|
||||
/*[112] =*/ 0,
|
||||
|
||||
// force_freq_offset
|
||||
// signed, unit is 8kHz
|
||||
/*[113] =*/ 0,
|
||||
// force_freq_offset
|
||||
// signed, unit is 8kHz
|
||||
/*[113] =*/ 0,
|
||||
|
||||
// rf_cal_use_flash
|
||||
// 0: RF init no RF CAL, using all RF CAL data in flash, it takes about 2ms for RF init
|
||||
// 1: RF init only do TX power control CAL, others using RF CAL data in flash, it takes about 20ms for RF init
|
||||
// 2: RF init no RF CAL, using all RF CAL data in flash, it takes about 2ms for RF init (same as 0?!)
|
||||
// 3: RF init do all RF CAL, it takes about 200ms for RF init
|
||||
/*[114] =*/ 1
|
||||
};
|
||||
// rf_cal_use_flash
|
||||
// 0: RF init no RF CAL, using all RF CAL data in flash, it takes about 2ms for RF init
|
||||
// 1: RF init only do TX power control CAL, others using RF CAL data in flash, it takes about 20ms for RF init
|
||||
// 2: RF init no RF CAL, using all RF CAL data in flash, it takes about 2ms for RF init (same as 0?!)
|
||||
// 3: RF init do all RF CAL, it takes about 200ms for RF init
|
||||
/*[114] =*/ 1
|
||||
};
|
||||
|
||||
|
||||
// These functions will be overriden from C++ code.
|
||||
// Unfortunately, we can't use extern "C" because Arduino preprocessor
|
||||
// doesn't generate forward declarations for extern "C" functions correctly,
|
||||
// so we use mangled names here.
|
||||
// These functions will be overriden from C++ code.
|
||||
// Unfortunately, we can't use extern "C" because Arduino preprocessor
|
||||
// doesn't generate forward declarations for extern "C" functions correctly,
|
||||
// so we use mangled names here.
|
||||
#define __get_adc_mode _Z14__get_adc_modev
|
||||
#define __get_rf_mode _Z13__get_rf_modev
|
||||
#define __run_user_rf_pre_init _Z22__run_user_rf_pre_initv
|
||||
|
||||
static bool spoof_init_data = false;
|
||||
static bool spoof_init_data = false;
|
||||
|
||||
extern int __real_spi_flash_read(uint32_t addr, uint32_t* dst, size_t size);
|
||||
extern int ICACHE_RAM_ATTR __wrap_spi_flash_read(uint32_t addr, uint32_t* dst, size_t size);
|
||||
extern int __get_adc_mode();
|
||||
extern int __real_spi_flash_read(uint32_t addr, uint32_t* dst, size_t size);
|
||||
extern int ICACHE_RAM_ATTR __wrap_spi_flash_read(uint32_t addr, uint32_t* dst, size_t size);
|
||||
extern int __get_adc_mode();
|
||||
|
||||
extern int ICACHE_RAM_ATTR __wrap_spi_flash_read(uint32_t addr, uint32_t* dst, size_t size)
|
||||
{
|
||||
if (!spoof_init_data || size != 128)
|
||||
{
|
||||
return __real_spi_flash_read(addr, dst, size);
|
||||
}
|
||||
|
||||
memcpy(dst, phy_init_data, sizeof(phy_init_data));
|
||||
((uint8_t*)dst)[107] = __get_adc_mode();
|
||||
return 0;
|
||||
extern int ICACHE_RAM_ATTR __wrap_spi_flash_read(uint32_t addr, uint32_t* dst, size_t size)
|
||||
{
|
||||
if (!spoof_init_data || size != 128) {
|
||||
return __real_spi_flash_read(addr, dst, size);
|
||||
}
|
||||
|
||||
extern int __get_rf_mode(void) __attribute__((weak));
|
||||
extern int __get_rf_mode(void)
|
||||
{
|
||||
return -1; // mode not set
|
||||
}
|
||||
|
||||
extern int __get_adc_mode(void) __attribute__((weak));
|
||||
extern int __get_adc_mode(void)
|
||||
{
|
||||
return 33; // default ADC mode
|
||||
}
|
||||
|
||||
extern void __run_user_rf_pre_init(void) __attribute__((weak));
|
||||
extern void __run_user_rf_pre_init(void)
|
||||
{
|
||||
return; // default do noting
|
||||
}
|
||||
|
||||
uint32_t user_rf_cal_sector_set(void)
|
||||
{
|
||||
spoof_init_data = true;
|
||||
return flashchip->chip_size / SPI_FLASH_SEC_SIZE - 4;
|
||||
}
|
||||
|
||||
void user_rf_pre_init()
|
||||
{
|
||||
// *((volatile uint32_t*) 0x60000710) = 0;
|
||||
spoof_init_data = false;
|
||||
volatile uint32_t* rtc_reg = (volatile uint32_t*) 0x60001000;
|
||||
rtc_reg[30] = 0;
|
||||
|
||||
system_set_os_print(0);
|
||||
int rf_mode = __get_rf_mode();
|
||||
if (rf_mode >= 0)
|
||||
{
|
||||
system_phy_set_rfoption(rf_mode);
|
||||
}
|
||||
__run_user_rf_pre_init();
|
||||
memcpy(dst, phy_init_data, sizeof(phy_init_data));
|
||||
((uint8_t*)dst)[107] = __get_adc_mode();
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern int __get_rf_mode(void) __attribute__((weak));
|
||||
extern int __get_rf_mode(void)
|
||||
{
|
||||
return -1; // mode not set
|
||||
}
|
||||
|
||||
extern int __get_adc_mode(void) __attribute__((weak));
|
||||
extern int __get_adc_mode(void)
|
||||
{
|
||||
return 33; // default ADC mode
|
||||
}
|
||||
|
||||
extern void __run_user_rf_pre_init(void) __attribute__((weak));
|
||||
extern void __run_user_rf_pre_init(void)
|
||||
{
|
||||
return; // default do noting
|
||||
}
|
||||
|
||||
uint32_t user_rf_cal_sector_set(void)
|
||||
{
|
||||
spoof_init_data = true;
|
||||
return flashchip->chip_size/SPI_FLASH_SEC_SIZE - 4;
|
||||
}
|
||||
|
||||
void user_rf_pre_init()
|
||||
{
|
||||
// *((volatile uint32_t*) 0x60000710) = 0;
|
||||
spoof_init_data = false;
|
||||
volatile uint32_t* rtc_reg = (volatile uint32_t*) 0x60001000;
|
||||
rtc_reg[30] = 0;
|
||||
|
||||
system_set_os_print(0);
|
||||
int rf_mode = __get_rf_mode();
|
||||
if (rf_mode >= 0) {
|
||||
system_phy_set_rfoption(rf_mode);
|
||||
}
|
||||
__run_user_rf_pre_init();
|
||||
}
|
||||
|
||||
|
||||
void ICACHE_RAM_ATTR user_spi_flash_dio_to_qio_pre_init() {}
|
||||
void ICACHE_RAM_ATTR user_spi_flash_dio_to_qio_pre_init() {}
|
||||
|
||||
};
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
postmortem.c - output of debug info on sketch crash
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
postmortem.c - output of debug info on sketch crash
|
||||
Copyright (c) 2015 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
@ -35,253 +35,223 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
extern void __real_system_restart_local();
|
||||
extern void __real_system_restart_local();
|
||||
|
||||
// These will be pointers to PROGMEM const strings
|
||||
static const char* s_panic_file = 0;
|
||||
static int s_panic_line = 0;
|
||||
static const char* s_panic_func = 0;
|
||||
static const char* s_panic_what = 0;
|
||||
// These will be pointers to PROGMEM const strings
|
||||
static const char* s_panic_file = 0;
|
||||
static int s_panic_line = 0;
|
||||
static const char* s_panic_func = 0;
|
||||
static const char* s_panic_what = 0;
|
||||
|
||||
static bool s_abort_called = false;
|
||||
static const char* s_unhandled_exception = NULL;
|
||||
static bool s_abort_called = false;
|
||||
static const char* s_unhandled_exception = NULL;
|
||||
|
||||
void abort() __attribute__((noreturn));
|
||||
static void uart_write_char_d(char c);
|
||||
static void uart0_write_char_d(char c);
|
||||
static void uart1_write_char_d(char c);
|
||||
static void print_stack(uint32_t start, uint32_t end);
|
||||
void abort() __attribute__((noreturn));
|
||||
static void uart_write_char_d(char c);
|
||||
static void uart0_write_char_d(char c);
|
||||
static void uart1_write_char_d(char c);
|
||||
static void print_stack(uint32_t start, uint32_t end);
|
||||
|
||||
// From UMM, the last caller of a malloc/realloc/calloc which failed:
|
||||
extern void *umm_last_fail_alloc_addr;
|
||||
extern int umm_last_fail_alloc_size;
|
||||
// From UMM, the last caller of a malloc/realloc/calloc which failed:
|
||||
extern void *umm_last_fail_alloc_addr;
|
||||
extern int umm_last_fail_alloc_size;
|
||||
|
||||
static void raise_exception() __attribute__((noreturn));
|
||||
static void raise_exception() __attribute__((noreturn));
|
||||
|
||||
extern void __custom_crash_callback(struct rst_info * rst_info, uint32_t stack, uint32_t stack_end)
|
||||
{
|
||||
(void) rst_info;
|
||||
(void) stack;
|
||||
(void) stack_end;
|
||||
extern void __custom_crash_callback( struct rst_info * rst_info, uint32_t stack, uint32_t stack_end ) {
|
||||
(void) rst_info;
|
||||
(void) stack;
|
||||
(void) stack_end;
|
||||
}
|
||||
|
||||
extern void custom_crash_callback( struct rst_info * rst_info, uint32_t stack, uint32_t stack_end ) __attribute__ ((weak, alias("__custom_crash_callback")));
|
||||
|
||||
|
||||
// Prints need to use our library function to allow for file and function
|
||||
// to be safely accessed from flash. This function encapsulates snprintf()
|
||||
// [which by definition will 0-terminate] and dumping to the UART
|
||||
static void ets_printf_P(const char *str, ...) {
|
||||
char destStr[160];
|
||||
char *c = destStr;
|
||||
va_list argPtr;
|
||||
va_start(argPtr, str);
|
||||
vsnprintf(destStr, sizeof(destStr), str, argPtr);
|
||||
va_end(argPtr);
|
||||
while (*c) {
|
||||
ets_putc(*(c++));
|
||||
}
|
||||
}
|
||||
|
||||
extern void custom_crash_callback(struct rst_info * rst_info, uint32_t stack, uint32_t stack_end) __attribute__((weak, alias("__custom_crash_callback")));
|
||||
void __wrap_system_restart_local() {
|
||||
register uint32_t sp asm("a1");
|
||||
uint32_t sp_dump = sp;
|
||||
|
||||
|
||||
// Prints need to use our library function to allow for file and function
|
||||
// to be safely accessed from flash. This function encapsulates snprintf()
|
||||
// [which by definition will 0-terminate] and dumping to the UART
|
||||
static void ets_printf_P(const char *str, ...)
|
||||
{
|
||||
char destStr[160];
|
||||
char *c = destStr;
|
||||
va_list argPtr;
|
||||
va_start(argPtr, str);
|
||||
vsnprintf(destStr, sizeof(destStr), str, argPtr);
|
||||
va_end(argPtr);
|
||||
while (*c)
|
||||
{
|
||||
ets_putc(*(c++));
|
||||
}
|
||||
}
|
||||
|
||||
void __wrap_system_restart_local()
|
||||
{
|
||||
register uint32_t sp asm("a1");
|
||||
uint32_t sp_dump = sp;
|
||||
|
||||
if (gdb_present())
|
||||
{
|
||||
/* When GDBStub is present, exceptions are handled by GDBStub,
|
||||
but Soft WDT will still call this function.
|
||||
Trigger an exception to break into GDB.
|
||||
TODO: check why gdb_do_break() or asm("break.n 0") do not
|
||||
break into GDB here. */
|
||||
raise_exception();
|
||||
}
|
||||
|
||||
struct rst_info rst_info;
|
||||
memset(&rst_info, 0, sizeof(rst_info));
|
||||
system_rtc_mem_read(0, &rst_info, sizeof(rst_info));
|
||||
if (rst_info.reason != REASON_SOFT_WDT_RST &&
|
||||
rst_info.reason != REASON_EXCEPTION_RST &&
|
||||
rst_info.reason != REASON_WDT_RST)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: ets_install_putc1 definition is wrong in ets_sys.h, need cast
|
||||
ets_install_putc1((void *)&uart_write_char_d);
|
||||
|
||||
if (s_panic_line)
|
||||
{
|
||||
ets_printf_P(PSTR("\nPanic %S:%d %S"), s_panic_file, s_panic_line, s_panic_func);
|
||||
if (s_panic_what)
|
||||
{
|
||||
ets_printf_P(PSTR(": Assertion '%S' failed."), s_panic_what);
|
||||
}
|
||||
ets_putc('\n');
|
||||
}
|
||||
else if (s_unhandled_exception)
|
||||
{
|
||||
ets_printf_P(PSTR("\nUnhandled C++ exception: %S\n"), s_unhandled_exception);
|
||||
}
|
||||
else if (s_abort_called)
|
||||
{
|
||||
ets_printf_P(PSTR("\nAbort called\n"));
|
||||
}
|
||||
else if (rst_info.reason == REASON_EXCEPTION_RST)
|
||||
{
|
||||
ets_printf_P(PSTR("\nException (%d):\nepc1=0x%08x epc2=0x%08x epc3=0x%08x excvaddr=0x%08x depc=0x%08x\n"),
|
||||
rst_info.exccause, rst_info.epc1, rst_info.epc2, rst_info.epc3, rst_info.excvaddr, rst_info.depc);
|
||||
}
|
||||
else if (rst_info.reason == REASON_SOFT_WDT_RST)
|
||||
{
|
||||
ets_printf_P(PSTR("\nSoft WDT reset\n"));
|
||||
}
|
||||
|
||||
uint32_t cont_stack_start = (uint32_t) & (g_pcont->stack);
|
||||
uint32_t cont_stack_end = (uint32_t) g_pcont->stack_end;
|
||||
uint32_t stack_end;
|
||||
|
||||
// amount of stack taken by interrupt or exception handler
|
||||
// and everything up to __wrap_system_restart_local
|
||||
// (determined empirically, might break)
|
||||
uint32_t offset = 0;
|
||||
if (rst_info.reason == REASON_SOFT_WDT_RST)
|
||||
{
|
||||
offset = 0x1b0;
|
||||
}
|
||||
else if (rst_info.reason == REASON_EXCEPTION_RST)
|
||||
{
|
||||
offset = 0x1a0;
|
||||
}
|
||||
else if (rst_info.reason == REASON_WDT_RST)
|
||||
{
|
||||
offset = 0x10;
|
||||
}
|
||||
|
||||
ets_printf_P(PSTR("\n>>>stack>>>\n"));
|
||||
|
||||
if (sp_dump > stack_thunk_get_stack_bot() && sp_dump <= stack_thunk_get_stack_top())
|
||||
{
|
||||
// BearSSL we dump the BSSL second stack and then reset SP back to the main cont stack
|
||||
ets_printf_P(PSTR("\nctx: bearssl\nsp: %08x end: %08x offset: %04x\n"), sp_dump, stack_thunk_get_stack_top(), offset);
|
||||
print_stack(sp_dump + offset, stack_thunk_get_stack_top());
|
||||
offset = 0; // No offset needed anymore, the exception info was stored in the bssl stack
|
||||
sp_dump = stack_thunk_get_cont_sp();
|
||||
}
|
||||
|
||||
if (sp_dump > cont_stack_start && sp_dump < cont_stack_end)
|
||||
{
|
||||
ets_printf_P(PSTR("\nctx: cont\n"));
|
||||
stack_end = cont_stack_end;
|
||||
}
|
||||
else
|
||||
{
|
||||
ets_printf_P(PSTR("\nctx: sys\n"));
|
||||
stack_end = 0x3fffffb0;
|
||||
// it's actually 0x3ffffff0, but the stuff below ets_run
|
||||
// is likely not really relevant to the crash
|
||||
}
|
||||
|
||||
ets_printf_P(PSTR("sp: %08x end: %08x offset: %04x\n"), sp_dump, stack_end, offset);
|
||||
|
||||
print_stack(sp_dump + offset, stack_end);
|
||||
|
||||
ets_printf_P(PSTR("<<<stack<<<\n"));
|
||||
|
||||
// Use cap-X formatting to ensure the standard EspExceptionDecoder doesn't match the address
|
||||
if (umm_last_fail_alloc_addr)
|
||||
{
|
||||
ets_printf_P(PSTR("\nlast failed alloc call: %08X(%d)\n"), (uint32_t)umm_last_fail_alloc_addr, umm_last_fail_alloc_size);
|
||||
}
|
||||
|
||||
custom_crash_callback(&rst_info, sp_dump + offset, stack_end);
|
||||
|
||||
ets_delay_us(10000);
|
||||
__real_system_restart_local();
|
||||
}
|
||||
|
||||
|
||||
static void print_stack(uint32_t start, uint32_t end)
|
||||
{
|
||||
for (uint32_t pos = start; pos < end; pos += 0x10)
|
||||
{
|
||||
uint32_t* values = (uint32_t*)(pos);
|
||||
|
||||
// rough indicator: stack frames usually have SP saved as the second word
|
||||
bool looksLikeStackFrame = (values[2] == pos + 0x10);
|
||||
|
||||
ets_printf_P(PSTR("%08x: %08x %08x %08x %08x %c\n"),
|
||||
pos, values[0], values[1], values[2], values[3], (looksLikeStackFrame) ? '<' : ' ');
|
||||
}
|
||||
}
|
||||
|
||||
static void uart_write_char_d(char c)
|
||||
{
|
||||
uart0_write_char_d(c);
|
||||
uart1_write_char_d(c);
|
||||
}
|
||||
|
||||
static void uart0_write_char_d(char c)
|
||||
{
|
||||
while (((USS(0) >> USTXC) & 0xff)) { }
|
||||
|
||||
if (c == '\n')
|
||||
{
|
||||
USF(0) = '\r';
|
||||
}
|
||||
USF(0) = c;
|
||||
}
|
||||
|
||||
static void uart1_write_char_d(char c)
|
||||
{
|
||||
while (((USS(1) >> USTXC) & 0xff) >= 0x7e) { }
|
||||
|
||||
if (c == '\n')
|
||||
{
|
||||
USF(1) = '\r';
|
||||
}
|
||||
USF(1) = c;
|
||||
}
|
||||
|
||||
static void raise_exception()
|
||||
{
|
||||
__asm__ __volatile__("syscall");
|
||||
while (1); // never reached, needed to satisfy "noreturn" attribute
|
||||
}
|
||||
|
||||
void abort()
|
||||
{
|
||||
s_abort_called = true;
|
||||
if (gdb_present()) {
|
||||
/* When GDBStub is present, exceptions are handled by GDBStub,
|
||||
but Soft WDT will still call this function.
|
||||
Trigger an exception to break into GDB.
|
||||
TODO: check why gdb_do_break() or asm("break.n 0") do not
|
||||
break into GDB here. */
|
||||
raise_exception();
|
||||
}
|
||||
|
||||
void __unhandled_exception(const char *str)
|
||||
struct rst_info rst_info;
|
||||
memset(&rst_info, 0, sizeof(rst_info));
|
||||
system_rtc_mem_read(0, &rst_info, sizeof(rst_info));
|
||||
if (rst_info.reason != REASON_SOFT_WDT_RST &&
|
||||
rst_info.reason != REASON_EXCEPTION_RST &&
|
||||
rst_info.reason != REASON_WDT_RST)
|
||||
{
|
||||
s_unhandled_exception = str;
|
||||
raise_exception();
|
||||
return;
|
||||
}
|
||||
|
||||
void __assert_func(const char *file, int line, const char *func, const char *what)
|
||||
{
|
||||
s_panic_file = file;
|
||||
s_panic_line = line;
|
||||
s_panic_func = func;
|
||||
s_panic_what = what;
|
||||
gdb_do_break(); /* if GDB is not present, this is a no-op */
|
||||
raise_exception();
|
||||
// TODO: ets_install_putc1 definition is wrong in ets_sys.h, need cast
|
||||
ets_install_putc1((void *)&uart_write_char_d);
|
||||
|
||||
if (s_panic_line) {
|
||||
ets_printf_P(PSTR("\nPanic %S:%d %S"), s_panic_file, s_panic_line, s_panic_func);
|
||||
if (s_panic_what) {
|
||||
ets_printf_P(PSTR(": Assertion '%S' failed."), s_panic_what);
|
||||
}
|
||||
ets_putc('\n');
|
||||
}
|
||||
else if (s_unhandled_exception) {
|
||||
ets_printf_P(PSTR("\nUnhandled C++ exception: %S\n"), s_unhandled_exception);
|
||||
}
|
||||
else if (s_abort_called) {
|
||||
ets_printf_P(PSTR("\nAbort called\n"));
|
||||
}
|
||||
else if (rst_info.reason == REASON_EXCEPTION_RST) {
|
||||
ets_printf_P(PSTR("\nException (%d):\nepc1=0x%08x epc2=0x%08x epc3=0x%08x excvaddr=0x%08x depc=0x%08x\n"),
|
||||
rst_info.exccause, rst_info.epc1, rst_info.epc2, rst_info.epc3, rst_info.excvaddr, rst_info.depc);
|
||||
}
|
||||
else if (rst_info.reason == REASON_SOFT_WDT_RST) {
|
||||
ets_printf_P(PSTR("\nSoft WDT reset\n"));
|
||||
}
|
||||
|
||||
void __panic_func(const char* file, int line, const char* func)
|
||||
{
|
||||
s_panic_file = file;
|
||||
s_panic_line = line;
|
||||
s_panic_func = func;
|
||||
s_panic_what = 0;
|
||||
gdb_do_break(); /* if GDB is not present, this is a no-op */
|
||||
raise_exception();
|
||||
uint32_t cont_stack_start = (uint32_t) &(g_pcont->stack);
|
||||
uint32_t cont_stack_end = (uint32_t) g_pcont->stack_end;
|
||||
uint32_t stack_end;
|
||||
|
||||
// amount of stack taken by interrupt or exception handler
|
||||
// and everything up to __wrap_system_restart_local
|
||||
// (determined empirically, might break)
|
||||
uint32_t offset = 0;
|
||||
if (rst_info.reason == REASON_SOFT_WDT_RST) {
|
||||
offset = 0x1b0;
|
||||
}
|
||||
else if (rst_info.reason == REASON_EXCEPTION_RST) {
|
||||
offset = 0x1a0;
|
||||
}
|
||||
else if (rst_info.reason == REASON_WDT_RST) {
|
||||
offset = 0x10;
|
||||
}
|
||||
|
||||
ets_printf_P(PSTR("\n>>>stack>>>\n"));
|
||||
|
||||
if (sp_dump > stack_thunk_get_stack_bot() && sp_dump <= stack_thunk_get_stack_top()) {
|
||||
// BearSSL we dump the BSSL second stack and then reset SP back to the main cont stack
|
||||
ets_printf_P(PSTR("\nctx: bearssl\nsp: %08x end: %08x offset: %04x\n"), sp_dump, stack_thunk_get_stack_top(), offset);
|
||||
print_stack(sp_dump + offset, stack_thunk_get_stack_top());
|
||||
offset = 0; // No offset needed anymore, the exception info was stored in the bssl stack
|
||||
sp_dump = stack_thunk_get_cont_sp();
|
||||
}
|
||||
|
||||
if (sp_dump > cont_stack_start && sp_dump < cont_stack_end) {
|
||||
ets_printf_P(PSTR("\nctx: cont\n"));
|
||||
stack_end = cont_stack_end;
|
||||
}
|
||||
else {
|
||||
ets_printf_P(PSTR("\nctx: sys\n"));
|
||||
stack_end = 0x3fffffb0;
|
||||
// it's actually 0x3ffffff0, but the stuff below ets_run
|
||||
// is likely not really relevant to the crash
|
||||
}
|
||||
|
||||
ets_printf_P(PSTR("sp: %08x end: %08x offset: %04x\n"), sp_dump, stack_end, offset);
|
||||
|
||||
print_stack(sp_dump + offset, stack_end);
|
||||
|
||||
ets_printf_P(PSTR("<<<stack<<<\n"));
|
||||
|
||||
// Use cap-X formatting to ensure the standard EspExceptionDecoder doesn't match the address
|
||||
if (umm_last_fail_alloc_addr) {
|
||||
ets_printf_P(PSTR("\nlast failed alloc call: %08X(%d)\n"), (uint32_t)umm_last_fail_alloc_addr, umm_last_fail_alloc_size);
|
||||
}
|
||||
|
||||
custom_crash_callback( &rst_info, sp_dump + offset, stack_end );
|
||||
|
||||
ets_delay_us(10000);
|
||||
__real_system_restart_local();
|
||||
}
|
||||
|
||||
|
||||
static void print_stack(uint32_t start, uint32_t end) {
|
||||
for (uint32_t pos = start; pos < end; pos += 0x10) {
|
||||
uint32_t* values = (uint32_t*)(pos);
|
||||
|
||||
// rough indicator: stack frames usually have SP saved as the second word
|
||||
bool looksLikeStackFrame = (values[2] == pos + 0x10);
|
||||
|
||||
ets_printf_P(PSTR("%08x: %08x %08x %08x %08x %c\n"),
|
||||
pos, values[0], values[1], values[2], values[3], (looksLikeStackFrame)?'<':' ');
|
||||
}
|
||||
}
|
||||
|
||||
static void uart_write_char_d(char c) {
|
||||
uart0_write_char_d(c);
|
||||
uart1_write_char_d(c);
|
||||
}
|
||||
|
||||
static void uart0_write_char_d(char c) {
|
||||
while (((USS(0) >> USTXC) & 0xff)) { }
|
||||
|
||||
if (c == '\n') {
|
||||
USF(0) = '\r';
|
||||
}
|
||||
USF(0) = c;
|
||||
}
|
||||
|
||||
static void uart1_write_char_d(char c) {
|
||||
while (((USS(1) >> USTXC) & 0xff) >= 0x7e) { }
|
||||
|
||||
if (c == '\n') {
|
||||
USF(1) = '\r';
|
||||
}
|
||||
USF(1) = c;
|
||||
}
|
||||
|
||||
static void raise_exception() {
|
||||
__asm__ __volatile__ ("syscall");
|
||||
while (1); // never reached, needed to satisfy "noreturn" attribute
|
||||
}
|
||||
|
||||
void abort() {
|
||||
s_abort_called = true;
|
||||
raise_exception();
|
||||
}
|
||||
|
||||
void __unhandled_exception(const char *str) {
|
||||
s_unhandled_exception = str;
|
||||
raise_exception();
|
||||
}
|
||||
|
||||
void __assert_func(const char *file, int line, const char *func, const char *what) {
|
||||
s_panic_file = file;
|
||||
s_panic_line = line;
|
||||
s_panic_func = func;
|
||||
s_panic_what = what;
|
||||
gdb_do_break(); /* if GDB is not present, this is a no-op */
|
||||
raise_exception();
|
||||
}
|
||||
|
||||
void __panic_func(const char* file, int line, const char* func) {
|
||||
s_panic_file = file;
|
||||
s_panic_line = line;
|
||||
s_panic_func = func;
|
||||
s_panic_what = 0;
|
||||
gdb_do_break(); /* if GDB is not present, this is a no-op */
|
||||
raise_exception();
|
||||
}
|
||||
|
||||
};
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,184 +1,178 @@
|
||||
/*
|
||||
core_esp8266_sigma_delta.c - sigma delta library for esp8266
|
||||
core_esp8266_sigma_delta.c - sigma delta library for esp8266
|
||||
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2014 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
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
|
||||
*/
|
||||
|
||||
#include "Arduino.h" // using pinMode
|
||||
|
||||
extern "C" {
|
||||
|
||||
// definitions in esp8266_peri.h style
|
||||
// definitions in esp8266_peri.h style
|
||||
#define GPSD ESP8266_REG(0x368) // GPIO_SIGMA_DELTA register @ 0x600000368
|
||||
#define GPSDT 0 // target, 8 bits
|
||||
#define GPSDP 8 // prescaler, 8 bits
|
||||
#define GPSDE 16 // enable
|
||||
|
||||
void sigmaDeltaSetPrescaler(uint8_t prescaler); // avoids compiler warning
|
||||
void sigmaDeltaSetPrescaler(uint8_t prescaler); // avoids compiler warning
|
||||
|
||||
/******************************************************************************
|
||||
FunctionName : sigmaDeltaEnable
|
||||
Description : enable the internal sigma delta source
|
||||
Parameters : none
|
||||
Returns : none
|
||||
*******************************************************************************/
|
||||
void ICACHE_FLASH_ATTR sigmaDeltaEnable()
|
||||
{
|
||||
GPSD = (0 << GPSDT) | (0 << GPSDP) | (1 << GPSDE); //SIGMA_DELTA_TARGET(0) | SIGMA_DELTA_PRESCALER(0) | SIGMA_DELTA_ENABLE(ENABLED)
|
||||
}
|
||||
/******************************************************************************
|
||||
* FunctionName : sigmaDeltaEnable
|
||||
* Description : enable the internal sigma delta source
|
||||
* Parameters : none
|
||||
* Returns : none
|
||||
*******************************************************************************/
|
||||
void ICACHE_FLASH_ATTR sigmaDeltaEnable()
|
||||
{
|
||||
GPSD = (0 << GPSDT) | (0 << GPSDP) | (1 << GPSDE); //SIGMA_DELTA_TARGET(0) | SIGMA_DELTA_PRESCALER(0) | SIGMA_DELTA_ENABLE(ENABLED)
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
FunctionName : sigmaDeltaDisable
|
||||
Description : stop the internal sigma delta source
|
||||
Parameters : none
|
||||
Returns : none
|
||||
*******************************************************************************/
|
||||
void ICACHE_FLASH_ATTR sigmaDeltaDisable()
|
||||
{
|
||||
GPSD = (0 << GPSDT) | (0 << GPSDP) | (0 << GPSDE); //SIGMA_DELTA_TARGET(0) | SIGMA_DELTA_PRESCALER(0) | SIGMA_DELTA_ENABLE(DISABLED)
|
||||
}
|
||||
/******************************************************************************
|
||||
* FunctionName : sigmaDeltaDisable
|
||||
* Description : stop the internal sigma delta source
|
||||
* Parameters : none
|
||||
* Returns : none
|
||||
*******************************************************************************/
|
||||
void ICACHE_FLASH_ATTR sigmaDeltaDisable()
|
||||
{
|
||||
GPSD = (0 << GPSDT) | (0 << GPSDP) | (0 << GPSDE); //SIGMA_DELTA_TARGET(0) | SIGMA_DELTA_PRESCALER(0) | SIGMA_DELTA_ENABLE(DISABLED)
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
FunctionName : sigmaDeltaAttachPin
|
||||
Description : connects the sigma delta source to a physical output pin
|
||||
Parameters : pin (0..15), channel = unused, for compatibility with ESP32
|
||||
Returns : none
|
||||
*******************************************************************************/
|
||||
void ICACHE_FLASH_ATTR sigmaDeltaAttachPin(uint8_t pin, uint8_t channel)
|
||||
{
|
||||
(void) channel;
|
||||
// make the chosen pin an output pin
|
||||
pinMode(pin, OUTPUT);
|
||||
if (pin < 16)
|
||||
{
|
||||
// set its source to the sigma delta source
|
||||
GPC(pin) |= (1 << GPCS); //SOURCE 0:GPIO_DATA,1:SigmaDelta
|
||||
}
|
||||
}
|
||||
/******************************************************************************
|
||||
* FunctionName : sigmaDeltaAttachPin
|
||||
* Description : connects the sigma delta source to a physical output pin
|
||||
* Parameters : pin (0..15), channel = unused, for compatibility with ESP32
|
||||
* Returns : none
|
||||
*******************************************************************************/
|
||||
void ICACHE_FLASH_ATTR sigmaDeltaAttachPin(uint8_t pin, uint8_t channel)
|
||||
{
|
||||
(void) channel;
|
||||
// make the chosen pin an output pin
|
||||
pinMode (pin, OUTPUT);
|
||||
if (pin < 16) {
|
||||
// set its source to the sigma delta source
|
||||
GPC(pin) |= (1 << GPCS); //SOURCE 0:GPIO_DATA,1:SigmaDelta
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
FunctionName : sigmaDeltaDetachPin
|
||||
Description : disconnects the sigma delta source from a physical output pin
|
||||
Parameters : pin (0..16)
|
||||
Returns : none
|
||||
*******************************************************************************/
|
||||
void ICACHE_FLASH_ATTR sigmaDeltaDetachPin(uint8_t pin)
|
||||
{
|
||||
if (pin < 16)
|
||||
{
|
||||
// set its source to the sigma delta source
|
||||
GPC(pin) &= ~(1 << GPCS); //SOURCE 0:GPIO_DATA,1:SigmaDelta
|
||||
}
|
||||
}
|
||||
/******************************************************************************
|
||||
* FunctionName : sigmaDeltaDetachPin
|
||||
* Description : disconnects the sigma delta source from a physical output pin
|
||||
* Parameters : pin (0..16)
|
||||
* Returns : none
|
||||
*******************************************************************************/
|
||||
void ICACHE_FLASH_ATTR sigmaDeltaDetachPin(uint8_t pin)
|
||||
{
|
||||
if (pin < 16) {
|
||||
// set its source to the sigma delta source
|
||||
GPC(pin) &= ~(1 << GPCS); //SOURCE 0:GPIO_DATA,1:SigmaDelta
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
FunctionName : sigmaDeltaIsPinAttached
|
||||
Description : query if pin is attached
|
||||
Parameters : pin (0..16)
|
||||
Returns : bool
|
||||
*******************************************************************************/
|
||||
bool ICACHE_FLASH_ATTR sigmaDeltaIsPinAttached(uint8_t pin)
|
||||
{
|
||||
if (pin < 16)
|
||||
{
|
||||
// set its source to the sigma delta source
|
||||
return (GPC(pin) & (1 << GPCS)); //SOURCE 0:GPIO_DATA,1:SigmaDelta
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/******************************************************************************
|
||||
* FunctionName : sigmaDeltaIsPinAttached
|
||||
* Description : query if pin is attached
|
||||
* Parameters : pin (0..16)
|
||||
* Returns : bool
|
||||
*******************************************************************************/
|
||||
bool ICACHE_FLASH_ATTR sigmaDeltaIsPinAttached(uint8_t pin)
|
||||
{
|
||||
if (pin < 16) {
|
||||
// set its source to the sigma delta source
|
||||
return (GPC(pin) & (1 << GPCS)); //SOURCE 0:GPIO_DATA,1:SigmaDelta
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
FunctionName : sigmaDeltaSetup
|
||||
Description : start the sigma delta generator with the chosen parameters
|
||||
Parameters : channel = unused (for compatibility with ESP32),
|
||||
freq : 1220-312500 (lowest frequency in the output signal's spectrum)
|
||||
Returns : uint32_t the actual frequency, closest to the input parameter
|
||||
*******************************************************************************/
|
||||
uint32_t ICACHE_FLASH_ATTR sigmaDeltaSetup(uint8_t channel, uint32_t freq)
|
||||
{
|
||||
(void) channel;
|
||||
/******************************************************************************
|
||||
* FunctionName : sigmaDeltaSetup
|
||||
* Description : start the sigma delta generator with the chosen parameters
|
||||
* Parameters : channel = unused (for compatibility with ESP32),
|
||||
* freq : 1220-312500 (lowest frequency in the output signal's spectrum)
|
||||
* Returns : uint32_t the actual frequency, closest to the input parameter
|
||||
*******************************************************************************/
|
||||
uint32_t ICACHE_FLASH_ATTR sigmaDeltaSetup(uint8_t channel, uint32_t freq)
|
||||
{
|
||||
(void) channel;
|
||||
|
||||
uint32_t prescaler = ((uint32_t)10000000 / (freq * 32)) - 1;
|
||||
uint32_t prescaler = ((uint32_t)10000000/(freq*32)) - 1;
|
||||
|
||||
if (prescaler > 0xFF)
|
||||
{
|
||||
prescaler = 0xFF;
|
||||
}
|
||||
sigmaDeltaEnable();
|
||||
sigmaDeltaSetPrescaler((uint8_t) prescaler);
|
||||
if(prescaler > 0xFF) {
|
||||
prescaler = 0xFF;
|
||||
}
|
||||
sigmaDeltaEnable();
|
||||
sigmaDeltaSetPrescaler ((uint8_t) prescaler);
|
||||
|
||||
return 10000000 / ((prescaler + 1) * 32);
|
||||
}
|
||||
return 10000000/((prescaler + 1) * 32);
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
FunctionName : sigmaDeltaWrite
|
||||
Description : set the duty cycle for the sigma-delta source
|
||||
Parameters : uint8 duty, 0-255, duty cycle = target/256,
|
||||
channel = unused, for compatibility with ESP32
|
||||
Returns : none
|
||||
*******************************************************************************/
|
||||
void ICACHE_FLASH_ATTR sigmaDeltaWrite(uint8_t channel, uint8_t duty)
|
||||
{
|
||||
uint32_t reg = GPSD;
|
||||
(void) channel;
|
||||
/******************************************************************************
|
||||
* FunctionName : sigmaDeltaWrite
|
||||
* Description : set the duty cycle for the sigma-delta source
|
||||
* Parameters : uint8 duty, 0-255, duty cycle = target/256,
|
||||
* channel = unused, for compatibility with ESP32
|
||||
* Returns : none
|
||||
*******************************************************************************/
|
||||
void ICACHE_FLASH_ATTR sigmaDeltaWrite(uint8_t channel, uint8_t duty)
|
||||
{
|
||||
uint32_t reg = GPSD;
|
||||
(void) channel;
|
||||
|
||||
reg = (reg & ~(0xFF << GPSDT)) | ((duty & 0xFF) << GPSDT);
|
||||
GPSD = reg;
|
||||
reg = (reg & ~(0xFF << GPSDT)) | ((duty & 0xFF) << GPSDT);
|
||||
GPSD = reg;
|
||||
|
||||
}
|
||||
/******************************************************************************
|
||||
FunctionName : sigmaDeltaRead
|
||||
Description : get the duty cycle for the sigma-delta source
|
||||
Parameters : channel = unused, for compatibility with ESP32
|
||||
Returns : uint8_t duty cycle value 0..255
|
||||
*******************************************************************************/
|
||||
uint8_t ICACHE_FLASH_ATTR sigmaDeltaRead(uint8_t channel)
|
||||
{
|
||||
(void) channel;
|
||||
return (uint8_t)((GPSD >> GPSDT) & 0xFF);
|
||||
}
|
||||
}
|
||||
/******************************************************************************
|
||||
* FunctionName : sigmaDeltaRead
|
||||
* Description : get the duty cycle for the sigma-delta source
|
||||
* Parameters : channel = unused, for compatibility with ESP32
|
||||
* Returns : uint8_t duty cycle value 0..255
|
||||
*******************************************************************************/
|
||||
uint8_t ICACHE_FLASH_ATTR sigmaDeltaRead(uint8_t channel)
|
||||
{
|
||||
(void) channel;
|
||||
return (uint8_t)((GPSD >> GPSDT) & 0xFF);
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
FunctionName : sigmaDeltaSetPrescaler
|
||||
Description : set the clock divider for the sigma-delta source
|
||||
Parameters : uint8 prescaler, 0-255, divides the 80MHz base clock by this amount
|
||||
Returns : none
|
||||
*******************************************************************************/
|
||||
void ICACHE_FLASH_ATTR sigmaDeltaSetPrescaler(uint8_t prescaler)
|
||||
{
|
||||
uint32_t reg = GPSD;
|
||||
/******************************************************************************
|
||||
* FunctionName : sigmaDeltaSetPrescaler
|
||||
* Description : set the clock divider for the sigma-delta source
|
||||
* Parameters : uint8 prescaler, 0-255, divides the 80MHz base clock by this amount
|
||||
* Returns : none
|
||||
*******************************************************************************/
|
||||
void ICACHE_FLASH_ATTR sigmaDeltaSetPrescaler(uint8_t prescaler)
|
||||
{
|
||||
uint32_t reg = GPSD;
|
||||
|
||||
reg = (reg & ~(0xFF << GPSDP)) | ((prescaler & 0xFF) << GPSDP);
|
||||
GPSD = reg;
|
||||
}
|
||||
reg = (reg & ~(0xFF << GPSDP)) | ((prescaler & 0xFF) << GPSDP);
|
||||
GPSD = reg;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
FunctionName : sigmaDeltaGetPrescaler
|
||||
Description : get the prescaler value from the GPIO_SIGMA_DELTA register
|
||||
Parameters : none
|
||||
Returns : uint8 prescaler, CLK_DIV , 0-255
|
||||
*******************************************************************************/
|
||||
uint8_t ICACHE_FLASH_ATTR sigmaDeltaGetPrescaler(void)
|
||||
{
|
||||
return (uint8_t)((GPSD >> GPSDP) & 0xFF);
|
||||
}
|
||||
/******************************************************************************
|
||||
* FunctionName : sigmaDeltaGetPrescaler
|
||||
* Description : get the prescaler value from the GPIO_SIGMA_DELTA register
|
||||
* Parameters : none
|
||||
* Returns : uint8 prescaler, CLK_DIV , 0-255
|
||||
*******************************************************************************/
|
||||
uint8_t ICACHE_FLASH_ATTR sigmaDeltaGetPrescaler(void)
|
||||
{
|
||||
return (uint8_t)((GPSD >> GPSDP) & 0xFF);
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
timer.c - Timer1 library for esp8266
|
||||
timer.c - Timer1 library for esp8266
|
||||
|
||||
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2015 Hristo Gochkov. 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 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.
|
||||
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
|
||||
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
|
||||
*/
|
||||
#include "wiring_private.h"
|
||||
#include "pins_arduino.h"
|
||||
@ -26,101 +26,82 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
// ------------------------------------------------------------------ -
|
||||
// timer 1
|
||||
// ------------------------------------------------------------------ -
|
||||
// timer 1
|
||||
|
||||
static volatile timercallback timer1_user_cb = NULL;
|
||||
static volatile timercallback timer1_user_cb = NULL;
|
||||
|
||||
void ICACHE_RAM_ATTR timer1_isr_handler(void *para)
|
||||
{
|
||||
(void) para;
|
||||
if ((T1C & ((1 << TCAR) | (1 << TCIT))) == 0)
|
||||
{
|
||||
TEIE &= ~TEIE1; //edge int disable
|
||||
}
|
||||
T1I = 0;
|
||||
if (timer1_user_cb)
|
||||
{
|
||||
// to make ISR compatible to Arduino AVR model where interrupts are disabled
|
||||
// we disable them before we call the client ISR
|
||||
uint32_t savedPS = xt_rsil(15); // stop other interrupts
|
||||
timer1_user_cb();
|
||||
xt_wsr_ps(savedPS);
|
||||
}
|
||||
void ICACHE_RAM_ATTR timer1_isr_handler(void *para){
|
||||
(void) para;
|
||||
if ((T1C & ((1 << TCAR) | (1 << TCIT))) == 0) TEIE &= ~TEIE1;//edge int disable
|
||||
T1I = 0;
|
||||
if (timer1_user_cb) {
|
||||
// to make ISR compatible to Arduino AVR model where interrupts are disabled
|
||||
// we disable them before we call the client ISR
|
||||
uint32_t savedPS = xt_rsil(15); // stop other interrupts
|
||||
timer1_user_cb();
|
||||
xt_wsr_ps(savedPS);
|
||||
}
|
||||
}
|
||||
|
||||
void ICACHE_RAM_ATTR timer1_isr_init()
|
||||
{
|
||||
ETS_FRC_TIMER1_INTR_ATTACH(timer1_isr_handler, NULL);
|
||||
void ICACHE_RAM_ATTR timer1_isr_init(){
|
||||
ETS_FRC_TIMER1_INTR_ATTACH(timer1_isr_handler, NULL);
|
||||
}
|
||||
|
||||
void timer1_attachInterrupt(timercallback userFunc) {
|
||||
timer1_user_cb = userFunc;
|
||||
ETS_FRC1_INTR_ENABLE();
|
||||
}
|
||||
|
||||
void ICACHE_RAM_ATTR timer1_detachInterrupt() {
|
||||
timer1_user_cb = 0;
|
||||
TEIE &= ~TEIE1;//edge int disable
|
||||
ETS_FRC1_INTR_DISABLE();
|
||||
}
|
||||
|
||||
void timer1_enable(uint8_t divider, uint8_t int_type, uint8_t reload){
|
||||
T1C = (1 << TCTE) | ((divider & 3) << TCPD) | ((int_type & 1) << TCIT) | ((reload & 1) << TCAR);
|
||||
T1I = 0;
|
||||
}
|
||||
|
||||
void ICACHE_RAM_ATTR timer1_write(uint32_t ticks){
|
||||
T1L = ((ticks)& 0x7FFFFF);
|
||||
if ((T1C & (1 << TCIT)) == 0) TEIE |= TEIE1;//edge int enable
|
||||
}
|
||||
|
||||
void ICACHE_RAM_ATTR timer1_disable(){
|
||||
T1C = 0;
|
||||
T1I = 0;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// timer 0
|
||||
|
||||
static volatile timercallback timer0_user_cb = NULL;
|
||||
|
||||
void ICACHE_RAM_ATTR timer0_isr_handler(void* para){
|
||||
(void) para;
|
||||
if (timer0_user_cb) {
|
||||
// to make ISR compatible to Arduino AVR model where interrupts are disabled
|
||||
// we disable them before we call the client ISR
|
||||
uint32_t savedPS = xt_rsil(15); // stop other interrupts
|
||||
timer0_user_cb();
|
||||
xt_wsr_ps(savedPS);
|
||||
}
|
||||
}
|
||||
|
||||
void timer1_attachInterrupt(timercallback userFunc)
|
||||
{
|
||||
timer1_user_cb = userFunc;
|
||||
ETS_FRC1_INTR_ENABLE();
|
||||
}
|
||||
void timer0_isr_init(){
|
||||
ETS_CCOMPARE0_INTR_ATTACH(timer0_isr_handler, NULL);
|
||||
}
|
||||
|
||||
void ICACHE_RAM_ATTR timer1_detachInterrupt()
|
||||
{
|
||||
timer1_user_cb = 0;
|
||||
TEIE &= ~TEIE1;//edge int disable
|
||||
ETS_FRC1_INTR_DISABLE();
|
||||
}
|
||||
void timer0_attachInterrupt(timercallback userFunc) {
|
||||
timer0_user_cb = userFunc;
|
||||
ETS_CCOMPARE0_ENABLE();
|
||||
}
|
||||
|
||||
void timer1_enable(uint8_t divider, uint8_t int_type, uint8_t reload)
|
||||
{
|
||||
T1C = (1 << TCTE) | ((divider & 3) << TCPD) | ((int_type & 1) << TCIT) | ((reload & 1) << TCAR);
|
||||
T1I = 0;
|
||||
}
|
||||
|
||||
void ICACHE_RAM_ATTR timer1_write(uint32_t ticks)
|
||||
{
|
||||
T1L = ((ticks) & 0x7FFFFF);
|
||||
if ((T1C & (1 << TCIT)) == 0)
|
||||
{
|
||||
TEIE |= TEIE1; //edge int enable
|
||||
}
|
||||
}
|
||||
|
||||
void ICACHE_RAM_ATTR timer1_disable()
|
||||
{
|
||||
T1C = 0;
|
||||
T1I = 0;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// timer 0
|
||||
|
||||
static volatile timercallback timer0_user_cb = NULL;
|
||||
|
||||
void ICACHE_RAM_ATTR timer0_isr_handler(void* para)
|
||||
{
|
||||
(void) para;
|
||||
if (timer0_user_cb)
|
||||
{
|
||||
// to make ISR compatible to Arduino AVR model where interrupts are disabled
|
||||
// we disable them before we call the client ISR
|
||||
uint32_t savedPS = xt_rsil(15); // stop other interrupts
|
||||
timer0_user_cb();
|
||||
xt_wsr_ps(savedPS);
|
||||
}
|
||||
}
|
||||
|
||||
void timer0_isr_init()
|
||||
{
|
||||
ETS_CCOMPARE0_INTR_ATTACH(timer0_isr_handler, NULL);
|
||||
}
|
||||
|
||||
void timer0_attachInterrupt(timercallback userFunc)
|
||||
{
|
||||
timer0_user_cb = userFunc;
|
||||
ETS_CCOMPARE0_ENABLE();
|
||||
}
|
||||
|
||||
void ICACHE_RAM_ATTR timer0_detachInterrupt()
|
||||
{
|
||||
timer0_user_cb = NULL;
|
||||
ETS_CCOMPARE0_DISABLE();
|
||||
}
|
||||
void ICACHE_RAM_ATTR timer0_detachInterrupt() {
|
||||
timer0_user_cb = NULL;
|
||||
ETS_CCOMPARE0_DISABLE();
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -1,22 +1,22 @@
|
||||
|
||||
/*
|
||||
core_esp8266_version.h - parse "git describe" at compile time
|
||||
Copyright (c) 2018 david gauchard. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
core_esp8266_version.h - parse "git describe" at compile time
|
||||
Copyright (c) 2018 david gauchard. 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 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.
|
||||
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
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef __CORE_ESP8266_VERSION_H
|
||||
@ -33,152 +33,148 @@
|
||||
extern "C++"
|
||||
{
|
||||
|
||||
// Following constexpr functions are compiled and executed
|
||||
// *after* pre-processing and *during* compilation
|
||||
//
|
||||
// Their result is treated like a numeric constant in final binary code.
|
||||
// git tags must be in the form:
|
||||
// - <major>.<minor>.<revision> (2.4.2) (2.5.0)
|
||||
// - <major>.<minor>.<revision>-rc<rc> (2.5.0-rc1) (2.5.0-rc2)
|
||||
//
|
||||
// "git describe" = ARDUINO_ESP8266_GIT_DESC will thus be in the form:
|
||||
// - <tag> (2.4.2) (2.5.0)
|
||||
// - <tag>-<numberOfCommits>-g<git-hash> (2.4.2-91-gcb05b86d) (2.5.0-rc3-1-gcb05b86d)
|
||||
//
|
||||
// Examples:
|
||||
// case 2.4.2 (fresh version/tag)
|
||||
// esp8266CoreVersionSubRevision() is 0 Numeric is: 20402000
|
||||
// case 2.4.2-91-gcb05b86d:
|
||||
// esp8266CoreVersionSubRevision() is -91 Numeric is: 20402091
|
||||
// case 2.5.0-rc3-1-gcb05b86d:
|
||||
// esp8266CoreVersionSubRevision() is 3 Numeric is: 20499903
|
||||
// case 2.5.0:
|
||||
// esp8266CoreVersionSubRevision() is 0 Numeric is: 20500000
|
||||
//
|
||||
// Using esp8266::coreVersionNumeric() in a portable way:
|
||||
//
|
||||
// #if HAS_ESP8266_VERSION_NUMERIC
|
||||
// if (esp8266::coreVersionNumeric() >= 20500042)
|
||||
// {
|
||||
// // modern api can be used
|
||||
// }
|
||||
// else
|
||||
// #endif
|
||||
// {
|
||||
// // code using older api
|
||||
// // (will not be compiled in when newer api is usable)
|
||||
// }
|
||||
// Following constexpr functions are compiled and executed
|
||||
// *after* pre-processing and *during* compilation
|
||||
//
|
||||
// Their result is treated like a numeric constant in final binary code.
|
||||
// git tags must be in the form:
|
||||
// - <major>.<minor>.<revision> (2.4.2) (2.5.0)
|
||||
// - <major>.<minor>.<revision>-rc<rc> (2.5.0-rc1) (2.5.0-rc2)
|
||||
//
|
||||
// "git describe" = ARDUINO_ESP8266_GIT_DESC will thus be in the form:
|
||||
// - <tag> (2.4.2) (2.5.0)
|
||||
// - <tag>-<numberOfCommits>-g<git-hash> (2.4.2-91-gcb05b86d) (2.5.0-rc3-1-gcb05b86d)
|
||||
//
|
||||
// Examples:
|
||||
// case 2.4.2 (fresh version/tag)
|
||||
// esp8266CoreVersionSubRevision() is 0 Numeric is: 20402000
|
||||
// case 2.4.2-91-gcb05b86d:
|
||||
// esp8266CoreVersionSubRevision() is -91 Numeric is: 20402091
|
||||
// case 2.5.0-rc3-1-gcb05b86d:
|
||||
// esp8266CoreVersionSubRevision() is 3 Numeric is: 20499903
|
||||
// case 2.5.0:
|
||||
// esp8266CoreVersionSubRevision() is 0 Numeric is: 20500000
|
||||
//
|
||||
// Using esp8266::coreVersionNumeric() in a portable way:
|
||||
//
|
||||
// #if HAS_ESP8266_VERSION_NUMERIC
|
||||
// if (esp8266::coreVersionNumeric() >= 20500042)
|
||||
// {
|
||||
// // modern api can be used
|
||||
// }
|
||||
// else
|
||||
// #endif
|
||||
// {
|
||||
// // code using older api
|
||||
// // (will not be compiled in when newer api is usable)
|
||||
// }
|
||||
|
||||
namespace conststr {
|
||||
namespace conststr {
|
||||
|
||||
constexpr
|
||||
bool isDecimal(const char c)
|
||||
{
|
||||
return c >= '0' && c <= '9';
|
||||
}
|
||||
constexpr
|
||||
bool isDecimal (const char c)
|
||||
{
|
||||
return c >= '0' && c <= '9';
|
||||
}
|
||||
|
||||
template<unsigned N> constexpr
|
||||
bool isMinus(const char (&arr) [N], unsigned i)
|
||||
{
|
||||
return arr[i] == '-' && isDecimal(arr[i + 1]);
|
||||
}
|
||||
template<unsigned N> constexpr
|
||||
bool isMinus (const char (&arr) [N], unsigned i)
|
||||
{
|
||||
return arr[i] == '-' && isDecimal(arr[i+1]);
|
||||
}
|
||||
|
||||
template<unsigned N> constexpr
|
||||
int atoi(const char (&arr) [N], unsigned i)
|
||||
{
|
||||
return ( // <= c++11 requires a "return statement"
|
||||
template<unsigned N> constexpr
|
||||
int atoi (const char (&arr) [N], unsigned i)
|
||||
{
|
||||
return ({ // <= c++11 requires a "return statement"
|
||||
int ret = 0;
|
||||
int sign = 1;
|
||||
if (arr[i] == '-')
|
||||
{
|
||||
int ret = 0;
|
||||
int sign = 1;
|
||||
if (arr[i] == '-')
|
||||
{
|
||||
sign = -1;
|
||||
sign = -1;
|
||||
i++;
|
||||
}
|
||||
while (isDecimal(arr[i]))
|
||||
ret = 10*ret + arr[i++] - '0';
|
||||
ret * sign;
|
||||
});
|
||||
}
|
||||
|
||||
template<unsigned N> constexpr
|
||||
int parseNthInteger (const char (&arr) [N], unsigned f)
|
||||
{
|
||||
return ({ // <= c++11 requires a "return statement"
|
||||
unsigned i = 0;
|
||||
while (f && arr[i])
|
||||
{
|
||||
if (isMinus(arr, i))
|
||||
i++;
|
||||
}
|
||||
while (isDecimal(arr[i]))
|
||||
ret = 10 * ret + arr[i++] - '0';
|
||||
ret * sign;
|
||||
});
|
||||
}
|
||||
for (; isDecimal(arr[i]); i++);
|
||||
f--;
|
||||
for (; arr[i] && !isMinus(arr, i) && !isDecimal(arr[i]); i++);
|
||||
}
|
||||
atoi(arr, i);
|
||||
});
|
||||
}
|
||||
|
||||
template<unsigned N> constexpr
|
||||
int parseNthInteger(const char (&arr) [N], unsigned f)
|
||||
{
|
||||
return ( // <= c++11 requires a "return statement"
|
||||
{
|
||||
unsigned i = 0;
|
||||
while (f && arr[i])
|
||||
{
|
||||
if (isMinus(arr, i))
|
||||
{
|
||||
i++;
|
||||
}
|
||||
for (; isDecimal(arr[i]); i++);
|
||||
f--;
|
||||
for (; arr[i] && !isMinus(arr, i) && !isDecimal(arr[i]); i++);
|
||||
}
|
||||
atoi(arr, i);
|
||||
});
|
||||
}
|
||||
}; // namespace conststr
|
||||
|
||||
}; // namespace conststr
|
||||
namespace esp8266 {
|
||||
|
||||
namespace esp8266 {
|
||||
/*
|
||||
* version major
|
||||
*/
|
||||
constexpr
|
||||
int coreVersionMajor ()
|
||||
{
|
||||
return conststr::parseNthInteger(__STR(ARDUINO_ESP8266_GIT_DESC), 0);
|
||||
}
|
||||
|
||||
/*
|
||||
version major
|
||||
*/
|
||||
constexpr
|
||||
int coreVersionMajor()
|
||||
{
|
||||
return conststr::parseNthInteger(__STR(ARDUINO_ESP8266_GIT_DESC), 0);
|
||||
}
|
||||
/*
|
||||
* version minor
|
||||
*/
|
||||
constexpr
|
||||
int coreVersionMinor ()
|
||||
{
|
||||
return conststr::parseNthInteger(__STR(ARDUINO_ESP8266_GIT_DESC), 1);
|
||||
}
|
||||
|
||||
/*
|
||||
version minor
|
||||
*/
|
||||
constexpr
|
||||
int coreVersionMinor()
|
||||
{
|
||||
return conststr::parseNthInteger(__STR(ARDUINO_ESP8266_GIT_DESC), 1);
|
||||
}
|
||||
/*
|
||||
* version revision
|
||||
*/
|
||||
constexpr
|
||||
int coreVersionRevision ()
|
||||
{
|
||||
return conststr::parseNthInteger(__STR(ARDUINO_ESP8266_GIT_DESC), 2);
|
||||
}
|
||||
|
||||
/*
|
||||
version revision
|
||||
*/
|
||||
constexpr
|
||||
int coreVersionRevision()
|
||||
{
|
||||
return conststr::parseNthInteger(__STR(ARDUINO_ESP8266_GIT_DESC), 2);
|
||||
}
|
||||
/*
|
||||
* git commit number since last tag (negative)
|
||||
* or RC-number (positive)
|
||||
*/
|
||||
constexpr
|
||||
int coreVersionSubRevision ()
|
||||
{
|
||||
return conststr::parseNthInteger(__STR(ARDUINO_ESP8266_GIT_DESC), 3);
|
||||
}
|
||||
|
||||
/*
|
||||
git commit number since last tag (negative)
|
||||
or RC-number (positive)
|
||||
*/
|
||||
constexpr
|
||||
int coreVersionSubRevision()
|
||||
{
|
||||
return conststr::parseNthInteger(__STR(ARDUINO_ESP8266_GIT_DESC), 3);
|
||||
}
|
||||
/*
|
||||
* unique revision indentifier (never decreases)
|
||||
*/
|
||||
constexpr
|
||||
int coreVersionNumeric ()
|
||||
{
|
||||
return coreVersionMajor() * 10000000
|
||||
+ coreVersionMinor() * 100000
|
||||
+ coreVersionRevision() * 1000
|
||||
+ (coreVersionSubRevision() < 0 ?
|
||||
-coreVersionSubRevision() :
|
||||
coreVersionSubRevision() ?
|
||||
coreVersionSubRevision() - 100 :
|
||||
0);
|
||||
}
|
||||
|
||||
/*
|
||||
unique revision indentifier (never decreases)
|
||||
*/
|
||||
constexpr
|
||||
int coreVersionNumeric()
|
||||
{
|
||||
return coreVersionMajor() * 10000000
|
||||
+ coreVersionMinor() * 100000
|
||||
+ coreVersionRevision() * 1000
|
||||
+ (coreVersionSubRevision() < 0 ?
|
||||
-coreVersionSubRevision() :
|
||||
coreVersionSubRevision() ?
|
||||
coreVersionSubRevision() - 100 :
|
||||
0);
|
||||
}
|
||||
|
||||
}; // namespace esp8266
|
||||
}; // namespace esp8266
|
||||
|
||||
} // extern "C++"
|
||||
#endif // __cplusplus
|
||||
|
@ -1,40 +1,40 @@
|
||||
/*
|
||||
esp8266_waveform - General purpose waveform generation and control,
|
||||
esp8266_waveform - General purpose waveform generation and control,
|
||||
supporting outputs on all pins in parallel.
|
||||
|
||||
Copyright (c) 2018 Earle F. Philhower, III. All rights reserved.
|
||||
Copyright (c) 2018 Earle F. Philhower, III. All rights reserved.
|
||||
|
||||
The core idea is to have a programmable waveform generator with a unique
|
||||
high and low period (defined in microseconds). TIMER1 is set to 1-shot
|
||||
mode and is always loaded with the time until the next edge of any live
|
||||
waveforms.
|
||||
The core idea is to have a programmable waveform generator with a unique
|
||||
high and low period (defined in microseconds). TIMER1 is set to 1-shot
|
||||
mode and is always loaded with the time until the next edge of any live
|
||||
waveforms.
|
||||
|
||||
Up to one waveform generator per pin supported.
|
||||
Up to one waveform generator per pin supported.
|
||||
|
||||
Each waveform generator is synchronized to the ESP cycle counter, not the
|
||||
timer. This allows for removing interrupt jitter and delay as the counter
|
||||
always increments once per 80MHz clock. Changes to a waveform are
|
||||
contiguous and only take effect on the next waveform transition,
|
||||
allowing for smooth transitions.
|
||||
Each waveform generator is synchronized to the ESP cycle counter, not the
|
||||
timer. This allows for removing interrupt jitter and delay as the counter
|
||||
always increments once per 80MHz clock. Changes to a waveform are
|
||||
contiguous and only take effect on the next waveform transition,
|
||||
allowing for smooth transitions.
|
||||
|
||||
This replaces older tone(), analogWrite(), and the Servo classes.
|
||||
This replaces older tone(), analogWrite(), and the Servo classes.
|
||||
|
||||
Everywhere in the code where "cycles" is used, it means ESP.getCycleTime()
|
||||
cycles, not TIMER1 cycles (which may be 2 CPU clocks @ 160MHz).
|
||||
Everywhere in the code where "cycles" is used, it means ESP.getCycleTime()
|
||||
cycles, not TIMER1 cycles (which may be 2 CPU clocks @ 160MHz).
|
||||
|
||||
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 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.
|
||||
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
|
||||
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
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
@ -43,318 +43,267 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
// Maximum delay between IRQs
|
||||
// Maximum delay between IRQs
|
||||
#define MAXIRQUS (10000)
|
||||
|
||||
// Set/clear GPIO 0-15 by bitmask
|
||||
// Set/clear GPIO 0-15 by bitmask
|
||||
#define SetGPIO(a) do { GPOS = a; } while (0)
|
||||
#define ClearGPIO(a) do { GPOC = a; } while (0)
|
||||
|
||||
// Waveform generator can create tones, PWM, and servos
|
||||
typedef struct
|
||||
{
|
||||
uint32_t nextServiceCycle; // ESP cycle timer when a transition required
|
||||
uint32_t expiryCycle; // For time-limited waveform, the cycle when this waveform must stop
|
||||
uint32_t nextTimeHighCycles; // Copy over low->high to keep smooth waveform
|
||||
uint32_t nextTimeLowCycles; // Copy over high->low to keep smooth waveform
|
||||
} Waveform;
|
||||
// Waveform generator can create tones, PWM, and servos
|
||||
typedef struct {
|
||||
uint32_t nextServiceCycle; // ESP cycle timer when a transition required
|
||||
uint32_t expiryCycle; // For time-limited waveform, the cycle when this waveform must stop
|
||||
uint32_t nextTimeHighCycles; // Copy over low->high to keep smooth waveform
|
||||
uint32_t nextTimeLowCycles; // Copy over high->low to keep smooth waveform
|
||||
} Waveform;
|
||||
|
||||
static Waveform waveform[17]; // State of all possible pins
|
||||
static volatile uint32_t waveformState = 0; // Is the pin high or low, updated in NMI so no access outside the NMI code
|
||||
static volatile uint32_t waveformEnabled = 0; // Is it actively running, updated in NMI so no access outside the NMI code
|
||||
static Waveform waveform[17]; // State of all possible pins
|
||||
static volatile uint32_t waveformState = 0; // Is the pin high or low, updated in NMI so no access outside the NMI code
|
||||
static volatile uint32_t waveformEnabled = 0; // Is it actively running, updated in NMI so no access outside the NMI code
|
||||
|
||||
// Enable lock-free by only allowing updates to waveformState and waveformEnabled from IRQ service routine
|
||||
static volatile uint32_t waveformToEnable = 0; // Message to the NMI handler to start a waveform on a inactive pin
|
||||
static volatile uint32_t waveformToDisable = 0; // Message to the NMI handler to disable a pin from waveform generation
|
||||
// Enable lock-free by only allowing updates to waveformState and waveformEnabled from IRQ service routine
|
||||
static volatile uint32_t waveformToEnable = 0; // Message to the NMI handler to start a waveform on a inactive pin
|
||||
static volatile uint32_t waveformToDisable = 0; // Message to the NMI handler to disable a pin from waveform generation
|
||||
|
||||
static uint32_t (*timer1CB)() = NULL;
|
||||
static uint32_t (*timer1CB)() = NULL;
|
||||
|
||||
|
||||
// Non-speed critical bits
|
||||
// Non-speed critical bits
|
||||
#pragma GCC optimize ("Os")
|
||||
|
||||
static inline ICACHE_RAM_ATTR uint32_t GetCycleCount()
|
||||
{
|
||||
uint32_t ccount;
|
||||
__asm__ __volatile__("esync; rsr %0,ccount":"=a"(ccount));
|
||||
return ccount;
|
||||
static inline ICACHE_RAM_ATTR uint32_t GetCycleCount() {
|
||||
uint32_t ccount;
|
||||
__asm__ __volatile__("esync; rsr %0,ccount":"=a"(ccount));
|
||||
return ccount;
|
||||
}
|
||||
|
||||
// Interrupt on/off control
|
||||
static ICACHE_RAM_ATTR void timer1Interrupt();
|
||||
static bool timerRunning = false;
|
||||
|
||||
static void initTimer() {
|
||||
timer1_disable();
|
||||
ETS_FRC_TIMER1_INTR_ATTACH(NULL, NULL);
|
||||
ETS_FRC_TIMER1_NMI_INTR_ATTACH(timer1Interrupt);
|
||||
timer1_enable(TIM_DIV1, TIM_EDGE, TIM_SINGLE);
|
||||
timerRunning = true;
|
||||
}
|
||||
|
||||
static void ICACHE_RAM_ATTR deinitTimer() {
|
||||
ETS_FRC_TIMER1_NMI_INTR_ATTACH(NULL);
|
||||
timer1_disable();
|
||||
timer1_isr_init();
|
||||
timerRunning = false;
|
||||
}
|
||||
|
||||
// Set a callback. Pass in NULL to stop it
|
||||
void setTimer1Callback(uint32_t (*fn)()) {
|
||||
timer1CB = fn;
|
||||
if (!timerRunning && fn) {
|
||||
initTimer();
|
||||
timer1_write(microsecondsToClockCycles(1)); // Cause an interrupt post-haste
|
||||
} else if (timerRunning && !fn && !waveformEnabled) {
|
||||
deinitTimer();
|
||||
}
|
||||
}
|
||||
|
||||
// Start up a waveform on a pin, or change the current one. Will change to the new
|
||||
// waveform smoothly on next low->high transition. For immediate change, stopWaveform()
|
||||
// first, then it will immediately begin.
|
||||
int startWaveform(uint8_t pin, uint32_t timeHighUS, uint32_t timeLowUS, uint32_t runTimeUS) {
|
||||
if ((pin > 16) || isFlashInterfacePin(pin)) {
|
||||
return false;
|
||||
}
|
||||
Waveform *wave = &waveform[pin];
|
||||
// Adjust to shave off some of the IRQ time, approximately
|
||||
wave->nextTimeHighCycles = microsecondsToClockCycles(timeHighUS);
|
||||
wave->nextTimeLowCycles = microsecondsToClockCycles(timeLowUS);
|
||||
wave->expiryCycle = runTimeUS ? GetCycleCount() + microsecondsToClockCycles(runTimeUS) : 0;
|
||||
if (runTimeUS && !wave->expiryCycle) {
|
||||
wave->expiryCycle = 1; // expiryCycle==0 means no timeout, so avoid setting it
|
||||
}
|
||||
|
||||
uint32_t mask = 1<<pin;
|
||||
if (!(waveformEnabled & mask)) {
|
||||
// Actually set the pin high or low in the IRQ service to guarantee times
|
||||
wave->nextServiceCycle = GetCycleCount() + microsecondsToClockCycles(1);
|
||||
waveformToEnable |= mask;
|
||||
if (!timerRunning) {
|
||||
initTimer();
|
||||
timer1_write(microsecondsToClockCycles(10));
|
||||
} else {
|
||||
// Ensure timely service....
|
||||
if (T1L > microsecondsToClockCycles(10)) {
|
||||
timer1_write(microsecondsToClockCycles(10));
|
||||
}
|
||||
}
|
||||
|
||||
// Interrupt on/off control
|
||||
static ICACHE_RAM_ATTR void timer1Interrupt();
|
||||
static bool timerRunning = false;
|
||||
|
||||
static void initTimer()
|
||||
{
|
||||
timer1_disable();
|
||||
ETS_FRC_TIMER1_INTR_ATTACH(NULL, NULL);
|
||||
ETS_FRC_TIMER1_NMI_INTR_ATTACH(timer1Interrupt);
|
||||
timer1_enable(TIM_DIV1, TIM_EDGE, TIM_SINGLE);
|
||||
timerRunning = true;
|
||||
while (waveformToEnable) {
|
||||
delay(0); // Wait for waveform to update
|
||||
}
|
||||
}
|
||||
|
||||
static void ICACHE_RAM_ATTR deinitTimer()
|
||||
{
|
||||
ETS_FRC_TIMER1_NMI_INTR_ATTACH(NULL);
|
||||
timer1_disable();
|
||||
timer1_isr_init();
|
||||
timerRunning = false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Set a callback. Pass in NULL to stop it
|
||||
void setTimer1Callback(uint32_t (*fn)())
|
||||
{
|
||||
timer1CB = fn;
|
||||
if (!timerRunning && fn)
|
||||
{
|
||||
initTimer();
|
||||
timer1_write(microsecondsToClockCycles(1)); // Cause an interrupt post-haste
|
||||
}
|
||||
else if (timerRunning && !fn && !waveformEnabled)
|
||||
{
|
||||
deinitTimer();
|
||||
}
|
||||
}
|
||||
|
||||
// Start up a waveform on a pin, or change the current one. Will change to the new
|
||||
// waveform smoothly on next low->high transition. For immediate change, stopWaveform()
|
||||
// first, then it will immediately begin.
|
||||
int startWaveform(uint8_t pin, uint32_t timeHighUS, uint32_t timeLowUS, uint32_t runTimeUS)
|
||||
{
|
||||
if ((pin > 16) || isFlashInterfacePin(pin))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
Waveform *wave = &waveform[pin];
|
||||
// Adjust to shave off some of the IRQ time, approximately
|
||||
wave->nextTimeHighCycles = microsecondsToClockCycles(timeHighUS);
|
||||
wave->nextTimeLowCycles = microsecondsToClockCycles(timeLowUS);
|
||||
wave->expiryCycle = runTimeUS ? GetCycleCount() + microsecondsToClockCycles(runTimeUS) : 0;
|
||||
if (runTimeUS && !wave->expiryCycle)
|
||||
{
|
||||
wave->expiryCycle = 1; // expiryCycle==0 means no timeout, so avoid setting it
|
||||
}
|
||||
|
||||
uint32_t mask = 1 << pin;
|
||||
if (!(waveformEnabled & mask))
|
||||
{
|
||||
// Actually set the pin high or low in the IRQ service to guarantee times
|
||||
wave->nextServiceCycle = GetCycleCount() + microsecondsToClockCycles(1);
|
||||
waveformToEnable |= mask;
|
||||
if (!timerRunning)
|
||||
{
|
||||
initTimer();
|
||||
timer1_write(microsecondsToClockCycles(10));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Ensure timely service....
|
||||
if (T1L > microsecondsToClockCycles(10))
|
||||
{
|
||||
timer1_write(microsecondsToClockCycles(10));
|
||||
}
|
||||
}
|
||||
while (waveformToEnable)
|
||||
{
|
||||
delay(0); // Wait for waveform to update
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Speed critical bits
|
||||
// Speed critical bits
|
||||
#pragma GCC optimize ("O2")
|
||||
// Normally would not want two copies like this, but due to different
|
||||
// optimization levels the inline attribute gets lost if we try the
|
||||
// other version.
|
||||
// Normally would not want two copies like this, but due to different
|
||||
// optimization levels the inline attribute gets lost if we try the
|
||||
// other version.
|
||||
|
||||
static inline ICACHE_RAM_ATTR uint32_t GetCycleCountIRQ()
|
||||
{
|
||||
uint32_t ccount;
|
||||
__asm__ __volatile__("rsr %0,ccount":"=a"(ccount));
|
||||
return ccount;
|
||||
}
|
||||
static inline ICACHE_RAM_ATTR uint32_t GetCycleCountIRQ() {
|
||||
uint32_t ccount;
|
||||
__asm__ __volatile__("rsr %0,ccount":"=a"(ccount));
|
||||
return ccount;
|
||||
}
|
||||
|
||||
static inline ICACHE_RAM_ATTR uint32_t min_u32(uint32_t a, uint32_t b)
|
||||
{
|
||||
if (a < b)
|
||||
{
|
||||
return a;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
static inline ICACHE_RAM_ATTR uint32_t min_u32(uint32_t a, uint32_t b) {
|
||||
if (a < b) {
|
||||
return a;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
// Stops a waveform on a pin
|
||||
int ICACHE_RAM_ATTR stopWaveform(uint8_t pin)
|
||||
{
|
||||
// Can't possibly need to stop anything if there is no timer active
|
||||
if (!timerRunning)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// If user sends in a pin >16 but <32, this will always point to a 0 bit
|
||||
// If they send >=32, then the shift will result in 0 and it will also return false
|
||||
uint32_t mask = 1 << pin;
|
||||
if (!(waveformEnabled & mask))
|
||||
{
|
||||
return false; // It's not running, nothing to do here
|
||||
}
|
||||
waveformToDisable |= mask;
|
||||
// Ensure timely service....
|
||||
if (T1L > microsecondsToClockCycles(10))
|
||||
{
|
||||
timer1_write(microsecondsToClockCycles(10));
|
||||
}
|
||||
while (waveformToDisable)
|
||||
{
|
||||
/* no-op */ // Can't delay() since stopWaveform may be called from an IRQ
|
||||
}
|
||||
if (!waveformEnabled && !timer1CB)
|
||||
{
|
||||
deinitTimer();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// Stops a waveform on a pin
|
||||
int ICACHE_RAM_ATTR stopWaveform(uint8_t pin) {
|
||||
// Can't possibly need to stop anything if there is no timer active
|
||||
if (!timerRunning) {
|
||||
return false;
|
||||
}
|
||||
// If user sends in a pin >16 but <32, this will always point to a 0 bit
|
||||
// If they send >=32, then the shift will result in 0 and it will also return false
|
||||
uint32_t mask = 1<<pin;
|
||||
if (!(waveformEnabled & mask)) {
|
||||
return false; // It's not running, nothing to do here
|
||||
}
|
||||
waveformToDisable |= mask;
|
||||
// Ensure timely service....
|
||||
if (T1L > microsecondsToClockCycles(10)) {
|
||||
timer1_write(microsecondsToClockCycles(10));
|
||||
}
|
||||
while (waveformToDisable) {
|
||||
/* no-op */ // Can't delay() since stopWaveform may be called from an IRQ
|
||||
}
|
||||
if (!waveformEnabled && !timer1CB) {
|
||||
deinitTimer();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// The SDK and hardware take some time to actually get to our NMI code, so
|
||||
// decrement the next IRQ's timer value by a bit so we can actually catch the
|
||||
// real CPU cycle counter we want for the waveforms.
|
||||
// The SDK and hardware take some time to actually get to our NMI code, so
|
||||
// decrement the next IRQ's timer value by a bit so we can actually catch the
|
||||
// real CPU cycle counter we want for the waveforms.
|
||||
#if F_CPU == 80000000
|
||||
#define DELTAIRQ (microsecondsToClockCycles(3))
|
||||
#define DELTAIRQ (microsecondsToClockCycles(3))
|
||||
#else
|
||||
#define DELTAIRQ (microsecondsToClockCycles(2))
|
||||
#define DELTAIRQ (microsecondsToClockCycles(2))
|
||||
#endif
|
||||
|
||||
|
||||
static ICACHE_RAM_ATTR void timer1Interrupt()
|
||||
{
|
||||
// Optimize the NMI inner loop by keeping track of the min and max GPIO that we
|
||||
// are generating. In the common case (1 PWM) these may be the same pin and
|
||||
// we can avoid looking at the other pins.
|
||||
static int startPin = 0;
|
||||
static int endPin = 0;
|
||||
static ICACHE_RAM_ATTR void timer1Interrupt() {
|
||||
// Optimize the NMI inner loop by keeping track of the min and max GPIO that we
|
||||
// are generating. In the common case (1 PWM) these may be the same pin and
|
||||
// we can avoid looking at the other pins.
|
||||
static int startPin = 0;
|
||||
static int endPin = 0;
|
||||
|
||||
uint32_t nextEventCycles = microsecondsToClockCycles(MAXIRQUS);
|
||||
uint32_t timeoutCycle = GetCycleCountIRQ() + microsecondsToClockCycles(14);
|
||||
uint32_t nextEventCycles = microsecondsToClockCycles(MAXIRQUS);
|
||||
uint32_t timeoutCycle = GetCycleCountIRQ() + microsecondsToClockCycles(14);
|
||||
|
||||
if (waveformToEnable || waveformToDisable)
|
||||
{
|
||||
// Handle enable/disable requests from main app.
|
||||
waveformEnabled = (waveformEnabled & ~waveformToDisable) | waveformToEnable; // Set the requested waveforms on/off
|
||||
waveformState &= ~waveformToEnable; // And clear the state of any just started
|
||||
waveformToEnable = 0;
|
||||
waveformToDisable = 0;
|
||||
// Find the first GPIO being generated by checking GCC's find-first-set (returns 1 + the bit of the first 1 in an int32_t)
|
||||
startPin = __builtin_ffs(waveformEnabled) - 1;
|
||||
// Find the last bit by subtracting off GCC's count-leading-zeros (no offset in this one)
|
||||
endPin = 32 - __builtin_clz(waveformEnabled);
|
||||
if (waveformToEnable || waveformToDisable) {
|
||||
// Handle enable/disable requests from main app.
|
||||
waveformEnabled = (waveformEnabled & ~waveformToDisable) | waveformToEnable; // Set the requested waveforms on/off
|
||||
waveformState &= ~waveformToEnable; // And clear the state of any just started
|
||||
waveformToEnable = 0;
|
||||
waveformToDisable = 0;
|
||||
// Find the first GPIO being generated by checking GCC's find-first-set (returns 1 + the bit of the first 1 in an int32_t)
|
||||
startPin = __builtin_ffs(waveformEnabled) - 1;
|
||||
// Find the last bit by subtracting off GCC's count-leading-zeros (no offset in this one)
|
||||
endPin = 32 - __builtin_clz(waveformEnabled);
|
||||
}
|
||||
|
||||
bool done = false;
|
||||
if (waveformEnabled) {
|
||||
do {
|
||||
nextEventCycles = microsecondsToClockCycles(MAXIRQUS);
|
||||
for (int i = startPin; i <= endPin; i++) {
|
||||
uint32_t mask = 1<<i;
|
||||
|
||||
// If it's not on, ignore!
|
||||
if (!(waveformEnabled & mask)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
bool done = false;
|
||||
if (waveformEnabled)
|
||||
{
|
||||
do
|
||||
{
|
||||
nextEventCycles = microsecondsToClockCycles(MAXIRQUS);
|
||||
for (int i = startPin; i <= endPin; i++)
|
||||
{
|
||||
uint32_t mask = 1 << i;
|
||||
Waveform *wave = &waveform[i];
|
||||
uint32_t now = GetCycleCountIRQ();
|
||||
|
||||
// If it's not on, ignore!
|
||||
if (!(waveformEnabled & mask))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Waveform *wave = &waveform[i];
|
||||
uint32_t now = GetCycleCountIRQ();
|
||||
|
||||
// Disable any waveforms that are done
|
||||
if (wave->expiryCycle)
|
||||
{
|
||||
int32_t expiryToGo = wave->expiryCycle - now;
|
||||
if (expiryToGo < 0)
|
||||
{
|
||||
// Done, remove!
|
||||
waveformEnabled &= ~mask;
|
||||
if (i == 16)
|
||||
{
|
||||
GP16O &= ~1;
|
||||
}
|
||||
else
|
||||
{
|
||||
ClearGPIO(mask);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Check for toggles
|
||||
int32_t cyclesToGo = wave->nextServiceCycle - now;
|
||||
if (cyclesToGo < 0)
|
||||
{
|
||||
waveformState ^= mask;
|
||||
if (waveformState & mask)
|
||||
{
|
||||
if (i == 16)
|
||||
{
|
||||
GP16O |= 1; // GPIO16 write slow as it's RMW
|
||||
}
|
||||
else
|
||||
{
|
||||
SetGPIO(mask);
|
||||
}
|
||||
wave->nextServiceCycle = now + wave->nextTimeHighCycles;
|
||||
nextEventCycles = min_u32(nextEventCycles, wave->nextTimeHighCycles);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (i == 16)
|
||||
{
|
||||
GP16O &= ~1; // GPIO16 write slow as it's RMW
|
||||
}
|
||||
else
|
||||
{
|
||||
ClearGPIO(mask);
|
||||
}
|
||||
wave->nextServiceCycle = now + wave->nextTimeLowCycles;
|
||||
nextEventCycles = min_u32(nextEventCycles, wave->nextTimeLowCycles);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
uint32_t deltaCycles = wave->nextServiceCycle - now;
|
||||
nextEventCycles = min_u32(nextEventCycles, deltaCycles);
|
||||
}
|
||||
}
|
||||
|
||||
// Exit the loop if we've hit the fixed runtime limit or the next event is known to be after that timeout would occur
|
||||
uint32_t now = GetCycleCountIRQ();
|
||||
int32_t cycleDeltaNextEvent = timeoutCycle - (now + nextEventCycles);
|
||||
int32_t cyclesLeftTimeout = timeoutCycle - now;
|
||||
done = (cycleDeltaNextEvent < 0) || (cyclesLeftTimeout < 0);
|
||||
} while (!done);
|
||||
} // if (waveformEnabled)
|
||||
|
||||
if (timer1CB)
|
||||
{
|
||||
nextEventCycles = min_u32(nextEventCycles, timer1CB());
|
||||
// Disable any waveforms that are done
|
||||
if (wave->expiryCycle) {
|
||||
int32_t expiryToGo = wave->expiryCycle - now;
|
||||
if (expiryToGo < 0) {
|
||||
// Done, remove!
|
||||
waveformEnabled &= ~mask;
|
||||
if (i == 16) {
|
||||
GP16O &= ~1;
|
||||
} else {
|
||||
ClearGPIO(mask);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (nextEventCycles < microsecondsToClockCycles(10))
|
||||
{
|
||||
nextEventCycles = microsecondsToClockCycles(10);
|
||||
// Check for toggles
|
||||
int32_t cyclesToGo = wave->nextServiceCycle - now;
|
||||
if (cyclesToGo < 0) {
|
||||
waveformState ^= mask;
|
||||
if (waveformState & mask) {
|
||||
if (i == 16) {
|
||||
GP16O |= 1; // GPIO16 write slow as it's RMW
|
||||
} else {
|
||||
SetGPIO(mask);
|
||||
}
|
||||
wave->nextServiceCycle = now + wave->nextTimeHighCycles;
|
||||
nextEventCycles = min_u32(nextEventCycles, wave->nextTimeHighCycles);
|
||||
} else {
|
||||
if (i == 16) {
|
||||
GP16O &= ~1; // GPIO16 write slow as it's RMW
|
||||
} else {
|
||||
ClearGPIO(mask);
|
||||
}
|
||||
wave->nextServiceCycle = now + wave->nextTimeLowCycles;
|
||||
nextEventCycles = min_u32(nextEventCycles, wave->nextTimeLowCycles);
|
||||
}
|
||||
} else {
|
||||
uint32_t deltaCycles = wave->nextServiceCycle - now;
|
||||
nextEventCycles = min_u32(nextEventCycles, deltaCycles);
|
||||
}
|
||||
nextEventCycles -= DELTAIRQ;
|
||||
}
|
||||
|
||||
// Do it here instead of global function to save time and because we know it's edge-IRQ
|
||||
// Exit the loop if we've hit the fixed runtime limit or the next event is known to be after that timeout would occur
|
||||
uint32_t now = GetCycleCountIRQ();
|
||||
int32_t cycleDeltaNextEvent = timeoutCycle - (now + nextEventCycles);
|
||||
int32_t cyclesLeftTimeout = timeoutCycle - now;
|
||||
done = (cycleDeltaNextEvent < 0) || (cyclesLeftTimeout < 0);
|
||||
} while (!done);
|
||||
} // if (waveformEnabled)
|
||||
|
||||
if (timer1CB) {
|
||||
nextEventCycles = min_u32(nextEventCycles, timer1CB());
|
||||
}
|
||||
|
||||
if (nextEventCycles < microsecondsToClockCycles(10)) {
|
||||
nextEventCycles = microsecondsToClockCycles(10);
|
||||
}
|
||||
nextEventCycles -= DELTAIRQ;
|
||||
|
||||
// Do it here instead of global function to save time and because we know it's edge-IRQ
|
||||
#if F_CPU == 160000000
|
||||
T1L = nextEventCycles >> 1; // Already know we're in range by MAXIRQUS
|
||||
T1L = nextEventCycles >> 1; // Already know we're in range by MAXIRQUS
|
||||
#else
|
||||
T1L = nextEventCycles; // Already know we're in range by MAXIRQUS
|
||||
T1L = nextEventCycles; // Already know we're in range by MAXIRQUS
|
||||
#endif
|
||||
TEIE |= TEIE1; // Edge int enable
|
||||
}
|
||||
TEIE |= TEIE1; // Edge int enable
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -1,40 +1,40 @@
|
||||
/*
|
||||
esp8266_waveform - General purpose waveform generation and control,
|
||||
esp8266_waveform - General purpose waveform generation and control,
|
||||
supporting outputs on all pins in parallel.
|
||||
|
||||
Copyright (c) 2018 Earle F. Philhower, III. All rights reserved.
|
||||
Copyright (c) 2018 Earle F. Philhower, III. All rights reserved.
|
||||
|
||||
The core idea is to have a programmable waveform generator with a unique
|
||||
high and low period (defined in microseconds). TIMER1 is set to 1-shot
|
||||
mode and is always loaded with the time until the next edge of any live
|
||||
waveforms.
|
||||
The core idea is to have a programmable waveform generator with a unique
|
||||
high and low period (defined in microseconds). TIMER1 is set to 1-shot
|
||||
mode and is always loaded with the time until the next edge of any live
|
||||
waveforms.
|
||||
|
||||
Up to one waveform generator per pin supported.
|
||||
Up to one waveform generator per pin supported.
|
||||
|
||||
Each waveform generator is synchronized to the ESP cycle counter, not the
|
||||
timer. This allows for removing interrupt jitter and delay as the counter
|
||||
always increments once per 80MHz clock. Changes to a waveform are
|
||||
contiguous and only take effect on the next waveform transition,
|
||||
allowing for smooth transitions.
|
||||
Each waveform generator is synchronized to the ESP cycle counter, not the
|
||||
timer. This allows for removing interrupt jitter and delay as the counter
|
||||
always increments once per 80MHz clock. Changes to a waveform are
|
||||
contiguous and only take effect on the next waveform transition,
|
||||
allowing for smooth transitions.
|
||||
|
||||
This replaces older tone(), analogWrite(), and the Servo classes.
|
||||
This replaces older tone(), analogWrite(), and the Servo classes.
|
||||
|
||||
Everywhere in the code where "cycles" is used, it means ESP.getCycleTime()
|
||||
cycles, not TIMER1 cycles (which may be 2 CPU clocks @ 160MHz).
|
||||
Everywhere in the code where "cycles" is used, it means ESP.getCycleTime()
|
||||
cycles, not TIMER1 cycles (which may be 2 CPU clocks @ 160MHz).
|
||||
|
||||
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 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.
|
||||
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
|
||||
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
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
|
@ -1,23 +1,23 @@
|
||||
/*
|
||||
core_esp8266_wiring.c - implementation of Wiring API for esp8266
|
||||
core_esp8266_wiring.c - implementation of Wiring API for esp8266
|
||||
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2014 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#include "wiring_private.h"
|
||||
#include "ets_sys.h"
|
||||
@ -27,205 +27,191 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
extern void ets_delay_us(uint32_t us);
|
||||
extern void esp_schedule();
|
||||
extern void esp_yield();
|
||||
extern void ets_delay_us(uint32_t us);
|
||||
extern void esp_schedule();
|
||||
extern void esp_yield();
|
||||
|
||||
static os_timer_t delay_timer;
|
||||
static os_timer_t micros_overflow_timer;
|
||||
static uint32_t micros_at_last_overflow_tick = 0;
|
||||
static uint32_t micros_overflow_count = 0;
|
||||
static os_timer_t delay_timer;
|
||||
static os_timer_t micros_overflow_timer;
|
||||
static uint32_t micros_at_last_overflow_tick = 0;
|
||||
static uint32_t micros_overflow_count = 0;
|
||||
#define ONCE 0
|
||||
#define REPEAT 1
|
||||
|
||||
void delay_end(void* arg)
|
||||
{
|
||||
(void) arg;
|
||||
void delay_end(void* arg) {
|
||||
(void) arg;
|
||||
esp_schedule();
|
||||
}
|
||||
|
||||
void delay(unsigned long ms) {
|
||||
if(ms) {
|
||||
os_timer_setfn(&delay_timer, (os_timer_func_t*) &delay_end, 0);
|
||||
os_timer_arm(&delay_timer, ms, ONCE);
|
||||
} else {
|
||||
esp_schedule();
|
||||
}
|
||||
|
||||
void delay(unsigned long ms)
|
||||
{
|
||||
if (ms)
|
||||
{
|
||||
os_timer_setfn(&delay_timer, (os_timer_func_t*) &delay_end, 0);
|
||||
os_timer_arm(&delay_timer, ms, ONCE);
|
||||
}
|
||||
else
|
||||
{
|
||||
esp_schedule();
|
||||
}
|
||||
esp_yield();
|
||||
if (ms)
|
||||
{
|
||||
os_timer_disarm(&delay_timer);
|
||||
}
|
||||
esp_yield();
|
||||
if(ms) {
|
||||
os_timer_disarm(&delay_timer);
|
||||
}
|
||||
}
|
||||
|
||||
void micros_overflow_tick(void* arg)
|
||||
{
|
||||
(void) arg;
|
||||
uint32_t m = system_get_time();
|
||||
if (m < micros_at_last_overflow_tick)
|
||||
{
|
||||
++micros_overflow_count;
|
||||
}
|
||||
micros_at_last_overflow_tick = m;
|
||||
}
|
||||
void micros_overflow_tick(void* arg) {
|
||||
(void) arg;
|
||||
uint32_t m = system_get_time();
|
||||
if(m < micros_at_last_overflow_tick)
|
||||
++micros_overflow_count;
|
||||
micros_at_last_overflow_tick = m;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
// millis() 'magic multiplier' approximation
|
||||
//
|
||||
// This function corrects the cumlative (296us / usec overflow) drift
|
||||
// seen in the orignal 'millis()' function.
|
||||
//
|
||||
// Input:
|
||||
// 'm' - 32-bit usec counter, 0 <= m <= 0xFFFFFFFF
|
||||
// 'c' - 32-bit usec overflow counter 0 <= c < 0x00400000
|
||||
// Output:
|
||||
// Returns milliseconds in modulo 0x1,0000,0000 (0 to 0xFFFFFFFF)
|
||||
//
|
||||
// Notes:
|
||||
//
|
||||
// 1) This routine approximates the 64-bit integer division,
|
||||
//
|
||||
// quotient = ( 2^32 c + m ) / 1000,
|
||||
//
|
||||
// through the use of 'magic' multipliers. A slow division is replaced by
|
||||
// a faster multiply using a scaled multiplicative inverse of the divisor:
|
||||
//
|
||||
// quotient =~ ( 2^32 c + m ) * k, where k = Ceiling[ 2^n / 1000 ]
|
||||
//
|
||||
// The precision difference between multiplier and divisor sets the
|
||||
// upper-bound of the dividend which can be successfully divided.
|
||||
//
|
||||
// For this application, n = 64, and the divisor (1000) has 10-bits of
|
||||
// precision. This sets the dividend upper-bound to (64 - 10) = 54 bits,
|
||||
// and that of 'c' to (54 - 32) = 22 bits. This corresponds to a value
|
||||
// for 'c' = 0x0040,0000 , or +570 years of usec counter overflows.
|
||||
//
|
||||
// 2) A distributed multiply with offset-summing is used find k( 2^32 c + m ):
|
||||
//
|
||||
// prd = (2^32 kh + kl) * ( 2^32 c + m )
|
||||
// = 2^64 kh c + 2^32 kl c + 2^32 kh m + kl m
|
||||
// (d) (c) (b) (a)
|
||||
//
|
||||
// Graphically, the offset-sums align in little endian like this:
|
||||
// LS -> MS
|
||||
// 32 64 96 128
|
||||
// | a[-1] | a[0] | a[1] | a[2] |
|
||||
// | m kl | 0 | 0 | a[-1] not needed
|
||||
// | | m kh | |
|
||||
// | | c kl | | a[1] holds the result
|
||||
// | | | c kh | a[2] can be discarded
|
||||
//
|
||||
// As only the high-word of 'm kl' and low-word of 'c kh' contribute to the
|
||||
// overall result, only (2) 32-bit words are needed for the accumulator.
|
||||
//
|
||||
// 3) As C++ does not intrinsically test for addition overflows, one must
|
||||
// code specifically to detect them. This approximation skips these
|
||||
// overflow checks for speed, hence the sum,
|
||||
//
|
||||
// highword( m kl ) + m kh + c kl < (2^64-1), MUST NOT OVERFLOW.
|
||||
//
|
||||
// To meet this criteria, not only do we have to pick 'k' to achieve our
|
||||
// desired precision, we also have to split 'k' appropriately to avoid
|
||||
// any addition overflows.
|
||||
//
|
||||
// 'k' should be also chosen to align the various products on byte
|
||||
// boundaries to avoid any 64-bit shifts before additions, as they incur
|
||||
// major time penalties. The 'k' chosen for this specific division by 1000
|
||||
// was picked primarily to avoid shifts as well as for precision.
|
||||
//
|
||||
// For the reasons list above, this routine is NOT a general one.
|
||||
// Changing divisors could break the overflow requirement and force
|
||||
// picking a 'k' split which requires shifts before additions.
|
||||
//
|
||||
// ** Test THOROUGHLY after making changes **
|
||||
//
|
||||
// 4) Results of time benchmarks run on an ESP8266 Huzzah feather are:
|
||||
//
|
||||
// usec x Orig Comment
|
||||
// Orig: 3.18 1.00 Original code
|
||||
// Corr: 13.21 4.15 64-bit reference code
|
||||
// Test: 4.60 1.45 64-bit magic multiply, 4x32
|
||||
//
|
||||
// The magic multiplier routine runs ~3x faster than the reference. Execution
|
||||
// times can vary considerably with the numbers being multiplied, so one
|
||||
// should derate this factor to around 2x, worst case.
|
||||
//
|
||||
// Reference function: corrected millis(), 64-bit arithmetic,
|
||||
// truncated to 32-bits by return
|
||||
// unsigned long ICACHE_RAM_ATTR millis_corr_DEBUG( void )
|
||||
// {
|
||||
// // Get usec system time, usec overflow conter
|
||||
// ......
|
||||
// return ( (c * 4294967296 + m) / 1000 ); // 64-bit division is SLOW
|
||||
// } //millis_corr
|
||||
//
|
||||
// 5) See this link for a good discussion on magic multipliers:
|
||||
// http://ridiculousfish.com/blog/posts/labor-of-division-episode-i.html
|
||||
//
|
||||
//---------------------------------------------------------------------------
|
||||
// millis() 'magic multiplier' approximation
|
||||
//
|
||||
// This function corrects the cumlative (296us / usec overflow) drift
|
||||
// seen in the orignal 'millis()' function.
|
||||
//
|
||||
// Input:
|
||||
// 'm' - 32-bit usec counter, 0 <= m <= 0xFFFFFFFF
|
||||
// 'c' - 32-bit usec overflow counter 0 <= c < 0x00400000
|
||||
// Output:
|
||||
// Returns milliseconds in modulo 0x1,0000,0000 (0 to 0xFFFFFFFF)
|
||||
//
|
||||
// Notes:
|
||||
//
|
||||
// 1) This routine approximates the 64-bit integer division,
|
||||
//
|
||||
// quotient = ( 2^32 c + m ) / 1000,
|
||||
//
|
||||
// through the use of 'magic' multipliers. A slow division is replaced by
|
||||
// a faster multiply using a scaled multiplicative inverse of the divisor:
|
||||
//
|
||||
// quotient =~ ( 2^32 c + m ) * k, where k = Ceiling[ 2^n / 1000 ]
|
||||
//
|
||||
// The precision difference between multiplier and divisor sets the
|
||||
// upper-bound of the dividend which can be successfully divided.
|
||||
//
|
||||
// For this application, n = 64, and the divisor (1000) has 10-bits of
|
||||
// precision. This sets the dividend upper-bound to (64 - 10) = 54 bits,
|
||||
// and that of 'c' to (54 - 32) = 22 bits. This corresponds to a value
|
||||
// for 'c' = 0x0040,0000 , or +570 years of usec counter overflows.
|
||||
//
|
||||
// 2) A distributed multiply with offset-summing is used find k( 2^32 c + m ):
|
||||
//
|
||||
// prd = (2^32 kh + kl) * ( 2^32 c + m )
|
||||
// = 2^64 kh c + 2^32 kl c + 2^32 kh m + kl m
|
||||
// (d) (c) (b) (a)
|
||||
//
|
||||
// Graphically, the offset-sums align in little endian like this:
|
||||
// LS -> MS
|
||||
// 32 64 96 128
|
||||
// | a[-1] | a[0] | a[1] | a[2] |
|
||||
// | m kl | 0 | 0 | a[-1] not needed
|
||||
// | | m kh | |
|
||||
// | | c kl | | a[1] holds the result
|
||||
// | | | c kh | a[2] can be discarded
|
||||
//
|
||||
// As only the high-word of 'm kl' and low-word of 'c kh' contribute to the
|
||||
// overall result, only (2) 32-bit words are needed for the accumulator.
|
||||
//
|
||||
// 3) As C++ does not intrinsically test for addition overflows, one must
|
||||
// code specifically to detect them. This approximation skips these
|
||||
// overflow checks for speed, hence the sum,
|
||||
//
|
||||
// highword( m kl ) + m kh + c kl < (2^64-1), MUST NOT OVERFLOW.
|
||||
//
|
||||
// To meet this criteria, not only do we have to pick 'k' to achieve our
|
||||
// desired precision, we also have to split 'k' appropriately to avoid
|
||||
// any addition overflows.
|
||||
//
|
||||
// 'k' should be also chosen to align the various products on byte
|
||||
// boundaries to avoid any 64-bit shifts before additions, as they incur
|
||||
// major time penalties. The 'k' chosen for this specific division by 1000
|
||||
// was picked primarily to avoid shifts as well as for precision.
|
||||
//
|
||||
// For the reasons list above, this routine is NOT a general one.
|
||||
// Changing divisors could break the overflow requirement and force
|
||||
// picking a 'k' split which requires shifts before additions.
|
||||
//
|
||||
// ** Test THOROUGHLY after making changes **
|
||||
//
|
||||
// 4) Results of time benchmarks run on an ESP8266 Huzzah feather are:
|
||||
//
|
||||
// usec x Orig Comment
|
||||
// Orig: 3.18 1.00 Original code
|
||||
// Corr: 13.21 4.15 64-bit reference code
|
||||
// Test: 4.60 1.45 64-bit magic multiply, 4x32
|
||||
//
|
||||
// The magic multiplier routine runs ~3x faster than the reference. Execution
|
||||
// times can vary considerably with the numbers being multiplied, so one
|
||||
// should derate this factor to around 2x, worst case.
|
||||
//
|
||||
// Reference function: corrected millis(), 64-bit arithmetic,
|
||||
// truncated to 32-bits by return
|
||||
// unsigned long ICACHE_RAM_ATTR millis_corr_DEBUG( void )
|
||||
// {
|
||||
// // Get usec system time, usec overflow conter
|
||||
// ......
|
||||
// return ( (c * 4294967296 + m) / 1000 ); // 64-bit division is SLOW
|
||||
// } //millis_corr
|
||||
//
|
||||
// 5) See this link for a good discussion on magic multipliers:
|
||||
// http://ridiculousfish.com/blog/posts/labor-of-division-episode-i.html
|
||||
//
|
||||
|
||||
#define MAGIC_1E3_wLO 0x4bc6a7f0 // LS part
|
||||
#define MAGIC_1E3_wHI 0x00418937 // MS part, magic multiplier
|
||||
|
||||
unsigned long ICACHE_RAM_ATTR millis()
|
||||
{
|
||||
union
|
||||
{
|
||||
uint64_t q; // Accumulator, 64-bit, little endian
|
||||
uint32_t a[2]; // ..........., 32-bit segments
|
||||
} acc;
|
||||
acc.a[1] = 0; // Zero high-acc
|
||||
unsigned long ICACHE_RAM_ATTR millis()
|
||||
{
|
||||
union {
|
||||
uint64_t q; // Accumulator, 64-bit, little endian
|
||||
uint32_t a[2]; // ..........., 32-bit segments
|
||||
} acc;
|
||||
acc.a[1] = 0; // Zero high-acc
|
||||
|
||||
// Get usec system time, usec overflow counter
|
||||
uint32_t m = system_get_time();
|
||||
uint32_t c = micros_overflow_count +
|
||||
((m < micros_at_last_overflow_tick) ? 1 : 0);
|
||||
// Get usec system time, usec overflow counter
|
||||
uint32_t m = system_get_time();
|
||||
uint32_t c = micros_overflow_count +
|
||||
((m < micros_at_last_overflow_tick) ? 1 : 0);
|
||||
|
||||
// (a) Init. low-acc with high-word of 1st product. The right-shift
|
||||
// falls on a byte boundary, hence is relatively quick.
|
||||
// (a) Init. low-acc with high-word of 1st product. The right-shift
|
||||
// falls on a byte boundary, hence is relatively quick.
|
||||
|
||||
acc.q = ((uint64_t)(m * (uint64_t)MAGIC_1E3_wLO) >> 32);
|
||||
acc.q = ( (uint64_t)( m * (uint64_t)MAGIC_1E3_wLO ) >> 32 );
|
||||
|
||||
// (b) Offset sum, low-acc
|
||||
acc.q += (m * (uint64_t)MAGIC_1E3_wHI);
|
||||
// (b) Offset sum, low-acc
|
||||
acc.q += ( m * (uint64_t)MAGIC_1E3_wHI );
|
||||
|
||||
// (c) Offset sum, low-acc
|
||||
acc.q += (c * (uint64_t)MAGIC_1E3_wLO);
|
||||
// (c) Offset sum, low-acc
|
||||
acc.q += ( c * (uint64_t)MAGIC_1E3_wLO );
|
||||
|
||||
// (d) Truncated sum, high-acc
|
||||
acc.a[1] += (uint32_t)(c * (uint64_t)MAGIC_1E3_wHI);
|
||||
// (d) Truncated sum, high-acc
|
||||
acc.a[1] += (uint32_t)( c * (uint64_t)MAGIC_1E3_wHI );
|
||||
|
||||
return (acc.a[1]); // Extract result, high-acc
|
||||
return ( acc.a[1] ); // Extract result, high-acc
|
||||
|
||||
} //millis
|
||||
} //millis
|
||||
|
||||
unsigned long ICACHE_RAM_ATTR micros()
|
||||
{
|
||||
return system_get_time();
|
||||
}
|
||||
unsigned long ICACHE_RAM_ATTR micros() {
|
||||
return system_get_time();
|
||||
}
|
||||
|
||||
uint64_t ICACHE_RAM_ATTR micros64()
|
||||
{
|
||||
uint32_t low32_us = system_get_time();
|
||||
uint32_t high32_us = micros_overflow_count + ((low32_us < micros_at_last_overflow_tick) ? 1 : 0);
|
||||
uint64_t duration64_us = (uint64_t)high32_us << 32 | low32_us;
|
||||
return duration64_us;
|
||||
}
|
||||
uint64_t ICACHE_RAM_ATTR micros64() {
|
||||
uint32_t low32_us = system_get_time();
|
||||
uint32_t high32_us = micros_overflow_count + ((low32_us < micros_at_last_overflow_tick) ? 1 : 0);
|
||||
uint64_t duration64_us = (uint64_t)high32_us << 32 | low32_us;
|
||||
return duration64_us;
|
||||
}
|
||||
|
||||
void ICACHE_RAM_ATTR delayMicroseconds(unsigned int us)
|
||||
{
|
||||
os_delay_us(us);
|
||||
}
|
||||
void ICACHE_RAM_ATTR delayMicroseconds(unsigned int us) {
|
||||
os_delay_us(us);
|
||||
}
|
||||
|
||||
void init()
|
||||
{
|
||||
initPins();
|
||||
timer1_isr_init();
|
||||
os_timer_setfn(µs_overflow_timer, (os_timer_func_t*) µs_overflow_tick, 0);
|
||||
os_timer_arm(µs_overflow_timer, 60000, REPEAT);
|
||||
}
|
||||
void init() {
|
||||
initPins();
|
||||
timer1_isr_init();
|
||||
os_timer_setfn(µs_overflow_timer, (os_timer_func_t*) µs_overflow_tick, 0);
|
||||
os_timer_arm(µs_overflow_timer, 60000, REPEAT);
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -1,25 +1,25 @@
|
||||
/*
|
||||
analog.c - analogRead implementation for esp8266
|
||||
analog.c - analogRead implementation for esp8266
|
||||
|
||||
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2015 Hristo Gochkov. 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 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.
|
||||
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
|
||||
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
|
||||
|
||||
|
||||
18/06/2015 analogRead bugfix by Testato
|
||||
18/06/2015 analogRead bugfix by Testato
|
||||
*/
|
||||
|
||||
#include "wiring_private.h"
|
||||
@ -28,16 +28,15 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
extern int __analogRead(uint8_t pin)
|
||||
{
|
||||
// accept both A0 constant and ADC channel number
|
||||
if (pin == 17 || pin == 0)
|
||||
{
|
||||
return system_adc_read();
|
||||
}
|
||||
return digitalRead(pin) * 1023;
|
||||
extern int __analogRead(uint8_t pin)
|
||||
{
|
||||
// accept both A0 constant and ADC channel number
|
||||
if(pin == 17 || pin == 0) {
|
||||
return system_adc_read();
|
||||
}
|
||||
return digitalRead(pin) * 1023;
|
||||
}
|
||||
|
||||
extern int analogRead(uint8_t pin) __attribute__((weak, alias("__analogRead")));
|
||||
extern int analogRead(uint8_t pin) __attribute__ ((weak, alias("__analogRead")));
|
||||
|
||||
};
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
digital.c - wiring digital implementation for esp8266
|
||||
digital.c - wiring digital implementation for esp8266
|
||||
|
||||
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2015 Hristo Gochkov. 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 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.
|
||||
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
|
||||
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
|
||||
*/
|
||||
#define ARDUINO_MAIN
|
||||
#include "wiring_private.h"
|
||||
@ -29,291 +29,220 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
uint8_t esp8266_gpioToFn[16] = {0x34, 0x18, 0x38, 0x14, 0x3C, 0x40, 0x1C, 0x20, 0x24, 0x28, 0x2C, 0x30, 0x04, 0x08, 0x0C, 0x10};
|
||||
uint8_t esp8266_gpioToFn[16] = {0x34, 0x18, 0x38, 0x14, 0x3C, 0x40, 0x1C, 0x20, 0x24, 0x28, 0x2C, 0x30, 0x04, 0x08, 0x0C, 0x10};
|
||||
|
||||
extern void __pinMode(uint8_t pin, uint8_t mode)
|
||||
{
|
||||
if (pin < 16)
|
||||
{
|
||||
if (mode == SPECIAL)
|
||||
{
|
||||
GPC(pin) = (GPC(pin) & (0xF << GPCI)); //SOURCE(GPIO) | DRIVER(NORMAL) | INT_TYPE(UNCHANGED) | WAKEUP_ENABLE(DISABLED)
|
||||
GPEC = (1 << pin); //Disable
|
||||
GPF(pin) = GPFFS(GPFFS_BUS(pin));//Set mode to BUS (RX0, TX0, TX1, SPI, HSPI or CLK depending in the pin)
|
||||
if (pin == 3)
|
||||
{
|
||||
GPF(pin) |= (1 << GPFPU); //enable pullup on RX
|
||||
}
|
||||
}
|
||||
else if (mode & FUNCTION_0)
|
||||
{
|
||||
GPC(pin) = (GPC(pin) & (0xF << GPCI)); //SOURCE(GPIO) | DRIVER(NORMAL) | INT_TYPE(UNCHANGED) | WAKEUP_ENABLE(DISABLED)
|
||||
GPEC = (1 << pin); //Disable
|
||||
GPF(pin) = GPFFS((mode >> 4) & 0x07);
|
||||
if (pin == 13 && mode == FUNCTION_4)
|
||||
{
|
||||
GPF(pin) |= (1 << GPFPU); //enable pullup on RX
|
||||
}
|
||||
}
|
||||
else if (mode == OUTPUT || mode == OUTPUT_OPEN_DRAIN)
|
||||
{
|
||||
GPF(pin) = GPFFS(GPFFS_GPIO(pin));//Set mode to GPIO
|
||||
GPC(pin) = (GPC(pin) & (0xF << GPCI)); //SOURCE(GPIO) | DRIVER(NORMAL) | INT_TYPE(UNCHANGED) | WAKEUP_ENABLE(DISABLED)
|
||||
if (mode == OUTPUT_OPEN_DRAIN)
|
||||
{
|
||||
GPC(pin) |= (1 << GPCD);
|
||||
}
|
||||
GPES = (1 << pin); //Enable
|
||||
}
|
||||
else if (mode == INPUT || mode == INPUT_PULLUP)
|
||||
{
|
||||
GPF(pin) = GPFFS(GPFFS_GPIO(pin));//Set mode to GPIO
|
||||
GPEC = (1 << pin); //Disable
|
||||
GPC(pin) = (GPC(pin) & (0xF << GPCI)) | (1 << GPCD); //SOURCE(GPIO) | DRIVER(OPEN_DRAIN) | INT_TYPE(UNCHANGED) | WAKEUP_ENABLE(DISABLED)
|
||||
if (mode == INPUT_PULLUP)
|
||||
{
|
||||
GPF(pin) |= (1 << GPFPU); // Enable Pullup
|
||||
}
|
||||
}
|
||||
else if (mode == WAKEUP_PULLUP || mode == WAKEUP_PULLDOWN)
|
||||
{
|
||||
GPF(pin) = GPFFS(GPFFS_GPIO(pin));//Set mode to GPIO
|
||||
GPEC = (1 << pin); //Disable
|
||||
if (mode == WAKEUP_PULLUP)
|
||||
{
|
||||
GPF(pin) |= (1 << GPFPU); // Enable Pullup
|
||||
GPC(pin) = (1 << GPCD) | (4 << GPCI) | (1 << GPCWE); //SOURCE(GPIO) | DRIVER(OPEN_DRAIN) | INT_TYPE(LOW) | WAKEUP_ENABLE(ENABLED)
|
||||
}
|
||||
else
|
||||
{
|
||||
GPF(pin) |= (1 << GPFPD); // Enable Pulldown
|
||||
GPC(pin) = (1 << GPCD) | (5 << GPCI) | (1 << GPCWE); //SOURCE(GPIO) | DRIVER(OPEN_DRAIN) | INT_TYPE(HIGH) | WAKEUP_ENABLE(ENABLED)
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (pin == 16)
|
||||
{
|
||||
GPF16 = GP16FFS(GPFFS_GPIO(pin));//Set mode to GPIO
|
||||
GPC16 = 0;
|
||||
if (mode == INPUT || mode == INPUT_PULLDOWN_16)
|
||||
{
|
||||
if (mode == INPUT_PULLDOWN_16)
|
||||
{
|
||||
GPF16 |= (1 << GP16FPD);//Enable Pulldown
|
||||
}
|
||||
GP16E &= ~1;
|
||||
}
|
||||
else if (mode == OUTPUT)
|
||||
{
|
||||
GP16E |= 1;
|
||||
}
|
||||
}
|
||||
extern void __pinMode(uint8_t pin, uint8_t mode) {
|
||||
if(pin < 16){
|
||||
if(mode == SPECIAL){
|
||||
GPC(pin) = (GPC(pin) & (0xF << GPCI)); //SOURCE(GPIO) | DRIVER(NORMAL) | INT_TYPE(UNCHANGED) | WAKEUP_ENABLE(DISABLED)
|
||||
GPEC = (1 << pin); //Disable
|
||||
GPF(pin) = GPFFS(GPFFS_BUS(pin));//Set mode to BUS (RX0, TX0, TX1, SPI, HSPI or CLK depending in the pin)
|
||||
if(pin == 3) GPF(pin) |= (1 << GPFPU);//enable pullup on RX
|
||||
} else if(mode & FUNCTION_0){
|
||||
GPC(pin) = (GPC(pin) & (0xF << GPCI)); //SOURCE(GPIO) | DRIVER(NORMAL) | INT_TYPE(UNCHANGED) | WAKEUP_ENABLE(DISABLED)
|
||||
GPEC = (1 << pin); //Disable
|
||||
GPF(pin) = GPFFS((mode >> 4) & 0x07);
|
||||
if(pin == 13 && mode == FUNCTION_4) GPF(pin) |= (1 << GPFPU);//enable pullup on RX
|
||||
} else if(mode == OUTPUT || mode == OUTPUT_OPEN_DRAIN){
|
||||
GPF(pin) = GPFFS(GPFFS_GPIO(pin));//Set mode to GPIO
|
||||
GPC(pin) = (GPC(pin) & (0xF << GPCI)); //SOURCE(GPIO) | DRIVER(NORMAL) | INT_TYPE(UNCHANGED) | WAKEUP_ENABLE(DISABLED)
|
||||
if(mode == OUTPUT_OPEN_DRAIN) GPC(pin) |= (1 << GPCD);
|
||||
GPES = (1 << pin); //Enable
|
||||
} else if(mode == INPUT || mode == INPUT_PULLUP){
|
||||
GPF(pin) = GPFFS(GPFFS_GPIO(pin));//Set mode to GPIO
|
||||
GPEC = (1 << pin); //Disable
|
||||
GPC(pin) = (GPC(pin) & (0xF << GPCI)) | (1 << GPCD); //SOURCE(GPIO) | DRIVER(OPEN_DRAIN) | INT_TYPE(UNCHANGED) | WAKEUP_ENABLE(DISABLED)
|
||||
if(mode == INPUT_PULLUP) {
|
||||
GPF(pin) |= (1 << GPFPU); // Enable Pullup
|
||||
}
|
||||
} else if(mode == WAKEUP_PULLUP || mode == WAKEUP_PULLDOWN){
|
||||
GPF(pin) = GPFFS(GPFFS_GPIO(pin));//Set mode to GPIO
|
||||
GPEC = (1 << pin); //Disable
|
||||
if(mode == WAKEUP_PULLUP) {
|
||||
GPF(pin) |= (1 << GPFPU); // Enable Pullup
|
||||
GPC(pin) = (1 << GPCD) | (4 << GPCI) | (1 << GPCWE); //SOURCE(GPIO) | DRIVER(OPEN_DRAIN) | INT_TYPE(LOW) | WAKEUP_ENABLE(ENABLED)
|
||||
} else {
|
||||
GPF(pin) |= (1 << GPFPD); // Enable Pulldown
|
||||
GPC(pin) = (1 << GPCD) | (5 << GPCI) | (1 << GPCWE); //SOURCE(GPIO) | DRIVER(OPEN_DRAIN) | INT_TYPE(HIGH) | WAKEUP_ENABLE(ENABLED)
|
||||
}
|
||||
}
|
||||
|
||||
extern void ICACHE_RAM_ATTR __digitalWrite(uint8_t pin, uint8_t val)
|
||||
{
|
||||
stopWaveform(pin);
|
||||
if (pin < 16)
|
||||
{
|
||||
if (val)
|
||||
{
|
||||
GPOS = (1 << pin);
|
||||
}
|
||||
else
|
||||
{
|
||||
GPOC = (1 << pin);
|
||||
}
|
||||
}
|
||||
else if (pin == 16)
|
||||
{
|
||||
if (val)
|
||||
{
|
||||
GP16O |= 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
GP16O &= ~1;
|
||||
}
|
||||
}
|
||||
} else if(pin == 16){
|
||||
GPF16 = GP16FFS(GPFFS_GPIO(pin));//Set mode to GPIO
|
||||
GPC16 = 0;
|
||||
if(mode == INPUT || mode == INPUT_PULLDOWN_16){
|
||||
if(mode == INPUT_PULLDOWN_16){
|
||||
GPF16 |= (1 << GP16FPD);//Enable Pulldown
|
||||
}
|
||||
GP16E &= ~1;
|
||||
} else if(mode == OUTPUT){
|
||||
GP16E |= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern int ICACHE_RAM_ATTR __digitalRead(uint8_t pin)
|
||||
{
|
||||
if (pin < 16)
|
||||
{
|
||||
return GPIP(pin);
|
||||
}
|
||||
else if (pin == 16)
|
||||
{
|
||||
return GP16I & 0x01;
|
||||
}
|
||||
return 0;
|
||||
extern void ICACHE_RAM_ATTR __digitalWrite(uint8_t pin, uint8_t val) {
|
||||
stopWaveform(pin);
|
||||
if(pin < 16){
|
||||
if(val) GPOS = (1 << pin);
|
||||
else GPOC = (1 << pin);
|
||||
} else if(pin == 16){
|
||||
if(val) GP16O |= 1;
|
||||
else GP16O &= ~1;
|
||||
}
|
||||
}
|
||||
|
||||
extern int ICACHE_RAM_ATTR __digitalRead(uint8_t pin) {
|
||||
if(pin < 16){
|
||||
return GPIP(pin);
|
||||
} else if(pin == 16){
|
||||
return GP16I & 0x01;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
GPIO INTERRUPTS
|
||||
*/
|
||||
|
||||
typedef void (*voidFuncPtr)(void);
|
||||
typedef void (*voidFuncPtrArg)(void*);
|
||||
|
||||
typedef struct {
|
||||
uint8_t mode;
|
||||
void (*fn)(void);
|
||||
void * arg;
|
||||
} interrupt_handler_t;
|
||||
|
||||
//duplicate from functionalInterrupt.h keep in sync
|
||||
typedef struct InterruptInfo {
|
||||
uint8_t pin;
|
||||
uint8_t value;
|
||||
uint32_t micro;
|
||||
} InterruptInfo;
|
||||
|
||||
typedef struct {
|
||||
InterruptInfo* interruptInfo;
|
||||
void* functionInfo;
|
||||
} ArgStructure;
|
||||
|
||||
static interrupt_handler_t interrupt_handlers[16];
|
||||
static uint32_t interrupt_reg = 0;
|
||||
|
||||
void ICACHE_RAM_ATTR interrupt_handler(void *arg) {
|
||||
(void) arg;
|
||||
uint32_t status = GPIE;
|
||||
GPIEC = status;//clear them interrupts
|
||||
uint32_t levels = GPI;
|
||||
if(status == 0 || interrupt_reg == 0) return;
|
||||
ETS_GPIO_INTR_DISABLE();
|
||||
int i = 0;
|
||||
uint32_t changedbits = status & interrupt_reg;
|
||||
while(changedbits){
|
||||
while(!(changedbits & (1 << i))) i++;
|
||||
changedbits &= ~(1 << i);
|
||||
interrupt_handler_t *handler = &interrupt_handlers[i];
|
||||
if (handler->fn &&
|
||||
(handler->mode == CHANGE ||
|
||||
(handler->mode & 1) == !!(levels & (1 << i)))) {
|
||||
// to make ISR compatible to Arduino AVR model where interrupts are disabled
|
||||
// we disable them before we call the client ISR
|
||||
uint32_t savedPS = xt_rsil(15); // stop other interrupts
|
||||
ArgStructure* localArg = (ArgStructure*)handler->arg;
|
||||
if (localArg && localArg->interruptInfo)
|
||||
{
|
||||
localArg->interruptInfo->pin = i;
|
||||
localArg->interruptInfo->value = __digitalRead(i);
|
||||
localArg->interruptInfo->micro = micros();
|
||||
}
|
||||
if (handler->arg)
|
||||
{
|
||||
((voidFuncPtrArg)handler->fn)(handler->arg);
|
||||
}
|
||||
else
|
||||
{
|
||||
handler->fn();
|
||||
}
|
||||
xt_wsr_ps(savedPS);
|
||||
}
|
||||
}
|
||||
ETS_GPIO_INTR_ENABLE();
|
||||
}
|
||||
|
||||
/*
|
||||
GPIO INTERRUPTS
|
||||
*/
|
||||
extern void cleanupFunctional(void* arg);
|
||||
|
||||
typedef void (*voidFuncPtr)(void);
|
||||
typedef void (*voidFuncPtrArg)(void*);
|
||||
extern void ICACHE_RAM_ATTR __attachInterruptArg(uint8_t pin, voidFuncPtr userFunc, void *arg, int mode) {
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t mode;
|
||||
void (*fn)(void);
|
||||
void * arg;
|
||||
} interrupt_handler_t;
|
||||
// #5780
|
||||
// https://github.com/esp8266/esp8266-wiki/wiki/Memory-Map
|
||||
if ((uint32_t)userFunc >= 0x40200000)
|
||||
{
|
||||
// ISR not in IRAM
|
||||
::printf((PGM_P)F("ISR not in IRAM!\r\n"));
|
||||
abort();
|
||||
}
|
||||
|
||||
//duplicate from functionalInterrupt.h keep in sync
|
||||
typedef struct InterruptInfo
|
||||
{
|
||||
uint8_t pin;
|
||||
uint8_t value;
|
||||
uint32_t micro;
|
||||
} InterruptInfo;
|
||||
if(pin < 16) {
|
||||
ETS_GPIO_INTR_DISABLE();
|
||||
interrupt_handler_t *handler = &interrupt_handlers[pin];
|
||||
handler->mode = mode;
|
||||
handler->fn = userFunc;
|
||||
if (handler->arg) // Clean when new attach without detach
|
||||
{
|
||||
cleanupFunctional(handler->arg);
|
||||
}
|
||||
handler->arg = arg;
|
||||
interrupt_reg |= (1 << pin);
|
||||
GPC(pin) &= ~(0xF << GPCI);//INT mode disabled
|
||||
GPIEC = (1 << pin); //Clear Interrupt for this pin
|
||||
GPC(pin) |= ((mode & 0xF) << GPCI);//INT mode "mode"
|
||||
ETS_GPIO_INTR_ATTACH(interrupt_handler, &interrupt_reg);
|
||||
ETS_GPIO_INTR_ENABLE();
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct
|
||||
{
|
||||
InterruptInfo* interruptInfo;
|
||||
void* functionInfo;
|
||||
} ArgStructure;
|
||||
extern void ICACHE_RAM_ATTR __attachInterrupt(uint8_t pin, voidFuncPtr userFunc, int mode )
|
||||
{
|
||||
__attachInterruptArg (pin, userFunc, 0, mode);
|
||||
}
|
||||
|
||||
static interrupt_handler_t interrupt_handlers[16];
|
||||
static uint32_t interrupt_reg = 0;
|
||||
extern void ICACHE_RAM_ATTR __detachInterrupt(uint8_t pin) {
|
||||
if(pin < 16) {
|
||||
ETS_GPIO_INTR_DISABLE();
|
||||
GPC(pin) &= ~(0xF << GPCI);//INT mode disabled
|
||||
GPIEC = (1 << pin); //Clear Interrupt for this pin
|
||||
interrupt_reg &= ~(1 << pin);
|
||||
interrupt_handler_t *handler = &interrupt_handlers[pin];
|
||||
handler->mode = 0;
|
||||
handler->fn = 0;
|
||||
if (handler->arg)
|
||||
{
|
||||
cleanupFunctional(handler->arg);
|
||||
}
|
||||
handler->arg = 0;
|
||||
if (interrupt_reg)
|
||||
ETS_GPIO_INTR_ENABLE();
|
||||
}
|
||||
}
|
||||
|
||||
void ICACHE_RAM_ATTR interrupt_handler(void *arg)
|
||||
{
|
||||
(void) arg;
|
||||
uint32_t status = GPIE;
|
||||
GPIEC = status;//clear them interrupts
|
||||
uint32_t levels = GPI;
|
||||
if (status == 0 || interrupt_reg == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
ETS_GPIO_INTR_DISABLE();
|
||||
int i = 0;
|
||||
uint32_t changedbits = status & interrupt_reg;
|
||||
while (changedbits)
|
||||
{
|
||||
while (!(changedbits & (1 << i)))
|
||||
{
|
||||
i++;
|
||||
}
|
||||
changedbits &= ~(1 << i);
|
||||
interrupt_handler_t *handler = &interrupt_handlers[i];
|
||||
if (handler->fn &&
|
||||
(handler->mode == CHANGE ||
|
||||
(handler->mode & 1) == !!(levels & (1 << i))))
|
||||
{
|
||||
// to make ISR compatible to Arduino AVR model where interrupts are disabled
|
||||
// we disable them before we call the client ISR
|
||||
uint32_t savedPS = xt_rsil(15); // stop other interrupts
|
||||
ArgStructure* localArg = (ArgStructure*)handler->arg;
|
||||
if (localArg && localArg->interruptInfo)
|
||||
{
|
||||
localArg->interruptInfo->pin = i;
|
||||
localArg->interruptInfo->value = __digitalRead(i);
|
||||
localArg->interruptInfo->micro = micros();
|
||||
}
|
||||
if (handler->arg)
|
||||
{
|
||||
((voidFuncPtrArg)handler->fn)(handler->arg);
|
||||
}
|
||||
else
|
||||
{
|
||||
handler->fn();
|
||||
}
|
||||
xt_wsr_ps(savedPS);
|
||||
}
|
||||
}
|
||||
ETS_GPIO_INTR_ENABLE();
|
||||
}
|
||||
void initPins() {
|
||||
//Disable UART interrupts
|
||||
system_set_os_print(0);
|
||||
U0IE = 0;
|
||||
U1IE = 0;
|
||||
|
||||
extern void cleanupFunctional(void* arg);
|
||||
for (int i = 0; i <= 5; ++i) {
|
||||
pinMode(i, INPUT);
|
||||
}
|
||||
// pins 6-11 are used for the SPI flash interface
|
||||
for (int i = 12; i <= 16; ++i) {
|
||||
pinMode(i, INPUT);
|
||||
}
|
||||
}
|
||||
|
||||
extern void ICACHE_RAM_ATTR __attachInterruptArg(uint8_t pin, voidFuncPtr userFunc, void *arg, int mode)
|
||||
{
|
||||
|
||||
// #5780
|
||||
// https://github.com/esp8266/esp8266-wiki/wiki/Memory-Map
|
||||
if ((uint32_t)userFunc >= 0x40200000)
|
||||
{
|
||||
// ISR not in IRAM
|
||||
::printf((PGM_P)F("ISR not in IRAM!\r\n"));
|
||||
abort();
|
||||
}
|
||||
|
||||
if (pin < 16)
|
||||
{
|
||||
ETS_GPIO_INTR_DISABLE();
|
||||
interrupt_handler_t *handler = &interrupt_handlers[pin];
|
||||
handler->mode = mode;
|
||||
handler->fn = userFunc;
|
||||
if (handler->arg) // Clean when new attach without detach
|
||||
{
|
||||
cleanupFunctional(handler->arg);
|
||||
}
|
||||
handler->arg = arg;
|
||||
interrupt_reg |= (1 << pin);
|
||||
GPC(pin) &= ~(0xF << GPCI);//INT mode disabled
|
||||
GPIEC = (1 << pin); //Clear Interrupt for this pin
|
||||
GPC(pin) |= ((mode & 0xF) << GPCI);//INT mode "mode"
|
||||
ETS_GPIO_INTR_ATTACH(interrupt_handler, &interrupt_reg);
|
||||
ETS_GPIO_INTR_ENABLE();
|
||||
}
|
||||
}
|
||||
|
||||
extern void ICACHE_RAM_ATTR __attachInterrupt(uint8_t pin, voidFuncPtr userFunc, int mode)
|
||||
{
|
||||
__attachInterruptArg(pin, userFunc, 0, mode);
|
||||
}
|
||||
|
||||
extern void ICACHE_RAM_ATTR __detachInterrupt(uint8_t pin)
|
||||
{
|
||||
if (pin < 16)
|
||||
{
|
||||
ETS_GPIO_INTR_DISABLE();
|
||||
GPC(pin) &= ~(0xF << GPCI);//INT mode disabled
|
||||
GPIEC = (1 << pin); //Clear Interrupt for this pin
|
||||
interrupt_reg &= ~(1 << pin);
|
||||
interrupt_handler_t *handler = &interrupt_handlers[pin];
|
||||
handler->mode = 0;
|
||||
handler->fn = 0;
|
||||
if (handler->arg)
|
||||
{
|
||||
cleanupFunctional(handler->arg);
|
||||
}
|
||||
handler->arg = 0;
|
||||
if (interrupt_reg)
|
||||
{
|
||||
ETS_GPIO_INTR_ENABLE();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void initPins()
|
||||
{
|
||||
//Disable UART interrupts
|
||||
system_set_os_print(0);
|
||||
U0IE = 0;
|
||||
U1IE = 0;
|
||||
|
||||
for (int i = 0; i <= 5; ++i)
|
||||
{
|
||||
pinMode(i, INPUT);
|
||||
}
|
||||
// pins 6-11 are used for the SPI flash interface
|
||||
for (int i = 12; i <= 16; ++i)
|
||||
{
|
||||
pinMode(i, INPUT);
|
||||
}
|
||||
}
|
||||
|
||||
extern void pinMode(uint8_t pin, uint8_t mode) __attribute__((weak, alias("__pinMode")));
|
||||
extern void digitalWrite(uint8_t pin, uint8_t val) __attribute__((weak, alias("__digitalWrite")));
|
||||
extern int digitalRead(uint8_t pin) __attribute__((weak, alias("__digitalRead")));
|
||||
extern void attachInterrupt(uint8_t pin, voidFuncPtr handler, int mode) __attribute__((weak, alias("__attachInterrupt")));
|
||||
extern void detachInterrupt(uint8_t pin) __attribute__((weak, alias("__detachInterrupt")));
|
||||
extern void pinMode(uint8_t pin, uint8_t mode) __attribute__ ((weak, alias("__pinMode")));
|
||||
extern void digitalWrite(uint8_t pin, uint8_t val) __attribute__ ((weak, alias("__digitalWrite")));
|
||||
extern int digitalRead(uint8_t pin) __attribute__ ((weak, alias("__digitalRead")));
|
||||
extern void attachInterrupt(uint8_t pin, voidFuncPtr handler, int mode) __attribute__ ((weak, alias("__attachInterrupt")));
|
||||
extern void detachInterrupt(uint8_t pin) __attribute__ ((weak, alias("__detachInterrupt")));
|
||||
|
||||
};
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
pulse.c - wiring pulseIn implementation for esp8266
|
||||
pulse.c - wiring pulseIn implementation for esp8266
|
||||
|
||||
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2015 Hristo Gochkov. 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 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.
|
||||
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
|
||||
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
|
||||
*/
|
||||
#include <limits.h>
|
||||
#include "wiring_private.h"
|
||||
@ -24,7 +24,7 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
extern uint32_t xthal_get_ccount();
|
||||
extern uint32_t xthal_get_ccount();
|
||||
|
||||
#define WAIT_FOR_PIN_STATE(state) \
|
||||
while (digitalRead(pin) != (state)) { \
|
||||
@ -34,26 +34,25 @@ extern "C" {
|
||||
optimistic_yield(5000); \
|
||||
}
|
||||
|
||||
// max timeout is 27 seconds at 160MHz clock and 54 seconds at 80MHz clock
|
||||
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
|
||||
{
|
||||
const uint32_t max_timeout_us = clockCyclesToMicroseconds(UINT_MAX);
|
||||
if (timeout > max_timeout_us)
|
||||
{
|
||||
timeout = max_timeout_us;
|
||||
}
|
||||
const uint32_t timeout_cycles = microsecondsToClockCycles(timeout);
|
||||
const uint32_t start_cycle_count = xthal_get_ccount();
|
||||
WAIT_FOR_PIN_STATE(!state);
|
||||
WAIT_FOR_PIN_STATE(state);
|
||||
const uint32_t pulse_start_cycle_count = xthal_get_ccount();
|
||||
WAIT_FOR_PIN_STATE(!state);
|
||||
return clockCyclesToMicroseconds(xthal_get_ccount() - pulse_start_cycle_count);
|
||||
// max timeout is 27 seconds at 160MHz clock and 54 seconds at 80MHz clock
|
||||
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
|
||||
{
|
||||
const uint32_t max_timeout_us = clockCyclesToMicroseconds(UINT_MAX);
|
||||
if (timeout > max_timeout_us) {
|
||||
timeout = max_timeout_us;
|
||||
}
|
||||
const uint32_t timeout_cycles = microsecondsToClockCycles(timeout);
|
||||
const uint32_t start_cycle_count = xthal_get_ccount();
|
||||
WAIT_FOR_PIN_STATE(!state);
|
||||
WAIT_FOR_PIN_STATE(state);
|
||||
const uint32_t pulse_start_cycle_count = xthal_get_ccount();
|
||||
WAIT_FOR_PIN_STATE(!state);
|
||||
return clockCyclesToMicroseconds(xthal_get_ccount() - pulse_start_cycle_count);
|
||||
}
|
||||
|
||||
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout)
|
||||
{
|
||||
return pulseIn(pin, state, timeout);
|
||||
}
|
||||
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout)
|
||||
{
|
||||
return pulseIn(pin, state, timeout);
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -1,24 +1,24 @@
|
||||
/*
|
||||
pwm.c - analogWrite implementation for esp8266
|
||||
pwm.c - analogWrite implementation for esp8266
|
||||
|
||||
Use the shared TIMER1 utilities to generate PWM signals
|
||||
Use the shared TIMER1 utilities to generate PWM signals
|
||||
|
||||
Original Copyright (c) 2015 Hristo Gochkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Original Copyright (c) 2015 Hristo Gochkov. 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 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.
|
||||
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
|
||||
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
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
@ -26,75 +26,56 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
static uint32_t analogMap = 0;
|
||||
static int32_t analogScale = PWMRANGE;
|
||||
static uint16_t analogFreq = 1000;
|
||||
static uint32_t analogMap = 0;
|
||||
static int32_t analogScale = PWMRANGE;
|
||||
static uint16_t analogFreq = 1000;
|
||||
|
||||
extern void __analogWriteRange(uint32_t range)
|
||||
{
|
||||
if (range > 0)
|
||||
{
|
||||
analogScale = range;
|
||||
}
|
||||
extern void __analogWriteRange(uint32_t range) {
|
||||
if (range > 0) {
|
||||
analogScale = range;
|
||||
}
|
||||
}
|
||||
|
||||
extern void __analogWriteFreq(uint32_t freq) {
|
||||
if (freq < 100) {
|
||||
analogFreq = 100;
|
||||
} else if (freq > 40000) {
|
||||
analogFreq = 40000;
|
||||
} else {
|
||||
analogFreq = freq;
|
||||
}
|
||||
}
|
||||
|
||||
extern void __analogWrite(uint8_t pin, int val) {
|
||||
if (pin > 16) {
|
||||
return;
|
||||
}
|
||||
uint32_t analogPeriod = 1000000L / analogFreq;
|
||||
if (val < 0) {
|
||||
val = 0;
|
||||
} else if (val > analogScale) {
|
||||
val = analogScale;
|
||||
}
|
||||
|
||||
analogMap &= ~(1 << pin);
|
||||
uint32_t high = (analogPeriod * val) / analogScale;
|
||||
uint32_t low = analogPeriod - high;
|
||||
pinMode(pin, OUTPUT);
|
||||
if (low == 0) {
|
||||
stopWaveform(pin);
|
||||
digitalWrite(pin, HIGH);
|
||||
} else if (high == 0) {
|
||||
stopWaveform(pin);
|
||||
digitalWrite(pin, LOW);
|
||||
} else {
|
||||
if (startWaveform(pin, high, low, 0)) {
|
||||
analogMap |= (1 << pin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern void __analogWriteFreq(uint32_t freq)
|
||||
{
|
||||
if (freq < 100)
|
||||
{
|
||||
analogFreq = 100;
|
||||
}
|
||||
else if (freq > 40000)
|
||||
{
|
||||
analogFreq = 40000;
|
||||
}
|
||||
else
|
||||
{
|
||||
analogFreq = freq;
|
||||
}
|
||||
}
|
||||
|
||||
extern void __analogWrite(uint8_t pin, int val)
|
||||
{
|
||||
if (pin > 16)
|
||||
{
|
||||
return;
|
||||
}
|
||||
uint32_t analogPeriod = 1000000L / analogFreq;
|
||||
if (val < 0)
|
||||
{
|
||||
val = 0;
|
||||
}
|
||||
else if (val > analogScale)
|
||||
{
|
||||
val = analogScale;
|
||||
}
|
||||
|
||||
analogMap &= ~(1 << pin);
|
||||
uint32_t high = (analogPeriod * val) / analogScale;
|
||||
uint32_t low = analogPeriod - high;
|
||||
pinMode(pin, OUTPUT);
|
||||
if (low == 0)
|
||||
{
|
||||
stopWaveform(pin);
|
||||
digitalWrite(pin, HIGH);
|
||||
}
|
||||
else if (high == 0)
|
||||
{
|
||||
stopWaveform(pin);
|
||||
digitalWrite(pin, LOW);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (startWaveform(pin, high, low, 0))
|
||||
{
|
||||
analogMap |= (1 << pin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern void analogWrite(uint8_t pin, int val) __attribute__((weak, alias("__analogWrite")));
|
||||
extern void analogWriteFreq(uint32_t freq) __attribute__((weak, alias("__analogWriteFreq")));
|
||||
extern void analogWriteRange(uint32_t range) __attribute__((weak, alias("__analogWriteRange")));
|
||||
extern void analogWrite(uint8_t pin, int val) __attribute__((weak, alias("__analogWrite")));
|
||||
extern void analogWriteFreq(uint32_t freq) __attribute__((weak, alias("__analogWriteFreq")));
|
||||
extern void analogWriteRange(uint32_t range) __attribute__((weak, alias("__analogWriteRange")));
|
||||
|
||||
};
|
||||
|
@ -1,72 +1,60 @@
|
||||
/*
|
||||
wiring_shift.c - shiftOut() function
|
||||
Part of Arduino - http://www.arduino.cc/
|
||||
wiring_shift.c - shiftOut() function
|
||||
Part of Arduino - http://www.arduino.cc/
|
||||
|
||||
Copyright (c) 2005-2006 David A. Mellis
|
||||
Copyright (c) 2005-2006 David A. Mellis
|
||||
|
||||
Note: file renamed with a core_esp8266_ prefix to simplify linker
|
||||
script rules for moving code into irom0_text section.
|
||||
Note: file renamed with a core_esp8266_ prefix to simplify linker
|
||||
script rules for moving code into irom0_text section.
|
||||
|
||||
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 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.
|
||||
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., 59 Temple Place, Suite 330,
|
||||
Boston, MA 02111-1307 USA
|
||||
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., 59 Temple Place, Suite 330,
|
||||
Boston, MA 02111-1307 USA
|
||||
|
||||
$Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
|
||||
*/
|
||||
$Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
|
||||
*/
|
||||
|
||||
#include "wiring_private.h"
|
||||
extern "C" {
|
||||
|
||||
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder)
|
||||
{
|
||||
uint8_t value = 0;
|
||||
uint8_t i;
|
||||
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) {
|
||||
uint8_t value = 0;
|
||||
uint8_t i;
|
||||
|
||||
for (i = 0; i < 8; ++i)
|
||||
{
|
||||
digitalWrite(clockPin, HIGH);
|
||||
if (bitOrder == LSBFIRST)
|
||||
{
|
||||
value |= digitalRead(dataPin) << i;
|
||||
}
|
||||
else
|
||||
{
|
||||
value |= digitalRead(dataPin) << (7 - i);
|
||||
}
|
||||
digitalWrite(clockPin, LOW);
|
||||
}
|
||||
return value;
|
||||
for(i = 0; i < 8; ++i) {
|
||||
digitalWrite(clockPin, HIGH);
|
||||
if(bitOrder == LSBFIRST)
|
||||
value |= digitalRead(dataPin) << i;
|
||||
else
|
||||
value |= digitalRead(dataPin) << (7 - i);
|
||||
digitalWrite(clockPin, LOW);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
|
||||
{
|
||||
uint8_t i;
|
||||
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val) {
|
||||
uint8_t i;
|
||||
|
||||
for (i = 0; i < 8; i++)
|
||||
{
|
||||
if (bitOrder == LSBFIRST)
|
||||
{
|
||||
digitalWrite(dataPin, !!(val & (1 << i)));
|
||||
}
|
||||
else
|
||||
{
|
||||
digitalWrite(dataPin, !!(val & (1 << (7 - i))));
|
||||
}
|
||||
for(i = 0; i < 8; i++) {
|
||||
if(bitOrder == LSBFIRST)
|
||||
digitalWrite(dataPin, !!(val & (1 << i)));
|
||||
else
|
||||
digitalWrite(dataPin, !!(val & (1 << (7 - i))));
|
||||
|
||||
digitalWrite(clockPin, HIGH);
|
||||
digitalWrite(clockPin, LOW);
|
||||
}
|
||||
digitalWrite(clockPin, HIGH);
|
||||
digitalWrite(clockPin, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -15,11 +15,11 @@ extern bool timeshift64_is_set;
|
||||
|
||||
void esp_yield();
|
||||
void esp_schedule();
|
||||
void tune_timeshift64(uint64_t now_us);
|
||||
void settimeofday_cb(void (*cb)(void));
|
||||
void disable_extra4k_at_link_time(void) __attribute__((noinline));
|
||||
void tune_timeshift64 (uint64_t now_us);
|
||||
void settimeofday_cb (void (*cb)(void));
|
||||
void disable_extra4k_at_link_time (void) __attribute__((noinline));
|
||||
|
||||
uint32_t sqrt32(uint32_t n);
|
||||
uint32_t sqrt32 (uint32_t n);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -1,36 +1,33 @@
|
||||
/*
|
||||
debug.cpp - debug helper functions
|
||||
Copyright (c) 2015 Markus Sattler. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
debug.cpp - debug helper functions
|
||||
Copyright (c) 2015 Markus Sattler. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "debug.h"
|
||||
|
||||
void ICACHE_RAM_ATTR hexdump(const void *mem, uint32_t len, uint8_t cols)
|
||||
{
|
||||
void ICACHE_RAM_ATTR hexdump(const void *mem, uint32_t len, uint8_t cols) {
|
||||
const uint8_t* src = (const uint8_t*) mem;
|
||||
os_printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len);
|
||||
for (uint32_t i = 0; i < len; i++)
|
||||
{
|
||||
if (i % cols == 0)
|
||||
{
|
||||
for(uint32_t i = 0; i < len; i++) {
|
||||
if(i % cols == 0) {
|
||||
os_printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i);
|
||||
yield();
|
||||
yield();
|
||||
}
|
||||
os_printf("%02X ", *src);
|
||||
src++;
|
||||
|
@ -9,8 +9,7 @@ extern "C" {
|
||||
|
||||
#define RTC_MEM ((volatile uint32_t*)0x60001200)
|
||||
|
||||
enum action_t
|
||||
{
|
||||
enum action_t {
|
||||
ACTION_COPY_RAW = 0x00000001,
|
||||
ACTION_LOAD_APP = 0xffffffff
|
||||
};
|
||||
@ -18,8 +17,7 @@ enum action_t
|
||||
#define EBOOT_MAGIC 0xeb001000
|
||||
#define EBOOT_MAGIC_MASK 0xfffff000
|
||||
|
||||
struct eboot_command
|
||||
{
|
||||
struct eboot_command {
|
||||
uint32_t magic;
|
||||
enum action_t action;
|
||||
uint32_t args[29];
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
esp8266_peri.h - Peripheral registers exposed in more AVR style for esp8266
|
||||
esp8266_peri.h - Peripheral registers exposed in more AVR style for esp8266
|
||||
|
||||
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2015 Hristo Gochkov. 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 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.
|
||||
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
|
||||
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
|
||||
*/
|
||||
#ifndef ESP8266_PERI_H_INCLUDED
|
||||
#define ESP8266_PERI_H_INCLUDED
|
||||
@ -842,8 +842,8 @@ extern uint8_t esp8266_gpioToFn[16];
|
||||
#define I2STXCM (0) //I2S_TX_CHAN_MOD_S
|
||||
|
||||
/**
|
||||
Random Number Generator 32bit
|
||||
http://esp8266-re.foogod.com/wiki/Random_Number_Generator
|
||||
Random Number Generator 32bit
|
||||
http://esp8266-re.foogod.com/wiki/Random_Number_Generator
|
||||
**/
|
||||
#define RANDOM_REG32 ESP8266_DREG(0x20E44)
|
||||
|
||||
|
@ -1,20 +1,20 @@
|
||||
/*
|
||||
flash_utils.h - Flash access function and data structures
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All right reserved.
|
||||
flash_utils.h - Flash access function and data structures
|
||||
Copyright (c) 2015 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
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
|
||||
*/
|
||||
|
||||
|
||||
@ -26,10 +26,10 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Definitions are placed in eboot. Include them here rather than duplicate them.
|
||||
Also, prefer to have eboot standalone as much as possible and have the core depend on it
|
||||
rather than have eboot depend on the core.
|
||||
*/
|
||||
/* Definitions are placed in eboot. Include them here rather than duplicate them.
|
||||
* Also, prefer to have eboot standalone as much as possible and have the core depend on it
|
||||
* rather than have eboot depend on the core.
|
||||
*/
|
||||
#include <../../bootloaders/eboot/flash.h>
|
||||
|
||||
|
||||
|
@ -1,44 +1,44 @@
|
||||
/*
|
||||
gdb_hooks.c - Default (no-op) hooks for GDB Stub library
|
||||
Copyright (c) 2018 Ivan Grokhotkov. All right reserved.
|
||||
gdb_hooks.c - Default (no-op) hooks for GDB Stub library
|
||||
Copyright (c) 2018 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
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
|
||||
*/
|
||||
|
||||
#include "ets_sys.h"
|
||||
#include "gdb_hooks.h"
|
||||
|
||||
|
||||
/* gdb_init and gdb_do_break do not return anything, but since the return
|
||||
value is in register, it doesn't hurt to return a bool, so that the
|
||||
same stub can be used for gdb_present. */
|
||||
/* gdb_init and gdb_do_break do not return anything, but since the return
|
||||
value is in register, it doesn't hurt to return a bool, so that the
|
||||
same stub can be used for gdb_present. */
|
||||
extern "C" {
|
||||
|
||||
static bool ICACHE_RAM_ATTR __gdb_no_op()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
static bool ICACHE_RAM_ATTR __gdb_no_op()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void gdb_init(void) __attribute__((weak, alias("__gdb_no_op")));
|
||||
void gdb_do_break(void) __attribute__((weak, alias("__gdb_no_op")));
|
||||
bool gdb_present(void) __attribute__((weak, alias("__gdb_no_op")));
|
||||
bool gdbstub_has_putc1_control(void) __attribute__((weak, alias("__gdb_no_op")));
|
||||
void gdbstub_set_putc1_callback(void (*func)(char)) __attribute__((weak, alias("__gdb_no_op")));
|
||||
bool gdbstub_has_uart_isr_control(void) __attribute__((weak, alias("__gdb_no_op")));
|
||||
void gdbstub_set_uart_isr_callback(void (*func)(void*, uint8_t), void* arg) __attribute__((weak, alias("__gdb_no_op")));
|
||||
void gdbstub_write_char(char c) __attribute__((weak, alias("__gdb_no_op")));
|
||||
void gdbstub_write(const char* buf, size_t size) __attribute__((weak, alias("__gdb_no_op")));
|
||||
void gdb_init(void) __attribute__ ((weak, alias("__gdb_no_op")));
|
||||
void gdb_do_break(void) __attribute__ ((weak, alias("__gdb_no_op")));
|
||||
bool gdb_present(void) __attribute__ ((weak, alias("__gdb_no_op")));
|
||||
bool gdbstub_has_putc1_control(void) __attribute__ ((weak, alias("__gdb_no_op")));
|
||||
void gdbstub_set_putc1_callback(void (*func)(char)) __attribute__ ((weak, alias("__gdb_no_op")));
|
||||
bool gdbstub_has_uart_isr_control(void) __attribute__ ((weak, alias("__gdb_no_op")));
|
||||
void gdbstub_set_uart_isr_callback(void (*func)(void*, uint8_t), void* arg) __attribute__ ((weak, alias("__gdb_no_op")));
|
||||
void gdbstub_write_char(char c) __attribute__ ((weak, alias("__gdb_no_op")));
|
||||
void gdbstub_write(const char* buf, size_t size) __attribute__ ((weak, alias("__gdb_no_op")));
|
||||
|
||||
};
|
||||
|
@ -1,20 +1,20 @@
|
||||
/*
|
||||
gdb_hooks.h - Hooks for GDB Stub library
|
||||
Copyright (c) 2018 Ivan Grokhotkov. All right reserved.
|
||||
gdb_hooks.h - Hooks for GDB Stub library
|
||||
Copyright (c) 2018 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
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
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
@ -24,97 +24,97 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
@brief Initialize GDB stub, if present
|
||||
|
||||
By default, this function is a no-op. When GDBStub library is linked,
|
||||
this function is overriden and does necessary initialization of that library.
|
||||
Called early at startup.
|
||||
*/
|
||||
* @brief Initialize GDB stub, if present
|
||||
*
|
||||
* By default, this function is a no-op. When GDBStub library is linked,
|
||||
* this function is overriden and does necessary initialization of that library.
|
||||
* Called early at startup.
|
||||
*/
|
||||
void gdb_init(void);
|
||||
|
||||
/**
|
||||
@brief Break into GDB, if present
|
||||
|
||||
By default, this function is a no-op. When GDBStub library is linked,
|
||||
this function is overriden and triggers entry into the debugger, which
|
||||
looks like a breakpoint hit.
|
||||
*/
|
||||
* @brief Break into GDB, if present
|
||||
*
|
||||
* By default, this function is a no-op. When GDBStub library is linked,
|
||||
* this function is overriden and triggers entry into the debugger, which
|
||||
* looks like a breakpoint hit.
|
||||
*/
|
||||
void gdb_do_break(void);
|
||||
|
||||
/**
|
||||
@brief Check if GDB stub is present.
|
||||
|
||||
By default, this function returns false. When GDBStub library is linked,
|
||||
this function is overriden and returns true. Can be used to check whether
|
||||
GDB is used.
|
||||
|
||||
@return true if GDB stub is present
|
||||
*/
|
||||
* @brief Check if GDB stub is present.
|
||||
*
|
||||
* By default, this function returns false. When GDBStub library is linked,
|
||||
* this function is overriden and returns true. Can be used to check whether
|
||||
* GDB is used.
|
||||
*
|
||||
* @return true if GDB stub is present
|
||||
*/
|
||||
bool gdb_present(void);
|
||||
|
||||
// If gdbstub has these set true, then we will disable our own
|
||||
// usage of them, but use gdbstub's callbacks for them instead
|
||||
/**
|
||||
@brief Check if GDB is installing a putc1 callback.
|
||||
|
||||
By default, this function returns false. When GDBStub library is linked,
|
||||
this function is overriden and returns true.
|
||||
|
||||
@return true if GDB is installing a putc1 callback
|
||||
*/
|
||||
* @brief Check if GDB is installing a putc1 callback.
|
||||
*
|
||||
* By default, this function returns false. When GDBStub library is linked,
|
||||
* this function is overriden and returns true.
|
||||
*
|
||||
* @return true if GDB is installing a putc1 callback
|
||||
*/
|
||||
bool gdbstub_has_putc1_control(void);
|
||||
|
||||
/**
|
||||
@brief Register a putc1 callback with GDB.
|
||||
@param func function GDB will proxy putc1 data to
|
||||
|
||||
By default, this function is a no-op. When GDBStub library is linked,
|
||||
this function is overriden and sets GDB stub's secondary putc1 callback to
|
||||
func. When GDB stub is linked, but a GDB session is not current attached,
|
||||
then GDB stub will pass putc1 chars directly to this function.
|
||||
*/
|
||||
* @brief Register a putc1 callback with GDB.
|
||||
* @param func function GDB will proxy putc1 data to
|
||||
*
|
||||
* By default, this function is a no-op. When GDBStub library is linked,
|
||||
* this function is overriden and sets GDB stub's secondary putc1 callback to
|
||||
* func. When GDB stub is linked, but a GDB session is not current attached,
|
||||
* then GDB stub will pass putc1 chars directly to this function.
|
||||
*/
|
||||
void gdbstub_set_putc1_callback(void (*func)(char));
|
||||
|
||||
/**
|
||||
@brief Check if GDB is installing a uart0 isr callback.
|
||||
|
||||
By default, this function returns false. When GDBStub library is linked,
|
||||
this function is overriden and returns true.
|
||||
|
||||
@return true if GDB is installing a uart0 isr callback
|
||||
*/
|
||||
* @brief Check if GDB is installing a uart0 isr callback.
|
||||
*
|
||||
* By default, this function returns false. When GDBStub library is linked,
|
||||
* this function is overriden and returns true.
|
||||
*
|
||||
* @return true if GDB is installing a uart0 isr callback
|
||||
*/
|
||||
bool gdbstub_has_uart_isr_control(void);
|
||||
|
||||
/**
|
||||
@brief Register a uart0 isr callback with GDB.
|
||||
@param func function GDB will proxy uart0 isr data to
|
||||
|
||||
By default, this function is a no-op. When GDBStub library is linked,
|
||||
this function is overriden and sets GDB stub's secondary uart0 isr callback
|
||||
to func. When GDB stub is linked, but a GDB session is not current attached,
|
||||
then GDB stub will pass uart0 isr data back to this function.
|
||||
*/
|
||||
* @brief Register a uart0 isr callback with GDB.
|
||||
* @param func function GDB will proxy uart0 isr data to
|
||||
*
|
||||
* By default, this function is a no-op. When GDBStub library is linked,
|
||||
* this function is overriden and sets GDB stub's secondary uart0 isr callback
|
||||
* to func. When GDB stub is linked, but a GDB session is not current attached,
|
||||
* then GDB stub will pass uart0 isr data back to this function.
|
||||
*/
|
||||
void gdbstub_set_uart_isr_callback(void (*func)(void*, uint8_t), void* arg);
|
||||
|
||||
/**
|
||||
@brief Write a character for output to a GDB session on uart0.
|
||||
@param c character to write
|
||||
|
||||
By default, this function is a no-op. When GDBStub library is linked,
|
||||
this function is overriden and writes a char to either the GDB session on
|
||||
uart0 or directly to uart0 if not GDB session is attached.
|
||||
*/
|
||||
* @brief Write a character for output to a GDB session on uart0.
|
||||
* @param c character to write
|
||||
*
|
||||
* By default, this function is a no-op. When GDBStub library is linked,
|
||||
* this function is overriden and writes a char to either the GDB session on
|
||||
* uart0 or directly to uart0 if not GDB session is attached.
|
||||
*/
|
||||
void gdbstub_write_char(char c);
|
||||
|
||||
/**
|
||||
@brief Write a char buffer for output to a GDB session on uart0.
|
||||
@param buf buffer of data to write
|
||||
@param size length of buffer
|
||||
|
||||
By default, this function is a no-op. When GDBStub library is linked,
|
||||
this function is overriden and writes a buffer to either the GDB session on
|
||||
uart0 or directly to uart0 if not GDB session is attached.
|
||||
*/
|
||||
* @brief Write a char buffer for output to a GDB session on uart0.
|
||||
* @param buf buffer of data to write
|
||||
* @param size length of buffer
|
||||
*
|
||||
* By default, this function is a no-op. When GDBStub library is linked,
|
||||
* this function is overriden and writes a buffer to either the GDB session on
|
||||
* uart0 or directly to uart0 if not GDB session is attached.
|
||||
*/
|
||||
void gdbstub_write(const char* buf, size_t size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -1,7 +1,7 @@
|
||||
/* heap.c - overrides of SDK heap handling functions
|
||||
Copyright (c) 2016 Ivan Grokhotkov. All rights reserved.
|
||||
This file is distributed under MIT license.
|
||||
*/
|
||||
/* heap.c - overrides of SDK heap handling functions
|
||||
* Copyright (c) 2016 Ivan Grokhotkov. All rights reserved.
|
||||
* This file is distributed under MIT license.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "umm_malloc/umm_malloc.h"
|
||||
@ -10,201 +10,186 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
// Debugging helper, last allocation which returned NULL
|
||||
void *umm_last_fail_alloc_addr = NULL;
|
||||
int umm_last_fail_alloc_size = 0;
|
||||
// Debugging helper, last allocation which returned NULL
|
||||
void *umm_last_fail_alloc_addr = NULL;
|
||||
int umm_last_fail_alloc_size = 0;
|
||||
|
||||
void* _malloc_r(struct _reent* unused, size_t size)
|
||||
{
|
||||
(void) unused;
|
||||
void *ret = malloc(size);
|
||||
if (0 != size && 0 == ret)
|
||||
{
|
||||
umm_last_fail_alloc_addr = __builtin_return_address(0);
|
||||
umm_last_fail_alloc_size = size;
|
||||
}
|
||||
return ret;
|
||||
void* _malloc_r(struct _reent* unused, size_t size)
|
||||
{
|
||||
(void) unused;
|
||||
void *ret = malloc(size);
|
||||
if (0 != size && 0 == ret) {
|
||||
umm_last_fail_alloc_addr = __builtin_return_address(0);
|
||||
umm_last_fail_alloc_size = size;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void _free_r(struct _reent* unused, void* ptr)
|
||||
{
|
||||
(void) unused;
|
||||
return free(ptr);
|
||||
}
|
||||
void _free_r(struct _reent* unused, void* ptr)
|
||||
{
|
||||
(void) unused;
|
||||
return free(ptr);
|
||||
}
|
||||
|
||||
void* _realloc_r(struct _reent* unused, void* ptr, size_t size)
|
||||
{
|
||||
(void) unused;
|
||||
void *ret = realloc(ptr, size);
|
||||
if (0 != size && 0 == ret)
|
||||
{
|
||||
umm_last_fail_alloc_addr = __builtin_return_address(0);
|
||||
umm_last_fail_alloc_size = size;
|
||||
}
|
||||
return ret;
|
||||
void* _realloc_r(struct _reent* unused, void* ptr, size_t size)
|
||||
{
|
||||
(void) unused;
|
||||
void *ret = realloc(ptr, size);
|
||||
if (0 != size && 0 == ret) {
|
||||
umm_last_fail_alloc_addr = __builtin_return_address(0);
|
||||
umm_last_fail_alloc_size = size;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void* _calloc_r(struct _reent* unused, size_t count, size_t size)
|
||||
{
|
||||
(void) unused;
|
||||
void *ret = calloc(count, size);
|
||||
if (0 != (count * size) && 0 == ret)
|
||||
{
|
||||
umm_last_fail_alloc_addr = __builtin_return_address(0);
|
||||
umm_last_fail_alloc_size = count * size;
|
||||
}
|
||||
return ret;
|
||||
void* _calloc_r(struct _reent* unused, size_t count, size_t size)
|
||||
{
|
||||
(void) unused;
|
||||
void *ret = calloc(count, size);
|
||||
if (0 != (count * size) && 0 == ret) {
|
||||
umm_last_fail_alloc_addr = __builtin_return_address(0);
|
||||
umm_last_fail_alloc_size = count * size;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ICACHE_RAM_ATTR vPortFree(void *ptr, const char* file, int line)
|
||||
{
|
||||
(void) file;
|
||||
(void) line;
|
||||
free(ptr);
|
||||
}
|
||||
void ICACHE_RAM_ATTR vPortFree(void *ptr, const char* file, int line)
|
||||
{
|
||||
(void) file;
|
||||
(void) line;
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
#ifdef DEBUG_ESP_OOM
|
||||
|
||||
void* ICACHE_RAM_ATTR pvPortMalloc(size_t size, const char* file, int line)
|
||||
{
|
||||
return malloc_loc(size, file, line);
|
||||
}
|
||||
void* ICACHE_RAM_ATTR pvPortMalloc(size_t size, const char* file, int line)
|
||||
{
|
||||
return malloc_loc(size, file, line);
|
||||
}
|
||||
|
||||
void* ICACHE_RAM_ATTR pvPortCalloc(size_t count, size_t size, const char* file, int line)
|
||||
{
|
||||
return calloc_loc(count, size, file, line);
|
||||
}
|
||||
void* ICACHE_RAM_ATTR pvPortCalloc(size_t count, size_t size, const char* file, int line)
|
||||
{
|
||||
return calloc_loc(count, size, file, line);
|
||||
}
|
||||
|
||||
void* ICACHE_RAM_ATTR pvPortRealloc(void *ptr, size_t size, const char* file, int line)
|
||||
{
|
||||
return realloc_loc(ptr, size, file, line);
|
||||
}
|
||||
void* ICACHE_RAM_ATTR pvPortRealloc(void *ptr, size_t size, const char* file, int line)
|
||||
{
|
||||
return realloc_loc(ptr, size, file, line);
|
||||
}
|
||||
|
||||
void* ICACHE_RAM_ATTR pvPortZalloc(size_t size, const char* file, int line)
|
||||
{
|
||||
return calloc_loc(1, size, file, line);
|
||||
}
|
||||
void* ICACHE_RAM_ATTR pvPortZalloc(size_t size, const char* file, int line)
|
||||
{
|
||||
return calloc_loc(1, size, file, line);
|
||||
}
|
||||
|
||||
#undef malloc
|
||||
#undef calloc
|
||||
#undef realloc
|
||||
|
||||
static const char oom_fmt[] PROGMEM STORE_ATTR = ":oom(%d)@?\n";
|
||||
static const char oom_fmt_1[] PROGMEM STORE_ATTR = ":oom(%d)@";
|
||||
static const char oom_fmt_2[] PROGMEM STORE_ATTR = ":%d\n";
|
||||
static const char oom_fmt[] PROGMEM STORE_ATTR = ":oom(%d)@?\n";
|
||||
static const char oom_fmt_1[] PROGMEM STORE_ATTR = ":oom(%d)@";
|
||||
static const char oom_fmt_2[] PROGMEM STORE_ATTR = ":%d\n";
|
||||
|
||||
void* malloc(size_t s)
|
||||
{
|
||||
void* ret = umm_malloc(s);
|
||||
if (!ret)
|
||||
{
|
||||
os_printf(oom_fmt, (int)s);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
void* malloc (size_t s)
|
||||
{
|
||||
void* ret = umm_malloc(s);
|
||||
if (!ret)
|
||||
os_printf(oom_fmt, (int)s);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void* calloc(size_t n, size_t s)
|
||||
{
|
||||
void* ret = umm_calloc(n, s);
|
||||
if (!ret)
|
||||
{
|
||||
os_printf(oom_fmt, (int)s);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
void* calloc (size_t n, size_t s)
|
||||
{
|
||||
void* ret = umm_calloc(n, s);
|
||||
if (!ret)
|
||||
os_printf(oom_fmt, (int)s);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void* realloc(void* p, size_t s)
|
||||
{
|
||||
void* ret = umm_realloc(p, s);
|
||||
if (!ret)
|
||||
{
|
||||
os_printf(oom_fmt, (int)s);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
void* realloc (void* p, size_t s)
|
||||
{
|
||||
void* ret = umm_realloc(p, s);
|
||||
if (!ret)
|
||||
os_printf(oom_fmt, (int)s);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void print_loc(size_t s, const char* file, int line)
|
||||
{
|
||||
void print_loc (size_t s, const char* file, int line)
|
||||
{
|
||||
os_printf(oom_fmt_1, (int)s);
|
||||
os_printf(file);
|
||||
os_printf(oom_fmt_2, line);
|
||||
}
|
||||
}
|
||||
|
||||
void* malloc_loc(size_t s, const char* file, int line)
|
||||
{
|
||||
void* ret = umm_malloc(s);
|
||||
if (!ret)
|
||||
{
|
||||
print_loc(s, file, line);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
void* malloc_loc (size_t s, const char* file, int line)
|
||||
{
|
||||
void* ret = umm_malloc(s);
|
||||
if (!ret)
|
||||
print_loc(s, file, line);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void* calloc_loc(size_t n, size_t s, const char* file, int line)
|
||||
{
|
||||
void* ret = umm_calloc(n, s);
|
||||
if (!ret)
|
||||
{
|
||||
print_loc(s, file, line);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
void* calloc_loc (size_t n, size_t s, const char* file, int line)
|
||||
{
|
||||
void* ret = umm_calloc(n, s);
|
||||
if (!ret)
|
||||
print_loc(s, file, line);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void* realloc_loc(void* p, size_t s, const char* file, int line)
|
||||
{
|
||||
void* ret = umm_realloc(p, s);
|
||||
if (!ret)
|
||||
{
|
||||
print_loc(s, file, line);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
void* realloc_loc (void* p, size_t s, const char* file, int line)
|
||||
{
|
||||
void* ret = umm_realloc(p, s);
|
||||
if (!ret)
|
||||
print_loc(s, file, line);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void* ICACHE_RAM_ATTR pvPortMalloc(size_t size, const char* file, int line)
|
||||
{
|
||||
(void) file;
|
||||
(void) line;
|
||||
return malloc(size);
|
||||
}
|
||||
void* ICACHE_RAM_ATTR pvPortMalloc(size_t size, const char* file, int line)
|
||||
{
|
||||
(void) file;
|
||||
(void) line;
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
void* ICACHE_RAM_ATTR pvPortCalloc(size_t count, size_t size, const char* file, int line)
|
||||
{
|
||||
(void) file;
|
||||
(void) line;
|
||||
return calloc(count, size);
|
||||
}
|
||||
void* ICACHE_RAM_ATTR pvPortCalloc(size_t count, size_t size, const char* file, int line)
|
||||
{
|
||||
(void) file;
|
||||
(void) line;
|
||||
return calloc(count, size);
|
||||
}
|
||||
|
||||
void* ICACHE_RAM_ATTR pvPortRealloc(void *ptr, size_t size, const char* file, int line)
|
||||
{
|
||||
(void) file;
|
||||
(void) line;
|
||||
return realloc(ptr, size);
|
||||
}
|
||||
void* ICACHE_RAM_ATTR pvPortRealloc(void *ptr, size_t size, const char* file, int line)
|
||||
{
|
||||
(void) file;
|
||||
(void) line;
|
||||
return realloc(ptr, size);
|
||||
}
|
||||
|
||||
void* ICACHE_RAM_ATTR pvPortZalloc(size_t size, const char* file, int line)
|
||||
{
|
||||
(void) file;
|
||||
(void) line;
|
||||
return calloc(1, size);
|
||||
}
|
||||
void* ICACHE_RAM_ATTR pvPortZalloc(size_t size, const char* file, int line)
|
||||
{
|
||||
(void) file;
|
||||
(void) line;
|
||||
return calloc(1, size);
|
||||
}
|
||||
|
||||
#endif // !defined(DEBUG_ESP_OOM)
|
||||
|
||||
size_t xPortGetFreeHeapSize(void)
|
||||
{
|
||||
return umm_free_heap_size();
|
||||
}
|
||||
size_t xPortGetFreeHeapSize(void)
|
||||
{
|
||||
return umm_free_heap_size();
|
||||
}
|
||||
|
||||
size_t ICACHE_RAM_ATTR xPortWantedSizeAlign(size_t size)
|
||||
{
|
||||
return (size + 3) & ~((size_t) 3);
|
||||
}
|
||||
size_t ICACHE_RAM_ATTR xPortWantedSizeAlign(size_t size)
|
||||
{
|
||||
return (size + 3) & ~((size_t) 3);
|
||||
}
|
||||
|
||||
void system_show_malloc(void)
|
||||
{
|
||||
umm_info(NULL, 1);
|
||||
}
|
||||
void system_show_malloc(void)
|
||||
{
|
||||
umm_info(NULL, 1);
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -1,39 +1,39 @@
|
||||
/*
|
||||
i2s.h - Software I2S library for esp8266
|
||||
i2s.h - Software I2S library for esp8266
|
||||
|
||||
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2015 Hristo Gochkov. 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 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.
|
||||
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
|
||||
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
|
||||
*/
|
||||
#ifndef I2S_h
|
||||
#define I2S_h
|
||||
|
||||
/*
|
||||
How does this work? Basically, to get sound, you need to:
|
||||
- Connect an I2S codec to the I2S pins on the ESP.
|
||||
- Start up a thread that's going to do the sound output
|
||||
- Call i2s_begin()
|
||||
- Call i2s_set_rate() with the sample rate you want.
|
||||
- Generate sound and call i2s_write_sample() with 32-bit samples.
|
||||
The 32bit samples basically are 2 16-bit signed values (the analog values for
|
||||
the left and right channel) concatenated as (Rout<<16)+Lout
|
||||
How does this work? Basically, to get sound, you need to:
|
||||
- Connect an I2S codec to the I2S pins on the ESP.
|
||||
- Start up a thread that's going to do the sound output
|
||||
- Call i2s_begin()
|
||||
- Call i2s_set_rate() with the sample rate you want.
|
||||
- Generate sound and call i2s_write_sample() with 32-bit samples.
|
||||
The 32bit samples basically are 2 16-bit signed values (the analog values for
|
||||
the left and right channel) concatenated as (Rout<<16)+Lout
|
||||
|
||||
i2s_write_sample will block when you're sending data too quickly, so you can just
|
||||
generate and push data as fast as you can and i2s_write_sample will regulate the
|
||||
speed.
|
||||
i2s_write_sample will block when you're sending data too quickly, so you can just
|
||||
generate and push data as fast as you can and i2s_write_sample will regulate the
|
||||
speed.
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
@ -56,8 +56,8 @@ bool i2s_rx_is_full();
|
||||
bool i2s_rx_is_empty();
|
||||
uint16_t i2s_available();// returns the number of samples than can be written before blocking
|
||||
uint16_t i2s_rx_available();// returns the number of samples than can be written before blocking
|
||||
void i2s_set_callback(void (*callback)(void));
|
||||
void i2s_rx_set_callback(void (*callback)(void));
|
||||
void i2s_set_callback(void (*callback) (void));
|
||||
void i2s_rx_set_callback(void (*callback) (void));
|
||||
|
||||
// writes a buffer of frames into the DMA memory, returns the amount of frames written
|
||||
// A frame is just a int16_t for mono, for stereo a frame is two int16_t, one for each channel.
|
||||
|
@ -21,21 +21,17 @@ extern "C" {
|
||||
//}
|
||||
//
|
||||
|
||||
class InterruptLock
|
||||
{
|
||||
class InterruptLock {
|
||||
public:
|
||||
InterruptLock()
|
||||
{
|
||||
InterruptLock() {
|
||||
_state = xt_rsil(15);
|
||||
}
|
||||
|
||||
~InterruptLock()
|
||||
{
|
||||
~InterruptLock() {
|
||||
xt_wsr_ps(_state);
|
||||
}
|
||||
|
||||
uint32_t savedInterruptLevel() const
|
||||
{
|
||||
uint32_t savedInterruptLevel() const {
|
||||
return _state & 0x0f;
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
/*
|
||||
cdecoder.c - c source to a base64 decoding algorithm implementation
|
||||
cdecoder.c - c source to a base64 decoding algorithm implementation
|
||||
|
||||
This is part of the libb64 project, and has been placed in the public domain.
|
||||
For details, see http://sourceforge.net/projects/libb64
|
||||
This is part of the libb64 project, and has been placed in the public domain.
|
||||
For details, see http://sourceforge.net/projects/libb64
|
||||
*/
|
||||
|
||||
#include <pgmspace.h>
|
||||
@ -11,117 +11,94 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
static int base64_decode_value_signed(int8_t value_in)
|
||||
{
|
||||
static const int8_t decoding[] PROGMEM = {62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51};
|
||||
static const int8_t decoding_size = sizeof(decoding);
|
||||
value_in -= 43;
|
||||
if (value_in < 0 || value_in > decoding_size)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return pgm_read_byte(&decoding[(int)value_in]);
|
||||
static int base64_decode_value_signed(int8_t value_in){
|
||||
static const int8_t decoding[] PROGMEM = {62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-2,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51};
|
||||
static const int8_t decoding_size = sizeof(decoding);
|
||||
value_in -= 43;
|
||||
if (value_in < 0 || value_in > decoding_size) return -1;
|
||||
return pgm_read_byte( &decoding[(int)value_in] );
|
||||
}
|
||||
|
||||
void base64_init_decodestate(base64_decodestate* state_in){
|
||||
state_in->step = step_a;
|
||||
state_in->plainchar = 0;
|
||||
}
|
||||
|
||||
static int base64_decode_block_signed(const int8_t* code_in, const int length_in, int8_t* plaintext_out, base64_decodestate* state_in){
|
||||
const int8_t* codechar = code_in;
|
||||
int8_t* plainchar = plaintext_out;
|
||||
int8_t fragment;
|
||||
|
||||
*plainchar = state_in->plainchar;
|
||||
|
||||
switch (state_in->step){
|
||||
while (1){
|
||||
case step_a:
|
||||
do {
|
||||
if (codechar == code_in+length_in){
|
||||
state_in->step = step_a;
|
||||
state_in->plainchar = *plainchar;
|
||||
return plainchar - plaintext_out;
|
||||
}
|
||||
fragment = (int8_t)base64_decode_value_signed(*codechar++);
|
||||
} while (fragment < 0);
|
||||
*plainchar = (fragment & 0x03f) << 2;
|
||||
case step_b:
|
||||
do {
|
||||
if (codechar == code_in+length_in){
|
||||
state_in->step = step_b;
|
||||
state_in->plainchar = *plainchar;
|
||||
return plainchar - plaintext_out;
|
||||
}
|
||||
fragment = (int8_t)base64_decode_value_signed(*codechar++);
|
||||
} while (fragment < 0);
|
||||
*plainchar++ |= (fragment & 0x030) >> 4;
|
||||
*plainchar = (fragment & 0x00f) << 4;
|
||||
case step_c:
|
||||
do {
|
||||
if (codechar == code_in+length_in){
|
||||
state_in->step = step_c;
|
||||
state_in->plainchar = *plainchar;
|
||||
return plainchar - plaintext_out;
|
||||
}
|
||||
fragment = (int8_t)base64_decode_value_signed(*codechar++);
|
||||
} while (fragment < 0);
|
||||
*plainchar++ |= (fragment & 0x03c) >> 2;
|
||||
*plainchar = (fragment & 0x003) << 6;
|
||||
case step_d:
|
||||
do {
|
||||
if (codechar == code_in+length_in){
|
||||
state_in->step = step_d;
|
||||
state_in->plainchar = *plainchar;
|
||||
return plainchar - plaintext_out;
|
||||
}
|
||||
fragment = (int8_t)base64_decode_value_signed(*codechar++);
|
||||
} while (fragment < 0);
|
||||
*plainchar++ |= (fragment & 0x03f);
|
||||
}
|
||||
}
|
||||
/* control should not reach here */
|
||||
return plainchar - plaintext_out;
|
||||
}
|
||||
|
||||
void base64_init_decodestate(base64_decodestate* state_in)
|
||||
{
|
||||
state_in->step = step_a;
|
||||
state_in->plainchar = 0;
|
||||
}
|
||||
static int base64_decode_chars_signed(const int8_t* code_in, const int length_in, int8_t* plaintext_out){
|
||||
base64_decodestate _state;
|
||||
base64_init_decodestate(&_state);
|
||||
int len = base64_decode_block_signed(code_in, length_in, plaintext_out, &_state);
|
||||
if(len > 0) plaintext_out[len] = 0;
|
||||
return len;
|
||||
}
|
||||
|
||||
static int base64_decode_block_signed(const int8_t* code_in, const int length_in, int8_t* plaintext_out, base64_decodestate* state_in)
|
||||
{
|
||||
const int8_t* codechar = code_in;
|
||||
int8_t* plainchar = plaintext_out;
|
||||
int8_t fragment;
|
||||
int base64_decode_value(char value_in){
|
||||
return base64_decode_value_signed(*((int8_t *) &value_in));
|
||||
}
|
||||
|
||||
*plainchar = state_in->plainchar;
|
||||
int base64_decode_block(const char* code_in, const int length_in, char* plaintext_out, base64_decodestate* state_in){
|
||||
return base64_decode_block_signed((int8_t *) code_in, length_in, (int8_t *) plaintext_out, state_in);
|
||||
}
|
||||
|
||||
switch (state_in->step)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
case step_a:
|
||||
do
|
||||
{
|
||||
if (codechar == code_in + length_in)
|
||||
{
|
||||
state_in->step = step_a;
|
||||
state_in->plainchar = *plainchar;
|
||||
return plainchar - plaintext_out;
|
||||
}
|
||||
fragment = (int8_t)base64_decode_value_signed(*codechar++);
|
||||
} while (fragment < 0);
|
||||
*plainchar = (fragment & 0x03f) << 2;
|
||||
case step_b:
|
||||
do
|
||||
{
|
||||
if (codechar == code_in + length_in)
|
||||
{
|
||||
state_in->step = step_b;
|
||||
state_in->plainchar = *plainchar;
|
||||
return plainchar - plaintext_out;
|
||||
}
|
||||
fragment = (int8_t)base64_decode_value_signed(*codechar++);
|
||||
} while (fragment < 0);
|
||||
*plainchar++ |= (fragment & 0x030) >> 4;
|
||||
*plainchar = (fragment & 0x00f) << 4;
|
||||
case step_c:
|
||||
do
|
||||
{
|
||||
if (codechar == code_in + length_in)
|
||||
{
|
||||
state_in->step = step_c;
|
||||
state_in->plainchar = *plainchar;
|
||||
return plainchar - plaintext_out;
|
||||
}
|
||||
fragment = (int8_t)base64_decode_value_signed(*codechar++);
|
||||
} while (fragment < 0);
|
||||
*plainchar++ |= (fragment & 0x03c) >> 2;
|
||||
*plainchar = (fragment & 0x003) << 6;
|
||||
case step_d:
|
||||
do
|
||||
{
|
||||
if (codechar == code_in + length_in)
|
||||
{
|
||||
state_in->step = step_d;
|
||||
state_in->plainchar = *plainchar;
|
||||
return plainchar - plaintext_out;
|
||||
}
|
||||
fragment = (int8_t)base64_decode_value_signed(*codechar++);
|
||||
} while (fragment < 0);
|
||||
*plainchar++ |= (fragment & 0x03f);
|
||||
}
|
||||
}
|
||||
/* control should not reach here */
|
||||
return plainchar - plaintext_out;
|
||||
}
|
||||
|
||||
static int base64_decode_chars_signed(const int8_t* code_in, const int length_in, int8_t* plaintext_out)
|
||||
{
|
||||
base64_decodestate _state;
|
||||
base64_init_decodestate(&_state);
|
||||
int len = base64_decode_block_signed(code_in, length_in, plaintext_out, &_state);
|
||||
if (len > 0)
|
||||
{
|
||||
plaintext_out[len] = 0;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
int base64_decode_value(char value_in)
|
||||
{
|
||||
return base64_decode_value_signed(*((int8_t *) &value_in));
|
||||
}
|
||||
|
||||
int base64_decode_block(const char* code_in, const int length_in, char* plaintext_out, base64_decodestate* state_in)
|
||||
{
|
||||
return base64_decode_block_signed((int8_t *) code_in, length_in, (int8_t *) plaintext_out, state_in);
|
||||
}
|
||||
|
||||
int base64_decode_chars(const char* code_in, const int length_in, char* plaintext_out)
|
||||
{
|
||||
return base64_decode_chars_signed((int8_t *) code_in, length_in, (int8_t *) plaintext_out);
|
||||
}
|
||||
int base64_decode_chars(const char* code_in, const int length_in, char* plaintext_out){
|
||||
return base64_decode_chars_signed((int8_t *) code_in, length_in, (int8_t *) plaintext_out);
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -1,8 +1,8 @@
|
||||
/*
|
||||
cdecode.h - c header for a base64 decoding algorithm
|
||||
cdecode.h - c header for a base64 decoding algorithm
|
||||
|
||||
This is part of the libb64 project, and has been placed in the public domain.
|
||||
For details, see http://sourceforge.net/projects/libb64
|
||||
This is part of the libb64 project, and has been placed in the public domain.
|
||||
For details, see http://sourceforge.net/projects/libb64
|
||||
*/
|
||||
|
||||
#ifndef BASE64_CDECODE_H
|
||||
@ -14,15 +14,13 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum
|
||||
{
|
||||
step_a, step_b, step_c, step_d
|
||||
typedef enum {
|
||||
step_a, step_b, step_c, step_d
|
||||
} base64_decodestep;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
base64_decodestep step;
|
||||
char plainchar;
|
||||
typedef struct {
|
||||
base64_decodestep step;
|
||||
char plainchar;
|
||||
} base64_decodestate;
|
||||
|
||||
void base64_init_decodestate(base64_decodestate* state_in);
|
||||
|
@ -1,8 +1,8 @@
|
||||
/*
|
||||
cencoder.c - c source to a base64 encoding algorithm implementation
|
||||
cencoder.c - c source to a base64 encoding algorithm implementation
|
||||
|
||||
This is part of the libb64 project, and has been placed in the public domain.
|
||||
For details, see http://sourceforge.net/projects/libb64
|
||||
This is part of the libb64 project, and has been placed in the public domain.
|
||||
For details, see http://sourceforge.net/projects/libb64
|
||||
*/
|
||||
|
||||
#include <pgmspace.h>
|
||||
@ -10,121 +10,105 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
void base64_init_encodestate(base64_encodestate* state_in)
|
||||
{
|
||||
void base64_init_encodestate(base64_encodestate* state_in){
|
||||
state_in->step = step_A;
|
||||
state_in->result = 0;
|
||||
state_in->stepcount = 0;
|
||||
state_in->stepsnewline = BASE64_CHARS_PER_LINE;
|
||||
}
|
||||
|
||||
|
||||
void base64_init_encodestate_nonewlines(base64_encodestate* state_in){
|
||||
base64_init_encodestate(state_in);
|
||||
state_in->stepsnewline = -1;
|
||||
}
|
||||
|
||||
char base64_encode_value(char value_in){
|
||||
static const char encoding[] PROGMEM = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
if (value_in > 63) return '=';
|
||||
return pgm_read_byte( &encoding[(int)value_in] );
|
||||
}
|
||||
|
||||
int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in){
|
||||
const char* plainchar = plaintext_in;
|
||||
const char* const plaintextend = plaintext_in + length_in;
|
||||
char* codechar = code_out;
|
||||
char result;
|
||||
char fragment;
|
||||
|
||||
result = state_in->result;
|
||||
|
||||
switch (state_in->step){
|
||||
while (1){
|
||||
case step_A:
|
||||
if (plainchar == plaintextend){
|
||||
state_in->result = result;
|
||||
state_in->step = step_A;
|
||||
state_in->result = 0;
|
||||
return codechar - code_out;
|
||||
}
|
||||
fragment = *plainchar++;
|
||||
result = (fragment & 0x0fc) >> 2;
|
||||
*codechar++ = base64_encode_value(result);
|
||||
result = (fragment & 0x003) << 4;
|
||||
case step_B:
|
||||
if (plainchar == plaintextend){
|
||||
state_in->result = result;
|
||||
state_in->step = step_B;
|
||||
return codechar - code_out;
|
||||
}
|
||||
fragment = *plainchar++;
|
||||
result |= (fragment & 0x0f0) >> 4;
|
||||
*codechar++ = base64_encode_value(result);
|
||||
result = (fragment & 0x00f) << 2;
|
||||
case step_C:
|
||||
if (plainchar == plaintextend){
|
||||
state_in->result = result;
|
||||
state_in->step = step_C;
|
||||
return codechar - code_out;
|
||||
}
|
||||
fragment = *plainchar++;
|
||||
result |= (fragment & 0x0c0) >> 6;
|
||||
*codechar++ = base64_encode_value(result);
|
||||
result = (fragment & 0x03f) >> 0;
|
||||
*codechar++ = base64_encode_value(result);
|
||||
|
||||
++(state_in->stepcount);
|
||||
if ((state_in->stepcount == BASE64_CHARS_PER_LINE/4) && (state_in->stepsnewline > 0)){
|
||||
*codechar++ = '\n';
|
||||
state_in->stepcount = 0;
|
||||
state_in->stepsnewline = BASE64_CHARS_PER_LINE;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* control should not reach here */
|
||||
return codechar - code_out;
|
||||
}
|
||||
|
||||
int base64_encode_blockend(char* code_out, base64_encodestate* state_in){
|
||||
char* codechar = code_out;
|
||||
|
||||
void base64_init_encodestate_nonewlines(base64_encodestate* state_in)
|
||||
{
|
||||
base64_init_encodestate(state_in);
|
||||
state_in->stepsnewline = -1;
|
||||
}
|
||||
switch (state_in->step){
|
||||
case step_B:
|
||||
*codechar++ = base64_encode_value(state_in->result);
|
||||
*codechar++ = '=';
|
||||
*codechar++ = '=';
|
||||
break;
|
||||
case step_C:
|
||||
*codechar++ = base64_encode_value(state_in->result);
|
||||
*codechar++ = '=';
|
||||
break;
|
||||
case step_A:
|
||||
break;
|
||||
}
|
||||
*codechar = 0x00;
|
||||
|
||||
char base64_encode_value(char value_in)
|
||||
{
|
||||
static const char encoding[] PROGMEM = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
if (value_in > 63)
|
||||
{
|
||||
return '=';
|
||||
}
|
||||
return pgm_read_byte(&encoding[(int)value_in]);
|
||||
}
|
||||
return codechar - code_out;
|
||||
}
|
||||
|
||||
int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in)
|
||||
{
|
||||
const char* plainchar = plaintext_in;
|
||||
const char* const plaintextend = plaintext_in + length_in;
|
||||
char* codechar = code_out;
|
||||
char result;
|
||||
char fragment;
|
||||
|
||||
result = state_in->result;
|
||||
|
||||
switch (state_in->step)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
case step_A:
|
||||
if (plainchar == plaintextend)
|
||||
{
|
||||
state_in->result = result;
|
||||
state_in->step = step_A;
|
||||
return codechar - code_out;
|
||||
}
|
||||
fragment = *plainchar++;
|
||||
result = (fragment & 0x0fc) >> 2;
|
||||
*codechar++ = base64_encode_value(result);
|
||||
result = (fragment & 0x003) << 4;
|
||||
case step_B:
|
||||
if (plainchar == plaintextend)
|
||||
{
|
||||
state_in->result = result;
|
||||
state_in->step = step_B;
|
||||
return codechar - code_out;
|
||||
}
|
||||
fragment = *plainchar++;
|
||||
result |= (fragment & 0x0f0) >> 4;
|
||||
*codechar++ = base64_encode_value(result);
|
||||
result = (fragment & 0x00f) << 2;
|
||||
case step_C:
|
||||
if (plainchar == plaintextend)
|
||||
{
|
||||
state_in->result = result;
|
||||
state_in->step = step_C;
|
||||
return codechar - code_out;
|
||||
}
|
||||
fragment = *plainchar++;
|
||||
result |= (fragment & 0x0c0) >> 6;
|
||||
*codechar++ = base64_encode_value(result);
|
||||
result = (fragment & 0x03f) >> 0;
|
||||
*codechar++ = base64_encode_value(result);
|
||||
|
||||
++(state_in->stepcount);
|
||||
if ((state_in->stepcount == BASE64_CHARS_PER_LINE / 4) && (state_in->stepsnewline > 0))
|
||||
{
|
||||
*codechar++ = '\n';
|
||||
state_in->stepcount = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* control should not reach here */
|
||||
return codechar - code_out;
|
||||
}
|
||||
|
||||
int base64_encode_blockend(char* code_out, base64_encodestate* state_in)
|
||||
{
|
||||
char* codechar = code_out;
|
||||
|
||||
switch (state_in->step)
|
||||
{
|
||||
case step_B:
|
||||
*codechar++ = base64_encode_value(state_in->result);
|
||||
*codechar++ = '=';
|
||||
*codechar++ = '=';
|
||||
break;
|
||||
case step_C:
|
||||
*codechar++ = base64_encode_value(state_in->result);
|
||||
*codechar++ = '=';
|
||||
break;
|
||||
case step_A:
|
||||
break;
|
||||
}
|
||||
*codechar = 0x00;
|
||||
|
||||
return codechar - code_out;
|
||||
}
|
||||
|
||||
int base64_encode_chars(const char* plaintext_in, int length_in, char* code_out)
|
||||
{
|
||||
base64_encodestate _state;
|
||||
base64_init_encodestate(&_state);
|
||||
int len = base64_encode_block(plaintext_in, length_in, code_out, &_state);
|
||||
return len + base64_encode_blockend((code_out + len), &_state);
|
||||
}
|
||||
int base64_encode_chars(const char* plaintext_in, int length_in, char* code_out){
|
||||
base64_encodestate _state;
|
||||
base64_init_encodestate(&_state);
|
||||
int len = base64_encode_block(plaintext_in, length_in, code_out, &_state);
|
||||
return len + base64_encode_blockend((code_out + len), &_state);
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -1,8 +1,8 @@
|
||||
/*
|
||||
cencode.h - c header for a base64 encoding algorithm
|
||||
cencode.h - c header for a base64 encoding algorithm
|
||||
|
||||
This is part of the libb64 project, and has been placed in the public domain.
|
||||
For details, see http://sourceforge.net/projects/libb64
|
||||
This is part of the libb64 project, and has been placed in the public domain.
|
||||
For details, see http://sourceforge.net/projects/libb64
|
||||
*/
|
||||
|
||||
#ifndef BASE64_CENCODE_H
|
||||
@ -19,17 +19,15 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum
|
||||
{
|
||||
step_A, step_B, step_C
|
||||
typedef enum {
|
||||
step_A, step_B, step_C
|
||||
} base64_encodestep;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
base64_encodestep step;
|
||||
char result;
|
||||
int stepcount;
|
||||
int stepsnewline;
|
||||
typedef struct {
|
||||
base64_encodestep step;
|
||||
char result;
|
||||
int stepcount;
|
||||
int stepsnewline;
|
||||
} base64_encodestate;
|
||||
|
||||
void base64_init_encodestate(base64_encodestate* state_in);
|
||||
|
@ -1,27 +1,27 @@
|
||||
/*
|
||||
libc_replacements.c - replaces libc functions with functions
|
||||
from Espressif SDK
|
||||
libc_replacements.c - replaces libc functions with functions
|
||||
from Espressif SDK
|
||||
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2015 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
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
|
||||
|
||||
Modified 03 April 2015 by Markus Sattler
|
||||
Modified 03 April 2015 by Markus Sattler
|
||||
|
||||
*/
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdarg.h>
|
||||
@ -47,103 +47,88 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
int ICACHE_RAM_ATTR _open_r(struct _reent* unused, const char *ptr, int mode)
|
||||
{
|
||||
(void)unused;
|
||||
(void)ptr;
|
||||
(void)mode;
|
||||
return 0;
|
||||
}
|
||||
int ICACHE_RAM_ATTR _open_r (struct _reent* unused, const char *ptr, int mode) {
|
||||
(void)unused;
|
||||
(void)ptr;
|
||||
(void)mode;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ICACHE_RAM_ATTR _close_r(struct _reent* unused, int file)
|
||||
{
|
||||
(void)unused;
|
||||
(void)file;
|
||||
return 0;
|
||||
}
|
||||
int ICACHE_RAM_ATTR _close_r(struct _reent* unused, int file) {
|
||||
(void)unused;
|
||||
(void)file;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ICACHE_RAM_ATTR _fstat_r(struct _reent* unused, int file, struct stat *st)
|
||||
{
|
||||
(void)unused;
|
||||
(void)file;
|
||||
st->st_mode = S_IFCHR;
|
||||
return 0;
|
||||
}
|
||||
int ICACHE_RAM_ATTR _fstat_r(struct _reent* unused, int file, struct stat *st) {
|
||||
(void)unused;
|
||||
(void)file;
|
||||
st->st_mode = S_IFCHR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ICACHE_RAM_ATTR _lseek_r(struct _reent* unused, int file, int ptr, int dir)
|
||||
{
|
||||
(void)unused;
|
||||
(void)file;
|
||||
(void)ptr;
|
||||
(void)dir;
|
||||
return 0;
|
||||
}
|
||||
int ICACHE_RAM_ATTR _lseek_r(struct _reent* unused, int file, int ptr, int dir) {
|
||||
(void)unused;
|
||||
(void)file;
|
||||
(void)ptr;
|
||||
(void)dir;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ICACHE_RAM_ATTR _read_r(struct _reent* unused, int file, char *ptr, int len)
|
||||
{
|
||||
(void)unused;
|
||||
(void)file;
|
||||
(void)ptr;
|
||||
(void)len;
|
||||
return 0;
|
||||
}
|
||||
int ICACHE_RAM_ATTR _read_r(struct _reent* unused, int file, char *ptr, int len) {
|
||||
(void)unused;
|
||||
(void)file;
|
||||
(void)ptr;
|
||||
(void)len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ICACHE_RAM_ATTR _write_r(struct _reent* r, int file, char *ptr, int len)
|
||||
{
|
||||
(void) r;
|
||||
int pos = len;
|
||||
if (file == STDOUT_FILENO)
|
||||
{
|
||||
while (pos--)
|
||||
{
|
||||
ets_putc(*ptr);
|
||||
++ptr;
|
||||
}
|
||||
int ICACHE_RAM_ATTR _write_r(struct _reent* r, int file, char *ptr, int len) {
|
||||
(void) r;
|
||||
int pos = len;
|
||||
if (file == STDOUT_FILENO) {
|
||||
while(pos--) {
|
||||
ets_putc(*ptr);
|
||||
++ptr;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
int ICACHE_RAM_ATTR _putc_r(struct _reent* r, int c, FILE* file) __attribute__((weak));
|
||||
int ICACHE_RAM_ATTR _putc_r(struct _reent* r, int c, FILE* file) __attribute__((weak));
|
||||
|
||||
int ICACHE_RAM_ATTR _putc_r(struct _reent* r, int c, FILE* file)
|
||||
{
|
||||
(void) r;
|
||||
if (file->_file == STDOUT_FILENO)
|
||||
{
|
||||
return ets_putc(c);
|
||||
}
|
||||
return EOF;
|
||||
int ICACHE_RAM_ATTR _putc_r(struct _reent* r, int c, FILE* file) {
|
||||
(void) r;
|
||||
if (file->_file == STDOUT_FILENO) {
|
||||
return ets_putc(c);
|
||||
}
|
||||
return EOF;
|
||||
}
|
||||
|
||||
int ICACHE_RAM_ATTR puts(const char * str)
|
||||
{
|
||||
char c;
|
||||
while ((c = *str) != 0)
|
||||
{
|
||||
ets_putc(c);
|
||||
++str;
|
||||
}
|
||||
ets_putc('\n');
|
||||
return true;
|
||||
int ICACHE_RAM_ATTR puts(const char * str) {
|
||||
char c;
|
||||
while((c = *str) != 0) {
|
||||
ets_putc(c);
|
||||
++str;
|
||||
}
|
||||
ets_putc('\n');
|
||||
return true;
|
||||
}
|
||||
|
||||
#undef putchar
|
||||
int ICACHE_RAM_ATTR putchar(int c)
|
||||
{
|
||||
ets_putc(c);
|
||||
return c;
|
||||
}
|
||||
int ICACHE_RAM_ATTR putchar(int c) {
|
||||
ets_putc(c);
|
||||
return c;
|
||||
}
|
||||
|
||||
void _exit(int status)
|
||||
{
|
||||
(void) status;
|
||||
abort();
|
||||
}
|
||||
void _exit(int status) {
|
||||
(void) status;
|
||||
abort();
|
||||
}
|
||||
|
||||
int atexit(void (*func)())
|
||||
{
|
||||
(void) func;
|
||||
return 0;
|
||||
}
|
||||
int atexit(void (*func)()) {
|
||||
(void) func;
|
||||
return 0;
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -1,24 +1,24 @@
|
||||
/*
|
||||
md5.h - exposed md5 ROM functions for esp8266
|
||||
md5.h - exposed md5 ROM functions for esp8266
|
||||
|
||||
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
|
||||
original C source from https://github.com/morrissinger/ESP8266-Websocket/raw/master/MD5.h
|
||||
original C source from https://github.com/morrissinger/ESP8266-Websocket/raw/master/MD5.h
|
||||
|
||||
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 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.
|
||||
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
|
||||
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
|
||||
*/
|
||||
#ifndef __ESP8266_MD5__
|
||||
#define __ESP8266_MD5__
|
||||
@ -27,16 +27,15 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t state[4];
|
||||
uint32_t count[2];
|
||||
uint8_t buffer[64];
|
||||
typedef struct {
|
||||
uint32_t state[4];
|
||||
uint32_t count[2];
|
||||
uint8_t buffer[64];
|
||||
} md5_context_t;
|
||||
|
||||
extern void MD5Init(md5_context_t *);
|
||||
extern void MD5Update(md5_context_t *, const uint8_t *, const uint16_t);
|
||||
extern void MD5Final(uint8_t [16], md5_context_t *);
|
||||
extern void MD5Init (md5_context_t *);
|
||||
extern void MD5Update (md5_context_t *, const uint8_t *, const uint16_t);
|
||||
extern void MD5Final (uint8_t [16], md5_context_t *);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
|
@ -1,45 +1,45 @@
|
||||
/*
|
||||
sigma_delta.h - esp8266 sigma-delta source
|
||||
sigma_delta.h - esp8266 sigma-delta source
|
||||
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2014 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
/*******************************************************************************
|
||||
Info Sigma delta module
|
||||
/*******************************************************************************
|
||||
* Info Sigma delta module
|
||||
|
||||
This module controls the esp8266 internal sigma delta source
|
||||
Each pin can be connected to the sigma delta source
|
||||
The target duty and frequency can be modified via the register GPIO_SIGMA_DELTA
|
||||
This module controls the esp8266 internal sigma delta source
|
||||
Each pin can be connected to the sigma delta source
|
||||
The target duty and frequency can be modified via the register GPIO_SIGMA_DELTA
|
||||
|
||||
THE TARGET FREQUENCY IS DEFINED AS:
|
||||
THE TARGET FREQUENCY IS DEFINED AS:
|
||||
|
||||
FREQ = 80,000,000/prescaler * target /256 HZ, 0<target<128
|
||||
FREQ = 80,000,000/prescaler * (256-target) /256 HZ, 128<target<256
|
||||
target: duty cycle,range 0-255
|
||||
prescaler: is a clock divider, range 0-255
|
||||
so the target and prescale will both affect the freq.
|
||||
CPU_FREQ has no influence on the sigma delta frequency.
|
||||
FREQ = 80,000,000/prescaler * target /256 HZ, 0<target<128
|
||||
FREQ = 80,000,000/prescaler * (256-target) /256 HZ, 128<target<256
|
||||
target: duty cycle,range 0-255
|
||||
prescaler: is a clock divider, range 0-255
|
||||
so the target and prescale will both affect the freq.
|
||||
CPU_FREQ has no influence on the sigma delta frequency.
|
||||
|
||||
Usage :
|
||||
1. sigmaDeltaSetup(0,f) : activate the sigma delta source with frequency f and default duty cycle (0)
|
||||
2. sigmaDeltaAttachPin(pin), any pin 0..15, TBC if gpio16 supports sigma-delta source
|
||||
This will set the pin to NORMAL output mode (pinMode(pin,OUTPUT))
|
||||
3. sigmaDeltaWrite(0,dc) : set the output signal duty cycle, duty cycle = dc/256
|
||||
Usage :
|
||||
1. sigmaDeltaSetup(0,f) : activate the sigma delta source with frequency f and default duty cycle (0)
|
||||
2. sigmaDeltaAttachPin(pin), any pin 0..15, TBC if gpio16 supports sigma-delta source
|
||||
This will set the pin to NORMAL output mode (pinMode(pin,OUTPUT))
|
||||
3. sigmaDeltaWrite(0,dc) : set the output signal duty cycle, duty cycle = dc/256
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
|
@ -1,41 +1,41 @@
|
||||
/*
|
||||
sntp-lwip2.c - ESP8266-specific functions for SNTP and lwIP-v2
|
||||
Copyright (c) 2015 Espressif (license is tools/sdk/lwip/src/core/sntp.c's)
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
3. The name of the author may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
OF SUCH DAMAGE.
|
||||
|
||||
|
||||
History:
|
||||
This code is extracted from lwip1.4-espressif's sntp.c
|
||||
which is a patched version of the original lwip1's sntp.
|
||||
(check the mix-up in tools/sdk/lwip/src/core/sntp.c)
|
||||
It is moved here as-is and cleaned for maintainability and
|
||||
because it does not belong to lwip.
|
||||
|
||||
TODOs:
|
||||
settimeofday(): handle tv->tv_usec
|
||||
sntp_mktm_r(): review, fix DST handling (this one is currently untouched from lwip-1.4)
|
||||
implement adjtime()
|
||||
*/
|
||||
* sntp-lwip2.c - ESP8266-specific functions for SNTP and lwIP-v2
|
||||
* Copyright (c) 2015 Espressif (license is tools/sdk/lwip/src/core/sntp.c's)
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
*
|
||||
* History:
|
||||
* This code is extracted from lwip1.4-espressif's sntp.c
|
||||
* which is a patched version of the original lwip1's sntp.
|
||||
* (check the mix-up in tools/sdk/lwip/src/core/sntp.c)
|
||||
* It is moved here as-is and cleaned for maintainability and
|
||||
* because it does not belong to lwip.
|
||||
*
|
||||
* TODOs:
|
||||
* settimeofday(): handle tv->tv_usec
|
||||
* sntp_mktm_r(): review, fix DST handling (this one is currently untouched from lwip-1.4)
|
||||
* implement adjtime()
|
||||
*/
|
||||
|
||||
#include <lwip/init.h>
|
||||
#include <sys/time.h>
|
||||
@ -45,46 +45,46 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
static void (*_settimeofday_cb)(void) = NULL;
|
||||
static void (*_settimeofday_cb)(void) = NULL;
|
||||
|
||||
void settimeofday_cb(void (*cb)(void))
|
||||
{
|
||||
_settimeofday_cb = cb;
|
||||
}
|
||||
void settimeofday_cb (void (*cb)(void))
|
||||
{
|
||||
_settimeofday_cb = cb;
|
||||
}
|
||||
|
||||
#if LWIP_VERSION_MAJOR == 1
|
||||
|
||||
#include <pgmspace.h>
|
||||
|
||||
static const char stod14[] PROGMEM = "settimeofday() can't set time!\n";
|
||||
bool sntp_set_timezone(sint8 timezone);
|
||||
bool sntp_set_timezone_in_seconds(sint32 timezone)
|
||||
static const char stod14[] PROGMEM = "settimeofday() can't set time!\n";
|
||||
bool sntp_set_timezone(sint8 timezone);
|
||||
bool sntp_set_timezone_in_seconds(sint32 timezone)
|
||||
{
|
||||
return sntp_set_timezone((sint8)(timezone/(60*60))); //TODO: move this to the same file as sntp_set_timezone() in lwip1.4, and implement correctly over there.
|
||||
}
|
||||
|
||||
void sntp_set_daylight(int daylight);
|
||||
|
||||
int settimeofday(const struct timeval* tv, const struct timezone* tz)
|
||||
{
|
||||
if (tz) /*before*/
|
||||
{
|
||||
return sntp_set_timezone((sint8)(timezone / (60 * 60))); //TODO: move this to the same file as sntp_set_timezone() in lwip1.4, and implement correctly over there.
|
||||
sntp_set_timezone_in_seconds(tz->tz_minuteswest * 60);
|
||||
// apparently tz->tz_dsttime is a bitfield and should not be further used (cf man)
|
||||
sntp_set_daylight(0);
|
||||
}
|
||||
|
||||
void sntp_set_daylight(int daylight);
|
||||
|
||||
int settimeofday(const struct timeval* tv, const struct timezone* tz)
|
||||
if (tv) /* after*/
|
||||
{
|
||||
if (tz) /*before*/
|
||||
{
|
||||
sntp_set_timezone_in_seconds(tz->tz_minuteswest * 60);
|
||||
// apparently tz->tz_dsttime is a bitfield and should not be further used (cf man)
|
||||
sntp_set_daylight(0);
|
||||
}
|
||||
if (tv) /* after*/
|
||||
{
|
||||
// can't call lwip1.4's static sntp_set_system_time()
|
||||
os_printf(stod14);
|
||||
// can't call lwip1.4's static sntp_set_system_time()
|
||||
os_printf(stod14);
|
||||
|
||||
// reset time subsystem
|
||||
timeshift64_is_set = false;
|
||||
// reset time subsystem
|
||||
timeshift64_is_set = false;
|
||||
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // lwip 1.4 only
|
||||
|
||||
@ -92,12 +92,12 @@ extern "C" {
|
||||
|
||||
#include <lwip/apps/sntp.h>
|
||||
|
||||
static uint32 realtime_stamp = 0;
|
||||
static uint16 dst = 0;
|
||||
static sint32 time_zone = 8 * (60 * 60); // espressif HQ's default timezone
|
||||
LOCAL os_timer_t sntp_timer;
|
||||
static uint32 realtime_stamp = 0;
|
||||
static uint16 dst = 0;
|
||||
static sint32 time_zone = 8 * (60 * 60); // espressif HQ's default timezone
|
||||
LOCAL os_timer_t sntp_timer;
|
||||
|
||||
/*****************************************/
|
||||
/*****************************************/
|
||||
#define SECSPERMIN 60L
|
||||
#define MINSPERHOUR 60L
|
||||
#define HOURSPERDAY 24L
|
||||
@ -115,404 +115,373 @@ extern "C" {
|
||||
|
||||
#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
|
||||
|
||||
int __tznorth;
|
||||
int __tzyear;
|
||||
char reult[100];
|
||||
static const int mon_lengths[2][12] =
|
||||
int __tznorth;
|
||||
int __tzyear;
|
||||
char reult[100];
|
||||
static const int mon_lengths[2][12] = {
|
||||
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
|
||||
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
|
||||
} ;
|
||||
|
||||
static const int year_lengths[2] = {
|
||||
365,
|
||||
366
|
||||
} ;
|
||||
struct tm
|
||||
{
|
||||
int tm_sec;
|
||||
int tm_min;
|
||||
int tm_hour;
|
||||
int tm_mday;
|
||||
int tm_mon;
|
||||
int tm_year;
|
||||
int tm_wday;
|
||||
int tm_yday;
|
||||
int tm_isdst;
|
||||
};
|
||||
|
||||
struct tm res_buf;
|
||||
typedef struct __tzrule_struct
|
||||
{
|
||||
char ch;
|
||||
int m;
|
||||
int n;
|
||||
int d;
|
||||
int s;
|
||||
time_t change;
|
||||
int offset;
|
||||
} __tzrule_type;
|
||||
|
||||
__tzrule_type sntp__tzrule[2];
|
||||
struct tm *
|
||||
sntp_mktm_r(const time_t * tim_p ,struct tm *res ,int is_gmtime)
|
||||
{
|
||||
long days, rem;
|
||||
time_t lcltime;
|
||||
int y;
|
||||
int yleap;
|
||||
const int *ip;
|
||||
|
||||
/* base decision about std/dst time on current time */
|
||||
lcltime = *tim_p;
|
||||
|
||||
days = ((long)lcltime) / SECSPERDAY;
|
||||
rem = ((long)lcltime) % SECSPERDAY;
|
||||
while (rem < 0)
|
||||
{
|
||||
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
|
||||
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
|
||||
} ;
|
||||
|
||||
static const int year_lengths[2] =
|
||||
{
|
||||
365,
|
||||
366
|
||||
} ;
|
||||
struct tm
|
||||
{
|
||||
int tm_sec;
|
||||
int tm_min;
|
||||
int tm_hour;
|
||||
int tm_mday;
|
||||
int tm_mon;
|
||||
int tm_year;
|
||||
int tm_wday;
|
||||
int tm_yday;
|
||||
int tm_isdst;
|
||||
};
|
||||
|
||||
struct tm res_buf;
|
||||
typedef struct __tzrule_struct
|
||||
{
|
||||
char ch;
|
||||
int m;
|
||||
int n;
|
||||
int d;
|
||||
int s;
|
||||
time_t change;
|
||||
int offset;
|
||||
} __tzrule_type;
|
||||
|
||||
__tzrule_type sntp__tzrule[2];
|
||||
struct tm *
|
||||
sntp_mktm_r(const time_t * tim_p, struct tm *res, int is_gmtime)
|
||||
{
|
||||
long days, rem;
|
||||
time_t lcltime;
|
||||
int y;
|
||||
int yleap;
|
||||
const int *ip;
|
||||
|
||||
/* base decision about std/dst time on current time */
|
||||
lcltime = *tim_p;
|
||||
|
||||
days = ((long)lcltime) / SECSPERDAY;
|
||||
rem = ((long)lcltime) % SECSPERDAY;
|
||||
while (rem < 0)
|
||||
{
|
||||
rem += SECSPERDAY;
|
||||
--days;
|
||||
}
|
||||
while (rem >= SECSPERDAY)
|
||||
{
|
||||
rem -= SECSPERDAY;
|
||||
++days;
|
||||
}
|
||||
|
||||
/* compute hour, min, and sec */
|
||||
res->tm_hour = (int)(rem / SECSPERHOUR);
|
||||
rem %= SECSPERHOUR;
|
||||
res->tm_min = (int)(rem / SECSPERMIN);
|
||||
res->tm_sec = (int)(rem % SECSPERMIN);
|
||||
|
||||
/* compute day of week */
|
||||
if ((res->tm_wday = ((EPOCH_WDAY + days) % DAYSPERWEEK)) < 0)
|
||||
{
|
||||
res->tm_wday += DAYSPERWEEK;
|
||||
}
|
||||
|
||||
/* compute year & day of year */
|
||||
y = EPOCH_YEAR;
|
||||
if (days >= 0)
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
yleap = isleap(y);
|
||||
if (days < year_lengths[yleap])
|
||||
{
|
||||
break;
|
||||
}
|
||||
y++;
|
||||
days -= year_lengths[yleap];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
do
|
||||
{
|
||||
--y;
|
||||
yleap = isleap(y);
|
||||
days += year_lengths[yleap];
|
||||
} while (days < 0);
|
||||
}
|
||||
|
||||
res->tm_year = y - YEAR_BASE;
|
||||
res->tm_yday = days;
|
||||
ip = mon_lengths[yleap];
|
||||
for (res->tm_mon = 0; days >= ip[res->tm_mon]; ++res->tm_mon)
|
||||
{
|
||||
days -= ip[res->tm_mon];
|
||||
}
|
||||
res->tm_mday = days + 1;
|
||||
|
||||
if (!is_gmtime)
|
||||
{
|
||||
int offset;
|
||||
int hours, mins, secs;
|
||||
|
||||
// TZ_LOCK;
|
||||
// if (_daylight)
|
||||
// {
|
||||
// if (y == __tzyear || __tzcalc_limits (y))
|
||||
// res->tm_isdst = (__tznorth
|
||||
// ? (*tim_p >= __tzrule[0].change && *tim_p < __tzrule[1].change)
|
||||
// : (*tim_p >= __tzrule[0].change || *tim_p < __tzrule[1].change));
|
||||
// else
|
||||
// res->tm_isdst = -1;
|
||||
// }
|
||||
// else
|
||||
res->tm_isdst = -1;
|
||||
|
||||
offset = (res->tm_isdst == 1 ? sntp__tzrule[1].offset : sntp__tzrule[0].offset);
|
||||
|
||||
hours = offset / SECSPERHOUR;
|
||||
offset = offset % SECSPERHOUR;
|
||||
|
||||
mins = offset / SECSPERMIN;
|
||||
secs = offset % SECSPERMIN;
|
||||
|
||||
res->tm_sec -= secs;
|
||||
res->tm_min -= mins;
|
||||
res->tm_hour -= hours;
|
||||
|
||||
if (res->tm_sec >= SECSPERMIN)
|
||||
{
|
||||
res->tm_min += 1;
|
||||
res->tm_sec -= SECSPERMIN;
|
||||
}
|
||||
else if (res->tm_sec < 0)
|
||||
{
|
||||
res->tm_min -= 1;
|
||||
res->tm_sec += SECSPERMIN;
|
||||
}
|
||||
if (res->tm_min >= MINSPERHOUR)
|
||||
{
|
||||
res->tm_hour += 1;
|
||||
res->tm_min -= MINSPERHOUR;
|
||||
}
|
||||
else if (res->tm_min < 0)
|
||||
{
|
||||
res->tm_hour -= 1;
|
||||
res->tm_min += MINSPERHOUR;
|
||||
}
|
||||
if (res->tm_hour >= HOURSPERDAY)
|
||||
{
|
||||
++res->tm_yday;
|
||||
++res->tm_wday;
|
||||
if (res->tm_wday > 6)
|
||||
{
|
||||
res->tm_wday = 0;
|
||||
}
|
||||
++res->tm_mday;
|
||||
res->tm_hour -= HOURSPERDAY;
|
||||
if (res->tm_mday > ip[res->tm_mon])
|
||||
{
|
||||
res->tm_mday -= ip[res->tm_mon];
|
||||
res->tm_mon += 1;
|
||||
if (res->tm_mon == 12)
|
||||
{
|
||||
res->tm_mon = 0;
|
||||
res->tm_year += 1;
|
||||
res->tm_yday = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (res->tm_hour < 0)
|
||||
{
|
||||
res->tm_yday -= 1;
|
||||
res->tm_wday -= 1;
|
||||
if (res->tm_wday < 0)
|
||||
{
|
||||
res->tm_wday = 6;
|
||||
}
|
||||
res->tm_mday -= 1;
|
||||
res->tm_hour += 24;
|
||||
if (res->tm_mday == 0)
|
||||
{
|
||||
res->tm_mon -= 1;
|
||||
if (res->tm_mon < 0)
|
||||
{
|
||||
res->tm_mon = 11;
|
||||
res->tm_year -= 1;
|
||||
res->tm_yday = 365 + isleap(res->tm_year);
|
||||
}
|
||||
res->tm_mday = ip[res->tm_mon];
|
||||
}
|
||||
}
|
||||
// TZ_UNLOCK;
|
||||
}
|
||||
else
|
||||
{
|
||||
res->tm_isdst = 0;
|
||||
}
|
||||
// os_printf("res %d %d %d %d %d\n",res->tm_year,res->tm_mon,res->tm_mday,res->tm_yday,res->tm_hour);
|
||||
return (res);
|
||||
rem += SECSPERDAY;
|
||||
--days;
|
||||
}
|
||||
struct tm *
|
||||
sntp_localtime_r(const time_t * tim_p,
|
||||
struct tm *res)
|
||||
while (rem >= SECSPERDAY)
|
||||
{
|
||||
return sntp_mktm_r(tim_p, res, 0);
|
||||
rem -= SECSPERDAY;
|
||||
++days;
|
||||
}
|
||||
|
||||
struct tm *
|
||||
sntp_localtime(const time_t * tim_p)
|
||||
/* compute hour, min, and sec */
|
||||
res->tm_hour = (int) (rem / SECSPERHOUR);
|
||||
rem %= SECSPERHOUR;
|
||||
res->tm_min = (int) (rem / SECSPERMIN);
|
||||
res->tm_sec = (int) (rem % SECSPERMIN);
|
||||
|
||||
/* compute day of week */
|
||||
if ((res->tm_wday = ((EPOCH_WDAY + days) % DAYSPERWEEK)) < 0)
|
||||
res->tm_wday += DAYSPERWEEK;
|
||||
|
||||
/* compute year & day of year */
|
||||
y = EPOCH_YEAR;
|
||||
if (days >= 0)
|
||||
{
|
||||
return sntp_localtime_r(tim_p, &res_buf);
|
||||
for (;;)
|
||||
{
|
||||
yleap = isleap(y);
|
||||
if (days < year_lengths[yleap])
|
||||
break;
|
||||
y++;
|
||||
days -= year_lengths[yleap];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
do
|
||||
{
|
||||
--y;
|
||||
yleap = isleap(y);
|
||||
days += year_lengths[yleap];
|
||||
} while (days < 0);
|
||||
}
|
||||
|
||||
int sntp__tzcalc_limits(int year)
|
||||
res->tm_year = y - YEAR_BASE;
|
||||
res->tm_yday = days;
|
||||
ip = mon_lengths[yleap];
|
||||
for (res->tm_mon = 0; days >= ip[res->tm_mon]; ++res->tm_mon)
|
||||
days -= ip[res->tm_mon];
|
||||
res->tm_mday = days + 1;
|
||||
|
||||
if (!is_gmtime)
|
||||
{
|
||||
int days, year_days, years;
|
||||
int i, j;
|
||||
int offset;
|
||||
int hours, mins, secs;
|
||||
|
||||
if (year < EPOCH_YEAR)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
// TZ_LOCK;
|
||||
// if (_daylight)
|
||||
// {
|
||||
// if (y == __tzyear || __tzcalc_limits (y))
|
||||
// res->tm_isdst = (__tznorth
|
||||
// ? (*tim_p >= __tzrule[0].change && *tim_p < __tzrule[1].change)
|
||||
// : (*tim_p >= __tzrule[0].change || *tim_p < __tzrule[1].change));
|
||||
// else
|
||||
// res->tm_isdst = -1;
|
||||
// }
|
||||
// else
|
||||
res->tm_isdst = -1;
|
||||
|
||||
__tzyear = year;
|
||||
offset = (res->tm_isdst == 1 ? sntp__tzrule[1].offset : sntp__tzrule[0].offset);
|
||||
|
||||
years = (year - EPOCH_YEAR);
|
||||
hours = offset / SECSPERHOUR;
|
||||
offset = offset % SECSPERHOUR;
|
||||
|
||||
year_days = years * 365 +
|
||||
(years - 1 + EPOCH_YEARS_SINCE_LEAP) / 4 - (years - 1 + EPOCH_YEARS_SINCE_CENTURY) / 100 +
|
||||
(years - 1 + EPOCH_YEARS_SINCE_LEAP_CENTURY) / 400;
|
||||
mins = offset / SECSPERMIN;
|
||||
secs = offset % SECSPERMIN;
|
||||
|
||||
for (i = 0; i < 2; ++i)
|
||||
{
|
||||
if (sntp__tzrule[i].ch == 'J')
|
||||
{
|
||||
days = year_days + sntp__tzrule[i].d + (isleap(year) && sntp__tzrule[i].d >= 60);
|
||||
}
|
||||
else if (sntp__tzrule[i].ch == 'D')
|
||||
{
|
||||
days = year_days + sntp__tzrule[i].d;
|
||||
}
|
||||
else
|
||||
{
|
||||
int yleap = isleap(year);
|
||||
int m_day, m_wday, wday_diff;
|
||||
const int *ip = mon_lengths[yleap];
|
||||
res->tm_sec -= secs;
|
||||
res->tm_min -= mins;
|
||||
res->tm_hour -= hours;
|
||||
|
||||
days = year_days;
|
||||
if (res->tm_sec >= SECSPERMIN)
|
||||
{
|
||||
res->tm_min += 1;
|
||||
res->tm_sec -= SECSPERMIN;
|
||||
}
|
||||
else if (res->tm_sec < 0)
|
||||
{
|
||||
res->tm_min -= 1;
|
||||
res->tm_sec += SECSPERMIN;
|
||||
}
|
||||
if (res->tm_min >= MINSPERHOUR)
|
||||
{
|
||||
res->tm_hour += 1;
|
||||
res->tm_min -= MINSPERHOUR;
|
||||
}
|
||||
else if (res->tm_min < 0)
|
||||
{
|
||||
res->tm_hour -= 1;
|
||||
res->tm_min += MINSPERHOUR;
|
||||
}
|
||||
if (res->tm_hour >= HOURSPERDAY)
|
||||
{
|
||||
++res->tm_yday;
|
||||
++res->tm_wday;
|
||||
if (res->tm_wday > 6)
|
||||
res->tm_wday = 0;
|
||||
++res->tm_mday;
|
||||
res->tm_hour -= HOURSPERDAY;
|
||||
if (res->tm_mday > ip[res->tm_mon])
|
||||
{
|
||||
res->tm_mday -= ip[res->tm_mon];
|
||||
res->tm_mon += 1;
|
||||
if (res->tm_mon == 12)
|
||||
{
|
||||
res->tm_mon = 0;
|
||||
res->tm_year += 1;
|
||||
res->tm_yday = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (res->tm_hour < 0)
|
||||
{
|
||||
res->tm_yday -= 1;
|
||||
res->tm_wday -= 1;
|
||||
if (res->tm_wday < 0)
|
||||
res->tm_wday = 6;
|
||||
res->tm_mday -= 1;
|
||||
res->tm_hour += 24;
|
||||
if (res->tm_mday == 0)
|
||||
{
|
||||
res->tm_mon -= 1;
|
||||
if (res->tm_mon < 0)
|
||||
{
|
||||
res->tm_mon = 11;
|
||||
res->tm_year -= 1;
|
||||
res->tm_yday = 365 + isleap(res->tm_year);
|
||||
}
|
||||
res->tm_mday = ip[res->tm_mon];
|
||||
}
|
||||
}
|
||||
// TZ_UNLOCK;
|
||||
}
|
||||
else
|
||||
res->tm_isdst = 0;
|
||||
// os_printf("res %d %d %d %d %d\n",res->tm_year,res->tm_mon,res->tm_mday,res->tm_yday,res->tm_hour);
|
||||
return (res);
|
||||
}
|
||||
struct tm *
|
||||
sntp_localtime_r(const time_t * tim_p ,
|
||||
struct tm *res)
|
||||
{
|
||||
return sntp_mktm_r (tim_p, res, 0);
|
||||
}
|
||||
|
||||
for (j = 1; j < sntp__tzrule[i].m; ++j)
|
||||
{
|
||||
days += ip[j - 1];
|
||||
}
|
||||
struct tm *
|
||||
sntp_localtime(const time_t * tim_p)
|
||||
{
|
||||
return sntp_localtime_r (tim_p, &res_buf);
|
||||
}
|
||||
|
||||
m_wday = (EPOCH_WDAY + days) % DAYSPERWEEK;
|
||||
int sntp__tzcalc_limits(int year)
|
||||
{
|
||||
int days, year_days, years;
|
||||
int i, j;
|
||||
|
||||
wday_diff = sntp__tzrule[i].d - m_wday;
|
||||
if (wday_diff < 0)
|
||||
{
|
||||
wday_diff += DAYSPERWEEK;
|
||||
}
|
||||
m_day = (sntp__tzrule[i].n - 1) * DAYSPERWEEK + wday_diff;
|
||||
if (year < EPOCH_YEAR)
|
||||
return 0;
|
||||
|
||||
while (m_day >= ip[j - 1])
|
||||
{
|
||||
m_day -= DAYSPERWEEK;
|
||||
}
|
||||
__tzyear = year;
|
||||
|
||||
days += m_day;
|
||||
}
|
||||
years = (year - EPOCH_YEAR);
|
||||
|
||||
/* store the change-over time in GMT form by adding offset */
|
||||
sntp__tzrule[i].change = days * SECSPERDAY + sntp__tzrule[i].s + sntp__tzrule[i].offset;
|
||||
}
|
||||
year_days = years * 365 +
|
||||
(years - 1 + EPOCH_YEARS_SINCE_LEAP) / 4 - (years - 1 + EPOCH_YEARS_SINCE_CENTURY) / 100 +
|
||||
(years - 1 + EPOCH_YEARS_SINCE_LEAP_CENTURY) / 400;
|
||||
|
||||
__tznorth = (sntp__tzrule[0].change < sntp__tzrule[1].change);
|
||||
for (i = 0; i < 2; ++i)
|
||||
{
|
||||
if (sntp__tzrule[i].ch == 'J')
|
||||
days = year_days + sntp__tzrule[i].d + (isleap(year) && sntp__tzrule[i].d >= 60);
|
||||
else if (sntp__tzrule[i].ch == 'D')
|
||||
days = year_days + sntp__tzrule[i].d;
|
||||
else
|
||||
{
|
||||
int yleap = isleap(year);
|
||||
int m_day, m_wday, wday_diff;
|
||||
const int *ip = mon_lengths[yleap];
|
||||
|
||||
return 1;
|
||||
days = year_days;
|
||||
|
||||
for (j = 1; j < sntp__tzrule[i].m; ++j)
|
||||
days += ip[j-1];
|
||||
|
||||
m_wday = (EPOCH_WDAY + days) % DAYSPERWEEK;
|
||||
|
||||
wday_diff = sntp__tzrule[i].d - m_wday;
|
||||
if (wday_diff < 0)
|
||||
wday_diff += DAYSPERWEEK;
|
||||
m_day = (sntp__tzrule[i].n - 1) * DAYSPERWEEK + wday_diff;
|
||||
|
||||
while (m_day >= ip[j-1])
|
||||
m_day -= DAYSPERWEEK;
|
||||
|
||||
days += m_day;
|
||||
}
|
||||
|
||||
/* store the change-over time in GMT form by adding offset */
|
||||
sntp__tzrule[i].change = days * SECSPERDAY + sntp__tzrule[i].s + sntp__tzrule[i].offset;
|
||||
}
|
||||
|
||||
char* sntp_asctime_r(struct tm *tim_p, char *result)
|
||||
{
|
||||
static const char day_name[7][4] =
|
||||
{
|
||||
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
||||
};
|
||||
static const char mon_name[12][4] =
|
||||
{
|
||||
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
||||
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
||||
};
|
||||
os_sprintf(result, "%s %s %02d %02d:%02d:%02d %02d\n",
|
||||
day_name[tim_p->tm_wday],
|
||||
mon_name[tim_p->tm_mon],
|
||||
tim_p->tm_mday, tim_p->tm_hour, tim_p->tm_min,
|
||||
tim_p->tm_sec, 1900 + tim_p->tm_year);
|
||||
return result;
|
||||
__tznorth = (sntp__tzrule[0].change < sntp__tzrule[1].change);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
char* sntp_asctime_r(struct tm *tim_p ,char *result)
|
||||
{
|
||||
static const char day_name[7][4] = {
|
||||
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
||||
};
|
||||
static const char mon_name[12][4] = {
|
||||
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
||||
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
||||
};
|
||||
os_sprintf (result, "%s %s %02d %02d:%02d:%02d %02d\n",
|
||||
day_name[tim_p->tm_wday],
|
||||
mon_name[tim_p->tm_mon],
|
||||
tim_p->tm_mday, tim_p->tm_hour, tim_p->tm_min,
|
||||
tim_p->tm_sec, 1900 + tim_p->tm_year);
|
||||
return result;
|
||||
}
|
||||
|
||||
char* sntp_asctime(struct tm *tim_p)
|
||||
{
|
||||
return sntp_asctime_r (tim_p, reult);
|
||||
}
|
||||
|
||||
uint32 ICACHE_RAM_ATTR sntp_get_current_timestamp(void)
|
||||
{
|
||||
return realtime_stamp;
|
||||
}
|
||||
|
||||
char* sntp_get_real_time(time_t t)
|
||||
{
|
||||
return sntp_asctime(sntp_localtime (&t));
|
||||
}
|
||||
|
||||
/* Returns the set timezone in seconds. If the timezone was set as seconds, the fractional part is floored. */
|
||||
sint32 sntp_get_timezone_in_seconds(void)
|
||||
{
|
||||
return time_zone;
|
||||
}
|
||||
|
||||
/* Returns the set timezone in hours. If the timezone was set as seconds, the fractional part is floored. */
|
||||
sint8 sntp_get_timezone(void)
|
||||
{
|
||||
return (sint8)(time_zone / (60 * 60));
|
||||
}
|
||||
|
||||
/* Sets the timezone in hours. Internally, the timezone is converted to seconds. */
|
||||
bool sntp_set_timezone_in_seconds(sint32 timezone)
|
||||
{
|
||||
if(timezone >= (-11 * (60 * 60)) || timezone <= (13 * (60 * 60))) {
|
||||
time_zone = timezone;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
char* sntp_asctime(struct tm *tim_p)
|
||||
/* Sets the timezone in hours. Internally, the timezone is converted to seconds. */
|
||||
bool sntp_set_timezone(sint8 timezone)
|
||||
{
|
||||
return sntp_set_timezone_in_seconds((sint32)timezone * 60 * 60);
|
||||
}
|
||||
|
||||
|
||||
void sntp_set_daylight(int daylight)
|
||||
{
|
||||
dst = daylight;
|
||||
}
|
||||
|
||||
void ICACHE_RAM_ATTR sntp_time_inc (void)
|
||||
{
|
||||
realtime_stamp++;
|
||||
}
|
||||
|
||||
static void sntp_set_system_time (uint32_t t)
|
||||
{
|
||||
realtime_stamp = t + time_zone + dst;
|
||||
os_timer_disarm(&sntp_timer);
|
||||
os_timer_setfn(&sntp_timer, (os_timer_func_t *)sntp_time_inc, NULL);
|
||||
os_timer_arm(&sntp_timer, 1000, 1);
|
||||
}
|
||||
|
||||
int settimeofday(const struct timeval* tv, const struct timezone* tz)
|
||||
{
|
||||
if (tz) /*before*/
|
||||
{
|
||||
return sntp_asctime_r(tim_p, reult);
|
||||
sntp_set_timezone_in_seconds(tz->tz_minuteswest * 60);
|
||||
// apparently tz->tz_dsttime is a bitfield and should not be further used (cf man)
|
||||
sntp_set_daylight(0);
|
||||
}
|
||||
|
||||
uint32 ICACHE_RAM_ATTR sntp_get_current_timestamp(void)
|
||||
if (tv) /* after*/
|
||||
{
|
||||
return realtime_stamp;
|
||||
}
|
||||
|
||||
char* sntp_get_real_time(time_t t)
|
||||
{
|
||||
return sntp_asctime(sntp_localtime(&t));
|
||||
}
|
||||
|
||||
/* Returns the set timezone in seconds. If the timezone was set as seconds, the fractional part is floored. */
|
||||
sint32 sntp_get_timezone_in_seconds(void)
|
||||
{
|
||||
return time_zone;
|
||||
}
|
||||
|
||||
/* Returns the set timezone in hours. If the timezone was set as seconds, the fractional part is floored. */
|
||||
sint8 sntp_get_timezone(void)
|
||||
{
|
||||
return (sint8)(time_zone / (60 * 60));
|
||||
}
|
||||
|
||||
/* Sets the timezone in hours. Internally, the timezone is converted to seconds. */
|
||||
bool sntp_set_timezone_in_seconds(sint32 timezone)
|
||||
{
|
||||
if (timezone >= (-11 * (60 * 60)) || timezone <= (13 * (60 * 60)))
|
||||
{
|
||||
time_zone = timezone;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Sets the timezone in hours. Internally, the timezone is converted to seconds. */
|
||||
bool sntp_set_timezone(sint8 timezone)
|
||||
{
|
||||
return sntp_set_timezone_in_seconds((sint32)timezone * 60 * 60);
|
||||
}
|
||||
|
||||
|
||||
void sntp_set_daylight(int daylight)
|
||||
{
|
||||
dst = daylight;
|
||||
}
|
||||
|
||||
void ICACHE_RAM_ATTR sntp_time_inc(void)
|
||||
{
|
||||
realtime_stamp++;
|
||||
}
|
||||
|
||||
static void sntp_set_system_time(uint32_t t)
|
||||
{
|
||||
realtime_stamp = t + time_zone + dst;
|
||||
os_timer_disarm(&sntp_timer);
|
||||
os_timer_setfn(&sntp_timer, (os_timer_func_t *)sntp_time_inc, NULL);
|
||||
os_timer_arm(&sntp_timer, 1000, 1);
|
||||
}
|
||||
|
||||
int settimeofday(const struct timeval* tv, const struct timezone* tz)
|
||||
{
|
||||
if (tz) /*before*/
|
||||
{
|
||||
sntp_set_timezone_in_seconds(tz->tz_minuteswest * 60);
|
||||
// apparently tz->tz_dsttime is a bitfield and should not be further used (cf man)
|
||||
sntp_set_daylight(0);
|
||||
}
|
||||
if (tv) /* after*/
|
||||
{
|
||||
// reset time subsystem
|
||||
tune_timeshift64(tv->tv_sec * 1000000ULL + tv->tv_usec);
|
||||
|
||||
sntp_set_system_time(tv->tv_sec);
|
||||
|
||||
if (_settimeofday_cb)
|
||||
{
|
||||
_settimeofday_cb();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
// reset time subsystem
|
||||
tune_timeshift64(tv->tv_sec * 1000000ULL + tv->tv_usec);
|
||||
|
||||
sntp_set_system_time(tv->tv_sec);
|
||||
|
||||
if (_settimeofday_cb)
|
||||
_settimeofday_cb();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // lwip2 only
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
bool sntp_set_timezone_in_seconds(sint32 timezone);
|
||||
bool sntp_set_timezone_in_seconds(sint32 timezone);
|
||||
|
||||
}
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,9 +1,9 @@
|
||||
/*
|
||||
spiffs_cache.c
|
||||
|
||||
Created on: Jun 23, 2013
|
||||
Author: petera
|
||||
*/
|
||||
* spiffs_cache.c
|
||||
*
|
||||
* Created on: Jun 23, 2013
|
||||
* Author: petera
|
||||
*/
|
||||
|
||||
#include "spiffs.h"
|
||||
#include "spiffs_nucleus.h"
|
||||
@ -12,372 +12,311 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
// returns cached page for give page index, or null if no such cached page
|
||||
static spiffs_cache_page *spiffs_cache_page_get(spiffs *fs, spiffs_page_ix pix)
|
||||
{
|
||||
spiffs_cache *cache = spiffs_get_cache(fs);
|
||||
if ((cache->cpage_use_map & cache->cpage_use_mask) == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int i;
|
||||
for (i = 0; i < cache->cpage_count; i++)
|
||||
{
|
||||
spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i);
|
||||
if ((cache->cpage_use_map & (1 << i)) &&
|
||||
(cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) == 0 &&
|
||||
cp->pix == pix)
|
||||
{
|
||||
//SPIFFS_CACHE_DBG("CACHE_GET: have cache page " _SPIPRIi " for " _SPIPRIpg"\n", i, pix);
|
||||
cp->last_access = cache->last_access;
|
||||
return cp;
|
||||
}
|
||||
}
|
||||
//SPIFFS_CACHE_DBG("CACHE_GET: no cache for " _SPIPRIpg"\n", pix);
|
||||
return 0;
|
||||
// returns cached page for give page index, or null if no such cached page
|
||||
static spiffs_cache_page *spiffs_cache_page_get(spiffs *fs, spiffs_page_ix pix) {
|
||||
spiffs_cache *cache = spiffs_get_cache(fs);
|
||||
if ((cache->cpage_use_map & cache->cpage_use_mask) == 0) return 0;
|
||||
int i;
|
||||
for (i = 0; i < cache->cpage_count; i++) {
|
||||
spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i);
|
||||
if ((cache->cpage_use_map & (1<<i)) &&
|
||||
(cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) == 0 &&
|
||||
cp->pix == pix ) {
|
||||
//SPIFFS_CACHE_DBG("CACHE_GET: have cache page " _SPIPRIi " for " _SPIPRIpg"\n", i, pix);
|
||||
cp->last_access = cache->last_access;
|
||||
return cp;
|
||||
}
|
||||
}
|
||||
//SPIFFS_CACHE_DBG("CACHE_GET: no cache for " _SPIPRIpg"\n", pix);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// frees cached page
|
||||
static s32_t spiffs_cache_page_free(spiffs *fs, int ix, u8_t write_back)
|
||||
{
|
||||
s32_t res = SPIFFS_OK;
|
||||
spiffs_cache *cache = spiffs_get_cache(fs);
|
||||
spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, ix);
|
||||
if (cache->cpage_use_map & (1 << ix))
|
||||
{
|
||||
if (write_back &&
|
||||
(cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) == 0 &&
|
||||
(cp->flags & SPIFFS_CACHE_FLAG_DIRTY))
|
||||
{
|
||||
u8_t *mem = spiffs_get_cache_page(fs, cache, ix);
|
||||
SPIFFS_CACHE_DBG("CACHE_FREE: write cache page " _SPIPRIi " pix " _SPIPRIpg "\n", ix, cp->pix);
|
||||
res = SPIFFS_HAL_WRITE(fs, SPIFFS_PAGE_TO_PADDR(fs, cp->pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), mem);
|
||||
}
|
||||
|
||||
#if SPIFFS_CACHE_WR
|
||||
if (cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR)
|
||||
{
|
||||
SPIFFS_CACHE_DBG("CACHE_FREE: free cache page " _SPIPRIi " objid " _SPIPRIid "\n", ix, cp->obj_id);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
SPIFFS_CACHE_DBG("CACHE_FREE: free cache page " _SPIPRIi " pix " _SPIPRIpg "\n", ix, cp->pix);
|
||||
}
|
||||
cache->cpage_use_map &= ~(1 << ix);
|
||||
cp->flags = 0;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
// removes the oldest accessed cached page
|
||||
static s32_t spiffs_cache_page_remove_oldest(spiffs *fs, u8_t flag_mask, u8_t flags)
|
||||
{
|
||||
s32_t res = SPIFFS_OK;
|
||||
spiffs_cache *cache = spiffs_get_cache(fs);
|
||||
|
||||
if ((cache->cpage_use_map & cache->cpage_use_mask) != cache->cpage_use_mask)
|
||||
{
|
||||
// at least one free cpage
|
||||
return SPIFFS_OK;
|
||||
}
|
||||
|
||||
// all busy, scan thru all to find the cpage which has oldest access
|
||||
int i;
|
||||
int cand_ix = -1;
|
||||
u32_t oldest_val = 0;
|
||||
for (i = 0; i < cache->cpage_count; i++)
|
||||
{
|
||||
spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i);
|
||||
if ((cache->last_access - cp->last_access) > oldest_val &&
|
||||
(cp->flags & flag_mask) == flags)
|
||||
{
|
||||
oldest_val = cache->last_access - cp->last_access;
|
||||
cand_ix = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (cand_ix >= 0)
|
||||
{
|
||||
res = spiffs_cache_page_free(fs, cand_ix, 1);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
// allocates a new cached page and returns it, or null if all cache pages are busy
|
||||
static spiffs_cache_page *spiffs_cache_page_allocate(spiffs *fs)
|
||||
{
|
||||
spiffs_cache *cache = spiffs_get_cache(fs);
|
||||
if (cache->cpage_use_map == 0xffffffff)
|
||||
{
|
||||
// out of cache memory
|
||||
return 0;
|
||||
}
|
||||
int i;
|
||||
for (i = 0; i < cache->cpage_count; i++)
|
||||
{
|
||||
if ((cache->cpage_use_map & (1 << i)) == 0)
|
||||
{
|
||||
spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i);
|
||||
cache->cpage_use_map |= (1 << i);
|
||||
cp->last_access = cache->last_access;
|
||||
//SPIFFS_CACHE_DBG("CACHE_ALLO: allocated cache page " _SPIPRIi"\n", i);
|
||||
return cp;
|
||||
}
|
||||
}
|
||||
// out of cache entries
|
||||
return 0;
|
||||
}
|
||||
|
||||
// drops the cache page for give page index
|
||||
void spiffs_cache_drop_page(spiffs *fs, spiffs_page_ix pix)
|
||||
{
|
||||
spiffs_cache_page *cp = spiffs_cache_page_get(fs, pix);
|
||||
if (cp)
|
||||
{
|
||||
spiffs_cache_page_free(fs, cp->ix, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------
|
||||
|
||||
// reads from spi flash or the cache
|
||||
s32_t spiffs_phys_rd(
|
||||
spiffs *fs,
|
||||
u8_t op,
|
||||
spiffs_file fh,
|
||||
u32_t addr,
|
||||
u32_t len,
|
||||
u8_t *dst)
|
||||
{
|
||||
(void)fh;
|
||||
s32_t res = SPIFFS_OK;
|
||||
spiffs_cache *cache = spiffs_get_cache(fs);
|
||||
spiffs_cache_page *cp = spiffs_cache_page_get(fs, SPIFFS_PADDR_TO_PAGE(fs, addr));
|
||||
cache->last_access++;
|
||||
if (cp)
|
||||
{
|
||||
// we've already got one, you see
|
||||
#if SPIFFS_CACHE_STATS
|
||||
fs->cache_hits++;
|
||||
#endif
|
||||
cp->last_access = cache->last_access;
|
||||
u8_t *mem = spiffs_get_cache_page(fs, cache, cp->ix);
|
||||
_SPIFFS_MEMCPY(dst, &mem[SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr)], len);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((op & SPIFFS_OP_TYPE_MASK) == SPIFFS_OP_T_OBJ_LU2)
|
||||
{
|
||||
// for second layer lookup functions, we do not cache in order to prevent shredding
|
||||
return SPIFFS_HAL_READ(fs, addr, len, dst);
|
||||
}
|
||||
#if SPIFFS_CACHE_STATS
|
||||
fs->cache_misses++;
|
||||
#endif
|
||||
// this operation will always free one cache page (unless all already free),
|
||||
// the result code stems from the write operation of the possibly freed cache page
|
||||
res = spiffs_cache_page_remove_oldest(fs, SPIFFS_CACHE_FLAG_TYPE_WR, 0);
|
||||
|
||||
cp = spiffs_cache_page_allocate(fs);
|
||||
if (cp)
|
||||
{
|
||||
cp->flags = SPIFFS_CACHE_FLAG_WRTHRU;
|
||||
cp->pix = SPIFFS_PADDR_TO_PAGE(fs, addr);
|
||||
SPIFFS_CACHE_DBG("CACHE_ALLO: allocated cache page " _SPIPRIi " for pix " _SPIPRIpg "\n", cp->ix, cp->pix);
|
||||
|
||||
s32_t res2 = SPIFFS_HAL_READ(fs,
|
||||
addr - SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr),
|
||||
SPIFFS_CFG_LOG_PAGE_SZ(fs),
|
||||
spiffs_get_cache_page(fs, cache, cp->ix));
|
||||
if (res2 != SPIFFS_OK)
|
||||
{
|
||||
// honor read failure before possible write failure (bad idea?)
|
||||
res = res2;
|
||||
}
|
||||
u8_t *mem = spiffs_get_cache_page(fs, cache, cp->ix);
|
||||
_SPIFFS_MEMCPY(dst, &mem[SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr)], len);
|
||||
}
|
||||
else
|
||||
{
|
||||
// this will never happen, last resort for sake of symmetry
|
||||
s32_t res2 = SPIFFS_HAL_READ(fs, addr, len, dst);
|
||||
if (res2 != SPIFFS_OK)
|
||||
{
|
||||
// honor read failure before possible write failure (bad idea?)
|
||||
res = res2;
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// writes to spi flash and/or the cache
|
||||
s32_t spiffs_phys_wr(
|
||||
spiffs *fs,
|
||||
u8_t op,
|
||||
spiffs_file fh,
|
||||
u32_t addr,
|
||||
u32_t len,
|
||||
u8_t *src)
|
||||
{
|
||||
(void)fh;
|
||||
spiffs_page_ix pix = SPIFFS_PADDR_TO_PAGE(fs, addr);
|
||||
spiffs_cache *cache = spiffs_get_cache(fs);
|
||||
spiffs_cache_page *cp = spiffs_cache_page_get(fs, pix);
|
||||
|
||||
if (cp && (op & SPIFFS_OP_COM_MASK) != SPIFFS_OP_C_WRTHRU)
|
||||
{
|
||||
// have a cache page
|
||||
// copy in data to cache page
|
||||
|
||||
if ((op & SPIFFS_OP_COM_MASK) == SPIFFS_OP_C_DELE &&
|
||||
(op & SPIFFS_OP_TYPE_MASK) != SPIFFS_OP_T_OBJ_LU)
|
||||
{
|
||||
// page is being deleted, wipe from cache - unless it is a lookup page
|
||||
spiffs_cache_page_free(fs, cp->ix, 0);
|
||||
return SPIFFS_HAL_WRITE(fs, addr, len, src);
|
||||
}
|
||||
|
||||
u8_t *mem = spiffs_get_cache_page(fs, cache, cp->ix);
|
||||
_SPIFFS_MEMCPY(&mem[SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr)], src, len);
|
||||
|
||||
cache->last_access++;
|
||||
cp->last_access = cache->last_access;
|
||||
|
||||
if (cp->flags & SPIFFS_CACHE_FLAG_WRTHRU)
|
||||
{
|
||||
// page is being updated, no write-cache, just pass thru
|
||||
return SPIFFS_HAL_WRITE(fs, addr, len, src);
|
||||
}
|
||||
else
|
||||
{
|
||||
return SPIFFS_OK;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// no cache page, no write cache - just write thru
|
||||
return SPIFFS_HAL_WRITE(fs, addr, len, src);
|
||||
}
|
||||
// frees cached page
|
||||
static s32_t spiffs_cache_page_free(spiffs *fs, int ix, u8_t write_back) {
|
||||
s32_t res = SPIFFS_OK;
|
||||
spiffs_cache *cache = spiffs_get_cache(fs);
|
||||
spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, ix);
|
||||
if (cache->cpage_use_map & (1<<ix)) {
|
||||
if (write_back &&
|
||||
(cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) == 0 &&
|
||||
(cp->flags & SPIFFS_CACHE_FLAG_DIRTY)) {
|
||||
u8_t *mem = spiffs_get_cache_page(fs, cache, ix);
|
||||
SPIFFS_CACHE_DBG("CACHE_FREE: write cache page " _SPIPRIi " pix " _SPIPRIpg "\n", ix, cp->pix);
|
||||
res = SPIFFS_HAL_WRITE(fs, SPIFFS_PAGE_TO_PADDR(fs, cp->pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), mem);
|
||||
}
|
||||
|
||||
#if SPIFFS_CACHE_WR
|
||||
// returns the cache page that this fd refers, or null if no cache page
|
||||
spiffs_cache_page *spiffs_cache_page_get_by_fd(spiffs *fs, spiffs_fd *fd)
|
||||
if (cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) {
|
||||
SPIFFS_CACHE_DBG("CACHE_FREE: free cache page " _SPIPRIi " objid " _SPIPRIid "\n", ix, cp->obj_id);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
spiffs_cache *cache = spiffs_get_cache(fs);
|
||||
SPIFFS_CACHE_DBG("CACHE_FREE: free cache page " _SPIPRIi " pix " _SPIPRIpg "\n", ix, cp->pix);
|
||||
}
|
||||
cache->cpage_use_map &= ~(1 << ix);
|
||||
cp->flags = 0;
|
||||
}
|
||||
|
||||
if ((cache->cpage_use_map & cache->cpage_use_mask) == 0)
|
||||
{
|
||||
// all cpages free, no cpage cannot be assigned to obj_id
|
||||
return 0;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int i;
|
||||
for (i = 0; i < cache->cpage_count; i++)
|
||||
{
|
||||
spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i);
|
||||
if ((cache->cpage_use_map & (1 << i)) &&
|
||||
(cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) &&
|
||||
cp->obj_id == fd->obj_id)
|
||||
{
|
||||
return cp;
|
||||
}
|
||||
}
|
||||
// removes the oldest accessed cached page
|
||||
static s32_t spiffs_cache_page_remove_oldest(spiffs *fs, u8_t flag_mask, u8_t flags) {
|
||||
s32_t res = SPIFFS_OK;
|
||||
spiffs_cache *cache = spiffs_get_cache(fs);
|
||||
|
||||
return 0;
|
||||
if ((cache->cpage_use_map & cache->cpage_use_mask) != cache->cpage_use_mask) {
|
||||
// at least one free cpage
|
||||
return SPIFFS_OK;
|
||||
}
|
||||
|
||||
// all busy, scan thru all to find the cpage which has oldest access
|
||||
int i;
|
||||
int cand_ix = -1;
|
||||
u32_t oldest_val = 0;
|
||||
for (i = 0; i < cache->cpage_count; i++) {
|
||||
spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i);
|
||||
if ((cache->last_access - cp->last_access) > oldest_val &&
|
||||
(cp->flags & flag_mask) == flags) {
|
||||
oldest_val = cache->last_access - cp->last_access;
|
||||
cand_ix = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (cand_ix >= 0) {
|
||||
res = spiffs_cache_page_free(fs, cand_ix, 1);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
// allocates a new cached page and returns it, or null if all cache pages are busy
|
||||
static spiffs_cache_page *spiffs_cache_page_allocate(spiffs *fs) {
|
||||
spiffs_cache *cache = spiffs_get_cache(fs);
|
||||
if (cache->cpage_use_map == 0xffffffff) {
|
||||
// out of cache memory
|
||||
return 0;
|
||||
}
|
||||
int i;
|
||||
for (i = 0; i < cache->cpage_count; i++) {
|
||||
if ((cache->cpage_use_map & (1<<i)) == 0) {
|
||||
spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i);
|
||||
cache->cpage_use_map |= (1<<i);
|
||||
cp->last_access = cache->last_access;
|
||||
//SPIFFS_CACHE_DBG("CACHE_ALLO: allocated cache page " _SPIPRIi"\n", i);
|
||||
return cp;
|
||||
}
|
||||
}
|
||||
// out of cache entries
|
||||
return 0;
|
||||
}
|
||||
|
||||
// drops the cache page for give page index
|
||||
void spiffs_cache_drop_page(spiffs *fs, spiffs_page_ix pix) {
|
||||
spiffs_cache_page *cp = spiffs_cache_page_get(fs, pix);
|
||||
if (cp) {
|
||||
spiffs_cache_page_free(fs, cp->ix, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------
|
||||
|
||||
// reads from spi flash or the cache
|
||||
s32_t spiffs_phys_rd(
|
||||
spiffs *fs,
|
||||
u8_t op,
|
||||
spiffs_file fh,
|
||||
u32_t addr,
|
||||
u32_t len,
|
||||
u8_t *dst) {
|
||||
(void)fh;
|
||||
s32_t res = SPIFFS_OK;
|
||||
spiffs_cache *cache = spiffs_get_cache(fs);
|
||||
spiffs_cache_page *cp = spiffs_cache_page_get(fs, SPIFFS_PADDR_TO_PAGE(fs, addr));
|
||||
cache->last_access++;
|
||||
if (cp) {
|
||||
// we've already got one, you see
|
||||
#if SPIFFS_CACHE_STATS
|
||||
fs->cache_hits++;
|
||||
#endif
|
||||
cp->last_access = cache->last_access;
|
||||
u8_t *mem = spiffs_get_cache_page(fs, cache, cp->ix);
|
||||
_SPIFFS_MEMCPY(dst, &mem[SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr)], len);
|
||||
} else {
|
||||
if ((op & SPIFFS_OP_TYPE_MASK) == SPIFFS_OP_T_OBJ_LU2) {
|
||||
// for second layer lookup functions, we do not cache in order to prevent shredding
|
||||
return SPIFFS_HAL_READ(fs, addr, len, dst);
|
||||
}
|
||||
#if SPIFFS_CACHE_STATS
|
||||
fs->cache_misses++;
|
||||
#endif
|
||||
// this operation will always free one cache page (unless all already free),
|
||||
// the result code stems from the write operation of the possibly freed cache page
|
||||
res = spiffs_cache_page_remove_oldest(fs, SPIFFS_CACHE_FLAG_TYPE_WR, 0);
|
||||
|
||||
cp = spiffs_cache_page_allocate(fs);
|
||||
if (cp) {
|
||||
cp->flags = SPIFFS_CACHE_FLAG_WRTHRU;
|
||||
cp->pix = SPIFFS_PADDR_TO_PAGE(fs, addr);
|
||||
SPIFFS_CACHE_DBG("CACHE_ALLO: allocated cache page " _SPIPRIi " for pix " _SPIPRIpg "\n", cp->ix, cp->pix);
|
||||
|
||||
s32_t res2 = SPIFFS_HAL_READ(fs,
|
||||
addr - SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr),
|
||||
SPIFFS_CFG_LOG_PAGE_SZ(fs),
|
||||
spiffs_get_cache_page(fs, cache, cp->ix));
|
||||
if (res2 != SPIFFS_OK) {
|
||||
// honor read failure before possible write failure (bad idea?)
|
||||
res = res2;
|
||||
}
|
||||
u8_t *mem = spiffs_get_cache_page(fs, cache, cp->ix);
|
||||
_SPIFFS_MEMCPY(dst, &mem[SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr)], len);
|
||||
} else {
|
||||
// this will never happen, last resort for sake of symmetry
|
||||
s32_t res2 = SPIFFS_HAL_READ(fs, addr, len, dst);
|
||||
if (res2 != SPIFFS_OK) {
|
||||
// honor read failure before possible write failure (bad idea?)
|
||||
res = res2;
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// writes to spi flash and/or the cache
|
||||
s32_t spiffs_phys_wr(
|
||||
spiffs *fs,
|
||||
u8_t op,
|
||||
spiffs_file fh,
|
||||
u32_t addr,
|
||||
u32_t len,
|
||||
u8_t *src) {
|
||||
(void)fh;
|
||||
spiffs_page_ix pix = SPIFFS_PADDR_TO_PAGE(fs, addr);
|
||||
spiffs_cache *cache = spiffs_get_cache(fs);
|
||||
spiffs_cache_page *cp = spiffs_cache_page_get(fs, pix);
|
||||
|
||||
if (cp && (op & SPIFFS_OP_COM_MASK) != SPIFFS_OP_C_WRTHRU) {
|
||||
// have a cache page
|
||||
// copy in data to cache page
|
||||
|
||||
if ((op & SPIFFS_OP_COM_MASK) == SPIFFS_OP_C_DELE &&
|
||||
(op & SPIFFS_OP_TYPE_MASK) != SPIFFS_OP_T_OBJ_LU) {
|
||||
// page is being deleted, wipe from cache - unless it is a lookup page
|
||||
spiffs_cache_page_free(fs, cp->ix, 0);
|
||||
return SPIFFS_HAL_WRITE(fs, addr, len, src);
|
||||
}
|
||||
|
||||
// allocates a new cache page and refers this to given fd - flushes an old cache
|
||||
// page if all cache is busy
|
||||
spiffs_cache_page *spiffs_cache_page_allocate_by_fd(spiffs *fs, spiffs_fd *fd)
|
||||
{
|
||||
// before this function is called, it is ensured that there is no already existing
|
||||
// cache page with same object id
|
||||
spiffs_cache_page_remove_oldest(fs, SPIFFS_CACHE_FLAG_TYPE_WR, 0);
|
||||
spiffs_cache_page *cp = spiffs_cache_page_allocate(fs);
|
||||
if (cp == 0)
|
||||
{
|
||||
// could not get cache page
|
||||
return 0;
|
||||
}
|
||||
u8_t *mem = spiffs_get_cache_page(fs, cache, cp->ix);
|
||||
_SPIFFS_MEMCPY(&mem[SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr)], src, len);
|
||||
|
||||
cp->flags = SPIFFS_CACHE_FLAG_TYPE_WR;
|
||||
cp->obj_id = fd->obj_id;
|
||||
fd->cache_page = cp;
|
||||
SPIFFS_CACHE_DBG("CACHE_ALLO: allocated cache page " _SPIPRIi " for fd " _SPIPRIfd ":" _SPIPRIid "\n", cp->ix, fd->file_nbr, fd->obj_id);
|
||||
return cp;
|
||||
cache->last_access++;
|
||||
cp->last_access = cache->last_access;
|
||||
|
||||
if (cp->flags & SPIFFS_CACHE_FLAG_WRTHRU) {
|
||||
// page is being updated, no write-cache, just pass thru
|
||||
return SPIFFS_HAL_WRITE(fs, addr, len, src);
|
||||
} else {
|
||||
return SPIFFS_OK;
|
||||
}
|
||||
} else {
|
||||
// no cache page, no write cache - just write thru
|
||||
return SPIFFS_HAL_WRITE(fs, addr, len, src);
|
||||
}
|
||||
}
|
||||
|
||||
// unrefers all fds that this cache page refers to and releases the cache page
|
||||
void spiffs_cache_fd_release(spiffs *fs, spiffs_cache_page *cp)
|
||||
{
|
||||
if (cp == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
u32_t i;
|
||||
spiffs_fd *fds = (spiffs_fd *)fs->fd_space;
|
||||
for (i = 0; i < fs->fd_count; i++)
|
||||
{
|
||||
spiffs_fd *cur_fd = &fds[i];
|
||||
if (cur_fd->file_nbr != 0 && cur_fd->cache_page == cp)
|
||||
{
|
||||
cur_fd->cache_page = 0;
|
||||
}
|
||||
}
|
||||
spiffs_cache_page_free(fs, cp->ix, 0);
|
||||
#if SPIFFS_CACHE_WR
|
||||
// returns the cache page that this fd refers, or null if no cache page
|
||||
spiffs_cache_page *spiffs_cache_page_get_by_fd(spiffs *fs, spiffs_fd *fd) {
|
||||
spiffs_cache *cache = spiffs_get_cache(fs);
|
||||
|
||||
cp->obj_id = 0;
|
||||
if ((cache->cpage_use_map & cache->cpage_use_mask) == 0) {
|
||||
// all cpages free, no cpage cannot be assigned to obj_id
|
||||
return 0;
|
||||
}
|
||||
|
||||
int i;
|
||||
for (i = 0; i < cache->cpage_count; i++) {
|
||||
spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i);
|
||||
if ((cache->cpage_use_map & (1<<i)) &&
|
||||
(cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) &&
|
||||
cp->obj_id == fd->obj_id) {
|
||||
return cp;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// allocates a new cache page and refers this to given fd - flushes an old cache
|
||||
// page if all cache is busy
|
||||
spiffs_cache_page *spiffs_cache_page_allocate_by_fd(spiffs *fs, spiffs_fd *fd) {
|
||||
// before this function is called, it is ensured that there is no already existing
|
||||
// cache page with same object id
|
||||
spiffs_cache_page_remove_oldest(fs, SPIFFS_CACHE_FLAG_TYPE_WR, 0);
|
||||
spiffs_cache_page *cp = spiffs_cache_page_allocate(fs);
|
||||
if (cp == 0) {
|
||||
// could not get cache page
|
||||
return 0;
|
||||
}
|
||||
|
||||
cp->flags = SPIFFS_CACHE_FLAG_TYPE_WR;
|
||||
cp->obj_id = fd->obj_id;
|
||||
fd->cache_page = cp;
|
||||
SPIFFS_CACHE_DBG("CACHE_ALLO: allocated cache page " _SPIPRIi " for fd " _SPIPRIfd ":" _SPIPRIid "\n", cp->ix, fd->file_nbr, fd->obj_id);
|
||||
return cp;
|
||||
}
|
||||
|
||||
// unrefers all fds that this cache page refers to and releases the cache page
|
||||
void spiffs_cache_fd_release(spiffs *fs, spiffs_cache_page *cp) {
|
||||
if (cp == 0) return;
|
||||
u32_t i;
|
||||
spiffs_fd *fds = (spiffs_fd *)fs->fd_space;
|
||||
for (i = 0; i < fs->fd_count; i++) {
|
||||
spiffs_fd *cur_fd = &fds[i];
|
||||
if (cur_fd->file_nbr != 0 && cur_fd->cache_page == cp) {
|
||||
cur_fd->cache_page = 0;
|
||||
}
|
||||
}
|
||||
spiffs_cache_page_free(fs, cp->ix, 0);
|
||||
|
||||
cp->obj_id = 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// initializes the cache
|
||||
void spiffs_cache_init(spiffs *fs)
|
||||
{
|
||||
if (fs->cache == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
u32_t sz = fs->cache_size;
|
||||
u32_t cache_mask = 0;
|
||||
int i;
|
||||
int cache_entries =
|
||||
(sz - sizeof(spiffs_cache)) / (SPIFFS_CACHE_PAGE_SIZE(fs));
|
||||
if (cache_entries <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// initializes the cache
|
||||
void spiffs_cache_init(spiffs *fs) {
|
||||
if (fs->cache == 0) return;
|
||||
u32_t sz = fs->cache_size;
|
||||
u32_t cache_mask = 0;
|
||||
int i;
|
||||
int cache_entries =
|
||||
(sz - sizeof(spiffs_cache)) / (SPIFFS_CACHE_PAGE_SIZE(fs));
|
||||
if (cache_entries <= 0) return;
|
||||
|
||||
for (i = 0; i < cache_entries; i++)
|
||||
{
|
||||
cache_mask <<= 1;
|
||||
cache_mask |= 1;
|
||||
}
|
||||
for (i = 0; i < cache_entries; i++) {
|
||||
cache_mask <<= 1;
|
||||
cache_mask |= 1;
|
||||
}
|
||||
|
||||
spiffs_cache cache;
|
||||
memset(&cache, 0, sizeof(spiffs_cache));
|
||||
cache.cpage_count = cache_entries;
|
||||
cache.cpages = (u8_t *)((u8_t *)fs->cache + sizeof(spiffs_cache));
|
||||
spiffs_cache cache;
|
||||
memset(&cache, 0, sizeof(spiffs_cache));
|
||||
cache.cpage_count = cache_entries;
|
||||
cache.cpages = (u8_t *)((u8_t *)fs->cache + sizeof(spiffs_cache));
|
||||
|
||||
cache.cpage_use_map = 0xffffffff;
|
||||
cache.cpage_use_mask = cache_mask;
|
||||
_SPIFFS_MEMCPY(fs->cache, &cache, sizeof(spiffs_cache));
|
||||
cache.cpage_use_map = 0xffffffff;
|
||||
cache.cpage_use_mask = cache_mask;
|
||||
_SPIFFS_MEMCPY(fs->cache, &cache, sizeof(spiffs_cache));
|
||||
|
||||
spiffs_cache *c = spiffs_get_cache(fs);
|
||||
spiffs_cache *c = spiffs_get_cache(fs);
|
||||
|
||||
memset(c->cpages, 0, c->cpage_count * SPIFFS_CACHE_PAGE_SIZE(fs));
|
||||
memset(c->cpages, 0, c->cpage_count * SPIFFS_CACHE_PAGE_SIZE(fs));
|
||||
|
||||
c->cpage_use_map &= ~(c->cpage_use_mask);
|
||||
for (i = 0; i < cache.cpage_count; i++)
|
||||
{
|
||||
spiffs_get_cache_page_hdr(fs, c, i)->ix = i;
|
||||
}
|
||||
}
|
||||
c->cpage_use_map &= ~(c->cpage_use_mask);
|
||||
for (i = 0; i < cache.cpage_count; i++) {
|
||||
spiffs_get_cache_page_hdr(fs, c, i)->ix = i;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
#endif // SPIFFS_CACHE
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,9 +1,9 @@
|
||||
/*
|
||||
spiffs_config.h
|
||||
|
||||
Created on: Jul 3, 2013
|
||||
Author: petera
|
||||
*/
|
||||
* spiffs_config.h
|
||||
*
|
||||
* Created on: Jul 3, 2013
|
||||
* Author: petera
|
||||
*/
|
||||
|
||||
#ifndef SPIFFS_CONFIG_H_
|
||||
#define SPIFFS_CONFIG_H_
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,112 +1,112 @@
|
||||
/*
|
||||
spiffs_nucleus.h
|
||||
* spiffs_nucleus.h
|
||||
*
|
||||
* Created on: Jun 15, 2013
|
||||
* Author: petera
|
||||
*/
|
||||
|
||||
Created on: Jun 15, 2013
|
||||
Author: petera
|
||||
*/
|
||||
|
||||
/* SPIFFS layout
|
||||
|
||||
spiffs is designed for following spi flash characteristics:
|
||||
- only big areas of data (blocks) can be erased
|
||||
- erasing resets all bits in a block to ones
|
||||
- writing pulls ones to zeroes
|
||||
- zeroes cannot be pulled to ones, without erase
|
||||
- wear leveling
|
||||
|
||||
spiffs is also meant to be run on embedded, memory constraint devices.
|
||||
|
||||
Entire area is divided in blocks. Entire area is also divided in pages.
|
||||
Each block contains same number of pages. A page cannot be erased, but a
|
||||
block can be erased.
|
||||
|
||||
Entire area must be block_size * x
|
||||
page_size must be block_size / (2^y) where y > 2
|
||||
|
||||
ex: area = 1024*1024 bytes, block size = 65536 bytes, page size = 256 bytes
|
||||
|
||||
BLOCK 0 PAGE 0 object lookup 1
|
||||
PAGE 1 object lookup 2
|
||||
...
|
||||
PAGE n-1 object lookup n
|
||||
PAGE n object data 1
|
||||
PAGE n+1 object data 2
|
||||
...
|
||||
PAGE n+m-1 object data m
|
||||
|
||||
BLOCK 1 PAGE n+m object lookup 1
|
||||
PAGE n+m+1 object lookup 2
|
||||
...
|
||||
PAGE 2n+m-1 object lookup n
|
||||
PAGE 2n+m object data 1
|
||||
PAGE 2n+m object data 2
|
||||
...
|
||||
PAGE 2n+2m-1 object data m
|
||||
...
|
||||
|
||||
n is number of object lookup pages, which is number of pages needed to index all pages
|
||||
in a block by object id
|
||||
: block_size / page_size * sizeof(obj_id) / page_size
|
||||
m is number data pages, which is number of pages in block minus number of lookup pages
|
||||
: block_size / page_size - block_size / page_size * sizeof(obj_id) / page_size
|
||||
thus, n+m is total number of pages in a block
|
||||
: block_size / page_size
|
||||
|
||||
ex: n = 65536/256*2/256 = 2, m = 65536/256 - 2 = 254 => n+m = 65536/256 = 256
|
||||
|
||||
Object lookup pages contain object id entries. Each entry represent the corresponding
|
||||
data page.
|
||||
Assuming a 16 bit object id, an object id being 0xffff represents a free page.
|
||||
An object id being 0x0000 represents a deleted page.
|
||||
|
||||
ex: page 0 : lookup : 0008 0001 0aaa ffff ffff ffff ffff ffff ..
|
||||
page 1 : lookup : ffff ffff ffff ffff ffff ffff ffff ffff ..
|
||||
page 2 : data : data for object id 0008
|
||||
page 3 : data : data for object id 0001
|
||||
page 4 : data : data for object id 0aaa
|
||||
...
|
||||
|
||||
|
||||
Object data pages can be either object index pages or object content.
|
||||
All object data pages contains a data page header, containing object id and span index.
|
||||
The span index denotes the object page ordering amongst data pages with same object id.
|
||||
This applies to both object index pages (when index spans more than one page of entries),
|
||||
and object data pages.
|
||||
An object index page contains page entries pointing to object content page. The entry index
|
||||
in a object index page correlates to the span index in the actual object data page.
|
||||
The first object index page (span index 0) is called object index header page, and also
|
||||
contains object flags (directory/file), size, object name etc.
|
||||
|
||||
ex:
|
||||
BLOCK 1
|
||||
PAGE 256: objectl lookup page 1
|
||||
[*123] [ 123] [ 123] [ 123]
|
||||
[ 123] [*123] [ 123] [ 123]
|
||||
[free] [free] [free] [free] ...
|
||||
PAGE 257: objectl lookup page 2
|
||||
[free] [free] [free] [free] ...
|
||||
PAGE 258: object index page (header)
|
||||
obj.id:0123 span.ix:0000 flags:INDEX
|
||||
size:1600 name:ex.txt type:file
|
||||
[259] [260] [261] [262]
|
||||
PAGE 259: object data page
|
||||
obj.id:0123 span.ix:0000 flags:DATA
|
||||
PAGE 260: object data page
|
||||
obj.id:0123 span.ix:0001 flags:DATA
|
||||
PAGE 261: object data page
|
||||
obj.id:0123 span.ix:0002 flags:DATA
|
||||
PAGE 262: object data page
|
||||
obj.id:0123 span.ix:0003 flags:DATA
|
||||
PAGE 263: object index page
|
||||
obj.id:0123 span.ix:0001 flags:INDEX
|
||||
[264] [265] [fre] [fre]
|
||||
[fre] [fre] [fre] [fre]
|
||||
PAGE 264: object data page
|
||||
obj.id:0123 span.ix:0004 flags:DATA
|
||||
PAGE 265: object data page
|
||||
obj.id:0123 span.ix:0005 flags:DATA
|
||||
|
||||
*/
|
||||
/* SPIFFS layout
|
||||
*
|
||||
* spiffs is designed for following spi flash characteristics:
|
||||
* - only big areas of data (blocks) can be erased
|
||||
* - erasing resets all bits in a block to ones
|
||||
* - writing pulls ones to zeroes
|
||||
* - zeroes cannot be pulled to ones, without erase
|
||||
* - wear leveling
|
||||
*
|
||||
* spiffs is also meant to be run on embedded, memory constraint devices.
|
||||
*
|
||||
* Entire area is divided in blocks. Entire area is also divided in pages.
|
||||
* Each block contains same number of pages. A page cannot be erased, but a
|
||||
* block can be erased.
|
||||
*
|
||||
* Entire area must be block_size * x
|
||||
* page_size must be block_size / (2^y) where y > 2
|
||||
*
|
||||
* ex: area = 1024*1024 bytes, block size = 65536 bytes, page size = 256 bytes
|
||||
*
|
||||
* BLOCK 0 PAGE 0 object lookup 1
|
||||
* PAGE 1 object lookup 2
|
||||
* ...
|
||||
* PAGE n-1 object lookup n
|
||||
* PAGE n object data 1
|
||||
* PAGE n+1 object data 2
|
||||
* ...
|
||||
* PAGE n+m-1 object data m
|
||||
*
|
||||
* BLOCK 1 PAGE n+m object lookup 1
|
||||
* PAGE n+m+1 object lookup 2
|
||||
* ...
|
||||
* PAGE 2n+m-1 object lookup n
|
||||
* PAGE 2n+m object data 1
|
||||
* PAGE 2n+m object data 2
|
||||
* ...
|
||||
* PAGE 2n+2m-1 object data m
|
||||
* ...
|
||||
*
|
||||
* n is number of object lookup pages, which is number of pages needed to index all pages
|
||||
* in a block by object id
|
||||
* : block_size / page_size * sizeof(obj_id) / page_size
|
||||
* m is number data pages, which is number of pages in block minus number of lookup pages
|
||||
* : block_size / page_size - block_size / page_size * sizeof(obj_id) / page_size
|
||||
* thus, n+m is total number of pages in a block
|
||||
* : block_size / page_size
|
||||
*
|
||||
* ex: n = 65536/256*2/256 = 2, m = 65536/256 - 2 = 254 => n+m = 65536/256 = 256
|
||||
*
|
||||
* Object lookup pages contain object id entries. Each entry represent the corresponding
|
||||
* data page.
|
||||
* Assuming a 16 bit object id, an object id being 0xffff represents a free page.
|
||||
* An object id being 0x0000 represents a deleted page.
|
||||
*
|
||||
* ex: page 0 : lookup : 0008 0001 0aaa ffff ffff ffff ffff ffff ..
|
||||
* page 1 : lookup : ffff ffff ffff ffff ffff ffff ffff ffff ..
|
||||
* page 2 : data : data for object id 0008
|
||||
* page 3 : data : data for object id 0001
|
||||
* page 4 : data : data for object id 0aaa
|
||||
* ...
|
||||
*
|
||||
*
|
||||
* Object data pages can be either object index pages or object content.
|
||||
* All object data pages contains a data page header, containing object id and span index.
|
||||
* The span index denotes the object page ordering amongst data pages with same object id.
|
||||
* This applies to both object index pages (when index spans more than one page of entries),
|
||||
* and object data pages.
|
||||
* An object index page contains page entries pointing to object content page. The entry index
|
||||
* in a object index page correlates to the span index in the actual object data page.
|
||||
* The first object index page (span index 0) is called object index header page, and also
|
||||
* contains object flags (directory/file), size, object name etc.
|
||||
*
|
||||
* ex:
|
||||
* BLOCK 1
|
||||
* PAGE 256: objectl lookup page 1
|
||||
* [*123] [ 123] [ 123] [ 123]
|
||||
* [ 123] [*123] [ 123] [ 123]
|
||||
* [free] [free] [free] [free] ...
|
||||
* PAGE 257: objectl lookup page 2
|
||||
* [free] [free] [free] [free] ...
|
||||
* PAGE 258: object index page (header)
|
||||
* obj.id:0123 span.ix:0000 flags:INDEX
|
||||
* size:1600 name:ex.txt type:file
|
||||
* [259] [260] [261] [262]
|
||||
* PAGE 259: object data page
|
||||
* obj.id:0123 span.ix:0000 flags:DATA
|
||||
* PAGE 260: object data page
|
||||
* obj.id:0123 span.ix:0001 flags:DATA
|
||||
* PAGE 261: object data page
|
||||
* obj.id:0123 span.ix:0002 flags:DATA
|
||||
* PAGE 262: object data page
|
||||
* obj.id:0123 span.ix:0003 flags:DATA
|
||||
* PAGE 263: object index page
|
||||
* obj.id:0123 span.ix:0001 flags:INDEX
|
||||
* [264] [265] [fre] [fre]
|
||||
* [fre] [fre] [fre] [fre]
|
||||
* PAGE 264: object data page
|
||||
* obj.id:0123 span.ix:0004 flags:DATA
|
||||
* PAGE 265: object data page
|
||||
* obj.id:0123 span.ix:0005 flags:DATA
|
||||
*
|
||||
*/
|
||||
#ifndef SPIFFS_NUCLEUS_H_
|
||||
#define SPIFFS_NUCLEUS_H_
|
||||
|
||||
@ -148,14 +148,14 @@ extern "C" {
|
||||
|
||||
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
/* For GCC and clang */
|
||||
/* For GCC and clang */
|
||||
#define SPIFFS_PACKED __attribute__((packed))
|
||||
#elif defined(__ICCARM__) || defined(__CC_ARM)
|
||||
/* For IAR ARM and Keil MDK-ARM compilers */
|
||||
/* For IAR ARM and Keil MDK-ARM compilers */
|
||||
#define SPIFFS_PACKED
|
||||
|
||||
#else
|
||||
/* Unknown compiler */
|
||||
/* Unknown compiler */
|
||||
#define SPIFFS_PACKED
|
||||
#endif
|
||||
|
||||
@ -342,7 +342,7 @@ extern "C" {
|
||||
if (((ph).flags & SPIFFS_PH_FLAG_INDEX) != 0) return SPIFFS_ERR_NOT_INDEX; \
|
||||
if (((objid) & SPIFFS_OBJ_ID_IX_FLAG) == 0) return SPIFFS_ERR_NOT_INDEX; \
|
||||
if ((ph).span_ix != (spix)) return SPIFFS_ERR_INDEX_SPAN_MISMATCH;
|
||||
//if ((spix) == 0 && ((ph).flags & SPIFFS_PH_FLAG_IXDELE) == 0) return SPIFFS_ERR_DELETED;
|
||||
//if ((spix) == 0 && ((ph).flags & SPIFFS_PH_FLAG_IXDELE) == 0) return SPIFFS_ERR_DELETED;
|
||||
|
||||
#define SPIFFS_VALIDATE_DATA(ph, objid, spix) \
|
||||
if (((ph).flags & SPIFFS_PH_FLAG_USED) != 0) return SPIFFS_ERR_IS_FREE; \
|
||||
@ -402,85 +402,79 @@ extern "C" {
|
||||
((u8_t *)(&((c)->cpages[(ix) * SPIFFS_CACHE_PAGE_SIZE(fs)])) + sizeof(spiffs_cache_page))
|
||||
|
||||
// cache page struct
|
||||
typedef struct
|
||||
{
|
||||
// cache flags
|
||||
u8_t flags;
|
||||
// cache page index
|
||||
u8_t ix;
|
||||
// last access of this cache page
|
||||
u32_t last_access;
|
||||
union
|
||||
{
|
||||
// type read cache
|
||||
struct
|
||||
{
|
||||
// read cache page index
|
||||
spiffs_page_ix pix;
|
||||
};
|
||||
#if SPIFFS_CACHE_WR
|
||||
// type write cache
|
||||
struct
|
||||
{
|
||||
// write cache
|
||||
spiffs_obj_id obj_id;
|
||||
// offset in cache page
|
||||
u32_t offset;
|
||||
// size of cache page
|
||||
u16_t size;
|
||||
};
|
||||
#endif
|
||||
typedef struct {
|
||||
// cache flags
|
||||
u8_t flags;
|
||||
// cache page index
|
||||
u8_t ix;
|
||||
// last access of this cache page
|
||||
u32_t last_access;
|
||||
union {
|
||||
// type read cache
|
||||
struct {
|
||||
// read cache page index
|
||||
spiffs_page_ix pix;
|
||||
};
|
||||
#if SPIFFS_CACHE_WR
|
||||
// type write cache
|
||||
struct {
|
||||
// write cache
|
||||
spiffs_obj_id obj_id;
|
||||
// offset in cache page
|
||||
u32_t offset;
|
||||
// size of cache page
|
||||
u16_t size;
|
||||
};
|
||||
#endif
|
||||
};
|
||||
} spiffs_cache_page;
|
||||
|
||||
// cache struct
|
||||
typedef struct
|
||||
{
|
||||
u8_t cpage_count;
|
||||
u32_t last_access;
|
||||
u32_t cpage_use_map;
|
||||
u32_t cpage_use_mask;
|
||||
u8_t *cpages;
|
||||
typedef struct {
|
||||
u8_t cpage_count;
|
||||
u32_t last_access;
|
||||
u32_t cpage_use_map;
|
||||
u32_t cpage_use_mask;
|
||||
u8_t *cpages;
|
||||
} spiffs_cache;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
// spiffs nucleus file descriptor
|
||||
typedef struct
|
||||
{
|
||||
// the filesystem of this descriptor
|
||||
spiffs *fs;
|
||||
// number of file descriptor - if 0, the file descriptor is closed
|
||||
spiffs_file file_nbr;
|
||||
// object id - if SPIFFS_OBJ_ID_ERASED, the file was deleted
|
||||
spiffs_obj_id obj_id;
|
||||
// size of the file
|
||||
u32_t size;
|
||||
// cached object index header page index
|
||||
spiffs_page_ix objix_hdr_pix;
|
||||
// cached offset object index page index
|
||||
spiffs_page_ix cursor_objix_pix;
|
||||
// cached offset object index span index
|
||||
spiffs_span_ix cursor_objix_spix;
|
||||
// current absolute offset
|
||||
u32_t offset;
|
||||
// current file descriptor offset (cached)
|
||||
u32_t fdoffset;
|
||||
// fd flags
|
||||
spiffs_flags flags;
|
||||
typedef struct {
|
||||
// the filesystem of this descriptor
|
||||
spiffs *fs;
|
||||
// number of file descriptor - if 0, the file descriptor is closed
|
||||
spiffs_file file_nbr;
|
||||
// object id - if SPIFFS_OBJ_ID_ERASED, the file was deleted
|
||||
spiffs_obj_id obj_id;
|
||||
// size of the file
|
||||
u32_t size;
|
||||
// cached object index header page index
|
||||
spiffs_page_ix objix_hdr_pix;
|
||||
// cached offset object index page index
|
||||
spiffs_page_ix cursor_objix_pix;
|
||||
// cached offset object index span index
|
||||
spiffs_span_ix cursor_objix_spix;
|
||||
// current absolute offset
|
||||
u32_t offset;
|
||||
// current file descriptor offset (cached)
|
||||
u32_t fdoffset;
|
||||
// fd flags
|
||||
spiffs_flags flags;
|
||||
#if SPIFFS_CACHE_WR
|
||||
spiffs_cache_page *cache_page;
|
||||
spiffs_cache_page *cache_page;
|
||||
#endif
|
||||
#if SPIFFS_TEMPORAL_FD_CACHE
|
||||
// djb2 hash of filename
|
||||
u32_t name_hash;
|
||||
// hit score (score == 0 indicates never used fd)
|
||||
u16_t score;
|
||||
// djb2 hash of filename
|
||||
u32_t name_hash;
|
||||
// hit score (score == 0 indicates never used fd)
|
||||
u16_t score;
|
||||
#endif
|
||||
#if SPIFFS_IX_MAP
|
||||
// spiffs index map, if 0 it means unmapped
|
||||
spiffs_ix_map *ix_map;
|
||||
// spiffs index map, if 0 it means unmapped
|
||||
spiffs_ix_map *ix_map;
|
||||
#endif
|
||||
} spiffs_fd;
|
||||
|
||||
@ -490,48 +484,46 @@ typedef struct
|
||||
// page header, part of each page except object lookup pages
|
||||
// NB: this is always aligned when the data page is an object index,
|
||||
// as in this case struct spiffs_page_object_ix is used
|
||||
typedef struct SPIFFS_PACKED
|
||||
{
|
||||
// object id
|
||||
spiffs_obj_id obj_id;
|
||||
// object span index
|
||||
spiffs_span_ix span_ix;
|
||||
// flags
|
||||
u8_t flags;
|
||||
typedef struct SPIFFS_PACKED {
|
||||
// object id
|
||||
spiffs_obj_id obj_id;
|
||||
// object span index
|
||||
spiffs_span_ix span_ix;
|
||||
// flags
|
||||
u8_t flags;
|
||||
} spiffs_page_header;
|
||||
|
||||
// object index header page header
|
||||
typedef struct SPIFFS_PACKED
|
||||
#if SPIFFS_ALIGNED_OBJECT_INDEX_TABLES
|
||||
__attribute((aligned(sizeof(spiffs_page_ix))))
|
||||
__attribute(( aligned(sizeof(spiffs_page_ix)) ))
|
||||
#endif
|
||||
{
|
||||
// common page header
|
||||
spiffs_page_header p_hdr;
|
||||
// alignment
|
||||
u8_t _align[4 - ((sizeof(spiffs_page_header) & 3) == 0 ? 4 : (sizeof(spiffs_page_header) & 3))];
|
||||
// size of object
|
||||
u32_t size;
|
||||
// type of object
|
||||
spiffs_obj_type type;
|
||||
// name of object
|
||||
u8_t name[SPIFFS_OBJ_NAME_LEN];
|
||||
// common page header
|
||||
spiffs_page_header p_hdr;
|
||||
// alignment
|
||||
u8_t _align[4 - ((sizeof(spiffs_page_header)&3)==0 ? 4 : (sizeof(spiffs_page_header)&3))];
|
||||
// size of object
|
||||
u32_t size;
|
||||
// type of object
|
||||
spiffs_obj_type type;
|
||||
// name of object
|
||||
u8_t name[SPIFFS_OBJ_NAME_LEN];
|
||||
#if SPIFFS_OBJ_META_LEN
|
||||
// metadata. not interpreted by SPIFFS in any way.
|
||||
u8_t meta[SPIFFS_OBJ_META_LEN];
|
||||
// metadata. not interpreted by SPIFFS in any way.
|
||||
u8_t meta[SPIFFS_OBJ_META_LEN];
|
||||
#endif
|
||||
} spiffs_page_object_ix_header;
|
||||
|
||||
// object index page header
|
||||
typedef struct SPIFFS_PACKED
|
||||
{
|
||||
spiffs_page_header p_hdr;
|
||||
u8_t _align[4 - ((sizeof(spiffs_page_header) & 3) == 0 ? 4 : (sizeof(spiffs_page_header) & 3))];
|
||||
typedef struct SPIFFS_PACKED {
|
||||
spiffs_page_header p_hdr;
|
||||
u8_t _align[4 - ((sizeof(spiffs_page_header)&3)==0 ? 4 : (sizeof(spiffs_page_header)&3))];
|
||||
} spiffs_page_object_ix;
|
||||
|
||||
// callback func for object lookup visitor
|
||||
typedef s32_t (*spiffs_visitor_f)(spiffs *fs, spiffs_obj_id id, spiffs_block_ix bix, int ix_entry,
|
||||
const void *user_const_p, void *user_var_p);
|
||||
const void *user_const_p, void *user_var_p);
|
||||
|
||||
|
||||
#if SPIFFS_CACHE
|
||||
|
@ -1,54 +1,50 @@
|
||||
/*
|
||||
spiffs_api.cpp - file system wrapper for SPIFFS
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
spiffs_api.cpp - file system wrapper for SPIFFS
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
|
||||
This code was influenced by NodeMCU and Sming libraries, and first version of
|
||||
Arduino wrapper written by Hristo Gochkov.
|
||||
This code was influenced by NodeMCU and Sming libraries, and first version of
|
||||
Arduino wrapper written by Hristo Gochkov.
|
||||
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
#include "spiffs_api.h"
|
||||
|
||||
using namespace fs;
|
||||
|
||||
FileImplPtr SPIFFSImpl::open(const char* path, OpenMode openMode, AccessMode accessMode)
|
||||
{
|
||||
if (!isSpiffsFilenameValid(path))
|
||||
{
|
||||
if (!isSpiffsFilenameValid(path)) {
|
||||
DEBUGV("SPIFFSImpl::open: invalid path=`%s` \r\n", path);
|
||||
return FileImplPtr();
|
||||
}
|
||||
int mode = getSpiffsMode(openMode, accessMode);
|
||||
int fd = SPIFFS_open(&_fs, path, mode, 0);
|
||||
if (fd < 0 && _fs.err_code == SPIFFS_ERR_DELETED && (openMode & OM_CREATE))
|
||||
{
|
||||
if (fd < 0 && _fs.err_code == SPIFFS_ERR_DELETED && (openMode & OM_CREATE)) {
|
||||
DEBUGV("SPIFFSImpl::open: fd=%d path=`%s` openMode=%d accessMode=%d err=%d, trying to remove\r\n",
|
||||
fd, path, openMode, accessMode, _fs.err_code);
|
||||
auto rc = SPIFFS_remove(&_fs, path);
|
||||
if (rc != SPIFFS_OK)
|
||||
{
|
||||
if (rc != SPIFFS_OK) {
|
||||
DEBUGV("SPIFFSImpl::open: SPIFFS_ERR_DELETED, but failed to remove path=`%s` openMode=%d accessMode=%d err=%d\r\n",
|
||||
path, openMode, accessMode, _fs.err_code);
|
||||
return FileImplPtr();
|
||||
}
|
||||
fd = SPIFFS_open(&_fs, path, mode, 0);
|
||||
}
|
||||
if (fd < 0)
|
||||
{
|
||||
if (fd < 0) {
|
||||
DEBUGV("SPIFFSImpl::open: fd=%d path=`%s` openMode=%d accessMode=%d err=%d\r\n",
|
||||
fd, path, openMode, accessMode, _fs.err_code);
|
||||
return FileImplPtr();
|
||||
@ -58,8 +54,7 @@ FileImplPtr SPIFFSImpl::open(const char* path, OpenMode openMode, AccessMode acc
|
||||
|
||||
bool SPIFFSImpl::exists(const char* path)
|
||||
{
|
||||
if (!isSpiffsFilenameValid(path))
|
||||
{
|
||||
if (!isSpiffsFilenameValid(path)) {
|
||||
DEBUGV("SPIFFSImpl::exists: invalid path=`%s` \r\n", path);
|
||||
return false;
|
||||
}
|
||||
@ -70,15 +65,13 @@ bool SPIFFSImpl::exists(const char* path)
|
||||
|
||||
DirImplPtr SPIFFSImpl::openDir(const char* path)
|
||||
{
|
||||
if (strlen(path) > 0 && !isSpiffsFilenameValid(path))
|
||||
{
|
||||
if (strlen(path) > 0 && !isSpiffsFilenameValid(path)) {
|
||||
DEBUGV("SPIFFSImpl::openDir: invalid path=`%s` \r\n", path);
|
||||
return DirImplPtr();
|
||||
}
|
||||
spiffs_DIR dir;
|
||||
spiffs_DIR* result = SPIFFS_opendir(&_fs, path, &dir);
|
||||
if (!result)
|
||||
{
|
||||
if (!result) {
|
||||
DEBUGV("SPIFFSImpl::openDir: path=`%s` err=%d\r\n", path, _fs.err_code);
|
||||
return DirImplPtr();
|
||||
}
|
||||
@ -88,24 +81,19 @@ DirImplPtr SPIFFSImpl::openDir(const char* path)
|
||||
int getSpiffsMode(OpenMode openMode, AccessMode accessMode)
|
||||
{
|
||||
int mode = 0;
|
||||
if (openMode & OM_CREATE)
|
||||
{
|
||||
if (openMode & OM_CREATE) {
|
||||
mode |= SPIFFS_CREAT;
|
||||
}
|
||||
if (openMode & OM_APPEND)
|
||||
{
|
||||
if (openMode & OM_APPEND) {
|
||||
mode |= SPIFFS_APPEND;
|
||||
}
|
||||
if (openMode & OM_TRUNCATE)
|
||||
{
|
||||
if (openMode & OM_TRUNCATE) {
|
||||
mode |= SPIFFS_TRUNC;
|
||||
}
|
||||
if (accessMode & AM_READ)
|
||||
{
|
||||
if (accessMode & AM_READ) {
|
||||
mode |= SPIFFS_RDONLY;
|
||||
}
|
||||
if (accessMode & AM_WRITE)
|
||||
{
|
||||
if (accessMode & AM_WRITE) {
|
||||
mode |= SPIFFS_WRONLY;
|
||||
}
|
||||
return mode;
|
||||
@ -113,8 +101,7 @@ int getSpiffsMode(OpenMode openMode, AccessMode accessMode)
|
||||
|
||||
bool isSpiffsFilenameValid(const char* name)
|
||||
{
|
||||
if (name == nullptr)
|
||||
{
|
||||
if (name == nullptr) {
|
||||
return false;
|
||||
}
|
||||
auto len = strlen(name);
|
||||
|
@ -2,36 +2,36 @@
|
||||
#define spiffs_api_h
|
||||
|
||||
/*
|
||||
spiffs_api.h - file system wrapper for SPIFFS
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
spiffs_api.h - file system wrapper for SPIFFS
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
|
||||
This code was influenced by NodeMCU and Sming libraries, and first version of
|
||||
Arduino wrapper written by Hristo Gochkov.
|
||||
This code was influenced by NodeMCU and Sming libraries, and first version of
|
||||
Arduino wrapper written by Hristo Gochkov.
|
||||
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
#include <limits>
|
||||
#include "FS.h"
|
||||
#undef max
|
||||
#undef min
|
||||
#include "FSImpl.h"
|
||||
extern "C" {
|
||||
#include "spiffs/spiffs.h"
|
||||
#include "spiffs/spiffs_nucleus.h"
|
||||
#include "spiffs/spiffs.h"
|
||||
#include "spiffs/spiffs_nucleus.h"
|
||||
};
|
||||
#include "debug.h"
|
||||
#include "flash_utils.h"
|
||||
@ -65,10 +65,10 @@ class SPIFFSImpl : public FSImpl
|
||||
public:
|
||||
SPIFFSImpl(uint32_t start, uint32_t size, uint32_t pageSize, uint32_t blockSize, uint32_t maxOpenFds)
|
||||
: _start(start)
|
||||
, _size(size)
|
||||
, _pageSize(pageSize)
|
||||
, _blockSize(blockSize)
|
||||
, _maxOpenFds(maxOpenFds)
|
||||
, _size(size)
|
||||
, _pageSize(pageSize)
|
||||
, _blockSize(blockSize)
|
||||
, _maxOpenFds(maxOpenFds)
|
||||
{
|
||||
memset(&_fs, 0, sizeof(_fs));
|
||||
}
|
||||
@ -79,19 +79,16 @@ public:
|
||||
|
||||
bool rename(const char* pathFrom, const char* pathTo) override
|
||||
{
|
||||
if (!isSpiffsFilenameValid(pathFrom))
|
||||
{
|
||||
if (!isSpiffsFilenameValid(pathFrom)) {
|
||||
DEBUGV("SPIFFSImpl::rename: invalid pathFrom=`%s`\r\n", pathFrom);
|
||||
return false;
|
||||
}
|
||||
if (!isSpiffsFilenameValid(pathTo))
|
||||
{
|
||||
if (!isSpiffsFilenameValid(pathTo)) {
|
||||
DEBUGV("SPIFFSImpl::rename: invalid pathTo=`%s` \r\n", pathTo);
|
||||
return false;
|
||||
}
|
||||
auto rc = SPIFFS_rename(&_fs, pathFrom, pathTo);
|
||||
if (rc != SPIFFS_OK)
|
||||
{
|
||||
if (rc != SPIFFS_OK) {
|
||||
DEBUGV("SPIFFS_rename: rc=%d, from=`%s`, to=`%s`\r\n", rc,
|
||||
pathFrom, pathTo);
|
||||
return false;
|
||||
@ -107,8 +104,7 @@ public:
|
||||
info.maxPathLength = SPIFFS_OBJ_NAME_LEN;
|
||||
uint32_t totalBytes, usedBytes;
|
||||
auto rc = SPIFFS_info(&_fs, &totalBytes, &usedBytes);
|
||||
if (rc != SPIFFS_OK)
|
||||
{
|
||||
if (rc != SPIFFS_OK) {
|
||||
DEBUGV("SPIFFS_info: rc=%d, err=%d\r\n", rc, _fs.err_code);
|
||||
return false;
|
||||
}
|
||||
@ -119,14 +115,12 @@ public:
|
||||
|
||||
bool remove(const char* path) override
|
||||
{
|
||||
if (!isSpiffsFilenameValid(path))
|
||||
{
|
||||
if (!isSpiffsFilenameValid(path)) {
|
||||
DEBUGV("SPIFFSImpl::remove: invalid path=`%s`\r\n", path);
|
||||
return false;
|
||||
}
|
||||
auto rc = SPIFFS_remove(&_fs, path);
|
||||
if (rc != SPIFFS_OK)
|
||||
{
|
||||
if (rc != SPIFFS_OK) {
|
||||
DEBUGV("SPIFFS_remove: rc=%d path=`%s`\r\n", rc, path);
|
||||
return false;
|
||||
}
|
||||
@ -147,49 +141,40 @@ public:
|
||||
|
||||
bool setConfig(const FSConfig &cfg) override
|
||||
{
|
||||
if ((cfg._type != SPIFFSConfig::fsid::FSId) || (SPIFFS_mounted(&_fs) != 0))
|
||||
{
|
||||
if ((cfg._type != SPIFFSConfig::fsid::FSId) || (SPIFFS_mounted(&_fs) != 0)) {
|
||||
return false;
|
||||
}
|
||||
_cfg = *static_cast<const SPIFFSConfig *>(&cfg);
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool begin() override
|
||||
{
|
||||
if (SPIFFS_mounted(&_fs) != 0)
|
||||
{
|
||||
if (SPIFFS_mounted(&_fs) != 0) {
|
||||
return true;
|
||||
}
|
||||
if (_size == 0)
|
||||
{
|
||||
if (_size == 0) {
|
||||
DEBUGV("SPIFFS size is zero");
|
||||
return false;
|
||||
}
|
||||
if (_tryMount())
|
||||
{
|
||||
if (_tryMount()) {
|
||||
return true;
|
||||
}
|
||||
if (_cfg._autoFormat)
|
||||
{
|
||||
if (_cfg._autoFormat) {
|
||||
auto rc = SPIFFS_format(&_fs);
|
||||
if (rc != SPIFFS_OK)
|
||||
{
|
||||
if (rc != SPIFFS_OK) {
|
||||
DEBUGV("SPIFFS_format: rc=%d, err=%d\r\n", rc, _fs.err_code);
|
||||
return false;
|
||||
}
|
||||
return _tryMount();
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void end() override
|
||||
{
|
||||
if (SPIFFS_mounted(&_fs) == 0)
|
||||
{
|
||||
if (SPIFFS_mounted(&_fs) == 0) {
|
||||
return;
|
||||
}
|
||||
SPIFFS_unmount(&_fs);
|
||||
@ -200,27 +185,23 @@ public:
|
||||
|
||||
bool format() override
|
||||
{
|
||||
if (_size == 0)
|
||||
{
|
||||
if (_size == 0) {
|
||||
DEBUGV("SPIFFS size is zero");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool wasMounted = (SPIFFS_mounted(&_fs) != 0);
|
||||
|
||||
if (_tryMount())
|
||||
{
|
||||
if (_tryMount()) {
|
||||
SPIFFS_unmount(&_fs);
|
||||
}
|
||||
auto rc = SPIFFS_format(&_fs);
|
||||
if (rc != SPIFFS_OK)
|
||||
{
|
||||
if (rc != SPIFFS_OK) {
|
||||
DEBUGV("SPIFFS_format: rc=%d, err=%d\r\n", rc, _fs.err_code);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (wasMounted)
|
||||
{
|
||||
if (wasMounted) {
|
||||
return _tryMount();
|
||||
}
|
||||
|
||||
@ -229,7 +210,7 @@ public:
|
||||
|
||||
bool gc() override
|
||||
{
|
||||
return SPIFFS_gc_quick(&_fs, 0) == SPIFFS_OK;
|
||||
return SPIFFS_gc_quick( &_fs, 0 ) == SPIFFS_OK;
|
||||
}
|
||||
|
||||
protected:
|
||||
@ -256,26 +237,22 @@ protected:
|
||||
config.log_page_size = _pageSize;
|
||||
|
||||
|
||||
if (((uint32_t) std::numeric_limits<spiffs_block_ix>::max()) < (_size / _blockSize))
|
||||
{
|
||||
if (((uint32_t) std::numeric_limits<spiffs_block_ix>::max()) < (_size / _blockSize)) {
|
||||
DEBUGV("spiffs_block_ix type too small");
|
||||
abort();
|
||||
}
|
||||
|
||||
if (((uint32_t) std::numeric_limits<spiffs_page_ix>::max()) < (_size / _pageSize))
|
||||
{
|
||||
if (((uint32_t) std::numeric_limits<spiffs_page_ix>::max()) < (_size / _pageSize)) {
|
||||
DEBUGV("spiffs_page_ix type too small");
|
||||
abort();
|
||||
}
|
||||
|
||||
if (((uint32_t) std::numeric_limits<spiffs_obj_id>::max()) < (2 + (_size / (2 * _pageSize)) * 2))
|
||||
{
|
||||
if (((uint32_t) std::numeric_limits<spiffs_obj_id>::max()) < (2 + (_size / (2*_pageSize))*2)) {
|
||||
DEBUGV("spiffs_obj_id type too small");
|
||||
abort();
|
||||
}
|
||||
|
||||
if (((uint32_t) std::numeric_limits<spiffs_span_ix>::max()) < (_size / _pageSize - 1))
|
||||
{
|
||||
if (((uint32_t) std::numeric_limits<spiffs_span_ix>::max()) < (_size / _pageSize - 1)) {
|
||||
DEBUGV("spiffs_span_ix type too small");
|
||||
abort();
|
||||
}
|
||||
@ -290,8 +267,7 @@ protected:
|
||||
size_t fdsBufSize = SPIFFS_buffer_bytes_for_filedescs(&_fs, _maxOpenFds);
|
||||
size_t cacheBufSize = SPIFFS_buffer_bytes_for_cache(&_fs, _maxOpenFds);
|
||||
|
||||
if (!_workBuf)
|
||||
{
|
||||
if (!_workBuf) {
|
||||
DEBUGV("SPIFFSImpl: allocating %zd+%zd+%zd=%zd bytes\r\n",
|
||||
workBufSize, fdsBufSize, cacheBufSize,
|
||||
workBufSize + fdsBufSize + cacheBufSize);
|
||||
@ -348,7 +324,7 @@ public:
|
||||
SPIFFSFileImpl(SPIFFSImpl* fs, spiffs_file fd)
|
||||
: _fs(fs)
|
||||
, _fd(fd)
|
||||
, _written(false)
|
||||
, _written(false)
|
||||
{
|
||||
memset(&_stat, 0, sizeof(_stat));
|
||||
_getStat();
|
||||
@ -364,8 +340,7 @@ public:
|
||||
CHECKFD();
|
||||
|
||||
auto result = SPIFFS_write(_fs->getFs(), _fd, (void*) buf, size);
|
||||
if (result < 0)
|
||||
{
|
||||
if (result < 0) {
|
||||
DEBUGV("SPIFFS_write rc=%d\r\n", result);
|
||||
return 0;
|
||||
}
|
||||
@ -377,8 +352,7 @@ public:
|
||||
{
|
||||
CHECKFD();
|
||||
auto result = SPIFFS_read(_fs->getFs(), _fd, (void*) buf, size);
|
||||
if (result < 0)
|
||||
{
|
||||
if (result < 0) {
|
||||
DEBUGV("SPIFFS_read rc=%d\r\n", result);
|
||||
return 0;
|
||||
}
|
||||
@ -391,8 +365,7 @@ public:
|
||||
CHECKFD();
|
||||
|
||||
auto rc = SPIFFS_fflush(_fs->getFs(), _fd);
|
||||
if (rc < 0)
|
||||
{
|
||||
if (rc < 0) {
|
||||
DEBUGV("SPIFFS_fflush rc=%d\r\n", rc);
|
||||
}
|
||||
_written = true;
|
||||
@ -403,13 +376,11 @@ public:
|
||||
CHECKFD();
|
||||
|
||||
int32_t offset = static_cast<int32_t>(pos);
|
||||
if (mode == SeekEnd)
|
||||
{
|
||||
if (mode == SeekEnd) {
|
||||
offset = -offset;
|
||||
}
|
||||
auto rc = SPIFFS_lseek(_fs->getFs(), _fd, offset, (int) mode);
|
||||
if (rc < 0)
|
||||
{
|
||||
if (rc < 0) {
|
||||
DEBUGV("SPIFFS_lseek rc=%d\r\n", rc);
|
||||
return false;
|
||||
}
|
||||
@ -422,8 +393,7 @@ public:
|
||||
CHECKFD();
|
||||
|
||||
auto result = SPIFFS_lseek(_fs->getFs(), _fd, 0, SPIFFS_SEEK_CUR);
|
||||
if (result < 0)
|
||||
{
|
||||
if (result < 0) {
|
||||
DEBUGV("SPIFFS_tell rc=%d\r\n", result);
|
||||
return 0;
|
||||
}
|
||||
@ -434,8 +404,7 @@ public:
|
||||
size_t size() const override
|
||||
{
|
||||
CHECKFD();
|
||||
if (_written)
|
||||
{
|
||||
if (_written) {
|
||||
_getStat();
|
||||
}
|
||||
return _stat.size;
|
||||
@ -445,13 +414,10 @@ public:
|
||||
{
|
||||
CHECKFD();
|
||||
spiffs_fd *sfd;
|
||||
if (spiffs_fd_get(_fs->getFs(), _fd, &sfd) == SPIFFS_OK)
|
||||
{
|
||||
if (spiffs_fd_get(_fs->getFs(), _fd, &sfd) == SPIFFS_OK) {
|
||||
return SPIFFS_OK == spiffs_object_truncate(sfd, size, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -492,8 +458,7 @@ protected:
|
||||
{
|
||||
CHECKFD();
|
||||
auto rc = SPIFFS_fstat(_fs->getFs(), _fd, &_stat);
|
||||
if (rc != SPIFFS_OK)
|
||||
{
|
||||
if (rc != SPIFFS_OK) {
|
||||
DEBUGV("SPIFFS_fstat rc=%d\r\n", rc);
|
||||
memset(&_stat, 0, sizeof(_stat));
|
||||
}
|
||||
@ -525,15 +490,13 @@ public:
|
||||
|
||||
FileImplPtr openFile(OpenMode openMode, AccessMode accessMode) override
|
||||
{
|
||||
if (!_valid)
|
||||
{
|
||||
if (!_valid) {
|
||||
return FileImplPtr();
|
||||
}
|
||||
int mode = getSpiffsMode(openMode, accessMode);
|
||||
auto fs = _fs->getFs();
|
||||
spiffs_file fd = SPIFFS_open_by_dirent(fs, &_dirent, mode, 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
if (fd < 0) {
|
||||
DEBUGV("SPIFFSDirImpl::openFile: fd=%d path=`%s` openMode=%d accessMode=%d err=%d\r\n",
|
||||
fd, _dirent.name, openMode, accessMode, fs->err_code);
|
||||
return FileImplPtr();
|
||||
@ -543,8 +506,7 @@ public:
|
||||
|
||||
const char* fileName() override
|
||||
{
|
||||
if (!_valid)
|
||||
{
|
||||
if (!_valid) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@ -553,8 +515,7 @@ public:
|
||||
|
||||
size_t fileSize() override
|
||||
{
|
||||
if (!_valid)
|
||||
{
|
||||
if (!_valid) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -576,11 +537,10 @@ public:
|
||||
bool next() override
|
||||
{
|
||||
const int n = _pattern.length();
|
||||
do
|
||||
{
|
||||
do {
|
||||
spiffs_dirent* result = SPIFFS_readdir(&_dir, &_dirent);
|
||||
_valid = (result != nullptr);
|
||||
} while (_valid && strncmp((const char*) _dirent.name, _pattern.c_str(), n) != 0);
|
||||
} while(_valid && strncmp((const char*) _dirent.name, _pattern.c_str(), n) != 0);
|
||||
return _valid;
|
||||
}
|
||||
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
spiffs_hal.cpp - SPI read/write/erase functions for SPIFFS.
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
spiffs_hal.cpp - SPI read/write/erase functions for SPIFFS.
|
||||
Copyright (c) 2015 Ivan Grokhotkov. 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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <stdlib.h>
|
||||
@ -29,63 +29,55 @@ extern "C" {
|
||||
#include "spi_flash.h"
|
||||
}
|
||||
/*
|
||||
spi_flash_read function requires flash address to be aligned on word boundary.
|
||||
We take care of this by reading first and last words separately and memcpy
|
||||
relevant bytes into result buffer.
|
||||
spi_flash_read function requires flash address to be aligned on word boundary.
|
||||
We take care of this by reading first and last words separately and memcpy
|
||||
relevant bytes into result buffer.
|
||||
|
||||
alignment: 012301230123012301230123
|
||||
bytes requested: -------***********------
|
||||
read directly: --------xxxxxxxx--------
|
||||
read pre: ----aaaa----------------
|
||||
read post: ----------------bbbb----
|
||||
alignedBegin: ^
|
||||
alignedEnd: ^
|
||||
alignment: 012301230123012301230123
|
||||
bytes requested: -------***********------
|
||||
read directly: --------xxxxxxxx--------
|
||||
read pre: ----aaaa----------------
|
||||
read post: ----------------bbbb----
|
||||
alignedBegin: ^
|
||||
alignedEnd: ^
|
||||
*/
|
||||
|
||||
int32_t spiffs_hal_read(uint32_t addr, uint32_t size, uint8_t *dst)
|
||||
{
|
||||
int32_t spiffs_hal_read(uint32_t addr, uint32_t size, uint8_t *dst) {
|
||||
optimistic_yield(10000);
|
||||
|
||||
uint32_t result = SPIFFS_OK;
|
||||
uint32_t alignedBegin = (addr + 3) & (~3);
|
||||
uint32_t alignedEnd = (addr + size) & (~3);
|
||||
if (alignedEnd < alignedBegin)
|
||||
{
|
||||
if (alignedEnd < alignedBegin) {
|
||||
alignedEnd = alignedBegin;
|
||||
}
|
||||
|
||||
if (addr < alignedBegin)
|
||||
{
|
||||
if (addr < alignedBegin) {
|
||||
uint32_t nb = alignedBegin - addr;
|
||||
uint32_t tmp;
|
||||
if (!ESP.flashRead(alignedBegin - 4, &tmp, 4))
|
||||
{
|
||||
if (!ESP.flashRead(alignedBegin - 4, &tmp, 4)) {
|
||||
DEBUGV("_spif_read(%d) addr=%x size=%x ab=%x ae=%x\r\n",
|
||||
__LINE__, addr, size, alignedBegin, alignedEnd);
|
||||
__LINE__, addr, size, alignedBegin, alignedEnd);
|
||||
return SPIFFS_ERR_INTERNAL;
|
||||
}
|
||||
memcpy(dst, ((uint8_t*) &tmp) + 4 - nb, nb);
|
||||
}
|
||||
|
||||
if (alignedEnd != alignedBegin)
|
||||
{
|
||||
if (!ESP.flashRead(alignedBegin, (uint32_t*)(dst + alignedBegin - addr),
|
||||
alignedEnd - alignedBegin))
|
||||
{
|
||||
if (alignedEnd != alignedBegin) {
|
||||
if (!ESP.flashRead(alignedBegin, (uint32_t*) (dst + alignedBegin - addr),
|
||||
alignedEnd - alignedBegin)) {
|
||||
DEBUGV("_spif_read(%d) addr=%x size=%x ab=%x ae=%x\r\n",
|
||||
__LINE__, addr, size, alignedBegin, alignedEnd);
|
||||
__LINE__, addr, size, alignedBegin, alignedEnd);
|
||||
return SPIFFS_ERR_INTERNAL;
|
||||
}
|
||||
}
|
||||
|
||||
if (addr + size > alignedEnd)
|
||||
{
|
||||
if (addr + size > alignedEnd) {
|
||||
uint32_t nb = addr + size - alignedEnd;
|
||||
uint32_t tmp;
|
||||
if (!ESP.flashRead(alignedEnd, &tmp, 4))
|
||||
{
|
||||
if (!ESP.flashRead(alignedEnd, &tmp, 4)) {
|
||||
DEBUGV("_spif_read(%d) addr=%x size=%x ab=%x ae=%x\r\n",
|
||||
__LINE__, addr, size, alignedBegin, alignedEnd);
|
||||
__LINE__, addr, size, alignedBegin, alignedEnd);
|
||||
return SPIFFS_ERR_INTERNAL;
|
||||
}
|
||||
|
||||
@ -96,68 +88,58 @@ int32_t spiffs_hal_read(uint32_t addr, uint32_t size, uint8_t *dst)
|
||||
}
|
||||
|
||||
/*
|
||||
Like spi_flash_read, spi_flash_write has a requirement for flash address to be
|
||||
aligned. However it also requires RAM address to be aligned as it reads data
|
||||
in 32-bit words. Flash address (mis-)alignment is handled much the same way
|
||||
as for reads, but for RAM alignment we have to copy data into a temporary
|
||||
buffer. The size of this buffer is a tradeoff between number of writes required
|
||||
and amount of stack required. This is chosen to be 512 bytes here, but might
|
||||
be adjusted in the future if there are good reasons to do so.
|
||||
Like spi_flash_read, spi_flash_write has a requirement for flash address to be
|
||||
aligned. However it also requires RAM address to be aligned as it reads data
|
||||
in 32-bit words. Flash address (mis-)alignment is handled much the same way
|
||||
as for reads, but for RAM alignment we have to copy data into a temporary
|
||||
buffer. The size of this buffer is a tradeoff between number of writes required
|
||||
and amount of stack required. This is chosen to be 512 bytes here, but might
|
||||
be adjusted in the future if there are good reasons to do so.
|
||||
*/
|
||||
|
||||
static const int UNALIGNED_WRITE_BUFFER_SIZE = 512;
|
||||
|
||||
int32_t spiffs_hal_write(uint32_t addr, uint32_t size, uint8_t *src)
|
||||
{
|
||||
int32_t spiffs_hal_write(uint32_t addr, uint32_t size, uint8_t *src) {
|
||||
optimistic_yield(10000);
|
||||
|
||||
uint32_t alignedBegin = (addr + 3) & (~3);
|
||||
uint32_t alignedEnd = (addr + size) & (~3);
|
||||
if (alignedEnd < alignedBegin)
|
||||
{
|
||||
if (alignedEnd < alignedBegin) {
|
||||
alignedEnd = alignedBegin;
|
||||
}
|
||||
|
||||
if (addr < alignedBegin)
|
||||
{
|
||||
if (addr < alignedBegin) {
|
||||
uint32_t ofs = alignedBegin - addr;
|
||||
uint32_t nb = (size < ofs) ? size : ofs;
|
||||
uint8_t tmp[4] __attribute__((aligned(4))) = {0xff, 0xff, 0xff, 0xff};
|
||||
memcpy(tmp + 4 - ofs, src, nb);
|
||||
if (!ESP.flashWrite(alignedBegin - 4, (uint32_t*) tmp, 4))
|
||||
{
|
||||
if (!ESP.flashWrite(alignedBegin - 4, (uint32_t*) tmp, 4)) {
|
||||
DEBUGV("_spif_write(%d) addr=%x size=%x ab=%x ae=%x\r\n",
|
||||
__LINE__, addr, size, alignedBegin, alignedEnd);
|
||||
__LINE__, addr, size, alignedBegin, alignedEnd);
|
||||
return SPIFFS_ERR_INTERNAL;
|
||||
}
|
||||
}
|
||||
|
||||
if (alignedEnd != alignedBegin)
|
||||
{
|
||||
uint32_t* srcLeftover = (uint32_t*)(src + alignedBegin - addr);
|
||||
if (alignedEnd != alignedBegin) {
|
||||
uint32_t* srcLeftover = (uint32_t*) (src + alignedBegin - addr);
|
||||
uint32_t srcAlign = ((uint32_t) srcLeftover) & 3;
|
||||
if (!srcAlign)
|
||||
{
|
||||
if (!srcAlign) {
|
||||
if (!ESP.flashWrite(alignedBegin, (uint32_t*) srcLeftover,
|
||||
alignedEnd - alignedBegin))
|
||||
{
|
||||
alignedEnd - alignedBegin)) {
|
||||
DEBUGV("_spif_write(%d) addr=%x size=%x ab=%x ae=%x\r\n",
|
||||
__LINE__, addr, size, alignedBegin, alignedEnd);
|
||||
__LINE__, addr, size, alignedBegin, alignedEnd);
|
||||
return SPIFFS_ERR_INTERNAL;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
uint8_t buf[UNALIGNED_WRITE_BUFFER_SIZE];
|
||||
for (uint32_t sizeLeft = alignedEnd - alignedBegin; sizeLeft;)
|
||||
{
|
||||
for (uint32_t sizeLeft = alignedEnd - alignedBegin; sizeLeft; ) {
|
||||
size_t willCopy = std::min(sizeLeft, sizeof(buf));
|
||||
memcpy(buf, srcLeftover, willCopy);
|
||||
|
||||
if (!ESP.flashWrite(alignedBegin, (uint32_t*) buf, willCopy))
|
||||
{
|
||||
if (!ESP.flashWrite(alignedBegin, (uint32_t*) buf, willCopy)) {
|
||||
DEBUGV("_spif_write(%d) addr=%x size=%x ab=%x ae=%x\r\n",
|
||||
__LINE__, addr, size, alignedBegin, alignedEnd);
|
||||
__LINE__, addr, size, alignedBegin, alignedEnd);
|
||||
return SPIFFS_ERR_INTERNAL;
|
||||
}
|
||||
|
||||
@ -168,16 +150,14 @@ int32_t spiffs_hal_write(uint32_t addr, uint32_t size, uint8_t *src)
|
||||
}
|
||||
}
|
||||
|
||||
if (addr + size > alignedEnd)
|
||||
{
|
||||
if (addr + size > alignedEnd) {
|
||||
uint32_t nb = addr + size - alignedEnd;
|
||||
uint32_t tmp = 0xffffffff;
|
||||
memcpy(&tmp, src + size - nb, nb);
|
||||
|
||||
if (!ESP.flashWrite(alignedEnd, &tmp, 4))
|
||||
{
|
||||
if (!ESP.flashWrite(alignedEnd, &tmp, 4)) {
|
||||
DEBUGV("_spif_write(%d) addr=%x size=%x ab=%x ae=%x\r\n",
|
||||
__LINE__, addr, size, alignedBegin, alignedEnd);
|
||||
__LINE__, addr, size, alignedBegin, alignedEnd);
|
||||
return SPIFFS_ERR_INTERNAL;
|
||||
}
|
||||
}
|
||||
@ -185,21 +165,17 @@ int32_t spiffs_hal_write(uint32_t addr, uint32_t size, uint8_t *src)
|
||||
return SPIFFS_OK;
|
||||
}
|
||||
|
||||
int32_t spiffs_hal_erase(uint32_t addr, uint32_t size)
|
||||
{
|
||||
int32_t spiffs_hal_erase(uint32_t addr, uint32_t size) {
|
||||
if ((size & (SPI_FLASH_SEC_SIZE - 1)) != 0 ||
|
||||
(addr & (SPI_FLASH_SEC_SIZE - 1)) != 0)
|
||||
{
|
||||
(addr & (SPI_FLASH_SEC_SIZE - 1)) != 0) {
|
||||
DEBUGV("_spif_erase called with addr=%x, size=%d\r\n", addr, size);
|
||||
abort();
|
||||
}
|
||||
const uint32_t sector = addr / SPI_FLASH_SEC_SIZE;
|
||||
const uint32_t sectorCount = size / SPI_FLASH_SEC_SIZE;
|
||||
for (uint32_t i = 0; i < sectorCount; ++i)
|
||||
{
|
||||
for (uint32_t i = 0; i < sectorCount; ++i) {
|
||||
optimistic_yield(10000);
|
||||
if (!ESP.flashEraseSector(sector + i))
|
||||
{
|
||||
if (!ESP.flashEraseSector(sector + i)) {
|
||||
DEBUGV("_spif_erase addr=%x size=%d i=%d\r\n", addr, size, i);
|
||||
return SPIFFS_ERR_INTERNAL;
|
||||
}
|
||||
|
@ -4,61 +4,57 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
uint32_t sqrt32(uint32_t n)
|
||||
uint32_t sqrt32 (uint32_t n)
|
||||
{
|
||||
// http://www.codecodex.com/wiki/Calculate_an_integer_square_root#C
|
||||
// Another very fast algorithm donated by Tristan.Muntsinger@gmail.com
|
||||
// (note: tested across the full 32 bits range, see comment below)
|
||||
|
||||
// 15 iterations (c=1<<15)
|
||||
|
||||
unsigned int c = 0x8000;
|
||||
unsigned int g = 0x8000;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
// http://www.codecodex.com/wiki/Calculate_an_integer_square_root#C
|
||||
// Another very fast algorithm donated by Tristan.Muntsinger@gmail.com
|
||||
// (note: tested across the full 32 bits range, see comment below)
|
||||
if (g*g > n)
|
||||
g ^= c;
|
||||
c >>= 1;
|
||||
if (!c)
|
||||
return g;
|
||||
g |= c;
|
||||
}
|
||||
}
|
||||
|
||||
// 15 iterations (c=1<<15)
|
||||
/*
|
||||
* tested with:
|
||||
*
|
||||
|
||||
unsigned int c = 0x8000;
|
||||
unsigned int g = 0x8000;
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <math.h>
|
||||
|
||||
for (;;)
|
||||
int main (void)
|
||||
{
|
||||
for (uint32_t i = 0; ++i; )
|
||||
{
|
||||
uint32_t sr = sqrt32(i);
|
||||
uint32_t ifsr = sqrt(i);
|
||||
|
||||
if (ifsr != sr)
|
||||
printf("%d: i%d f%d\n", i, sr, ifsr);
|
||||
|
||||
if (!(i & 0xffffff))
|
||||
{
|
||||
if (g * g > n)
|
||||
{
|
||||
g ^= c;
|
||||
}
|
||||
c >>= 1;
|
||||
if (!c)
|
||||
{
|
||||
return g;
|
||||
}
|
||||
g |= c;
|
||||
printf("%i%% (0x%08x)\r", ((i >> 16) * 100) >> 16, i);
|
||||
fflush(stdout);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
tested with:
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <math.h>
|
||||
|
||||
int main (void)
|
||||
{
|
||||
for (uint32_t i = 0; ++i; )
|
||||
{
|
||||
uint32_t sr = sqrt32(i);
|
||||
uint32_t ifsr = sqrt(i);
|
||||
|
||||
if (ifsr != sr)
|
||||
printf("%d: i%d f%d\n", i, sr, ifsr);
|
||||
|
||||
if (!(i & 0xffffff))
|
||||
{
|
||||
printf("%i%% (0x%08x)\r", ((i >> 16) * 100) >> 16, i);
|
||||
fflush(stdout);
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
*
|
||||
*/
|
||||
|
||||
};
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user