Development environment
Required tools
| Tool | Version | Purpose |
|---|---|---|
| MPLAB X IDE | Latest | Integrated development environment |
| XC16 Compiler | Latest | C compiler for PIC24 devices |
| MPLAB Code Configurator (Optional) | Latest | Peripheral configuration |
Installation
Download MPLAB X IDE
Download from the Microchip website
- Available for Windows, macOS, and Linux
- Install following the platform-specific instructions
Install XC16 compiler
Download and install the XC16 compiler from Microchip
- Free version supports optimization
- Install before opening the project in MPLAB X
Project structure
The firmware is organized into two main projects:Building the bootloader
The bootloader occupies memory addresses0x0800 to 0x17FF and is protected from being overwritten during firmware upgrades.
Using MPLAB X IDE
Open the bootloader project
- Launch MPLAB X IDE
- Open
firmware/bootloader/bootloader.X - Wait for project to load completely
Select build configuration
Choose the appropriate configuration from the toolbar:
- Debug: Includes debug symbols
- Release: Optimized for production
Using command line
.hex file will be in the dist/ directory.
Building the application
The application firmware starts at address0x1800 with the entry point at 0x2000.
Using MPLAB X IDE
Configure build settings
Verify compiler optimization settings:
- Right-click project → Properties
- Select XC16 Compiler → Optimizations
- Recommended: Optimization level 1 or higher
Using command line
Memory configuration
Bootloader memory layout
The bootloader linker script (bootloader.gld) defines the memory regions:
- Bootloader code:
0x0800-0x17FF(6 KB) - Application space:
0x1800-0xAB000(~676 KB) - Maximum addressable:
0xAA800(698 KB total flash)
Interrupt vector table
The bootloader uses alternate interrupt vectors:Key configuration values
System configuration
Frommappings.h:
UART settings
EEPROM configuration addresses
Flash memory operations
The bootloader implements low-level flash operations:Flash erase
Fromprotocol.c:ProtocolFlashErase():
Flash write
Fromprotocol.c:ProtocolFlashWrite():
- Writes two 24-bit instruction words (6 bytes) at a time
- Automatically skips addresses below
0x1800(protected bootloader region) - Validates each write operation
Hardware dependencies
Microcontroller
- Device: PIC24FJ1024GA606
- Architecture: 16-bit modified Harvard
- Flash: 1024 KB (instruction space)
- Clock: 16 MHz (with PLL for 32 MHz instruction cycle)
Peripherals used
| Peripheral | Purpose |
|---|---|
| UART1 | Bluetooth module communication |
| UART2 | USB/System communication (firmware upgrades) |
| SPI1 | External EEPROM (configuration storage) |
| Timer1 | System millisecond timer |
Compilation output
Successful builds produce several files:| File | Description |
|---|---|
*.hex | Intel HEX format (used for programming) |
*.elf | ELF executable (contains debug symbols) |
*.map | Memory map (address allocation) |
*.lst | Assembly listing |
Only the
.hex file is needed for firmware upgrades. The other files are useful for debugging and analysis.Common build issues
Compiler not found
Error: “Cannot find compiler executable” Solution:- Verify XC16 is installed
- In MPLAB X: Tools → Options → Embedded → Add the XC16 installation path
- Typical paths:
- Windows:
C:\Program Files\Microchip\xc16 - macOS:
/Applications/microchip/xc16 - Linux:
/opt/microchip/xc16
- Windows:
Memory overflow
Error: “region ‘program’ overflowed” Solution:- Reduce code size by enabling optimizations
- Check that the linker script matches your device memory
- Review large data structures or arrays
Missing dependencies
Error: “No such file or directory” for header files Solution:- Ensure all source files are in the project
- Check include paths in project properties
- Verify XC16 peripheral libraries are installed
Programming the device
After building, program the device using:- PICkit 3/4: Direct ICSP programming
- ICD 3/4: In-circuit debugging and programming
- USB Bootloader: For application firmware only (bootloader must already be present)