Это видео недоступно.
Сожалеем об этом.
#149
HTML-код
- Опубликовано: 13 июн 2019
- An absolute beginners' introduction to dual-core processing using the keenly-priced ESP32 processor boards from Espressif.
Happy Birthday to PCBWay.com who are celebrating their 5th birthday all this month (June 2019)!
They want to share some goodies with you so visit
www.pcbway.com... and see what might take your fancy.
PCB Prototype the Easy Way - Full feature custom PCB prototype service.
$5 for 10 PCBs: www.pcbway.com/
All this and more on my GitHub:
github.com/Ral...
Boards entry: File -- Preferences
then click right down the bottom of the page to Additional Boards Manager URLs, to add this line:
dl.espressif.c...
Wemos D1 Mini32 ESP32 as used in the demo.
Note, UK warehouse price same as China price but get it in two days!
bit.ly/2Wx5Cbd
An Arduino-sized ESP32 board (TTGO ESP32) with all the relevant pins marked up, I have one myself:
UK warehouse: bit.ly/2wKa5rI
CN warehouse: bit.ly/2K77Qrd
There are various TTGO ESP32 boards available, different formats, different prices, worth browsing before buying.
Other ESP32-related links:
Hardware design reference: public.robotic...
Getting started with the ESP32: docs.espressif...
Nothing to do with this video, but I bought a dual solder reel holder, works like a charm and very solid. I'll show you this in a future video on SMD soldering:
bit.ly/2ZdlgFy
All this and more on my GitHub:
github.com/Ral...
If you like this video please give it a thumbs up, share it and if you're not already subscribed please consider doing so and joining me on my Arduinite journey
My channel and blog are here:
------------------------------------------------------------------
/ ralphbacon
ralphbacon.blog
------------------------------------------------------------------
Very nice video on running multiple threads on the two cores of the ESP32, I had no idea how but, now I do. Thanks. Keep broadcasting!!
Thank you for your feedback, GeekMustHave, noted and appreciated!
Thanks Ralph. Great video, yes please do more.
Thank you for your feedback, Peter Heartfield, noted and appreciated!
Instead of doing delay(1) in your main Arduino loop, you can use taskYIELD() to let the scheduler know to go do other more important work (Ready tasks with a priority equal to or greater than your task). Because, as you mentioned, in Arduino the main loop is just called repeated from an outer method, the scheduler does not stop running that task unless we allow it (unless we are running the preemptive scheduler). So that will work for core 0, but not core 1. Now since the Arduino loop is running on core 1 with priority 1, calling yield will not work if your task is on core 1 with priority 0, as that is less than the current priority. So to make the task run on core 1, change the priority to 1 or greater. Then calling taksYIELD() will run the task on core 1. And this also takes me back to why delay(1) works. Delay is going to pause the current task (Arduino loop) and yield control to the scheduler. This will put the Arduino loop task into a blocked state (ie not Ready), so the scheduler will then run lower priority tasks (ie your core 1 priority 0 task). Hope that helps explain what you were seeing...
I hate showing my stupidity so early in the game, but wtf? It sound very informative, but I have no idea what you're talking about.
+vonries
A real time operating system splits work into several tasks which share cpu time. Tasks can be in one of several states (blocked, ready to run, running). The system has a timer and the scheduler gets control each timer tick. It suspends the current task and switches to the highest priority task in ready state.
The esp32 has two cores: protocol (#0) and application (#1). It runs a modified version of free RTOS. Each core has its own scheduler and the two cores are NOT synchronised. For example disabling interrupts on one will NOT disable interrupts on the other and does NOT protect data from corruption.
Communicating across cores needs expert knowledge - lots of traps for young players!
There is some good info on all this at docs.espressif.com
Ps: the 8266 also has RTOS not just the esp32
@@mikelopez9893 thanks, very cool.
Since doing this video, Toby, I've been looking into this a bit. Yes, a typical British understatement if ever you heard one!
*taskYIELD()* doesn't give the expected results, I'm afraid, as neither does *vPortYield()* which is what taskYIELD() expands to. Whilst we're on the subject, *yield()* doesn't do it either as that just calls vPortYield().
What I've discovered is that if we _really_ don't want to use the standard loop() then just execute a *vTaskDelete(NULL)* to get rid of it once and for all. Everything else just keeps running as expected.
However, I've begun to use loop() as a "master" controller in some of my sketches that can control things (eg suspend/resume tasks, very useful) and keep a watching brief on what is happening. Task orchestration, if you will, which reminds me strongly of an event listener framework I developed for work using JavaScript. What goes round comes around...
More of this and other stuff in a future video!
Thanks for your input, Toby, you have a lot of experience in this area and your continued feedback is greatly appreciated. Nice to hear from you.
Ralph S Bacon
I think the issue is just priorities. TLDR - use priority 1. Here’s the espressif scheme:
Priorities 10 to 14 are reserved for the system. For example, 14 is the watchdog timer, 10 is the TCP/IP stack.
Priorities 1 to 9 are for general use. For example, WiFi events use priority 2.
Priority 0 is the idle task.
There are three ways a time slot can end. 1) it can voluntarily yield and the state goes to ready. 2) it can block waiting for something to happen - the state goes to blocked. 3) The time is used up - the state goes to ready.
At the end of the time slot the scheduler takes the highest priority task in ready state and runs it. If several ready tasks have the same priority they are allocated “round robin” (but see caveat below)
Caveat: the above is correct for the esp8266, but the esp32 does not have a complete multiprocessor implementation of free RTOS and can not guarantee equal round robin treatment across cores.
I am concerned that you used priority zero which is reserved for idle. Essentially you are telling the system “nothing important to run here”.
Hope this helps.
very usable.
in a very simple way
how exactly the two-core system works in esp32.
It is a very interesting field for applications of esp32 and would like if you can continue on this field.
And continue I will, George! Stay tuned!
Great stuff, and yes please, more like this! Lots blogs etc about esp blink but not so much dipping your toe deeper into the ESP32
Thank you for your feedback, Boat data systems, noted and appreciated!
I've never (knowingly) tried this for processes running on different cores, but you might be able to use a variable of portMUX_TYPE to handle synchronization between the cores. Any variables you plan to change between cores might need to be declared volatile and then you add some wrappers around the variables to you want to modify to keep them from being modified by various processes at the same time.
// Declared globally
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
// In function to modify shared variables
portENTER_CRITICAL(&mux);
// modify/update variable here
portEXIT_CRITICAL(&mux);
This works great for interrupt handlers and might work with multiprocessor task as well.
Hi, speaking of ESP32's an Australian RUclipsr who has a great channel called "The Unexpected Maker" has spent about a year designing a product called "TinyPico" Its as the name suggests a very small ESP32 development board, and I and many others have been watching the highs and lows of his design and thanks to crowd supply (and enormous effort on his part) is being shipped, I highly recommend that you have a look at his "TinyPico" as it really is feature packed, plenty of IO and he has a fully matched 3D antenna on there for outstanding WIFI performance. Take a look at his channel lots of interesting stuff....especially for ESP32's!
And look I did, Andy, very interesting. I shall read (and watch) some of his videos when time permits.
For others, here's the link: unexpectedmaker.com/tinypico
Thank you Ralph! o) I'm new to the microcontroller space and I really like your videos. The way you talk and present, it's very pleasant to watch and learn. I wouldn't mind more coding related videos. I am a software dev myself, but I have issues understanding on how to throw all the things together in loops/interrupts (say you want wifi, mqtt, config via webserver, read multiple sensors and drive a display with menu at the same time). Doing this on a microcontroller is quite different to desktop development, I surely have a lot to learn, thank you for getting me started. o)
And I'm glad to have you along, ytbone! If you're a dev then programming the Arduino and similar µControllers is not going to be difficult. In fact, the ESP32, with its operating system, feels like home to me! Keep tuned for further videos...
Very interesting Video ! Please more videos about the ESP32 !
Thank you for your feedback, Thomas, noted and appreciated!
Ralph, thank you for the time and effort you put into this as I really appreciate it. I always seem to learn something.
Thanks for that, heyok, nice to hear from you.
Great video as usual. I was thinking about getting an esp32 now I will thanks :)
Thank you for your feedback, Michael Hyde, noted and appreciated!
Oh yes, ESP32 is a great great platform ! We would all love to see inter-task communication ... passing arguments and data.
Yes, that will be "interesting", John. Keep tuned. Thanks for posting.
Great video for getting started with esp32. More please ;)
Thanks, will do!
Ralph another awesome video. I have seen other ESP32 videos but yours is the BEST! You did things I have never seen before. AWESOME!!!!!!
Wow! I'm glad you enjoyed the video, Bruce. It's a subject that is interesting for quite a few people. I shall have to do at least one further video.
I've been subscribing to your channel for a few weeks now Ralph. Excellent work and at a pace I can cope with. Brilliant. Just bought a couple of these ESP32s and started with your RTC sketch. Now can't wait for next episode re sharing variables between cores. Yes, more on the ESP32 please !!!
Thank you for your feedback, Paul, noted and appreciated!
Thank you, Ralph. Not only the contents but also the effects of your video are excellent. Please continue.
Thanks, will do! Well, once my house move has completed and I have a new workshop!
Really good stuff, Ralph! It is nice to go a bit under the hood of devices we often use without knowing what they are really capable of.
You are most welcome John, I'm glad you like the video. Nice to hear from you.
At work and yet to see. But boy, I’ve been waiting for this one. Well done Ralph😁🏴
I hope your expectations were met, Dean, and stay tuned for further videos.
Nice Video, thank you.
I would be happy if you do another video an go more into detail. - like how to get variables from on core to the other.
Thank you for your feedback, Tom Watzek, noted and appreciated! Variables from one _task_ to another (the core is irrelevant) is the subject of my next video, Tom. Oh, perhaps I should not have said that.. spoiler alert! Too late!
Oh my, thank you so much!
I work with FreeRTOS all the time on the professional side, and have some ESP-32 boards for experimenting and making new things at home.
This gives me a whole 'nother level of being able to mix the two "under the hood", and has opened up a whole new bunch of ideas on how to meld the two.
With your RTOS experience you should be quite at home with the ESP32 then, Rob. The Arduino-style implementation doesn't expose all RTOS functions but if you read the Espressif web pages they tell you what to edit in their implementation header files to add functions. You might even prefer the ESP-IDF development environment.
docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html
Fun times ahead!
Another worthwhile video. Count me in for more esp32 vids. As far as the speed difference ... that's just mind boggling.
Yes, Joey, it took me by surprise, actually. I wondered what I had done wrong! I even include some debugging statements in the code and it whizzed through so quickly it was amazing. Amazing chip.
Very much enjoy this and ALL your videos Ralph... would like to see more!! Thanks!!!!
Thank you for your feedback, Max, noted and appreciated!
Fantastic! Ralph. I think this should be the second microcontroller board newbies get, the Delay fuction is so evil to beginners, and can hold them back.
I would really like to see more of this board in the Arduino IDE, using Bluetooth fuction and passing information between the two cores. Thanks again.
Thank you for your feedback, gordon, noted and appreciated!
Ralph: Thank you so much for your videos! I bought my ESP32 when they 1st come out but at the time there was limited support. Now I feel confident I can load the board definitions into my IDE and press forward.
Quite right, Nick, when I did my first video on the ESP32, I remember them being a bit temperamental. And support was hit and miss. Now though, 55 videos later, I too feel that they have come of age. Keep tuned for further videos...
HI Ralph, very interesting video. I have just ordered a couple of boards. Please keep this type of video coming, The more the merrier.
Thank you for your feedback, Trevor30926, noted and appreciated!
Hi Ralph, I really enjoy watching your videos, so thank you for sharing.
The reason that putting nothing in loop() would result in your program doing nothing is because a "function" is just a location in memory which happens to contain instructions. Those instructions are generated by the compiler. If your function doesn't have any instructions, it would be effectively just a "jump" and "return", or possibly a "call" instruction (in fancier instruction sets).
Basically, because the function doesn't have any content, when it generates the binary/executible, since it is optimizing for space, it most likely doesn't include that function, or won't provide an actual definition for it (different compilers and different optimization profiles will do different things, so I can't say precicely what it will do).
Keep up the explorations, like I said, I really enjoy them.
Yes, it's unclear whether the compiler would optimise an empty loop away (you would hope it would) and if so the task handler could not start the expected (default) loop task. That might crash the chip I suppose. I've just deleted the task in other sketches if I don't want the default loop(). Thanks for posting, Robert, good to hear from you.
Depending on how obsessive you want to be, you can take a look at the ELF binary that gets generated, and see exactly what's getting created. It's not something I really enjoy doing, but it is usually enlightening. A very quick way to verify without looking at the binary itself would be to look at the size of the resulting binary with and without the delay added to loop(). If there's a massive size difference, you can be reasonably sure that the compiler is simply optimizing away most of the program.
My thought would be that if loop() didn't contain any instructions, not only would the compiler remove the "call loop()" instruction, but it in theory should (being that it's C) optimize away most of even the basic wrapper program. Perhaps an example might illustrate: Suppose the "wrapper" program looks like:
void main() {
setup();
while(1) {
loop();
}
return;
}
if loop() is empty, that goes away, and the compiler is left with an additional empty while(1) loop. This also should get optimized away, and thus main() would become:
void main() {
setup()
return;
}
resulting in the board not crashing per-se, it just essentially finishes the entirety of the task.
anyway, I'm just some dude on the internet so feel absolutely free to take with a grain of salt.
I'm thinking that the ESP32 (RTOS) would start the task handler for specific tasks (one of them being loop) but if the loop did not exist it may do weird and wonderful things. Eventually we will find out, I'm sure.
What a great video, looking forward to more ESP32 videos, area of interest are around maximising power saving capabilities, this unit is great, Perhaps looking at the ESP-PICO-D4 as well..
Thanks for that, ROBIN GILES, nice to hear from you.
Still enjoying these videos Ralph! Keep up the great work, good sir! 👍🏻
Glad you like them!
Just got myself a few esp32's so I will be revisiting this
Yes, indeed, Ed, it's quite interesting what you can do with them.
Ralph, AWESOME video. The ESP32 is the processor to use today. how about a video on the sharing of variables between the two processors, going both ways.
Hmm, I wonder what could be the topic of my next-but-one video, Michael, I wonder... Nope, can't figure it out. We shall just have to wait and see.
Excellent again, looking forward to more ESP32 videos
Thank you for your feedback, David, noted and appreciated!
I just found your channel, subscribed! Great video, I've been using esp32 for quite a while now and learned some new stuff. I like the teaching style too. I love these kinds of things that add to my knowledge! Bravo.
Thanks for that, BiggsWorld, nice to hear from you. Thanks for subscribing too!
I've programmed a bit in other languages pre-IoT and am transitioning to the world you reflect here. I really like your clear and thoughtful approach to explaining things. I know (perhaps) just enough to see that you are using functions with parameters that pass information. May I suggest that when you use a function (like xTaskCreatePinnedToCore), you include a link to it's library that lists all of the functions, parameters, and what they do? That way, advanced beginners like me can more quickly build on what you teach without trying to find the libraries you are already using.
I've used Adafruit tutorials that show functions with links to (what looks to me to be) partial libraries or incomplete explanations, which limit their functionality.
Many thanks!
Point taken, Michael, although in this case it's not so much a library as an entire operating system Free RTOS. It's been modified, somehow, by Espressif for this chip (not sure of the differences) but RTOS is well documented and there's a downloadable Reference Manual pdf too: www.freertos.org/Documentation/RTOS_book.html I hope this helps!
@@RalphBacon Thank you very much, it does. Please keep doing that so that people can look up the full list of functions.
So many details. Thank you, Ralph! Subbed, looking to more content on MC's and FPGA's.
Thank you for your feedback, NLab, noted and appreciated!
Today i learned a lot; ESP32 has 2 cores and we can use it! Thanks Ralph
We can use two cores with care, André, as you will hear in my next video. Multitasking is a great liberator and at the same time a minefield. Keep watching!
This tutorial is excelent!! Thank you very much for the effort you put into the video
You're very welcome!
Excellent Tutorial Ralph. Enjoying it
Glad you enjoyed it!
Interesting video, I have played a bit with the ESP32, but have not studied it and did not know it had a RTOS.
With a RTOS it means you could run both your task on the same processor, you do not need to distribute it on two processors to run them in parallel.
One big advantage for ESP is 32 bit architecture, it means anything but byte variables will be faster on it by more than the clock speed. The speed increase in your test was insane, there must be many factors to get that much.
One detail I did find out about the ESP32 is that programs are stored in a serial memory and it will use a serial protocol to read the program. This reading is fast, but much much slower than the processor, it uses a internal cache to prevent much delay while running the program.
Shh, don't spoil my next video on parallel task, Henrik! Yes, of course you can run multiple tasks on a single core, and as RTOS is running WiFi and BT tasks (among others) on Core 0 it's probably wise to treat core 0 with serious respect (memories of watchdog resets come to mind when I was coding my ESP8266 deice).
You are correct about busses of any kind slowing down the processor - caching helps of course, but there's a limit and my first video #95 of the ESP32 driving a MAX7219 display was _very_ slow (caused by the stupidly slow I2C - or was it SPI? - bus setting).
Anyway, great to hear from you, more in a future video!
I am looking forward to more videos about ESP32, a good youtube video is a fast way to learn stuff. It looks like the ESP32 has some limitations on real time response, due to the RTOS, what I mean is that you have to handle a lot of stuff with RTOS calls, not directly. This means lots of clock cycles slower. You can check my projects about "computer controlled ..." stuff to see what I mean: lygte-info.dk/project/indexProjects%20UK.html
A RTOS calls will probably mean that the ESP32 will not be faster than a old style Arduino to setup the function (Bad description, what do you call programming multiple times simultaneous), but when running it will have better precision.
When I get better acquainted with the ESP32 (or any ARM CPU) I may redo the above boxes, but for now they work very well.
Nice video, but it might spread some misunderstanding about multi-core and RTOS.
Note that RTOS can run multiple tasks on a SINGLE core.
Pro tips:
1) Keep your tasks on core 1 and leave core 0 for Wifi etc
2) Use priority 1 or higher for tasks. Leave priority 0 for the “idle” task.
3) Use core affinity with caution!
There is a good tutorial on free RTOS at www.freertos.org/tutorial.
Keep up the good work. I always enjoy your videos.
Mike Lopez thank you Mike, that’s great additional Information and appreciated. 👍🏴
What you say, Mike, is of course 100% correct but I had to think of a way of presenting this clearly and using both cores was (I felt) a good way. I have other sketches that use the same core (or even switch cores) but that's for another day (if this video is received well). In general use, though, tasks should leave core 0 alone as much as possible to let WiFi and BT have their dedicated space to run.
I'm a little disappointed that not all RTOS commands are available to the Arduino and the configuration files that control what is and isn't can be a veritable minefield. I'll keep it simple and in line with what my viewers will use most!
It's an amazing device though, at an equally amazing price, and will probably convert me away from my current chip-of-choice, the ESP8266 but which doesn't have enough pins for me!
I'll put your link in my GitHub if you don't mind, that's a good resource, thanks for the heads-up, appreciated.
Very interesting, please continue
Thank you for your feedback, Herman Nienhuis, noted and appreciated!
Hi Ralph, just wanted to thank you for your incredible videos, they are extremely informative, very easy to understand and to learn from with no nonsense whatsoever like annoying background music which always seems too loud and makes understanding difficult. Of course I subscribed and hit the bell, to not miss a single video. Btw, the ESP32 is my new found love and I intent to learn every bit if information about it. Already designed and have manufactured a CNC milled aluminum case that will go with my version of a 3.5" IPS display with capacitive touch and the required PCB of course.
Anyway, sorry about the long comment. Happy Holidays and a great New Year, stay safe.
Thanks so much for your kind words, Hayri. The ESP32 is indeed a wonderful device (not Revision 0 though!) and I am now reading up more on the freeRTOS and multitasking. Very powerful stuff which I will share in the New Year. Have a good one!
@@RalphBacon That is the least I can do, wish I could do more to support you than just subscribing but I do have my own struggles :-) Nevertheless if you are interested in my new upcoming PCB with the ESP32 WROVER on it, or anything esle I work on for that matter, I will be glad to send you one as a gift to show my appreciation for your excellent work. Looking forward to see freeRTOS and multitasking examples. Be well.
Just quickly browsing through your tutorials - love what your're sharing! Multi-threaded stuff works fine when working with unique pins ... when using separate tasks to do stuff concurrently over the I2C or SPI bus, obviously needs resource locking... you may have mentioned this, already so apologies if you have. Great to see simple examples.
Yes, semaphores and mutexes can lock resources but that's no use if the GPIO pin you want to share is already in use with a peripheral! Imagine some part of your program interrupting an SPI packet!
Yeah, I like this sort of stuff. Brilliant vid, Ralph.
Thank you for your feedback, Michael , noted and appreciated! Good to hear from you again!
Thank you, yes more, plz
Thank you for your feedback, Elie, noted and appreciated!
Another Great, Informative video. I'm starting to use the ESP32 and this background info is Very usefull.
Thanks for your post, William, good to hear from you.
Excellent video. Keep them coming. Much appreciated.
Thank you for your feedback, Guido Zijlstra, noted and appreciated!
i love the esp. i avoided arduino wherever i could. just used it to flash my printer 😂 great video with onpoint informations thx👍
You are most welcome John, I'm glad you like the video. Nice to hear from you.
This is soooooo cool....I have like 3 of these boards...late night aliexpress shopping..lol and needed lots of help to really understand what in the heck I got into and how to use them.....Andreas Spiess is good but a little to advanced for my newbie abilities....Thanks
Thanks for your post, Gene, good to hear from you.
Believe it or not, I actually have a wonderful use for your initial duel blink program, it will solve a problem I have been tinkering with for over a year now! You see I am a retired police officer, a few years back, my good wife of 50 years gave me one of those old model's of a 57 Chevy. Now back in 69, we drove a 57 Chevy out to Minnesota for our first year of married life, so it was that, I believe that she homed in on. That said, the model was painted the black and white of a police vehicle of that era, so I wanted to build and mount a small light bar that I could turn on to have the model be in pursuit whilst sitting atop my TV Stand. I have it working, but not right, I do have the wig-wag of the blue and red lights working right but the amber caution light in the rear window has to flash in conjunction with one of the overhead lights. This is not the way they really worked, in the old days when I began police work, the overhead lights ran by chain drive, the bulbs were aircraft landing lights, and the rear flasher ran off a simple flasher like a turn signal and the bulb was a simple stop light bulb from a car, I believe it was an 1157 if memory serves. So, you see, I can now have one core run the wig-wag overhead and the second do the caution amber, should work great once I replace the Nano with an ESP32! Sure in the future, I may try and program it so I can turn on the lights remotely from my laptop, that would really be cool, yes I think I will try and program it to do just that!
You can certainly do it like this, Jerry, but I would caution you against using Core 0 (too much). Just put all your tasks on Core 1 and it will work wonderfully well without upsetting WiFi and BT that runs (behind the scenes) on Core 0. I'll explain all this in a couple of weeks in a new video but given what you are describing is probably 2 or three "blink" programs this will do for now!
Consider using the Parallax 8 core "Prop" microcontroller. it is easy to use and ideal for concurrent programming. it can be programmed in C or in SPIN a very very easy to learn language.
Thats what i m looking for. Thank you.
Glad I could help!
Great Video, very informative. Keep up the good work sir BTW new sub. Please make more ESP32 Videos.
Thanks for your post, Chetanya Saxena, good to hear from you and thanks for the sub. You are most welcome.
Really good stuff, thanks Ralph 👍
Thank you for your feedback, Steve Hallam, noted and appreciated!
I am a student come from Viet Nam, this is a excellent video, thank you because share this topic.
Glad you enjoyed it! Thanks for posting!
thanks mate, 2020 and still a great video
Glad you enjoyed it! I want to do another on Timer Tasks, more efficient. It's on the list...
@@RalphBacon we'll wait for it,
And tonight I have decided to get me an esp32, thx haha
BTW would it be possible to distribute load across 2 esp32 as processors while another is handling load distribution he he thx
@@RalphBacon im back sir, i just recieved my ESP32 today and im going to test it using your examples yaaayyyyyyy! cheers!
Very nicely done. Thank you. Please continue.
Thank you for your feedback, Jose, noted and appreciated!
I plan to use both cores in the ESP for a project I am working on. I want to use core 0 to read wav file data from an SD card and play it using I2S, and provide the audio frame that is being played to core 1. To keep a program running some code from the SD card to move some servos in syvn with the audio. So I have been looking on information how to get this all working. When I read your comments “To be honest, I would not use two cores to do this. Use two tasks instead, of equal priority (ie keep them at 1) on core 1. That way you won't upset either the BT, Wi-Fi or other system function running on Core 0. “
and
“It is true - to an extent. Putting some simple tasks on Core 0 won't make it unstable as long as you "yield()" control back to the other tasks fairly frequently. That said, putting an intensive task on Core 0 is not a good idea - keep those for Core 1 (which is where your "loop()" runs) “
After reading some of your comments, I thought the ESP32 will not work for my project so I looked around and I found information that said if you do not enable the WIFI or the Bluetooth the code will not be running?
esp32.com/viewtopic.php?t=31169
Some comments said if you do not include it in your code it will not run,
“This is nuts. Don't initialise it. There's nothing to stop. “
I think this might be right, but I will have to look into it more. But it makes sense If you do not include WIFI in your sketch why would the code run?
If you don't invoke Wi-Fi (on Core 0, as you say) then no Wi-Fi code will run. So your Core 0 can be used more like an App core (but there may be other stuff running on Core 0 that is sensitive to delays). If you get a PANIC on Core 0 you know you have crossed the line.
Good practice would state that, usually, we should leave Core 0 to do all the Espressif things (including Wi-Fi, BT et al) and use Core 1 for our app. But, like all good rules, they are made to be broken now and again!
As usual, great stuff. More thankyou.
Thank you for your feedback, Jeff Bluejets, noted and appreciated!
Hey! Ralph.
I just looked at your video and I will try this tomorrow (it’s past 1AM for me, now).
Thanks for this tutorial. 160 times faster ?? Really ?? It just might solve my flickering problem but I’ll try to get the TFT working first, then, the GPS. I’ll give you updates soon (via mail)
Happy new year .. Talk to you soon !
Talk soon! Don't be a stranger!
Esp32 CAM
Hii guys, i am stuck at designing a system.
I want one task to continually check for touch sensor signal, if it comes, then save image and insert its name into queue.
For second task i simply need to poll queue if there is a file name, send that file to email, pop the name and continue polling.
I am unable to run both tasks concurrently on both cores, esp32cam resets itself when i run the program.
Can someone help me how should i design my tasks so they can work concurrently?
Please pardon me i am new to these things.
Just found this. Brilliant. Earned a sub
Thanks, DannyK, welcome aboard.
Yes!!! Please!!! Esp32
Thank you for your feedback, Justin, noted and appreciated!
I'm late to the party here but I read through a bunch of the comments below and did not see an answer to your question. So, I thought I would share why I believe you need the delay in the loop() function to make it work:
It is as you say because the ESP-32 implementation is sitting on top of FreeRTOS, the RTOS is checking for tight loops and can result in the watchdog timeouts. Hence we need to have the vTaskDelay() calls if we are not writing code that makes use of the loop() method. The "delay" command internally tickle the watchdog to keep things happy.
There was an issue created for this a couple of years ago that discusses it and it can be enlightening: github.com/espressif/arduino-esp32/issues/595
Yes, I'd come the same conclusion, Chris, that the sketch's loop was being called in a for(;;) loop - so if nothing was added to the loop() function then it would be considered a tight loop and starve other processes of CPU cycles.
The solution I show in my next video (gosh! I'm ahead of the game this week) is to simply delete the task if it is not required. If we _are_ going to use the loop() as a sort of task supervisor then we don't delete it, obviously, nor does it need that delay(1) which worked so well but was just consuming CPU cycles for no benefit - hence the vTaskDelete.
More in my next video, thanks for the confirmation!
Yeah I like to see that sort of stuff (multicore programming on ESP32)
Yes, multicore vs multitasking is quite different though, so keep tuned.
MORE please sir!
Ooh, that's enthusiastic, Kenn! I was just thinking (always dangerous) the other day about multitasking on this chip. We will have to see whether all that Deep Thought comes up with new videos.
@@RalphBacon Use one core per task? Or virtual multi-tasking?
yes please more of this !
Thank you for your feedback, patrick maartense, noted and appreciated!
Very Useful for me, thank you for your sharing :)
Glad it was helpful!
Is the Arduino runtime environment for the ESP32 thread-safe and reentrant? In the past, casual inspection of some Arduino libraries show that some/many are not thread-safe because they use statically allocated memory without any locking or protection. This is the tricky part of multi-threaded programming and usually results in intermittent and hard to diagnose bugs because the cause is so timing dependent. Like is the Serial.print method not using any static buffers while its formatting, e.g., Integer values into text? What happens if both threads are simultaneously running the same method?
In other examples of adapting the Arduino environment into an RTOS with multiple threads, the practice usually is to only have one execution thread be allowed to use the Arduino environment because its unsafe, and the other threads do "other" processing in known-to-be-thread-safe ways.
Excellent point you make, Louis. I shall investigate. Given that most libraries were never written to be thread safe I suspect the safest way to use them is to ensure that only one task ever uses them at a time - which, given the logic of a program could be quite straightforward. For example, if you are writing to a TFT display ensure that only one task (at a time) can access it. I'll investigate what Espressif say, too, on this subject. Thanks for posting, good to hear from you.
As usual... clear and important info... TX
Glad it was helpful!
Thanks for this tutorial Ralph, it works for me. I would be very interested to know more about semaphores and mutex for the ESP32 - perhaps you have already made another video on the subject? Also, if I'm using wifi, async. server and client libraries what core would they be running on by default?
Wi-Fi runs on Core 0, along with BT and any other Espressif code. Your code should all run on Core 1. If you place any code on Core 0 you run the risk of it causing a PANIC on Core 0 as it is _very_ sensitive to any delays.
I can't remember whether my video #151 (ruclips.net/video/ywbq1qR-fY0/видео.html ) dealt with semaphores / mutexes - I'll leave it for you to check!
@@RalphBacon Video #151 explains a lot of the finer detail, very good thank you. I wonder how 'sensitive' the single core ESP8266 is when it comes to WiFi operation and the user code delaying WiFi operations.
i think the main loop has the highest priority, you need the delay(1) so put a hold on it so the other loops can run
The main loop runs at a priority of 1, the same as others unless you put them higher or lower. Ideally, all tasks should run at priority 1 so the scheduler can do its time-slicing work, unless you're doing some advanced stuff.
Thanks yes more on ESP32 👍
Thank you for your feedback, Paul C Johnson, noted and appreciated!
Can i use hardware interrupts and dual core mode at the same time? When i tried it, i got a lot of errors in runtime.
For example, structure in my project:
0 core for attach Interrupts, and some of algorithms.
1 core for Wi-fi communication and updates tft display using i2c.
Is it possible?
You must be careful in using Core 0. Try and keep all your tasks on Core 1 initially. Passing semaphores between tasks is fine, but *not* from an ISR, you need a different function. Core 0 is used by RTOS for the WiFi and BlueTooth; if you starve them of CPU cycles they will crash.
Very , very nice into to ESP32 !
Thanks!
Hey, Ralph!I
I thought, since that time I got my ESP32 from China, I might as well give it a go and see how to program this li'l wonder. 2 cores seems a nice things to have and I'm glad the use of these cores are very common to yours in my way of thinking .. and a bunch of GPIOs too, although not all of them ore broken out on my Board.
I got the ESP32 Development board, from DOIT (or rather, a very nice clone, may I add). I downloaded the board specifics and driver from GitHub and I THINK it's working. I say « I THINK » because the IDE does not seem to see the board as far as the com port is concern.
I'll seek other videos to see if I should connect this board via FTDI or if I can program it via the microUSB port, just like the Arduino boards. To be followed ..
On that, a few days from the New Year, I wish you all the best for 2020. Talk to you soon, my Friend !
Happy New Year Daniel! The question is, does your PC see the COM port of the ESP32? You can use the USB socket just as on an Arduino to upload sketches etc and get back serial messages. Make sure you have selected the correct board from the long list.
nice
You are most welcome inferno 601, I'm glad you like the video. Nice to hear from you.
Thank you.
Most welcome, Gil
What a great Video, thanks!
Glad you liked it!
Did you figure out why the delay(1) is needed within the loop ? I can not find a proper explanation for that. Thank you in advanced!
Yes, Niek, I found out the reason. A _delay()_ has an inbuilt _yield()_ which, as you know, allows other processes to take their time slot from the processor. Without the _delay()_ the loop never relinquishes MCU time to other tasks.
I tried the following options:
//delay(1);
//taskYIELD();
//vPortYield();
//yield();
vTaskDelete(NULL);
The latter is the best option as it removes the _loop()_ task permanently, never to be called again. Some of the other _yield_ options didn't do what I expected.
@@RalphBacon Thank you Ralph, really helpful. Keep it up! Your explanations are very clear.
Ralph, thank you very much for you videos and especially for the time you have dedicated to the world of Arduino. May I ask, of you, what program did you use for programing? You said the name during the first part of the video but I was not able to get the name clear enough to do a search for it. It looks like a very interesting system for programing Arduino type processors. Thanks again, Bill in Oklahoma, USA.
I use the Eclipse (Sloeber) version but wait for the new Arduino IDE later this year, Bill. It's based on the same Eclipse setup so should be very good.
You should also consider using the "Visual Studio Code" IDE in combination with PlatformIO. It provides Intellisense/command completion, a nice outline, a header file per project remembering things like board type and details, etc.
Hello Ralph, thanks for your video. I learned quite few new things by watching it. I bet other videos of yours are no less excited.
BTW: What is the IDE you're using at 16:29?
Glad it was helpful! I was using the Eclipse Sloeber IDE but as support for it has ceased I now use PlatformIO on Visual Studio.
@@RalphBacon Thanks. I'm using PlatformIO as well.
Hah, I hadn't yet finished the video yet and was going to ask if this supported "semaphores".. which I learned about in Operating Systems class back in 1995. Anyways, cool to see they use the same terminology and that this processor can do this :) Semaphores are great for whben accessing shared global variables.
Indeed. Trying to "share" global variables without using them is a recipe for disaster! Glad you like the video 👍
Yes some more please.
Thank you for your feedback, George, noted and appreciated!
Excellent vids
Thanks Bryan, glad you liked this one!
Please tell how to make a non blocking wifi program on esp32
I have a serial display to update at the same time MQTT data upload .if it looses MQTT broker it hangs ...
Don't use Core 0, Raviprakash. That is used by Espressif for WiFi and other stuff (BT). If all your code is on Core 1 (which it will be by default) then WiFi etc will not block and will not hang.
Hi I've built a MQTT controlled robot using single core maybe this could help.
github.com/0xraks/esp32_MQTT_Bot
I've watched this again tonight, and have gained even more from it.
I have to ask: does the Arduino IDE/library allow you to create multiple tasks on a single core?
It's called multi-threading. it's actually pseudo-multitasking because the mcu is a serial processor is going to perform a little bit of each task you gave it to, in order to give you the appearance of multi-tasking
My next video on the ESP32 shows this but you can try just by changing the core parameter on those tasks in this demo. Works as you would expect. If you don't specify a core to run on, then the task scheduler will allocate work to a core as it sees fit. there's much more detail here:
www.freertos.org/implementation/a00005.html
I hear one of the 2 cores is reserved for wifi and adding tasks can make it unstable. Is this true?
It is true - to an extent. Putting some simple tasks on Core 0 won't make it unstable as long as you "yield()" control back to the other tasks fairly frequently. That said, putting an intensive task on Core 0 is not a good idea - keep those for Core 1 (which is where your "loop()" runs).
Well presented esp (pun) ially for a newbie like myself
Groan. Almost a pun, anyway, Graham! Glad you liked the video!
Neat one. Thank you.
You are most welcome Dejan, I'm glad you like the video. Nice to hear from you.
@@RalphBacon ESP32 has so much to unveil. Just to mention chips ID, touch sensor, temperature sensor, etc. It would be nice to someone take all of that, all of connectivity and make a nice video, or blog article. I am planning to put it on a magazine article i write for. Time is all i don't have. Damn...
I'm starting a petition for the UK government to create a 30-hour day to give me more time to do the stuff I need to. Sounds like you will be the first signatory! Yes, I'll get round to the ESP32 features, I hope, in due course, if only I had more time. Oh, I have a feeling of deja vu...
I tried the code in the video and I didn't get the same results as you did. I copied the code from your GitHub, after changing the pins to the pins I am using (18 & 21), it worked perfectly. The difference is the semaphore = xSemaphoreCreateBinary(); line that was in your GitHub code. I didn't see that in the video. I have SOOOO much to learn.
We're all learning, John! Missing a line of code is no biggie. Glad you got it working in the end. ESP32 is such a powerful chip, don't you think?
@@RalphBacon Yes, it is an amazing chip. I probably need to focus a bit more. I started with the Arduino, then got the ESP32, Analog Discovery 2, and a few days ago, my NVidia Jeston Nano arrived. I know a little about all of them... I think it is time to stop buying toys and to start learning some stuff.
If you want a suggestion for a video, I have one. My next project is to get an ESP32 to turn on/off and speed control a 4 pin cooling fan (I have an NF-A4X20 5V PWM). The speed would be based on the temperature that the temp sensor is sensing, in my case, between 26 and 37 degrees. The manufacturer told me that it will work with 3.2V so the ESP32 should drive it.
Keep up the great content.
Hi, Thanks for the video
I have a task that is resposible for posting data to firebase but I'm not sure how much memory should I assign to it.
Thanks
Start at a reasonable amount (eg 3,000b, or higher if it crashes!) and print the remaining stack memory every 60 seconds in the task. Then reduce it so that it has headroom of about 500 bytes. Like this:
static unsigned long prevMillis = 0;
if (millis() - prevMillis > (60000))
{
unsigned long remainingStack = uxTaskGetStackHighWaterMark(NULL);
log_v("Free stack:%lu", remainingStack);
prevMillis = millis();
}
Great video as always Ralph! I see a lot of yes please do more esp32 videos and not a single one that says no thank you go back to only doing the arduino. I'll add my vote for doing more esp32 videos please.
I wonder if you turned off the bluetooth and wifi if it would run your test program even faster. If they were needed, I would think they could be turned back on to publish the results. Like publishing the numbers to a webpage etc.
I don't think you would see a speed increase if WiFi and BT were switched off, Steven, because they run on Core 0 (behind the scenes) and we were running that Prime Number nonsense purely on Core 1 - and the two are completely independent. In fact, running a duplicate task on Core 0 could have caused a processor reset if the WiFi or BT tasks were starved of CPU cycles - you have to tread very carefully when using Core 0.
OK, more videos on ESP32 is what I hear. Keep tuned!
@@RalphBacon oh I thought you had it doing multicore processing.
How can loop0 give the semaphore twice? Can it just give freely? So it's never affected by loop1 taking it?
Correct, and well spotted, Rob. A task can _give_ a semaphore even if no other task is waiting for it. Once taken, it is gone until the task _gives_ it again. It's little more than a flag, really.
@@RalphBacon More like a permission slip then?
Yes, nicely put.
Yea, I like this sort of stuff
Thank you for your feedback, foxabilo, noted and appreciated!
@@RalphBacon Just been chatting with Andreas Spiess on the platform.io JTAG debug feature western digital has just given everyone for free, about to test the ST-Link V2 with the ESP32 and the Atom editor to see if the maker space can get dirt cheap jtag debugging. Seems the ESP32 is making a resurgence in popularity for all things IOT and a whole bunch of new cool things and toys, really exciting time to be getting to know this cheap abundant powerful hardware. Hope you have a great time with it all and I hope the channel grows well, solid walkthrows of interesting stuff are always a great resource to link to when explaining something to someone just starting out.
So, just to clarify, will JTAG debugging allow single stepping of code, or how does it work? I've used MS Visual Studio for years and was spoiled by this feature (impossible to debug enterprise level code otherwise). If the ESP32 allows it... well, I'll await your response first.
@@RalphBacon I am in the breadboard and rats nest phase of finding out, platformio says it supports multi threaded debugging, I'll let you know how that works once i put all this magic smoke back in ;)
So first update, this is specifically regarding the STLink32 V2 and clones, if you set the debug_tool = stlink then you get a compile time error of "DebugInvalidOptions: Unknown debug tool `stlink`. Please use one of `esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa` or `custom`:" I am going to have a scout round the net to see if it is possible to use the "custom" option to set up for use with the STLink if not I will have to order one of the ESP-Prog boards or look at the JLINK clone project as I doubt anyone has £600 floating about to debug a blink led project ;)
Ralph, what happens if you add a xSemaphoreGive statement after the xSemaphoreTake statement in loop2?
You've released the semaphore immediately it's been taken.
Study on power consumption variations when one cpu is off?
Thanks.
No, I haven't even gone there (but I bet others have). I'm not sure that I would choose an ESP32 for battery usage but I guess 'needs must'. I might do something in a future video, but nothing is planned right now.
Ditto for setting the different clock frequencies and measuring power.
But the blue led doesn't give the semaphore until it is off, so why doesn't the red led turn on as the blue led turns off? (Or is LOW 'on' on an Esp32? )
The blue LED gives the semaphore when it has finished its cycle, _including_ the off period. So the two tasks can now start again more-or-less at the same time - otherwise the red LED would run whilst the blue one was still off. Make sense, Andy? Need more explanation? Let me know!
Does it make sense to create a task each time you want to update the OLED display? I notice it atakes like 20ms to refresh an OLED with I2C ... pretty slow. Say I have some other process I want to run continuously without a 20ms interruption.. just wondering if the dual core processor would help in this situation.
I would have a task that runs continuously to update the OLED display but which sits there idle until a resource is made available (a flag, if you will).
Because it is waiting on a resource (a binary semaphore, maybe, or mutex) it uses no resources itself because it will not be scheduled until the resource becomes available.
This leaves other tasks to do their business without hinderance.
I would like to know the way using 2 cores of esp32 to get vibration values by using 2 adxl sensors?
To be honest, I would not use two cores to do this. Use two tasks instead, of equal priority (ie keep them at 1) on core 1. That way you won't upset either the BT, Wi-Fi or other system function running on Core 0.
Hello, quick question, do you meanwhile know why you have to include the delay from (1)?
If you are talking about the one in the loop( ) it is to stop it being optimised away by the compiler. You can remove it, and the loop ( ) function disappears too which _might_ give you an error because the outer shell is calling it.
Is code written in sloeber-workspace for an ESP32 governed by the same open source license requirements? Am I able to keep my code private for commercial hardware I develop that uses ESP32 devices?
However you write your code, it's not the IDE tool that decides whether or not the final product can be closed source.
If you use open-source libraries that have to be made available in the same manner as you found them (ie open source) then your final code cannot be closed source.
You must check the licence in each of the libraries. Absence of a licence does *not* mean that it is free - quite the reverse.
If, however, the software you include in your project is licenced under the MIT licence then you can do anything you want, including making closed-source software from it.
It's a legal minefield. For the ESP32 you also need to check whether the version of RTOS they use is OK for closed source software projects.
It may be possible to create some of your software as closed source but release the parts that must be open source.
See choosealicense.com/
Amazing tutorial!
You are most welcome Gabriel, I'm glad you like the video. Nice to hear from you.