arduino` yield()`函数的秘密是什么?

问题描述 投票:13回答:3

Arduino文档解释了yield()关于Due的https://www.arduino.cc/en/Reference/Scheduler。显然它是Scheduler库的一部分:

#include <Scheduler.h>

但是,我可以在我的Nano或ESP8266上调用yield()而不包括Scheduler lib - 但仅限于我的主程序,而不是包含文件。此外,包含不适用于我的非会费。

关于yield()或者yield()在除Ar之外的Arduino平台上做了什么,我有什么秘密?

arduino esp8266 arduino-ide
3个回答
26
投票

但是,我可以在Nano或ESP8266上调用yield()而不包括Scheduler lib

yield()函数也在ESP8266库中实现:

生产

这是ESP8266与更经典的Arduino微控制器之间最重要的差异之一。 ESP8266在后台运行许多实用功能 - 保持WiFi连接,管理TCP / IP堆栈以及执行其他任务。阻止这些功能运行可能导致ESP8266崩溃并自行重置。为了避免这些神秘的重置,请避免在草图中使用长的阻塞循环。

ESP8266 Arduino库的惊人创建者还实现了yield()函数,该函数调用后台函数以允许它们执行其操作。

这就是为什么你可以在包含ESP8266标题的主程序中调用yield()的原因。

ESP8266 Thing Hookup Guide

更新:

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)。


1
投票

对于AVR,产量是来自Arduino核心的“弱”函数。我在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()函数将在延迟函数循环期间执行。因此,在延迟结束时或者用于执行具有超时功能的功能时,产量将用于一些后台处理。

注意:必须在application / sketch中定义yield

更新:这个问题让我兴奋地做一点post about yield and other hidden features from arduino core


0
投票

Detailed Explanation about yield()我在ESP8266中找到了关于使用yield()的非常详细的解释。据我所知,ESP8266需要定期运行Wifi堆栈,否则您的ESP将从您的路由器中退出。因此抛出yield()函数将使您的Wifi堆栈保持运行。

© www.soinside.com 2019 - 2024. All rights reserved.