(Edit)The Fibers class has become a C++ template class(/Edit)
For those who are looking for coroutine support without the use of an RTOS, I have made the initial version of the Fibers library in my git repository at:
git@github.com:ve3wwg/teensy3_lib.git
See subdirectory teensy3/fibers.
This is a C++ Fibers template class library for use on the Teensy-3.x. This will only appeal to the non-Arduino IDE users unfortunately, since it is tricky to build into that toolchain (but can be done if someone wants to tackle it).
Additionally, this library requires a symbol to be added to the mk20dx256*.ld loader script. This is necessary so that it can optionally determine how much stack a given fiber (or main routine) uses. Look for instructions at the end of the file fibers.hpp.
Library Notes:
In your main program, or suitable place:
By default, the template class Fibers declares room for 15 fibers + main (16). To explicitly declare this use:
If you want a non-default main stack size, use:
Create a new Fiber
From any fiber:
Runs foo as a coroutine:
Yield
To yield control to the next fiber in round-robin fashion, from main fiber or a coroutine, simply:
Determining Stack Size
To determine the stack size(s), instrument the Fibers class first:
Allow your fibers to run for a while and at some point do:
It is not strictly necessary to create a fiber, if you just want to determine the main fiber's stack usage. In this case, just:
After main has run for a while, or prior to exit:
Testing
April 29/2014: This library has been successfully tested on a Teensy-3.1, with the following caveat: Do not allow your fiber/coroutine to exit. This is an area that needs work.
For those who are looking for coroutine support without the use of an RTOS, I have made the initial version of the Fibers library in my git repository at:
git@github.com:ve3wwg/teensy3_lib.git
See subdirectory teensy3/fibers.
This is a C++ Fibers template class library for use on the Teensy-3.x. This will only appeal to the non-Arduino IDE users unfortunately, since it is tricky to build into that toolchain (but can be done if someone wants to tackle it).
Additionally, this library requires a symbol to be added to the mk20dx256*.ld loader script. This is necessary so that it can optionally determine how much stack a given fiber (or main routine) uses. Look for instructions at the end of the file fibers.hpp.
Library Notes:
In your main program, or suitable place:
Code:
#include <fibers.hpp>
Fibers<> fibers; // Make global to allow other modules to invoke fibers.yield()
By default, the template class Fibers declares room for 15 fibers + main (16). To explicitly declare this use:
Code:
Fibers<[B]16[/B]> fibers;
If you want a non-default main stack size, use:
Code:
Fibers<> fibers([B]2000[/B]); // Use 2000 bytes for main fiber stack (bytes)
Create a new Fiber
From any fiber:
Code:
fibers.create(foo,foo_arg,3000); // Returns a fiber ID as unsigned
Runs foo as a coroutine:
Code:
void foo(void *arg) {
...
}
Yield
To yield control to the next fiber in round-robin fashion, from main fiber or a coroutine, simply:
Code:
fibers.yield();
Determining Stack Size
To determine the stack size(s), instrument the Fibers class first:
Code:
Fibers<> fibers(2000,[B]true[/B]); // true = "instrument" enable
Allow your fibers to run for a while and at some point do:
Code:
bytes[0] = fibers.stack_size(0); // Approx main stack bytes used
bytes[1] = fibers.stack_size(1); // Approx fiber #1 stack bytes used
...etc...
It is not strictly necessary to create a fiber, if you just want to determine the main fiber's stack usage. In this case, just:
Code:
Fibers<> fibers(main_stack_size,[B]true[/B]); // Instantiate with "instrumentation" enabled
After main has run for a while, or prior to exit:
Code:
main_stack_bytes = fibers.stack_size(0);
Testing
April 29/2014: This library has been successfully tested on a Teensy-3.1, with the following caveat: Do not allow your fiber/coroutine to exit. This is an area that needs work.
Last edited: