From e0fec98477d80c1db97caa6eec67072879aa65e0 Mon Sep 17 00:00:00 2001
From: Hauke Petersen <mail@haukepetersen.de>
Date: Tue, 5 Aug 2014 00:51:23 +0200
Subject: [PATCH] boards: initial import of yunjia-nrf51822 board

---
 boards/yunjia-nrf51822/Makefile              |   4 +
 boards/yunjia-nrf51822/Makefile.include      |  52 ++++++++
 boards/yunjia-nrf51822/board.c               |  30 +++++
 boards/yunjia-nrf51822/dist/debug            |  14 +++
 boards/yunjia-nrf51822/dist/debug-server.sh  |  17 +++
 boards/yunjia-nrf51822/dist/debug.sh         |  19 +++
 boards/yunjia-nrf51822/dist/flash.sh         |  27 +++++
 boards/yunjia-nrf51822/dist/gdb.cfg          |   1 +
 boards/yunjia-nrf51822/dist/openocd.cfg      |   8 ++
 boards/yunjia-nrf51822/dist/reset.sh         |  17 +++
 boards/yunjia-nrf51822/include/board.h       |  62 ++++++++++
 boards/yunjia-nrf51822/include/periph_conf.h | 119 +++++++++++++++++++
 12 files changed, 370 insertions(+)
 create mode 100644 boards/yunjia-nrf51822/Makefile
 create mode 100644 boards/yunjia-nrf51822/Makefile.include
 create mode 100644 boards/yunjia-nrf51822/board.c
 create mode 100755 boards/yunjia-nrf51822/dist/debug
 create mode 100755 boards/yunjia-nrf51822/dist/debug-server.sh
 create mode 100755 boards/yunjia-nrf51822/dist/debug.sh
 create mode 100755 boards/yunjia-nrf51822/dist/flash.sh
 create mode 100644 boards/yunjia-nrf51822/dist/gdb.cfg
 create mode 100644 boards/yunjia-nrf51822/dist/openocd.cfg
 create mode 100755 boards/yunjia-nrf51822/dist/reset.sh
 create mode 100644 boards/yunjia-nrf51822/include/board.h
 create mode 100644 boards/yunjia-nrf51822/include/periph_conf.h

