Makefile error: <command-line>:0:6: error: expected identifier or ‘(’ before numeric constant

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've been using this Makefile for a couple years with the ATmega328p with no problems. So I got an ATxmega128a4u and changed the top line:

 

MCU = atxmega128a4u
#MCU = atmega328p
F_CPU = 16000000UL
BAUD = 19200UL
LIBDIR = lib

print-%:
	@echo '$*=$($*)'

CC = avr-gcc
TARGET = main
SOURCES=$(wildcard *.c $(LIBDIR)/*.c)
OBJECTS=$(SOURCES:.c=.o)
HEADERS=$(SOURCES:.c=.h)
CPPFLAGS = -DMAIN_C -DF_CPU=$(F_CPU) -DBAUD=$(BAUD) -I. -I$(LIBDIR)
CFLAGS = -Os -g -std=gnu99 -Wall
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
CFLAGS += -ffunction-sections -fdata-sections
LDFLAGS = -Wl,-Map,$(TARGET).map
LDFLAGS += -Wl,--gc-sections
TARGET_ARCH = -mmcu=$(MCU)

%.o: %.c $(HEADERS) Makefile
	$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $<;

$(TARGET).elf: $(OBJECTS)
	$(CC) $(LDFLAGS) $(TARGET_ARCH) $^ $(LDLIBS) -o $@

%.hex: %.elf
	$(OBJCOPY) -j .text -j .data -O ihex $< $@

%.eeprom: %.elf
	$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@

%.lst: %.elf
	$(OBJDUMP) -S $< > $@

## These targets don't have files named after them
.PHONY: all

all: $(TARGET).hex

debug:
	@echo
	@echo "Source files:"   $(SOURCES)
	@echo "MCU, F_CPU, BAUD:"  $(MCU), $(F_CPU), $(BAUD)
	@echo

 

and I get a strange error:

avr-gcc -Os -g -std=gnu99 -Wall -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -ffunction-sections -fdata-sections -DMAIN_C -DF_CPU=16000000UL -DBAUD=19200UL -I. -Ilib -mmcu=atxmega128a4u -c -o USART.o USART.c;
<command-line>:0:6: error: expected identifier or ‘(’ before numeric constant

I did a google search and it has something to do with not escaping nl's in macros.

 

so I made a script file:

 

avr-gcc -O2 -Wall -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -ffunction-sections -fdata-sections -fmessage-length=0 -ffunction-sections -DF_CPU=16000000UL -mmcu=atxmega128a4u -c -o main.o main.c

avr-gcc -O2 -Wall -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -ffunction-sections -fdata-sections -fmessage-length=0 -ffunction-sections -DF_CPU=16000000UL -mmcu=atxmega128a4u -c -o USART.o USART.c

avr-gcc -mmcu=atxmega128a4u -Wl,--gc-sections -Wl, -omain.elf main.o

and it compiles the main.c but the USART.c gives these errors:

 

In file included from main.c:9:0:
USART.h:7:2: warning: #warning "ATxmega128A4U defined" [-Wcpp]
 #warning "ATxmega128A4U defined"
  ^
In file included from USART.c:24:0:
USART.h:7:2: warning: #warning "ATxmega128A4U defined" [-Wcpp]
 #warning "ATxmega128A4U defined"
  ^
USART.c: In function ‘initUSART’:
USART.c:29:2: error: ‘UBRR0H’ undeclared (first use in this function)
  UBRR0H = (UCHAR)(baud >> 8);
  ^
USART.c:29:2: note: each undeclared identifier is reported only once for each function it appears in
USART.c:30:2: error: ‘UBRR0L’ undeclared (first use in this function)
  UBRR0L = (UCHAR)baud;
  ^
USART.c:31:2: error: ‘UCSR0B’ undeclared (first use in this function)
  UCSR0B = (1<<RXEN0) | (1<<TXEN0);
  ^
USART.c:31:15: error: ‘RXEN0’ undeclared (first use in this function)
  UCSR0B = (1<<RXEN0) | (1<<TXEN0);
               ^
USART.c:31:28: error: ‘TXEN0’ undeclared (first use in this function)
  UCSR0B = (1<<RXEN0) | (1<<TXEN0);
                            ^
USART.c:32:2: error: ‘UCSR0C’ undeclared (first use in this function)
  UCSR0C = (1<<USBS0) | (3<<UCSZ00);
  ^
USART.c:32:15: error: ‘USBS0’ undeclared (first use in this function)
  UCSR0C = (1<<USBS0) | (3<<UCSZ00);
               ^
USART.c:32:28: error: ‘UCSZ00’ undeclared (first use in this function)
  UCSR0C = (1<<USBS0) | (3<<UCSZ00);
                            ^
USART.c: In function ‘transmitByte’:
USART.c:51:10: error: ‘UCSR0A’ undeclared (first use in this function)
  while(!(UCSR0A & (1<<UDRE0)))
          ^
USART.c:51:23: error: ‘UDRE0’ undeclared (first use in this function)
  while(!(UCSR0A & (1<<UDRE0)))
                       ^
USART.c:52:2: error: ‘UDR0’ undeclared (first use in this function)
  UDR0 = data;
  ^
USART.c: In function ‘receiveByte’:
USART.c:56:10: error: ‘UCSR0A’ undeclared (first use in this function)
  while(!(UCSR0A & (1<<RXC0)));
          ^
USART.c:56:23: error: ‘RXC0’ undeclared (first use in this function)
  while(!(UCSR0A & (1<<RXC0)));
                       ^
USART.c:57:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
/usr/lib/gcc/avr/4.9.2/../../../avr/bin/ld: cannot find : No such file or directory
collect2: error: ld returned 1 exit status

I'm compiling main.c & USART.C:

 

#include <avr/io.h>
#include "../avr8-gnu-toolchain-linux_x86/avr/include/util/delay.h"
#include "sfr_helper.h"
#include <stdlib.h>
#include "macros.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "USART.h"

int main(void)
{
	unsigned char xbyte;
	
	initUSART(19200);
	_delay_ms(100);
	xbyte = 0x21;
	_delay_ms(100);

	while(1)
	{
		_delay_ms(50);
		transmitByte(xbyte);
		if(--xbyte < 0x21)
			xbyte = 0x7e;

	}
	return 0;
}

 

#ifndef _USART_H
#define _USART_H
#include <avr/io.h>
#include "../avr8-gnu-toolchain-linux_x86/avr/include/util/delay.h"
//#include "../avr8-gnu-toolchain-linux_x86/avr/include/avr/iom128a.h"
#include "sfr_helper.h"
#include "USART.h"
#include <util/setbaud.h>

void initUSART(UINT baud)
{
	UBRR0H = (UCHAR)(baud >> 8);
	UBRR0L = (UCHAR)baud;
	UCSR0B = (1<<RXEN0) | (1<<TXEN0);
	UCSR0C = (1<<USBS0) | (3<<UCSZ00);
}
#if 0
void initUSART(void) {                                /* requires BAUD */
  UBRR0H = UBRRH_VALUE;                        /* defined in setbaud.h */
  UBRR0L = UBRRL_VALUE;
#if USE_2X
  UCSR0A |= (1 << U2X0);
#else
  UCSR0A &= ~(1 << U2X0);
#endif
                                  /* Enable USART transmitter/receiver */
  UCSR0B = (1 << TXEN0) | (1 << RXEN0);
  UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);   /* 8 data bits, 1 stop bit */
}
#endif

void transmitByte(uint8_t data)
{
	while(!(UCSR0A & (1<<UDRE0)))
	UDR0 = data;
}
UCHAR receiveByte(void)
{
	while(!(UCSR0A & (1<<RXC0)));
}

#if 0
void transmitByte(uint8_t data) {
                                     /* Wait for empty transmit buffer */
  loop_until_bit_is_set(UCSR0A, UDRE0);
  UDR0 = data;                                            /* send data */
}

uint8_t receiveByte(void) {
  loop_until_bit_is_set(UCSR0A, RXC0);       /* Wait for incoming data */
  return UDR0;                                /* return register value */
}
#endif

                       /* Here are a bunch of useful printing commands */

void printString(const char myString[]) {
  uint8_t i = 0;
  while (myString[i]) {
    transmitByte(myString[i]);
    i++;
  }
}

void readString(char myString[], uint8_t maxLength) {
  char response;
  uint8_t i;
  i = 0;
  while (i < (maxLength - 1)) {                   /* prevent over-runs */
    response = receiveByte();
    transmitByte(response);                                    /* echo */
    if (response == '\r') {                     /* enter marks the end */
      break;
    }
    else {
      myString[i] = response;                       /* add in a letter */
      i++;
    }
  }
  myString[i] = 0;                          /* terminal NULL character */
}

void printByte(uint8_t byte) {
              /* Converts a byte to a string of decimal text, sends it */
  transmitByte('0' + (byte / 100));                        /* Hundreds */
  transmitByte('0' + ((byte / 10) % 10));                      /* Tens */
  transmitByte('0' + (byte % 10));                             /* Ones */
}

void printWord(UINT word) {
  transmitByte('0' + (word / 10000));                 /* Ten-thousands */
  transmitByte('0' + ((word / 1000) % 10));               /* Thousands */
  transmitByte('0' + ((word / 100) % 10));                 /* Hundreds */
  transmitByte('0' + ((word / 10) % 10));                      /* Tens */
  transmitByte('0' + (word % 10));                             /* Ones */
}

void printBinaryByte(uint8_t byte) {
                       /* Prints out a byte as a series of 1's and 0's */
  uint8_t bit;
  for (bit = 7; bit < 255; bit--) {
    if (bit_is_set(byte, bit))
      transmitByte('1');
    else
      transmitByte('0');
  }
}

char nibbleToHexCharacter(uint8_t nibble) {
                                   /* Converts 4 bits into hexadecimal */
  if (nibble < 10) {
    return ('0' + nibble);
  }
  else {
    return ('A' + nibble - 10);
  }
}

void printHexByte(uint8_t byte) {
                        /* Prints a byte as its hexadecimal equivalent */
  uint8_t nibble;
  nibble = (byte & 0b11110000) >> 4;
  transmitByte(nibbleToHexCharacter(nibble));
  nibble = byte & 0b00001111;
  transmitByte(nibbleToHexCharacter(nibble));
}

uint8_t getNumber(void) {
  // Gets a numerical 0-255 from the serial port.
  // Converts from string to number.
  char hundreds = '0';
  char tens = '0';
  char ones = '0';
  char thisChar = '0';
  do {                                                   /* shift over */
    hundreds = tens;
    tens = ones;
    ones = thisChar;
    thisChar = receiveByte();                   /* get a new character */
    transmitByte(thisChar);                                    /* echo */
  } while (thisChar != '\r');                     /* until type return */
  return (100 * (hundreds - '0') + 10 * (tens - '0') + ones - '0');
}
#endif

What's really weird is if I change the top line back to MCU = atmega328p it compiles fine.

- Dan

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And I know the right include file is being include because if I try to include it explicitly:

 

#ifndef _USART_H
#define _USART_H
#include <avr/io.h>
#include "../avr8-gnu-toolchain-linux_x86/avr/include/util/delay.h"
#include "../avr8-gnu-toolchain-linux_x86/avr/include/avr/iox128a4u.h"
#include "sfr_helper.h"
#include "macros.h"
#include "USART.h"
#include <util/setbaud.h>

I get this error:

 

In file included from USART.c:5:0:
../avr8-gnu-toolchain-linux_x86/avr/include/avr/iox128a4u.h:42:4: error: #error "Attempt to include more than one <avr/ioXXX.h> file."
 #  error "Attempt to include more than one <avr/ioXXX.h> file."

 

- Dan

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I redid the script file like this:

 

rm *.hex
avr-gcc -Os -g -std=gnu99 -Wall -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -ffunction-sections -fdata-sections -DMAIN_C -DF_CPU=8200000UL	 -DBAUD=19200UL -I. -Ilib -mmcu=atmega328p -c -o USART.o USART.c;

#avr-gcc -Os -g -std=gnu99 -Wall -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -ffunction-sections -fdata-sections -DMAIN_C -DF_CPU=8200000UL	 -DBAUD=19200UL -I. -Ilib -mmcu=atmega328p -c -o t6963.o t6963.c;

avr-gcc -Os -g -std=gnu99 -Wall -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -ffunction-sections -fdata-sections -DMAIN_C -DF_CPU=8200000UL	 -DBAUD=19200UL -I. -Ilib -mmcu=atmega328p -c -o main.o main.c;

#avr-gcc -Wl,-Map,main.map -Wl,--gc-sections -mmcu=atmega328p USART.o t6963.o main.o  -o main.elf
avr-gcc -Wl,-Map,main.map -Wl,--gc-sections -mmcu=atmega328p USART.o main.o  -o main.elf
avr-objcopy -j .text -j .data -O ihex main.elf main.hex

#rm *.o *.map *.elf

