1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-22 21:23:07 +03:00
esp8266/tests/host/common/MockEsp.cpp
david gauchard 9a2ed274f3 polledTimeout: add option to use CPU count instead of millis() (#5870)
* polledTimeout: add option to use CPU count instead of millis()

* use more "using" alias

* more c++/clear code, using typename (thanks @devyte)

* rename class name to include unit, introduce timeMax() and check it with assert()

* remove useless defines

* improve api readability, add micro-second unit

* update example

* mock: emulate getCycleCount, add/fix polledTimeout CI test

* + nano-seconds, assert -> message, comments, host test

* allow 0 for timeout (enables immediate timeout, fix division by 0)

* typo, set member instead of local variable

* unify error message

* slight change on checkExpired() allows "never expired"
also removed printed message, add YieldAndDelay, simplify calculations

* remove traces of debug.h/cpp in this PR

* include missing <limits> header

* back to original expired test, introduce boolean _neverExpires, fix reset(), getTimeout() is invalid

* fix expiredOneShot with _timeout==0 check

* reenable getTimeout()

* expose checkExpired with unit conversion

* fix timing comments, move critical code to iram

* add member ::neverExpires and use it where relevant

* improve clarity

* remove exposed checkExpired(), adapt LEAmDNS with equivalent

* add API ::resetToNeverExpires(), use it in LEAmDNS

* remove offending constness from ::flagged() LEAmDNS (due do API fix in PolledTimeout)

* simplify "Fast" base classes

* minor variable rename

* Fix examples

* compliance with good c++ manners

* minor changes for consistency

* add missing const

* expired() and bool() moved to iram

* constexpr compensation computing

* add/update comments

* move neverExpires and alwaysExpired
2019-04-05 10:50:53 -03:00

226 lines
4.8 KiB
C++

/*
Arduino emulation - esp8266's core
Copyright (c) 2018 david gauchard. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal with the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimers.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimers in the
documentation and/or other materials provided with the distribution.
- The names of its contributors may not be used to endorse or promote
products derived from this Software without specific prior written
permission.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS WITH THE SOFTWARE.
*/
#include <Esp.h>
#include <eboot_command.h>
#include <sys/time.h>
#include <stdlib.h>
unsigned long long operator"" _kHz(unsigned long long x) {
return x * 1000;
}
unsigned long long operator"" _MHz(unsigned long long x) {
return x * 1000 * 1000;
}
unsigned long long operator"" _GHz(unsigned long long x) {
return x * 1000 * 1000 * 1000;
}
unsigned long long operator"" _kBit(unsigned long long x) {
return x * 1024;
}
unsigned long long operator"" _MBit(unsigned long long x) {
return x * 1024 * 1024;
}
unsigned long long operator"" _GBit(unsigned long long x) {
return x * 1024 * 1024 * 1024;
}
unsigned long long operator"" _kB(unsigned long long x) {
return x * 1024;
}
unsigned long long operator"" _MB(unsigned long long x) {
return x * 1024 * 1024;
}
unsigned long long operator"" _GB(unsigned long long x) {
return x * 1024 * 1024 * 1024;
}
uint32_t _SPIFFS_start;
void eboot_command_write (struct eboot_command* cmd)
{
(void)cmd;
}
EspClass ESP;
void EspClass::restart ()
{
mockverbose("Esp.restart(): exiting\n");
exit(EXIT_SUCCESS);
}
uint32_t EspClass::getChipId()
{
return 0xee1337;
}
bool EspClass::checkFlashConfig(bool needsEquals)
{
return true;
}
uint32_t EspClass::getSketchSize()
{
return 400000;
}
uint32_t EspClass::getFreeHeap()
{
return 30000;
}
String EspClass::getResetReason()
{
return "Power on";
}
uint32_t EspClass::getFreeSketchSpace()
{
return 4 * 1024 * 1024;
}
uint8_t EspClass::getCpuFreqMHz()
{
return 160;
}
const char *EspClass::getSdkVersion()
{
return "2.5.0";
}
uint32_t EspClass::getFlashChipSpeed()
{
return 40;
}
void EspClass::getHeapStats(uint32_t* hfree, uint16_t* hmax, uint8_t* hfrag) {
uint32_t hf = 10 * 1024;
float hm = 1 * 1024;
if (hfree) *hfree = hf;
if (hmax) *hmax = hm;
if (hfrag) *hfrag = 100 - (sqrt(hm) * 100) / hf;
}
bool EspClass::flashEraseSector(uint32_t sector)
{
return true;
}
FlashMode_t EspClass::getFlashChipMode()
{
return FM_DOUT;
}
FlashMode_t EspClass::magicFlashChipMode(uint8_t byte)
{
return FM_DOUT;
}
bool EspClass::flashWrite(uint32_t offset, uint32_t *data, size_t size)
{
(void)offset;
(void)data;
(void)size;
return true;
}
bool EspClass::flashRead(uint32_t offset, uint32_t *data, size_t size)
{
(void)offset;
(void)data;
(void)size;
return true;
}
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::getFlashChipRealSize(void)
{
return magicFlashChipSize(4);
}
uint32_t EspClass::getFlashChipSize(void)
{
return magicFlashChipSize(4);
}
String EspClass::getFullVersion ()
{
return "host-emulation";
}
uint32_t EspClass::getFreeContStack()
{
return 4000;
}
void EspClass::resetFreeContStack()
{
}
uint32_t EspClass::getCycleCount()
{
timeval t;
gettimeofday(&t, NULL);
return (((uint64_t)t.tv_sec) * 1000000 + t.tv_usec) * (F_CPU / 1000000);
}