Skip to content

Commit 218af70

Browse files
authored
Merge branch 'master' into master
2 parents fce93af + bea64df commit 218af70

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+881
-552
lines changed

cores/esp8266/Arduino.h

+8
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ void setup(void);
204204
void loop(void);
205205

206206
void yield(void);
207+
207208
void optimistic_yield(uint32_t interval_us);
208209

209210
#define _PORT_GPIO16 1
@@ -281,6 +282,13 @@ void configTime(int timezone, int daylightOffset_sec, const char* server1,
281282
void configTime(const char* tz, const char* server1,
282283
const char* server2 = nullptr, const char* server3 = nullptr);
283284

285+
// esp32 api compatibility
286+
inline void configTzTime(const char* tz, const char* server1,
287+
const char* server2 = nullptr, const char* server3 = nullptr)
288+
{
289+
configTime(tz, server1, server2, server3);
290+
}
291+
284292
#endif // __cplusplus
285293

286294
#include "pins_arduino.h"

cores/esp8266/FS.cpp

+15-7
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ time_t File::getLastWrite() {
187187
return _p->getLastWrite();
188188
}
189189

190+
time_t File::getCreationTime() {
191+
if (!_p)
192+
return 0;
193+
194+
return _p->getCreationTime();
195+
}
196+
190197
void File::setTimeCallback(time_t (*cb)(void)) {
191198
if (!_p)
192199
return;
@@ -224,6 +231,12 @@ time_t Dir::fileTime() {
224231
return _impl->fileTime();
225232
}
226233

234+
time_t Dir::fileCreationTime() {
235+
if (!_impl)
236+
return 0;
237+
return _impl->fileCreationTime();
238+
}
239+
227240
size_t Dir::fileSize() {
228241
if (!_impl) {
229242
return 0;
@@ -262,17 +275,11 @@ bool Dir::rewind() {
262275
return _impl->rewind();
263276
}
264277

265-
time_t Dir::getLastWrite() {
266-
if (!_impl)
267-
return 0;
268-
269-
return _impl->getLastWrite();
270-
}
271-
272278
void Dir::setTimeCallback(time_t (*cb)(void)) {
273279
if (!_impl)
274280
return;
275281
_impl->setTimeCallback(cb);
282+
timeCallback = cb;
276283
}
277284

278285

@@ -289,6 +296,7 @@ bool FS::begin() {
289296
DEBUGV("#error: FS: no implementation");
290297
return false;
291298
}
299+
_impl->setTimeCallback(timeCallback);
292300
bool ret = _impl->begin();
293301
DEBUGV("%s\n", ret? "": "#error: FS could not start");
294302
return ret;

cores/esp8266/FS.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ class File : public Stream
112112
String readString() override;
113113

114114
time_t getLastWrite();
115+
time_t getCreationTime();
115116
void setTimeCallback(time_t (*cb)(void));
116117

117118
protected:
@@ -120,7 +121,6 @@ class File : public Stream
120121
// Arduino SD class emulation
121122
std::shared_ptr<Dir> _fakeDir;
122123
FS *_baseFS;
123-
time_t (*timeCallback)(void) = nullptr;
124124
};
125125

126126
class Dir {
@@ -132,20 +132,19 @@ class Dir {
132132
String fileName();
133133
size_t fileSize();
134134
time_t fileTime();
135+
time_t fileCreationTime();
135136
bool isFile() const;
136137
bool isDirectory() const;
137138

138139
bool next();
139140
bool rewind();
140141

141-
time_t getLastWrite();
142142
void setTimeCallback(time_t (*cb)(void));
143143

144144
protected:
145145
DirImplPtr _impl;
146146
FS *_baseFS;
147147
time_t (*timeCallback)(void) = nullptr;
148-
149148
};
150149

151150
// Backwards compatible, <4GB filesystem usage

cores/esp8266/FSImpl.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class FileImpl {
5151
// as the FS is allowed to return either the time of the last write() operation or the
5252
// time present in the filesystem metadata (often the last time the file was closed)
5353
virtual time_t getLastWrite() { return 0; } // Default is to not support timestamps
54+
// Same for creation time.
55+
virtual time_t getCreationTime() { return 0; } // Default is to not support timestamps
5456

5557
protected:
5658
time_t (*timeCallback)(void) = nullptr;
@@ -75,7 +77,11 @@ class DirImpl {
7577
virtual FileImplPtr openFile(OpenMode openMode, AccessMode accessMode) = 0;
7678
virtual const char* fileName() = 0;
7779
virtual size_t fileSize() = 0;
80+
// Return the last written time for a file. Undefined when called on a writable file
81+
// as the FS is allowed to return either the time of the last write() operation or the
82+
// time present in the filesystem metadata (often the last time the file was closed)
7883
virtual time_t fileTime() { return 0; } // By default, FS doesn't report file times
84+
virtual time_t fileCreationTime() { return 0; } // By default, FS doesn't report file times
7985
virtual bool isFile() const = 0;
8086
virtual bool isDirectory() const = 0;
8187
virtual bool next() = 0;
@@ -86,11 +92,6 @@ class DirImpl {
8692
// same name. The default implementation simply returns time(&null)
8793
virtual void setTimeCallback(time_t (*cb)(void)) { timeCallback = cb; }
8894

89-
// Return the last written time for a file. Undefined when called on a writable file
90-
// as the FS is allowed to return either the time of the last write() operation or the
91-
// time present in the filesystem metadata (often the last time the file was closed)
92-
virtual time_t getLastWrite() { return 0; } // Default is to not support timestamps
93-
9495
protected:
9596
time_t (*timeCallback)(void) = nullptr;
9697
};

cores/esp8266/StackThunk.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,14 @@ uint32_t stack_thunk_get_max_usage()
111111
/* Print the stack from the first used 16-byte chunk to the top, decodable by the exception decoder */
112112
void stack_thunk_dump_stack()
113113
{
114-
uint32_t *pos = stack_thunk_top;
115-
while (pos < stack_thunk_ptr) {
114+
uint32_t *pos = stack_thunk_ptr;
115+
while (pos < stack_thunk_top) {
116116
if ((pos[0] != _stackPaint) || (pos[1] != _stackPaint) || (pos[2] != _stackPaint) || (pos[3] != _stackPaint))
117117
break;
118118
pos += 4;
119119
}
120120
ets_printf(">>>stack>>>\n");
121-
while (pos < stack_thunk_ptr) {
121+
while (pos < stack_thunk_top) {
122122
ets_printf("%08x: %08x %08x %08x %08x\n", (int32_t)pos, pos[0], pos[1], pos[2], pos[3]);
123123
pos += 4;
124124
}

cores/esp8266/core_esp8266_main.cpp

+12-7
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ extern "C" {
3737

3838
#define LOOP_TASK_PRIORITY 1
3939
#define LOOP_QUEUE_SIZE 1
40-
#define OPTIMISTIC_YIELD_TIME_US 16000
4140

4241
extern "C" void call_user_start();
4342
extern void loop();
@@ -58,7 +57,7 @@ cont_t* g_pcont __attribute__((section(".noinit")));
5857
static os_event_t s_loop_queue[LOOP_QUEUE_SIZE];
5958

6059
/* Used to implement optimistic_yield */
61-
static uint32_t s_micros_at_task_start;
60+
static uint32_t s_cycles_at_yield_start;
6261

6362
/* For ets_intr_lock_nest / ets_intr_unlock_nest
6463
* Max nesting seen by SDK so far is 2.
@@ -97,6 +96,7 @@ extern "C" bool can_yield() {
9796
static inline void esp_yield_within_cont() __attribute__((always_inline));
9897
static void esp_yield_within_cont() {
9998
cont_yield(g_pcont);
99+
s_cycles_at_yield_start = ESP.getCycleCount();
100100
run_scheduled_recurrent_functions();
101101
}
102102

@@ -125,14 +125,19 @@ extern "C" void __yield() {
125125
extern "C" void yield(void) __attribute__ ((weak, alias("__yield")));
126126

127127
extern "C" void optimistic_yield(uint32_t interval_us) {
128-
if (can_yield() &&
129-
(system_get_time() - s_micros_at_task_start) > interval_us)
128+
const uint32_t intvl_cycles = interval_us *
129+
#if defined(F_CPU)
130+
clockCyclesPerMicrosecond();
131+
#else
132+
ESP.getCpuFreqMHz();
133+
#endif
134+
if ((ESP.getCycleCount() - s_cycles_at_yield_start) > intvl_cycles &&
135+
can_yield())
130136
{
131137
yield();
132138
}
133139
}
134140

135-
136141
// Replace ets_intr_(un)lock with nestable versions
137142
extern "C" void IRAM_ATTR ets_intr_lock() {
138143
if (ets_intr_lock_stack_ptr < ETS_INTR_LOCK_NEST_MAX)
@@ -183,7 +188,7 @@ static void loop_wrapper() {
183188

184189
static void loop_task(os_event_t *events) {
185190
(void) events;
186-
s_micros_at_task_start = system_get_time();
191+
s_cycles_at_yield_start = ESP.getCycleCount();
187192
cont_run(g_pcont, &loop_wrapper);
188193
if (cont_check(g_pcont) != 0) {
189194
panic();
@@ -211,7 +216,7 @@ extern void __unhandled_exception(const char *str);
211216
static void __unhandled_exception_cpp()
212217
{
213218
#ifndef __EXCEPTIONS
214-
abort();
219+
abort();
215220
#else
216221
static bool terminating;
217222
if (terminating)

cores/esp8266/core_esp8266_noniso.cpp

+74-4
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
2323
*/
2424

25-
#include <stdio.h>
2625
#include <stdlib.h>
2726
#include <string.h>
2827
#include <stdbool.h>
2928
#include <stdint.h>
3029
#include <math.h>
30+
#include <limits>
3131
#include "stdlib_noniso.h"
3232

3333
extern "C" {
@@ -41,9 +41,79 @@ char* ultoa(unsigned long value, char* result, int base) {
4141
}
4242

4343
char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
44-
char fmt[32];
45-
sprintf(fmt, "%%%d.%df", width, prec);
46-
sprintf(s, fmt, number);
44+
bool negative = false;
45+
46+
if (isnan(number)) {
47+
strcpy(s, "nan");
48+
return s;
49+
}
50+
if (isinf(number)) {
51+
strcpy(s, "inf");
52+
return s;
53+
}
54+
55+
char* out = s;
56+
57+
int fillme = width; // how many cells to fill for the integer part
58+
if (prec > 0) {
59+
fillme -= (prec+1);
60+
}
61+
62+
// Handle negative numbers
63+
if (number < 0.0) {
64+
negative = true;
65+
fillme--;
66+
number = -number;
67+
}
68+
69+
// Round correctly so that print(1.999, 2) prints as "2.00"
70+
// I optimized out most of the divisions
71+
double rounding = 2.0;
72+
for (uint8_t i = 0; i < prec; ++i)
73+
rounding *= 10.0;
74+
rounding = 1.0 / rounding;
75+
76+
number += rounding;
77+
78+
// Figure out how big our number really is
79+
double tenpow = 1.0;
80+
int digitcount = 1;
81+
double nextpow;
82+
while (number >= (nextpow = (10.0 * tenpow))) {
83+
tenpow = nextpow;
84+
digitcount++;
85+
}
86+
87+
// minimal compensation for possible lack of precision (#7087 addition)
88+
number *= 1 + std::numeric_limits<decltype(number)>::epsilon();
89+
90+
number /= tenpow;
91+
fillme -= digitcount;
92+
93+
// Pad unused cells with spaces
94+
while (fillme-- > 0) {
95+
*out++ = ' ';
96+
}
97+
98+
// Handle negative sign
99+
if (negative) *out++ = '-';
100+
101+
// Print the digits, and if necessary, the decimal point
102+
digitcount += prec;
103+
int8_t digit = 0;
104+
while (digitcount-- > 0) {
105+
digit = (int8_t)number;
106+
if (digit > 9) digit = 9; // insurance
107+
*out++ = (char)('0' | digit);
108+
if ((digitcount == prec) && (prec > 0)) {
109+
*out++ = '.';
110+
}
111+
number -= digit;
112+
number *= 10.0;
113+
}
114+
115+
// make sure the string is terminated
116+
*out = 0;
47117
return s;
48118
}
49119

cores/esp8266/coredecls.h

+2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ extern "C" {
1313
#include <cont.h> // g_pcont declaration
1414

1515
extern bool timeshift64_is_set;
16+
extern uint32_t sntp_real_timestamp;
1617

1718
bool can_yield();
1819
void esp_yield();
1920
void esp_schedule();
2021
void tune_timeshift64 (uint64_t now_us);
2122
void disable_extra4k_at_link_time (void) __attribute__((noinline));
23+
bool sntp_set_timezone_in_seconds(int32_t timezone);
2224

2325
uint32_t sqrt32 (uint32_t n);
2426
uint32_t crc32 (const void* data, size_t length, uint32_t crc = 0xffffffff);

0 commit comments

Comments
 (0)