This section demonstrates how to configure a bootable application configuration for 8-bit
AVR devices.
Description: With the bootloader code built and configured to the needed
specifications, configure the target application to work alongside the bootloader
instead of erasing the bootloader code from the device and programming the new target
application over it. MPLAB X IDE allows the user to set various project configuration
properties to match the bootloader configuration. The first step for completing this
task is to create a blinking LED project.
Create a new MPLAB X IDE project.
Figure 5-139. New Application
Project
MCC opens up automatically when a
new project is created. If not, click the MCC button in the menu bar to
start the MPLAB Code Configurator.
Figure 5-140. Open MCC
To keep the build configuration
clean, it is suggested to create at least two project configurations for your
application. One that can be used to prototype your application functionality
(default) and another configuration that builds that application in
the manner needed to load it through the bootloader (bootable).Figure 5-141. Open the Project
Properties
Navigate to Project Properties>Manage
Configurations...
With the default configuration highlighted, click the
Duplicate button, and then Rename the new configuration to
bootable’. Select OK to save the name.
Figure 5-142. Create the New
‘bootable’ Configuration
Set the bootable configuration as the active project by clicking
Set Active and then click OK to close the menu.
Figure 5-143. Activate the
‘bootable’ Configuration
Configure the on-board LED by setting RC6 as an output using the Pin Grid
View. Set the Custom Name to LED.
Figure 5-144. LED Configuration
Configure the on-board switch by setting RC7 as an input using the Pin
Grid View. Set the Custom Name to BTN and enable Weak
Pull-up.
Figure 5-145. Switch
Configuration
Add the Delay driver from
Device Resources>Drivers>Timer to the project.
Figure 5-146. Delay
Driver
Add the RSTCTRL driver from Device
Resources>System>RSTCTRL to the project.Figure 5-147. RSTCTRL Driver
It is suggested to match the Configuration Bits and Clock settings in both the
application and bootloader project. This will make the process of merging the
functionality of both projects easier.
Select the Configuration Bits
module and set the BOOTSIZE FUSE to the same value that has been set in the
bootloader.
Figure 5-148. Set the Application
Configuration Bits
Figure 5-149. Set the Application
Clock Settings
Press the Generate button in the Project Resources tab to
generate the project code.
Tip: Matching
the Configuration Bits and Clock settings in the application and
bootloader will make merging the two applications easier.
Figure 5-150. Generate
Application Code
For a blinking LED application, add the following code to
main.c in Source Files under the project folder. The
delay.h and the rstctrl.h header file
must also be included in the main
file.
// if the BTN is pressed, reset the device.if (BTN_GetValue() == 0U)
{
RSTCTRL_reset();
}
LED_Toggle();
DELAY_milliseconds(200U);
Figure 5-151. Update the
Application Code
Add the following code at the top
of the main.c file before the main function. This reserves the
last bytes to hold the Checksum value. The address presented below in the
__at() is PROGMEM_SIZE - 2, since the Checksum hash size
used is two bytes.
Note: For Checksum, CRC16 and Check State Flag
verification schemes, the hash size is two bytes. For CRC32, the hash
size is four bytes.
For AVR devices, a batch file execution is enabled in the Project Properties
to fill the empty spaces in the application project with a given sequence.
Follow the steps below to create the batch file.
Right click the
Important Files>New>Empty File....
Figure 5-153. New Empty
File
In the File Name text
box, enter postBuild.bat as the file name and click
Finish.
: If you are
using a Mac or Linux operating system, you will need to create this
file using the .sh file extension.
Figure 5-154. New
postBuild.bat File
Add the following code to
the postBuild.bat or postBuild.sh file
to configure the application verification based on the verification
scheme selected.
CAUTION: The following
example code blocks will demonstrate the post build operation for
Checksum verification where the start address is 0x2000 and the end
of the application space is 0x1FFFF. You may need to change these
values.
Windows Users
(postBuild.bat)
REM ----------------------------------------------------------------------------
REM - This file will be used to fill the application hex file with empty data
REM - and then calculate the required checksum on the application code.
REM - Script Arguments:
REM - %1 - Debug mode flag. 'true' when building in debug mode.
REM - %2 - Application project name.
REM - %3 - Path to the build folder.
REM - %4 - Image type being built.
REM ----------------------------------------------------------------------------
REM - create variables from given arguments
set isDebug=%1
set appHexFile=%3\%2.X.%4.hex
if %isDebug% == false (
REM - Fill the empty application data
hexmate r0-FFFFFFFF,%appHexFile% -O%appHexFile% -FILL=w1:0xFF@0x2000:0x1FFFF
REM - Calculate the checksum over the application space
hexmate %appHexFile% -O%appHexFile% +-CK=2000-1FFFD@1FFFEg2w-2
) else (
REM - When building in debug mode we cannot run hexmate operations
echo "Warning - Post Build Process was Skipped."
echo "Warning - Application is being built in Debug mode."
)
Mac or Linux (postBuild.sh)
# ---------------------------------------------------------------------------- # - This file will be used to fill the application hex file with empty data# - and then calculate the required checksum on the application code.# - Script Arguments:# - $1 - Debug mode flag. 'true' when building in debug mode.# - $2 - Application project name.# - $3 - Path to the build folder.# - $4 - Image type being built.# ----------------------------------------------------------------------------
isDebug=$1
appHexFile=$3\$2.X.$4.hex
if [ $isDebug = false ]; then# - Fill the empty application data
hexmate r0-FFFFFFFF,$appHexFile -O$appHexFile -FILL=w1:0xFF@0x2000:0x1FFFF# - Calculate the checksum over the application space
hexmate $appHexFile -O$appHexFile +-CK=2000-1FFFD@1FFFEg2w-2else# - When building in debug mode we cannot run hexmate operationsecho"Warning - Post Build Process was Skipped."echo"Warning - Application is being built in Debug mode."fi
Reset
Vector and Status Byte verification do not
require any of the below calculation commands. However, fill
the empty application space in the hex file.
For the other verification schemes, perform the required
calculations on the application data by referring to the
table below.
CAUTION: The
following example code blocks will demonstrate the post
build operation for Checksum verification where the
start address is 0x2000 and the end of the application
space is 0x1FFFF. You may need to change these
values.
: When
using Mac or Linux, you will need to use the argument
format `$X` instead of `%X%` in the above
commands.
Figure 5-155. postBuild.bat or
postBuild.sh File
With the postBuild script
created, open Project Properties>Building. Enable the
“Execute this line after build” checkbox. Add the following lines in the
text box below it.
Configure the project properties. Go to File>Project
Properties and select AVR128DA48 Curiosity Nano under Connected
Hardware Tool, DFP version under Packs and the XC8 or GCC version under
Compiler Toolchain.
Figure 5-157. Setup Project
Properties
Linker and Compiler setting will
be divided by the compiler being used.
AVR - XC8 Compiler
Open
Project Properties>XC8 Global Options>XC8
Linker>Additional Options. In the “Extra linker options”
text box, add
In XC8 Compiler-Preprocessing and messages, make sure to
enable “Use CCI Syntax”.
Figure 5-159. XC8 Compiler
Settings - Preprocessing and Messages In XC8 Global Options>Global options, make sure to
enable “Enabled access to const variables directly from flash”.Figure 5-160. XC8 Compiler
Settings - Global Options
AVR - GCC Compiler
Open Project
Properties>Avr GCC (Global Options)>avr-ld>Memory
Settings. In the “FLASH segment” text box, add
.text=0x<Application Start Address in WORDS>.
Open Project Properties>Avr GCC (Global
Options)>avr-ld>Memory Settings. In the “FLASH
segment” text box, add application_footer=0x<Application Footer
Address in WORDS>.
Click Clean and Build to the generate the project hex file.
Figure 5-163. Clean and Build
Image File Generation
With the bootloader client and the application projects created, the image file
can be generated by using the Firmware Image Builder ( pyfwimagebuilder)
tool by combining the bootloader_configuration.toml file and
the application hex file.
Note: The toml file will be inside Booloader
Project Folder>mcc_generated
files>bootloader>configurations, and the hex file is
present at Application Project
Folder>dist>default>production.