Micromouse book
Categories
Recent Comments
Meta
Popular Posts
- Simple ADC use on the STM32 (3,936)
- STM32 Arm-Cortex bootloader (2,658)
- STM32 USART basics (2,538)
- All Japan Micromouse 2011 – finals (1,970)
- STM32F4 – the first taste of speed (1,615)
- Micromouse Book (1,532)
- Nokia 3410 LCD on the STM32 (1,267)
- CodeSourcery GNU Toolchain for the ARM on a Mac (1,097)
- Bit Banding in the STM32 (974)
- ARM STM32 JTAG (921)
Blogroll
-
Upcoming Events
-
Feb6Mon
-
Apr14Sat
-
Category Archives: AVR
USBasp on the Mac
The USBasp AVR programmer arrived today so I lost no time connecting it up to give it a try. This is a clever little beast that uses a software USB stack on a pre-programmed ATMEGA8 processor. It works directly with AVRdude to program your target processor with the minimum of fuss.
My example came from an eBay seller in Hong Kong and arrived complete with a suitable USB cable and 10 pin ribbon cable programming connector. I have plenty of cables but it was nice to see them included so that I could get straight on with the testing. How many times have you ordered stuff only to find the necessary cables are nowhere to be found?There you will find a schematic and source code to build your own. I had mine ready-made because the price was well worth paying to save an evening messing about and, in any case, I didn’t have a programmer to allow me to program the programmer. Now I do so I will probably build a couple of spares. For larger projects, it may even be worth building one of these into the project to allow easy programming and upgrading.To use the USBasp programmer with AVRdude, you will need to make sure the correct settings are used. I use a Makefile and the programmer settings look like this:
DEVICE = atmega16 CLOCK = 4000000 PROGRAMMER = -c USBasp AVRDUDE = avrdude $(PROGRAMMER) -p $(DEVICE)
Note that there is no need to specify a port. The USBasp identifies itself as a named device when it is connected to the Mac. Using AVRdude in a Makefile means that you will also probably want to use it to set the fuses on your target device. This is a fiddly business not helped by the way Atmel use inverted logic.As an alternative, you could use AVRFuses.:
This is a GUI interface to AVRdude and is able to show you the fuses specific to your selected processor – a great convenience. You still need to know what the damned things do though.If you use the Makefile generated by AVRMacPack, writing a hex file to the processor is a simple process. Either use AVRfuses to select a .HEX file and send it or use a command line:

