Skip to content

Instantly share code, notes, and snippets.

@motchy869
Created August 16, 2017 14:55
Show Gist options
  • Save motchy869/644df57bd6c216fdfce76ee863b75b3d to your computer and use it in GitHub Desktop.
Save motchy869/644df57bd6c216fdfce76ee863b75b3d to your computer and use it in GitHub Desktop.
Makefile for avr-gcc on Linux
#makefile for AVR on Linux
#
#デフォルトターゲットは コンパイル(if needed)+書き込み となっている。
#プログラム書き込み時にEEPROMを消去したいときは90行目付近の avrdude に -e オプションを付けること。
#MCU名
#1つ目がavr-gcc(or avr-g++)で使う名前。2つ目がavrdudeで使う名前。
#同じ製品なのにツールによってニックネームが違うからこんな面倒なことになっている...。
#前者はググる。後者は "avrdude -p ?" で一覧を見れる。
MCU := atmega328p m328p
#CPU周波数(MHz)
F_CPU := 1000000
#ライタ("avrdude -c ?" で一覧を見れる)
WRITER := dragon_isp
#プログラム名
PROGNAME := main
#ビルドタイプ
BUILD_TYPE := release
#プログラミング言語(C, C++)
LANG := C
#ソースファイル
SRCS := main.c
#コンパイラ, ソースファイル拡張子
ifeq ($(LANG), C)
CMP := avr-gcc
SRCEXT := c
else ifeq ($(LANG), C++)
CMP := avr-g++
SRCEXT := cpp
endif
CMPFLAGS := -Wall -mmcu=$(word 1,$(MCU)) -DF_CPU=$(F_CPU)UL
ifeq ($(BUILD_TYPE), release)
CMPFLAGS += -DNDEBUG -O3
else ifeq ($(BUILD_TYPE), debug)
CMPFLAGS += -O0 -g
endif
#出力先ディレクトリ
OUTDIR := ../build/$(BUILD_TYPE)
#実行ファイル(.out)
OUT := $(OUTDIR)/$(PROGNAME).out
#16進データ(.hex)
HEX := $(OUTDIR)/$(PROGNAME).hex
#オブジェクトファイル
OBJS := $(SRCS:%.$(SRCEXT)=$(OUTDIR)/%.o)
#ヘッダ依存関係情報ファイル
DEPS := $(SRCS:%.$(SRCEXT)=$(OUTDIR)/%.d)
#依存関係情報ファイルのインクルード
-include $(DEPS)
#デフォルトターゲット
.DEFAULT_GOAL := write
#オブジェクトファイルとヘッダ依存関係ファイルを生成
$(OUTDIR)/%.o: %.$(SRCEXT)
@if [ ! -e `dirname $@` ]; then mkdir -p `dirname $@`; fi
$(CMP) $(CMPFLAGS) -o $@ -c -MMD -MP -MF $(@:%.o=%.d) $<
#リンク
$(OUT): $(OBJS)
$(CMP) $(CMPFLAGS) -o $(OUT) $(OBJS)
#HEXファイル
$(OUTDIR)/%.hex: $(OUTDIR)/%.out
avr-objcopy -j .text -j .data -O ihex $< $@
#コンパイル(HEXファイル作成)
.PHONY: comp
comp: $(HEX)
#書き込み
.PHONY: write
write: $(HEX)
avrdude -c $(WRITER) -p $(word 2,$(MCU)) -U flash:w:$(HEX)
#クリーニング
.PHONY: clean
clean:
@rm -rf $(OBJS) $(DEPS) $(OUT) $(HEX)
#削除
.PHONY: remove
remove:
@rm -rf $(OUTDIR)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment