AVR GCC Toolchain – Setup for Windows

This tutorial will guide you through installing and setting up the AVR GCC Toolchain on Microsoft Windows. You’ll also learn how to install some additional and useful tools that will make it easier for you to write your first microcontroller programs.

This guide has been recently updated and tested on a Microsoft Windows 11 machine.

Prerequisites

What do we need to complete this guide?

  • Computer with Microsoft Windows 11.
  • Administrative access to Windows.
  • One USB port that is available for use.
  • ISP programmer – type USBasp is recommended.
  • AVR-powered microcontroller board. We use, as always, a Tinusaur board.

NOTE: It takes about 2 hours for an inexperienced person to do it.

In educational settings and workshops, we often focus on the ATtiny85 because of its simplicity and low cost. To reduce setup time, we use browser-based tools such as Blocktinu Code, where students can write and compile C code without installing compilers locally.

Blocktinu Code - C code editor for ATtiny85 MCU AVR

GNU GCC AVR Toolchain

1. What is the AVR GCC Toolchain?

The AVR GCC Toolchain is a collection of tools and libraries used to compile your code for the AVR microcontrollers. This collection includes a compiler, assembler, linker, and some libraries.

Most of the tools are based on efforts from the GNU community (GCC stands for GNU Compiler Collection), and some others are developed by Microchip.

2. What is WINAVR?

WINAVR is a great project, or rather was.

WinAVR

It was a Windows installation package that contained everything you needed to compile your C or C++ code for an AVR microcontroller under Windows, and, most importantly, it allowed us to do it in the text console.

The most “recentWINAVR files are from 2010-01-20. 🙂 In other words, it is outdated.

There are many projects that have attempted to replace it, but none of them (AFAIK) is extremely popular.

This guide will walk you through the steps for setting everything up manually. There is no installation package you can run that will do everything for you. This approach will also teach you how everything works.

3. AVR GCC Toolchain – Setup for Windows

Microcontroller

To set up and use the AVR GCC Toolchain, we need 3 essential components:

  1. AVR GCC – Windows Binary
  2. GNU Make – on Windows
  3. AVRDUDE – on Windows

All the suggested downloads, folders, and file names are based on our setup, and some of them may be changed to suit your preferences.

AVR GCC – Windows Binary

Let’s start with the AVR GCC. Let’s download it from the Microchip website.

Toolchains for AVR® Microcontrollers (MCUs)

Go to: https://www.microchip.com/en-us/tools-resources/develop/microchip-studio/gcc-compilers
On the page, look for something like “AVR 8-Bit Toolchain (Windows)” or a similar title.

Create a “Programs” folder (if it does not exist already) for the various programs that we’re going to install. The name and location are up to you; those given here are just an example.

C:\Users\MY_USER_NAME\Programs\

Extract the contents of the ZIP file in the “Programs” folder. All the folders and files should be in an “avr8-gnu-toolchain-win32_x86_64” folder. Let’s rename it to “avr8-gnu-toolchain” just to simplify things – it is not required.

The Toolchain is now at:

C:\Users\MY_USER_NAME\Programs\avr8-gnu-toolchain\

The sub-folders should look something like this: avr, bin, doc, i686-w64-mingw32, include, info, lib, libexec, man, share.

Do not add anything to your Windows Environment PATH variable.

Alternatives

There is a very interesting project that you could use as an alternative:
AVR-GCC 11.1.0 for Windows 32 and 64 bit
https://blog.zakkemble.net/avr-gcc-builds/

The AVRGCCStart.cmd Script

It would be very convenient if there were a command that would start the console development environment for us. So, let’s create a short command-line script that sets up everything for us. Let’s call it AVR GCC Toolchain STARTER.

Instead of adding the Toolchain file system Path to the system PATH (in the environment variables), we are going to create a simple command prompt script that will do this only for the command prompt opened by that script. The advantage of doing it this way is that we’re not going to “contaminate” the system PATH variable with too many executables and libraries. Doing otherwise might cause issues with some other already installed development environments.

How to create the AVRGCCStart.cmd file?

  1. Open the Windows Notepad application.
  2. Press CTRL + S (to save).
  3. Type in the name “AVRGCCStart.cmd”.

Choose a folder where to save the file. The “Programs” folder is a good choice.

Add the following lines to the AVRGCCStart.cmd file:

set Path=%Path%;C:\Users\MY_USER_NAME\Programs\avr8-gnu-toolchain\bin
set Path=%Path%;C:\Users\MY_USER_NAME\Programs\avr8-gnu-toolchain\avr\bin

start cmd

That will add what is necessary to the Path and will open a command prompt window.

IMPORTANT: Don’t forget to replace MY_USER_NAME with your username.

Testing

AVR GCC

Start the AVRGCCStart.cmd script.
In the command prompt, execute:

avr-gcc --version

That should output the version of the AVR GCC compiler.
Something like this:

avr-gcc (AVR_8_bit_GNU_Toolchain_4.0.0_52) 15.1.0

That also means that everything is correctly installed and set up to this point.

GNU Make for Windows

To package all compiled C or C++ source code into a HEX file in a convenient way, i.e., using the so-called Makefile, we need the GNU Make program.

GNU Make

There are many Windows bundles that include GNU Make; we are going to use the GNUWin32 packages at http://gnuwin32.sourceforge.net/packages.html. They are part of the GnuWin (also known as gnuwin32) project at http://gnuwin32.sourceforge.net/.

In the “Programs” folder, create a “gnuwin32” sub-folder for the gnuwin32 packages.

Download the Make packages from: http://gnuwin32.sourceforge.net/packages/make.htm

  • Locate the Binaries ZIP file. Download it to your computer. This should be something like “make-3.81-bin.zip”.
  • Extract the contents in the gnuwin32 folder.

The sub-folders and files should look like: bin, contrib, man, manifest, share.

The make.exe requires some additional libraries, so we should also download the Dependencies Zip (a file like “make-3.81-dep.zip”) from the same page and extract them at the same location.

Optionally, you could download and extract the Documentation Zip file.

NOTE: If the GnuWin32 web page does not work for some reason, you can go directly to https://sourceforge.net/projects/gnuwin32/files/make/3.81/ and download the “make-3.81-bin.zip” file.

Update AVRGCCStart

Edit the AVRGCCStart.cmd script – add the following line (before the “start cmd” line):

set Path=%Path%;C:\Users\MY_USER_NAME\Programs\gnuwin32\bin

Don’t forget to replace MY_USER_NAME with your username.

IMPORTANT: Every time you update the content of the AVRGCCStart.cmd script, close its open windows (if there are any) and relaunch the script again, so the changes will take effect.

Testing

Gnu Make

In the command prompt, execute:

make --version

That should output the version of GNU Make. Something like this:

GNU Make 3.81

That also means that everything is correctly installed and set up to this point.

GNU CoreUtils for Windows

GNU CoreUtils

Some Makefile files might need other GNU tools (like ls, rm, mv, mkdir, etc.) that are part of the GNU CoreUtils; we will use them later.

Download the CoreUtils package: http://gnuwin32.sourceforge.net/packages/coreutils.htm

  • Locate the Binaries ZIP file. This should be something like “coreutils-5.3.0-bin.zip”.
  • Extract the contents in the gnuwin32 folder.
  • Download and extract Dependencies.
  • Optionally, download and extract the Documentation.

NOTE: Some of the files in the Dependencies package might already exist in the gnuwin32 folder – it’s OK to override (or skip) them.

Recommended Packages

Grep for Windows: http://gnuwin32.sourceforge.net/packages/grep.htm

Alternatives

  1. GNU Make could also be downloaded from Steve.fi (Steve Kemp) website: https://steve.fi/software/make/

Please note that those are quite old.

  1. Another alternative is the Gow (Gnu On Windows) project – a lightweight alternative to Cygwin: https://github.com/bmatzelle/gow

Please note that those are quite old.

Testing