make flash
The USBasp has a couple of jumper options (switches on mine). One of these allows the target to be powered from the USB bus. I tried that and had a fair bit of trouble. this should not have been awkward but is worth bearing in mind if you get problems. Powering the target independently eliminated those errors.Since the board is bare underneath and has no mounting holes, I chose to attach it to a small piece of thin plywood by means of a short length of heatshrink tubing. that should make it much less vulnerable to short in the mess that passes for a work environment on my desk:
Find out more about the USBasp programmer here:
http://www.fischl.de/usbasp/
You can get a copy of AVRfuses here:
http://www.vonnieda.org/software/avrfuses
Find out more about setting AVR fuses here:
http://electrons.psychogenic.com/modules/arms/art/14/AVRFusesHOWTOGuide.php
Continue reading
AVR, Eclipse and the Mac
AVRMacPack works well enough in Xcode but I don’t understand Xcode so I wen looking for an alternative. While trawling through back issues of SERVO magazine, I came across a pair of articles by Dennis Clarke about setting up Eclipse with AVRMacPack and the avr-eclipse plugin. One of my recurring mistakes is the feeling that you can’t have enough development tools so I gave it a go…
The AVRMacPack installation, I already had on the machine. It neatly puts everything your need – the compiler, avrdude and so on – into the folder /usr/local for you. Next I had to get, and install a copy of Eclipse. That is mostly straightforward. A suitable tarball can be had from
http://www.eclipse.org/downloads
At the time of writing, the current release is ‘Ganymede’ – version 3.4.2. In the Servo articles, Dennis Clark refers to the ‘Europa’ release which seems to install slightly differently. The tarball contains a single folder called eclipse. I dragged this straight into the Applications folder on my iMac. In there is an application icon for eclipse. It can be dragged to the dock if you want rapid access to it. Otherwise, double clicking will launched Eclipse. By default, it will ask you where to put the workspace. Several workspaces can be created and you can switch between them at will. I renamed this one to distinguish it form another, workspace folder on my machine. At this stage, you could have a little play with the options. I was a bit disappointed in the welcome splash screen since the text by the links didn’t render properly. To see fundamental errors that early in your experience with a product is never encouraging. Now I needed the avr-eclipse plugin. That can be had from:
http://avr-eclipse.sourceforge.net/
The link takes you straight to a wiki and, if you follow the link for downloading, you will find instructions on how to grab the plugin directly from eclipse. It goes like this:
- In eclipse, click Help | Software Updates
- Choose Available Software then Add Site…
- Enter this URL: http://avr-eclipse.sourceforge.net/updatesite/
- After a pause while the site is checked, you should see the AVR Eclipse Plugin listed.
- Tick the appropriate boxes and click the Install… button
- After a while the plugin will have downloaded and you can hit Next and agree to the license.
- Eclipse needs to restart and you should be ready to go./li>
It should be imediately apparent that Eclipse now knows about the AVR because there is a new menu item called (reasonably enough) AVR. This is used to program the target device. For now, you can choose File | new… to create a new C project:
The project is set up without any files so it is now a simple matter of adding or writing the relevant source code files and the project can be built in the normal Eclipse manner. AVRMacPack gives a choice of versions for the GCC compiler to be used. I left mine set as it arrived which uses avr-gcc version 4.3.2. After a bit more fiddling, I tried the programmer. Avrdude is the programmer of choice and I had already got this working on an Arduino board when trying the Xcode tools. In there I had to edit the makefile to get the options right. Here I change the project properties and it is all a lot more comprehensive. The Arduino can be programmed with a direct USB connection thanks to its on-board usb-serial converter and the bootloader that emulates an avrisp (STK500). The programmer needs the -F flag (set in the advanced settings tab) to override the checking of the device signature. The serial speed is set to 19,200 baud and everything works almost like magic. After the project is built, a click of the programming button and it just sorts itself out. Marvellous. If I had realised that avrdude was so handy before, I would have used it ages ago.
AVRMacPack and Xcode
The AVRmacpack for AVR development on the Apple Mac contains command line tools. These are all well and good if you want to do things the traditional way with text editors, terminal windows and make files. No real problem. However, if you want the benefits of a modern IDE with things like function lists and refactoring, you might want to try Xcode. It is, after all, supplied with your Mac.
It took me rather longer than I would have liked to find out how to get going with this. AVRMacPack comes with a sample project template. Once you have installed the tools you will find it in
/usr/usr/local/AVRMacPack/etc/templates/TemplateProject
Interestingly, this appears to be the exact same template that can be found tucked away in the Arduino package for the Mac. To make this template available to Xcode, you will need to copy it in the appropriate place. As far as I can tell, this is
~/Library/Application Support/Developer/Shared/Xcode/Project Templates
If you don’t know, the ~ is your home folder. To make it a bit clearer, rename the TemplateProject folder to something like AVR Project. I also put it into a folder called AVR in case I want to add other AVR project types later.Now, when you do File | New Project in Xcode, you should see a section called AVR in bold and, in there, your AVR project template.the template puts the code into a folder called firmware and places a Makefile in the project follder root. You will need to edit that Makefile to suit the details of your project – processor, clock speed etc.A project created in this way will build using the normal Xcode buttons. So far, however, I have not seem how to avoid opening a terminal to run the make flash command needed to program the target. Also, because the code is not associated with an Xcode target, refactoring will not work. Still, it is progress and I am sure that can be fixed at some stage. I know nothing about Xcode yet… Continue reading
AVR on a MAC: Blinky success
As a first step, the blinking of an LED is an essential part of development for a new processor and environment. I had read somewhere that the bootloader on an arduino was compatible with an avrisp/STK500 programmer. That turns out to be quite right!
Following the instructions in the getting started guide for the AVR Macpack, I created a new project and entered the following for main.c:
#include#include int main(void) { DDRB = 0xff; /* make the LED pin an output */ for(;;){ char i; for(i = 0; i < 10; i++){ _delay_ms(10); /* max is 262.14 ms / F_CPU in MHz */ } PORTB ^= 0xff; /* toggle the LED */ } return 0; /* never reached */ }
the Makefile needed a little bit of editing. This just involved the lines near the top describing the device and the programmer:
DEVICE = atmega168 CLOCK = 16000000 PROGRAMMER = -c avrisp -F -b 19200 -P /dev/tty.usbserial-A9006L3s
The port is where my iMac found the Arduino USB-serial converter. The baud rate needs to be set to 19,200 for the bootloader and I had to set the -F flag to stop avrdude complaining about the device signature. Presumably, the Arduino bootloader code reports itself differently. If I can find out what that is, it would be worth adding some appropriate lines to the avrdude.conf file.After all that a traditional
make make flash
and, after a dab at the reset button, we have a blinky light on the board - always a pleasing achievement. Now to haul out some old AVR kit and do some more experiments...
AVR on the MAC
After many hours playing with the new iMac, it is time to get something ‘proper’ done with it. By proper, I mean, of course, some microcontroller development. To start with, I have been looking at the AVR.
Probably the easiest tool to get going with is the popular Arduino. Ready-to-go packages exist for the Mac which install in the proper way and give you a delightfully simple GUI to write simple, and not-so-simple programs. The various flavours of Arduino board mostly have a USB to serial converter on them and the arduino talks to a bootloader on the chip through this port. From what I can see, arbitrarily complex programs can be written for the target hardware, subject to the processor memory constraints. More on the Arduino another time.For now, look at this site: http://www.arduino.cc/For more general purpose AVR development, there are a few places to go to get ready-built sets of tools similar to the WinAVR package for Windows. These gather together everything you need to compile for the AVR using one of the various GCC packages. Downloading is generally by means of AVRdude which can talk to a variety of hardware. After a lot of hunting around and a couple of mistakes, I have chosen the AVRMacPack.The main reason is that it installs neatly and has an uninstaller. The other popular package is ASX-AVR. The main disadvantage of this is that it scatters several packages around your /usr/local/ folder and has no uninstaller so getting rid of it, and presumably upgrading it, looks to be a bit of a pain.AVRMacPack details can be found here: http://www.obdev.at/products/avrmacpack/index-de.htmlTo program my processsors, I will be using the ASBasp. This open-source programmer is supported by the version of AVRdude in the tools and would be very easy to build. Except, of course for the chicken and egg business of getting the programmer programmed. Thus I have ordered a ready-made one from an eBay seller. Once that arrives, I can make some more.All this sound very well but I have not yet tried any of it so more later… Continue reading
Add to Google