From 67d7346d27cbe9757f743d81b28fda8749fc4b0a Mon Sep 17 00:00:00 2001
From: Bumsik Kim <kbumsik@gmail.com>
Date: Wed, 3 May 2017 04:18:45 -0400
Subject: [PATCH] boards/arduino-mkr1000: Updated periph config and SWD
 flashing (#3)

---
 boards/arduino-mkr1000/Makefile.features      |  4 +
 boards/arduino-mkr1000/Makefile.include       | 30 ++++---
 boards/arduino-mkr1000/board.c                |  2 +-
 .../arduino-mkr1000/include/arduino_board.h   |  7 ++
 .../arduino-mkr1000/include/arduino_pinmap.h  | 44 ++++++----
 boards/arduino-mkr1000/include/board.h        | 10 +--
 boards/arduino-mkr1000/include/gpio_params.h  |  2 +-
 boards/arduino-mkr1000/include/periph_conf.h  | 85 ++++++++++++++-----
 8 files changed, 122 insertions(+), 62 deletions(-)

diff --git a/boards/arduino-mkr1000/Makefile.features b/boards/arduino-mkr1000/Makefile.features
index 9e10c5ba07..59999a6fe1 100644
--- a/boards/arduino-mkr1000/Makefile.features
+++ b/boards/arduino-mkr1000/Makefile.features
@@ -1,7 +1,11 @@
 # Put defined MCU peripherals here (in alphabetical order)
+FEATURES_PROVIDED += periph_adc
 FEATURES_PROVIDED += periph_cpuid
 FEATURES_PROVIDED += periph_gpio
 FEATURES_PROVIDED += periph_i2c
+FEATURES_PROVIDED += periph_pwm
+FEATURES_PROVIDED += periph_rtc
+FEATURES_PROVIDED += periph_rtt
 FEATURES_PROVIDED += periph_spi
 FEATURES_PROVIDED += periph_timer
 FEATURES_PROVIDED += periph_uart
diff --git a/boards/arduino-mkr1000/Makefile.include b/boards/arduino-mkr1000/Makefile.include
index c0991eadb7..42923676a3 100644
--- a/boards/arduino-mkr1000/Makefile.include
+++ b/boards/arduino-mkr1000/Makefile.include
@@ -1,18 +1,28 @@
 # define the cpu used by Arduino/Genuino MKR1000 board
 export CPU = samd21
-export CPU_MODEL = samd21g18a_mkr1000
-export CFLAGS += -D__SAMD21G18A__
+export CPU_MODEL = samd21g18a
 
-# set default port depending on operating system
-PORT_LINUX ?= /dev/ttyACM0
-PORT_DARWIN ?= $(firstword $(sort $(wildcard /dev/tty.usbmodem*)))
+#export needed for flash rule
+export PORT_LINUX ?= /dev/ttyACM0
+export PORT_DARWIN ?= $(firstword $(sort $(wildcard /dev/tty.usbmodem*)))
 
-# define board specific flasher options
-export FLASHER = $(RIOTBOARD)/$(BOARD)/dist/flash.sh
-export OFLAGS = -O binary
+ifeq ($(PROGRAMMER),jlink)
+    # in case J-Link is attached to SWD pins, use a plain CPU memory model
+    export JLINK_DEVICE := atsamw25
+    include $(RIOTMAKE)/tools/jlink.inc.mk
+else
+    # when BOSSA is used (default), use a different flash map
+    # refer https://github.com/shumatech/BOSSA for this programmer
+    export PROGRAMMER = bossa
+    export LINKER_SCRIPT ?= $(RIOTCPU)/sam0_common/ldscripts/$(CPU_MODEL)_mkr1000.ld
 
-# setup the boards dependencies
-include $(RIOTBOARD)/$(BOARD)/Makefile.dep
+    # define board specific flasher options
+    export FLASHER = $(RIOTBOARD)/$(BOARD)/dist/flash.sh
+    export OFLAGS = -O binary
+endif
 
 # setup serial terminal
 include $(RIOTMAKE)/tools/serial.inc.mk
+
+# setup the boards dependencies
+include $(RIOTBOARD)/$(BOARD)/Makefile.dep
diff --git a/boards/arduino-mkr1000/board.c b/boards/arduino-mkr1000/board.c
index cd36bd113a..b06f898c8d 100644
--- a/boards/arduino-mkr1000/board.c
+++ b/boards/arduino-mkr1000/board.c
@@ -28,6 +28,6 @@ void board_init(void)
 {
     /* initialize the CPU */
     cpu_init();
-    /* initialize the on-board Amber "L" LED on pin PA17 */
+    /* initialize the on-board Green "L" LED on pin PA20 */
     gpio_init(LED0_PIN, GPIO_OUT);
 }
diff --git a/boards/arduino-mkr1000/include/arduino_board.h b/boards/arduino-mkr1000/include/arduino_board.h
index fecdc110fe..d95f0e8e3d 100644
--- a/boards/arduino-mkr1000/include/arduino_board.h
+++ b/boards/arduino-mkr1000/include/arduino_board.h
@@ -44,6 +44,13 @@ static const gpio_t arduino_pinmap[] = {
     ARDUINO_PIN_5,
     ARDUINO_PIN_6,
     ARDUINO_PIN_7,
+    ARDUINO_PIN_8,
+    ARDUINO_PIN_9,
+    ARDUINO_PIN_10,
+    ARDUINO_PIN_11,
+    ARDUINO_PIN_12,
+    ARDUINO_PIN_13,
+    ARDUINO_PIN_14,
     ARDUINO_PIN_A0,
     ARDUINO_PIN_A1,
     ARDUINO_PIN_A2,
diff --git a/boards/arduino-mkr1000/include/arduino_pinmap.h b/boards/arduino-mkr1000/include/arduino_pinmap.h
index 39ad1bddc9..f8764779c2 100644
--- a/boards/arduino-mkr1000/include/arduino_pinmap.h
+++ b/boards/arduino-mkr1000/include/arduino_pinmap.h
@@ -29,26 +29,34 @@ extern "C" {
 #endif
 
 /**
- * @brief   Mapping of MCU pins to Arduino pins
+ * @name    Mapping of MCU pins to Arduino pins
  * @{
  */
-#define ARDUINO_PIN_0           GPIO_PIN(PA, 22)
-#define ARDUINO_PIN_1           GPIO_PIN(PA, 23)
-#define ARDUINO_PIN_2           GPIO_PIN(PA, 10)
-#define ARDUINO_PIN_3           GPIO_PIN(PA, 11)
-#define ARDUINO_PIN_4           GPIO_PIN(PB, 10)
-#define ARDUINO_PIN_5           GPIO_PIN(PB, 11)
-#define ARDUINO_PIN_6           GPIO_PIN(PA, 20) /* on-board LED */
-#define ARDUINO_PIN_7           GPIO_PIN(PA, 21)
-
-#define ARDUINO_PIN_A0          GPIO_PIN(PA, 2)
-#define ARDUINO_PIN_A1          GPIO_PIN(PB, 2)
-#define ARDUINO_PIN_A2          GPIO_PIN(PB, 3)
-#define ARDUINO_PIN_A3          GPIO_PIN(PA, 4)
-#define ARDUINO_PIN_A4          GPIO_PIN(PA, 5)
-#define ARDUINO_PIN_A5          GPIO_PIN(PA, 6)
-#define ARDUINO_PIN_A6          GPIO_PIN(PA, 7)
-/** @ */
+#define ARDUINO_PIN_0           GPIO_PIN(PA, 22) /* TC4-W0 */
+#define ARDUINO_PIN_1           GPIO_PIN(PA, 23) /* TC4-W1 */
+#define ARDUINO_PIN_2           GPIO_PIN(PA, 10) /* TCC0-W2 */
+#define ARDUINO_PIN_3           GPIO_PIN(PA, 11) /* TCC0-W3 */
+#define ARDUINO_PIN_4           GPIO_PIN(PB, 10) /* TCC0-W4 */
+#define ARDUINO_PIN_5           GPIO_PIN(PB, 11) /* TCC0-W5 */
+#define ARDUINO_PIN_6           GPIO_PIN(PA, 20) /* TCC0-W6, on-board LED */
+#define ARDUINO_PIN_7           GPIO_PIN(PA, 21) /* TCC0-W7 */
+
+#define ARDUINO_PIN_8           GPIO_PIN(PA, 16) /* SERCOM1-MOSI */
+#define ARDUINO_PIN_9           GPIO_PIN(PA, 17) /* SERCOM1-SCK */
+#define ARDUINO_PIN_10          GPIO_PIN(PA, 19) /* SERCOM1-MISO */
+#define ARDUINO_PIN_11          GPIO_PIN(PA, 8)  /* SERCOM0-SDA, on-board pull-up */
+#define ARDUINO_PIN_12          GPIO_PIN(PA, 9)  /* SERCOM0-SCL, on-board pull-up */
+#define ARDUINO_PIN_13          GPIO_PIN(PB, 23) /* SERCOM5-RX from MCU */
+#define ARDUINO_PIN_14          GPIO_PIN(PB, 22) /* SERCOM5-TX from MCU */
+
+#define ARDUINO_PIN_A0          GPIO_PIN(PA, 2)  /* AIN0, DAC0 */
+#define ARDUINO_PIN_A1          GPIO_PIN(PB, 2)  /* AIN10 */
+#define ARDUINO_PIN_A2          GPIO_PIN(PB, 3)  /* AIN11 */
+#define ARDUINO_PIN_A3          GPIO_PIN(PA, 4)  /* AIN4 */
+#define ARDUINO_PIN_A4          GPIO_PIN(PA, 5)  /* AIN5 */
+#define ARDUINO_PIN_A5          GPIO_PIN(PA, 6)  /* AIN6 */
+#define ARDUINO_PIN_A6          GPIO_PIN(PA, 7)  /* AIN7 */
+/** @} */
 
 #ifdef __cplusplus
 }
diff --git a/boards/arduino-mkr1000/include/board.h b/boards/arduino-mkr1000/include/board.h
index 4018b5117c..407f5442fb 100644
--- a/boards/arduino-mkr1000/include/board.h
+++ b/boards/arduino-mkr1000/include/board.h
@@ -32,15 +32,7 @@ extern "C" {
 #endif
 
 /**
- * @brief   xtimer configuration
- * @{
- */
-#define XTIMER              TIMER_0
-#define XTIMER_CHAN         (0)
-/** @} */
-
-/**
- * @brief   LED pin definitions and handlers
+ * @name   LED pin definitions and handlers
  * @{
  */
 #define LED0_PIN            GPIO_PIN(PA, 20)
diff --git a/boards/arduino-mkr1000/include/gpio_params.h b/boards/arduino-mkr1000/include/gpio_params.h
index 20f4b2cbc8..dd5f5ecdb8 100644
--- a/boards/arduino-mkr1000/include/gpio_params.h
+++ b/boards/arduino-mkr1000/include/gpio_params.h
@@ -34,7 +34,7 @@ extern "C" {
 static const  saul_gpio_params_t saul_gpio_params[] =
 {
     {
-        .name = "LED(orange)",
+        .name = "LED(Green)",
         .pin = LED0_PIN,
         .mode = GPIO_OUT
     },
diff --git a/boards/arduino-mkr1000/include/periph_conf.h b/boards/arduino-mkr1000/include/periph_conf.h
index 33607dc803..220ebc68eb 100644
--- a/boards/arduino-mkr1000/include/periph_conf.h
+++ b/boards/arduino-mkr1000/include/periph_conf.h
@@ -18,6 +18,7 @@
  * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
  * @author      Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
  * @author      Alexandre Abadie <alexandre.abadie@inria.fr>
+ * @author      Bumsik kim <kbumsik@gmail.com>
  */
 
 #ifndef PERIPH_CONF_H
@@ -98,7 +99,6 @@ extern "C" {
 #define TIMER_1_CHANNELS    2
 #define TIMER_1_MAX_VALUE   (0xffffffff)
 #define TIMER_1_ISR         isr_tc4
-
 /** @} */
 
 /**
@@ -108,8 +108,8 @@ extern "C" {
 static const uart_conf_t uart_config[] = {
     {
         .dev    = &SERCOM5->USART,
-        .rx_pin = GPIO_PIN(PB,23),
-        .tx_pin = GPIO_PIN(PB,22),
+        .rx_pin = GPIO_PIN(PB,23),  /* ARDUINO_PIN_13, RX Pin */
+        .tx_pin = GPIO_PIN(PB,22),  /* ARDUINO_PIN_14, TX Pin */
         .mux    = GPIO_MUX_D,
         .rx_pad = UART_PAD_RX_3,
         .tx_pad = UART_PAD_TX_2
@@ -159,16 +159,33 @@ static const pwm_conf_t pwm_config[] = {
  * @name ADC configuration
  * @{
  */
-#define ADC_CONFIG {            \
-    { GPIO_PIN(PA, 2), 0, 0  }, \
-    { GPIO_PIN(PB, 2), 0, 2  }, \
-    { GPIO_PIN(PB, 3), 0, 3  }, \
-    { GPIO_PIN(PA, 4), 0, 4  }, \
-    { GPIO_PIN(PA, 5), 0, 5  }, \
-    { GPIO_PIN(PA, 6), 0, 10 }, \
-    { GPIO_PIN(PA, 7), 0, 10 }}
-
-#define ADC_NUMOF           (6)
+#define ADC_0_EN                           1
+#define ADC_MAX_CHANNELS                   14
+/* ADC 0 device configuration */
+#define ADC_0_DEV                          ADC
+#define ADC_0_IRQ                          ADC_IRQn
+
+/* ADC 0 Default values */
+#define ADC_0_CLK_SOURCE                   0 /* GCLK_GENERATOR_0 */
+#define ADC_0_PRESCALER                    ADC_CTRLB_PRESCALER_DIV512
+
+#define ADC_0_NEG_INPUT                    ADC_INPUTCTRL_MUXNEG_GND
+#define ADC_0_GAIN_FACTOR_DEFAULT          ADC_INPUTCTRL_GAIN_1X
+#define ADC_0_REF_DEFAULT                  ADC_REFCTRL_REFSEL_INT1V
+
+static const adc_conf_chan_t adc_channels[] = {
+    /* port, pin, muxpos */
+    {GPIO_PIN(PA, 2), ADC_INPUTCTRL_MUXPOS_PIN0},     /* A0 */
+    {GPIO_PIN(PB, 2), ADC_INPUTCTRL_MUXPOS_PIN10},    /* A1 */
+    {GPIO_PIN(PB, 3), ADC_INPUTCTRL_MUXPOS_PIN11},    /* A2 */
+    {GPIO_PIN(PA, 4), ADC_INPUTCTRL_MUXPOS_PIN4},     /* A3 */
+    {GPIO_PIN(PA, 5), ADC_INPUTCTRL_MUXPOS_PIN5},     /* A4 */
+    {GPIO_PIN(PA, 6), ADC_INPUTCTRL_MUXPOS_PIN6},     /* A5 */
+    {GPIO_PIN(PA, 7), ADC_INPUTCTRL_MUXPOS_PIN7},     /* A6 */
+};
+
+#define ADC_0_CHANNELS                     (7U)
+#define ADC_NUMOF                          ADC_0_CHANNELS
 /** @} */
 
 /**
@@ -178,16 +195,16 @@ static const pwm_conf_t pwm_config[] = {
 static const spi_conf_t spi_config[] = {
     {
         .dev      = &SERCOM1->SPI,
-        .miso_pin = GPIO_PIN(PA, 19),
-        .mosi_pin = GPIO_PIN(PB, 16),
-        .clk_pin  = GPIO_PIN(PB, 17),
-        .miso_mux = GPIO_MUX_D,
-        .mosi_mux = GPIO_MUX_D,
-        .clk_mux  = GPIO_MUX_D,
+        .miso_pin = GPIO_PIN(PA, 19),   /* ARDUINO_PIN_8, SERCOM1-MISO */
+        .mosi_pin = GPIO_PIN(PA, 16),   /* ARDUINO_PIN_10, SERCOM1-MOSI */
+        .clk_pin  = GPIO_PIN(PA, 17),   /* ARDUINO_PIN_9, SERCOM1-SCK */
+        .miso_mux = GPIO_MUX_C,
+        .mosi_mux = GPIO_MUX_C,
+        .clk_mux  = GPIO_MUX_C,
         .miso_pad = SPI_PAD_MISO_3,
         .mosi_pad = SPI_PAD_MOSI_0_SCK_1
     },
-    {
+    {   /* SPI Pins connected to WINC1500 wifi module */
         .dev      = &SERCOM2->SPI,
         .miso_pin = GPIO_PIN(PA, 15),
         .mosi_pin = GPIO_PIN(PA, 12),
@@ -196,7 +213,7 @@ static const spi_conf_t spi_config[] = {
         .mosi_mux = GPIO_MUX_D,
         .clk_mux  = GPIO_MUX_D,
         .miso_pad = SPI_PAD_MISO_3,
-        .mosi_pad = SPI_PAD_MOSI_2_SCK_3
+        .mosi_pad = SPI_PAD_MOSI_0_SCK_1
     }
 };
 
@@ -221,11 +238,33 @@ static const spi_conf_t spi_config[] = {
 #define I2C_0_GCLK_ID       SERCOM0_GCLK_ID_CORE
 #define I2C_0_GCLK_ID_SLOW  SERCOM0_GCLK_ID_SLOW
 /* I2C 0 pin configuration */
-#define I2C_0_SDA           GPIO_PIN(PA, 8)
-#define I2C_0_SCL           GPIO_PIN(PA, 9)
+#define I2C_0_SDA           GPIO_PIN(PA, 8) /* SERCOM0-SDA, on-board pull-up */
+#define I2C_0_SCL           GPIO_PIN(PA, 9) /* SERCOM0-SCL, on-board pull-up */
 #define I2C_0_MUX           GPIO_MUX_C
 /** @} */
 
+/**
+ * @name RTC configuration
+ * @{
+ */
+#define RTC_NUMOF           (1U)
+#define RTC_DEV             RTC->MODE2
+/** @} */
+
+/**
+ * @name RTT configuration
+ * @{
+ */
+#define RTT_NUMOF           (1U)
+#define RTT_DEV             RTC->MODE0
+#define RTT_IRQ             RTC_IRQn
+#define RTT_IRQ_PRIO        10
+#define RTT_ISR             isr_rtc
+#define RTT_MAX_VALUE       (0xffffffff)
+#define RTT_FREQUENCY       (32768U)    /* in Hz. For changes see `rtt.c` */
+#define RTT_RUNSTDBY        (1)         /* Keep RTT running in sleep states */
+/** @} */
+
 #ifdef __cplusplus
 }
 #endif
-- 
GitLab