GNU ls -la

Start the AVRGCCStart.cmd. In the command prompt, execute:

ls -la

That should list the files in the current folder. Something like this:

total 20
drw-rw-rw-   2 neven 0     0 2021-12-28 17:09 .
drw-rw-rw-  32 neven 0 12288 2021-12-28 15:52 ..
-rwxrwxrwx   1 neven 0   205 2021-12-28 17:37 AVRGCCStart.cmd

That also means that everything is correctly installed and set up to this point.

AVRDUDE on Windows

AVRDUDE Logo

When we start compiling our programs and packaging them as HEX files, we will need a program to upload them to the microcontroller. One such very popular program is AVRDUDE. And yes, there is, of course, a build for Windows.

In the “Programs” folder, create an “avrdude-mingw32” sub-folder for the AVRDUDE package.

Go to download.savannah.gnu.org/releases/avrdude/ and locate the mingw32 ZIP file.

At the time of writing this guide, the most recent version that works out of the box without the need to install additional libraries and/or packages is 6.4.

Download the AVRDUDE package: https://download.savannah.gnu.org/releases/avrdude/avrdude-6.4-mingw32.zip

  • Locate the Binaries ZIP file. This should be something like “avrdude-6.4-mingw32.zip”.
  • Extract the contents in the avrdude-mingw32 folder.

Alternatives

Update AVRGCCStart

Add the following line to the AVRGCCStart.cmd script (before the “start cmd” line):

set Path=%Path%;C:\Users\MY_USER_NAME\Programs\avrdude-mingw32

Testing

AVRDUDE

In the command prompt, execute:

avrdude -v

That should output the version of AVRDUDE. Something like this:

avrdude: Version 6.4

That also means that everything is correctly installed and set up to this point.

USB Programmer Drivers

To upload the compiled and packaged binary code (in the form of a HEX file) into the microcontroller, we will need a Programmer. In most cases, that is a USB device. A very popular device is the USBasp programmer. Under Windows, such a device requires an additional driver to be installed for it to work properly.

One of the best and easiest ways to install a driver for that kind of Programmer is to use the Zadig program.

  • Download the latest EXE file from https://zadig.akeo.ie.
  • Insert your USBasp programmer into the USB of your computer.
  • Run the program. You may need to click on some warning buttons that Windows will show you.
Zadig libusbK
Zadig libusb-win32
Device Manager USBasp
  • Choose the correct driver, which is the “libusbK”.
    Note: on 32-bit Windows, you need to choose libusb-win32.
  • Press the “Install” button.

You might need to wait a minute or two.

Check the Windows Device Manager to see if the driver was installed correctly.

Testing

AVRDUDE

Let’s now check if the USBasp programmer could communicate with the microcontroller (ATtiny85 in our case). First, make sure that:

  • The USBasp programmer is inserted into the USB port, and
  • The microcontroller board is connected to the programmer.

Execute this command:

avrdude -c usbasp -p t85

The result should be something like this:

avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.09s
avrdude: Device signature = 0x1e930b (probably t85)
avrdude: safemode: Fuses OK (E:FF, H:DF, L:62)
avrdude done.  Thank you.

If we have a microcontroller different than the one specified in the command (ATtiny85), the avrdude will suggest what it might be.

Other Useful Tools

Those were the minimum tools that we will need to compile our source code for the AVR platform.

The following are recommended but not required additional tools.

Shell – BASH

bash

It will be convenient to have a console that is a bit better than the Windows Command Prompt. A good alternative is the BASH shell. Developed as part of the GNU Project, it is now used on many other operating systems, including LinuxAndroidMac OS, and, of course, Windows.

OPTION-1

We have put together an archive containing bash.exe and a few other necessary .dll files. It is located at our GitHub repository:

Also available at our GitLab repository:

Download the ZIP archive and extract it to a temporary location.
Copy the “bash-mingw” sub-folder to the “Programs” folder.
The BASH executable will be located in the “Programs\bash-mingw\bin“ folder.

OPTION-2

