You have a rather complex sketch. It’s not working right, and you just can’t find out why. If only there were a way to single-step the program and see what it’s actually doing.
Well, there is now, and here’s how.
From version 0.8.6t (beta version) there are now enhanced facilities for linking up with MPLAB-X.
First you will need a decent hardware programmer, such as a PICkit™3, ICD, or the chipKIT™ PGM. Secondly you will need to install MPLAB-X (if you don’t already have it).
Once that’s all set up and running as it should be you are ready to set it up for chipKIT debugging.
The first thing you need to do is tell it what a .ino or .pde file is. So in MPLAB-X, go to Tools -> Options, then select the Miscellaneous section. Within there is a Files tab. You need to add two entries to the File Associations section.
Start by clicking the New… button and entering “ino” as the file extension. Now in the Associated File Type (MIME) drop-down list select “C++ Data Loader (text/x-c++)”.
Repeat for “pde” with the same MIME type. Once done, press the “OK” button and you are good to go.
Now for the debugging.
Load up UECIDE and open the preferences. Switch to the “Compiler” tab, and turn on the “Compile the sketch in the sketch folder” option. This will place the build folder inside your sketch folder rather than in your temporary directory. It also won’t delete the build folder when you exit UECIDE. You also want to ensure that “Remove old build folder before each build” is turned off.
Now load your program into UECIDE as per normal, ensure your board etc are all selected right. Now comes the magic. For the programmer, select “Pickit 2”. It doesn’t matter that you’re not using a PICkit 2. What this does is change the linker script that is used by the compiler to the “-nobootloader” version. The sketch can then be run stand-alone without the need for the bootloader (in fact parts of it replace the bootloader completely, so you will want to replace the bootloader once you have finished debugging – I’d go grab the hex file for it now if I were you).
Hit the build button in UECIDE (not the program button, just the build button), and it should generate you, amongst other things, a .elf file within the build folder in your sketch.
Now back to MPLAB-X. Create a new project in there, and select “Microchip Embedded” and “Prebuild (Hex, Loadable Image) Project”. For the Prebuilt FIlename, browse to the .elf file that has just been generated. Select the right chip in the Device drop-down, and your programmer in the list of programmers. Hit finish, and it should load your program into MPLAB-X.
You can now use all the debugging tools in MPLAB-X to find out what has gone wrong with your program.
Any changes you want to make, just switch back to UECIDE, make the changes, hit compile, and MPLAB-X should see that new version of the .elf file automatically.
If MPLAB-X says the programmer can’t communicate with the board, or it’s not in debugging mode, etc, then it could be that the settings in the linker script for the chip on your board don’t exactly match the layout of your board. Unfortunately the linker scripts are set up by chip, not by board, and some boards which use the same chips have different layouts, especially as to which programming pins they use. If that is the case, then you may want to create a new blank project in MPLAB-X for your chip type and use the Configuration editor (Window->PIC Memory Views->Configuration Bits) to create a configuration that is correct for your board. Copy the HEX values for the configuration into the “config0” – “config3” settings in the -nobootloader.ld file for your chip (located in %UECIDE%/cores/chipKIT/api).