Created by: gwideman, Mar 25, 2014 3:50 am
Revised by: gwideman, Apr 7, 2015 9:20 pm (20 revisions)

Overview

There are many articles and tutorials on the web discussing the hardware and procedures for Arduino and AVR programming. However I found most to be riddled with glib descriptions laden with inaccuracies and ambiguities. This page gathers some notes about the ground truths involved.

Background

Numerous Arduino models have been introduced over the years, with progressively newer Atmel AVR microcontroller chips. All of these were in the AVR 8-bit ATmega family (ATmega8, 168, 328, 1280, 2560) until the Due and later, which uses an ARM processor. Atmel also has a line of physically and computationally smaller AVR 8-bit chips, in the ATtiny series.
All of these Arduinos and AVR chips can be programmed with the Arduino environment (and also separate from Arduino, using various tools, for example Atmel's Studio).
All present-day off-the-shelf Arduino boards can be programmed via a USB cable using the Arduino software environment. But how does one proceed to program a raw Atmega or ATtiny chip, independently of an Arduino board. Or program a fresh AVR to be used in an Arduino board?

Two distinct methods and workflows

This article with describe two main programming methods and workflows:
  • Bootloader-facilitated programming: Programming an AVR which alrady has a bootloader function programmed into it. This is the programming method normally engaged when using the Arduino IDE to talk to an Arduino.
  • ISP programming: This programming method can be used with an AVR that lacks a bootloader. Hence it's useful for non-Arduino scenarios, and also to program a bootloader onto an AVR to get it ready for use in an Arduino.
  • There are other programming modes, but these are less convenient and reserved for relatively esoteric purposes.
The following sections discuss the details and variations of these different methods, along with the wiring and hardware involved.

AVR memory types: What can be programmed?

An AVR chip has several memory-ish features salient to the topic at hand:
  • Flash memory: This memory is non-volatile (content preserved during power-down) and holds program code. It can also hold permanent data, but the main point is that it's only from this memory that the AVR CPU executes code. A normally-running program cannot write to flash. (Though a bootloader can... hmm.)
    • Note: In many other computing contexts, "flash" connotes a storage device like a USB memory stick or SD card, that, analogous to a disk, the CPU accesses serially via some I/O chip, Data from that device has to be read into RAM in order to be available for the CPU to execute. In contrast, the AVR's CPU executes program code direct from the on-board flash.
  • EEPROM: This memory is also non-volatile, and is intended to hold data. It is writable by a normal AVR program.
  • SRAM: An AVR has some static RAM -- this is for temporary data storage while the chip is powered up; its contents are lost during power down.
  • Fuses: These are elements that set some aspect of the chip's hardware operation, such as its clock divider number, and so on. These are non-volatile and must be set by a programmer device (cannot be set by bootloader).

At the hardware level, several methods for programming

The AVR provides a number of methods to program the chip, which differ in which of the above features can be programmed.
Method
Flash?
EEPROM?
Fuses?
Comment
ISP "In system programming"
Yes
Yes
Yes, but
Can program fuses, but a mistake can program the fuses into a state where the chip is incapable of further ISP programming. See Recovering from a "locked out" AVR. Worst case, it's necessary to use "High Voltage" programming (Parallel or Serial) to get complete control of the fuses
Programming via bootloader
Yes
Yes
No

High-voltage programming
Yes
Yes
Yes
Serial or Parallel variants, depending on chip. Almost definitely not "in-system".
For the most part, the current article is concerned with ISP and Bootloader-enabled programming.

Programming hardware and connections

Each programming method offers different benefits and shortcomings:
Feature
ISP
Bootloader-facilitated
Chip preparation
None required [1]
Bootloader must previously have been installed (typically by ISP method)
Overhead

Bootloader takes some flash memory; may not be feasible on smaller AVRs
Safety while programming
Might accidentally set fuses to settings that preclude ISP [1]
Difficult to mess up fuses
Connections to AVR
SPI + Reset
Serial "TTL-232": UART Rx, Tx + DTR connected to Reset
Programmer hardware required
ISP programmer; Arduino programmed as ISP programmer.
Arduino: USB cable; Bare AVR: USB-TTL-serial cable or board. (Also, some AVRs have built-in USB.)
USB driver (Windows)
Programmer typically requires a matching USB driver.
Arduino may require USB-serial driver. USB-serial cable, likewise
Host programming software
Atmel AVR Studio; AVRDUDE; (Arduino IDE can program a bootloader via ISP; calls its own copy of AVRDUDE)
Arduino IDE

[1] If an ISP programming session sets fuses to some settings, normal ISP will no longer work. For some incorrect settings, performing ISP plus an external clock signal may work. Otherwise the chip can only be returned to programmability using "high-voltage programming" method.
Ambiguity note: One often reads of "bootloader programming". Usually, but not always, this means "bootloader-facilitated programming" (eg.: of a sketch), rather than "programming bootloader code into a fresh AVR".
Anomaly: Despite the two programming methods being complementary, and involving conceptually similar serial signals, only a few programmer gadgets provide both functionalities.

Subsidiary pages with programmer details

Miscellaneous programmer ideas


References