Tracing Arduino .ino program with breakpoint - Page 1
Forum section: electronics / projects

author: Wilson__
author_rank: Frequent Contributor
timestamp: June 20, 2024, 04:20:25 am
content: Is there a software platform (seem PlatformIO or others??) allowingbreakpoint and variable value inspectionfor.ino fileon Artduino ATMEGA 2560 hardware?We need to trace one piece of .ino code to understand how it worksMany thanks

author: Whales
author_rank: Super Contributor
timestamp: June 20, 2024, 05:23:11 am
content: Arduino MEGAs don't have debug hardware on them. You either need to buy an external debugging tool to plug into the board or a different board that has one built in.There is a list of supported boards here:https://docs.arduino.cc/software/ide-v2/tutorials/ide-v2-debugger/Further detail: The USB connector on an Arduino Mega goes to a chip that converts USB signals to UART ("serial") signals. These UART signals go to the ATMEGA2560 chip. This chip has a program on it loaded at the factory called a bootloader, this program listens to the UART for new programs to write to the flash. No part of this chain supports debugging commands, to debug an AVR chip you need to use a different interface (different wires) than the serial UART.I generally use printf() style debugging in these situations instead (unlike breakpoints and memory watches you don't need any special hardware or software).

author: SteveThackery
author_rank: Super Contributor
timestamp: June 20, 2024, 11:08:14 am
content: Quote from: Wilson__ on June 20, 2024, 04:20:25 amThis might do the trick:https://visualmicro.comIt's an add-in for Microsoft Visual Studio and provides an improved development environment including the debugging features you are asking for. It is 100% back-and-forth compatible with the Arduino code editor.Importantly, unlike Arduino editor V2, it will debug any Arduino, not just those that support hardware debugging.I use it almost exclusively for all my Arduino work and I think it's great.

author: Whales
author_rank: Super Contributor
timestamp: June 20, 2024, 12:46:35 pm
content: Quote from: SteveThackery on June 20, 2024, 11:08:14 amHuh? How?Don't you need extra hardware? Or does it upload its own supervisor code?

author: Wilson__
author_rank: Frequent Contributor
timestamp: June 20, 2024, 01:43:58 pm
content: Many thanks.While hadware debug are built-in with ARM chip, PIC and others, there were time when MPU first come out and used software debugger.The debug software linked in a 'remote monitoring' code with the user program.When human user requests a breakpoint, it put (overwtite) an assembly language JUMP instruction at that point in RAM.When user program hit the breakpoint, control jump into the 'remote monitoring' code that can communicate with the user interface (over uart serial) to read/modify variables that it knows where they are and its structure from the linker MAP file.When user hit resume key, it restore the original machine code at RAM and execute a JUMP to the point.Many thanks

author: Peabody
author_rank: Super Contributor
timestamp: June 20, 2024, 01:58:28 pm
content: Michael Kohn's Naken Assembler includes a single-step simulator for AVR code. It's pretty basic, and of course works at the compiled assembly language level, so it doesn't keep track of variables, only the registers. But it's free.

author: SteveThackery
author_rank: Super Contributor
timestamp: June 20, 2024, 03:29:57 pm
content: Quote from: Whales on June 20, 2024, 12:46:35 pmAs explained by @Wilson__, it's a software debugger. That's why you have to recompile and upload when adding or removing a breakpoint or watch window. But don't worry about that - despite it sounding like a kludge it is actually really easy to use and works brilliantly. There are situations where hardware debugging is better, but for most Arduinos you don't get a choice. Software debugging techniques have been refined over decades and are very reliable.One last small point - VisualMicro will make use of a hardware debugger if it is supported by the target, so it provides a very clean and well integrated environment.

author: Wilson__
author_rank: Frequent Contributor
timestamp: June 20, 2024, 08:01:11 pm
content: Many thanks, Steve.Does visualmicro has typical functions of hardware debugger, break point, single step, sep into, step out,...Watch/inspect/edit variable decoded as data type (char, int...) and c typedef structure, array, etc.?

author: tooki
author_rank: Super Contributor
timestamp: June 20, 2024, 08:23:03 pm
content: There is also the software debugger in PlatformIO,avr-stub.https://community.platformio.org/t/how-to-debug-on-arduino-mega-2560/18446

author: thm_w
author_rank: Super Contributor
timestamp: June 20, 2024, 11:28:13 pm
content: Quote from: Wilson__ on June 20, 2024, 08:01:11 pmYes but 30 day trial:https://www.visualmicro.com/page/User-Guide.aspx?doc=Debugging-Explained.htmlVisualgdb is also 30 day trial:https://visualgdb.com/tutorials/arduino/If you are dead set on arduino, its worth paying for one of these.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: June 21, 2024, 02:25:52 am
content: I never used a debugger on my microcontroller projects, and at some times it may have been useful, I do not miss it very much.More complex algorithm (for example verifying of CRC algorithms) I first debug on my PC and only put it in a microcontroller when the code is correct.When interacting with microcontroller hardware, classical debugging (break points stepping though code) is often also of limited use because the debugger completely upsets timing.Sometimes I push out some data through an unused peripheral (Uart, SPI) or even some spare generic I/O pins and catch that with a logic analyzer. This way you can follow the code execution path in the microcontroller with very little changes in timing. It's also easy to combine this with a few #define macro's so your debug code can either be defined into oblivion for the normal program flow, or defined into debug statements for the debugging. For an AVR processor, starting with an EUR10 "Saleaea" clone logic analyzer, and using it with Sigrok / Pulseview is a good start. This should be in anyone's toolbox when programming / debugging microcontroller stuff.

author: tooki
author_rank: Super Contributor
timestamp: June 21, 2024, 10:18:40 am
content: Quote from: thm_w on June 20, 2024, 11:28:13 pmWhy, when it’s entirely free to do with PlatformIO?I just tested it in PlatformIO, works fine.

author: tooki
author_rank: Super Contributor
timestamp: June 21, 2024, 10:25:18 am
content: Quote from: Doctorandus_P on June 21, 2024, 02:25:52 amWhile I don’t disagree with the other approaches you mention — I also debug algorithms on a computer (actually, I usually just use onlineGDB), and a logic analyzer is definitely helpful. But your post makes it sound as though interactive debugging is practically pointless on an MCU, and I definitely disagree with that — and clearly, so do many others, since there’s a huge industry around MCU debugging.Given that for many MCUs, the barrier to entry to hardware debugging is so low,* it seems silly to me to not include it in your toolbox. It won’t solve every problem, but it is decidedly useful.*The official ESP32 debugger is $10. STM32 has a few options, the cheapest being around $30 IIRC — or get a Nucleo board for $20 that has it built in! For PIC and AVR, a picKIT is under $100. The software debugger for AVR in Arduino is free. If it were a multi-hundred-dollar investment I wouldn’t bother, either, but for these peanuts, why not?

author: SteveThackery
author_rank: Super Contributor
timestamp: June 21, 2024, 12:21:47 pm
content: Quote from: Wilson__ on June 20, 2024, 08:01:11 pmSorry for the late reply. Yes to all of those, except I need to check the exact situation with arrays, structures, etc, because my memory is unreliable. You have probably already checked it for yourself by now - the VisualMicro documentation is excellent.I'm afraid I can't comment on PlatformIO, as I've never used it, but VisualMicro have made a comparison chart between VisualMicro, PlatformIO and the Arduino IDE. Obviously they are biased, but it is pretty clear that paying the extra for VisualMicro gets you some useful features over PlatformIO. Only you can decide which is best for you.It is true that software debugging has limitations around timing, etc, but honestly that's mostly the nature of debugging microcontrollers. Most of them operate effectively as real-time devices. Hardware debugging has similar limitations. In any case, for the great majority of Arduinos it's software debugging or nothing (apart from the age-old trick of sticking print statements all over the place).

