arduino `yield()` 函数的秘密是什么?
What is the secret of the arduino `yield()`function?
Arduino 文档在 https://www.arduino.cc/en/Reference/Scheduler 处解释了 yield()
关于 Due 的问题。显然它是调度程序库的一部分:
#include <Scheduler.h>
但是,我可以在我的 Nano 或 ESP8266 上调用 yield()
而无需包含调度程序库——但只能在我的主程序中调用,而不是在包含文件中。此外,包含不适用于我的非会费。
关于 yield()
我想念的秘密是什么,或者- yield()
在 Arduino 平台上除了 Due 之外做了什么?
However, I can call yield() on my Nano or ESP8266 without including
the Scheduler lib
yield()
函数也在ESP8266库中实现:
Yielding
This is one of the most critical differences between the ESP8266 and a
more classical Arduino microcontroller. The ESP8266 runs a lot of
utility functions in the background – keeping WiFi connected, managing
the TCP/IP stack, and performing other duties. Blocking these
functions from running can cause the ESP8266 to crash and reset
itself. To avoid these mysterious resets, avoid long, blocking loops
in your sketch.
The amazing creators of the ESP8266 Arduino libraries also implemented
a yield() function, which calls on the background functions to allow
them to do their things.
这就是为什么您可以在包含 ESP8266 header 的主程序中调用 yield()
的原因。
更新:
yield()
在 Arduino.h 中定义为:
void yield(void);
yield()
也在hooks.h
中声明如下:
/**
* Empty yield() hook.
*
* This function is intended to be used by library writers to build
* libraries or sketches that supports cooperative threads.
*
* Its defined as a weak symbol and it can be redefined to implement a
* real cooperative scheduler.
*/
static void __empty() {
// Empty
}
void yield(void) __attribute__ ((weak, alias("__empty")));
所以,在 Nano
上,它可能什么都不做(除非你有其他库 #included
)。
yield 是 Arduino 内核中用于 AVR 的 "weak" 函数。我在 wiring.c 中看到一个要求。
void delay(unsigned long ms)
{
uint32_t start = micros();
while (ms > 0) {
yield();
while ( ms > 0 && (micros() - start) >= 1000) {
ms--;
start += 1000;
}
}
}
这意味着yield()函数将在delay函数的循环中执行。因此,当延迟结束时,yield 将用于一些后台处理,或者用于执行具有超时功能的功能。
注意:yield 必须定义在 application/sketch
更新:这个问题让我兴奋地想赚点钱 post about yield and other hidden features from arduino core。
Arduino 文档在 https://www.arduino.cc/en/Reference/Scheduler 处解释了 yield()
关于 Due 的问题。显然它是调度程序库的一部分:
#include <Scheduler.h>
但是,我可以在我的 Nano 或 ESP8266 上调用 yield()
而无需包含调度程序库——但只能在我的主程序中调用,而不是在包含文件中。此外,包含不适用于我的非会费。
关于 yield()
我想念的秘密是什么,或者- yield()
在 Arduino 平台上除了 Due 之外做了什么?
However, I can call yield() on my Nano or ESP8266 without including the Scheduler lib
yield()
函数也在ESP8266库中实现:
Yielding
This is one of the most critical differences between the ESP8266 and a more classical Arduino microcontroller. The ESP8266 runs a lot of utility functions in the background – keeping WiFi connected, managing the TCP/IP stack, and performing other duties. Blocking these functions from running can cause the ESP8266 to crash and reset itself. To avoid these mysterious resets, avoid long, blocking loops in your sketch.
The amazing creators of the ESP8266 Arduino libraries also implemented a yield() function, which calls on the background functions to allow them to do their things.
这就是为什么您可以在包含 ESP8266 header 的主程序中调用 yield()
的原因。
更新:
yield()
在 Arduino.h 中定义为:
void yield(void);
yield()
也在hooks.h
中声明如下:
/**
* Empty yield() hook.
*
* This function is intended to be used by library writers to build
* libraries or sketches that supports cooperative threads.
*
* Its defined as a weak symbol and it can be redefined to implement a
* real cooperative scheduler.
*/
static void __empty() {
// Empty
}
void yield(void) __attribute__ ((weak, alias("__empty")));
所以,在 Nano
上,它可能什么都不做(除非你有其他库 #included
)。
yield 是 Arduino 内核中用于 AVR 的 "weak" 函数。我在 wiring.c 中看到一个要求。
void delay(unsigned long ms)
{
uint32_t start = micros();
while (ms > 0) {
yield();
while ( ms > 0 && (micros() - start) >= 1000) {
ms--;
start += 1000;
}
}
}
这意味着yield()函数将在delay函数的循环中执行。因此,当延迟结束时,yield 将用于一些后台处理,或者用于执行具有超时功能的功能。
注意:yield 必须定义在 application/sketch
更新:这个问题让我兴奋地想赚点钱 post about yield and other hidden features from arduino core。