Install AVR Toolchain with Code Blocks

What is an AVR Toolchain?

This is a 2nd instructable on Toolchains. In the first we described ATMEL’s Toolchain and use Notepad++ to run it. 

 

AVR Toolchain is a collection of tools/libraries used to create applications for AVR microcontrollers. This collection includes compiler, assembler, linker, and binutils (GCC and Binutils) and Standard C library (AVR-libc).

Arduino IDE is a great tool and has been positive for newbies, like myself, to get into the wonderful world of microcontrollers. However, the downside has been hiding “under the hood” the internal process of compiling, linking libraries and even the uploading of the program (by means of avrdude) into the chip.

The Toolchain “liberate” and show us how the process works. Following, a schematic of a simple compiling process:

The next table shows, at the time of writing, the Toolchains that I have worked with:

Toolchain Table

As you can see, all but Kemble’s Toolchain use same binutils and avr-libc but differ somehow in the GCC’s compiler version.

There are many ways you can run the compiling process of a Toolchain. For example:

  1. Manually run the GCC command lines in your DOS prompt or Git Bash shell,
  2. Run the “Make” tool, which is a sort of a script program or batch file but with many more features,
  3. Let the Integrated Development Environment (IDE) manage the compiling process, e.g., the way Arduino IDE does it.

In a previous post, we used Microchip AVR Toolchain (3.6.2) to compile a program “main.c” file and we process it by giving two examples: 1) via a “Make” tool and 2)  by manually typing the commands directly in avrdude.

This time we’ll do it with Code Blocks, a cross platform IDE, that can compile the process and which let us choose the Toolchain to be used.

Microchip AVR Toolchain (3.6.2) has a compiler GCC version 5.4.0 while Kemble’s has an updated toolchain with a GCC 9.2.0.  Let me clearly state that I am not an expert in these toolchains, let’s upgrade our AVR Toolchain we the latest Zak Kemble’s GCC and use Code Blocks to edit and upload a program into an atmega328p.

In order to have an AVR Toolchain you’ll need:

  • Zak Kemble’s latest AVR Toolchain (latest gcc, binutilities, avr-libc, etc.),
  • Latest Avrdude version,
  • srec_cat tool,
  • Code Blocks IDE.

Zak Kemble’s AVR Toolchain: go to Zak Kemble’s Website and download the latest AVR Toolchain (at the moment 9.2.0):

  • Download from https://blog.zakkemble.net/avr-gcc-builds/
  • Extract files into C:\Program Files (x86)\avr-gcc-9.2.0-x86-mingw
  • Your GCC compiler should be at C:\Program Files (x86)\avr-gcc-9.2.0-x86-mingw\bin\avr-gcc.exe

Avrdude (6.3 version): Download latest version (something like ….avrdude-6.3-mingw32.zip)

avrdude 6.3

srec_cat (1.63 version): SRecord package is a collection of powerful tools for manipulating EPROM load files. It reads and writes numerous file formats, and can perform many different manipulations.

Download latest version:

  • Extract files in a folder. In my case I have it in C:\Program Files (x86)\srecord-1.63-win32

Took me a while to understand how to configure and how srec_cat works in this context.  More on this later…..

Code Blocks: is a free C, C++ and Fortran IDE built to meet the most demanding needs of its users. It is designed to be very extensible and fully configurable. It also can be configure to run an external AVR Toolchain.

  • Download from http://www.codeblocks.org/
  • Open Codeblocks
  • Settings->Compiler->From the drop down list at the top select “GNU gcc compiler for AVR”
  • Toolchain executables->Compilers installation directory: “C:\Program Files (x86)\avr-gcc-9.2.0-x86-mingw\bin”
    • C compiler: avr-gcc.exe
    • c++ compiler: avr-g++.exe
    • linker for dynamic : avr-g++.exe
    • linker for static: avr-ar.exe
  • Hit ok

Create avr project:

  • File->New->Project->AVR-> follow the wizard

Configure Avrdude:

  • Tool->Configure Tools->Add…
    • Name: avrdude
    • Executable: C:\Program Files\avrdude\avrdude.exe
    • Parameters: -P usb -b 19200 -c usbtiny -p $(MCU) -U flash:w:${TARGET_OUTPUT_BASENAME}.hex -U eeprom:w:${TARGET_OUTPUT_BASENAME}.eep -U lfuse:w:${TARGET_OUTPUT_BASENAME}.lfs -U hfuse:w:${TARGET_OUTPUT_BASENAME}.hfs -U efuse:w:${TARGET_OUTPUT_BASENAME}.efs -U lock:w:${TARGET_OUTPUT_BASENAME}.lock
    • Working Directory: ${PROJECT_DIR}/${TARGET_OUTPUT_DIR}

Done configuring Code Blocks!

Before starting, some brief explanation how srec_cat works…

Enter to Pre/post builds steps by:

Project->Build options->Pre/post build steps->Select the project name in the tree control on the left…..and will see the srec_cat commands:

Project post Build

Which translates into:

srec_cat bin\Release\Blinky.fuse -Intel -crop 0x00 0x01 -offset  0x00 -O bin\Release\Blinky.lfs -Intel
srec_cat bin\Release\Blinky.fuse -Intel -crop 0x01 0x02 -offset -0x01 -O bin\Release\Blinky.hfs –Intel
srec_cat bin\Release\Blinky.fuse -Intel -crop 0x02 0x03 -offset -0x02 -O bin\Release\Blinky.efs -Intel

