Skip to content

Commit 1f40bd0

Browse files
author
Elliot Williams
committed
Changed project-specific Makefiles to be generic
to do this, every sub-project needed its own directory so there's a few more created also, care is taken to make sure all projects at 8MHz have corresponding 8MHz Makefiles: for f in `grep -s -l clock_div_1 Chapter*/*/*` ; do dirname $f ; done
1 parent 79cd4d9 commit 1f40bd0

File tree

88 files changed

+5013
-2483
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+5013
-2483
lines changed

AVR-Programming-Library/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
##########------------------------------------------------------##########
66

77
MCU = atmega168p
8-
F_CPU = 1000000UL
8+
F_CPU = 1000000UL
99
BAUD = 9600UL
1010
## Also try BAUD = 19200 or 38400 if you're feeling lucky.
1111

Chapter02_Programming-AVRs/blinkLED/Makefile

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,15 @@
44
########## Check these every time you start a new project ##########
55
##########------------------------------------------------------##########
66

7-
MCU = atmega168
8-
F_CPU = 1000000
9-
BAUD = 9600
7+
MCU = atmega168p
8+
F_CPU = 1000000UL
9+
BAUD = 9600UL
1010
## Also try BAUD = 19200 or 38400 if you're feeling lucky.
1111

12-
## This is where your main() routine lives
13-
MAIN = blinkLED.c
14-
15-
## If you've split your program into multiple .c / .h files,
16-
## include the additional source (in same directory) here
17-
LOCAL_SOURCE =
18-
19-
## Here you can link to one more directory (and multiple .c files)
20-
EXTRA_SOURCE_DIR = ../../AVR-Programming-Library/
21-
EXTRA_SOURCE_FILES = USART.c
12+
## A directory for common include files and the simple USART library.
13+
## If you move either the current folder or the Library folder, you'll
14+
## need to change this path to match.
15+
LIBDIR = ../../AVR-Programming-Library
2216

2317
##########------------------------------------------------------##########
2418
########## Programmer Defaults ##########
@@ -31,59 +25,78 @@ PROGRAMMER_TYPE = usbtiny
3125
PROGRAMMER_ARGS =
3226

3327
##########------------------------------------------------------##########
34-
########## Makefile Magic! ##########
35-
########## Summary: ##########
36-
########## We want a .hex file ##########
37-
########## Compile source files into .elf ##########
38-
########## Convert .elf file into .hex ##########
39-
########## You shouldn't need to edit below. ##########
28+
########## Program Locations ##########
29+
########## Won't need to change if they're in your PATH ##########
4030
##########------------------------------------------------------##########
4131

42-
## Defined programs / locations
4332
CC = avr-gcc
4433
OBJCOPY = avr-objcopy
4534
OBJDUMP = avr-objdump
4635
AVRSIZE = avr-size
4736
AVRDUDE = avrdude
4837

49-
## Compilation options, type man avr-gcc if you're curious.
50-
CFLAGS = -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL -DBAUD=$(BAUD) -Os -I. -I$(EXTRA_SOURCE_DIR)
51-
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
52-
CFLAGS += -Wall -Wstrict-prototypes
53-
CFLAGS += -g -ggdb
54-
CFLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections -Wl,--relax
55-
CFLAGS += -std=gnu99
56-
## CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## for floating-point printf
57-
## CFLAGS += -Wl,-u,vfprintf -lprintf_min ## for smaller printf
58-
59-
## Lump target and extra source files together
60-
TARGET = $(strip $(basename $(MAIN)))
61-
SRC = $(TARGET).c
62-
EXTRA_SOURCE = $(addprefix $(EXTRA_SOURCE_DIR), $(EXTRA_SOURCE_FILES))
63-
SRC += $(EXTRA_SOURCE)
64-
SRC += $(LOCAL_SOURCE)
38+
##########------------------------------------------------------##########
39+
########## Makefile Magic! ##########
40+
########## Summary: ##########
41+
########## We want a .hex file ##########
42+
########## Compile source files into .elf ##########
43+
########## Convert .elf file into .hex ##########
44+
########## You shouldn't need to edit below. ##########
45+
##########------------------------------------------------------##########
6546

66-
## List of all header files
67-
HEADERS = $(SRC:.c=.h)
47+
## The name of your project (without the .c)
48+
# TARGET = blinkLED
49+
## Or name it automatically after the enclosing directory
50+
TARGET = $(lastword $(subst /, ,$(CURDIR)))
6851