At Steve.fi – (Steve Kemp’s website), there is a Windows binary for download. It is quite old but still works.

In the “Programs” folder, create a “bash” sub-folder for the bash package.

Go to the website: https://steve.fi/software/bash/

  • Locate the download link: https://steve.fi/software/bash/bash-203.zip
  • Download and save the ZIP file.
  • Extract the contents into a temporary location. There will be a bash-2.03 folder.
  • From the bash-2.03 folder, copy  2 files: bash.exe and bash.dll, into the “Programs/bash” folder.
  • Optionally, you could also copy the .bash.rc file in your home folder:
    C:\Users\MY_USER_NAME\
    Edit it – if you know how it functions.

NOTE: The executables are really old – some of them date back to 1998, 1999.

Alternatives

  1. win-bash – A stand-alone bash for Windows
    http://win-bash.sourceforge.net
  2. How To Install Bash On Windows 10
    https://hackernoon.com/how-to-install-bash-on-windows-10-lqb73yj3

Update AVRGCCStart

Add the following line to the AVRGCCStart.cmd script (before the “start cmd” line):

set Path=%Path%;C:\Users\MY_USER_NAME\Programs\bash-mingw\bin

On your Windows machine, if you have installed another package that has bash (for example, WSL – Windows Subsystem for Linux), and want to make sure that the AVRGCCStart starts the bash-mingw version of bash, use this instead:

set Path=C:\Users\MY_USER_NAME\Programs\bash-mingw\bin;%Path%

Also, replace the line:

start cmd

With:

set HOME=%USERPROFILE%
start bash
bash

Testing

Start the AVRGCCStart script.

It should look like this:

bash-3.1$_

Note: You might see some warning messages about a missing “tmp” folder.

Text editor – nano

A console-based text editor will be (optional) a nice finish!

nano

The nano text editor is very popular and is available even for Windows.

GNU nano for Windows is a good option, and it is located at:
https://github.com/lhmouse/nano-win

The package could be downloaded from this page:
https://files.lhmouse.com/nano-win/

Look for the latest build at the top. Should be something like “nano-win_10648_v9.0-4-g03acf5e8.7z”.

Download it and extract it into a temporary location. It should look like this:

pkg_i686-w64-mingw32           (this is the 32-bit version)
pkg_x86_64-w64-mingw32 (this is the 64-bit version)

In the “Programs” folder, create a “nano-mingw32” sub-folder for the nano package.

Pick a folder for the 32 or 64-bit version, depending on your computer, and copy its contents (e.g., bin, share) to the “nano-mingw32” subfolder.

Alternatives

  1. nano for Windows
    https://sourceforge.net/projects/nano-for-windows/
    Note: this is a version of the one above.
  2. nano – Text editor / win32-support
    https://nano-editor.org/dist/win32-support/
    Note: might not work on a 32-bit Windows.

Update AVRGCCStart

Add the following line to the AVRGCCStart.cmd script (before the “start cmd” or “start bash” line):

set Path=%Path%;C:\Users\MY_USER_NAME\Programs\nano-mingw32\bin
nano

Testing

  1. In the command prompt, execute:
nano --version

That should output the version of nano. Something like this:

GNU nano, version 8.7.1
  1. In the command prompt, execute:
nano

That should start the nano editor.

To exit the editor, press CTRL + X.
The “^” symbol in the menu means that you have to press the “CTRL” key.

Update AVRGCCStart

Before we start the command prompt with either “cmd” or “bash”, we could go to the Home or another folder (let’s say our projects folder).

Add a line to the script before the “start bash” line:

cd %HOME%\my_avr_projects

At this point, the contents of the AVRGCCStart.cmd file should be something like this:

set Path=%Path%;C:\Users\MY_USER_NAME\Programs\avr8-gnu-toolchain\bin
set Path=%Path%;C:\Users\MY_USER_NAME\Programs\avr8-gnu-toolchain\avr\bin
set Path=%Path%;C:\Users\MY_USER_NAME\Programs\gnuwin32\bin
set Path=%Path%;C:\Users\MY_USER_NAME\Programs\avrdude-mingw32
set Path=C:\Users\MY_USER_NAME\Programs\bash-mingw\bin;%Path%
set Path=%Path%;C:\Users\MY_USER_NAME\Programs\nano-mingw32\bin

