USB boot-loader on a PIC 18F2550

Microchip provides pretty decent datasheets for all their parts. The datasheet for the 18F2550/4550 provides good starting information for getting USB working on this chip. However, it doesn't really cover the software side very well. They do provide application notes with sample code.

I recommend downloading the Create USB Interface code from UCSB. It is a customized version of Microship's sample code for their custom CUI board. The CUI instructions, schematics, and code should be enough to get you up and running with a USB bootloader and USB firmware, after a few stumbling blocks.

The CUI firmware doesn't do anything interesting. For those looking to get something up and running quick, making your PIC look like a USB COM port is pretty handy. Microchip provides application notes for just this. Just go to their site and do a documentation search for AN956. It should work with the USB bootloader right out of the box (mostly, see notes).

General Notes

I couldn't figure out why my PIC wasn't running. The oscillator was functioning, MCLR was driven high, and VDD/VSS were hooked up. Something that didn't occur to me was the CONFIG bits. I always set them via code macros rather than at hardware programming time. The CUI and Microship code doesn't set any of them in code macros. The C18 compilers provide an easy way to do this using #pragma statements with documentation in a provided PDF.

Another issue i ran in to was the need for a capacitor on the Vusb pin in USB power mode. This shows up on the CUI schematic but I over looked it. I also can't seem to find it in the 18F2550 datasheet at all. It might be in the errata. They call for a 470nF cap, but mine works with a 2.2uF cap I had on hand.

Finally, you need to be aware about how the bootloader works. The bootloader is loaded into the first segment of memory in the PIC. It then hands off execution to your code that is stored farther down. If you write firmware without keeping this in mind, you will overwrite your firmware. Luckily, the samples mentioned handle this automatically. This little bit of magic is done in the linker file. Compare the linker file in the CUI firmware, with the appropriate linker file that comes with the C18 compiler. You should see the difference.

For the USB COM port firmware, there is a minro change to get it to work. Without this change, the device will connect and then immediately disconnect. In 'usbcfg.h', comment out:

//#define USE_SELF_POWER_SENSE_IO
//#define USE_USB_BUS_SENSE_IO

When using a bootloader, config bits are generally set when you burn the bootloader. The firmware doesn't need to worry about config bits.

18F2550 Specific Notes

In my case, I am using a PIC 18F2550. This chip is functionally the same as the 18F4550 that Microchip and the CUI use in all of their hardware/software. However, the 18F2550 differs in the number of IO ports and in 1 or 2 peripherals.

The first step to get USB working is to modify the USB bootloader. The bootloader is setup to output USB status to port D. The 18F2550 doesn't HAVE a port D. Since I didn't want to take up any pins, I just commented out any calls to the functions dealing with the LEDs.

Another issue is the linker file. The CUI code is setup for an 18F4550. Copy the 18F2550 linker file from your C18 compiler install to your CUI bootloader directory. Remove the 18F4550 linker file from the project and add the copied 18F2550 linker file.

You should now be able to compile and burn the bootloader to the 18F2550.

Next comes the firmware. This has the SAME issue as the bootloader with port D. Just make the same change.

The firmware linker file also needs to be updated from 18F4550 to 18F2550. Luckily for us, both chips have the same setup. Copy the rm18F4550.lkr file to rm18F2550.lkr. Edit rm18F2550.lkr to read 'FILES p18f2550.lib' instead of 'FILES p18f4550.lib'.

You should now be able to compile and burn the firmware to the 18F2550 using the Pdfsusb app.

Sample Code

Below is an already modified set of sample code for the 18F2550.

This code differs in a few ways from the CUI and Microchip code. It contains the fixes above and it sets the config bits in main.c in the bootloader. By setting the config bits in the bootloader code, it doesn't have to be done at programming time.

AttachmentSize
18F2550_USB_20060511.zip2.04 MB