C++ Debugging for the ESP32 in Visual Studio

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so a programmer an engineer and a mechanic are riding in a car through the mountains they get to a mountain pass it's very steep they come down the hill and the brakes are smoking and almost fail and they're terrified they think they almost die they get to the bottom of the hill they control the car get it over the side they get out and they kind of assess their situation the mechanic says i think the brake pads in this thing are smoked we need to get a new set of pads and the engineer says no i think they're actually undersized for what we're doing with it i think we need to upsize the whole system and the programmer says well maybe we just didn't get lucky let's push it back up the top of the hill and try it again [Music] hey i'm dave welcome to my shop i'm dave plummer a retired operating systems engineer from microsoft going back to the ms dawson windows 95 days and today i'm going to demo and show you how to achieve source level visual studio style debugging on an arduino microcontroller such as the esp32 complete with breakpoints watches call stacks exceptions and everything you'd hope for in a modern dev environment and it works on windows linux and the mac for me the amount of fun and enjoyment one can get out of a coding session is often directly related to how seamless and elegant the development process actually is being stuck with printf style debugging on a system that has a long edit compile and test loop can be demoralizing because it's just not a lot of fun we'll even set up the luxury of single stepping through the code line by line but i really think it's the basics that are the most important the ability to set a breakpoint within a key function and be able to inspect memory and variables when that breakpoint fires i recently released a video on how to set up source level debugging for the raspberry pi but that was using the full visual studio product from microsoft today we'll be using the lighter and snappier alternative known as visual studio code within code we'll use the free platform i o ide behind the scenes it will invoke such tools such as gcc and openocd for you in order to provide a great development environment this integration makes it appear as though visual studio code has all of these features natively built right in finding the right hardware to debug with can be tricky i'll be using a board right from the chip manufacturer espresso who also makes the esp32 chip itself because you can't just turn debugging on on a normal esp32 you need what's known as a jtag interface that interface gives you a serial port on one end and a bunch of breakout wires you connect directly to specific pins on the esp32 itself normally you would buy a separate daughter board like the esp prog board you'd plug your serial cable into the esp prog board and it in turn be connected on top of the esp32 and provide in-circuit debugging that way the rover kit however has it all in one there's a special chip known as an ftdi chip that comes all the way from scotland and it exposes not one but two interfaces on the same physical port that means you can be communicating with the chip itself over the normal serial port while you get a second connection to a hardware debugger that open ocd intern uses to control and debug the esp32 you can also be debugging and have the serial monitor open at the same time because they are truly separate logical ports note that both are connected to the same physical usb connector so it's one cable to make this all work we need to do one bit of magic and that is to replace the normal usb serial port driver on the port that we're going to be debugging with we need to install a special wind usb driver on that device so the debugger can work with it directly how the heck are we going to swap the driver out from a running device and what do we replace it with fortunately somebody wrote a nice simple utility called z-a-diag that does it all for you you simply find the port and click a button so let's do that now rather than script and describe everything i plan to do in order to get debugging running i thought i'd set it up for real and demo it for you live that will also help ensure that i don't skip or forget to mention any important steps and now i seamlessly transitioned to obs studio oops i guess if my microphone was in frame and now i seamlessly transition to obs studio where i can do this segment of the show live so that you can see me do all the steps and i won't forget to match any of them because it'll either work or not when i'm done now i have done this on this machine before and i tried to undo all the steps the only thing i couldn't undo was the driver but i'll explain that because there's two interfaces it'll make sense when i show you because the first thing we need to do is to replace one of the serial port drivers i know this sounds scary but it's actually not and once you have the za diag program that you can get from the video description and so i'll switch to a desktop view which will allow me to bring in z a diag now you'll notice right away it says absolutely nothing it has no devices listed i don't fully understand this app but the first thing you need to do is go to options and pick list all devices next we need to find the device that says dual interface rs232 dual interface here we go there's interface 0 and interface 1. now if i look at interface 0 it actually already has the win usb driver because i've already done it if i look at interface 1 it still says ftdi bus once you've installed just the basic vanilla drivers for your board that's what both of your interfaces will say ftdibus for both of them we're going to replace it just on interface 0. so go to your interface 0 and click reinstall driver for just that one leave interface 1 entirely alone when you're done let's bring up device manager and i'll show you basically what you get so now i have com10 which is the com port for this board and i have usb serial converter b and there used to be usb serial converter a once you plug in the board and have the ftdi drivers you should if you want to check it first you should have two devices down here in your in your usb list and it should be usb serial converter a and b once you've made the change you'll just have b left next make sure the jumpers on your rover kit are configured correctly i believe mine came correct out of the box but the main thing is if you check online you can find the jtag jumper configuration and it basically is install all the jumpers and i'll show you the little video of mine so you can see roughly what it looks like and you'll be able to just basically replicate this it is i believe if i look down here the white ones that are the important ones for debugging purposes at least with the device plugged in configured and installed we can head over to the desktop and i can bring up visual studio code that is we're going to say file new actually no we're not we're going to go to platform io if you haven't already installed platform io that's the only extension that you need to do it will suggest some others when you do that like the c plus extension and you should take those payloads but the main thing is to get platform io installed because it is the ide extension in which we will be doing all of our microcontroller programming next i'm going to say new project i'm going to give it a name like debug test esp32 and i'm just going to type rover and it should suggest and find the rover kit board which it does we'll make framework arduino and we'll use the default location as soon as i click finish it'll chew away for a few seconds and it's already ready we can go look at main and we can see this truly is skeletal it just has a setup and a loop that do nothing but it's a good proof of concept so let's try ctrl shift build and see what we get five seconds hm let me clean it and build again i'm sure it can do it in four i'm sure oh but i'm my machine's also encoding 4k video and doing everything else at the same time here there you go four and a half seconds let me know in the comments how long it takes your machine and what your machine is i'm just curious to see what the relative build times are like and if you're faster than mine well i mean it's because i'm encoding 4k video at the same time it's not not my fault anywho let's try debugging it all we need to do is set a breakpoint in setup you know what let's not get too fancy let's try uploading it first all right we're uploaded let's go to the debug tab and we'll click go i'm going to switch over to the debug console i can see it's connected so the serial port stuff is all working i can see both cores so i know it's talking to the real chip and it will take a second and then it'll finally hit the initial app main breakpoint we're right inside the arduino code for the esp32 and if you've ever wondered how does it call setup and loop if you're not curious i'm going to tell you anyway there's a loop task which is a separate thread gets spun off here and if we go look at loop task which we can do now by saying go to definition how handy we can see it calls set up once and then just perpetually calls loop it also whacks the watchdog so is it still politically correct to say whack the watchdog or how do you discourage the watchdog from you rebooting your system i don't know he used to be kick the watch dog which is not you know kick dogs that's that's nonsense dogs are lovely well except the ones that chase you wouldn't bite you but so from that initial app main break point if we click go it should hit our setup breakpoint and sure enough it promptly does i can press step over or and we're back into the collar now this is a really really simple project this much worked a while ago but i found that more complicated projects even a year or two ago didn't work if you had attempted to you know debug interrupts or multiple threads or pretty much any complicated scenario it just didn't work for me but it does now and that is why i'm making this video because it's cool and it works let's go to a complicated project i'm going to load up night driver strip this is led control software that i wrote to do basically christmas lights and all kinds of led effects but it has all kinds of esp32 arduino features such as wi-fi and a web server built in it has a socket for receiving color data over the web it draws leds it's just got a ton of functionality it does audio processing it does an fft 30 times a second to be able to draw a spectrum analyzer waveform all kinds of things so i'm going to set a breakpoint deep inside the drawing code and we'll see if we can have that work and inspect variables and so on in single step once we get in that far if we go to drawing i should be is this the drawing function yeah this is the drawing loop i can scroll through deep to where it is about to it has just drawn or was about to draw all the pixels and now i was able to place them on the actual led strip so here we can actually see fast led set leds being called the real core work let's build this make sure it builds a much more substantial project this one took 18 seconds and it runs in the 15 to 18 second range let me upload it i wonder which tool this is using is it going to use ftdi or serial we'll find out neither because it's set to com7 and we need com10 oh so it's going to use esp tool i'm going to change it at the ftdi and i'll show you it uploading with the ftdi protocol which is really more case if i believe it's placing memory blocks and then verifying and writing them but i'm not a hardware guy so i don't know well it's working but it said lib usb open failed i can see the lights on the chip going though so let's see if the verify passes verify okay now i've added one extra line here to platform io.ini debugging it break equals nothing and that will get rid of that initial breakpoint or it should um i've had problems with being able to delete breakpoints that they just persist once they're set and i have to reboot the chip and after a little delay it finally hit our breakpoint now i can check and i can see that my local variables are available my call stack is here i don't have a lot of screen real estate here to show you all this but uh let me bring this down a bit so you can see all my threads i've named my threads so that you can see they have reasonably logical names like draw loop network handling debug loop screen loop and so on so we can see what the screen loop is doing as an example it's like an update screen doing a print at this second but back to our it might look a little confusing because i have full optimizations o3 turned on in this code so it looks like it's jumps around a bit but it does appear to be working properly so this is fairly complex code you know compared to what the demo project was and it all seems to work this video is some serious narrow casting the number of people just casually surfing youtube that would actually want to watch a video on debugging the esp32 arduino setup is shockingly low so if you know where those people actually are be it on twitter or reddit or a message group or wherever please share a link to this episode with them i'd very much appreciate it now that we're getting towards the end of the year it could be your last chance to grab a classic dave's garage mug from the channel store why because all channel profits from the calendar year 2021 are being donated to the uw autism center subscribe thanks for joining me out here in the shop today in the meantime in between time i hope to see you next time right here in dave's garage are you subscribed yet this little chair will be waiting for one of you and a rocking chair for another who likes to rock and a big armchair for two to curl up in all next time on dave's garage [Music]
Info
Channel: Dave's Garage
Views: 68,996
Rating: undefined out of 5
Keywords: esp32, debugging, c++, c/c++, source, openocd, gcc, arduino, arduino development, arduino debugging, arduino howto, arduino programming, esp32 tutorial, how to, arduino project, esp32 project, debugging arduino, arduino tutorial
Id: b1VkJODi_4w
Channel Id: undefined
Length: 13min 46sec (826 seconds)
Published: Fri Nov 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.