69-
## For every .c file, compile an .o object file
70-
OBJ = $(SRC:.c=.o)
52+
# Object files: will find all .c/.h files in current directory
53+
# and in LIBDIR. If you have any other (sub-)directories with code,
54+
# you can add them in to SOURCES below in the wildcard statement.
55+
SOURCES=$(wildcard *.c $(LIBDIR)/*.c)
56+
OBJECTS=$(SOURCES:.c=.o)
57+
HEADERS=$(SOURCES:.c=.h)
7158

72-
## Generic Makefile targets. (Only .hex file is necessary)
73-
all: $(TARGET).hex
59+
## Compilation options, type man avr-gcc if you're curious.
60+
CPPFLAGS = -DF_CPU=$(F_CPU) -DBAUD=$(BAUD) -I. -I$(LIBDIR)
61+
CFLAGS = -Os -g -std=gnu99 -Wall
62+
## Use short (8-bit) data types
63+
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
64+
## Splits up object files per function
65+
CFLAGS += -ffunction-sections -fdata-sections
66+
LDFLAGS = -Wl,-Map,$(TARGET).map
67+
## Optional, but often ends up with smaller code
68+
LDFLAGS += -Wl,--gc-sections
69+
## Relax shrinks code even more, but makes disassembly messy
70+
## LDFLAGS += -Wl,--relax
71+
## LDFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## for floating-point printf
72+
## LDFLAGS += -Wl,-u,vfprintf -lprintf_min ## for smaller printf
73+
TARGET_ARCH = -mmcu=$(MCU)
74+
75+
## Explicit pattern rules:
76+
## To make .o files from .c files
77+
%.o: %.c $(HEADERS) Makefile
78+
$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $<;
79+
80+
$(TARGET).elf: $(OBJECTS)
81+
$(CC) $(LDFLAGS) $(TARGET_ARCH) $^ $(LDLIBS) -o $@
7482

7583
%.hex: %.elf
76-
$(OBJCOPY) -R .eeprom -O ihex $< $@
77-
78-
%.elf: $(SRC)
79-
$(CC) $(CFLAGS) $(SRC) --output $@
84+
$(OBJCOPY) -j .text -j .data -O ihex $< $@
8085

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

89+
%.lst: %.elf
90+
$(OBJDUMP) -S $< > $@
91+
92+
## These targets don't have files named after them
93+
.PHONY: all disassemble disasm eeprom size clean squeaky_clean flash fuses
94+
95+
all: $(TARGET).hex
96+
8497
debug:
8598
@echo
86-
@echo "Source files:" $(SRC)
99+
@echo "Source files:" $(SOURCES)
87100
@echo "MCU, F_CPU, BAUD:" $(MCU), $(F_CPU), $(BAUD)
88101
@echo
89102

@@ -95,11 +108,6 @@ disassemble: $(TARGET).lst
95108

96109
disasm: disassemble
97110

98-
eeprom: $(TARGET).eeprom
99-
100-
%.lst: %.elf
101-
$(OBJDUMP) -S $< > $@
102-
103111
# Optionally show how big the resulting program is
104112
size: $(TARGET).elf
105113
$(AVRSIZE) -C --mcu=$(MCU) $(TARGET).elf
@@ -111,7 +119,7 @@ clean:
111119
$(TARGET).eeprom
112120

113121
squeaky_clean:
114-
rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~
122+
rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~ *.eeprom
115123

116124
##########------------------------------------------------------##########
117125
########## Programmer-specific details ##########

Chapter18_Using-Flash-Program-Memory/progmemDemo/Makefile renamed to Chapter02_Programming-AVRs/blinkLED_AVR_style/Makefile

Lines changed: 61 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,15 @@
44
########## Check these every time you start a new project ##########
55
##########------------------------------------------------------##########
66

7-
MCU = atmega168
8-
F_CPU = 1000000
9-
BAUD = 9600
7+
MCU = atmega168p
8+
F_CPU = 1000000UL
9+
BAUD = 9600UL
1010
## Also try BAUD = 19200 or 38400 if you're feeling lucky.
1111

12-
## This is where your main() routine lives
13-
MAIN = progmemDemo5.c
14-
MAIN = progmemDemo4.c
15-
MAIN = progmemDemo3.c
16-
MAIN = progmemDemo2.c
17-
#MAIN = progmemDemo1.c
18-
19-
## If you've split your program into multiple .c / .h files,
20-
## include the additional source (in same directory) here
21-
LOCAL_SOURCE =
22-
23-
## Here you can link to one more directory (and multiple .c files)
24-
EXTRA_SOURCE_DIR = ../../AVR-Programming-Library/
25-
EXTRA_SOURCE_FILES = USART.c
12+
## A directory for common include files and the simple USART library.
13+
## If you move either the current folder or the Library folder, you'll
14+
## need to change this path to match.
15+
LIBDIR = ../../AVR-Programming-Library
2616

2717
##########------------------------------------------------------##########
2818
########## Programmer Defaults ##########
@@ -35,59 +25,78 @@ PROGRAMMER_TYPE = usbtiny
3525
PROGRAMMER_ARGS =
3626

3727
##########------------------------------------------------------##########
38-
########## Makefile Magic! ##########
39-
########## Summary: ##########
40-
########## We want a .hex file ##########
41-
########## Compile source files into .elf ##########
42-
########## Convert .elf file into .hex ##########
43-
########## You shouldn't need to edit below. ##########
28+
########## Program Locations ##########
29+
########## Won't need to change if they're in your PATH ##########
4430
##########------------------------------------------------------##########
4531

46-
## Defined programs / locations
4732
CC = avr-gcc
4833
OBJCOPY = avr-objcopy
4934
OBJDUMP = avr-objdump
5035
AVRSIZE = avr-size
5136
AVRDUDE = avrdude
5237

53-
## Compilation options, type man avr-gcc if you're curious.
54-
CFLAGS = -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL -DBAUD=$(BAUD) -Os -I. -I$(EXTRA_SOURCE_DIR)
55-
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
56-
CFLAGS += -Wall -Wstrict-prototypes
57-
CFLAGS += -g -ggdb
58-
CFLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections -Wl,--relax
59-
CFLAGS += -std=gnu99
60-
## CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## for floating-point printf
61-
## CFLAGS += -Wl,-u,vfprintf -lprintf_min ## for smaller printf
62-
63-
## Lump target and extra source files together
64-
TARGET = $(strip $(basename $(MAIN)))
65-
SRC = $(TARGET).c
66-
EXTRA_SOURCE = $(addprefix $(EXTRA_SOURCE_DIR), $(EXTRA_SOURCE_FILES))
67-
SRC += $(EXTRA_SOURCE)
68-
SRC += $(LOCAL_SOURCE)
38+
##########------------------------------------------------------##########
39+
########## Makefile Magic! ##########
40+
########## Summary: ##########
41+
########## We want a .hex file ##########
42+
########## Compile source files into .elf ##########
43+
########## Convert .elf file into .hex ##########
44+
########## You shouldn't need to edit below. ##########
45+
##########------------------------------------------------------##########
6946

70-
## List of all header files
71-
HEADERS = $(SRC:.c=.h)
47+
## The name of your project (without the .c)
48+
# TARGET = blinkLED
49+
## Or name it automatically after the enclosing directory
50+
TARGET = $(lastword $(subst /, ,$(CURDIR)))
7251

73-
## For every .c file, compile an .o object file
74-
OBJ = $(SRC:.c=.o)
52+
# Object files: will find all .c/.h files in current directory
53+
# and in LIBDIR. If you have any other (sub-)directories with code,
54+
# you can add them in to SOURCES below in the wildcard statement.
55+
SOURCES=$(wildcard *.c $(LIBDIR)/*.c)
56+
OBJECTS=$(SOURCES:.c=.o)
57+
HEADERS=$(SOURCES:.c=.h)
7558

76-
## Generic Makefile targets. (Only .hex file is necessary)
77-
all: $(TARGET).hex
59+
## Compilation options, type man avr-gcc if you're curious.
60+
CPPFLAGS = -DF_CPU=$(F_CPU) -DBAUD=$(BAUD) -I. -I$(LIBDIR)
61+
CFLAGS = -Os -g -std=gnu99 -Wall
62+
## Use short (8-bit) data types
63+
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
64+
## Splits up object files per function
65+
CFLAGS += -ffunction-sections -fdata-sections
66+
LDFLAGS = -Wl,-Map,$(TARGET).map
67+
## Optional, but often ends up with smaller code
68+
LDFLAGS += -Wl,--gc-sections
69+
## Relax shrinks code even more, but makes disassembly messy
70+
## LDFLAGS += -Wl,--relax
71+
## LDFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## for floating-point printf
72+
## LDFLAGS += -Wl,-u,vfprintf -lprintf_min ## for smaller printf
73+
TARGET_ARCH = -mmcu=$(MCU)
74+
75+
## Explicit pattern rules:
76+
## To make .o files from .c files
77+
%.o: %.c $(HEADERS) Makefile
78+
$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $<;
79+
80+
$(TARGET).elf: $(OBJECTS)
81+
$(CC) $(LDFLAGS) $(TARGET_ARCH) $^ $(LDLIBS) -o $@
7882

7983
%.hex: %.elf
80-
$(OBJCOPY) -R .eeprom -O ihex $< $@
81-
82-
%.elf: $(SRC)
83-
$(CC) $(CFLAGS) $(SRC) --output $@
84+
$(OBJCOPY) -j .text -j .data -O ihex $< $@
8485

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

89+
%.lst: %.elf
90+
$(OBJDUMP) -S $< > $@
91+
92+
## These targets don't have files named after them
93+
.PHONY: all disassemble disasm eeprom size clean squeaky_clean flash fuses
94+
95+
all: $(TARGET).hex
96+
8897
debug:
8998
@echo
90-
@echo "Source files:" $(SRC)
99+
@echo "Source files:" $(SOURCES)
91100
@echo "MCU, F_CPU, BAUD:" $(MCU), $(F_CPU), $(BAUD)
92101
@echo
93102

@@ -99,11 +108,6 @@ disassemble: $(TARGET).lst
99108

100109
disasm: disassemble
101110

102-
eeprom: $(TARGET).eeprom
103-
104-
%.lst: %.elf
105-
$(OBJDUMP) -S $< > $@
106-
107111
# Optionally show how big the resulting program is
108112
size: $(TARGET).elf
109113
$(AVRSIZE) -C --mcu=$(MCU) $(TARGET).elf
@@ -115,7 +119,7 @@ clean:
115119
$(TARGET).eeprom
116120

117121
squeaky_clean:
118-
rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~
122+
rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~ *.eeprom
119123

120124
##########------------------------------------------------------##########
121125
########## Programmer-specific details ##########

0 commit comments

Comments
 (0)