5.7 Combining the Bootloader and Application for AVR® Devices

This section demonstrates how to merge the bootloader and application projects into a single hex file for 8-bit AVR® devices.

In a production environment, it is common practice to build both the bootloader code and the application code together into a single hex file. This allows both projects to be programmed at the exact same time. This section demonstrates how to combine the bootloader and application projects into a unified hex file using MPLAB X.

  1. To begin combining the application and bootloader projects, set the bootloader project as the Main Project.

    From the Projects tab, right click on the bootloader project and select Set as Main Project

    Figure 5-166. Set the Bootloader as Main Project
  2. Next, create a new configuration in the bootloader project and rename it to ‘NO_CONFIG_BITS’.
    Important: This new configuration will create a project ecosystem where the bootloader and application will share the same configuration bits. Make sure that your bootloader can operate with the application’s configuration.

    Navigate to Project Properties>Manage Configurations.... Click Duplicate with the default configuration selected to create a copy, and then Rename the resulting copy to ‘NO_CONFIG_BITS’. Then click OK and OK again.

    Figure 5-167. Create the New Bootloader Configuration
    Figure 5-168. Resulting Bootloader Configurations
  3. In the new configuration, define a new macro by navigating to Project Properties>XC8 Global Options>XC8 Compiler>Preprocessing and messages. Double click on the Define macros text box and add one macro for ‘NO_CONFIG’. Click OK.
    Figure 5-169. Defining a New Macro
  4. Add the following changes to the config_bits.c file.
    Note: This change will remove the configuration bits from the NO_CONFIG_BITS configuration.
    Figure 5-170. Edit the Configuration Bits Source File
    CAUTION: The NO_CONFIG_BITS configuration must never be directly programmed to the device. This configuration must only be used when you are attempting to combine the application and bootloader hex files.
  1. Set the application project as the Main Project.
  2. Create a new configuration in the application project and rename it to ‘COMBINED’.

    Navigate to Project Properties>Manage Configurations.... Click Duplicate with the bootable configuration selected and then Rename the resulting copy to ‘COMBINED’. Then click OK and OK again.

    Figure 5-171. Create the New Combined Configuration
  3. Add the bootloader’s NO_CONFIG_BIT configuration as a loadable project to the ‘COMBINED’ application configuration.

    In the COMBINED configuration, navigate to Project Properties>Loading and then click the Add Loadable Project... button.

    Figure 5-172. Adding the Bootloader to the Application
    Figure 5-173. Select the Bootloader NO_CONFIG_BITS Configuration

    Click Add and then Apply.

    Figure 5-174. Completed COMBINED Configuration
  4. Update the post build operations to combined the application hex file for the COMBINED configuration. Add the corresponding command to the post build script after the calculation command.

    postBuild.bat

    set isCombinedBuild='%5' == 'COMBINED'
    set unifiedHexFile=%3\%2.X.%4.unified.hex
    
    ...
    ...
    
    REM - Update the unified hex file for the COMBINED configuration
    if %isCombinedBuild% hexmate r2000-1FFFF,%appHexFile% r0-1FFF,%unifiedHexFile% -O%unifiedHexFile%

    postBuild.sh

    isCombinedBuild='$5' == 'COMBINED'
    unifiedHexFile=$3\$2.X.$4.unified.hex
    
    ...
    ...
    
    # - Update the unified hex file for the COMBINED configuration
    if [$isCombinedBuild]; then hexmate r2000-1FFFF,%appHexFile r0-1FFF,$unifiedHexFile -O$unifiedHexFile
    Figure 5-175. Update the Post Build Script

    Update the post build command from within the new COMBINED configuration by navigating to Project Properties>COMBINED>Building and adding COMBINED onto the end of the command.

    Figure 5-176. Update Post Build Command
  5. Figure 5-177. Select the COMBINED Configuration
  6. Clean and Build the COMBINED configuration.