Tuesday 17 May 2016

Microcontrollers part III: Setting up the programmer

To be functional, the microprocessor needs a program. But how can we get one inside as there's no place to stick in a USB drive?

Let's take another look at the pinout of ATtiny85:
On the right side, we see that pins PB0, PB1, and PB2 have alternative labels MOSI, MISO, and SCK. These pins are used for Serial Peripheral Interface (SPI) which is a popular protocol to make embedded devices communicate with each other. When the RESET pin is pulled to ground (as opposite to the normal case), the SPI bus can be used to program the microcontroller.

To get started with programming, we first need to install and setup some essential software to the Raspberry Pi. The easiest way to do this is to paste the following line to the terminal:

 sudo apt-get install gcc-avr binutils-avr gdb-avr avr-libc avrdude  

This installs all the necessary stuff to e.g. compile and debug the programs (gcc-avr, binutils-avr, gbd-avr, avr-libc) and write them to the chip (avrdude). If you use the Pi like a normal computer with a display, mouse and keyboard, that's probably sufficient. I have only one display and I don't want to switch between my PC and Pi all the time, so I connected my Pi to the router with an Ethernet cable and access it remotely from my PC to spice up the nerd factor. However, working over the SSH connection can be tedious sometimes, so I installed the same package on my PC to develop bigger codes on it, leaving the Pi as the chip programmer.

The next step is to edit avrdude's configuration file to list the GPIO bus as a programming device. I did this following a tutorial on adafruit.com and as I'm not planning to steal any credit, I won't copy it on my post but advice you to look up the same tutorial here. Unlike the tutorial, I chose Pi's pin #8 for RESET, #9 for MISO, #10 for MOSI, and #11 for CLK as I wanted them closer to each other. It's a matter of taste what pins you choose.

Pi's GPIO pins. Source and more info: https://www.raspberrypi.org/documentation/usage/gpio-plus-and-raspi2/

All right, time to test out whether we talk with the microcontroller. To connect the chip to Pi, I used a solderless breadboard to which one can just stick all the components and wires in. The most annoying part is to connect all the pins with individual wire. One day I'll build a neat cable for this but for now I have deal with a bunch of 60 cm (2 ft.) long spaghetti. It's a mess.

Pi's GPIO connected to the chip. The red wire is the operating voltage (+3.3 V), grey is SCK (or CLK), violet MISO, orange MOSI, white RESET, and the black is ground.
Be extra careful when making these connections. You don't want to short any pins as it might break the GPIO pins or in the worst case the whole Raspberry Pi! I accidentally did short two pins once and it jammed the whole Pi. I already thought I've done goofed but fortunately turning it off and on again brought it alive again.

After connecting the chip it is ready to be tested. Assuming you followed the adafruit tutorial, the configuration file should be in the home folder and the GPIO programmer should be named 'pi_1'. To test reading the chip type the following command to the terminal replacing 'attiny85' with the model you are using:

 sudo avrdude -p attiny85 -C ~/avrdude_gpio.conf -c pi_1 -v  

avrdude then outputs a wall of text and numbers on the screen. If everything went as expected, there should be this line somewhere near the end:

 avrdude: AVR device initialized and ready to accept instructions  

Congratulations, your microcontroller is working, connected properly and ready to be programmed and you're ready to move to the next level post! However, if it isn't, you might get something like this as I did at the first try:

 avrdude: AVR device not responding  
 avrdude: initialization failed, rc=-1  
      Double check connections and try again, or use -F to override  
      this check.  

It can mean that your chip is broken, but most likely some wires are not connected as they should be. In my case none of them were; it turned out that I can't count pins up to eight properly.

No comments:

Post a Comment