set HOME=%USERPROFILE%
cd %HOME%\my_avr_projects
start bash

4. AVR GCC “Hello, World!”

Let’s now write our first program – the “Hello, World!”.

In the world of microcontrollers, this is the Blinking LED program.

But, before that, let’s see if we can write, compile, package, and upload any program.

AVR GCC – The “Empty” Program

Let’s first create a folder (if it doesn’t already exist) where we will put all our projects.

mkdir my_first_test
cd my_first_test

Let’s write just a few lines of code in a “main.c” file.

Start the nano with a new file – main.c:

nano main.c

Enter the code:

int main(void) {
    return 0;
}

Save the code: use CTRL+O, then press Enter.

To exit the editor, press CTRL+X.

  1. Compile the source code:
avr-gcc main.c -o main.elf

Note: the result will be an ELF file – main.elf.

We need to convert it to a HEX file,

  1. Convert to HEX:
avr-objcopy main.elf -O ihex main.hex

The result is a main.hex file.

Let’s see what’s inside the file:

cat main.hex

The result should look like this:

:10000000CF93DF93CDB7DEB780E090E0DF91CF9163
:02001000089551
:00000001FF
AVRDUDE USBasp Test

Now, with the AVRDUDE, we could upload that file into the microcontroller.

Mmake sure the programmer and the mocrocontroller board are connected to the USB port.

  1. Upload the HEX:
avrdude -c usbasp -p t85 -U flash:w:"main.hex":a

In this example, “-p t85” specifies that the microcontroller is an ATtiny85.

NOTE that this program does not do anything. It is just an example of checking our setup.

We could also take a look at the assembly instructions of our program:

avr-objdump main.elf -d

The result should look li this:

   0:   cf 93           push    r28
   2:   df 93           push    r29
   4:   cd b7           in      r28, 0x3d       ; 61
   6:   de b7           in      r29, 0x3e       ; 62
   8:   80 e0           ldi     r24, 0x00       ; 0
   a:   90 e0           ldi     r25, 0x00       ; 0
   c:   df 91           pop     r29
   e:   cf 91           pop     r28
  10:   08 95           ret

If we add the “-Os”, the compilation will be performed and optimized for smaller code sizes. Let’s try this:

avr-gcc main.c -Os -o main.elf

Then, check the assembly instructions:

avr-objdump main.elf -d

The result will be something like that:

   0:   80 e0           ldi     r24, 0x00       ; 0
   2:   90 e0           ldi     r25, 0x00       ; 0
   4:   08 95           ret

It is really obvious that the code size is much smaller.

Using a Makefile file

The Makefile file is like a script how to build your code.

Here is a very simple version of it for the example above.

Start the nano editor with a new Makefile file:

nano Makefile

Enter the code:

all:
avr-gcc main.c -o main.elf
avr-objcopy main.elf -O ihex main.hex
avr-objdump main.elf -d
cat main.hex
avrdude -c usbasp -p t85 -U flash:w:"main.hex":a

IMPORTANT: The indention at the beginning of the lines MUST be tab and not just spaces.

Make sure the programmer and the mocrocontroller board are connected to the USB port.

Run the make to execute the script.

make

This will compile the source code, conver it to HEX and upload it to the board.

The real “Hello, World!” program

For how to write a real “Hello, World!” program that actually does the Blinking LED, look at our “AVR GCC Hello World” tutorial.

From our experience working with schools and student groups, reducing the number of installation steps helps beginners get started faster when learning the ATtiny85. That is why we also use an online C editor and compiler called Blocktinu Code for introductory microcontroller activities.

Blocktinu Code - C code editor for ATtiny85 MCU AVR
Item added to cart.
0 items - $0.00