Category Archives: open source

Taking Arduino to the next level with avr-gcc: Part 2

Our AVR toolchain

For our example project, we will create two similar toolchains – one for Mac OS X and one for Linux.  Here’s a visual overview of how code will flow:

toolchain.png

If you’ve developed with C or C++ before, the diagram should look familiar. I’m assuming just one object here – if we had more, avr-ld works much the same as our traditional linker does. And, of course, we can write C++, tossing avr-g++ in for avr-gcc.

On the Linux side, I chose not to set up an IDE, but maybe my Mac approach will help you set one up. For Mac, I set up a Makefile-based Xcode project and run script to invoke these tools in order and provide the “pretty” Build Results screen. Someday, I hope to introduce gdb on-device debugging, but for now, it’s Build and Run only.

Toolchain Construction: Mac OS X

I’m assuming that if you’re reading this, you probably already have Xcode. If not, it’s not strictly necessary – you can follow the text editor + command line formula outlined for Linux equally well on OS X.

Our big helper here is MacPorts. If you have the latest version already installed, you can skip the next section.

Installing MacPorts

MacPorts is a great package management tool for Mac OS X, akin to ports on BSD and apt-get on Linux. A .pkg (standard OS X) installer is available here. Download the .dmg, mount it, and double click the Installer package. The usual installer semantics apply here.

Updating MacPorts

I neglected this step and it bit me hard.

Fire up Terminal.app and issue:

sudo port -v selfupdate

which will ask you for your password. Now, we can start the fun of actually installing the appropriate packages from the repository.

Installing Relevant Packages

We may not need all of these right away, but here’s our grocery list:

  • avr-binutils – tools for cross-platform development with AVR
  • avr-gcc – the bread and butter of our toolchain, obvious from its name.
  • avr-gdb – again, from the name, the GNU debugger for AVR.
  • avr-libc – some core C functions for use on the AVR platform. Without this, our programs probably won’t be very useful (or pretty).
  • avrdude – AVR flasher software.

To install them, again use Terminal.app, this time with the command:

sudo port install avr-binutils avr-gcc avr-gdb avr-libc avrdude

You may see some unrecognized packages floating by; these are likely dependencies of the packages we wish to install. It is normal for this process to take quite some time. Let’s all go to the kitchen and have ourselves a snack!

Test the new toys

After some time, everything will be built and ready to go. This is the moment of truth where we see if your PATH environment variable is set up to play nicely with MacPorts.

Create a new Terminal window and try running the command:

avr-gcc -v

The response should end with something like

gcc version 4.0.2

If that’s so, it looks like you’re ready to rock!

Toolchain Construction: Linux

For Ubuntu users, grab the following packages via apt-get or synaptic:

  • binutils-avr – tools for cross-platform development with AVR
  • gcc-avr – the bread and butter of our toolchain, obvious from its name.
  • gdb-avr – again, from the name, the GNU debugger for AVR.
  • avr-libc – some core C functions for use on the AVR platform. Without this, our programs probably won’t be very useful (or pretty).
  • avrdude – AVR flasher software.
  • avarice – If we need to do GDB over JTAG.
  • simulavr – Simulates AVR hardware for debugging and testing.

If you’re down with the terminal (and I hope you are), grab them all at once with the command:

apt-get install binutils-avr gcc-avr gdb-avr avr-libc avrdude avarice simulavr

Create a new Terminal window and try running the command:

avr-gcc -v

The response should end with something like

gcc version 4.3.4

If that’s so, it looks like you’re ready to rock!


UPDATE: Part III is available here.

Taking Arduino to the next level with avr-gcc: Part 1

Why Arduino?

Arduino Duemilanove.  Similar to the Diecimila but with handy auto-power-switching!
In 2007, I was looking for a fun way to get into the lower level of programming to get into the physical computing realm. My brother had been playing with the Arduino, so I ordered a Diecimila from Adafruit Industries. Arduino development is typically done in the eponymous IDE based on Wiring and Processing. That was more than sufficient for most of my early explorations – I’m by no means an electrical engineer, so making a few LEDs light up based on a potentiometer’s input was plenty of fun.

A new project

Now that I’ve had some time to experiment, the usual happened: I’ve started to see interesting possibilities all over the place. I’ll be following my development of a TED-style countdown timer and in the process, I’ll release source for a digit display library that uses the TI CD4067BE multiplexer to save some of the Arduino’s precious output pins.

I’ve gone through the motions of setting up avr-gcc on Mac OS X and on Linux, so I’ll be covering both of those paths in the next post. For now: A bit of background.

Background: Arduino and AVR

The Arduino hardware platform supports one key piece of silicon that is the object of interest for lower-level programming: an Atmel AVR microcontroller.

AVR


Each microcontroller in Atmel’s AVR family is an 8-bit RISC processor with embedded non-volatile flash memory. They are available with a variety of form factors and flash capacities, as well as some other specialized features.

A variety of form factors!

Like many other RISC architectures, AVRs have 32 processor registers. Here, they are 8-bit wide.

The particular model used in the Arduino Diecimila is the ATMEGA168-20PU running at 16MHz. It contains 16KB of flash, 1KB of SRAM, and most instructions in the set are single-clock execution.

blockdiagram.png

Here, the key aspects of the diagram to notice are the CPU connected to Flash and SRAM and the ports D, B, and C at the bottom. Obviously enough, we’ll be storing our programs in that Flash. The Ports B, C, and D map to the digital and analog inputs and outputs of our Arduino – for instance, Port B.6 maps to the digital pin 9 of the Arduino board. That will be examined in more detail once we get to coding.

Arduino

Arduino’s hardware provides a lot of tools that make working with the AVR simpler. Almost all Arduino boards come equipped with an FTDI FT232RL USB-to-serial adapter on-board to provide serial communications for debugging and programming. USB also usually can provide 5V power to the board, including the AVR chip. Finally, as mentioned above, the Arduino mainline boards provide breakout headers for many of the pins on the DIP, such as Ports B-D.

arduino-nano.jpg
Some specialized Arduino form factors, such as the Nano, opt instead to make the Arduino board itself breadboardable, providing pin-outs to plug it directly into a breadboard.

The Arduino board also provides the 16.0MHz clock and some LEDs to inform us when flash memory is being read and written over serial. A reset button is present on most Arduinos.

Returning to the serial adapter: we use serial to write our programs to the AVR’s Flash memory when using the Arduino toolchain. Internally, the Arduino IDE uses a tool called avrdude (AVR Downloader/UploaDEr) to open the appropriate channels and write to the flash. Using this serial link rather than a dedicated programmer is called ICSP – in-circuit serial programming. When you buy an Arduino, the AVR chip itself has been pre-flashed with an Arduino “bootloader” that facilitates this programming via serial rather than requiring an external programmer. If you wish to build a new home for your Arduino-based designs and use a fresh AVR, you may need to flash in the bootloader using an actual programmer like the AVRISP or USBtinyISP.

Conclusion, Part 1

When you use the word “Arduino”, there is a lot of ambiguity as to what you refer to. It could mean:

  • The platform and initiative as a whole
  • The physical hardware board
    • Old serial-based boards
    • Diecimila
    • Duemilanove
    • Nano
    • … and several others.
  • The Arduino IDE
    • the Wiring language
    • the Processing-like front-end
  • The Arduino bootloader

Hopefully those distinctions are now clear and we have a basic understanding of what the AVR’s role is in that picture. Next time, we’ll look at installation of our toolchain – avr-gcc, avr-libc, and avrdude – on Mac OS X and Ubuntu Linux.


UPDATE: Part II is available here.