-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
89 lines (64 loc) · 2.48 KB
/
Copy pathMakefile
File metadata and controls
89 lines (64 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
###############################################
#
# Automation script for AVRASM and Linux
#
###############################################
AS=avrasm
NAME=tsb_tinymega
APP ?= ../ukit-firmware/build/main.hex
APPNTSB=build/app-n-tsb.hex
AVR_PROGRAMMER ?= usbasp
V ?= $(VERBOSE)
ifeq ("$(V)","1")
Q :=
vecho := @true
else
Q := @
vecho := @echo
endif
WRITE_OPTS =
ifdef ERASE
WRITE_OPTS += -e
endif
build/$(NAME).hex: $(NAME).asm
$(Q) $(AS) $(NAME)
compile: build/$(NAME).hex
build/$(NAME).elf: build/$(NAME).hex
$(Q) avr-objcopy -I ihex -O elf32-avr $< $@
elf: build/$(NAME).elf
flash: build/$(NAME).hex
$(Q) sudo avrdude -c $(AVR_PROGRAMMER) -p t1634 -B 12 $(WRITE_OPTS) -U flash:w:build/$(NAME).hex:i
patch:
$(Q) export TEMP_DIR=`mktemp -d`
# 3. Copy the final code from the device:
sudo avrdude -c $(AVR_PROGRAMMER) -p t1634 -B 11 -U flash:r:"${TEMP_DIR}/flash-mem-tsb.bin":r
# Convert the BIN file to HEX
$(Q) bin2hex.py $(TEMP_DIR)/flash-mem-tsb.bin ${TEMP_DIR}/flash-mem-tsb.hex
# Find the address where the RESET label is located
avr-objdump -m avr -l -w -S -j .sec1 -D ${TEMP_DIR}/flash-mem-tsb.hex | less
# Calculate the address using that example:
# Address = 0x3d00 = 15616 /2 -> 7808 -> 1E80 -> 80 1E
# python: hex( 0x3d00 / 2 ) -> swap bytes
$(Q) echo -en "\x0c\x94\x80\x1E" > ${TEMP_DIR}/data.bin
# 7. Edit the BIN file and replace the first four bytes the right jump opcode
$(Q) cp ${TEMP_DIR}/flash-mem-tsb.bin ${TEMP_DIR}/flash-mem-tsb-with-reset.bin
$(Q) dd if=${TEMP_DIR}/data.bin of=${TEMP_DIR}/flash-mem-tsb-with-reset.bin bs=1 count=5 conv=notrunc
# 8. Burn the new firmware to the device
sudo avrdude -c $(AVR_PROGRAMMER) -p t1634 -B 12 -U flash:w:${TEMP_DIR}/flash-mem-tsb-with-reset.bin:r
$(Q) rm -rf ${TEMP_DIR}
$(APPNTSB): build/$(NAME).hex $(APP)
# Merge files and remove their end markers
$(Q) cat $(APP) build/$(NAME).hex | grep -v ':00000001FF' > $(APPNTSB)
# Modify the reset vector
# python: hex( 0x3d00 / 2 ) -> swap bytes
# replace 0C940401 with 0C94D01E / 1EC9
$(Q) sed -i -- 's/100000000C9404010C9439000C9448000C943800B2/100000000C941EC90C9439000C9448000C943800D0/g' $(APPNTSB)
# Modify the APP JUMP address on the last page
$(Q) echo -n ":103FE0000401FFFFFFFFFFFFFFFFFFFFFFFFFFFFDA\r\n" >> $(APPNTSB)
# Add final reset vector
$(Q) echo ':00000001FF' >> $(APPNTSB)
merge: $(APPNTSB)
flash-app: $(APPNTSB)
$(Q) sudo avrdude -c $(AVR_PROGRAMMER) -p t1634 -B 12 -U flash:w:build/$(APPNTSB):i
all: compile
.PHONY: all compile flash merge elf