Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow test framework to use cores/esp8266/Arduino.h directly #7377

Merged
merged 23 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
84d8866
Allow test framework to use cores/esp8266/Arduino.h directly
mcspr Jun 14, 2020
c89d0f6
fix wps debugging
mcspr Jun 14, 2020
0e0849d
some more missing debug.h
mcspr Jun 14, 2020
65fab13
Merge remote-tracking branch 'origin/master' into tests/sync-arduino-h
mcspr Jul 17, 2020
555c272
Hunt down debug.h and roll-back
mcspr Jul 17, 2020
6b36f6e
Move abs+round checks to test/device/test_sw
mcspr Jul 17, 2020
ffc3744
Restore macros for C code
mcspr Jul 17, 2020
6db24f8
fixup! Move abs+round checks to test/device/test_sw
mcspr Jul 18, 2020
e59f926
Fix bad c/p, actually try round with ints
mcspr Jul 18, 2020
0c8c7ab
Merge branch 'master' into tests/sync-arduino-h
d-a-v Jul 28, 2020
1f14878
tweak c macros per review
mcspr Aug 16, 2020
9e1b019
Merge remote-tracking branch 'origin/master' into tests/sync-arduino-h
mcspr Aug 17, 2020
247c6c3
fix gcc-10 missing cerrno include
mcspr Aug 17, 2020
685c25f
amend 555c272, again
mcspr Aug 17, 2020
60de322
fixup! Merge remote-tracking branch 'origin/master' into tests/sync-a…
mcspr Aug 17, 2020
1a2fd27
fixup! amend 555c272, again
mcspr Aug 17, 2020
8c89d90
switch to the current -std=... opts
mcspr Aug 17, 2020
f48cd49
Revert "switch to the current -std=... opts"
mcspr Aug 17, 2020
481b351
trying to fix CI, clean-up configTime decl + def
mcspr Aug 17, 2020
b496509
Merge branch 'master' into tests/sync-arduino-h
mcspr Sep 9, 2020
8e7a003
Merge remote-tracking branch 'origin/master' into tests/sync-arduino-h
mcspr Oct 6, 2020
1fa62d4
Merge branch 'master' into tests/sync-arduino-h
mcspr Oct 6, 2020
3d7379f
Merge branch 'master' into tests/sync-arduino-h
d-a-v Oct 6, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions cores/esp8266/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ extern "C" {
#include "binary.h"
#include "esp8266_peri.h"
#include "twi.h"

#include "core_esp8266_features.h"
#include "core_esp8266_version.h"

Expand Down Expand Up @@ -127,15 +128,11 @@ void timer0_isr_init(void);
void timer0_attachInterrupt(timercallback userFunc);
void timer0_detachInterrupt(void);

// Use stdlib abs() and round() to avoid issues with the C++ libraries
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
#define radians(deg) ((deg)*DEG_TO_RAD)
#define degrees(rad) ((rad)*RAD_TO_DEG)
#define sq(x) ((x)*(x))

void ets_intr_lock();
void ets_intr_unlock();

#define interrupts() xt_rsil(0)
#define noInterrupts() xt_rsil(15)

Expand Down Expand Up @@ -164,11 +161,12 @@ typedef uint16_t word;
typedef bool boolean;
typedef uint8_t byte;

void ets_intr_lock();
void ets_intr_unlock();

void init(void);
void initVariant(void);

int atexit(void (*func)()) __attribute__((weak));

void pinMode(uint8_t pin, uint8_t mode);
void digitalWrite(uint8_t pin, uint8_t val);
int digitalRead(uint8_t pin);
Expand Down Expand Up @@ -218,28 +216,31 @@ void optimistic_yield(uint32_t interval_us);
} // extern "C"
#endif

// undefine stdlib's definitions when encountered, provide abs that supports floating point for C code
#ifndef __cplusplus
#undef abs
#define abs(x) ((x)>0?(x):-(x))
mcspr marked this conversation as resolved.
Show resolved Hide resolved
#undef round
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
#endif // ifndef __cplusplus


// from this point onward, we need to configure the c++ environment
#ifdef __cplusplus

#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <pgmspace.h>

#include "WCharacter.h"
#include "WString.h"

