5.6 Creating an Application for AVR® Devices
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.
- 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.
- 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).
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.
Set the bootable configuration as the active project by clicking Set Active and then click OK to close the menu.
-
Configure the on-board LED by setting RC6 as an output using the Pin Grid View. Set the Custom Name to LED.
-
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.
- Add the Delay driver from
Device Resources>Drivers>Timer to the project.
- Add the RSTCTRL driver from Device Resources>System>RSTCTRL to the project.
- 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.
-
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.
-
For a blinking LED application, add the following code to
main.c
in Source Files under the project folder. Thedelay.h
and therstctrl.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);
- 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.Checksum Verification Scheme
#include <stdint.h> #ifdef __XC8__ #include <xc.h> #endif volatile const uint16_t #ifdef __XC8__ __at(0x1FFFE) #endif applicationFooter __attribute__((used, section("application_footer"))) = 0xFFFF;
CRC32 Verification Scheme
#include <stdint.h> #ifdef __XC8__ #include <xc.h> #endif volatile const uint32_t #ifdef __XC8__ __at(0x1FFFC) #endif applicationFooter __attribute__((used, section("application_footer"))) = 0xFFFFFFFF;
CRC16 Verification Scheme
#include <stdint.h> #ifdef __XC8__ #include <xc.h> #endif volatile const uint16_t #ifdef __XC8__ __at(0x1FFFE) #endif applicationFooter __attribute__((used, section("application_footer"))) = 0xFFFF;
Check State Flag Verification Scheme
#include <stdint.h> #ifdef __XC8__ #include <xc.h> #endif volatile const uint8_t #ifdef __XC8__ __at(0x1FFFE) #endif applicationFooter __attribute__((used, section("application_footer"))) = 0x55;
-
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....
- 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. - Add the following code to
the
postBuild.bat
orpostBuild.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-2 else # - 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." 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.
Table 5-2. Verification Calculation Commands Verification Scheme Hexmate Command Example Checksum hexmate %appHexFile% -O%appHexFile% +-CK=2000-1FFFD@1FFFEg2w-2
CRC-16 hexmate %appHexFile% -O%appHexFile% +-CK=2000-1FFFD@1FFFE+FFFFg5w-2p1021
CRC-32 hexmate %appHexFile% -O%appHexFile% +-CK=2000-1FFFB@1FFFC+FFFFFFFFg-5w-4p04C11DB7
: When using Mac or Linux, you will need to use the argument format `$X` instead of `%X%` in the above commands.
- 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.
postBuild${ShExtension} ${IsDebug} ${ProjectName} ${ImageDir} ${IMAGE_TYPE}
- Right click the
Important Files>New>Empty File....
-
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.
- 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-Ttext=<Application Start Address> -Wl,-u,applicationFooter
In XC8 Compiler-Preprocessing and messages, make sure to enable “Use CCI Syntax”.
In XC8 Global Options>Global options, make sure to enable “Enabled access to const variables directly from flash”.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.