Avr-gcc

From Attie's Wiki
(Difference between revisions)
Jump to: navigation, search
m
 
(35 intermediate revisions by 4 users not shown)
Line 4: Line 4:
 
$ yum install avr-gcc avr-libc avr-binutils avrdude
 
$ yum install avr-gcc avr-libc avr-binutils avrdude
 
</pre>
 
</pre>
 +
 +
See: using the [[Arduino IDE]]
  
 
<h2>avrdude</h2>
 
<h2>avrdude</h2>
 +
use [[avrdude]] to load files into the arduino
  
For ATMega168:
+
<h2>makefile</h2>
<pre>
+
$ avrdude -c buspirate -P /dev/ttyUSB1 -x speed=3 -p m168
+
</pre>
+
 
+
For ATMega328p:
+
<pre>
+
$ avrdude -c buspirate -P /dev/ttyUSB1 -x speed=3 -p m328p
+
</pre>
+
  
Terminal mode: <code>-t</code>. Use command <code>part</code> to see detailed part information.<br>
+
<h3>devices</h3>
To upload file: <code>-U memtype:op:filename[:format]</code>
+
* <code>atmega168</code>
 
+
* <code>atmega328p</code>
<h2>makefile</h2>
+
  
 
<h3>simple</h3>
 
<h3>simple</h3>
Line 26: Line 20:
 
<pre>
 
<pre>
 
all:
 
all:
         avr-gcc avr.c -mmcu=atmega168 -o example.elf -Wall -Os
+
         avr-gcc avr.c -mmcu=atmega328p -o example.elf -Wall -Os
 
         avr-objcopy -j .text -O ihex example.elf example.hex
 
         avr-objcopy -j .text -O ihex example.elf example.hex
         avrdude -c buspirate -P /dev/ttyUSB1 -x speed=3 -p m168 -U flash:w:example.hex
+
         avrdude -c buspirate -P /dev/ttyUSB1 -p m328p -U flash:w:example.hex
 
</pre>
 
</pre>
  
Line 35: Line 29:
 
<pre>
 
<pre>
 
all:
 
all:
         avr-gcc -g -mmcu=atmega168 -c avr.c -Wa,-alh,-L -o avr.o > avr.asm
+
         avr-gcc -g -mmcu=atmega328p -c avr.c -Wa,-alh,-L -o example.o > example.asm
         avr-gcc -g -mmcu=atmega168 -Wl,-Map,example.map -o example.elf example.o
+
         avr-gcc -g -mmcu=atmega328p -Wl,-Map,example.map -o example.elf example.o
 
         avr-objdump -h -S example.elf > example.lst
 
         avr-objdump -h -S example.elf > example.lst
 
         avr-objcopy -j .text -j .data -O ihex example.elf example.hex
 
         avr-objcopy -j .text -j .data -O ihex example.elf example.hex
         avrdude -c buspirate -P /dev/ttyUSB1 -x speed=3 -p m168 -U flash:w:example.hex
+
         avrdude -c buspirate -P /dev/ttyUSB1 -p m328p -U flash:w:example.hex
 
</pre>
 
</pre>
  
Line 72: Line 66:
 
</source>
 
</source>
  
<h2>icsp pins</h2>
+
<h3>S.O.S.</h3>
connecting to bus pirate
+
<source lang="c">
{| border="1"
+
/* 8 Mhz cpu */
|-
+
#define F_CPU 8000000UL
| 1 - brown  ||  ||align="right |  gray - 2
+
 
|-
+
/* led pin */
| 3 - yellow  ||  ||align="right |  orange - 4
+
#define LED 0b00100000
|-
+
 
|  5 - red  ||  ||align="right |  black - 6
+
#include <util/delay.h>
|}
+
#include <avr/io.h>
 +
 
 +
void dot(void) {
 +
  /* turn led on */
 +
  PORTB |= LED;
 +
  _delay_ms(200);
 +
 
 +
  /* turn led off */
 +
  PORTB &= ~LED;
 +
  _delay_ms(500);
 +
}
 +
 
 +
