Skip to content

Commit 9e1b019

Browse files
committed
Merge remote-tracking branch 'origin/master' into tests/sync-arduino-h
2 parents 1f14878 + e2b8f6a commit 9e1b019

39 files changed

+339
-144
lines changed

cores/esp8266/Arduino.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ extern "C" {
4444
#define HIGH 0x1
4545
#define LOW 0x0
4646

47-
#define PWMRANGE 1023
48-
4947
//GPIO FUNCTIONS
5048
#define INPUT 0x00
5149
#define INPUT_PULLUP 0x02
@@ -174,13 +172,9 @@ int analogRead(uint8_t pin);
174172
void analogReference(uint8_t mode);
175173
void analogWrite(uint8_t pin, int val);
176174
void analogWriteFreq(uint32_t freq);
175+
void analogWriteResolution(int res);
177176
void analogWriteRange(uint32_t range);
178177

179-
unsigned long millis(void);
180-
unsigned long micros(void);
181-
uint64_t micros64(void);
182-
void delay(unsigned long);
183-
void delayMicroseconds(unsigned int us);
184178
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
185179
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout);
186180

cores/esp8266/Crypto.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ void *createBearsslHmac(const br_hash_class *hashType, const void *data, const s
100100

101101
String createBearsslHmac(const br_hash_class *hashType, const uint8_t hashTypeNaturalLength, const String &message, const void *hashKey, const size_t hashKeyLength, const size_t hmacLength)
102102
{
103+
(void) hashTypeNaturalLength;
103104
assert(1 <= hmacLength && hmacLength <= hashTypeNaturalLength);
104105

105106
uint8_t hmac[hmacLength];
@@ -152,6 +153,7 @@ void *createBearsslHmacCT(const br_hash_class *hashType, const void *data, const
152153

153154
String createBearsslHmacCT(const br_hash_class *hashType, const uint8_t hashTypeNaturalLength, const String &message, const void *hashKey, const size_t hashKeyLength, const size_t hmacLength)
154155
{
156+
(void) hashTypeNaturalLength;
155157
assert(1 <= hmacLength && hmacLength <= hashTypeNaturalLength);
156158

157159
uint8_t hmac[hmacLength];

cores/esp8266/Esp.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -699,15 +699,16 @@ static SpiFlashOpResult spi_flash_write_puya(uint32_t offset, uint32_t *data, si
699699
} else {
700700
bytesLeft = 0;
701701
}
702-
rc = spi_flash_read(pos, flash_write_puya_buf, bytesNow);
702+
size_t bytesAligned = (bytesNow + 3) & ~3;
703+
rc = spi_flash_read(pos, flash_write_puya_buf, bytesAligned);
703704
if (rc != SPI_FLASH_RESULT_OK) {
704705
return rc;
705706
}
706-
for (size_t i = 0; i < bytesNow / 4; ++i) {
707+
for (size_t i = 0; i < bytesAligned / 4; ++i) {
707708
flash_write_puya_buf[i] &= *ptr;
708709
++ptr;
709710
}
710-
rc = spi_flash_write(pos, flash_write_puya_buf, bytesNow);
711+
rc = spi_flash_write(pos, flash_write_puya_buf, bytesAligned);
711712
pos += bytesNow;
712713
}
713714
return rc;

cores/esp8266/Esp.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,13 @@ class EspClass {
125125
#if defined(F_CPU) || defined(CORE_MOCK)
126126
constexpr uint8_t getCpuFreqMHz() const
127127
{
128-
return clockCyclesPerMicrosecond();
128+
return esp_get_cpu_freq_mhz();
129129
}
130130
#else
131-
uint8_t getCpuFreqMHz();
131+
uint8_t getCpuFreqMHz() const
132+
{
133+
return esp_get_cpu_freq_mhz();
134+
}
132135
#endif
133136

134137
uint32_t getFlashChipId();

cores/esp8266/HardwareSerial.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
#include "HardwareSerial.h"
3333
#include "Esp.h"
3434

35+
36+
// SerialEvent functions are weak, so when the user doesn't define them,
37+
// the linker just sets their address to 0 (which is checked below).
38+
// The Serialx_available is just a wrapper around Serialx.available(),
39+
// but we can refer to it weakly so we don't pull in the entire
40+
// HardwareSerial instance if the user doesn't also refer to it.
41+
void serialEvent() __attribute__((weak));
42+
3543
HardwareSerial::HardwareSerial(int uart_nr)
3644
: _uart_nr(uart_nr), _rx_size(256)
3745
{}
@@ -162,6 +170,14 @@ size_t HardwareSerial::readBytes(char* buffer, size_t size)
162170

163171
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
164172
HardwareSerial Serial(UART0);
173+
174+
// Executed at end of loop() processing when > 0 bytes available in the Serial port
175+
void serialEventRun(void)
176+
{
177+
if (serialEvent && Serial.available()) {
178+
serialEvent();
179+
}
180+
}
165181
#endif
166182
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL1)
167183
HardwareSerial Serial1(UART1);

cores/esp8266/HardwareSerial.h

+2
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,6 @@ class HardwareSerial: public Stream
207207
extern HardwareSerial Serial;
208208
extern HardwareSerial Serial1;
209209

210+
extern void serialEventRun(void) __attribute__((weak));
211+
210212
#endif

cores/esp8266/PolledTimeout.h

+37-17
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/*
66
PolledTimeout.h - Encapsulation of a polled Timeout
7-
7+
88
Copyright (c) 2018 Daniel Salazar. All rights reserved.
99
This file is part of the esp8266 core for Arduino environment.
1010
@@ -23,9 +23,10 @@
2323
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2424
*/
2525

26-
#include <limits>
27-
28-
#include <Arduino.h>
26+
#include <c_types.h> // IRAM_ATTR
27+
#include <limits> // std::numeric_limits
28+
#include <type_traits> // std::is_unsigned
29+
#include <core_esp8266_features.h>
2930

3031
namespace esp8266
3132
{
@@ -70,13 +71,13 @@ struct TimeSourceMillis
7071

7172
struct TimeSourceCycles
7273
{
73-
// time policy based on ESP.getCycleCount()
74+
// time policy based on esp_get_cycle_count()
7475
// this particular time measurement is intended to be called very often
7576
// (every loop, every yield)
7677

77-
using timeType = decltype(ESP.getCycleCount());
78-
static timeType time() {return ESP.getCycleCount();}
79-
static constexpr timeType ticksPerSecond = ESP.getCpuFreqMHz() * 1000000UL; // 80'000'000 or 160'000'000 Hz
78+
using timeType = decltype(esp_get_cycle_count());
79+
static timeType time() {return esp_get_cycle_count();}
80+
static constexpr timeType ticksPerSecond = esp_get_cpu_freq_mhz() * 1000000UL; // 80'000'000 or 160'000'000 Hz
8081
static constexpr timeType ticksPerSecondMax = 160000000; // 160MHz
8182
};
8283

@@ -161,13 +162,13 @@ class timeoutTemplate
161162
return expiredRetrigger();
162163
return expiredOneShot();
163164
}
164-
165+
165166
IRAM_ATTR // fast
166167
operator bool()
167168
{
168-
return expired();
169+
return expired();
169170
}
170-
171+
171172
bool canExpire () const
172173
{
173174
return !_neverExpires;
@@ -178,6 +179,7 @@ class timeoutTemplate
178179
return _timeout != alwaysExpired;
179180
}
180181

182+
// Resets, will trigger after this new timeout.
181183
IRAM_ATTR // called from ISR
182184
void reset(const timeType newUserTimeout)
183185
{
@@ -186,12 +188,30 @@ class timeoutTemplate
186188
_neverExpires = (newUserTimeout < 0) || (newUserTimeout > timeMax());
187189
}
188190

191+
// Resets, will trigger after the timeout previously set.
189192
IRAM_ATTR // called from ISR
190193
void reset()
191194
{
192195
_start = TimePolicyT::time();
193196
}
194197

198+
// Resets to just expired so that on next poll the check will immediately trigger for the user,
199+
// also change timeout (after next immediate trigger).
200+
IRAM_ATTR // called from ISR
201+
void resetAndSetExpired (const timeType newUserTimeout)
202+
{
203+
reset(newUserTimeout);
204+
_start -= _timeout;
205+
}
206+
207+
// Resets to just expired so that on next poll the check will immediately trigger for the user.
208+
IRAM_ATTR // called from ISR
209+
void resetAndSetExpired ()
210+
{
211+
reset();
212+
_start -= _timeout;
213+
}
214+
195215
void resetToNeverExpires ()
196216
{
197217
_timeout = alwaysExpired + 1; // because canWait() has precedence
@@ -202,7 +222,7 @@ class timeoutTemplate
202222
{
203223
return TimePolicyT::toUserUnit(_timeout);
204224
}
205-
225+
206226
static constexpr timeType timeMax()
207227
{
208228
return TimePolicyT::timeMax;
@@ -235,14 +255,14 @@ class timeoutTemplate
235255
}
236256
return false;
237257
}
238-
258+
239259
IRAM_ATTR // fast
240260
bool expiredOneShot() const
241261
{
242262
// returns "always expired" or "has expired"
243263
return !canWait() || checkExpired(TimePolicyT::time());
244264
}
245-
265+
246266
timeType _timeout;
247267
timeType _start;
248268
bool _neverExpires;
@@ -259,14 +279,14 @@ using periodic = polledTimeout::timeoutTemplate<true> /*__attribute__((deprecate
259279
using oneShotMs = polledTimeout::timeoutTemplate<false>;
260280
using periodicMs = polledTimeout::timeoutTemplate<true>;
261281

262-
// Time policy based on ESP.getCycleCount(), and intended to be called very often:
282+
// Time policy based on esp_get_cycle_count(), and intended to be called very often:
263283
// "Fast" versions sacrifices time range for improved precision and reduced execution time (by 86%)
264-
// (cpu cycles for ::expired(): 372 (millis()) vs 52 (ESP.getCycleCount()))
284+
// (cpu cycles for ::expired(): 372 (millis()) vs 52 (esp_get_cycle_count()))
265285
// timeMax() values:
266286
// Ms: max is 26843 ms (26.8 s)
267287
// Us: max is 26843545 us (26.8 s)
268288
// Ns: max is 1073741823 ns ( 1.07 s)
269-
// (time policy based on ESP.getCycleCount() is intended to be called very often)
289+
// (time policy based on esp_get_cycle_count() is intended to be called very often)
270290

271291
using oneShotFastMs = polledTimeout::timeoutTemplate<false, YieldPolicy::DoNothing, TimePolicy::TimeFastMillis>;
272292
using periodicFastMs = polledTimeout::timeoutTemplate<true, YieldPolicy::DoNothing, TimePolicy::TimeFastMillis>;

cores/esp8266/base64.h

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#ifndef CORE_BASE64_H_
2626
#define CORE_BASE64_H_
2727

28+
#include <WString.h>
29+
2830
class base64
2931
{
3032
public:

cores/esp8266/core_esp8266_features.h

+23
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,29 @@ extern "C" {
129129
#endif
130130

131131
void precache(void *f, uint32_t bytes);
132+
unsigned long millis(void);
133+
unsigned long micros(void);
134+
uint64_t micros64(void);
135+
void delay(unsigned long);
136+
void delayMicroseconds(unsigned int us);
137+
138+
#if defined(F_CPU) || defined(CORE_MOCK)
139+
#ifdef __cplusplus
140+
constexpr
141+
#else
142+
inline
143+
#endif
144+
int esp_get_cpu_freq_mhz()
145+
{
146+
return F_CPU / 1000000L;
147+
}
148+
#else
149+
inline int esp_get_cpu_freq_mhz()
150+
{
151+
return system_get_cpu_freq();
152+
}
153+
#endif
154+
132155

133156
#ifdef __cplusplus
134157
}

cores/esp8266/core_esp8266_main.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ static void loop_wrapper() {
195195
}
196196
loop();
197197
loop_end();
198+
if (serialEventRun) {
199+
serialEventRun();
200+
}
198201
esp_schedule();
199202
}
200203

cores/esp8266/core_esp8266_wiring_pwm.cpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,21 @@
2727
extern "C" {
2828

2929
static uint32_t analogMap = 0;
30-
static int32_t analogScale = PWMRANGE;
30+
static int32_t analogScale = 255; // Match upstream default, breaking change from 2.x.x
3131
static uint16_t analogFreq = 1000;
3232

3333
extern void __analogWriteRange(uint32_t range) {
34-
if (range > 0) {
34+
if ((range >= 15) && (range <= 65535)) {
3535
analogScale = range;
3636
}
3737
}
3838

39+
extern void __analogWriteResolution(int res) {
40+
if ((res >= 4) && (res <= 16)) {
41+
analogScale = (1 << res) - 1;
42+
}
43+
}
44+
3945
extern void __analogWriteFreq(uint32_t freq) {
4046
if (freq < 100) {
4147
analogFreq = 100;
@@ -57,6 +63,10 @@ extern void __analogWrite(uint8_t pin, int val) {
5763
val = analogScale;
5864
}
5965

66+
// Per the Arduino docs at https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/
67+
// val: the duty cycle: between 0 (always off) and 255 (always on).
68+
// So if val = 0 we have digitalWrite(LOW), if we have val==range we have digitalWrite(HIGH)
69+
6070
analogMap &= ~(1 << pin);
6171
uint32_t high = (analogPeriod * val) / analogScale;
6272
uint32_t low = analogPeriod - high;
@@ -75,5 +85,6 @@ extern void __analogWrite(uint8_t pin, int val) {
7585
extern void analogWrite(uint8_t pin, int val) __attribute__((weak, alias("__analogWrite")));
7686
extern void analogWriteFreq(uint32_t freq) __attribute__((weak, alias("__analogWriteFreq")));
7787
extern void analogWriteRange(uint32_t range) __attribute__((weak, alias("__analogWriteRange")));
88+
extern void analogWriteResolution(int res) __attribute__((weak, alias("__analogWriteResolution")));
7889

7990
};

doc/boards.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,6 @@ DOIT ESP-Mx DevKit (ESP8285)
476476
----------------------------
477477

478478
DOIT ESP-Mx DevKit - This is a development board by DOIT, with a DOIT ESP-Mx module (`datasheet <https://github.com/SmartArduino/SZDOITWiKi/wiki/ESP8285---ESP-M2>`__) using a ESP8285 Chip. With the DOIT ESP-Mx module, GPIO pins 9 and 10 are not available. The DOIT ESP-Mx DevKit board has a red power LED and a blue LED connected to GPIO16 and is active low to turn on. It uses a CH340C, USB to Serial converter chip.
479-
ESP8285 (`datasheet <http://www.espressif.com/sites/default/files/0a-esp8285_datasheet_en_v1.0_20160422.pdf>`__) is a multi-chip package which contains ESP8266 and 1MB flash.
480479

480+
ESP8285 (`datasheet <http://www.espressif.com/sites/default/files/0a-esp8285_datasheet_en_v1.0_20160422.pdf>`__) is a multi-chip package which contains ESP8266 and 1MB flash.
481481

doc/esp8266wifi/bearssl-client-secure-class.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ setFingerprint(const uint8_t fp[20]) / setFingerprint(const char \*fpStr)
139139

140140
Verify the SHA1 fingerprint of the certificate returned matches this one. If the server certificate changes, it will fail. If an array of 20 bytes are sent in, it is assumed they are the binary SHA1 values. If a `char*` string is passed in, it is parsed as a series of human-readable hex values separated by spaces or colons (e.g. `setFingerprint("00:01:02:03:...:1f");`)
141141

142-
This fingerprint is calcuated on the raw X509 certificate served by the server. In very rare cases, these certificates have certain encodings which should be normalized before taking a fingerprint (but in order to preserve memory BearSSL does not do this normalization since it would need RAM for an entire copy of the cert), and the fingerprint BearSSL calculates will not match the fingerprint OpenSSL calculates. In this case, you can enable SSL debugging and get a dump of BearSSL's calculated fingerprint and use that one in your code, or use full certificate validation. See the `original issue and debug here <https://github.com/esp8266/Arduino/issues/6209>`__.
142+
This fingerprint is calculated on the raw X509 certificate served by the server. In very rare cases, these certificates have certain encodings which should be normalized before taking a fingerprint (but in order to preserve memory BearSSL does not do this normalization since it would need RAM for an entire copy of the cert), and the fingerprint BearSSL calculates will not match the fingerprint OpenSSL calculates. In this case, you can enable SSL debugging and get a dump of BearSSL's calculated fingerprint and use that one in your code, or use full certificate validation. See the `original issue and debug here <https://github.com/esp8266/Arduino/issues/6209>`__.
143143

144144
setTrustAnchors(BearSSL::X509List \*ta)
145145
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

doc/esp8266wifi/client-class.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ In this mode, every ``write()`` is flushed. It means that after a call to
6161

6262
When set to ``true`` in ``WiFiClient`` implementation,
6363

64-
- It slows down transfers, and implicitely disable the Nagle algorithm.
64+
- It slows down transfers, and implicitly disable the Nagle algorithm.
6565

6666
- It also allows to avoid a temporary copy of data that otherwise consumes
6767
at most ``TCP_SND_BUF`` = (2 * ``MSS``) bytes per connection,

doc/esp8266wifi/scan-class.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ Return information if a network discovered during the scan is hidden or not.
191191
192192
WiFi.isHidden(networkItem)
193193
194-
Returned value if the ``bolean`` type, and ``true`` means that network is hidden. The ``networkItem`` is a zero based index of network discovered during scan.
194+
Returned value if the ``boolean`` type, and ``true`` means that network is hidden. The ``networkItem`` is a zero based index of network discovered during scan.
195195

196196
getNetworkInfo
197197
^^^^^^^^^^^^^^

doc/esp8266wifi/scan-examples.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ Complete sketch is below. The code inside ``setup()`` is the same as described i
206206
lastScanMillis = currentMillis;
207207
}
208208
209-
// print out Wi-Fi network scan result uppon completion
209+
// print out Wi-Fi network scan result upon completion
210210
int n = WiFi.scanComplete();
211211
if(n >= 0)
212212
{

0 commit comments

Comments
 (0)