Skip to content

Commit 36b444d

Browse files
authored
Allow test framework to use cores/esp8266/Arduino.h directly (#7377)
* 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
1 parent 7ba3101 commit 36b444d

17 files changed

+147
-335
lines changed

cores/esp8266/Arduino.h

+28-16
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ extern "C" {
3737
#include "binary.h"
3838
#include "esp8266_peri.h"
3939
#include "twi.h"
40+
4041
#include "core_esp8266_features.h"
4142
#include "core_esp8266_version.h"
4243

@@ -125,15 +126,11 @@ void timer0_isr_init(void);
125126
void timer0_attachInterrupt(timercallback userFunc);
126127
void timer0_detachInterrupt(void);
127128

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

134-
void ets_intr_lock();
135-
void ets_intr_unlock();
136-
137134
#define interrupts() xt_rsil(0)
138135
#define noInterrupts() xt_rsil(15)
139136

@@ -162,11 +159,12 @@ typedef uint16_t word;
162159
typedef bool boolean;
163160
typedef uint8_t byte;
164161

162+
void ets_intr_lock();
163+
void ets_intr_unlock();
164+
165165
void init(void);
166166
void initVariant(void);
167167

168-
int atexit(void (*func)()) __attribute__((weak));
169-
170168
void pinMode(uint8_t pin, uint8_t mode);
171169
void digitalWrite(uint8_t pin, uint8_t val);
172170
int digitalRead(uint8_t pin);
@@ -212,28 +210,31 @@ void optimistic_yield(uint32_t interval_us);
212210
} // extern "C"
213211
#endif
214212

213+
// undefine stdlib's definitions when encountered, provide abs that supports floating point for C code
214+
#ifndef __cplusplus
215+
#undef abs
216+
#define abs(x) ({ __typeof__(x) _x = (x); _x > 0 ? _x : -_x; })
217+
#undef round
218+
#define round(x) ({ __typeof__(x) _x = (x); _x >= 0 ? (long)(_x + 0.5) : (long)(_x - 0.5); })
219+
#endif // ifndef __cplusplus
215220

216-
221+
// from this point onward, we need to configure the c++ environment
217222
#ifdef __cplusplus
218223

219224
#include <algorithm>
225+
#include <cstdlib>
220226
#include <cmath>
221-
#include <pgmspace.h>
222-
223-
#include "WCharacter.h"
224-
#include "WString.h"
225-
226-
#include "HardwareSerial.h"
227-
#include "Esp.h"
228-
#include "Updater.h"
229-
#include "debug.h"
230227

231228
using std::min;
232229
using std::max;
233230
using std::round;
234231
using std::isinf;
235232
using std::isnan;
236233

234+
// Use float-compatible stl abs() and round(), we don't use Arduino macros to avoid issues with the C++ libraries
235+
using std::abs;
236+
using std::round;
237+
237238
#define _min(a,b) ({ decltype(a) _a = (a); decltype(b) _b = (b); _a < _b? _a : _b; })
238239
#define _max(a,b) ({ decltype(a) _a = (a); decltype(b) _b = (b); _a > _b? _a : _b; })
239240

@@ -273,8 +274,19 @@ inline void configTzTime(const char* tz, const char* server1,
273274
configTime(tz, server1, server2, server3);
274275
}
275276

277+
// Everything we expect to be implicitly loaded for the sketch
278+
#include <pgmspace.h>
279+
280+
#include "WCharacter.h"
281+
#include "WString.h"
282+
283+
#include "HardwareSerial.h"
284+
#include "Esp.h"
285+
#include "Updater.h"
286+
276287
#endif // __cplusplus
277288

289+
#include "debug.h"
278290
#include "pins_arduino.h"
279291

280292
#endif

cores/esp8266/Esp.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
#include "MD5Builder.h"
2727
#include "umm_malloc/umm_malloc.h"
2828
#include "cont.h"
29+
2930
#include "coredecls.h"
31+
#include <pgmspace.h>
3032

3133
extern "C" {
3234
#include "user_interface.h"

cores/esp8266/Esp.h

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#define ESP_H
2323

2424
#include <Arduino.h>
25+
#include "core_esp8266_features.h"
2526
#include "spi_vendors.h"
2627

2728
/**

cores/esp8266/abi.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
#include <stdlib.h>
2020
#include <assert.h>
21-
#include <debug.h>
2221
#include <Arduino.h>
2322
#include <cxxabi.h>
2423

cores/esp8266/core_esp8266_features.h

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
// level 0 will enable ALL interrupts,
5555
//
5656
#ifndef CORE_MOCK
57+
5758
#define xt_rsil(level) (__extension__({uint32_t state; __asm__ __volatile__("rsil %0," __STRINGIFY(level) : "=a" (state) :: "memory"); state;}))
5859
#define xt_wsr_ps(state) __asm__ __volatile__("wsr %0,ps; isync" :: "a" (state) : "memory")
5960

@@ -73,6 +74,9 @@ inline uint32_t esp_get_program_counter() {
7374

7475
#else // CORE_MOCK
7576

77+
#define xt_rsil(level) (level)
78+
#define xt_wsr_ps(state) do { (void)(state); } while (0)
79+
7680
inline uint32_t esp_get_program_counter() { return 0; }
7781

7882
#endif // CORE_MOCK

cores/esp8266/esp8266_peri.h

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#ifndef ESP8266_PERI_H_INCLUDED
2222
#define ESP8266_PERI_H_INCLUDED
2323

24+
// we expect mocking framework to provide these
25+
#ifndef CORE_MOCK
26+
2427
#include "c_types.h"
2528
#include "esp8266_undocumented.h"
2629

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

853+
#endif // ifndef CORE_MOCK
854+
850855
#endif

cores/esp8266/libc_replacements.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ void _exit(int status) {
127127
abort();
128128
}
129129

130+
int atexit(void (*func)()) __attribute__((weak));
130131
int atexit(void (*func)()) {
131132
(void) func;
132133
return 0;

cores/esp8266/umm_malloc/umm_malloc_cfg.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
#include <stdint.h>
1818
#include <stddef.h>
1919
#include <stdbool.h>
20-
#include <debug.h>
20+
2121
#include <pgmspace.h>
22-
#include <esp8266_undocumented.h>
22+
#include "../debug.h"
23+
#include "../esp8266_undocumented.h"
2324

2425
#ifdef __cplusplus
2526
extern "C" {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Small math example, checking whether we properly integrate with c++ math
3+
4+
ref:
5+
- https://github.com/esp8266/Arduino/issues/5530
6+
- https://github.com/espressif/arduino-esp32/pull/2738
7+
8+
Released to public domain
9+
*/
10+
11+
#include <BSTest.h>
12+
#include <type_traits>
13+
14+
BS_ENV_DECLARE();
15+
16+
void setup()
17+
{
18+
Serial.begin(115200);
19+
BS_RUN(Serial);
20+
}
21+
22+
bool pretest()
23+
{
24+
return true;
25+
}
26+
27+
#define TEST_MATH_IS_SAME(OP1, OP2) \
28+
std::is_same<decltype(OP1), decltype(OP2)>::value
29+
30+
TEST_CASE("std::abs and abs result is the same", "[arduino-math]")
31+
{
32+
CHECK(TEST_MATH_IS_SAME(abs(-5), std::abs(-5)));
33+
CHECK(TEST_MATH_IS_SAME(abs(-25.0), std::abs(-25.0)));
34+
CHECK(TEST_MATH_IS_SAME(abs(10.0), std::abs(10.0)));
35+
CHECK(! TEST_MATH_IS_SAME(abs(10.0), std::abs(10)));
36+
CHECK(! TEST_MATH_IS_SAME(abs(-5), std::abs(10.0)));
37+
}
38+
39+
TEST_CASE("abs works with ints", "[arduino-math]")
40+
{
41+
int a = -3;
42+
int b = 3;
43+
CHECK(TEST_MATH_IS_SAME(abs(a), a));
44+
CHECK(TEST_MATH_IS_SAME(abs(b), b));
45+
CHECK(abs(a) == b);
46+
CHECK(abs(b) == b);
47+
}
48+
49+
template <typename T>
50+
bool compare_floats(T a, T b) {
51+
static_assert(std::is_floating_point<T>::value, "");
52+
return std::fabs(a - b) < std::numeric_limits<float>::epsilon();
53+
}
54+
55+
TEST_CASE("abs works with floats", "[arduino-math]")
56+
{
57+
float a = -3.5;
58+
float b = 3.5;
59+
CHECK(TEST_MATH_IS_SAME(abs(a), a));
60+
CHECK(TEST_MATH_IS_SAME(abs(b), b));
61+
CHECK(compare_floats(abs(a), b));
62+
CHECK(compare_floats(abs(b), b));
63+
}
64+
65+
TEST_CASE("round works with ints", "[arduino-math]")
66+
{
67+
int a = 5;
68+
int b = 10;
69+
CHECK(TEST_MATH_IS_SAME(round(a), std::round(a)));
70+
CHECK(TEST_MATH_IS_SAME(round(b), std::round(b)));
71+
CHECK(compare_floats(round(a), std::round(a)));
72+
CHECK(compare_floats(round(b), std::round(b)));
73+
}
74+
75+
TEST_CASE("round works with floats", "[arduino-math]")
76+
{
77+
float a = 2.9;
78+
float b = 3.0;
79+
CHECK(TEST_MATH_IS_SAME(round(a), a));
80+
CHECK(TEST_MATH_IS_SAME(round(b), b));
81+
CHECK(compare_floats(round(a), b));
82+
CHECK(compare_floats(round(b), b));
83+
}
84+
85+
void loop(){}
86+

tests/host/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ FLAGS += -DHTTPCLIENT_1_1_COMPATIBLE=0
174174
FLAGS += -DLWIP_IPV6=0
175175
FLAGS += -DHOST_MOCK=1
176176
FLAGS += -DNONOSDK221=1
177+
FLAGS += -DF_CPU=80000000
177178
FLAGS += $(MKFLAGS)
178179
FLAGS += -Wimplicit-fallthrough=2 # allow "// fall through" comments to stop spurious warnings
179180
FLAGS += $(USERCFLAGS)

0 commit comments

Comments
 (0)