Top ↑

Compile code for the Tiny Bootloader using SDCC

Firstly, let me say I'll be using a PIC 18F4520 for this tutorial. TinyBLD is known to work with other chips too. If you're reading this tutorial, I'm going to assume you are reasonably competent with C and electronics around PICs. Some Linux skills are needed, but the most you'll be doing is pasting commands into Bash, so it's not too intensive.

This tutorial focuses mainly on the programming of the PIC, not the circuitry around it. If you're looking for "how to wire up a PIC", Google is your friend I'm afraid. Just make sure you have a reset button wired up to MCLR and a 20MHz crystal on the two XTAL pins.

Requirements

Hardware:

  • Computer with (gasp) a serial RS-232 port.
  • JDM PIC programmer, powered off the serial port. One very similar to what I use can be found here.
  • PIC 18F4520 along with MAX232 to convert serial line voltages (+/- 12V) to +5V TTL levels the PIC can handle.
  • Serial cable terminated in a pinheader for easy breadboard connection.

Software

  • Ubuntu or any other Debian derivative. This tutorial will work with other flavours of Linux, but you might need to tweak a few things.

Installing the software

We don't need much to program PICs on Linux, but we do need something so run this as root:

sudo apt-get install picprog gputils sdcc

This will install picprog, the PIC serial programmer, gputils for creating PIC HEX and ASM files, and SDCC, the Small Device C Compiler.

The final program you'll need is TinyBLDLin. This is a Linux version of the GUI program downloader for TinyBLD. Download it here (.deb file), or download the source here. Installing the .deb is simple. Open a terminal and find the folder tinybldlin-0.6.6_all.deb was downloaded in. Now run:

sudo dpkg -i tinybldlin-0.6.6_all.deb

You've now got TinyBLDLin installed and ready to use. If you've downloaded the source, I'll leave you to compiling that.

Wiring the breadboard

If you haven't already, you'll need to wire up the MAX232 following the schematic (left).

The final thing this circuit needs is a +5V power supply, easily obtained from a 7805 regulator. Google around for suitable circuits.

Install TinyBLD on your PIC

Our first step is to get the PIC to listen to commands on the serial port so we can program it without using a programmer. It's a rather chicken and egg situation, however, because to program the bootloader we need a normal programmer. A good one can be found here.

I've made this step pretty easy for you by providing a precompiled .hex file (link) for the PIC 18F4520. It's set up for a 20MHz crystal, and other frequencies won't work, so make sure you've got a 20MHz crystal hooked up to the PIC.

After you've downloaded the .hex file, open up a terminal and navigate to the directory containing the downloaded file. Now comes the programming of the PIC with TinyBLD. Run the following command

sudo picprog --erase --burn --input TinyBLD_PIC18F4520.hex --pic /dev/ttyS0 

Hopefully, everything went fine and the console should say so. If it didnt, try changing /dev/ttyS0 to /dev/ttyS1 for COM2, etc. Another issue could be that picprog couldn't find the file. Make sure you're in the correct directory and try again.

Testing out TinyBLD

To test TinyBLD out, I've written a sample program along with Makefile. Download it here and have a mess around. Extract the archive to the directory of your choice and, like we did with picprog, navigate there using the terminal. Typing ls should list the following files:

  • 18f4520.lkr
  • 18F4520-USART.c
  • crt018.asm
  • Makefile
  • TinyBLD_18F4520_115200_20MHz.hex

Compile example

To compile the example program, just type make into the terminal. You don't need to know a huge amount about the files in this directory, but there are a couple of things.

  • 18f4520.lkr is essential to the correct functioning of TinyBLD; it describes memory mappings. Use this in all projects with SDCC and TinyBLD.
  • Makefile should retain it's references to 18f4520.lkr and crt018.asm as these two files are needed to make SDCC compile TinyBLD-compatible hex.

Program example to the PIC

If all went well and your test compiled fine, it's now time to program the PIC using TinyBLDLin. Open it up by running tinybldlin in the console, or run it using Alt+F2. Another option is to find Tinybldlin in Applications.

You should now see the window to the left appear. Click Browse and find the .hex file you compiled with make earlier. Do not use TinyBLD_18F4520_115200_20MHz.hex. If it's not already there, change the baud rate (first option under Comm) to 115000.

To program the PIC, press the reset button and then click Write Flash in Tinybldlin. This will reset the PIC and make TinyBLD listen for data on the serial port. When you click Write Flash, the hex file chosen will be sent down the serial port and programmed into the PIC.

Test your program

Now you've downloaded the program to the PIC, power cycle it to get it to run the code. What this program does is simply echo anything sent to it via the serial port. Click the Terminal tab in Tinybldlin, set the baud rate to 9600 and click Open. With any luck, whatever you type and send will be echoed back.

Troubleshooting

This is rather open ended. The first place to look is the circuitry around the MAX232. To make sure it's working correctly, connect the TX and RX pins on the PIC side of the chip together. Typing something into the Tinybldlin serial console should echo it back; the receive and transmit wires are connected together. If this doesn't happen check you've got the correct COM port (ttyS*), as well as the circuit around the MAX232.

If the above test works, make sure you've set the correct baud rate in Tinybldlin. The supplied .hex file will only operate at 115000 baud. For the program test, that works at 9600 baud.

If you have any other problems, please leave a comment or send me a message and I'll lend a hand.

Conclusion

This is a simple tutorial focusing on getting the Small Device C Compiler working with the Tiny Bootloader. Hopefully it's worked for, and if you have any comments or suggestions, feel free to leave them at the bottom of this page, or if you have a more personal question, such as how to get TinyBLD working on a different device, please contact me.

If you want to take this code and modify it, the only file that needs changing is the C source file (.c). The others will continue to work, providing you don't change any filenames. If you do change a filename, make sure to update the Makefile as necessary.