Linux Microchip PIC development
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:
- MicrochipHidBootloaderQt64 is a Qt based GUI application based on an official Microchip MLA project.
- An older libUSB based shell tool, which I used initially. To compile:
- sudo apt-get install libusb-1.0-0-dev
- g++ main.cpp -o hid_bootloader -lusb-1.0
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:
- Make a copy of /usr/share/gputils/lkr/18f4550_g.lkr, as say hid_18f4550.lkr somewhere handy.
sudo cp /usr/share/gputils/lkr/18f4550_g.lkr somewhere/handy/hid_18f4550.lkr
- Modify the codepage section in to:
CODEPAGE NAME=bootloader START=0x0 END=0xFFF PROTECTED
CODEPAGE NAME=vectors START=0x1000 END=0x1029 PROTECTED
CODEPAGE NAME=page START=0x102A END=0x7FFF
- 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:
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.
- 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
- 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.
- 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).
- 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).