DEPRECTION NOTICE:This repo and associated examples are deprecated.

Please refer to the latest examples at:
https://github.com/microchip-pic-avr-examples/dspic33_dsc_bootloader_code_examples

Bootloader Example Using the CAN Physical Layer

Introduction

This example shows how to use the pre-configured CAN demo on the dsPIC33CK256MP508 processor module for the Explorer 16/32 board. This demo uses the ISO 15765-2 CAN Transport Protocol layer to transport custom bootloader commands over the CAN bus as seen in the diagram below:

Tools

Software

  1. The unzipped example project files, boot.X and app.X
  2. Microchip MPLAB® X version 5.40 or later
  3. Microchip MPLAB® XC16 C compiler version 1.60 or later
  4. Unified Bootloader Host Application v1.17.0 or later
  5. Microchip MPLAB® Code Configurator version v4.0.1 or later (optional – for code configuration)
  6. 16-bit Bootloader MCC module version 1.18.3 or later (optional – for code configuration)
  7. The drivers for the selected Peak CAN protocol analyzer/protocol generator (available at https://www.peak-system.com/Home.59.0.html?&L=1)

Hardware

  1. Explorer 16/32 Demo board
  2. dsPIC33CK256MP508 Processor module
  3. A programmer/debugger of choice
  4. A Peak CAN protocol analyzer/protocol generator. A PCAN-USB FD was used for development of this demo (https://www.peak-system.com/PCAN-USB-FD.365.0.html?&L=1).
  5. A CAN cable
  6. An MCP2542 click board (https://www.mikroe.com/mcp2542-click) or another appropriate CAN transceiver click board. It is important that this is just a transceiver and not a CAN-to-SPI/UART converter chip.
  7. A micro USB cable, a USB-Type C cable, or a 9v power adapter for the Explorer 16/32 (for powering the board).

Running the Demo

Hardware Configuration

  1. With the Explorer 16/32 detached from any power source, programmer, or USB connection:
    1. Connect the dsPIC33CK256MP508 processor module into the socket on the Explorer 16/32 such that the notched edge is in the top left corner matching the silk screen on the Explorer 16/32
    2. Connect the MCP2542 daughter board into the mikroBUS™ A slot.
  2. Connect the programmer to the Explorer 16/32
  3. Connect the Peak analyzer/generator to your computer.
  4. Connect the Peak analyzer/generator to the MCP2543 daughter board with the CAN cable.
  5. Power the board using either USB cable or the 9 volt adapter plug.


Programming the Bootloader

  1. Open the boot.x project in MPLAB® X
  2. Press the “Make and Program” button on the top bar.


  3. Select the appropriate programmer if prompted.
  4. The project should compile and program successfully.
  5. Verify the D3 LED is solid on the Explorer 16/32. This indicates the bootloader is running.

Building the Application

  1. Open the app.X project in MPLAB® X
  2. Press the “Clean and Build Project” button on the top bar. NOTE: Make sure not to hit the program button. This will program the application code over the bootloader that was just programmed.

  3. The project should compile cleanly. app.X/dist/default/production/app.X.production.hex should be generated.
  4. Verify that the D3 LED is still solid.
    1. If the D3 LED is blinking instead of D3 solid, then the application code was programmed instead of only compiled. Go back to the “Programming the Bootloader” stage and re-program the bootloader.

Loading the Application

  1. Open the Universal Bootloader Host Application tool (UBHA)


  2. Select the “PIC24/dsPIC” option from the “Device Architecture” selection drop down


  3. Select “CAN” from the protocol drop down selection box:


  4. Select the “Settings->CAN” option from the top menu


  5. Select the Peak protocol analyzer being used and the appropriate CAN configuration settings and press “Apply” when done. By default, the configuration for the demo are the following:
    1. Nominal bit rate: 125 kbits/sec
    2. Host to device ID: 0xA1
    3. Device to host ID: 0xA2


  6. Press the “Read Device Settings” button.
    1. The Application start address and Application end address fields should have updated. If it did not or if you get a communication error, please go back to the “Programming the Bootloader” stage to make sure the bootloader was programmed correctly.


  7. Load the application hex file by selecting “File->Open/Load File (*.hex)”
    1. Select the file generated in the previous section: app.X/dist/default/production/app.X.production.hex


  8. Press “Program Device”. The application should program erase, program and readback verify correctly.
    1. After a few seconds, D3 should be blinking.

Customizing the Bootloader Project

The bootloader project can be opened in MCC and new modules can be added and configured as they normally would in MCC. Additionally, the parameters in the Bootloader:Bootloader module can be re-configured.

Changing the CAN bitrate

The CAN bitrate of the bootloader can be easily changed. By default the bitrate is set to 125 kbits/second. To change the bitrate, do the following:
  1. Open the bootloader project.
  2. Open the MCC plug-in by clicking on the MCC icon in the tool bar of MPLAB X.


  3. Select the CAN1 module from the project resources pane on the left navigation pane. This will open the CAN1 module settings in the main configuration window.


  4. Expand the "Step 2: Nominal Bit Rate Settings" pane and make the desired bit rate changes.


  5. Re-generate the project by pressing the generate button in the project resources pane.


  6. Re-build/program the bootloader into the board.


  7. Re-open the settings window in the UBHA program.


  8. Change the nominal bit rate to match what was selected in MCC. NOTE: The Peak analyzer/generator is limited to what bit rates it can generate so not all bit rates possible by the micro-controller can be realized by this hardware generator. You are limited to the bit rates listed in the UBHA selection window.
    1. When the bit rate has been changed, press apply.


  9. You can now press the program button or read device settings button as before and the device should now be communicating using the new bit rate selected.

Changing the Host to Device ID

This is changing the message ID of the messages sent from the PC bootloader host to the target board. Currently the same message ID must be used for both ISO 15765-2 message and flow control frames. To change the message ID used from host to device, do the following:
  1. Open the bootloader project.
  2. Open the MCC plug-in by clicking on the MCC icon in the tool bar of MPLAB X.


  3. Select the CAN1 module from the project resources pane on the left navigation pane. This will open the CAN1 module settings in the main configuration window.


  4. Expand the "Step 5: Filter Object Settings" pane.


  5. Change the message ID in the Message ID section to the desired value. In this example we changed it from 0xA1 to 0xA5.


  6. Re-generate the project by pressing the generate button in the project resources pane.


  7. Re-build/program the bootloader into the board.


  8. Re-open the settings window in the UBHA program.


  9. Change the "Host to Device ID" field to match the value used in the MCC filter object settings, in this example 0xA5.


    1. When the message ID has been changed, press apply.
  10. You can now press the program button or read device settings button as before and the device should now be communcicating using the new message IDs from the host to the device.

Changing the Device To Host ID

This is changing the message ID of the messages sent from the the target board back to the PC host. Currently the same message ID must be used for both ISO 15765-2 message and flow control frames. To change the message ID used from device to host, do the following:
  1. Open the bootloader project.
  2. Open the can_tp_config.h file in the "Headers/MCC Generated Files" section of the MPLAB X project.


  3. Change the values for the CAN_TP_MESSAGE_ID_DATA and CAN_TP_MESSAGE_ID_FLOW_CONTROL to the desired value. In this example it is changed from 0xA2 to 0xA4.
    NOTE: Due to a constraint in the UBHA hos program, the same message ID must be used for both the data and flow control messages. Please make these two values the same.


  4. Re-build/program the bootloader into the board.


  5. Re-open the settings window in the UBHA program.


  6. Change the "Device To Host ID" field to match the value used in can_tp_config.h file, in this example 0xA4.


    1. When the message ID has been changed, press apply.
  7. You can now press the program button or read device settings button as before and the device should now be communicating using the new message IDs from the device to the host.

Using Extended (29-bit) CAN IDs

The ISO 15765-2 specification says that the communication pair should use the same format IDs when talking to each other. Thus we need to change settings in both the host application and the firmware to add support for extended IDs.
  1. Open the bootloader project.
  2. Open the can_tp_config.h file in the "Headers/MCC Generated Files" section of the MPLAB X project.


  3. Change the value CAN_TP_MESSAGE_ID_IS_EXTENDED from false to true. If this definition doesn't exist, define it and set the value to true. This will change all the messages from the device to the host to extended ID messages.

  4. Open the MCC plug-in by clicking on the MCC icon in the tool bar of MPLAB X.


  5. Select the CAN1 module from the project resources pane on the left navigation pane. This will open the CAN1 module settings in the main configuration window.


  6. Expand the "Step 5: Filter Object Settings" pane.


  7. Modify the filter from "0xA1" to "0xA1x". The trailing "x" indicates that this is an extended address.


  8. Re-generate the project by pressing the generate button in the project resources pane.


  9. Re-build/program the bootloader into the board.


  10. Re-open the settings window in the UBHA program.


  11. Change the "Message Format" field to extended (29-bit) mode.


    1. When the mode had been changed, press apply.
  12. You can now press the program button or read device settings button as before and the device should now be communicating using the new message IDs from the device to the host.

Customizing the Application Project

The application MCC configuration can be opened and modules can be added or modified as desired.

Known Limitations

Terminology

ISO 15765-2 / CAN-TP / ISO-TP

ISO 15765-2 is a transport protocol layer designed for usage with the CAN bus. The original CAN bus specification allows for up to 8 bytes of data to be in a single frame. Larger messages need to be fragmented into smaller frames for transmission on the CAN bus. ISO 15765-2 defines a protocol for fragmenting larger messages and sending them over the CAN bus. It also incorporates flexibility in handling the data transmission through the use of flow control packets to control the frequency of when packets arrive as well allowing to pause the transmission of a message until the receiver is ready to receive the message.

MCC

Microchip’s MPLAB® Code Configurator tool. This tool helps configure and generate source code for peripherals and libraries.

UBHA - Unified Bootloader Host Application

The PC application used to send the firmware update and verification commands from the PC to the board to complete the firmware update.

Trademarks

MPLAB® is a registered trademark of Microchip Technology Inc. All other trademarks are the property of their respective owner.