diff --git a/boards/yunjia-nrf51822/Makefile b/boards/yunjia-nrf51822/Makefile
new file mode 100644
index 0000000000..37891de8e6
--- /dev/null
+++ b/boards/yunjia-nrf51822/Makefile
@@ -0,0 +1,4 @@
+# tell the Makefile.base which module to build
+MODULE = $(BOARD)_base
+
+include $(RIOTBASE)/Makefile.base
diff --git a/boards/yunjia-nrf51822/Makefile.include b/boards/yunjia-nrf51822/Makefile.include
new file mode 100644
index 0000000000..a0ca43a533
--- /dev/null
+++ b/boards/yunjia-nrf51822/Makefile.include
@@ -0,0 +1,52 @@
+# define the cpu used by the yunjia-nrf51822 board
+export CPU = nrf51822
+export CPU_MODEL = nrf51822qfaa
+
+#define the default port depending on the host OS
+OS := $(shell uname)
+ifeq ($(OS),Linux)
+  PORT ?= /dev/ttyUSB0
+else ifeq ($(OS),Darwin)
+  PORT ?= $(shell ls -1 /dev/tty.SLAB_USBtoUART* | head -n 1)
+else
+  $(info CAUTION: No flash tool for your host system found!)
+  # TODO: add support for windows as host platform
+endif
+export PORT
+
+# define tools used for building the project
+export PREFIX = arm-none-eabi-
+export CC = $(PREFIX)gcc
+export AR = $(PREFIX)ar
+export AS = $(PREFIX)as
+export LINK = $(PREFIX)gcc
+export SIZE = $(PREFIX)size
+export OBJCOPY = $(PREFIX)objcopy
+export TERMPROG = $(RIOTBASE)/dist/tools/pyterm/pyterm
+export FLASHER = $(RIOTBOARD)/$(BOARD)/dist/flash.sh
+export DEBUGGER = $(RIOTBOARD)/$(BOARD)/dist/debug.sh
+export DEBUGSERVER = $(RIOTBOARD)/$(BOARD)/dist/debug-server.sh
+export RESET = $(RIOTBOARD)/$(BOARD)/dist/reset.sh
+
+# define build specific options
+CPU_USAGE = -mcpu=cortex-m0
+FPU_USAGE =
+export CFLAGS += -ggdb -g3 -std=gnu99 -Os -Wall -Wstrict-prototypes $(CPU_USAGE) $(FPU_USAGE) -mlittle-endian -mthumb -mthumb-interwork -nostartfiles
+export CFLAGS += -ffunction-sections -fdata-sections -fno-builtin
+export ASFLAGS += -ggdb -g3 $(CPU_USAGE) $(FPU_USAGE) -mlittle-endian
+export LINKFLAGS += -g3 -ggdb -std=gnu99 $(CPU_USAGE) $(FPU_USAGE) -mlittle-endian -static -lgcc -mthumb -mthumb-interwork -nostartfiles
+# $(LINKERSCRIPT) is specified in cpu/Makefile.include
+export LINKFLAGS += -T$(LINKERSCRIPT)
+export OFLAGS = -O binary
+export HEXFILE = $(ELFFILE:.elf=.bin)
+export TERMFLAGS += -p "$(PORT)"
+export FFLAGS = $(HEXFILE)
+export DEBUGGER_FLAGS = $(ELFFILE)
+
+# use the nano-specs of the NewLib when available
+ifeq ($(shell $(LINK) -specs=nano.specs -E - 2>/dev/null >/dev/null </dev/null ; echo $$?),0)
+export LINKFLAGS += -specs=nano.specs -lc -lnosys
+endif
+
+# export board specific includes to the global includes-listing
+export INCLUDES += -I$(RIOTBOARD)/$(BOARD)/include
diff --git a/boards/yunjia-nrf51822/board.c b/boards/yunjia-nrf51822/board.c
new file mode 100644
index 0000000000..4e87ebb906
--- /dev/null
+++ b/boards/yunjia-nrf51822/board.c
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2014 Freie Universität Berlin
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser General
+ * Public License v2.1. See the file LICENSE in the top level directory for more
+ * details.
+ */
+
+/**
+ * @ingroup     board_yunjia-nrf51822
+ * @{
+ *
+ * @file        board.c
+ * @brief       Board specific implementations for the Yunjia NRF51822 board
+ *
+ * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
+ *
+ * @}
+ */
+
+#include "board.h"
+#include "cpu.h"
+
+#include "periph/uart.h"
+
+void board_init(void)
+{
+    /* initialize the CPU */
+    cpu_init();
+}
diff --git a/boards/yunjia-nrf51822/dist/debug b/boards/yunjia-nrf51822/dist/debug
new file mode 100755
index 0000000000..c2626d71a2
--- /dev/null
+++ b/boards/yunjia-nrf51822/dist/debug
@@ -0,0 +1,14 @@
+#!/bin/sh
+
+if [ -L "$0" ]; then
+    FILE=$(readlink -e "$0")
+else
+    FILE="$0"
+fi
+
+BIN_FOLDER=$(dirname "${FILE}")
+
+openocd -f "${BIN_FOLDER}/openocd.cfg" \
+    -c "init" \
+    -c "targets" \
+    -c "reset halt" \
diff --git a/boards/yunjia-nrf51822/dist/debug-server.sh b/boards/yunjia-nrf51822/dist/debug-server.sh
new file mode 100755
index 0000000000..b892483caf
--- /dev/null
+++ b/boards/yunjia-nrf51822/dist/debug-server.sh
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+if [ -L "$0" ]; then
+    FILE=$(readlink -e "$0")
+else
+    FILE="$0"
+fi
+
+BIN_FOLDER=$(dirname "${FILE}")
+
+echo "##"
+echo "## Starting debug server"
+echo "##"
+openocd -f "${BIN_FOLDER}/openocd.cfg" \
+    -c "init" \
+    -c "targets" \
+    -c "reset halt" \
diff --git a/boards/yunjia-nrf51822/dist/debug.sh b/boards/yunjia-nrf51822/dist/debug.sh
new file mode 100755
index 0000000000..09be600cda
--- /dev/null
+++ b/boards/yunjia-nrf51822/dist/debug.sh
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+if [ ! -f "$1" ]; then
+    echo "ELF-file $1 does not exist"
+    exit 1
+fi
+
+if [ -L "$0" ]; then
+    FILE=$(readlink -e "$0")
+else
+    FILE="$0"
+fi
+
+BIN_FOLDER=$(dirname "${FILE}")
+
+echo "##"
+echo "## Debugging $1"
+echo "##"
+arm-none-eabi-gdb -tui -command="${BIN_FOLDER}/gdb.cfg" $1
diff --git a/boards/yunjia-nrf51822/dist/flash.sh b/boards/yunjia-nrf51822/dist/flash.sh
new file mode 100755
index 0000000000..3afb80068b
--- /dev/null
+++ b/boards/yunjia-nrf51822/dist/flash.sh
@@ -0,0 +1,27 @@
+#!/bin/sh
+
+if [ ! -f "$1" ]; then
+    echo "Binary file $1 does not exist"
+    exit 1
+fi
+
+if [ -L "$0" ]; then
+    FILE=$(readlink -e "$0")
+else
+    FILE="$0"
+fi
+
+BIN_FOLDER=$(dirname "${FILE}")
+
+echo "##"
+echo "## Flashing $1"
+echo "##"
+openocd -f "${BIN_FOLDER}/openocd.cfg" \
+    -c "init" \
+    -c "targets" \
+    -c "flash banks" \
+    -c "reset halt" \
+    -c "flash write_image erase $1 0" \
+    -c "verify_image $1" \
+    -c "reset run"\
+    -c "shutdown"
diff --git a/boards/yunjia-nrf51822/dist/gdb.cfg b/boards/yunjia-nrf51822/dist/gdb.cfg
new file mode 100644
index 0000000000..900a550d05
--- /dev/null
+++ b/boards/yunjia-nrf51822/dist/gdb.cfg
@@ -0,0 +1 @@
+target extended-remote 127.0.0.1:3333
diff --git a/boards/yunjia-nrf51822/dist/openocd.cfg b/boards/yunjia-nrf51822/dist/openocd.cfg
new file mode 100644
index 0000000000..ab9a01187e
--- /dev/null
+++ b/boards/yunjia-nrf51822/dist/openocd.cfg
@@ -0,0 +1,8 @@
+# nRF51822 Target
+source [find interface/stlink-v2.cfg]
+
+set WORKAREASIZE 0x4000
+source [find target/nrf51_stlink.tcl]
+
+# use hardware reset, connect under reset
+#reset_config srst_only srst_nogate
diff --git a/boards/yunjia-nrf51822/dist/reset.sh b/boards/yunjia-nrf51822/dist/reset.sh
new file mode 100755
index 0000000000..daa5a334f3
--- /dev/null
+++ b/boards/yunjia-nrf51822/dist/reset.sh
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+if [ -L "$0" ]; then
+    FILE=$(readlink "$0")
+else
+    FILE="$0"
+fi
+
+BIN_FOLDER=$(dirname "${FILE}")
+
+echo "##"
+echo "## Resetting $BOARD"
+echo "##"
+openocd -f "${BIN_FOLDER}/openocd.cfg" \
+    -c "init" \
+    -c "reset run" \
+    -c "shutdown"
diff --git a/boards/yunjia-nrf51822/include/board.h b/boards/yunjia-nrf51822/include/board.h
new file mode 100644
index 0000000000..70180b7ce5
--- /dev/null
+++ b/boards/yunjia-nrf51822/include/board.h
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2014 Freie Universität Berlin
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser General
+ * Public License v2.1. See the file LICENSE in the top level directory for more
+ * details.
+ */
+
+/**
+ * @defgroup    board_yunjia-nrf51822 Yunjia NRF51822
+ * @ingroup     boards
+ * @brief       Board specific files for the Yunjia NRF51822 board
+ * @{
+ *
+ * @file        board.h
+ * @brief       Board specific definitions for the Yunjia NRF51822 board
+ *
+ * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
+ */
+
+#ifndef __BOARD_H
+#define __BOARD_H
+
+#include "cpu.h"
+
+/**
+ * @name Define the nominal CPU core clock in this board
+ */
+#define F_CPU               (16000000UL)
+
+/**
+ * @name Define the boards stdio
+ * @{
+ */
+#define STDIO               UART_0
+#define STDIO_BAUDRATE      (115200U)
+/** @} */
+
+/**
+ * @name Assign the hardware timer
+ */
+#define HW_TIMER            TIMER_0
+
+/**
+ * @name Macros for controlling the on-board LEDs.
+ * @{
+ */
+#define LED_RED_ON          /* not available */
+#define LED_RED_OFF         /* not available */
+#define LED_RED_TOGGLE      /* not available */
+#define LED_GREEN_ON        /* not available */
+#define LED_GREEN_OFF       /* not available */
+#define LED_GREEN_TOGGLE    /* not available */
+/* @} */
+
+/**
+ * @brief Initialize board specific hardware, including clock, LEDs and std-IO
+ */
+void board_init(void);
+
+#endif /** __BOARD_H */
+/** @} */
diff --git a/boards/yunjia-nrf51822/include/periph_conf.h b/boards/yunjia-nrf51822/include/periph_conf.h
new file mode 100644
index 0000000000..13a2556ff0
--- /dev/null
+++ b/boards/yunjia-nrf51822/include/periph_conf.h
@@ -0,0 +1,119 @@
+/*
+ * Copyright (C) 2014 Freie Universität Berlin
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser General
+ * Public License v2.1. See the file LICENSE in the top level directory for more
+ * details.
+ */
+
+/**
+ * @ingroup     board_yunjia-nrf51822
+ * @{
+ *
+ * @file        periph_conf.h
+ * @brief       Peripheral MCU configuration for the Yunjia NRF51822 board
+ *
+ * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
+ */
+
+#ifndef __PERIPH_CONF_H
+#define __PERIPH_CONF_H
+
+/**
+ * @name Timer configuration
+ * @{
+ */
+#define TIMER_NUMOF         (1U)
+#define TIMER_0_EN          1
+#define TIMER_1_EN          0
+#define TIMER_2_EN          0
+#define TIMER_IRQ_PRIO      1
+
+/* Timer 0 configuration */
+#define TIMER_0_DEV         NRF_TIMER0
+#define TIMER_0_CHANNELS    3
+#define TIMER_0_MAX_VALUE   (0xffffffff)
+#define TIMER_0_BITMODE     TIMER_BITMODE_BITMODE_32Bit
+#define TIMER_0_ISR         isr_timer0
+#define TIMER_0_IRQ         TIMER0_IRQn
+
+/* Timer 1 configuration */
+#define TIMER_1_DEV         NRF_TIMER1
+#define TIMER_1_CHANNELS    3
+#define TIMER_1_MAX_VALUE   (0xffff)
+#define TIEMR_1_BITMODE     TIMER_BITMODE_BITMODE_16Bit
+#define TIMER_1_ISR         isr_timer1
+#define TIMER_1_IRQ         TIMER1_IRQn
+
+/* Timer 2 configuration */
+#define TIMER_2_DEV         NRF_TIMER2
+#define TIMER_2_CHANNELS    3
+#define TIMER_2_MAX_VALUE   (0xffff)
+#define TIMER_2_BITMODE     TIMER_BITMODE_BITMODE_16Bit
+#define TIMER_2_ISR         isr_timer2
+#define TIMER_2_IRQ         TIMER2_IRQn
+/** @} */
+
+/**
+ * @name UART configuration
+ * @{
+ */
+#define UART_NUMOF          (1U)
+#define UART_0_EN           1
+#define UART_IRQ_PRIO       1
+
+/* UART 0 device configuration */
+#define UART_0_DEV          NRF_UART0
+#define UART_0_PIN_RX       1
+#define UART_0_PIN_TX       2
+/** @} */
+
+/**
+ * @name Real time counter configuration
+ * @{
+ */
+#define RTT_NUMOF           (1U)
+#define RTT_IRQ_PRIO        1
+
+#define RTT_DEV             NRF_RTC0
+#define RTT_IRQ             RTC0_IRQn
+#define RTT_ISR             isr_rtc0
+#define RTT_MAX_VALUE       (0xffffff)
+#define RTT_FREQUENCY       (10)            /* in Hz */
+#define RTT_PRESCALER       (3275U)         /* run with 10 Hz */
+/** @} */
+
+/**
+ * @name Random Number Generator configuration
+ * @{
+ */
+#define RANDOM_NUMOF        (1U)
+/** @} */
+
+/**
+ * @name GPIO configuration
+ * @{
+ */
+#define GPIO_NUMOF          (8U)
+#define GPIO_0_EN           1
+#define GPIO_1_EN           1
+#define GPIO_2_EN           1
+#define GPIO_3_EN           1
+#define GPIO_4_EN           1
+#define GPIO_5_EN           1
+#define GPIO_6_EN           1
+#define GPIO_7_EN           1
+#define GPIO_IRQ_PRIO       1
+
+/* GPIO pin configuration */
+#define GPIO_0_PIN          7
+#define GPIO_1_PIN          8
+#define GPIO_2_PIN          9
+#define GPIO_3_PIN          10
+#define GPIO_4_PIN          11
+#define GPIO_5_PIN          12
+#define GPIO_6_PIN          13
+#define GPIO_7_PIN          14
+/** @} */
+
+#endif /* __PERIPH_CONF_H */
-- 
GitLab