Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multithreading support with xTaskCreate #228

Closed
marvindinneweth opened this issue Feb 23, 2017 · 20 comments
Closed

Multithreading support with xTaskCreate #228

marvindinneweth opened this issue Feb 23, 2017 · 20 comments
Assignees
Labels
Type: For reference Common questions & problems

Comments

@marvindinneweth
Copy link

I want my wifi connection to listen in a separate thread? How can i do this?

@copercini
Copy link
Contributor

copercini commented Feb 23, 2017

@me-no-dev
Copy link
Member

what do you mean by "listen on another thread"?

@marvindinneweth
Copy link
Author

I want to have a different thread where a little rest api is running and responds to get requests. This intirely separated from the main loop.

@whatnick
Copy link
Contributor

whatnick commented Mar 17, 2017

You might be better served using the RTOS port for the ESP32 which has a clear concept of threads, rather than abstracted like Arduino. https://github.com/espressif/ESP31_RTOS_SDK

@igrr
Copy link
Member

igrr commented Mar 17, 2017

@whatnick what makes you think that this Arduino core is bare metal? It runs on top of FreeRTOS.

@whatnick
Copy link
Contributor

I meant as threads explicitly exposed, rather than abstracted away in Arduino land.

@me-no-dev
Copy link
Member

@whatnick nothing is abstracted ;) Arduino runs in one thread and you are free to spin another one or more if you need to. API is exactly the same... as @igrr said, Arduino on ESP32 runs on FreeRTOS

@whatnick
Copy link
Contributor

What would be the concrete answer to the above ? A threaded webserver sample ?

@me-no-dev
Copy link
Member

@whatnick so you want an example? I'm not quite sure what the issue is here?
Again, Arduino runs on top of RTOS and the specific RTOS is: https://github.com/espressif/esp-idf

@whatnick
Copy link
Contributor

whatnick commented Mar 18, 2017 via email

@forthlightning
Copy link

Not sure about threading.h but freertos has tasks which probably satisfy what you want to do.

I'm on freertos.org all day, here is the task API call that esp-idf uses: http://www.freertos.org/a00125.html

A task has to be of the form

void do_thing_task( void* p ) // gotta have a void pointer as parameter even if unused
{
while( true ) // gotta have infinite loop
    {
        // do things
        vTaskDelay( 1000 / portTICK_PERIOD_MS ) // wait / yield time to other tasks
    }
}

Then you start it with

xTaskCreate( 
&do_thing_task,        // function
"do_thing_task",       // name
 2048,                 // stack
NULL,                  // void* to input parameter
 10,                   // priority 
NULL                   // task handle 
);

@baldram
Copy link

baldram commented Jul 1, 2017

This is a really interesting topic. To clarify my doubts I would like to ask by showing example.

Since arduino-esp32 is built on top of FreeRTOS, does it mean it is possible to call xTaskCreate directly from *.ino code? Is there a sufficient API for doing that?

It would look somehow like this example then:
https://github.com/feilipu/Arduino_FreeRTOS_Library/blob/master/examples/Blink_AnalogRead/Blink_AnalogRead.ino

Is it possible with arduino-esp32 ?

@hakuren
Copy link

hakuren commented Jul 1, 2017

Check out this example by @copercini
https://github.com/copercini/esp32-iot-examples/blob/master/multiloop/multiloop.ino

@lonerzzz lonerzzz added the Type: For reference Common questions & problems label Aug 25, 2017
@lonerzzz lonerzzz changed the title Multithreading? Multithreading support with xTaskCreate? Aug 25, 2017
@lonerzzz lonerzzz changed the title Multithreading support with xTaskCreate? Multithreading support with xTaskCreate Aug 25, 2017
@copercini
Copy link
Contributor

You can use FreeRTOS APIs without include anything in the arduino sketch, but run directly in IDF is really more optimized

@Humancell
Copy link

As this is a thread "for reference" I wanted to ask a few questions, and add the answers to this thread for later documentation.

  1. If I use xTaskCreate to create a task, will FreeRTOS automatically load balance these tasks across the two cores? I do know that xTaskCreatePinnedToCore can be used to pin the task to a specific core, but if I am simple going to schedule numerous tasks, how does xTaskCreate determine which core the task will run on? I have read the documentation here, and it does not seem to specify how core selection is done in this xTaskCreate case? http://www.freertos.org/a00125.html

  2. It appears that I can use xTaskCreate or xTaskCreatePinnedToCore to create "run once" tasks, if I simple do NOT provide a loop within the task. Is this correct? I could then potentially have a loop that is using a timer or other input event to create a set of tasks that run once, and then terminate. Later events could then recreate/reschedule these tasks to run again. Correct?

  3. Is there a way to determine how "busy" either core is, to measure and determine where I want to run tasks?

@me-no-dev
Copy link
Member

you can read more here: http://esp-idf.readthedocs.io/en/latest/api-guides/freertos-smp.html
the only way core0 to be less loaded is if you are running without BLE and WiFi, in which case many threads that usually run there will not be started

@Humancell
Copy link

Hello,

Thank you VERY much for this reference ... great information! Per what I have read, it appears that the following are the answers to my questions:

  1. "Note that the vanilla FreeRTOS functions xTaskCreate and xTaskCreateStatic have been macro defined in ESP-IDF FreeRTOS to call xTaskCreatePinnedToCore() and xTaskCreateStaticPinnedToCore() respectively with tskNO_AFFINITY as the xCoreID value." - This appears to mean that the task will be added to the Task List with xCoreID set to tskNO_AFFINITY, and that appears to mean that the scheduler run from either core (PRO_CORE) or (APP_CORE) would match that task and run it. I'm thinking of how I can write some code to verify this is the case.

  2. Reading further here ( http://www.freertos.org/implementing-a-FreeRTOS-task.html ) it does state: "Tasks must not attempt to return from their implementing function or otherwise exit. In newer FreeRTOS port attempting to do so will result in an configASSERT() being called if it is defined. If it is necessary for a task to exit then have the task call vTaskDelete( NULL ) to ensure its exit is clean." This appears to mean it is better to utilize Queues, Semaphores, or Mutexes to control the execution of tasks, rather than attempting to schedule and reschedule them.

  3. I did find a few more references to ways to gather task and CPU utilization information. I'll again have to write some test code and see what I get, but these seemed to have touched on the areas that I'm interested in. http://www.freertos.org/a00021.html#vTaskGetRunTimeStats and http://www.freertos.org/rtos-run-time-stats.html

Thanks again!

@lonerzzz
Copy link
Contributor

lonerzzz commented Nov 6, 2017

@Humancell

I have found it fairly straightforward to use task control tools as you mentioned to manage the tasks as opposed to playing with scheduling at run time.

As for gathering task information, only some are available based on the FreeRTOS configuration file in IDF so be prepared for the usage pf some to result in errors. I use those for checking the task heap and whether tasks are blocked primarily.

@VojtechBartoska
Copy link
Collaborator

@PilnyTomas I think this issue can be closed as it was mainly covered by your PR above.

@PilnyTomas
Copy link
Contributor

@PilnyTomas I think this issue can be closed as it was mainly covered by your PR above.

I agree :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: For reference Common questions & problems
Projects
None yet
Development

No branches or pull requests