How to get started with the Atmega32u4 Breakout Board+ on Linux

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
kasbah
 
Posts: 13
Joined: Fri Jun 17, 2011 5:37 am

How to get started with the Atmega32u4 Breakout Board+ on Linux

Post by kasbah »

The Atmega32u4 Breakout Board+ is a great little board for people interested in moving away from Arduino and wanting to get started programming microcontrollers in C. It comes pre-loaded with a bootloader that allows programming without having to invest in an external programmer.

But when I received my Atmega32u4 Breakout Board+ I was a bit confused on how to get started programming it. The product page mainly covers hooking it up with the Teensydunio IDE but has little info on using programming in C and using gcc-avr, make and avrdude.

I presume a basic familiarity with using the Linux command line and how to install packages on your distribution. Some experience with programming C using make and basic electronics knowledge will might help but you can probably get through this tutorial without it. This tutorial is written for Linux users but it's probably not too different from people using WinAVR etc but I have never tried these so I cannot adapt it for it. Maybe a helpful forum user would be able to post amendments to make this work for other platforms.

This basic tutorial will cover getting all the right tools in place and hooking up an LED and writing, compiling and flashing a simple program that will flash the LED.

Requirements:
  • Atmega32u4 Breakout Board+
  • Mini-USB to USB cable to hook up the Breakout Board
  • Breadboard
  • LED and appropriate current limiting resistor*
  • A computer running Linux
*If you don't know what this means then I suggest reading up on LEDs and current limiting resistors. You have to check the forward voltage drop and the forward current of your LED and then work out the appropriate resistor value. The pins on the Atmega32u4 supply 5v so we can work it out as follows:
(5V - LED forward voltage drop) / LED forward current = resistance


Firstly, follow the product page up to the point where it mentions Teensyduino. You should have now have a Breakout Board with headers soldered and you can place it onto a breadboard and hook it up with your USB cable to your PC. You should see the red PWR LED light up.
Image
Now on your computer make sure you have all the appropriate packages installed. You will need packages that contain:
  • GNU Make
  • avr-gcc
  • avr-libc
  • avrdude

If you have all of those then you can proceed. Make a new folder and cd into it in your terminal. Make a new file "main.c" and edit it to contain:

Code: Select all

int main(void) {
    return 0;
}
This program does nothing at all except return successfully and we will use it merely to test if all our tools are in place and working correctly.
To compile it we use gcc-avr:

Code: Select all

$ avr-gcc -mmcu=atmega32u4 -Wall -c main.c -o main.out
If this worked correctly you should now see a main.out file. Let's break this down: "-mmcu=atmega32u4" selects the AVR, "-Wall" tells the compiler to output all warnings, "-c main.c" selects the C file to compile and "-o main.out" selects the output file.

We will now determine what the serial interface on our Breakout Board. With the board still connected press the reset button on it. This should enable the bootloader on the Breakout Board and the green "Boot" LED should be dimming and lighting up (breathing). Now type "dmesg" in your terminal. You may have to do "sudo dmesg" depending on what privileges your distribution gives you. On the last few lines you should see something like this:

Code: Select all

[  208.440047] usb 5-1: new full speed USB device number 2 using uhci_hcd
[  208.836738] cdc_acm 5-1:1.0: ttyACM0: USB ACM device
[  208.839901] usbcore: registered new interface driver cdc_acm
[  208.839906] cdc_acm: USB Abstract Control Model driver for USB modems and ISDN adapte
What is important here is "ttyACM0" on the second line. This is the usb-serial port of the Breakout Board. If you have other usb-serial devices connected this may appear as "ttyACM1" or "ttyACM2" or even higher. You will need to to replace "ttyACM0" with whatever shows up for you in the steps following this.

Now we will use avrdude to upload your compiled file to the Breakout Board. Press the reset button again to make sure the bootloader is enabled and then invoke avrdude while the Boot LED is still green:

Code: Select all

$ avrdude -p m32u4 -P /dev/ttyACM0 -c avr109 main.out
You should see avrdude do it's thing and say "Thank you" at the end. Let's break this down: "-p m32u4" selects the type of AVR, "-P /dev/ttyACM0" selects the correct port, remember to change this to the port we determined earlier if yours is differemt, "-c avr109" selects the programmer id which is determined by the bootloader and main.out selects the file to write.
As we will be recompiling and re-flashing quite often, it makes sense to write a Makefile which will automate the task. Write a file called "makefile" that contains the commands we are using:

