From 17fe3f4348d773129488f34eba5e5fed1314eff3 Mon Sep 17 00:00:00 2001
From: Hinnerk van Bruinehsen <h.v.bruinehsen@fu-berlin.de>
Date: Tue, 23 Sep 2014 11:24:39 +0200
Subject: [PATCH] boards:mega2560: add debug and debug-server targets

---
 boards/arduino-mega2560/Makefile.include  | 18 +++++++++++++++---
 boards/arduino-mega2560/dist/debug.sh     |  8 ++++++++
 boards/arduino-mega2560/dist/debug_srv.sh |  7 +++++++
 boards/arduino-mega2560/dist/gdb.conf     |  1 +
 4 files changed, 31 insertions(+), 3 deletions(-)
 create mode 100755 boards/arduino-mega2560/dist/debug.sh
 create mode 100755 boards/arduino-mega2560/dist/debug_srv.sh
 create mode 100644 boards/arduino-mega2560/dist/gdb.conf

diff --git a/boards/arduino-mega2560/Makefile.include b/boards/arduino-mega2560/Makefile.include
index 1ed943f90b..af890c5724 100644
--- a/boards/arduino-mega2560/Makefile.include
+++ b/boards/arduino-mega2560/Makefile.include
@@ -28,16 +28,28 @@ else
 endif
 export FLASHER
 export PORT
+export DIST_PATH = $(RIOTBOARD)/$(BOARD)/dist
+export DEBUGSERVER_PORT = 4242
+export DEBUGSERVER = $(DIST_PATH)/debug_srv.sh
+export DEBUGSERVER_FLAGS = "-g -j usb :$(DEBUGSERVER_PORT)"
+export DEBUGGER_FLAGS = "-x $(RIOTBOARD)/$(BOARD)/dist/gdb.conf $(ELFFILE)"
+export DEBUGGER = $(DIST_PATH)/debug.sh $(DEBUGSERVER_FLAGS) $(DIST_PATH) $(DEBUGSERVER_PORT)
+
+# PROGRAMMER defaults to stk500v2 which is the internal flasher via USB. Can be
+# overridden for debugging (which requires changes that require to use an ISP)
+export PROGRAMMER ?= stk500v2
+
+ifeq ($(PROGRAMMER), stk500v2)
+    export PROGRAMMER_FLAGS = -P $(PORT) -b 115200
+endif
 
 # define build specific options
 export CPU_USAGE = -mmcu=atmega2560
 export CFLAGS += -ggdb -g3 -std=gnu99 -Os -Wall -Wstrict-prototypes $(CPU_USAGE)
 export ASFLAGS += -ggdb -g3 $(CPU_USAGE) $(FPU_USAGE)
 export LINKFLAGS += -g3 -ggdb -std=gnu99 $(CPU_USAGE) $(FPU_USAGE) -static -lgcc -e reset_handler
-# linkerscript specified in cpu/Makefile.include
-#export LINKFLAGS += -T$(LINKERSCRIPT)
 export OFLAGS += -j .text -j .data -O ihex
-export FFLAGS += -p m2560 -c stk500v2 -P $(PORT) -b 115200 -F -U flash:w:bin/$(BOARD)/$(PROJECT)$(APPLICATION).hex
+export FFLAGS += -p m2560 -c $(PROGRAMMER) $(PROGRAMMER_FLAGS) -F -U flash:w:bin/$(BOARD)/$(PROJECT)$(APPLICATION).hex
 
 # export board specific includes to the global includes-listing
 export INCLUDES += -I$(RIOTBOARD)/$(BOARD)/include
diff --git a/boards/arduino-mega2560/dist/debug.sh b/boards/arduino-mega2560/dist/debug.sh
new file mode 100755
index 0000000000..ddeca9ff2f
--- /dev/null
+++ b/boards/arduino-mega2560/dist/debug.sh
@@ -0,0 +1,8 @@
+#!/bin/bash
+sleep 2
+setsid -w avarice $1 &
+#sleep 2 && $2/avr-gdb-wrapper -ex "target remote localhost:$3" $4
+sleep 3 && avr-gdb -ex "target remote localhost:$3" $4
+
+# avarice exits with 1 if the connection is released, therefore we always exit with 0
+exit 0
diff --git a/boards/arduino-mega2560/dist/debug_srv.sh b/boards/arduino-mega2560/dist/debug_srv.sh
new file mode 100755
index 0000000000..449651afbc
--- /dev/null
+++ b/boards/arduino-mega2560/dist/debug_srv.sh
@@ -0,0 +1,7 @@
+#!/bin/bash
+
+sleep 2
+avarice $1
+
+# avarice exits with 1 if the connection is released, therefore we always exit with 0
+exit 0
diff --git a/boards/arduino-mega2560/dist/gdb.conf b/boards/arduino-mega2560/dist/gdb.conf
new file mode 100644
index 0000000000..ca68eb344c
--- /dev/null
+++ b/boards/arduino-mega2560/dist/gdb.conf
@@ -0,0 +1 @@
+set $pc=0x00
-- 
GitLab