author: tooki
author_rank: Super Contributor
timestamp: June 21, 2024, 04:12:15 pm
content: Quote from: SteveThackery on June 21, 2024, 12:21:47 pmWow, that comparison table is disingenuous.“Arduino IDE CompatibleYes - Visual Micro is fully compatible with the Arduino IDE, allowing backwards compatibility for your projects.Yes - All Arduino IDE Sketches will work in the Arduino IDENo - PlatformIO requires converting your INO to CPP, which makes it incompatible with the Arduino IDE” — outright false. Though it is true that PlatformIO is optimized to use .cpp files, I know for a fact that you can open .ino files in PlatformIO and they will compile and run. It knows that if it encounters a .ino file, to do what Arduino IDE (and VisualMicro) know, which is to silently insert the Arduino.h header and tweak some compiler flags or something.“Supports Single INOYes - Visual Micro supports INO Sketches fullyYes - Arduino IDE supports INO Sketches fullyNo - PlatformIO requires conversion from INO to CPP and additional headers adding .”Nope. It doesn’t require it. And isn’t this just the same thing as above worded differently?If you do choose to convert, converting to .cpp requires one single change: inserting the “#include <arduino.h>” line. AFAIK, adding that to a .ino file won’t hurt it even in Arduino IDE. Explicitly adding it to a .ino file in PlatformIO gives the IntelliSense engine the header to provide code autocompletion and gets rid of squiggly underlines.“Supports Multiple INOYes - Visual Micro allows multiple INO files in line with the Arduino SpecificationYes - Arduino IDE supports multiple INO files in tabsNo - PlatformIO requires conversion from INO to CPP and #include statements using ”Could be. I never triednotusing #include statements in Arduino IDE…“Sharing of variables and methods within multiple .ino filesYes - Visual Micro allows sharing of variables and functions between your INO filesYes - Arduino IDE allows sharing of variables and functions between your INO filesNo - PlatformIO requires conversion from INO to CPP and #include statements using ”Isn’t this just the same thing worded differently?“Use Arduino Examples (without code change)Yes - All examples can be used in Visual Micro with no alterationsYes - All examples can be used in Arduino IDE with no alterationsNo - Examples have to be modified to work in PlatformIO”And“Use any historic Arduino project from the web (without code change)Yes - All historic Arduino compatible projects are supported by Visual Micro without modificationYes - All historic project are supported by the Arduino IDE without modificationNo - PlatformIO requires modification to INO files as a minimum to use Arduino projects from the web”The same point two more times, reworded again, and still mostly untrue.“Use same hardware configuration as the Arduino IDEYes - All Arduino board options and board packages are fully supported in Visual MicroYes - All board options and board packages are supported by the Arduino IDENo - Configuration is performed via an INI file with different options to standard Arduino pacakges”True, but this is a good thing, in that it’s way more rational.“Use same library configuration as the Arduino IDEYes - All Arduino Library specification options are supported in Visual Micro for seamless integrationYes - All Arduino Library specification options are supported in the Arduino IDENo - Libraries may differ for PlatformIO configurations and often require additional files adding to the package”Library management in PlatformIO is one of its biggest strengths! Arduino IDE’s global-only library management, with no concern for libraries using the same file names, is absolutely bonkers bad, and one of the strongest arguments for switching to PlatformIO. Visual Micro continuing that tradition is nothing to brag about.“Cross-compatible code editing with Arduino IDEYes - You can edit code in Visual Micro and the Arduino IDE without any changesYes - You can edit code in the Arduino IDE without modification from Visual MicroNo - Code changes are required to use the Arduino IDE again after PlatformIO”Utter nonsense as far as I can tell.If anything, code breaks in Arduino IDEeven when coming directly from Arduino IDEbecause of missing or conflicting libraries, and Arduino IDE has no way to fix that. PlatformIO, thanks to knowing exactly which libraries you use, will go and grab them, and one project’s libraries won’t conflict with another’s.“Requires additional config outside of normal Arduino usageNo - All Arduino configuration is supported in Visual Micro without changesNo - All Arduino configuration is supported in the Arduino IDEYes - Additional INI file configuration is required which is not available for Arduino projects”A tiny, tiny bit true. But mostly it’s just that the configuration is stored differently than in Arduino IDE. There’s still a config file, it’s just not normally edited as text by the user.“Automatic Prototype GenerationYes - Function Prototypes are generated automatically, and can be disabled if required (Optional)Yes - Function Prototypes are generated automaticallyNo - Function Prototypes must be written with the code for correct compilation”When you use PlatformIO with .ino files, it behaves like Arduino IDE and generates prototypes automatically.“Supports Shared ProjectsYes - Shared Projects can be used to modularize code and use it in multiple cross platform projectsNo - Only libraries are available in the Arduino IDENo - Only libraries are avaiable in PlatformIO”Or a user can learn to use #include…Intellisense Code SuggestionsYes - Code Suggestions are generated in Visual Micro as you typeYes - Code Suggestions are available in the Arduino IDE 2.0Yes - Code Suggerstions are shown in PlatformIO as you type”Not mentioned: that the code suggestions in Arduino IDE are SLOOOOOW. I don’t know what’s happening behind the scenes, but it sometimes makes me think it’s actually compiling the code constantly, and since it can’t compile as fast as I can type, it gets behind.No such trouble in PlatformIO or Visual Studio.“Provides a Serial/USB debugger (No code changes, all boards)Yes - Visual Micro's unique Serial Debugger is simple to setup, and requires no code changes to your project, with many features available'No - Only Serial.println() can be used in the Arduino IDE, which requires modification to your sketchNo - Only Serial.println() can be used in PlatformIO, which requires modification to your sketch”Nice little “no code changes, all boards” qualifier, since both Arduino IDE and PlatformIO support debugging with no code changes onsomeboards, and support it with tiny code changes on other boards.“Supports Hardware Debug ProbesYes - Visual Micro supports a wide variety of debug probes in a few clicks, as well as custom configuration available to suit any scenarioYes - Arduino IDE 2 supports some debug probes with some configuration required for some boardsYes - PlatformIO supports a range of debug probes using the INI configuration system”Maybe it has an advantage here. I’d have to test it. Debugging in PlatformIO is very easy to configure for some boards (like SMT32), fickle for others (like ESP32).“On-PC Unit Testing (without code change)Yes - PC based Unit Testing Projects are supported in Visual Micro to allow cross platform compatible code to be tested without the hardware being requiredNo - No PC based Unit Testing features are currently available in the Arduino IDENo - No PC based Unit Testing features are currently available in PlatformIO”And“On Chip Unit Testing (with Code Changes)No - Currently no On-Chip Unit Testing is available in Visual MicroNo - Currently no On-Chip Unit Testing is available in the Arduino IDEYes - PlatformIO supports On-Chip unit testing via Serial using a Library which requires code changes”I have zero experience with unit testing so I can’t comment on these.But basically, that table is designed to make PlatformIO appear WAY inferior to both Visual Microand Arduino IDE(!), when that certainly is not the case overall.It certainly does seem as though they designed Visual Micro to be absolutely round-trip compatible with Arduino IDE with zero configuration changes needed. But migrating to (or from) PlatformIO is not the nightmare they try to portray it as.Whatisa nightmare is Arduino IDE’s library management, if you can call it “management”. It’s more like a collection of toddlers thrown into one sandbox. PlatformIO actuallymanageslibraries, including versioning and dependencies, and does it per-project, so that projects don’t trample on each other, and so that projects will always be able to compile later. I’ve seen too many Arduino projects that no longer compile because the author never documented which libraries they used, and the names of the header files are often not sufficient to identify the correct library. (For example, there are numerous DS3231 RTC libraries that all call themselves ds3231.h, but are incompatible with each other. Never mind that each one could have incompatible versions of itself!)

