Skip to content

Instantly share code, notes, and snippets.

@clintonyeb
Last active May 15, 2018 01:24
Show Gist options
  • Save clintonyeb/f658a989e42d9a3e0eb4e58d80db7bc6 to your computer and use it in GitHub Desktop.
Save clintonyeb/f658a989e42d9a3e0eb4e58d80db7bc6 to your computer and use it in GitHub Desktop.
Generates A Makefile for an Arduino when created with pure c

Generates A Makefile for an Arduino when created with pure c

How to Use: Call script the name of the file where is to be compiled and uploaded. make: simply compile file make upload: will compile and upload to target machine

This script is adopted for the Arduino UNO Atmega328p

#!/usr/bin/env bash

if [ -z "$1" ]
  then
    echo
    echo "Cannot generate makefile"
    echo "Please provide a program file name"
    exit -1
fi

name=$1

cat > makefile << EOF
PRG            = ${name}
OBJ            = \$(PRG).o
MCU_TARGET     = atmega328p
PROGRAMMER     = arduino
AVRDUDE_TARGET = ATMEGA328P
PORT		   = /dev/ttyACM0
OPTIMIZE       = -Os
DEFS           = -DF_CPU=16000000UL
LIBS           =
DUDE           = avrdude
CC             = avr-gcc
OBJCOPY        = avr-objcopy
OBJDUMP        = avr-objdump
BAUD_RATE      = 115200
override CFLAGS        = -g -Wall \$(OPTIMIZE) -mmcu=\$(MCU_TARGET) \$(DEFS)
override LDFLAGS       = -Wl,-Map,\$(PRG).map

all: text

\$(PRG).elf: \$(OBJ)
	\$(CC) \$(CFLAGS) \$(LDFLAGS) -o \$@ \$^ \$(LIBS)

clean:
	rm -rf \$(PRG).o \$(PRG).elf \$(PRG).eps  \$(PRG).hex
	rm -rf \$(PRG).lst \$(PRG).map \$(EXTRA_CLEAN_FILES)

text: hex

hex:  \$(PRG).hex

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

upload: 
	\$(DUDE) -F -V -p \$(AVRDUDE_TARGET) -c \$(PROGRAMMER) -P \$(PORT) -b \$(BAUD_RATE) -U flash:w:\$(PRG).hex 
	 	
erase:
	\$(DUDE) -p \$(AVRDUDE_TARGET) -c \$(PROGRAMMER) -P \$(PORT) -e -vv -b \$(BAUD_RATE)
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment