MPLAB X and Pinguino PIC32 DIY

From Pinguino
Jump to: navigation, search

In this article I will show you how to use the Microchip MPLAB_X Integrated Development Environment for implement an application and upload it to the Pinguino PIC32 DIY board through the bootloader.

This procedure has been tested on this board but with minor changes it can be used with any kind of Pinguino Board supported by the Pinguino IDE. I will assume that you are already confident with the MPLAB_X usage so I will focus only about the changes that have to be carried out to allows direct uploading through the bootloader.

First in the MPLAB-X I picked a template with "New Project - Samples - Microchip Embedded and I choose PIC32 C Template. Then in File - Project Properties I've selected the right processor and compiler XC32. In my case it was the PIC32MX250F128B but I guess the same procedure apply for all processor. Only pay attention to use the specific file tailored for your Pic. At this point the template wizard has inserted in the program tree many .c and .h that are the skeleton for the application.


From the skeleton files you now can delete any file you dont need. A good programming practice is to use separate file to implement different function in the code so for very large programs will be easy to find the part you need to edit. If you choose to clean and build when you finish to write the code the compiler should complete without errors. The .hex file generated at this point is not suitable to be uploaded through the bootloader because the absolute address of the program code and other memory areas does not take into account the structure the bootloader expect.

In order to make the application loadable with the bootloader you need to add a file in the program tree structure in MPLAB under the tab linker-files

1. Step: Copy the file /opt/microchip/xc32/v1.31/pic32mx/lib/ldscripts/elf32pic32mx.x in the root directory of your project, usually in the directory MPLABXProjects/

2. Step: Rename the file elf32pic32mx.x to elf32pic32mx.ld then in MPLAB add this existing file in the Linker Files tree structure

3. Step: Open in MPLAB editor this file elf32PIC32MX.ld and comment out the line that say

INCLUDE procdefs.ld

/* Default linker script, for normal executables */
 * Provide for a minimum stack and heap size
 * - _min_stack_size - represents the minimum space that must be made
 *                     available for the stack.  Can be overridden from
 *                     the command line using the linker's --defsym option.
 * - _min_heap_size  - represents the minimum space that must be made
 *                     available for the heap.  Can be overridden from
 *                     the command line using the linker's --defsym option.
EXTERN (_min_stack_size _min_heap_size)
PROVIDE(_min_stack_size = 0x400) ;
/* PROVIDE(_min_heap_size = 0) ; Defined on the command line */
/* INCLUDE procdefs.ld */

Now we have to add here the procdefs.ld file that came with the Pinguino IDE that have all the reference to the memory address areas used with the bootloader. For my processor this file is x.4/p32/lkr/PINGUINO32MX250/procdefs.ld under the directory where you installed the IDE.

 * Processor-specific object file.  Contains SFR definitions.
 * linker file for PIC32MX250F128B with USB bootloader
 * Jean-Pierre Mandon may-2012

 * For interrupt vector handling
PROVIDE(_vector_spacing = 0x00000001);
_ebase_address  = 0x9D003000;

 * Memory Address Equates
_RESET_ADDR              = 0x9D004000;
_BEV_EXCPT_ADDR          = 0x9D004380;
_DBG_EXCPT_ADDR          = 0x9D004480;
_DBG_CODE_ADDR           = 0xBFC02000;
_GEN_EXCPT_ADDR          = _ebase_address + 0x180;

 * Memory Regions
 * Memory regions without attributes cannot be used for orphaned sections.
 * Only sections specifically assigned to these regions can be allocated
 * into these regions.
  kseg0_program_mem    (rx)  : ORIGIN = 0x9D004A00, LENGTH = 0x1B5FF
  kseg0_boot_mem             : ORIGIN = 0x9D004490, LENGTH = 0x570
  exception_mem              : ORIGIN = 0x9D003000, LENGTH = 0x1000
  kseg1_user_mem             : ORIGIN = 0xBFC00400, LENGTH = 0x800
  kseg1_boot_mem             : ORIGIN = 0x9D004000, LENGTH = 0x490
  debug_exec_mem             : ORIGIN = 0xBFC02000, LENGTH = 0xFF0
  config3                    : ORIGIN = 0xBFC02FF0, LENGTH = 0x4
  config2                    : ORIGIN = 0xBFC02FF4, LENGTH = 0x4
  config1                    : ORIGIN = 0xBFC02FF8, LENGTH = 0x4
  config0                    : ORIGIN = 0xBFC02FFC, LENGTH = 0x4
  kseg1_data_mem       (w!x) : ORIGIN = 0xA0000000, LENGTH = 0x8000
  sfrs                       : ORIGIN = 0xBF800000, LENGTH = 0x100000
  .config_BFC02FF0 : {
  } > config3
  .config_BFC02FF4 : {
  } > config2
  .config_BFC02FF8 : {
  } > config1
  .config_BFC02FFC : {
  } > config0

 .extra_prg_mem :
	* (extra_prg_mem)
  } >kseg1_user_mem

4. Step Copy this file and insert it where it was INCLUDE procdefs.ld in the elf32pic32mx.ld file

5. Step Since the template include the peripheral lib that has been excluded in the pinguino ide procdefs.ld you need to add this two lines

* Processor-specific peripheral libraries are optional


after the line


in the elf32PIC32MX.ld modified. Now you can clean and rebuild everything that should be error free. To upload the hex file which is located in /MPLABXProjects/<name of your project>/dist/XC32_PIC32MX250F128B/production #

I used pic32prog that you can download at

The upload command is

  1. pic32prog file.hex -D -S -p

If you want to perform and automatic upload to the board after compiling and build you can add this line pic32prog ${ImageDir}/${ImageName} -S -p

in the MPLAB-X project properties under the section building at the voice "Execute this line after building"

Enjoy Pinguino. Fabio.