Home Security Engineering Projects Research

Microchip AVR Hacking

This project describes how to interface with and program Microchip (formerly ATMEL) AVR RISC microcontrollers with a specific focus on ATmega328P.

There are many excellent references on the web for working with an ATMega. I have relied heavily on the Application Notes that come with each particular chip ([1]-[3]), All About Circuits [5], and AVR Freaks [6] as the primary sources.

ATMega328 Overview

AVR CPU Block Diagram

The figure below shows the architecture of the AVR CPU core. The core is a model of simplicity and can serve as an execellent example in a teaching setting. It is an example of a "Harvard Architecture" in which programs and data memory within the CPU are separated. It is also an example of a RISC (reduced instruction set computer) in which the CPU implements a set of simple instructions whose aim is to be executed in one instruction cycle. By contrast, most commodity CPUs today implement a "Von Neumann" architecture (program and data memory are shared) and complex instruction sets (CISC).

Unless you are planning on programming in assembly language, the CPU architecture is probably not worth studying in any great depth.

System Block Diagram

The figure below summarizes the ATMega328 system block diagram.

Programming Considerations - Fuse Bits

There are two passes involved in programming an ATMega chip. In the first pass, you program "fuses" that set the chip up for its physical environment. The "fuses" aren't permanently burned or blown - they can be changed back to factory values with another programming pass. In the second pass, your application code is downloaded into memory and the chip begins to do its tasks.

Programming will not always involve setting the fuses. In fact, the fuses are likely to be set once followed by a long period of application code programming. However, it's important to know that the fuses have to be adjusted in order to set up the peripherals properly.

In order to know which fuses to set, you really need to have a good idea of how you want to configure the clock, peripherals, and internal devices for your target application. In this section, we look very quickly at the fuse bits, their purpose, and how to program. Note that this information is not substantially different from the manual [2]. A good description of the fuse bits and their purpose can be found here. I'm going to repeat some of what was written there.

There are 19 fuse bits separated into three bytes as shown in the figure below. Yellow indicates that the fuse bit is unprogrammed out of the factory (a logic 1) and green indicates that the fuse is programmed (a logic 0). That's worth repeating: you enable the function specified by a fuse bit by setting it to zero (0).

The fuse bits are all latched when the ATmega328P powers-up in normal operating mode and when entering Programming mode. Except for EEPROM memory save bit, changes to the fuse bits after they have been latched won't take effect until they are re-latched (i.e. exiting programming mode, restart in normal mode of operation).

Out of the factory, the Fuse Low Byte has CKSEL to 0010. This selects the clock source to be an internal RC Oscillator that runs at 8MHz. But bit 7 is set (i.e. 0) which means that this clock is divided by 8. So the out-of-the-box operating frequency is 1MHz.

Minimal ATMega Circuit

The figure below shows the pinout for the 28-pin Dual Inline Package (DIP) version of the 328p.

A "minimal" configuration of the ATMega (i.e. the minimum number of external parts needed to get it running) varies a bit with intended use. At a minimum, the device needs power, ground, and a way to connect to a programmer. The figure below shows a minimal circuit for connecting to the chip using a Sparkfun Pocket AVR Programmer.

And here is the implemented circuit on a breadboard with the Pocket AVR Programmer connected.

It isn't hard to find a good, step-by-step build of a minimal circuit on a proto board. Other examples can be found here and here.

Technically, an external crystal isn't needed to run the chip since there is an internal RC oscillator that can provide a clock signal. The circuit shown above does not have a crystal attached.

Note that if the analog-to-digital capabilities are going to be used, then filtering capacitors should be added to the circuit. Here, we are just getting the chip into operation so no extraneous passive components are necessary. We can consult an application note specifically on the use of the A2D when the time comes.

The 328 can be programmed through it's Serial Peripheral Interaface (SPI). This is the interface that is shown connected to the AVR Pocket Programmer in the figure above.

Programming the ATMega 328 Using Atmel Studio 7

The 328 (and many other devices) can be programmed using Microchip's Atmel Studio 7. Studio 7 is free, provides an integrated development environment and simulator, and has hooks for an online app store for extending the environment. Studio 7 is only available for Windows computers.

Download the Adafruit Driver Installer or the USBtinyISP signed driver from Adafruit. I used the installer and selected USBTiny during the installation process.

When Studio 7 generates a binary for the target device, it can use one of several hardware programmers to move the binary to the device. The programmer selection can be made when you select a target device. However, the AVR Pocket Programmer won't be found in that list so we need to set up as a "custom" programmer selection. For this purpose, we use "AVRDude" - a popular command-line utility that is used to download from and upload to Atmel microcontrollers.

AVRDude is available here in the Download Area. Get whatever version is the latest for windows (avrdude-6.3-mingw32.zip as of this note). Unzip the utility to someplace accessible - preferably without spaces in the filename or path. You will need to remember where you unzipped it for the next step.

Ensure that you set up your profile in Studio 7 as "Advanced". This can be checked by clicking on Tools->Select Profile. You will be shown a window similar to the one below. If you are not the "Advanced" profile, make the switch now. It is necessary so that you can see the full "Tools" dialogue.

Click on Tools->External Tools and fill in the dialogue in a manner similar to what's shown below:

Now let's create a short "Hello World" program for the minimal circuit shown above. Click on File->New Project. You will get a window similar to the one shown below. Select "GCC Executable Project". Give it an appropriate name and location.

You will be presented with a "target architecture" selection similar to the dialogue below. Select the ATmega328P.

Finally, you will be given a starting code framework similar to one shown below.

Edit the code to include the following:

Now click on Build->Build Solution as shown below. When you do this, you should see a number of progress messages in the Output window.

If everything compiled correctly, click on Tools->AVRPocket and the code will be downloaded into your ATMega. If it works, it will blink fast. Note here that the tool name (AVRPocket) is the name we gave AVRDude earlier. If you gave it a different name, you should see this name in the list.

Programming the ATMega Without Studio 7

If you'd rather do all your programming without the assistance of Studio 7 IDE, then you will want to download and install some avr tools. I would recommend doing this on a Linux box.

I used this reference as a starting point for setting up my handraulic avr programming environment. The steps were as follows:

Burning Back a Bootloader (if you blew one away)

If you happen to have used your Arduino board for its ATMega328 and then turned around and needed it for something else, then it would be nice to return the bootloader back to its original state so it can talk to the Arduino software again. Here's what I did to achieve this (after spending several hours trying to figure out why the device wouldn't work before remembering I blew up the bootloader).

References

  1. Atmel ATmega328P Datasheet Summary (11/20/16).
  2. Atmel ATmega328P Datasheet.
  3. Atmel ATmega328P Application Notes (Under Documents tab).
  4. Professor Hill's Course "Mircroprocessor Principles and Applications (California State University).
  5. All About Circuits.
  6. AVR Freaks.
http://www.linuxandubuntu.com/home/setting-up-avr-gcc-toolchain-and-avrdude-to-program-an-avr-development-board-in-ubuntu https://www.avrfreaks.net/forum/unrecognized-option-mmcuavr5