Linux Microchip PIC development

Recently I dug out an old Microchip PIC 18F4550 USB demo board, which I'd bought many years ago from Tiertex.

It has the Microchip HID USB bootloader already flashed onto the chip. The goal is to get a nice setup with Linux (Ubuntu 16.04 LTS). I tested two tools to download code to the bootloader:

The next part is compiling your own program to run on the board. I decided to use SDCC. There is a package available in the Ubuntu repositories, however this package does not seem to include the PIC headers from Microchip, as apparently they are not GPL compatible :( Thus, it is necessary to download (latest version currently 3.6.0). I just grabbed the binary. You need to add --use-non-free for Microchip development. You can still install the gputils package from the Ubuntu repository.

The main challenge is getting the linker setup to respect the fact that the USB bootloader lives in the memory before 0x1000. I did this by performing the following two steps:

  1. Make a copy of /usr/share/gputils/lkr/18f4550_g.lkr, as say hid_18f4550.lkr somewhere handy.
    1. sudo cp /usr/share/gputils/lkr/18f4550_g.lkr somewhere/handy/hid_18f4550.lkr
  2. Modify the codepage section in to:
    1. CODEPAGE NAME=bootloader START=0x0 END=0xFFF PROTECTED
    2. CODEPAGE NAME=vectors START=0x1000 END=0x1029 PROTECTED
    3. CODEPAGE NAME=page START=0x102A END=0x7FFF
  1. Recompile the CRT startup file crt0i.c (which by default wants to start at 0x0) with the new vector address (0x1000). I called the output bl_crt0i.o and put it in my program directory to avoid future confusions:
    1. sdcc --use-non-free -mpic16 -p18f4550 -c --ivt-loc=0x1000 [sdcc path]/lib/src/pic16/startup/crt0i.c -o bl_crt0i.o
  1. Note that no linking is performed here.
  2. Combine the ingredients (linker script and modified CRT) from above to compile your program (named simple.c here):
    1. sdcc --use-non-free -mpic16 -p18f4550 -Wl,-s,hid_18f4550.lkr --use-crt=bl_crt0i.o --ivt-loc=0x1000 simple.c
  1. If curious you can check the disassembly of the resulting hex file with:
      1. gpdasm -p18f4550 simple.hex
    1. This will (hopefully!) confirm that the code begins at 0x1000.
  1. Use either of the programs mentioned above to write the hex file to the chip. Here is a screenshot of a successful write using the GUI app. Note that you will need to press a button or connect a jumper (or something) on your board to enter boot loader mode (RB2 on my board).
    1. Success! Reset and enjoy playing :)

For reference, the program simple.c I wrote is attached below (it just lights the LED corresponding to the button pressed).