Upcoming maintenance
Dear Customers and Partners.
This website will be undergoing scheduled maintenance on June 14, 2023. Please be aware there may be disruption to the developer portal website and associated services during the scheduled maintenance period.
This upgrade is essential to ensure the continued performance, reliability, and security of Developer World.
We apologize for any inconvenience.
Multithreading with Arduino IDE
-
Hello everyone,
I am trying to write a program using multithreading on the Arduino IDE.
I have successfully run the example in the following tutorial:
https://developer.sony.com/develop/spresense/docs/arduino_set_up_en.html?fbclid=IwAR2K82Q1_7XzTIg-8gxxddYsXKGGMpAAzAEArnSPJ7Zn0Mqnuyjexeh60-QAnd I have also installed the Spresense SDK by following this instruction, since I read that this SDK provides Multitasking RTOS.
https://developer.sony.com/develop/spresense/docs/sdk_set_up_ide_en.html#_creating_an_application_projectCan you give me advice on how to apply the Spresense SDK in Arduino IDE to use Multitasking RTOS.
If Arduino IDE is not usable, is there any instruction or example on using Spresense SDK for Multitasking RTOS?
And one more thing, when I build an application using "Application build" or "Build and Flash" it seems to build the whole SDK every time I make changes to the code, and it takes a long time to rebuild, is there any way to improve this?
Thank you very much for your help.
Best regards, -
Hi @tuan-nguyen
I would like to confirm whether you would like to use multithreading on one core or would you like to use multicore?
Could you also tell me what tutorial you have followed? The first link does not take me anywhere.
Best Regards,
Kamil Tomaszewski -
Thank you for the reply. I would like to use multithreading on multicore.
About the tutorial in the first link, it was about writing a program using Arduino IDE to blink the 4 LEDs on the board itself, it was just to verify the setup was correct.
Best Regards,
Tuan Nguyen -
Hi @tuan-nguyen
You've probably followed this tutorial: https://developer.sony.com/develop/spresense/docs/arduino_tutorials_en.html#_tutorial_multicore
NuttX is also running on the sub cores. This allows you to use NuttX functions like
task_create ()
.Below is the code that controls the led in another task on sub core 1:
#if (SUBCORE != 1) #error "Core selection is wrong!!" #endif #include <MP.h> #include <time.h> static uint64_t get_time() { struct timespec tp; clock_gettime(CLOCK_REALTIME, &tp); return (((uint64_t)tp.tv_sec) * 1000 + tp.tv_nsec / 1000000); } static int led_thread(int argc, FAR char *argv[]) { bool value = false; uint64_t t_time; uint64_t cur_time; t_time = get_time(); while (1) { cur_time = get_time(); if (cur_time >= t_time + 1000) { value = !value; digitalWrite(LED0, value); t_time = cur_time; } } return 0; } void setup() { MP.begin(); task_create("led_thread0", 100, 2048, led_thread, NULL); } void loop() { delay(1000); MPLog("loop\n"); }
If you are using Arduino IDE then you don't need to install Spresense SDK. The Spresense SDK is already included with the Spresense Arduino libraries as an exported SDK.
I hope this answers your questions.
Best Regards,
Kamil Tomaszewski -
Thank you so much for your supports.
I'll be sure to follow your instructions carefully.Best Regards,
Tuan Nguyen -
I have followed your instructions and successfully run multithreading on multicore. However, I need to run a great number of multithreaded, which is more than the number of 5 subcores, therefore, I would like to use multithreading on one core as well.
Could you give me some advice?
Best Regards,
Tuan Nguyen -
Hi @tuan-nguyen
You can create a new task as I showed before. If you want more then you can create more as shown below:
task_create("led_thread0", 100, 2048, led_thread0, NULL); task_create("led_thread1", 100, 2048, led_thread1, NULL); task_create("led_thread2", 100, 2048, led_thread2, NULL); ...
Best Regards,
Kamil Tomaszewski -