Skip to content

Commit 84d8866

Browse files
committed
Allow test framework to use cores/esp8266/Arduino.h directly
1 parent 599492e commit 84d8866

File tree

11 files changed

+89
-317
lines changed

11 files changed

+89
-317
lines changed

cores/esp8266/Arduino.h

+31-17
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

@@ -127,21 +128,26 @@ void timer0_isr_init(void);
127128
void timer0_attachInterrupt(timercallback userFunc);
128129
void timer0_detachInterrupt(void);
129130

130-
// undefine stdlib's abs if encountered
131+
// undefine stdlib's definitions when encountered, provide abs that supports floating point for C code
132+
// in case we are using c++, these will either be:
133+
// - undef'ed by the algorithm include down below, implicitly including cstdlib
134+
// - undef'ed by the stdlib.h header up above in a more recent versions of gcc
131135
#ifdef abs
132136
#undef abs
137+
#define abs(x) ((x)>0?(x):-(x))
133138
#endif
134139

135-
#define abs(x) ((x)>0?(x):-(x))
140+
#ifdef round
141+
#undef round
142+
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
143+
#endif
144+
145+
// the rest of math definitions are from Arduino
136146
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
137-
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
138147
#define radians(deg) ((deg)*DEG_TO_RAD)
139148
#define degrees(rad) ((rad)*RAD_TO_DEG)
140149
#define sq(x) ((x)*(x))
141150

142-
void ets_intr_lock();
143-
void ets_intr_unlock();
144-
145151
#define interrupts() xt_rsil(0)
146152
#define noInterrupts() xt_rsil(15)
147153

@@ -170,11 +176,12 @@ typedef uint16_t word;
170176
typedef bool boolean;
171177
typedef uint8_t byte;
172178

179+
void ets_intr_lock();
180+
void ets_intr_unlock();
181+
173182
void init(void);
174183
void initVariant(void);
175184

176-
int atexit(void (*func)()) __attribute__((weak));
177-
178185
void pinMode(uint8_t pin, uint8_t mode);
179186
void digitalWrite(uint8_t pin, uint8_t val);
180187
int digitalRead(uint8_t pin);
@@ -233,25 +240,22 @@ const int TIM_DIV265 __attribute__((deprecated, weak)) = TIM_DIV256;
233240

234241

235242

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

238246
#include <algorithm>
247+
#include <cstdlib>
239248
#include <cmath>
240-
#include <pgmspace.h>
241-
242-
#include "WCharacter.h"
243-
#include "WString.h"
244-
245-
#include "HardwareSerial.h"
246-
#include "Esp.h"
247-
#include "Updater.h"
248-
#include "debug.h"
249249

250250
using std::min;
251251
using std::max;
252252
using std::isinf;
253253
using std::isnan;
254254

255+
// these are important, as we may end up using C versions otherwise
256+
using std::abs;
257+
using std::round;
258+
255259
#define _min(a,b) ({ decltype(a) _a = (a); decltype(b) _b = (b); _a < _b? _a : _b; })
256260
#define _max(a,b) ({ decltype(a) _a = (a); decltype(b) _b = (b); _a > _b? _a : _b; })
257261

@@ -291,6 +295,16 @@ inline void configTzTime(const char* tz, const char* server1,
291295
configTime(tz, server1, server2, server3);
292296
}
293297

298+
// Everything we expect to be implicitly loaded for the sketch
299+
#include <pgmspace.h>
300+
301+
#include "WCharacter.h"
302+
#include "WString.h"
303+
304+
#include "HardwareSerial.h"
305+
#include "Esp.h"
306+
#include "Updater.h"
307+
294308
#endif // __cplusplus
295309

296310
#include "pins_arduino.h"

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

+4-10
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
/**
@@ -168,21 +169,14 @@ class EspClass {
168169
uint32_t random() const;
169170

170171
#ifndef CORE_MOCK
171-
inline uint32_t getCycleCount() __attribute__((always_inline));
172+
uint32_t getCycleCount() __attribute__((always_inline)) {
173+
return esp_get_cycle_count();
174+
}
172175
#else
173176
uint32_t getCycleCount();
174177
#endif
175178
};
176179

177-
#ifndef CORE_MOCK
178-
179-
uint32_t EspClass::getCycleCount()
180-
{
181-
return esp_get_cycle_count();
182-
}
183-
184-
#endif // !defined(CORE_MOCK)
185-
186180
extern EspClass ESP;
187181

188182
#endif //ESP_H

cores/esp8266/core_esp8266_features.h

+11-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838

3939
#ifdef __cplusplus
4040

41+
#include <pgmspace.h>
42+
4143
namespace arduino
4244
{
4345
extern "C++"
@@ -82,7 +84,13 @@ namespace arduino
8284
// level 15 will disable ALL interrupts,
8385
// level 0 will enable ALL interrupts,
8486
//
85-
#ifndef CORE_MOCK
87+
#ifdef CORE_MOCK
88+
89+
#define xt_rsil(level) (level)
90+
#define xt_wsr_ps(state) do { (void)(state); } while (0)
91+
92+
#else
93+
8694
#define xt_rsil(level) (__extension__({uint32_t state; __asm__ __volatile__("rsil %0," __STRINGIFY(level) : "=a" (state) :: "memory"); state;}))
8795
#define xt_wsr_ps(state) __asm__ __volatile__("wsr %0,ps; isync" :: "a" (state) : "memory")
8896

@@ -92,7 +100,8 @@ inline uint32_t esp_get_cycle_count() {
92100
__asm__ __volatile__("rsr %0,ccount":"=a"(ccount));
93101
return ccount;
94102
}
95-
#endif // not CORE_MOCK
103+
104+
#endif // ifdef CORE_MOCK
96105

97106

98107
// Tools for preloading code into the flash cache

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;
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Small math example, checking whether we can support various Arduino math operations.
3+
Meant for debugging **only**
4+
5+
Released to public domain
6+
*/
7+
8+
void setup() {
9+
Serial.begin(115200);
10+
11+
// checking if we can support Arduino functions
12+
Serial.printf("abs(-3)\n = %d", abs(-3));
13+
Serial.printf("abs(+3)\n = %d", abs(3));
14+
Serial.printf("abs(-3.5)\n = %f", abs(-3.5));
15+
Serial.printf("abs(+3.5)\n = %f", abs(3.5));
16+
Serial.printf("round(2.9)\n = %f", round(2.9));
17+
Serial.printf("constrain(5, 1, 15) = %d\n", constrain(5, 1, 15));
18+
Serial.printf("constrain(16, 1, 15) = %d\n", constrain(16, 1, 15));
19+
20+
// the same thing, but with c++ std::..., which should not cause any conflicts
21+
Serial.printf("std::abs(-3) = %d\n", std::abs(-3));
22+
Serial.printf("std::abs(+3) = %d\n", std::abs(3));
23+
Serial.printf("std::abs(-3.5) = %f\n", std::abs(-3.5));
24+
Serial.printf("std::abs(+3.5) = %f\n", std::abs(3.5));
25+
Serial.printf("std::round(2.9) = %f\n", std::round(2.9));
26+
}
27+
28+
void loop() {
29+
}
30+

tests/host/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ FLAGS += -DHTTPCLIENT_1_1_COMPATIBLE=0
157157
FLAGS += -DLWIP_IPV6=0
158158
FLAGS += -DHOST_MOCK=1
159159
FLAGS += -DNONOSDK221=1
160+
FLAGS += -DF_CPU=80000000
160161
FLAGS += $(MKFLAGS)
161162
FLAGS += -Wimplicit-fallthrough=2 # allow "// fall through" comments to stop spurious warnings
162163
CXXFLAGS += -std=c++11 -fno-rtti $(FLAGS) -funsigned-char

0 commit comments

Comments
 (0)