What is the secret of the `yield ()` arduino function?

Arduino docs explain yield() https://www.arduino.cc/en/Reference/Scheduler regarding the due date. This seems to be part of the scheduler library:

 #include <Scheduler.h> 

However, I can call yield() on my Nano or ESP8266 without including the scheduler library - but only in my main program, and not inside the include files. Also, inclusion does not work on my non-fees.

What is the secret I miss in yield() or- what does yield() on Arduino platforms other than Due?

+12
source share
3 answers

However, I can call yield () on my Nano or ESP8266 without enabling the lib scheduler

The yield() function is also implemented inside the ESP8266 libraries:

Yielding

This is one of the most important differences between the ESP8266 and the more classic Arduino microcontroller. The ESP8266 launches many utility functions in the background - maintaining WiFi, managing the TCP / IP stack, and other responsibilities. Blocking these functions from starting can cause the ESP8266 to crash and reset itself. To avoid these mysterious discharges, avoid the long, blocking loops in your sketch.

The amazing creators of the Arduino ESP8266 libraries also have a yield () function that requires background functions to let them do their thing.

This is why you can call yield() from your main program, which includes the ESP8266 header.

See ESP8266 Things Setup Guide .

Update

yield() is defined in Arduino.h as:

 void yield(void); 

yield() also declared in hooks.h as follows:

 /** * 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"))); 

So on Nano it probably does nothing (unless you have other #included libraries).

+25
source

the output is a "weak" function from the Arduino core for AVR. I see one call for it inside wiring.c.

 void delay(unsigned long ms) { uint32_t start = micros(); while (ms > 0) { yield(); while ( ms > 0 && (micros() - start) >= 1000) { ms--; start += 1000; } } } 

This means that the yield () function will execute during the delay function loop. Thus, the output will be used for some background processing until the delay ends or to perform a function with a timeout function.

Note: the output must be defined in the application / sketch

UPDATE: the question made me excitedly make a small post about the crop and other hidden functions from the arduino core .

0
source

Detailed explanation of yield () I found this very detailed explanation of using yield () in ESP8266. From what I know, ESP8266 needs to run Wifi Stack periodically, otherwise your ESP will drop out of your router. So with the yield () function, your Wifi stack will work.

0
source

All Articles