USB Bootloader PDF Print E-mail
User Rating: / 23
PoorBest 
Written by Mat   
Monday, 19 June 2006

This Article is intended as an extension to the basic USB Article, and disscusses a method for allowing a developer to access the Microchip USB bootloader in their own PC applications, giving the end user the ability to update the firmware on the PIC18F4550 without the need for a programmer.

Please note all code is released AS-IS and should used at the users own risk, no guarantee is made by PICcoder.co.uk or the author with regards to the accuracy of the diagrams or code contained in this article.

Discuss this article on the forums. (27 posts)

OverviewBootloader Example Application

In order to implement a bootloader, and allow the user to upload code onto PIC, two sections of code must be developed, the PIC firmware and a PC application to download the firmware. Microchip have already provided the necessary PIC code to create a USB bootloader, and therefore this has been used for the basis for this article, as the code is well tested and there is little point reinventing the wheel. However one of the main problems with the Microchip Bootloader is the lack of support for including bootloader functionality into a PC application, and therefore a DLL has been written to address this.

PIC Code

It is necessary to program the PIC first using a traditional programmer, to provide the required USB functionality and routines necessary to update the PIC firmware. If you are using a PICDEM FS USB then this should come preloaded with the bootloader, and it will not be necessary program it.

The require hex file can be downloaded below, or found in the standard Microchip USB firmware download.  

bootloader.hex bootloader.hex (5.63 KB)

 If you compile your own copy of the firmware please be aware that a full copy of C18 is required, as without optimisations turned on the bootloader will extend passed the address 0x0800, and therefore will corrupt its self when the firmware is updated via the USB.

Example PC Code

The PC code consists of two sections, a DLL and an example application both written in C# .NET, they can be downloaded via the supplied link below and compiled using VS.NET 2003 or Visual C# Express 2005 (available for free on the Microsoft website)

PIC USB Bootloader Example.zip PIC USB Bootloader Example.zip (655.90 KB) 

This code may only be used for individuals and hobby use, and should not be used for commercial purposes. If you wish to use this code in a commercial enviroment then please contact me for a commercial version. 

By incorporating all of the complex code in a DLL this can be included in any windows application and therefore only a simple interface is require to the main application, as can be seen by the example application. Rather than trying to explain in detail how to program the example application is it probably far simpler for you to examine the code contained in form1.cs, as there is not much to it!

In order to use the bootloader to download a hex file to the PIC it is necessary to first enter bootloader mode by restarting the PIC whilst holding down SW2, this will cause the PIC to execute the USB bootloader code and the PC to prompt for the required drivers, which may be found in the standard USB firmware download provided by microchip.

Once bootloader mode has been sucessfully entered, downloading a hex file to the PIC should be relatively simple, the hex file should be loaded by calling the LoadFromHEX(String FileName) function in the instantiated Bootloader object to load the hex file into the application. Then ProgramDevice(), to download the new firmware. The new firmware may then be executed by calling the Execute() function.

To download the PIC firmware, it is necessary to call the ReadDevice() function to download the hex file from the PIC, followed by SaveToHexFile(String FileName) to save the firmware to a hex file.

It should never be necessary to call the Erase() function whilst programming the device, however this funcitonality has been included incase the developer wishes to clear the firmware (excluding bootloader of course!)

Conclusion

This article has introduced a simple method of  including the ability to update the firmware of a PIC18F4550 in end-user applications. Several extensions to the project have been suggested such as the ability to encrypt hex files and allow the bootloader to decrypt them, safe guarding the firmware, or adding the ability to cope with multiple devices connected simultaneously. If you are interested in any further features please let me know. Also please do not use this code for commercial purposes, if you would like to then please contact me and we can sort something out (I'll even remove the nag screen and add some new features )

 Mat Clayton

Discuss this article on the forums. (27 posts)

Last Updated ( Monday, 19 June 2006 )
 
< Prev   Next >