#include "HardwareSerial.h"
#include "Esp.h"
#include "Updater.h"
#include "debug.h"
mcspr marked this conversation as resolved.
Show resolved Hide resolved

using std::min;
using std::max;
using std::round;
using std::isinf;
using std::isnan;

// Use float-compatible stl abs() and round(), we don't use Arduino macros to avoid issues with the C++ libraries
using std::abs;
using std::round;

#define _min(a,b) ({ decltype(a) _a = (a); decltype(b) _b = (b); _a < _b? _a : _b; })
#define _max(a,b) ({ decltype(a) _a = (a); decltype(b) _b = (b); _a > _b? _a : _b; })

Expand Down Expand Up @@ -279,8 +280,19 @@ inline void configTzTime(const char* tz, const char* server1,
configTime(tz, server1, server2, server3);
}

// Everything we expect to be implicitly loaded for the sketch
#include <pgmspace.h>

#include "WCharacter.h"
#include "WString.h"

#include "HardwareSerial.h"
#include "Esp.h"
#include "Updater.h"

#endif // __cplusplus

#include "debug.h"
#include "pins_arduino.h"

#endif
Expand Down
2 changes: 2 additions & 0 deletions cores/esp8266/Esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
#include "MD5Builder.h"
#include "umm_malloc/umm_malloc.h"
#include "cont.h"

#include "coredecls.h"
#include <pgmspace.h>

extern "C" {
#include "user_interface.h"
Expand Down
16 changes: 5 additions & 11 deletions cores/esp8266/Esp.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define ESP_H

#include <Arduino.h>
#include "core_esp8266_features.h"
#include "spi_vendors.h"

/**
Expand Down Expand Up @@ -168,21 +169,14 @@ class EspClass {
uint32_t random() const;

#ifndef CORE_MOCK
inline uint32_t getCycleCount() __attribute__((always_inline));
uint32_t getCycleCount() __attribute__((always_inline)) {
return esp_get_cycle_count();
}
mcspr marked this conversation as resolved.
Show resolved Hide resolved
#else
uint32_t getCycleCount();
#endif
#endif // ifndef CORE_MOCK
};

#ifndef CORE_MOCK

uint32_t EspClass::getCycleCount()
{
return esp_get_cycle_count();
}

#endif // !defined(CORE_MOCK)

extern EspClass ESP;

#endif //ESP_H
6 changes: 6 additions & 0 deletions cores/esp8266/core_esp8266_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

#ifdef __cplusplus

#include <pgmspace.h>

namespace arduino
{
extern "C++"
Expand Down Expand Up @@ -83,6 +85,7 @@ namespace arduino
// level 0 will enable ALL interrupts,
//
#ifndef CORE_MOCK

#define xt_rsil(level) (__extension__({uint32_t state; __asm__ __volatile__("rsil %0," __STRINGIFY(level) : "=a" (state) :: "memory"); state;}))
#define xt_wsr_ps(state) __asm__ __volatile__("wsr %0,ps; isync" :: "a" (state) : "memory")

Expand All @@ -102,6 +105,9 @@ inline uint32_t esp_get_program_counter() {

#else // CORE_MOCK

#define xt_rsil(level) (level)
#define xt_wsr_ps(state) do { (void)(state); } while (0)

inline uint32_t esp_get_program_counter() { return 0; }

#endif // CORE_MOCK
Expand Down
5 changes: 5 additions & 0 deletions cores/esp8266/esp8266_peri.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#ifndef ESP8266_PERI_H_INCLUDED
#define ESP8266_PERI_H_INCLUDED

// we expect mocking framework to provide these
#ifndef CORE_MOCK

#include "c_types.h"
#include "esp8266_undocumented.h"

Expand Down Expand Up @@ -847,4 +850,6 @@ extern volatile uint32_t* const esp8266_gpioToFn[16];
**/
#define RANDOM_REG32 ESP8266_DREG(0x20E44)

#endif // ifndef CORE_MOCK

#endif
1 change: 1 addition & 0 deletions cores/esp8266/libc_replacements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ void _exit(int status) {
abort();
}

int atexit(void (*func)()) __attribute__((weak));
mcspr marked this conversation as resolved.
Show resolved Hide resolved
int atexit(void (*func)()) {
(void) func;
return 0;
Expand Down
2 changes: 2 additions & 0 deletions libraries/ESP8266WiFi/src/include/ClientContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ typedef void (*discard_cb_t)(void*, ClientContext*);
extern "C" void esp_yield();
extern "C" void esp_schedule();

#include <debug.h>

#include "DataSource.h"

bool getDefaultPrivateGlobalSyncValue ();
Expand Down
1 change: 1 addition & 0 deletions libraries/ESP8266WiFi/src/include/UdpContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void esp_schedule();
}

#include <AddrList.h>
#include <debug.h>

#define PBUF_ALIGNER_ADJUST 4
#define PBUF_ALIGNER(x) ((void*)((((intptr_t)(x))+3)&~3))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Small math example, checking whether we properly integrate with c++ math

ref:
- https://github.com/esp8266/Arduino/issues/5530
- https://github.com/espressif/arduino-esp32/pull/2738

Released to public domain
*/

#include <BSTest.h>
#include <type_traits>

BS_ENV_DECLARE();

void setup()
{
Serial.begin(115200);
BS_RUN(Serial);
}

bool pretest()
{
return true;
}

#define TEST_MATH_IS_SAME(OP1, OP2) \
std::is_same<decltype(OP1), decltype(OP2)>::value

TEST_CASE("std::abs and abs result is the same", "[arduino-math]")
{
CHECK(TEST_MATH_IS_SAME(abs(-5), std::abs(-5)));
CHECK(TEST_MATH_IS_SAME(abs(-25.0), std::abs(-25.0)));
CHECK(TEST_MATH_IS_SAME(abs(10.0), std::abs(10.0)));
CHECK(! TEST_MATH_IS_SAME(abs(10.0), std::abs(10)));
CHECK(! TEST_MATH_IS_SAME(abs(-5), std::abs(10.0)));
}

TEST_CASE("abs works with ints", "[arduino-math]")
{
int a = -3;
int b = 3;
CHECK(TEST_MATH_IS_SAME(abs(a), a));
CHECK(TEST_MATH_IS_SAME(abs(b), b));
CHECK(abs(a) == b);
CHECK(abs(b) == b);
}

template <typename T>
bool compare_floats(T a, T b) {
static_assert(std::is_floating_point<T>::value, "");
return std::fabs(a - b) < std::numeric_limits<float>::epsilon();
}

TEST_CASE("abs works with floats", "[arduino-math]")
{
float a = -3.5;
float b = 3.5;
CHECK(TEST_MATH_IS_SAME(abs(a), a));
CHECK(TEST_MATH_IS_SAME(abs(b), b));
CHECK(compare_floats(abs(a), b));
CHECK(compare_floats(abs(b), b));
}

TEST_CASE("round works with ints", "[arduino-math]")
{
int a = 5;
int b = 10;
CHECK(TEST_MATH_IS_SAME(round(a), std::round(a)));
CHECK(TEST_MATH_IS_SAME(round(b), std::round(b)));
CHECK(compare_floats(round(a), std::round(a)));
CHECK(compare_floats(round(b), std::round(b)));
}

TEST_CASE("round works with floats", "[arduino-math]")
{
float a = 2.9;
float b = 3.0;
CHECK(TEST_MATH_IS_SAME(round(a), a));
CHECK(TEST_MATH_IS_SAME(round(b), b));
CHECK(compare_floats(round(a), b));
CHECK(compare_floats(round(b), b));
}

void loop(){}

1 change: 1 addition & 0 deletions tests/host/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ FLAGS += -DHTTPCLIENT_1_1_COMPATIBLE=0
FLAGS += -DLWIP_IPV6=0
FLAGS += -DHOST_MOCK=1
FLAGS += -DNONOSDK221=1
FLAGS += -DF_CPU=80000000
FLAGS += $(MKFLAGS)
FLAGS += -Wimplicit-fallthrough=2 # allow "// fall through" comments to stop spurious warnings
CXXFLAGS += -std=c++11 -fno-rtti $(FLAGS) -funsigned-char
Expand Down
Loading