4.2 PIC18F57Q43 Bootloader Example
This example shows how to configure a generic bootloader for PIC devices that uses UART for
communication.
Summary
This section details a full standard bootloader use case on the PIC18F57Q43 microcontroller. This example applies to many variants of the PIC18F family and may also be extrapolated to other 8-bit PIC chips as well. This example uses the PIC18F57Q43 Curiosity Nano Board purchased from Microchip. Visit the device schematics for configuration information.
Example
This example shows how to bootload a program that blinks the LED of the PIC18F family of chips.
Create New Project
Click the New Project icon in the top left of the menu.
Select Standalone
Select Standalone Project and then click the Next.
Setup Target Device
PIC18F57Q43 is used for this example. Type the name of the device into the Device field and MPLAB® X IDE will display the options.
Select a tool for programming from the drop down list and click Next. The PIC18F57Q43 Curiosity Nano Board will be used for this example as well.
Select a Compiler
Project Name
Activate MCC
Click on New MCC Melody button for this example.
Use the Optional Content tab to install optional libraries (such as the bootloader). If no other libraries need to be installed, click Finish.
Attach the 8-bit Bootloader Library
Confirm that the bootloader module has been included into the Builder window. Builder UI should reflect all the required peripheral driver dependencies such as Memory or NVM module, the UART module, and the timer Delay module and link them to the Bootloader library.
Attach the UART Driver
Check the schematics to configure the I/O pin for using the LED on the board. In the schematics there is an I/O interface declaration, similar to the image below. For the PIC18F57Q43, RF3 port needs to be connected to control the LED.
Go back to MPLAB® X IDE and navigate to the Pin Grid View in the bottom center of the screen. Then select the RF3 pin for the BOOT INDICATE pin in the grid.
Note: For further configuration, access the Pins Module UI through: Project Resources > System > Pins.
Notifications
Before generating the initial code, address the warnings in the Notifications[MCC] tab. For example, NVM options must be enabled. Addressing the notification will either turn it into a hint or remove it from the list.
For example, enable Generate Device ID APIs for the NVM Driver. Addressing the notification will either turn it into a hint or remove it from the list.
Boot End Address
For the notification "Boot End Address cannot be set to 0x0", memory offset/boot end address must be set.The minimum required offset for PIC18F devices is 0x3000. Make sure to enable Support Checksum option for all the verification schemes while using PIC devices. The calculated checksum is then checked against the checksum value generated by UBHA to ensure proper communication.
Code Generation and Programming
End Application Project
With the bootloader code is built and configured to 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’s configuration. The first step for completing this task is to create a Blinking LED Project.
1. Create a new Standalone project and set it as the main project in MPLAB®.
2. Open MCC Melody.
3. Configure the on-board LED by setting the RF3 pin.
4. Add the Delay timer to the project resources by double clicking on it under Device Resources/Drivers/Timer/DELAY. This is used to add delay for LED toggling.
5. Press "Generate code" near Project Resources to generate the project code.
6. For a blinking LED application, add the following code to main.c in Source Files under the project folder. The delay.h header file must also be included in the main file.
7. A certificate.c file must be created in the Source Folder. Right click on Source Folder, select New->Other.
8. In the New File window, select C under Categories and then select C Source File under File Types. Click Next.
9. Provide the filename certificate and click Finish.
10. Add the code in the following image to the newly created certificate.c file. This reserves the last 4bytes of the Flash memory and writes them with 0xFFFFFFFF. This is later updated with the correct Checksum or CRC value calculated post build.
11. Next step is to configure the project properties. This can be opened by selecting "File->Project Properties". Select "PIC18F57Q43 Curiosity Nano" under Connected Hardware Tool, DFP version under Packs and the XC8 version under compiler toolchain.
12. Rename the default configuration with the verification scheme used. For example, Checksum. Click on Manage Configuration->Rename.
13. Multiple different configurations for the different verification schemes can be created by using the 'Duplicate' option.
14. Click on a configuration name and select 'Set Active' to set it as the active configuration for the current build.
15. For the end application project, configure the linker setting for the verification scheme that is decided to be used. Checkout the Compiler and Linker Settings section for the details.
16. Clean and Build the project to the generate the project hex file.
Compiler and Linker Settings
The following section is intended to provide an explanation of the compiler and linker settings utilized in the PIC18F57Q43 end application project settings. The example below uses Checksum verification scheme for demonstration.
- Linker>Additional Options
- Codeoffset = 3000h
- Checksum = 3000-1FFFD@1FFFE,width=-2,algorithm=2
Here,
- 3000 -> Bootloader offset value
- 1FFFD -> Program Memory Size
- Width -> Width of Checksum - This value is used because the checksum calculated is 2 bytes and it will occupy the last 2bytes of the Program Memory. For CRC32, CRC value is 4 bytes and CRC16, CRC value is 2 bytes.
- 1FFFE -> Checksum value will be stored here for 2bytes
- algorithm -> Checksum verification schemes algorithm value
- polynomial -> Hexadecimal value used when calculating CRC. For more information, refer the Melody 8-bit Bootloader_VerificationSchemas section.
Linker Additional options setting for the verification schemes:
a) CRC16 -> 3000-1FFFD@1FFFE,width=-2,algorithm=5,offset=FFFF,polynomial=1021
b) CRC32 -> 3000-1FFFB@1FFFC,width=-4,algorithm=-5,offset=FFFFFFFF,polynomial=04C11DB7
c) Checksum -> 3000-1FFFD@1FFFE,width=-2,algorithm=2
UBHA End Application Programming
1. Open the Unified Bootloader Host Application and select the device architecture to bootlload.
2. Pick the device family (PIC16 or PIC18) from the drop-down menu.
3. Click on Settings ->Serial to configure the COM port.
4. Load the End application Hex file by selecting File->Open/Load File. Hex file will be present at “Application Project Folder\dist\verification_scheme\production”
5. Set the program memory size depending on the target device. For PIC18F57Q43, the program memory size is 0x20000. Enter the offset programmed previously in the bootloader project, i.e. 0x3000. The size of every location depends on the target device. Some devices have word-addressable Flash and others have it byte-addressable. For PIC16 devices, convert the words into bytes and then use for configuration in UBHA.
6. Click on the Program Device button. Once the device is programmed, the bootloader will disconnect from the COM port. Open Tools->Console to view output/status.