#sudo dfu-programmer atxmega128a4u erase
#sudo dfu-programmer atxmega128a4u flash --erase-first main.hex
rm *.hex

and it compiles and programs the part just fine, and then I deleted the main.elf file and left the same *.o files and tried to run make:

 

avr-gcc -Wl,-Map,main.map -Wl,--gc-sections -mmcu=atxmega128a4u USART.o main.o  -o main.elf
/usr/lib/gcc/avr/4.9.2/../../../avr/bin/ld: avr:5 architecture of input file `USART.o' is incompatible with avr:107 output
/usr/lib/gcc/avr/4.9.2/../../../avr/bin/ld: avr:5 architecture of input file `main.o' is incompatible with avr:107 output
collect2: error: ld returned 1 exit status
Makefile:85: recipe for target 'main.elf' failed
make: *** [main.elf] Error 1

 

- Dan

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wouldn't it be easier to use an IDE?

 

But if you are intent on using Makefiles then Google "Mfile AVR" and use that. It is bound to work.

 

BTW in:

<command-line>:0:6: error:

The :6 means the 6th character on the line. Note also that the location is not Makefile:3:6 or source.c:18:5 or something but "<command-line>" so this suggests it may be the invocation of make itself at fault. Can you show the complete build attempt not just potted snippets so we can see this in full context?

 

PS reading some more:

/usr/lib/gcc/avr/4.9.2/../../../avr/bin/ld: avr:5 architecture of input file `USART.o' is incompatible with avr:107 output

things like this occur because you aren't using a sensible makefile. You have to pass -mmcu= to both the compile AND the link. If you pass it to the compile then, yes the .o files will have avr:107 architecture but if you then try to link without -mmcu it will assume a default (think it is very likely avr1) and avr1!=avr:107  and hence the issue.

 

Or, as appears to be the case here, you are passing -mmcu=xmega to the link so the USART.o must have been compiled with a different -mmcu= before that.

 

Anyway just do yourself a favour and get Mfile and then all this goes away.

Last Edited: Fri. Oct 27, 2017 - 08:14 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also note that after a change of anything in a makefile all intermediate files (e.g. .o files) should be deleted so that on the next build everything is re-built. Else you risk that some intermediary files are from a build with the old settings in the makefile (e.g. the mmcu / TARGET_ARCH setting).

 

A sensible makefile has a special target for this called "clean". The Mfile utility that 'clawson' mentioned produces a makefile with such a target. Here it is: http://www.sax.de/~joerg/mfile/

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

sunbelt57 wrote:
and it compiles the main.c but the USART.c gives these errors:
You simply can't use your Mega UART code for a XMega.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Doh! That went right over my head !! blush

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

Doh! That went right over my head !! blush

 

+1 

 

blushblush

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah, I did change the USART code to what I thought would work for the xmega (going from the datasheet). I tried using the Atmel Studio on my windows machine but would like to stay in Linux. I think I may be going from the wrong datasheet. Can't really find a lot of example code like I could for the atmega328. I took out the 'clean' from the makefile along with everything else not related to compiling and linking because I was trying to get it to an absolute minimum.

- Dan

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The problem was, I was either setting up the CPPFLAGS & CFLAGS in the wrong order or I was trying to define BAUD in with the CPPFLAGS variable, anyway, this works:

 

##########------------------------------------------------------##########
##########              Project-specific Details                ##########
##########    Check these every time you start a new project    ##########
##########------------------------------------------------------##########
MCU = atxmega128a4u
F_CPU = 16000000UL	#this works on the original one
#F_CPU = 16000000UL	#this works on the new ones
BAUD  = 19200UL
## Also try BAUD = 19200 or 38400 if you're feeling lucky.

## A directory for common include files and the simple USART library.
## If you move either the current folder or the Library folder, you'll
##  need to change this path to match.
#LIBDIR = /usr/lib/gcc/avr/4.9.2/avrxmega5/

##########------------------------------------------------------##########
##########                 Programmer Defaults                  ##########
##########          Set up once, then forget about it           ##########
##########        (Can override.  See bottom of file.)          ##########
##########------------------------------------------------------##########

print-%:
	@echo '$*=$($*)'

PROGRAMMER_TYPE = buspirate
# extra arguments to avrdude: baud rate, chip type, -F flag, etc.
PROGRAMMER_ARGS = -C avrdude.conf +atmega328p.conf -P /dev/ttyUSB0 -v
# -p$(AVRDUDE_MCU) -c$(AVRDUDE_PROGRAMMERID) -P /dev/ttyUSB0 -e -U flash:w:$(PROJECT).hex:a -U eeprom:w:$(PROJECT).eep:a -U lfuse:w:0x$(AVRDUDE_LFUSE):m -U hfuse:w:0x$(AVRDUDE_HFUSE):m -U efuse:w:0x$(AVRDUDE_EFUSE):m

##########------------------------------------------------------##########
##########                  Program Locations                   ##########
##########     Won't need to change if they're in your PATH     ##########
##########------------------------------------------------------##########

#CC = /usr/bin/avr-gcc
ROOT1 = /home/dan/dev/Atmel_other/avr8-gnu-toolchain-linux_x86/
#BIN = $(ROOT1)bin/
#CC = $(BIN)avr-gcc
CC = avr-gcc
#LIBDIR = $(ROOT1)lib/gcc/avr/4.9.2/avrxmega7/
LIBDIR =
#INCDIR = $(ROOT1)include/
#OBJCOPY = $(BIN)avr-objcopy
#OBJDUMP = $(BIN)avr-objdump
#AVRSIZE = $(BIN)avr-size

#INCDIR = $(ROOT1)include/
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
AVRSIZE = avr-size

#AVRDUDE = ./avrdude-test2
AVRDUDE = sudo avrdude

##########------------------------------------------------------##########
##########                   Makefile Magic!                    ##########
##########         Summary:                                     ##########
##########             We want a .hex file                      ##########
##########        Compile source files into .elf                ##########
##########        Convert .elf file into .hex                   ##########
##########        You shouldn't need to edit below.             ##########
##########------------------------------------------------------##########

## The name of your project (without the .c)
TARGET = main
## Or name it automatically after the enclosing directory
#TARGET = $(lastword $(subst /, ,$(CURDIR)))

# Object files: will find all .c/.h files in current directory
#  and in LIBDIR.  If you have any other (sub-)directories with code,
#  you can add them in to SOURCES below in the wildcard statement.
SOURCES=$(wildcard *.c $(LIBDIR)/*.c)
OBJECTS=$(SOURCES:.c=.o)
HEADERS=$(SOURCES:.c=.h)

## Compilation options, type man avr-gcc if you're curious.
#CPPFLAGS = -mmcu=$(MCU) -DF_CPU=$(F_CPU) -DBAUD=$(BAUD) -I. -I$(LIBDIR)
CPPFLAGS = -mmcu=$(MCU) -DF_CPU=$(F_CPU) #-DBAUD=$(BAUD)
#CPPFLAGS = -mmcu=$(MCU) -DF_CPU=$(F_CPU) -DBAUD=$(BAUD) -I. -I$(INCDIR)
# use the TTY_DISPLAY define to print out to a tty (like minicom)
#CPPFLAGS = -DMAIN_C -DTTY_DISPLAY -DF_CPU=$(F_CPU) -DBAUD=$(BAUD) -I. -I$(LIBDIR)
CFLAGS =  -Os -g -std=gnu99 -Wall
## Use short (8-bit) data types
#CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
## Splits up object files per function
#CFLAGS += -ffunction-sections -fdata-sections
CFLAGS += -Wstrict-prototypes
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums 
LDFLAGS = -Wl,-Map,$(TARGET).map
## Optional, but often ends up with smaller code
LDFLAGS += -Wl,--gc-sections -mmcu=$(MCU)
## Relax shrinks code even more, but makes disassembly messy
## LDFLAGS =+ -Wl,--relax
## LDFLAGS =+ -Wl,-u,vfprintf -lprintf_flt -lm  ## for floating-point printf
## LDFLAGS =+ -Wl,-u,vfprintf -lprintf_min      ## for smaller printf
#TARGET_ARCH = -mmcu=$(MCU)
TARGET_ARCH =

## Explicit pattern rules:
##  To make .o files from .c files
%.o: %.c $(HEADERS) Makefile
#	$(CC) $(CFLAGS) $(TARGET_ARCH) $(CPPFLAGS) -c -o $@ $<;
#	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<;
	$(CC) $(CFLAGS)  $(CPPFLAGS) -c -o $@ $<;

$(TARGET).elf: $(OBJECTS)
	$(CC) $(LDFLAGS) $(TARGET_ARCH) $^ $(LDLIBS) -o $@

%.hex: %.elf
	$(OBJCOPY) -j .text -j .data -O ihex $< $@

%.eeprom: %.elf
	$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@

%.lst: %.elf
	$(OBJDUMP) -S $< > $@

## These targets don't have files named after them
.PHONY: all disassemble disasm eeprom size clean squeaky_clean flash fuses

all: $(TARGET).hex

debug:
	@echo
	@echo "Source files:"   $(SOURCES)
	@echo "MCU, F_CPU, BAUD:"  $(MCU), $(F_CPU), $(BAUD)
	@echo

# Optionally create listing file from .elf
# This creates approximate assembly-language equivalent of your code.
# Useful for debugging time-sensitive bits,
# or making sure the compiler does what you want.
disassemble: $(TARGET).lst

disasm: disassemble

# Optionally show how big the resulting program is
size:  $(TARGET).elf
	$(AVRSIZE) -C --mcu=$(MCU) $(TARGET).elf
	rm -f $(TARGET).elf $(TARGET).hex $(TARGET).obj \
	$(TARGET).o $(TARGET).d $(TARGET).eep $(TARGET).lst \
	$(TARGET).lss $(TARGET).sym $(TARGET).map $(TARGET)~ \
	$(TARGET).eeprom

clean:
	rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~ *.eeprom

##########------------------------------------------------------##########
##########              Programmer-specific details             ##########
##########           Flashing code to AVR using avrdude         ##########
##########------------------------------------------------------##########

flash: $(TARGET).hex
	sudo dfu-programmer atxmega128a4u erase
	sudo dfu-programmer atxmega128a4u flash --erase-first main.hex

#	$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U flash:w:$<
#	$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS)

## An alias
program: flash

flash_eeprom: $(TARGET).eeprom
	$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U eeprom:w:$<

avrdude_terminal:
	$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nt

## If you've got multiple programmers that you use,
## you can define them here so that it's easy to switch.
## To invoke, use something like `make flash_arduinoISP`
flash_usbtiny: PROGRAMMER_TYPE = usbtiny
flash_usbtiny: PROGRAMMER_ARGS =  # USBTiny works with no further arguments
flash_usbtiny: flash

flash_usbasp: PROGRAMMER_TYPE = usbasp
flash_usbasp: PROGRAMMER_ARGS =  # USBasp works with no further arguments
flash_usbasp: flash

flash_arduinoISP: PROGRAMMER_TYPE = avrisp
flash_arduinoISP: PROGRAMMER_ARGS = -b 19200 -P /dev/ttyACM0
## (for windows) flash_arduinoISP: PROGRAMMER_ARGS = -b 19200 -P com5
flash_arduinoISP: flash

flash_109: PROGRAMMER_TYPE = avr109
flash_109: PROGRAMMER_ARGS = -b 9600 -P /dev/ttyUSB0
flash_109: flash

flash_buspirate: PROGRAMMER_TYPE = buspirate
flash_buspirate: PROGRAMMER_ARGS = -P /dev/ttyUSB0 -v

##########------------------------------------------------------##########
##########       Fuse settings and suitable defaults            ##########
##########------------------------------------------------------##########

## Mega 48, 88, 168, 328 default values
LFUSE = 0xE2
HFUSE = 0xD9
EFUSE = 0xFF

## Generic
FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m

fuses:
	$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) \
	           $(PROGRAMMER_ARGS) $(FUSE_STRING)
show_fuses:
	$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nv

## Called with no extra definitions, sets to defaults
set_default_fuses:  FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m
set_default_fuses:  fuses

## Set the fuse byte for full-speed mode
## Note: can also be set in firmware for modern chips
set_fast_fuse: LFUSE = 0xE2
set_fast_fuse: FUSE_STRING = -U lfuse:w:$(LFUSE):m
set_fast_fuse: fuses

## Set the EESAVE fuse byte to preserve EEPROM across flashes
set_eeprom_save_fuse: HFUSE = 0xD7
set_eeprom_save_fuse: FUSE_STRING = -U hfuse:w:$(HFUSE):m
set_eeprom_save_fuse: fuses

## Clear the EESAVE fuse byte
clear_eeprom_save_fuse: FUSE_STRING = -U hfuse:w:$(HFUSE):m
clear_eeprom_save_fuse: fuses

 

The flash part works because I'm using dfu-programmer so I haven't changed all the avrdude references yet.

 

Plus I found some good examples on the Atmel website in the App notes section.

- Dan