How to Restore a chipKIT Bootloader

How to connect a PICkit 3 programmer, leaving the Power Jack and USB port accessible
How to connect a PICkit 3 programmer, leaving the Power Jack and USB port accessible

Did you know that chipKIT boards can also be used as generic embedded controllers? chipKIT-compatible boards can be programmed not only with MPIDE or UECIDE, but also in several other IDEs using programming languages such C, C++, Pascal or Basic. When using another IDE such as MPLAB X, the chipKIT bootloader may become erased or over-written, which is fine, if you plan to stick with that environment. However, if you want to use MPIDE or UECIDE again after your bootloader has been erased, you’ll need to restore the chipKIT bootloader. To do so, you can follow the Restore Your chipKIT Bootloader Guide with step-by-step instructions and lots of pictures. Knowing how to do this might save you from a massive headache ūüėČ

Good luck!

VN:F [1.9.22_1171]
Rating: 9.8/10 (4 votes cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)

Programming chipKIT Boards from MPLAB¬ģ X IDE Without a Programmer/Debugger

Overview:

Did you know that you can program your chipKIT board or any PIC32 chip preprogrammed with a chipKIT Bootloader right from MPLAB X? It‚Äôs actually quite easy to do. This tutorial will demonstrate how to obtain the proper binaries and how to set up MPLAB X to download programs written in C or MIPS Assembly to a PIC32 using the AVRdude bootloader. Note that this tutorial including screen captures was done using a Windows¬ģ 7 PC.

What you will need:

To complete this task, you will require the following:

Procedure:

  1. Unzip the chipKIT MPIDE to a location on your computer
  2. Navigate to the MPIDE folder an locate avrdude.exe and avrdude.conf in the following folders:
    1. avrdude.exe: mpide-0023-windows-xxxxxxxx\hardware\tools\avr\bin\avrdude.exelocateAVRdude
    2. avrdude.conf: mpide-0023-windows-xxxxxxxx\hardware\tools\avr\etc\avrdude.conflocateCONFfile
  3. Copy both of these and paste into your MPLAB X directory: C:\Program Files\Microchip\MPLABX\mplab_ide\binmplabXBin*Note: MPLAB X IDE will need to be installed on your computer for this folder to have been created.
  4. Next, open MPLAB X and set up your project as you normally would and write your code. (If you are not familiar on how to do any of these please refer to the tutorials towards the bottom of our Academia page: http://chipkit.net/academia/)
  5. The bootloader pre-programmed on the PIC32 does take up some room in Flash program memory. Therefore, to make sure we do not overwrite the bootloader, we will need to tell MPLAB X where to put our program code. This will be done using a linker script. There are special linker scripts in our MPIDE download that are already formatted to accommodate the bootloader. In the MPLAB X IDE Project window, right click on the Linker Files folder and select Add Existing Items‚Ķ. In the dialog, navigate to where you unzipped MPIDE and locate the linker script for your particular device (PIC32MX250F128B used in this tutorial). It will be called something like ‚ÄúchipKIT-application-32MX250F128.ld‚ÄĚ. You will also need an additional script called ‚ÄúchipKIT-application-COMMON.ld‚ÄĚ. Select both of these files, check the Copy checkbox, and press Select to close the window and add these files to your project folder.addingLDscripts
  6. We will need to make some changes to these linker scripts. Double-click on the part-specific linker script (chipKIT-application-32MX250F128.ld in this tutorial) to open it. Scroll down through the linker script to the last line and uncomment the section:
     INCLUDE "chipKIT-application-COMMON.ld"
  7. Double-click on the ‚ÄúchipKIT-application-COMMON.ld linker‚ÄĚ script. Scroll down to approximately line 574 and comment out the line:
     /* ASSERT (SIZEOF(.header_info) == 76, "MPIDE Version not specfied correctly")*/
  8. Next, we will need to let MPLAB X know that we wish to use avrdude to program the PIC32. Therefore, right-click on your project and select Properties to open the Project Properties window.openMPLABXproperties
  9. In the Project Properties Window, select the Building category. On the right side of the window, check the Execute this line after build check box and add the following code to the text field under the check box:
    "C:\Program Files\Microchip\MPLABX\mplab_ide\bin\avrdude.exe" -C "C:\Program\Microchip\MPLABX\mplab_ide\bin\avrdude.conf" -v -p32MX250F128B -cstk500v2 -P COM49 -b 115200 -U flash:w:${ImagePath} selectExecuteAfterBuild
    Some important things to note here:
    1. You will need to make sure you’ve selected your MCU if not using the PIC32MX250F128B as shown in the example.
    2. Similarly, you will also need to change the COM port to the one assigned to your chipKIT Platform. The COM port can be found in your Device Manager (on Windows machines)
  10. Finally, to program your PIC32, make sure that it is in bootloader mode. Depending on the board you have, this may require that you press some buttons or this could be done automatically through the use of an FTDI UART-to-USB chip on the board. Once in bootloader mode, click either the Build or Clean and Build button. Following build, the avrdude bootloader should be called and the PIC32 programmed over the COM port selected with your code. 
  IMPORTANT!! The bootloader will be executed anytime you select Build, Clean and Build or if you attempt to enter Debug mode. The bootloader often resides in sections of memory that are used for debugging. Therefore, if you enter debug mode and your PIC32 is connected to your computer, there is a good chance you will overwrite the bootloader and it will need to be programmed back onto the chip using an external programmer/debugger.¬† Therefore, always disconnect the USB cable if you plan on doing any code debugging with the simulator. You will not be able to do hardware debugging. A programmer/debugger is needed for that. You will also need to deselect the Execute this line after build checkbox in the Project Properties>Building window shown in Step 9. Otherwise you will receive an error.    
VN:F [1.9.22_1171]
Rating: 8.2/10 (11 votes cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)

avrdude and the chipKIT Platform

The bootloader on the chipKIT is based on the same bootloader that I developed for the mega2560. It uses stk500v2 protocol.

Avrdude can be used from the command line with the chipKIT boards or any other board that has the bootloader installed The command would be something like this >avrude -c stk500v2 -p pic32 -P /dev/tty.usbmodem1234 -b 115200 -U flash:w:filename.hex Or, under Windows(If you’ve copied avrdude.exe and avrdude.conf into the MPLABX bin folder): “C:\Program Files\Microchip\MPLABX\mplab_ide\bin\avrdude.exe” -C”C:\Program Files\Microchip\MPLABX\mplab_ide\bin\avrdude.conf” -v -p32MX795F512L -cstk500v2 -P COM40 -b 115200 -U flash:w:pic32_template_1.production.hex

Getting an hex file for chipKIT

When using mpide, default is to remove all the intermediate files between the mpide source of your project (.mpe file) and code uploaded on the chipKIT. But all the files generated by mpide (including the hex file) can be kept by changing mpide preference file. From Linux:
  1. close any running mpide
  2. edit ~/.mpide/preferences.txt
  3. look for preproc.save_build_files attribute
  4. Set it to true: preproc.save_build_files=true
  5. save the preference file
Now, all temporary files from build command will be kept. To locate the right directory with your build, from¬†mpide, press the “shift” key while starting the build/verify command. You will have details about the build process (commands, outputs, path …) in¬†mpide¬†output console. Example:
/opt/mpide-0023-linux32-20111221/hardware/pic32/compiler/pic32-tools/bin/pic32-g++  -O2  -c  -mno-smart-io  -w  -fno-exceptions  -ffunction-sections  -fdata-sections  -G1024  -g  -mdebugger  -Wcast-align  -mprocessor=32MX320F128H  -DF_CPU=80000000L  -DARDUINO=23  -D_BOARD_UNO_  -Danything_you_want  -Danything=1   -I/home/fred/Arduino/Serial2   -I/opt/mpide-0023-linux32-20111221/hardware/pic32/cores/pic32   -I/opt/mpide-0023-linux32-20111221/hardware/pic32/variants/Uno32    /tmp/build6773645236763377018.tmp/Serial2.cpp  -o  /tmp/build6773645236763377018.tmp/Serial2.cpp.o

...

/opt/mpide-0023-linux32-20111221/hardware/pic32/compiler/pic32-tools/bin/pic32-ar  rcs  /tmp/build6773645236763377018.tmp/core.a  /tmp/build6773645236763377018.tmp/main.cpp.o
/opt/mpide-0023-linux32-20111221/hardware/pic32/compiler/pic32-tools/bin/pic32-g++  -Os  -Wl,--gc-sections  -mdebugger  -mprocessor=32MX320F128H  -o  /tmp/build6773645236763377018.tmp/Serial2.cpp.elf  /tmp/build6773645236763377018.tmp/Serial2.cpp.o    /tmp/build6773645236763377018.tmp/core.a  -L/tmp/build6773645236763377018.tmp  -lm  -T  /opt/mpide-0023-linux32-20111221/hardware/pic32/cores/pic32/chipKIT-UNO32-application-32MX320F128L.ld
/opt/mpide-0023-linux32-20111221/hardware/pic32/compiler/pic32-tools/bin/pic32-objcopy  -O  ihex  -j  .eeprom  --set-section-flags=.eeprom=alloc,load  --no-change-warnings  --change-section-lma  .eeprom=0  /tmp/build6773645236763377018.tmp/Serial2.cpp.elf  /tmp/build6773645236763377018.tmp/Serial2.cpp.eep
/opt/mpide-0023-linux32-20111221/hardware/pic32/compiler/pic32-tools/bin/pic32-bin2hex  -a  /tmp/build6773645236763377018.tmp/Serial2.cpp.elf
Binary sketch size: 10748 bytes (of a 126976 byte maximum)
  • temporary directory is¬†/tmp/build6773645236763377018.tmp/
  • Hex file will be named¬†/tmp/build6773645236763377018.tmp/Serial2.cpp.hex
WARNING: the temporary directory will be removed when mpide will be closed. You have to backup the hex file before closing mpide. To go back to initial configuration (files removed after each build), just set the preproc.save_build_files attribute to false
preproc.save_build_files=false

Using AVRDUDE from Linux console

copy avrdude.conf file from mpide to ~/.avrduderc, example:
cp /opt/mpide-0023-linux32-20111221/hardware/tools/avrdude.conf ~/.avrdude.rc
Use ever¬†avrdude¬†from¬†mpide¬†or from your own Linux distro… Here are details about avrdude from Ubuntu Precise Penguin:
dpkg-query -l avrdude
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                             Version                          Description
+++-================================-================================-================================================================================
ii  avrdude                          5.11.1-1                         software for programming Atmel AVR microcontrollers
Check if the avrdude configuration file from mpide is used, and which PIC32 devices are available:
fred@kat:~/MPLABXProjects/Blink$ avrdude -p ?

Valid parts are:
  pic32-440 = 32MX440F512H    [/home/fred/.avrduderc:16438]
  pic32-460 = 32MX460F512L    [/home/fred/.avrduderc:16251]
  pic32-360 = 32MX320F128H    [/home/fred/.avrduderc:16054]
  pic32-360 = 32MX320F064H    [/home/fred/.avrduderc:15863]
  pic32-360 = 32MX360F512L    [/home/fred/.avrduderc:15672]
  pic32 = 32MX795F512L    [/home/fred/.avrduderc:15483]

...
We can see that pic32-360 has to be used for Uno32 (32MX320F128H) Example, with:
  • avrdude from Ubuntu distro (Precise Penguin – avrdude 5.11.1)
  • Uno32 located on /dev/ttyUSB0
  • hex file build by using mpide
fred@kat:~/MPLABXProjects$ avrdude -P /dev/ttyUSB0 -p pic32-360  -b 115200 -c stk500v2 -v -v -U flash:w:Haribo.cpp.hex

avrdude: Version 5.11.1, compiled on Oct 30 2011 at 10:37:28
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "/etc/avrdude.conf"
         User configuration file is "/home/fred/.avrduderc"

         Using Port                    : /dev/ttyUSB0
         Using Programmer              : stk500v2
         Overriding Baud Rate          : 115200
avrdude: ser_recv(): programmer is not responding
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_recv(): programmer is not responding
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_recv(): programmer is not responding
avrdude: stk500v2_ReceiveMessage(): timeout
Oops… Upload error¬†? I have to press the Uno32 reset button, then code is uploaded:
         AVR Part                      : 32MX320F128H
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PA0
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    10     8    0 no       4096    8      0  9000  9000 0x00 0x00
           flash         65    10   256    0 yes    131072  256    512  4500  4500 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : STK500V2
         Description     : Atmel STK500 Version 2.x firmware
         Programmer Model: AVRISP
         Hardware Version: 15
         Firmware Version Master : 2.10
         Vtarget         : 0.0 V
         SCK period      : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x504943
avrdude: safemode: lfuse reads as 0
avrdude: safemode: hfuse reads as 0
avrdude: safemode: efuse reads as 0
avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: current erase-rewrite cycle count is -1145324613 (if being tracked)
avrdude: erasing chip
avrdude: reading input file "Haribo.cpp.hex"
avrdude: input file Haribo.cpp.hex auto detected as Intel Hex
avrdude: writing flash (32664 bytes):

Writing | ################################################## | 100% 9.15s

avrdude: 32664 bytes of flash written
avrdude: verifying flash memory against Haribo.cpp.hex:
avrdude: load data flash data from input file Haribo.cpp.hex:
avrdude: input file Haribo.cpp.hex auto detected as Intel Hex
avrdude: input file Haribo.cpp.hex contains 32664 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 3.29s

avrdude: verifying ...
avrdude: 32664 bytes of flash verified

avrdude: safemode: lfuse reads as 0
avrdude: safemode: hfuse reads as 0
avrdude: safemode: efuse reads as 0
avrdude: safemode: Fuses OK

avrdude done.  Thank you.

Using AVRDUDE from within MPLAB X

To use avrdude to upload a hex file you just built in MPLAB X, there are a couple steps you need to follow:
  1. (optional) From the MPIDE install folder, copy (as an example) C:\Program Files\mpide-0023-windows-20120122-test\hardware\tools\avr\bin\avrdude.exe to someplace you want to put avrdude and its configuration file, like C:\Program Files\Microchip\MPLABX\mplab_ide\bin\avrdude.exe
  2. (optional) From the MPIDE install folder, copy (as an example) C:\Program Files\mpide-0023-windows-20120122-test\hardware\tools\avr\etc\avrdude.conf to someplace you want to put avrdude and its configuration file, like C:\Program Files\Microchip\MPLABX\mplab_ide\bin\avrdude.conf
  3. From within MPLABX, open the properties for your current project. Select the Building/Loading screen, then enter the following for “Execute this line after build” (and check the box as well):
    • “C:\Program Files\Microchip\MPLABX\mplab_ide\bin\avrdude.exe” -C”C:\Program Files\Microchip\MPLABX\mplab_ide\bin\avrdude.conf” -v -p32MX795F512L -cstk500v2 -P COM40 -b 115200 -U flash:w:${ImagePath}
    • (Note that you will need to substitute the proper COM port number of your board, as well as the actual location of avrdude.exe and avrdude.conf into this command line. Also, if you’re not using an MX795 you’ll have to update that as well.)
  4. Make sure you have copied the proper linker script into your project. For example, if you’re using a chipKIT MAX32 board, just copy the chipKIT-MAX32-application-32MX795F512L.ld file from C:\Program Files\mpide-0023-windows-20120122-test\hardware\pic32\cores\pic32 into your MPLABX project folder, and then add that file to the Linker Files folder in the Project Properties.
If all of the paths are set up properly, you should get an automatic download every time you rebuild your project in MPLABX. Many times, I need to reset my chipKIT board in order to get the download to work, even though I don’t need to do this when using MPIDE. I’m not sure why this is – if anyone can suggest a workaround, that would really help.
VN:F [1.9.22_1171]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)

USB Bootloader

The PIC32-avrdude-bootloader communicates over either a serial port (UART) or the USB port (on those pic32 chips that support USB.)  It can be built for either serial or USB communication with the PC. The bootloader communicates with the avrdude executable on the PC side of the connection. The following boards have been tested using the PIC32-avrdude-bootloader:
  • Microchip PIC32 USB starter kit
  • Microchip PIC32 Ethernet starter kit
  • UBW32 – MX460
  • UBW32 – MX795
  • Fubarino SD
  • Fubarino Mini
In order to use this bootloader, you can either download the full source from github, or just pick and choose the HEX file that is right for your board. There are three bootloaders that have been created for chipKIT and chipKIT compatible boards:
  • PIC32-avrdude-bootloader¬†–¬†This new bootloader for MPIDE/chipKIT PIC32 boards is buildable for all currently available ¬†chipKIT boards (UNO32, MAX32, uC32) as well as other PIC32 based boards like the UBW32 and Fubarino boards.
  • pic32-Arduino-USB-Bootloader-original¬†– This is the original version of the bootloader that works only if compiled with the C32 v1.xx version due to changes in the way that newer version of C32/XC32 handles the linker scripts files.
  • pic32-Arduino-Bootloader ‚Äď This is the older original version of the bootloader which supports only Digilent uno and mega pic32 boards.This bootloader is compatible with v5.8 of avrdude.
  • For Digilent pic32 boards, it is highly recommended to download the Digilent official bootloader from the Digilent website at¬†http://www.digilentinc.com/.
VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Using USB

The PIC32MX3xx series parts do not have a USB controller. The other PIC32 series (i.e. PIC32MX4xx/5xx/6xx/7xx) all have a USB controller. The Uno32 uses a PIC32MX320F128H and therefore does not have a USB controller. The Uno32 and Max32 have standard FTDI serial to USB interface chips (FT232R) to keep consistent with the Arduino way of interfacing. However, it is also nice to be able to use the built in USB port. Many other PIC32 boards have this USB port brought out to a standard connector such as the Microchip USB Starter Kit and the Digilent Cerebot 32MX4 and Cerebot 32MX7. The latest version of HardwareSerial.cpp now supports the first serial port (Serial.begin(), Serial.print() etc) can be reconfigured to use the USB port instead. In order to take full advantage of this, first you have to program the board with the USB bootloader, then use the appropriate board setting in the BOARDS menu. If you are using a custom board in the boards file you can just add the following to your board description: custom_pic32.compiler.define=-D_USE_USB_FOR_SERIAL_ When using the USB for Serial, UART1 becomes Serial0 Serial1 etc, are still there normal configurations Serial.begin(baudrate); //The baudrate is ignored, when doing real USB, there is no baudrate. Receive is interrupt driven and behaves EXACTLY like regular Serial. NO CODE CHANGES REQUIRED. If you want to see how this is done, look at HardwareSerial.h and HardwareSerial.cpp, the actual usb code is in HardwareSerial_cdcacm.c and HardwareSerial_usb.c. The USB code was written by Rich T (http://www.cpustick.com/) More documentation will be provided on how to do this soon. (Created 7/3/2011)
VN:F [1.9.22_1171]
Rating: 9.0/10 (1 vote cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)