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.

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.

Select a Compiler

In this example XC8 compiler v2.40 will be used. Click Nextafter selecting the compiler.

Project Name

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.

Activate MCC

Click on the MCC button in the menu bar. This will start the Code Configurator.

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

In previous of MCC versions a user needed to attach all peripheral drivers required by their library manually, using the Device Resources window. 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 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

One of the modules that cannot be configured automatically is the communication driver. In order for the bootloader to communicate with the Bootloader Host Application, a communication driver needs to be exposed that pushes 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 port RF0 for transmitting data serially and port RF1 for receiving data serially.

A communication driver version that communicates through the RF0 and RF1 ports is needed. For workflows on the PIC18F57Q43 CNano, UART1 driver needs to be attached because that is the UART driver that can communicate through those ports. Select UART1 from the UART PLIB Selector. UART1 settings should open up.

Note: Some devices will automatically select the required UART pins. Open the Pin Grid view to make sure the pins are enabled.

Clock Control

In the System/Clock Control Module, configure the oscillator and clock divider. In general, faster is better for more reliable communication. For this PIC18F57Q43example, 64 MHz internal oscillator is selected with Clock Divider set to 1.

Configure I/O Indicator Pin

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. 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 bootloader tab.

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

Now with the basic configurations complete, click on the Generate button in Project Resources tab to generate the project code.

After generation is complete click Clean & Build to compile the bootloader code.

Program the device by clicking Make and Program Device Main Project.

Note: The offset value set in the bootloader will be reflected in the project linker settings. Open Project Properties window: this can be done by right clicking on a project and selecting properties. Select XC8 Linker, under the XC8 global options. Select “Memory Model” under the Option categories drop down box. 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.

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.