From 35570f3d0f144e2672eaf5ef0aed51c15b4d498b Mon Sep 17 00:00:00 2001
From: Anders Blomdell <anders.blomdell@control.lth.se>
Date: Tue, 29 May 2018 12:19:33 +0200
Subject: [PATCH] Enable running from top level directory

---
 .gitignore              |  3 ++-
 Makefile                | 14 ++++++++++++++
 README.md               | 12 ++++++++++++
 lib/avr/Makefile.common | 27 +++++++++++++++++++++------
 4 files changed, 49 insertions(+), 7 deletions(-)
 create mode 100644 Makefile

diff --git a/.gitignore b/.gitignore
index 4565959..0ee7f02 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
-*/avr/compiled
\ No newline at end of file
+*/avr/compiled
+*~
\ No newline at end of file
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..a95c907
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,14 @@
+all:
+	@echo "Do 'make PROCESS.LOAD' or 'make PROCESS.FUSE'" >&2
+	@echo "where PROCESS is one of" >&2
+	@for m in */*/Makefile ; do \
+	    echo "  $$(dirname $$(dirname $$m))" >&2 \
+	; done
+	@echo >&2
+	@exit 1
+
+%.LOAD:
+	make -C $*/avr $@
+
+%.FUSE:
+	make -C $*/avr $@
diff --git a/README.md b/README.md
index 44d0bc5..5411b86 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,15 @@
 Software for processes used at Department of Automatic Control
 ==============================================================
 
+batch-2002
+
+
+How to (re)program a chip
+=========================
+
+Clone this repository and change to the top level directory, then
+
+  * If it's the first time this chip is programmed, the fuse bits
+    need to be programmed, this is done by running `make PROCESS_NAME.FUSE`
+
+  * Program the chip by running `make PROCESS_NAME.LOAD`
diff --git a/lib/avr/Makefile.common b/lib/avr/Makefile.common
index cc9828b..159b24d 100644
--- a/lib/avr/Makefile.common
+++ b/lib/avr/Makefile.common
@@ -68,12 +68,27 @@ OBJECTS=$($(1).C:%=compiled/$(1)/%.o)
 	        -U flash:w:compiled/$(TARGET)/$*.sr:s
 
 %.FUSE:
-	[ -n "$($(TARGET).FUSE_L)" ] && avrdude -P usb -c avrisp2 -p $(CHIP) \
-	        -U lfuse:w:$($(TARGET).FUSE_L):m || true
-	[ -n "$($(TARGET).FUSE_H)" ] && avrdude -P usb -c avrisp2 -p $(CHIP) \
-	        -U hfuse:w:$($(TARGET).FUSE_H):m || true
-	[ -n "$($(TARGET).FUSE_E)" ] && avrdude -P usb -c avrisp2 -p $(CHIP) \
-	        -U efuse:w:$($(TARGET).FUSE_E):m || true
+	@if [ -n "$($(TARGET).FUSE_L)" ] ; then \
+	    echo avrdude -P usb -c avrisp2 -p $(CHIP) \
+		         -U lfuse:w:$($(TARGET).FUSE_L):m >&2 \
+	    ; avrdude -qq -P usb -c avrisp2 -p $(CHIP) \
+                    -U lfuse:w:$($(TARGET).FUSE_L):m \
+	    || (echo "failed to program FUSE_L with $($(TARGET).FUSE_L)" >&2 && exit 1) \
+	; fi
+	@if [ -n "$($(TARGET).FUSE_H)" ] ; then \
+	    echo avrdude -P usb -c avrisp2 -p $(CHIP) \
+		         -U hfuse:w:$($(TARGET).FUSE_H):m >&2 \
+	    ; avrdude -qq -P usb -c avrisp2 -p $(CHIP) \
+                    -U hfuse:w:$($(TARGET).FUSE_H):m \
+	    || (echo "failed to program FUSE_H with $($(TARGET).FUSE_H)" >&2 && exit 1) \
+	; fi
+	@if [ -n "$($(TARGET).FUSE_E)" ] ; then \
+	    echo avrdude -P usb -c avrisp2 -p $(CHIP) \
+		         -U efuse:w:$($(TARGET).FUSE_E):m >&2 \
+	    ; avrdude -qq -P usb -c avrisp2 -p $(CHIP) \
+                    -U efuse:w:$($(TARGET).FUSE_E):m \
+	    || (echo "failed to program FUSE_E with $($(TARGET).FUSE_E)" >&2 && exit 1) \
+	; fi
 
 %.FUSE_uisp:
 	exit 1
-- 
GitLab