Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

The Arduino docs explain yield() at https://www.arduino.cc/en/Reference/Scheduler with regards to the Due. Apparently it is part of the Scheduler library:

#include <Scheduler.h> 

However, I can call yield() on my Nano or ESP8266 without including the Scheduler lib -- but only in my main program, not inside include files. Also, the include does not work on my non-Dues.

What's the secret that I'm missing about yield() or- what does yield() do on Arduino platforms other than Due?

like image 919
andig Avatar asked Dec 28 '15 17:12

andig


People also ask

What does yield () do in Arduino?

Scheduler - yield()Passes control to other tasks when called. Ideally yield() should be used in functions that will take awhile to complete.

What is yield in ESP8266?

The ESP8266's delay() funciton, while of course delaying for a set number of milliseconds, also makes a quick call to the background functions. 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 thing.


2 Answers

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

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

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.

That's why you can call yield() from within your main program where the ESP8266 header is included.

See ESP8266 Thing Hookup Guide.

Update:

yield() is defined in Arduino.h as:

void yield(void); 

yield() is 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 the Nano, it probably does nothing (unless you have other libraries #included).

like image 104
Danny_ds Avatar answered Nov 24 '22 05:11

Danny_ds


yield is a "weak" function from 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 be executed during the loop of delay function. Thus, yield would be used for some background processing while the delay ends or for doing a function with timeout feature.

Note: yield must be defined in application/sketch

UPDATE: The question made me excited to make a little post about yield and other hidden features from arduino core.

like image 37
Yahya Tawil Avatar answered Nov 24 '22 04:11

Yahya Tawil