Wednesday 28 July 2010

Design with microcontrollers: The Attiny2313

Figure 1. Parallel target board










Below (Figure 2) is a sample serial target board for programming the 8 bit 20 pin attiny2313, a derivative of the AVR microcontroller family.


Figure 2. Serial target board


What you see in Figure 1 is a sample parallel target board. I built these two and another one (shall post it soon) during my university days while i was experimenting with the 8 bit 20 pin attiny2313. I preferred the connection in figure one because it was easy to use with isp_prog 2007 programming software to read/write/erase data from either the flash memory or eeprom memory of the chip. i learnt it was easier to write the flash memory than it was to write the eeprom memory. All one needed to do is to write their codes in C (a hardware friendly programming language) or whichever language they preferred, compile it to generate the hex codes using avr studio or any other preferred compiler, then it is this hex codes that one needed to write to the chip.
The third target board was also easy to use and more interesting to me since i would address the chip directly through the command prompt. I used avrdude, a command line program and so i had to type in all commands. An instruction like avrdude -c dapa -P lpt1 -p attiny2313 -U (flash/eeprom):(r/w):(name of hex file).(hex/eep) would be reflected in the form of status bars in the command window.

Monday 19 July 2010

Using avrdude

You have to install avrdude first before being able to enjoy using it. Winavr usually comes packaged with avrdude. Then under windows ,open the command window and type in avrdude, you should get the following list of what avrdude can do.
















To get a list of supported programmers e.g. dasa, dapa, stk500 e.t.c., type in avrdude -c asdf
To get a list of parts supported by avrdude, type in avrdude -c avrisp. Thats the list of chips that avrdude knows about. Almost all of them are ISP programmable.

-P <port> tells avrdude where to look for your programmer
-U <memtype>:r/w/v:<filename>[:format]: tells avrdude how to put data on the chip
<memtype> can be flash, eeprom, hfuse(high fuse), lfuse (lowfuse), efuse (extended fuse).
r/w/v can be read, write or verify
<filename> the input (writing or verifying) or output file (reading)
[:format] optional, the format of the file. You can leave this off for writing, but for reading use i for Intel Hex (the prevailing standard)

e.g to read the low fuse into a file use the command -U lfuse:r:lfusefile.hex:i
to write a file called bobo.hex to the flash use the command -U flash:w:bobo.hex
to verify a file called mystuff.eep from the eeprom use the command
-U eeprom:v:mystuff.eep

Example:
In the command window type in avrdude -c usbtiny -p attiny2313 -U flash:w:test_leds.hex
avrdude should go through the following steps:
1. initializing the programmer (you won't see this if it works)
2. initializing the AVR device and making sure it is ready for instructions
3. reading the device signature (0x1e910a) which confirms that the chip you specified in the command line (attiny2313) is in fact the chip the programmer is connected to
4. erasing the chip
5. reading the file and verifying it is a valid file
6. writing the flash
7. verifying the flash

















Burning fuses
Fuse is a separate chunk of flash that is not written to when you update the firmware. They define things like clock speed, crystal type, whether JTAG is enabled, what the brownout (minimum voltage) level is, e.t.c. Setting the fuses incorrectly may 'brick' the chip e.g. you may disable future programming or make it so the chip is expecting an external crystal when there isn't one.
To program the fuses use:
-U lfuse:w:<0xhh>:m
-U hfuse:w:<0xhh>:m
-U efuse:w:<0xhh>:m
where <0xhh> is the desired fuse value in hex. But first you will want to calculate the fuse values using the very convenient AVR fuse calculator.
Triple check the fuse values, check again to make sure you aren't disabling ISP programming or the reset pin or setting the clock speed to 32kHz, then verify again that you have the correct chip for calculation. Finally you can try writing them to the chip.
If the programmer is not connected properly to the chip you will get the following message:










This simply means that the programmer could not 'talk' to the chip. 99% of the time it is a problem with wiring. Check that the chip is powered, plugged properly to the programmer, the programming cables are plugged in properly e.t.c. If you are using a 'simple' programmer such as a serial or parallel port bitbang programmer it could mean the programmer is at fault.

For most microcontrollers, the .hex files are not cross-compatible. This causes a signature failure. Once the signature is different from the expected one it stops. For example try to run this command avrdude -c usbtiny -p atmega8 -U flash:w:test_leds.hex









It stops at step 2. This is because code that is compiled for an attiny2313 wont run on an atmega8.