author: thm_w
author_rank: Super Contributor
timestamp: June 21, 2024, 10:44:01 pm
content: Quote from: tooki on June 21, 2024, 10:18:40 amIf its hobby purposes, absolutely go for it. If its for work, and spending $100 on a license saves an hours time, its worth it to me.I've spent many many hours screwing around with platformIO launch.json settings (not for arduino, other projects). I'm not a programmer so I lack experience with these things and easily get hung up on them.Good point about the library versioning.

author: Wilson__
author_rank: Frequent Contributor
timestamp: June 22, 2024, 06:45:52 am
content: Many thanks for testing it out.I have platformio installed time ago for a brief trail. Not really using it.Please advises pointer to information on where I needto continnueto get"Arduino ATMEGA2560" doing breakpoint.Either hardware or software breakpoit is fine. I need to trace and follow the flow of a piece of old code that is working fine. No need for speed. Just steping through the code. Many thanks

author: tooki
author_rank: Super Contributor
timestamp: June 22, 2024, 07:59:08 am
content: Quote from: thm_w on June 21, 2024, 10:44:01 pmFWIW, I’m basically a non-programmer, too. But I found PlatformIO to be quite easy to wrap my head around.I absolutely understand about the value of saving time.But yeah, that Visual Micro uses the Arduino way of “managing” libraries is lunacy IMHO. That alone makes it worth switching to PlatformIO if you ask me. Even just among dinky hobby projects, I’ve got old Arduino projects I can’t compile anymore because I can’t find the library I used, and none of the ones in library manager work. (And switching them around risks breaking other projects!) That problem is greatly reduced in PlatformIO, where you can even specify a particular version of a library for a particular project. And it is more intuitive to me how to add manually modified libraries to a project, since VS Code simply shows you your folder structure.With that said, depending on how professional a project needs to be, I might not use Arduino at all. Overall I like that platform, but one thing that bugs me about it is how slowallof the available IDEs are to compile. Arduino compiles fastest for AVR, it seems, but when you compare the IDEs from the MCU vendors, the compile-upload-debug cycles are waaaay shorter, and debugging seems more reliable. (At least among the ones I’ve used so far, namely MPLAB X for PIC, and Keil for STM32.) The last MCU-based projects I did were with PIC (because my boss told me to) and after a short learning curve, I was coding just as (in)effectively as I would in PlatformIO or Arduino IDE. :p

author: tooki
author_rank: Super Contributor
timestamp: June 22, 2024, 08:34:39 am
content: Quote from: Wilson__ on June 22, 2024, 06:45:52 amI already gave you that, for PlatformIO, in a post above. What exactly is still unclear now?