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 the latest version (3.6.0).  I just grabbed the binary.  You need to add --use-non-free for Microchip development.  Note 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.
  2. Modify the codepage section to:
    CODEPAGE   NAME=bootloader START=0x0                END=0xFFF          PROTECTED
    CODEPAGE   NAME=vectors    START=0x1000             END=0x1029         PROTECTED 
    CODEPAGE   NAME=page       START=0x102A            END=0x7FFF
  3. Recompile the CRT startup file crt0i.c (which unfortunately always seems 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:
    sdcc --use-non-free -mpic16 -p18f4550 -c --ivt-loc=0x1000 [sdcc path]/lib/src/pic16/startup/crt0i.c -o bl_crt0i.o
    Note that no linking is performed here.
  4. Combine the ingredients (linker script and modified CRT) from above to compile your program (named simple.c here):
    sdcc --use-non-free -mpic16 -p18f4550 -Wl,-s,hid_18f4550.lkr --use-crt=bl_crt0i.o --ivt-loc=0x1000 simple.c
  5. If curious you can check the disassembly of the resulting hex file with:
    gpdasm -p18f4550 simple.hex
    This will (hopefully!) confirm that the code begins at 0x1000.
  6. 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:
  7. 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).

ċ
simple.c
(1k)
Peter Windridge,
Jun 19, 2016, 2:43 PM
Comments