fredag den 28. november 2014

Arduino without the bootloader.

Like me, you might have been playing around with the Arduino and found it rather fun and/or interesting to write code to tell how the little platform should act. Maybe with a sensor and an LCD display to show the read out or you might have used it to control a little car or something else with a motor or servo. The Arduino is a great way to get started fairly easy with microcontrollers like the Atmega328 that is on the Arduino printed circuit board. With the inspiration from Processing the guys behind Arduino has made it easy with a few lines of code quickly to get functionality up and running.

After a while you might want to dig deeper and control all those hardware goodies in the Atmega328 chip yourself without the help of the Arduino and the bootloader. Oh well, you might just have run out of memory in your projects and want to get rid of the bootloader.

Good thing there is a way to program the AVR chip without the bootloader then. However this takes a bit more effort and a different way of talking to the chip via USB. Here's how I do it in Arch Linux - but it may apply to Debian based Linux distros as well.

The following assumes knowledge on how to use the terminal as most commands is executed there.

The programmer

First off you need to get a programmer to put the software in your chip. There are a lot of different devices out there, but I decided to get the USBtinyISP from Lady Ada.
Picture courtesy of www.ladyada.net
This programmer comes as a kit so it takes a bit of soldering, but it's cheap and some times that's what counts. It works great just the same.

To make this programmer work without superuser privileges you need to make file with the following contents only:
SUBSYSTEM=="usb",ATTRS{idVendor}=="1781",ATTRS{idProduct}=="0c9f",GROUP="users",MODE="0666"
In my case I've named it 'arduino.rules'. You can name it what you want, but it has to end on .rules . This file is then put in the /etc/udev/rules.d/ folder. You will need to be root for this operation.
This file is used by the system to decide what to do with a device that matches those attributes. In this case it grants the rights of '0666' to the users in the group 'users'. So make sure your own user account is a member of that user group - or change it to your liking. A further explanation on the contents of this file can be found here.

The makefile

In a Linux environment you have a nifty tool called make. This program executes a set of commands listed in a file called makefile or Makefile. For programming the Atmega328p chip my makefile looks like this:

CC=avr-gcc
UPLOADER = avrdude
CONVERTER = avr-objcopy

FILENAMES = main.c
PROJECT = blinky
MCU = atmega328p # ATmega328 (Arduino)
PROGRAMMER = usptiny

all: compile hex

compile:
    @echo "Cross compiling..."
    $(CC) -Wall -mmcu=$(MCU) -Os -D F_CPU=16000000 $ $(FILENAMES) -o $(PROJECT).out
    @echo "Done compiling"

hex: $(PROJECT).out
    @echo "Converting to hex..."
    $(CONVERTER) -j .text -j .data -O ihex $(PROJECT).out $(PROJECT).hex
    @echo "Done converting"

upload: $(PROJECT).hex
    avrdude -c usbtiny -P usb -p m328p -U flash:w:$(PROJECT).hex:i

clean: *.out *.hex
    @echo "Cleaning project...."
    rm *.out *.hex


(For a tutorial on makefiles look here.)

The above makefile shows the use of avr-gcc, avrdude and avr-objcopy. These tools can be optained by issueing the following command:
sudo pacman -S avrdude gcc-avr avr-libc
These are also dependencies of the Arduino IDE, so if you already have this up and running the makefile should run.






This makefile uses avr-gcc to compile the main.c file specifically for the selected microcontroller with the selected clock frequency. It is then converted to .hex file using avr-objcopy. This makes it ready for upload to the MCU - a task which is handled by avrdude.

The updated makefile is found at my GitHub page


We are now ready to move on to actual programming which will be described in the next post.

Ingen kommentarer:

Send en kommentar