These commands are built by default by Code Blocks and extract the low, high and extended fuses in order to later be used as an input in avrdude parameters (more on this later..).

When creating a project you have the option of generating a .fuse file where you can establish the values for your low, high and extended fuses and here is where the srec_cat come into play. There is an avr-objcopy command that creates a .fuse file and the srec_cat commands will extract each fuse so that, later, when avrdude uploads the Blinky file it will also burn the fuses! Nice, right? Alternatively, you can omit creating this .fuse file and just configure the fuses via your favorite ISP programmer and avrdude.

Path variables: Include the address in System variables path.

  • Include C:\Program Files\avrdude in the %PATH%.
  • Include C:\Program Files (x86)\srecord-1.63-win32 in the %PATH%

Manually update PATH by going into Control Panel and then:

  • System
  • Advance system settings
  • Advance TAB; environment variables….
  • System variables; path; New or Edit

Path variables

Verify that installations and paths are working ok:

  • In command prompt, run avrdude –v…and you should get a response of the current version of the installed compiler (in this case it will be 6.3).
  • In command prompt, run srec_cat –vers…and you should get a response of the current version of the installed compiler (in this case it will be 1.63D001).

Congratulations. You are done!!

Now let’s try an example:

Compiling and uploading process

As an example let’s use a blink led program (main.c) to be compiled and uploaded into an Atmega328p microcontroller. We will be using USBTinyISP programmer and using 16mhz external xtal. Make sure you have the correct fuses: Lfuse 0xFF; Hfuse 0xD9.

We will use the AVR Toolchain that we’ve just installed. Use Code Blocks to open the following main.c.

#include <avr/io.h>
#include <util/delay.h>

/*
Atmega328p
Make sure you have the correct fuses:
if run with Internal 8 Mhz div by 8 = 1 Mhz; Lfuse 0x62 Hfuse 0xD9
if run with External oscillator 16mhz; Lfuse 0xFF Hfuse 0xD9
*/

int main (void)
{
DDRB |= 1<<DDB1; // Data Direction Register and assigning pin1 on portB to Output

while(1)
{
PORTB |= 1<<PB1;		// Assigning 5V to portB 1 pin15
_delay_ms(1000);		//delay of 1000 ms
PORTB &= ~(1<<PB1);		// Assigning 5V to portB 1 pin15
_delay_ms(1000);		//delay of 1000 ms

}
}

Compile and upload

  • File->New->Project…
  • Create a project and let’s call it Blinky.
  • A file main.c and fuse.c will be created.
  • Copy the above program into main.c.
  • Modify .fuse file as follows: .low = 0xFF and .high = 0xD9; you can leave extended as it is (.extended = EFUSE_DEFAULT).
  • Press build in menu (or re-build in case you are compiling again).
  • Watch your build log…cross your fingers…and hopefully everything is OK. No errors!
  • Got to tools, and hit avrdude.
  • You are Done!

NEXT STEPS…….

Are we done? Let’s take a look at our previous summary Toolchain table:

Toolchain Table

Let’s see….we have the latest GCC and Binutils..but there are some microcontrollers that are not in this Toolchain….such as ATmega328pb.

None of the above has it but Atmel Studio 7. So how can we update the Toolchain to include this uC ?:

I am no expert and do not have an ATmega328pb myself to test this so I’m just giving guidelines from what I read in the web:

  • Download latest pack from https://packs.download.microchip.com/
  • As of the day of this write-up: “Microchip.ATmega_DFP.2.1.87.atpack”
  • Need to replace .atpack with .zip in order to open it (Microchip.ATmega_DFP.2.1.87.zip)
  • In ….\AVR pack 2.1.87\avr-gcc\dev…you will find “crtatmega328pb.o” and “libatmega328pb.a”
  • In ….\AVR pack 2.1.87\include\avr…you will find “iom328pb.h”
  • Put the lib and the crtatmega328pb.o into “\avr\lib”…\avr-gcc-9.2.0-x86-mingw\avr\lib\avr5
  • Put the io*.h into “avr\include\avr”…\avr-gcc-9.2.0-x86-mingw\avr\include\avr
  • Finally we need to update “avrdude.conf”. Include the following below the atmega328 part:

part parent “m328”
id                  = “m328pb”;
desc                = “ATmega328PB”;
signature           = 0x1e 0x95 0x16;
ocdrev              = 1;
;

ATmega328pb

I hope this instructable has giving us a better understanding of AVR Toolchains and how we can use Code Blocks IDE in compiling and flashing a uC. Happy flashing!

REFERENCE

http://nongnu.org/avr-libc/user-manual/pages.html
https://www.gnu.org/software/make/manual/html_node/Options-Summary.html
https://blog.zakkemble.net/avr-gcc-builds/
http://wiki.codeblocks.org/index.php?title=Variable_expansion#Language_and_encoding
https://packs.download.microchip.com/
https://github.com/watterott/ATmega328PB-Testing

One comment

  1. […] I have written a new instructable where we can use the excellent Zak Kemble’s latest GCC 9.2.0 version and I have used Code Blocks IDE to run an AVR Project. Click here.  […]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: