4.2 PIC18F57Q43 Bootloader Example

This example shows how to configure a generic bootloader for PIC devices that uses UART for communication.

Note: When adding a bootloader to an existing application, make sure to select a device that has sufficient memory to hold both the bootloader and the application.

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.

  1. Click the New Project button in the top left of the menu.

    Figure 4-60. New Project Icon
  2. Select Standalone Project and then click the Next.

    Figure 4-61. Select Standalone Project
  3. Use PIC18F57Q43 for this example. Type the device name in the “Device” field and MPLAB X IDE will display the options.

    Figure 4-62. Select Device
  4. 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.
    Figure 4-63. Select Tool
  5. This example uses the XC8 compiler v2.40. Click Next after selecting the compiler.

    Figure 4-64. Select Compiler
  6. 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.

    Figure 4-65. Select Project Name and Folder
  7. Click the MCC button in the menu bar to start the Code Configurator.

    Figure 4-66. Open MCC
  8. Click the Select MCC Melody button.

    Figure 4-67. Select MCC Melody
  9. Use the Optional Content tab to install optional libraries, such as the bootloader. If no other libraries need to be installed, click Finish.

    Figure 4-68. Optional Content Tab
  10. 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.

    Figure 4-69. Device Resources
  11. 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.

    Figure 4-70. Builder UI
  12. 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.

    Figure 4-71. Serial/CDC Port

    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.

    Figure 4-72. UART PLIB Selector
    Figure 4-73. UART1 Settings
    Note: Some devices will automatically select the required UART pins. Open the Pin Grid View tab to make sure the pins are enabled.
  13. 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.

    Figure 4-74. Clock Control Settings
  14. 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.

    Figure 4-75. I/O Pin Indicator

    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.

    Figure 4-76. On-Board Peripherals

    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.

    Figure 4-77. Pin Grid View
    Note: For further configuration, access the Pins Module UI through Project Resources> System>Pins.
  15. 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.

    Figure 4-78. Notification [MCC] Tab

    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.

    Figure 4-79. Clear NVM Notification
  16. 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.

    Figure 4-80. Boot End Address
  17. Now with the basic configurations complete, click the Generate button in Project Resources tab to generate the project code.

    Figure 4-81. Generate Project Code
  18. 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.

    Figure 4-82. Clean and Build Main Project
  19. Click Make and Program Device Main Project to program the device.

    Figure 4-83. Make and Program Device Main Project
    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.
    Figure 4-84. Linker Settings - Memory Model

End Application Project

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’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.

    Figure 4-85. New Application Project
  2. Open MCC Melody.
  3. Configure the on-board LED by setting the RF3 pin.

    Figure 4-86. LED Configuration
  4. 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.

    Figure 4-87. Delay Timer
  5. Press the Generate button in the Project Resources tab to generate the project code.

    Figure 4-88. Generate Application 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.

    Figure 4-89. Blinking LED Code
  7. Create a certificate.c file in the Source Folder. Right click the Source Folder, select New>Other.

    Figure 4-90. New Source File
  8. In the New File window, select C under “Categories” and then select C Source File under “File Types”. Click Next.

    Figure 4-91. C Source File
  9. Provide the file name “certificate” and click Finish.

    Figure 4-92. New File Name
  10. 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.

    Figure 4-93. Certificate File
  11. 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”.

    Figure 4-94. Set Project Properties
  12. Rename the default configuration with the verification scheme used. For example, Checksum. Click Manage Configuration>Rename.

    Figure 4-95. Create New Configuration
  13. Multiple different configurations for the different verification schemes can be created by using the “Duplicate” option.

    Figure 4-96. Duplicate Configuration
  14. Click a configuration name and select the Set Active button to set it as the active configuration for the current build.

    Figure 4-97. Set Active - New Configuration
  15. 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.
  16. 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
Figure 4-98. Project Properties - Checksum Configuration

UBHA End Application Programming

  1. Open the Unified Bootloader Host Application and select the device architecture to bootload.

    Figure 4-99. UBHA Device Architecture
  2. Pick the device family (PIC16 or PIC18) from the drop-down menu.

    Figure 4-100. UBHA 8-Bit Architecture
  3. Click Settings>Serial to configure the COM port.

    Figure 4-101. Serial Port Settings
  4. 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.

    Figure 4-102. Load Application Hex File
  5. 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.

    Figure 4-103. UBHA Memory Settings
  6. 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.

    Figure 4-104. UBHA Program Device