Created by: gwideman, Apr 9, 2015 4:15 pm
Revised by: gwideman, Apr 14, 2015 4:19 pm (16 revisions)

Overview

Some notes on AVR programming independent of Arduino IDE.
Example project: Adafruit's TV-B-GONE kit. In this page I follow steps to recreate the same product, without the kit. The main challenge is programming the ATTiny85 microcontroller.
Links for kit and finished product:

Driver installation

If this is a first project entailing installation of the tools for the first time, it's likely that driver installation will be needed. That is a topic about which there's considerable confusion on the internets, so see here: AVRISP mkII driver notes.

Development tools and other components needed

The set of components needed:
[Source code] --> compiler/linker --> [hex file, and possibly other output] --> programmer software--> drivers --> programmer device --> ATTiny
What to choose for each piece?
Item
Alternatives
Discussion
Source code
Adafruit download
Several firmware versions available, seems like most recent (v1.2) is a good bet. Contains source (but for what environment? Tutorial suggests WinAVR? In any case, it's all gcc), and also a possibly-ready hex file.
Details:

main.c/.h; util.c/.h; WORLDcodes.c; Makefile
This MIGHT be enough to recompile. However, it relies on a number of includes, and it's not clear that these are universally available in all avr compiler environments. To be determined. Also -- what Make utility?

tvbgone.hex
Might be ready-to-go and could just be loaded onto ATTINY85. But what about fuse settings?

parsegen3.pl
No docs on what this does. Probably involved in transforming raw IR pulse data into WORLDcodes.c. Not mentioned in Makefile. Hopefully we don't have to run this.



Compiler and linker
WinAVR
This is a hodge podge of tools (including gcc?), each at least as old as 2010, the installer seems flaky, or at least has a bunch of complaints of trashing the system PATH, and the drivers it comes with don't run on Win7-64. (libusb0 out of date and buggy)

gcc toolchain from Arduino?
Not sure how to run this stand-alone. Probably just requires an appropriate PATH setup. Also -- see verbose output in Arduino "console" for examples.
Compile/link/program
Atmel Studio (6.2 is current)
Should be fully capable, including having a driver (that works on Win7-64) for the AVRISP mkII. But:
  • Installing the included driver preempts a libusb0-based driver, and hence disables avrdude's ability to talk to Atmel AVRISP mkII. (See page: AVRISP mkII driver notes on how to get both installed successfully.)
  • Does this environment, in current version , supply the includes needed by the tvbgone source code?
  • Can it run the standalone Makefile? Or do we have to decipher the Makefile into sensible options in the Atmel Studio GUI?
    • Tools > Command Prompt launches Windows console window with PATH set appropriately. (Not to be confused with Ateml Studio's "Command Window".)



Programmer software
Atmel Studio
Given a hex file, Atmel Studio has functionality in the UI to program a chip.
  • Atmel Studio comes with drivers (Jungo) for Atmel

avrdude (for example, supplied with Arduino)
Probably could program the existing hex into the device, but how to set up a libusb-style library+ driver by which avrdude can talk to the Atmel AVRISP mkII programmer? See page: AVRISP mkII driver notes



Programmer device
Atmel AVRISP MkII
Would seem like the obvious choice, given that it's from the manufacturer of AVRs. Works fine with Atmel Studio. But, does not install libusb-style library and driver that AVRdude knows how to talk to (on Windows). (Oddly, Atmel Studio does actually come with libusb0 drivers, does register them in the registry, but then what?)
For avrdude, there are various docs and installers floating around, but if they predate Win7-64 then the driver won't run on Win7-64. See page: AVRISP mkII driver notes.

Non-Atmel programmers

Exercising avrdude as installed by Arduino IDE

... and bypassing the AVRISP mkII driver issue for the moment.
Arduino IDE uses avrdude to load sketches to an Arduino. This can be seen in the Arduino "console" output, if Arduino's verbose settings are enabled (File > Preferences)
Now to exercise avrdude from the command line to interact with Arduino.
Here are sample avrdude commands to retrieve info from an Arduino from the command line:
cd "C:\Program Files (x86)\Arduino\hardware\tools\avr\bin"
[prompt]>avrdude -C"C:\Program Files (x86)\Arduino\hardware\tools\avr\etc\avrdude.conf" -v -patmega328p -carduino -PCOM3 -n
Note: -C can take relative path. So:
[prompt]>avrdude -C"..\etc\avrdude.conf" -v -patmega328p -carduino -PCOM3 -n
Key:
Flag
Args
Description
-C
File path
Location of avrdude.conf config file. Absolute path, or relative to present working directory
-v
none
Verbose (can increase verbosity by -v -v, and so on).
-p
Part name
Target device (part) to be programmed or read.This presumably corresponds to values listed in avrdude.conf section "PART DEFINITIONS". Comments in avrdude.conf say that it's the "id" field to be used in the -p argument, yet examples seem to correspond to the desc field.
-c
Programmer name
Name of programmer to be used. Presumably corresponds to values listed in avrdude.conf, section "PROGRAMMER DEFINITIONS", either the "id" or "desc" fields?
-P
Port
Port via which to communicate with programmer. Here COM3, because communication with Arduino is via serial-over-USB. (Arduino programmer definition specifies connection_type = serial.)
Can be COMn, LPTn (parallel port), or usb (note: usb in lower case.)'
-n
none
Do nothing
Note: avrdude doesn't seem to care whether you put a space between the flag ('-c') and the subsequent argument ('arduino').

Some avrdude handy ideas

What?
Descr
Help
avrdude -?
However, most of the parameter descriptions don't list valid values. Alternatively: http://www.nongnu.org/avrdude/user-manual/avrdude_4.html
Batch file
avrdude.bat: C:\path\to\avrdude.exe -CC:\path\to\avrdude.conf %*
Put the batch file somewhere in the system PATH. (I have a directory 'C:\_Commands' for this purpose, which I include in the system PATH.)
List of parts
avrdude.bat -c avrisp (or any valid programmer)
List of programmers
avrdude.bat -c asdf (or any string != valid programmer)

avrdude to AVRISP MkII

(Using avrdude.bat, which incorporates -C path.)
Flag
Args
Description
-v
none
Verbose (can increase verbosity by -v -v, and so on).
-p
t85
On my test board, attiny85
-c
avrispmkII
Note: the list of programmers output by AVRdude has several items that could be for the AVRISP MkII. Only by examining avrdude.conf do you see that just one of these specifies USB communication.
-P
usb
Port. (This should be redundant, given that the programmer definition in avrdude.conf associates avrisp mkII with usb.)
-n
none
Tells avrdude to do no programming
Command line:
  • avrdude.bat -v -p t85 -c avrispmkII -P usb -n
This would exercise AVRISP MkII (blinking lights etc) and produce some output text describing the target part.
This probably will not work if the only installation so far is that for Arduino.

Install driver(s) for AVRISP MkII

See page: AVRISP mkII driver notes

Back to TV-B-GONE

Using the setup just described, I programmed the tvbgone.hex for v1.2 firmware to the ATTINY85. Observing on a scope, I could see promising remote-control-like pulses, but at a pulse rate of only about 5 kHz (ie: 100usec high, or low), where we would expect around 40kHz. That's about a factor of 8 too slow -- hmmm! As anticipated, some fiddling with the ATTINY85's fuses is needed (not to mention running the chip off a resonator rather than internal clock).
Inspection of Makefile:
# Program the device.
program: $(TARGET).hex
    $(AVRDUDE) $(AVRDUDE_FLAGS) -B 1 -U flash:w:$(TARGET).hex
 
full: $(TARGET).hex
    make burn-fuse
    make program
 
burn-fuse:
    $(AVRDUDE) $(AVRDUDE_FLAGS) -B 250 -u -U lfuse:w:0xfd:m -U hfuse:w:0xdf:m
 
--------------------------
Note: I eliminated parts to do with EEPROM programming since there's no .eep file in this project.
Fuse settings:
Fuse
Default
Meaning
TVBG
Meaning
lfuse
62

FD

hfuse
DF

DF
same as default





See fusecalc tool: http://www.engbedded.com/fusecalc/
Bit
Name
Descr
Default
TVBG
Low Fuse




7
CKDIV8
Clock divide by 8
x

6
CKOUT
Clock out enable


5
SUT1
Start-up time


4
SUT0
"
x

3
CKSEL3
Clock src sel
x

2
CKSEL2
"
x

1
CKSEL1
"

x
0
CKSEL0
"
x

0




High Fuse




7
RSTDISBL
Disable ext reset


6
DWEN
DebugWIRE en


5
SPIEN
SPI En
x
x
4
WDTON
Watchdog En


3
EESAV
Save EEPROM during chip erase


2
BODLevel2
Brown-out detector level


1
BODLevel1
"


0
BODLevel0
"







Key: x means bit = 0, otherwise 1.
http://www.atmel.com/images/atmel-2586-avr-8-bit-microcontroller-attiny25-attiny45-attiny85_datasheet.pdf
Clock settings:
  • Default: ClkSel[3:0] = 0010 = 8MHz
  • TV-B-Gone: ClkSel[3:0] = 1101.
    • ClkSel[3:1] = 110 means Freq range 3 to 8 MHz.
    • ClkSel[0] = 1 with SUT[1:0] = 00 means medium speed startup and slow reset suitable for ceramic resonator with slowly rising power.

Clock assumptions during programming

Looks like the Makefile command 'make full' will first program the fuses, and then program the flash memory.
  • Fuse-programming step: A fresh ATTINY85 will start with the default fuses, which tell it to run on internal clock.
    • I guess in case the fuses were previously set to super slow mode, the make burn-fuse command sets the bit-fiddling rate to very slow (-B flag: 250us).
  • Flash-programming step: The fuse programming tells the ATTINY85 to rely on an external resonator clock. So the assumption at this point is that a resonator is connected.
    • If the ATTINY85 is situated in an actual TV-B-GONE, then the resonator will be in place. However, the output that controls the IR-LEDs should probably not be connected in order to avoid the possibility of a of high-current pulses (and possible Vcc fluctuation) during programming.

Can the TV-B-GONE source code by compiled by Atmel Studio?

To do!

Other References