Code: Select all

all:
	avr-gcc -mmcu=atmega32u4 -Wall -c main.c -o main.out
writeflash:
	avrdude -p m32u4 -P /dev/ttyACM0 -c avr109 main.out
Now you can compile by simply typing "make" and re-programm using "make writeflash". If you want to try this first remove the compiled file:

Code: Select all

$ rm -f main.out
Then:

Code: Select all

$ make
Then, press the reset button and run

Code: Select all

$ make writeflash
You should see the same outputs as when we invoked avrdude directly. Let's add the removal to the makefile as well:

Code: Select all

all:
	avr-gcc -mmcu=atmega32u4 -Wall -c main.c -o main.out
writeflash:
	avrdude -p m32u4 -P /dev/ttyACM0 -c avr109 main.out
clean:
	rm -f main.out
Now you can remove main.out by typing "make clean". This is needed sometimes if you make changes to the code and the compiler doesn't realize and may not recompile properly.

Now, this hasn't been very impressive as the code does absolutely nothing. Let's now hook up the LED to pin B0. The long, positive end of the LED goes into the port, then to the resistor and the resistor goes to ground.
Image
Now let's change the code in main.c to blink the LED:

Code: Select all

#define F_CPU 16000000
#include <avr/io.h>
#include <util/delay.h>

int main (void)
{
	DDRB = 0x01; //set up pin 0 on port B
	while (1) //loop forever
    {
        PORTB = 0x01; //set pin 0 on port B high
	    _delay_ms(500);
	    PORTB = 0x00; //set pin 0 on port B low
	    _delay_ms(500);
	}
	return 0;
}
The first line set's the CPU frequency to 16Mhz which is what it is clocked at on the BReakoutBoard. The first include enables the IO pins and the second the delay function. The main function runs an infinite loop which turns the LED on and off delaying 500ms after each action.
We have to modify our makefile slightly to include compiler optimizations which the delay library needs. Change:

Code: Select all

	avr-gcc -mmcu=atmega32u4 -Wall -c main.c -o main.out
to

Code: Select all

	avr-gcc -mmcu=atmega32u4 -Wall -Os -c main.c -o main.out
Now you can run "make", press the reset and run "make writeflash" and you should see your LED blink! Celebrate!

There are people out there that are much better at writing makefiles than we are and it might be worth using a pre-written one instead. I recommend the one from psychogenic and with the following modifications it is usable for this project:

Code: Select all

MCU=atmega32u4
PROGRAMMER_MCU=m32u4
PROJECTNAME=main
PRJSRC=main.c 
AVRDUDE_PROGRAMMERID=avr109
AVRDUDE_PORT=/dev/ttyACM0

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm

Re: How to get started with the Atmega32u4 Breakout Board+ on Linux

Post by adafruit »

thats a nice tutorial! do you mind if we add it to our tutorial list and/or format it for our site (and give you attribution)?

User avatar
kasbah
 
Posts: 13
Joined: Fri Jun 17, 2011 5:37 am

Re: How to get started with the Atmega32u4 Breakout Board+ on Linux

Post by kasbah »

Yeah sure! Use it wherever you like. Would be good if you could correct some of the spelling and grammar too :)

cody
 
Posts: 3
Joined: Sun Oct 09, 2011 10:36 am

Re: How to get started with the Atmega32u4 Breakout Board+ on Linux

Post by cody »

I followed this nice tutorial but it didnt work. The bootloader LED is always blinking and my program doesnt seam to get executed.
There are no error messages.
What could be wrong?
(It is the first time i work with an USB-Atmega)

User avatar
kasbah
 
Posts: 13
Joined: Fri Jun 17, 2011 5:37 am

Re: How to get started with the Atmega32u4 Breakout Board+ on Linux

Post by kasbah »

Cody, when you plug the Board into the computer the green LED is blinking all the time?

cody
 
Posts: 3
Joined: Sun Oct 09, 2011 10:36 am

Re: How to get started with the Atmega32u4 Breakout Board+ on Linux

Post by cody »

yes!

I wonder how the bootloader decides if it should run itself or my program.

I use linux and the board is displayed as /dev/ttyACM0.

User avatar
kasbah
 
Posts: 13
Joined: Fri Jun 17, 2011 5:37 am

Re: How to get started with the Atmega32u4 Breakout Board+ on Linux

Post by kasbah »

then it is a problem with the board or bootloader most likely. or are you saying you get different behaviour on a non-linux machine? it should only be green for a bit when you press the button.

cody
 
Posts: 3
Joined: Sun Oct 09, 2011 10:36 am

Re: How to get started with the Atmega32u4 Breakout Board+ on Linux

Post by cody »

No i didnt try it on a windows machine yet. So should I reflash the bootloader with an ISP-programmer?

User avatar
kasbah
 
Posts: 13
Joined: Fri Jun 17, 2011 5:37 am

Re: How to get started with the Atmega32u4 Breakout Board+ on Linux

Post by kasbah »

You could check the voltage on the pin marked RST. This should be at 5V and if it is at 0V then you probably have a short on the board somewhere. Maybe the button is broken for instance. I am sure adafruit support is more qualified to help you out with this problem though.

superm
 
Posts: 3
Joined: Fri Dec 02, 2011 1:04 pm

Re: How to get started with the Atmega32u4 Breakout Board+ on Linux

Post by superm »

Hi,

I just ran into the same problem, but I could get a bit further by using the '-U' parameter, when flashing the file to the chip. This line worked:
(apparently, the file was not copied at all ...)

Code: Select all

writeflash:
       avrdude -p m32u4 -P /dev/ttyACM0 -c avr109 -U main.out
Now the boot led stopped blinking, but the actual led did not start blinking ... May be the part for storing the 'main.out' file should not be written to flash, or to some other location?

Greetings,
superm

User avatar
kasbah
 
Posts: 13
Joined: Fri Jun 17, 2011 5:37 am

Re: How to get started with the Atmega32u4 Breakout Board+ on Linux

Post by kasbah »

Are you using the Arduino Bootloader or the original Adafruit one?

I can't seem to reload the original bootloader and so can't go through the steps again myself. I think I will re-write it again for the Arduino bootloader.

I have also realised that there is no step to convert to HEX. Can someone explain to me why this still worked?

superm
 
Posts: 3
Joined: Fri Dec 02, 2011 1:04 pm

Re: How to get started with the Atmega32u4 Breakout Board+ on Linux

Post by superm »

I'am using the bootloader shipped from adafruit...

I thought building with avr-gcc generates the right binary format. If you use the -c compile option from gcc, the output is some how in wrong format, at least this is was my output reads:

Code: Select all

avrdude: reading input file "main.out"
avrdude: input file main.out auto detected as invalid format
avrdude: invalid input file format: -1
avrdude: write to file 'main.out' failed
So, if you don't use this flag, the file can be transformed, I am just unsure, if it is the right place/offset/...

superm
 
Posts: 3
Joined: Fri Dec 02, 2011 1:04 pm

Re: How to get started with the Atmega32u4 Breakout Board+ on Linux

Post by superm »

Finally I got it to work by using a default makefile from another forum: http://www.mikrocontroller.net/wikifiles/b/b6/Makefile

+ adopting it to my needs (only changes displayed)
MCU = atmega32u4
AVRDUDE_PROGRAMMER = avr109
AVRDUDE_PORT = /dev/ttyACM0 # programmer connected to usb device

now with 'make program' the given main.c file will be compiled, linked, converted and flashed to the chip. Now i get a nice blinking led ... Awsome ... ;-)

alwindavis
 
Posts: 1
Joined: Sun Sep 09, 2012 3:16 pm

Re: How to get started with the Atmega32u4 Breakout Board+ on Linux

Post by alwindavis »

Hi,
im using m32u4 mcu on windows platform. I have two boards with me in which i tried to load the the led blinking code. while one works fine another one gives out errors.
avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x0000
0x0c != 0x00
avrdude: verification error; content mismatch

avrdude done. Thank you.
i keep getting the mismatch at same location in the flash with different other codes and i suspect avrdude is not able to erase the chip and load new programs. any solutions for this error?

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: How to get started with the Atmega32u4 Breakout Board+ on Linux

Post by adafruit_support_rick »

Are these adafruit boards?

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”