Skip to content

Commit

Permalink
Allow test framework to use cores/esp8266/Arduino.h directly (#7377)
Browse files Browse the repository at this point in the history
* Allow test framework to use cores/esp8266/Arduino.h directly
* fix wps debugging
* some more missing debug.h
* Hunt down debug.h and roll-back
  TODO: rename it to something else... it is an internal header
* Move abs+round checks to test/device/test_sw
* Restore macros for C code
* fixup! Move abs+round checks to test/device/test_sw
* Fix bad c/p, actually try round with ints
* tweak c macros per review
* fix gcc-10 missing cerrno include
  • Loading branch information
mcspr committed Oct 6, 2020
1 parent 7ba3101 commit 36b444d
Show file tree
Hide file tree
Showing 17 changed files with 147 additions and 335 deletions.
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 @@ -125,15 +126,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 @@ -162,11 +159,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 @@ -212,28 +210,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) ({ __typeof__(x) _x = (x); _x > 0 ? _x : -_x; })
#undef round
#define round(x) ({ __typeof__(x) _x = (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"

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 @@ -273,8 +274,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
1 change: 1 addition & 0 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
1 change: 0 additions & 1 deletion cores/esp8266/abi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include <stdlib.h>
#include <assert.h>
#include <debug.h>
#include <Arduino.h>
#include <cxxabi.h>

Expand Down
4 changes: 4 additions & 0 deletions cores/esp8266/core_esp8266_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
// 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 @@ -73,6 +74,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));
int atexit(void (*func)()) {
(void) func;
return 0;
Expand Down
5 changes: 3 additions & 2 deletions cores/esp8266/umm_malloc/umm_malloc_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <debug.h>

#include <pgmspace.h>
#include <esp8266_undocumented.h>
#include "../debug.h"
#include "../esp8266_undocumented.h"

#ifdef __cplusplus
extern "C" {
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 @@ -174,6 +174,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
FLAGS += $(USERCFLAGS)
Expand Down
Loading

0 comments on commit 36b444d

Please sign in to comment.