void dash(void) {
 +
  /* turn led on */
 +
  PORTB |= LED;
 +
  _delay_ms(700);
 +
 
 +
  /* turn led off */
 +
  PORTB &= ~LED;
 +
  _delay_ms(500);
 +
}
 +
 
 +
int main(void) {
 +
  char i;
 +
 
 +
  /* set port b direction register to output for led pin */
 +
  DDRB = LED;
 +
 
 +
  while(1) {
 +
 
 +
    for (i=0;i<3;i++) {
 +
      dot();
 +
    }
 +
    for (i=0;i<3;i++) {
 +
      dash();
 +
    }
 +
    for (i=0;i<3;i++) {
 +
      dot();
 +
    }
 +
 
 +
    _delay_ms(5000);
 +
 
 +
  }
 +
}
 +
</source>
  
 
<h2>other commands</h2>
 
<h2>other commands</h2>

Latest revision as of 23:40, 18 December 2010

How to setup avr-gcc and get compiling for an Arduino!

$ yum install avr-gcc avr-libc avr-binutils avrdude

See: using the Arduino IDE

Contents

avrdude

use avrdude to load files into the arduino

makefile

devices

  • atmega168
  • atmega328p

simple

Just produces the .elf and .hex file. Also optimizes for space (HUGE difference)

all:
        avr-gcc avr.c -mmcu=atmega328p -o example.elf -Wall -Os
        avr-objcopy -j .text -O ihex example.elf example.hex
        avrdude -c buspirate -P /dev/ttyUSB1 -p m328p -U flash:w:example.hex

complex

produces .o, .asm, .map and .lst files too

all:
        avr-gcc -g -mmcu=atmega328p -c avr.c -Wa,-alh,-L -o example.o > example.asm
        avr-gcc -g -mmcu=atmega328p -Wl,-Map,example.map -o example.elf example.o
        avr-objdump -h -S example.elf > example.lst
        avr-objcopy -j .text -j .data -O ihex example.elf example.hex
        avrdude -c buspirate -P /dev/ttyUSB1 -p m328p -U flash:w:example.hex

sample source

blink

/* 8 Mhz cpu */
#define F_CPU 8000000UL
 
/* led pin */
#define LED 0b00100000
 
#include <util/delay.h>
#include <avr/io.h>
 
int main(void) {
  /* set port b direction register to output for led pin */
  DDRB = LED;
 
  while(1) {
 
    /* turn led off */
    PORTB &= ~LED;
    _delay_ms(450);
 
    /* turn led on */
    PORTB |= LED;
    _delay_ms(50);
  }
}

S.O.S.

/* 8 Mhz cpu */
#define F_CPU 8000000UL
 
/* led pin */
#define LED 0b00100000
 
#include <util/delay.h>
#include <avr/io.h>
 
void dot(void) {
  /* turn led on */
  PORTB |= LED;
  _delay_ms(200);
 
  /* turn led off */
  PORTB &= ~LED;
  _delay_ms(500);
}
 
void dash(void) {
  /* turn led on */
  PORTB |= LED;
  _delay_ms(700);
 
  /* turn led off */
  PORTB &= ~LED;
  _delay_ms(500);
}
 
int main(void) {
  char i;
 
  /* set port b direction register to output for led pin */
  DDRB = LED;
 
  while(1) {
 
    for (i=0;i<3;i++) {
       dot();
    }
    for (i=0;i<3;i++) {
       dash();
    }
    for (i=0;i<3;i++) {
       dot();
    }
 
    _delay_ms(5000);
 
  }
}

other commands

avr-size example.elf
avr-gcc --help=optimizers

resources

https://www.mainframe.cx/~ckuethe/avr-c-tutorial/
http://arduino.cc/en/Hacking/PinMapping168
http://hintshop.ludvig.co.nz/show/buspirate-avr-programming/
http://itp.nyu.edu/physcomp/uploads/6pinAVRproghead.jpg
http://dangerousprototypes.com/bus-pirate-manual/
http://www.ladyada.net/library/arduino/bootloader.html

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox