AVR STK500 on OS X

AVR STK500 on OS X

So I bought my own copy STK500, and I installed my version of Fusion, with Windows 7 in order to run AVR Studio for programming my ATMega8515 microcontroller with the STK. After 2 weeks, I finally managed not only to properly operate the STK on a Mac booted on Windows 7, but to use it with Fusion, Parallels, and OS X as well. So, in order to save you some time, here is a quick walkthrough on how to develop software for Atmel’s AVR using the STK and a Mac. In this article you will also find information about AVR setup on OS X in general.

Obtaining a USB-Serial cable

Macs, as well as new PCs do not come with a serial port that is needed for developing software for any AVR. So, to make sure that everything will work as expected, you need to buy a USB-to-Serial cable (or a RS-232 adapter that is essentially the same thing with another name). I chose to buy the cable from SerialIO, because specific Intel OS X drivers are provided. For the windows part, windows drivers are also provided, but things are more complicated there.

OS X drivers are based on the standard drivers that come with Prolific. Prolific manufactures the chip that comes with every usb-serial adapter, and each company uses that chip into their products. Because of the open source basis of those drivers, and because the Mac always complies with the universal standards, any drivers, either from prolific themselves, or from serialIO will work on the mac.

On windows, this is not always the case. I have seen cables to work with the mac, but not with Windows on the same machine, as well as other cables that work with both platforms on the same machine with the same drivers. USB-Serial cables are not always trustworthy. In order to test whether your cable is recognized by the Mac, visit System Profiler, and check the USB section and see if the USB-Serial device is recognized under the company “Prolific” (of course, your cable must be connected to a USB port on your Mac).

For  the windows counterpart, if you realize that windows cannot recognize the cable, perhaps you should try these drivers. I found them by searching a lot in the internet. Many people have said that these drivers solved them many problems with their adaptors. I have tested them on Windows XP Home SP3 and Windows 7 and they work flawlessly, where other drivers from prolific failed (especially with Windows 7). In order to use the cable with a virtual machine, make sure that you have installed the appropriate drivers on BOTH the virtual machine and the Mac. With the drivers I gave you, I managed to use the cable with Fusion and Windows 7.

The software

Well, for Windows you don’t have many choices, but the already available ones will do just fine. Just go to Atmel, and download AVR Studio 4, with its service packs, and you are done. If you have done everything correctly, the application will have no problem flashing any program onto the ATMEL STK500. If you want to program in embedded C, you must download and install WinAVR. Then, without any other effort, after restarting AVR Studio it will offer you the option of creating a GCC project, which is essentially an AVR project where you can program in C.

If you want to program using XCode, then that’s a whole different story. You will certainly need to download Crosspack, a package that includes the AVR-GCC package, avrdude, objcpy and many other command line applications, headers and sources that are needed to write programs and flash them onto the STK. No restart is required.

in the crosspack website there is a small tutorial on programming the AVR using the terminal. It’s on their main page. However, I should point you out into other manual pages, mostly because you need to change a bit the arguments for AVRDUDE in order to specify which type of microcontroller you have, and which programming platform you use. I use the one located here.

Xcode Integration Part 1: Explanation

As I found out the hard way, AVR-Studio is not the best tool for writing code. No code folding, no correct syntax coloring, no code completion.  During my efforts to integrate Xcode with Avr-GCC, I stumbled upon a template, in which I made some improvements, and will continue to make as I proceed with development on AVR microcontrollers. The template I give you is the original one.

Download the template here.

Inside that template there is a makefile, that is responsible for generating the executables, and placing them onto the AVR. The project does not have a physical target, it’s just the makefile that does the hard work. All targets in the Xcode project are actually command line arguments for the makefile, while the project is built. That’s why the “Build and Run” button is disabled.

Xcode Integration Part 2: Setting up the MakeFile

All the hard work that you need to do is to provide the correct device specifications for the AVRDUDE program, such as the fuses, the programmer id (in this case we have the STK 500)  and the AVR model you will be programming on. You can find those information in  the official AVRDUDE reference guide.

You will need the following information:

  • The programmer id. In our case, for the STK500, it is ‘stk500v2’
  • The AVR model ID for the AVR-GCC compiler. The compiler needs to know for what microdevice the code will be compiled. For me, it is ‘atmega8515’ .
  • The AVR model ID for AVRDUDE. AVRDUDE needs to know this in order to search for the microdevice in the stk500.
  • (OPTIONAL) A project name.
  • The fuses for the AVR. Fuses are 2 bytes (or more in some cases) that alter the behavior of the microcontroller in several ways. You can read about those switches and what they do in your AVR’s datasheet. I found out what values are needed by using AVR Studio, which explains the fuses and what they do in the case of ATMEGA8515.
  • Last but not least, the programmatic location of your USB-to-Serial adaptor. How do you find this? Well, a little terminal is necessary here. Open the terminal and type these 2 commands while the adaptor is connected to the Mac:cd /dev
    ls A large list of available items is displayed. The one you are interested in is probably the one that contains “usb”, or”serial” inside it. In my case, the path to the item is “/dev/cu.usbserial”.

For example, I have the STK500 with the ATMEGA8515 on it. So, in the makefile script, I set the following (the variables already exist, but they are set to different values than the ones desired):

Here is the altered variables and their corresponding values for my case. Note that depending on your microcontroller, some (if not all) values will be different.

MCU = atmega8515
PROGRAMMER_MCU = m8515
PROJECTNAME = myproject
AVRDUDE_PROGRAMMERID = stk500v2
AVRDUDE_PORT = /dev/cu.usbserialFUSES = -U hfuse:w:0x40:m -U lfuse:w:0xA4:m

Xcode Integration Part 3: Compiling, and fusing the program into the AVR.

The procedure is normally this:

  • Select the “build” target and build. Normally this would work. But in my case, it didn’t.

The procedure that must be followed if the previews didn’t work is to compile the targets in the following order:

  • clean
  • debug_init
  • fuse
  • flashThat should do it.Personal Note: Improving the scriptIn the script, I made a new addition at the end of it:
mine:$(REMOVE) $(TRG) $(TRG).map $(DUMPTRG)
$(REMOVE) $(OBJDEPS)
$(REMOVE) $(LST) $(GDBINITFILE)
$(REMOVE) $(GENASMFILES)
$(REMOVE) $(HEXTRG)
$(TRG)
$(AVRDUDE) -c $(AVRDUDE_PROGRAMMERID) -p $(PROGRAMMER_MCU) -P $(AVRDUDE_PORT) -e-U flash:w:$(HEXROMTRG)

Perhaps it needs some improvements, but at the time it cleans over the target MCU, builds a new .hex file, and uploads it to the stk. After that, you can make a new target in the Xcode that runs the makefile script with the argument “mine”  or you can go to the terminal, and after you change directory to the “firmware” folder inside your project, you can type “make mine”, and the script will take care of everything.

Outro

That’s all. Maybe the article was long, but the actual things you need to remember are quite few. This article will be improved from time to time with new information, and perhaps some source samples.

Edit 2012-07-01: Changed the links for the drivers and the template. Now they should work.

Interesting Links:

Electron’s AVR Articles
AVR Freaks forum
Sparkfun Electronics Tutorial for Embedded Devices
AVR Instruction Set (Assembly)
AVR Fuses (Cross platform Application for fusing AVR bits. Includes explanations)
XCode Template for working with AVR on Xcode