4.2 PIC18F57Q43 Bootloader Example
This example shows how to configure a generic bootloader for PIC devices that uses UART for
communication.
Description: This section details a full standard bootloader use case on the PIC18F57Q43 microcontroller, which applies to many variants of the PIC18F family and can also be extrapolated to other 8-bit PIC chips as well. This example uses the PIC18F57Q43 Curiosity Nano board purchased from Microchip. Refer to the device schematics for configuration information.
Example: See how to bootload a program that blinks the LED of the PIC18F family of chips.
-
Click the New Project button in the top left of the menu.
-
Select Standalone Project and then click the Next.
-
Use PIC18F57Q43 for this example. Type the device name in the “Device” field and MPLAB X IDE will display the options.
-
Select a tool for programming from the drop-down menu and click Next. The PIC18F57Q43 Curiosity Nano board will be used for this example as well.
Note: If only “Simulator” is displayed, then the target board is not plugged into the computer or it is not communicating with MPLAB X IDE properly. -
This example uses the XC8 compiler v2.40. Click Next after selecting the compiler.
-
Type the desired project name into the “Project Name” field. Click Next. Make sure to give a descriptive name so that the project can be easily identified.
-
Click the MCC button in the menu bar to start the Code Configurator.
-
Click the Select MCC Melody button.
-
Use the Optional Content tab to install optional libraries, such as the bootloader. If no other libraries need to be installed, click Finish.
-
In previous versions of MCC, a user needed to manually attach all peripheral drivers required by their library, using the Device Resources tab. The new MCC Melody allows users to select the library needed for the project. Melody brings in all the dependencies and configures them.
-
Confirm that the bootloader module has been included into the Builder window. Builder UI reflects all the required peripheral driver dependencies such as the Memory or NVM, UART and Timer Delay modules and links them to the Bootloader library.
-
One of the modules that cannot be configured automatically is the communication driver. For the bootloader to communicate with the Bootloader Host Application, a communication driver needs to be exposed to push data through the serial ports on the target device. To understand which communication ports are required to be opened for the device in use, check the schematics and look for the Serial or CDC Ports definition for the device, as shown in the image below. Using this definition, MCU is communicating through the RF0 port for transmitting data serially and the RF1 port for receiving data serially.
A communication driver version that communicates through the RF0 and RF1 ports is needed. For workflows on the PIC18F57Q43 Curiosity Nano, attach the UART1 driver as that is the UART driver that can communicate through those ports. Select UART1 from the “UART PLIB Selector” in the UART(None) tab. The UART1 settings will open up.
Note: Some devices will automatically select the required UART pins. Open the Pin Grid View tab to make sure the pins are enabled. -
In the System/Clock Control Module, configure the oscillator and clock divider. In general, faster is better for more reliable communication. For this PIC18F57Q43 example, 64 MHz internal oscillator is selected with Clock Divider set to 1.
-
There are several advantages of configuring I/O pins, but the most important one is receiving a state back from the MCU based on the LED status. For example, the bootloader module will light up the LED when in a Boot state and it will turn off when the application state takes over. This helps to track the program flow during the development process. Telling the Bootloader module to utilize the I/O Indicator pin sets up the functionality. This is done by clicking on the switch that activates the I/O Pin Indicator. The I/O Entry Pin can also be configured to force the MCU into a Bootloader state by checking for voltage levels on the entry pin at any device Reset. To activate this pin, select the UI switch from the Bootloader8-bit tab.
Check the schematics to configure the I/O pin for using the LED on the board. Find the I/O interface declaration, similar to the image below. For the PIC18F57Q43, make sure the RF3 port is connected to control the LED.
Go back to MPLAB X IDE and navigate to the Pin Grid View tab 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. -
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.
-
For the notification “Boot End Address cannot be set to 0x0”, set the memory offset/boot end address. The minimum required offset for PIC18F devices is 0x3000. Make sure to enable the 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.
-
Now with the basic configurations complete, click the Generate button in Project Resources tab to generate the project code.
-
After generation is complete, click the “Clean and Build” icon drop-down option and select Clean and Build Main Project to compile the bootloader code.
-
Click Make and Program Device Main Project to program the device.
Note: The offset value set in the bootloader will be reflected in the project linker settings. Open the Project Properties window: this can be done by right clicking a project and selecting Properties. Go to XC8 Linker, under the XC8 global options. Select “Memory Model” under the Option categories drop-down menu. For example, entering 0x3000 would restrict the generated code to the first 0x3000 bytes of ROM and can be seen in ROM ranges as 0-0x2fff. Refer to the code size for appropriate ROM reservation size. To best understand the required ROM range, after successful build of the Bootloader project, refer to the PIC Memory Views>Program Memory or the Window>Dashboard, Memory section.
-
Create a new Standalone project and set it as the main project in MPLAB.
- Open MCC Melody.
-
Configure the on-board LED by setting the RF3 pin.
-
Add the Delay timer to the project resources by double clicking it under Project Resources>Drivers>Timer>Delay. This is used to add delay for LED toggling.
-
Press the Generate button in the Project Resources tab to generate the project code.
-
For a blinking LED application, add the following code to
main.c
in Source Files under the project folder. Thedelay.h
header file must also be included in the main file. -
Create a
certificate.c
file in the Source Folder. Right click the Source Folder, select New>Other. -
In the New File window, select C under “Categories” and then select C Source File under “File Types”. Click Next.
-
Provide the file name “certificate” and click Finish.
-
Add the code in the following image to the newly created
certificate.c
file. This reserves the last four bytes of the Flash memory and writes them with 0xFFFFFFFF. This is later updated with the correct Checksum or CRC value calculated post build. -
Configure the project properties. Go to File>Project Properties and select PIC18F57Q43 Curiosity Nano under “Connected Hardware Tool”, DFP version under “Packs” and the XC8 version under “Compiler Toolchain”.
-
Rename the default configuration with the verification scheme used. For example, Checksum. Click Manage Configuration>Rename.
-
Multiple different configurations for the different verification schemes can be created by using the “Duplicate” option.
-
Click a configuration name and select the Set Active button to set it as the active configuration for the current build.
- For the end application project, configure the linker setting for the verification scheme that is decided to be used. Check out the Compiler and Linker Settings section below for the details.
- Click Clean and Build 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 the 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 two bytes and it will occupy the last two bytes of the Program Memory. For CRC32, the CRC value is four bytes and for CRC16, the CRC value is two bytes.
- 1FFFE → Checksum value will be stored here for two bytes
- Algorithm → Checksum verification schemes algorithm value
- Polynomial → Hexadecimal value used when calculating CRC. Refer to the Verification Schemes section for more information.
Linker Additional options setting for the verification schemes:
- CRC16 → 3000-1FFFD@1FFFE,width=-2,algorithm=5,offset=FFFF,polynomial=1021
- CRC32 → 3000-1FFFB@1FFFC,width=-4,algorithm=-5,offset=FFFFFFFF,polynomial=04C11DB7
- Checksum → 3000-1FFFD@1FFFE,width=-2,algorithm=2
UBHA End Application Programming
-
Open the Unified Bootloader Host Application and select the device architecture to bootload.
-
Pick the device family (PIC16 or PIC18) from the drop-down menu.
-
Click Settings>Serial to configure the COM port.
-
Load the End application hex file by selecting File>Open/Load File. The hex file will be present at
Application Project Folder\dist\verification_scheme\production
. -
Set “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.
-
Click the Program Device button. Once the device is programmed, the bootloader will disconnect from the COM port. Open Tools>Console to view the output/status.