Page 1 of 1

ESP32 Multitasking

Posted: Sun Nov 21, 2021 11:05 am
by mnfisher
The ESP32 runs Free RTOS - this is a 'multitasking' operating system.

This can be used from within Flowcode to allow timed or multiple threads to execution to occur 'simultaneously' without any timing or intervention from the program. The 'threads' must 'yield' control to the OS to allow the multitasking to occur - this can easily be achieved by adding a delay. This allows multiple tasks to be handled very easily :-)

The relevant function to use is xTaskCreate (which needs to be used in a C block)

The arguments to this are:
Function (or task) (a Flowcode macro's address)
Task name
Stack size
Parameters
Priority
Task Handle

To get the address of a macro in FC we use &FCM_Name

A simple example.

This toggles 3 pins on and off (in this case at 1, 2.5 and 5 Hz) I used A18, A19 and A5 (Note that initially the pins I used 'crashed' with pins ... are input only although FC doesn't complain)

One pin is toggled in the main loop - the other 2 are toggled 'independently' by Flash_1 and Flash_2 -which are added as tasks. Note that stack size depends on the macro (1024 used here is probably overkill!)
RTOS 1.fcfx
(9.73 KiB) Downloaded 116 times
Martin

Re: ESP32 Multitasking

Posted: Sun Nov 21, 2021 10:44 pm
by mnfisher
A bit more playing - and passing arguments to a 'task' is easy.

The 'parameters' in xCreateTask should be a pointer to an array of data. The operator AddressOf (&) can take an array of data (&FCL_DAT in the example below) and by declaring an array as the macro parameters - we can easily access the data.
(As an aside - it should be okay to pass a variable amount of data - make the first array entry a 'size' value - then the task could process each entry before 'stopping' - see below)

Another simple example - here I pass an array of 3 numbers to Flash_1 which displays them on UART. Normally the parameters would be used as arguments for the task..

One thing to watch is that 'print' statements actually give the RTOS a 'swap' task opportunity - so printing in Tasks and the Main loop or in multiple tasks might give you 'mixed' text... So I would recommend only printing from 'main' normally..

Also - further reading - stack size is in words, so I assume this means stack entries (and in our example will be 4096 bytes)

The 'tasks' should not exit (so infinite loops here) - but can stop themselves using 'handle' (or be stopped from elsewhere) - my first attempt with this didn't compile though...
RTOS 2.fcfx
(13.65 KiB) Downloaded 107 times

Martin

Re: ESP32 Multitasking

Posted: Mon Nov 22, 2021 7:46 am
by mnfisher
A final bit of playing....

vTaskDelete(handle); is used (in a C block) to terminate a task.

In the task itself - use vTaskDelete(0);

If using in main in xTaskCreate pass &FCL_HANDLE as the final parameter - and then use
vTaskDelete(FCL_HANDLE);

Here the above example is modified to only run the 5Hz pin for 3s before stopping it... Also in Flash_1 is code to stop itself after 5s...
RTOS 2.fcfx
(14.21 KiB) Downloaded 111 times
Martin

Re: ESP32 Multitasking

Posted: Mon Dec 27, 2021 1:40 pm
by lucibel
Hi,
Very good!!!
I knew that ESP32 is a 3 cores at 240Mhz, I didn't know how to use them I will try to use it immediately!
It mean that 3 tasks maximum can be launched in same time (1 task main prog + 2 tasks with Xtaskcreate ?

Do you know how to use vTaskDelay() to pause the task 500ms?
vTaskDelay (xDelay)

Xdelay already defined to 500/portTICK_PERIOD_MS
How to get the portTICK_PERIOD_MS ?

Thx

Re: ESP32 Multitasking

Posted: Mon Dec 27, 2021 2:35 pm
by mnfisher
You can use vTaskDelay in a C block:

Code: Select all

vTaskDelay( 500 / portTICK_PERIOD_MS) ;
is okay. I'm not sure if there is an advantage to using FC's 'delay' macro.

I think multiple tasks can run on each core - each task must 'yield' to the OS before the WDT causes an interrupt. (Only one task per core runs at any time - the 'switching' between tasks is so fast that it appears to run multiple tasks..)

Martin

Re: ESP32 Multitasking

Posted: Mon Dec 27, 2021 4:04 pm
by lucibel
Ok I will try. thx