a Cooperative Scheduler Arduino style

XCore Project reviews, ideas, videos and proposals.
Posts: 19
Joined: Mon Jan 08, 2018 4:14 pm

a Cooperative Scheduler Arduino style

Postby fabriceo » Mon Jun 10, 2019 8:33 pm


I’ve developed the SCoop library for the Arduino platform some years ago here:
and I was missing an easy Cooperative scheduler to execute multiple cpp tasks on XMOS.
The intend is for me to manage some sort of front-panel and I/O within the USB application, in an Arduino style.

Here is a xc demo application using XCScheduler.cpp library file providing a Cooperative scheduler solution, in addition and compatible with the XCore multitasking inherent capability of the XMOS architecture; it uses Setjump/longjump and is 80% based on the original code ad smart approach of from Mikael Patel here: https://github.com/mikaelpatel/Arduino-Scheduler
All credits to him for this very smart approach.

A main.xc program declares 2 tasks in a par statement for 2 cores.
One core is just printing a message every 5 seconds using a blocking delay function;
The other core is
- initializing the XCScheduler with a certain amount of stack space for all the cpp tasks
- launching the initialization of multiple tasks written in cpp
- allocating a specific stack space for each of them
- orchestrating and monitoring a simple context switch between the task with a round robin approach.
- This is done as a default branch of a select statement.
Each cpp task is blocking and just yielding to the other tasks when cpu is not needed.
A sleepMs function provides a simple delay(ms) with embedded yield call.

A stronger and clean library will most probably be provided on my github in a next post here but wanted to share this interesting outcome as of now, hope it worth your reading.

- A #pragma stackfunction is needed and the amount of memory reserved should be greater than the sum of all stack task (2500 words in this demo)
- With this implementation spread between xc and cpp, there is no any linker error due to stack information missing or due to name-mangling. All seems ok 
- The xcore task1 can just yield or execute a loop() in the cpp code, depending if we want a circularly or sequential approach. The second one gives the possibility to measure the time of a complete circularly cycle, which is of 2us in this example. Any firm blocking timer within a task will increase the time for a cycle.
- the demo uses only printf to the stdio rerouted with xscope so need a xtag

Don’t forget to change the platform .xn file to suit your dev board. This app is running on a DXIO board bought at diyinhk.com
You do not have the required permissions to view the files attached to this post.
Posts: 19
Joined: Mon Jan 08, 2018 4:14 pm

Postby fabriceo » Tue Oct 08, 2019 11:21 am

there was 19 downloads as of october 8th, any feedback or suggestions ?

Who is online

Users browsing this forum: No registered users and 0 guests