From 1ada39d7fdcd5a8cb4e1a47466891a790f5f032c Mon Sep 17 00:00:00 2001
From: Oleg Hahm <oleg@hobbykeller.org>
Date: Mon, 19 Aug 2013 18:58:45 +0200
Subject: [PATCH] initial commit for TelosB support

---
 telosb/Makefile             |  33 ++++++++++
 telosb/Makefile.dep         |   2 +
 telosb/Makefile.include     |  25 +++++++
 telosb/board.c              | 127 ++++++++++++++++++++++++++++++++++++
 telosb/include/board-conf.h |  13 ++++
 telosb/include/board.h      |  58 ++++++++++++++++
 telosb/uart.c               |  45 +++++++++++++
 7 files changed, 303 insertions(+)
 create mode 100644 telosb/Makefile
 create mode 100644 telosb/Makefile.dep
 create mode 100644 telosb/Makefile.include
 create mode 100644 telosb/board.c
 create mode 100644 telosb/include/board-conf.h
 create mode 100644 telosb/include/board.h
 create mode 100644 telosb/uart.c

diff --git a/telosb/Makefile b/telosb/Makefile
new file mode 100644
index 0000000000..1f6fc07cb0
--- /dev/null
+++ b/telosb/Makefile
@@ -0,0 +1,33 @@
+SRC = $(wildcard *.c)
+BINDIR = bin/
+OBJ = $(SRC:%.c=$(BINDIR)%.o)## defines
+export ARCH = telosb_base.a
+
+DEP = $(SRC:%.c=$(BINDIR)%.d)
+
+INCLUDES += -I${RIOTBASE}/core/include/
+INCLUDES += -I$(RIOTBASE)/cpu/msp430-common/include/ -I$(RIOTBASE)/cpu/msp430x16x/include/
+INCLUDES += -I$(RIOTBASE)/drivers/cc2420/include/ -I$(RIOTBASE)/sys/include
+INCLUDES += -I$(RIOTBASE)/sys/net/
+
+all: $(BINDIR)$(ARCH)
+
+$(BINDIR)$(ARCH): $(OBJ)
+	$(AR) rcs $(BINDIR)$(ARCH) $(OBJ)
+
+# pull in dependency info for *existing* .o files
+-include $(OBJ:.o=.d)
+
+# compile and generate dependency info
+$(BINDIR)%.o: %.c
+	mkdir -p $(BINDIR)
+	$(CC) $(CFLAGS) $(INCLUDES) $(BOARDINCLUDE) $(PROJECTINCLUDE) $(CPUINCLUDE) -c $*.c -o $(BINDIR)$*.o
+	$(CC) $(CFLAGS) $(INCLUDES) $(BOARDINCLUDE) $(PROJECTINCLUDE) $(CPUINCLUDE) -MM $*.c > $(BINDIR)$*.d
+	@printf "$(BINDIR)"|cat - $(BINDIR)$*.d > /tmp/riot_out && mv /tmp/riot_out $(BINDIR)$*.d
+
+# remove compilation products
+clean:
+	rm -f $(BINDIR)$(ARCH) $(OBJ) $(DEP)
+	@if [ -d $(BINDIR) ] ; \
+	then rmdir $(BINDIR) ; \
+	fi
diff --git a/telosb/Makefile.dep b/telosb/Makefile.dep
new file mode 100644
index 0000000000..4392a18adb
--- /dev/null
+++ b/telosb/Makefile.dep
@@ -0,0 +1,2 @@
+USEMODULE += msp430_common
+
diff --git a/telosb/Makefile.include b/telosb/Makefile.include
new file mode 100644
index 0000000000..e07ce0f5da
--- /dev/null
+++ b/telosb/Makefile.include
@@ -0,0 +1,25 @@
+include $(RIOTBOARD)/$(BOARD)/Makefile.dep
+## the cpu to build for
+export CPU = msp430x16x
+export MCU = msp430f1611
+
+# toolchain config
+export PREFIX = @msp430-
+export CC = @$(PREFIX)gcc
+export AR = @$(PREFIX)ar
+export CFLAGS += -std=gnu99 -Wstrict-prototypes -gdwarf-2 -Os -Wall -mmcu=$(MCU)
+export ASFLAGS += -mmcu=$(MCU) --defsym $(MCU)=1 --gdwarf-2
+export AS = $(PREFIX)as
+export LINK = $(PREFIX)gcc
+export SIZE = $(PREFIX)size
+export OBJCOPY = $(PREFIX)objcopy
+export LINKFLAGS = -mmcu=$(MCU) -lgcc $(RIOTBASE)/bin/startup.o
+export TERMPROG = $(RIOTBASE)/dist/tools/pyterm/pyterm.py
+export FLASHER = goodfet.bsl
+ifeq ($(strip $(PORT)),)
+    export PORT = /dev/ttyUSB0
+endif
+export HEXFILE = bin/$(PROJECT).hex
+export FFLAGS = --telosb -c $(PORT) -r -e -I -p $(HEXFILE)
+
+export INCLUDES += -I $(RIOTCPU)/msp430-common/include/
diff --git a/telosb/board.c b/telosb/board.c
new file mode 100644
index 0000000000..d461e3b4c1
--- /dev/null
+++ b/telosb/board.c
@@ -0,0 +1,127 @@
+  /*
+   * board.c - Board initiazilation for the TelosB
+   * Copyright (C) 2013 Oliver Hahm <oliver.hahm@inria.fr>
+   *
+   * This file subject to the terms and conditions of the GLGPLv2 License. See the file LICENSE in the  
+   * top level directory for more details.
+   */
+
+#include "cpu.h"
+#include "board.h"
+
+void uart_init(void);
+
+static void telosb_ports_init(void)
+{
+    /* Port 1: GDO, Flash, BSL TX */
+    P1SEL = 0x02;    /* Port1 Select: 00000010 = 0x02 */
+    P1OUT = 0x00;    /* Port1 Output: 00000000 = 0x00 */
+    P1DIR = 0x87;    /* Port1 Direction: 10000111 = 0x87 */
+
+    /* Port 2: GPIO, BSL RX, 1wire */
+    P2SEL = 0x04;    /* Port2 Select: 00000100 = 0x04 */
+    P2OUT = 0x00;    /* Port2 Output: 00000000 = 0x00 */
+    P2DIR = 0xFF;    /* Port2 Direction: 11111111 = 0xFF */
+
+    /* Port 3: UART and SPI */
+    P3SEL = 0xCE;    /* Port3 Select: 11001110 = 0xCE */
+    P3OUT = 0x00;    /* Port3 Output: 00000000 = 0x00 */
+    P3DIR = 0x4E;    /* Port3 Direction: 01001110 = 0x4E */
+
+    /* Port 4: CS */
+    P4SEL = 0x02;    /* Port4 Select: 00000010 = 0x02 */
+    P4OUT = 0x04;    /* Port4 Output: 00000100 = 0x04 */
+    P4DIR = 0x64;    /* Port4 Direction: 01100100 = 0x64 */
+
+    /* Port 5: SPI, LED */
+    P5SEL = 0x00;    /* Port5 Select: 00000000 = 0x00 */
+    P5OUT = 0x70;    /* Port5 Output: 01110000 = 0x70 */
+    P5DIR = 0x70;    /* Port5 Direction: 01110000 = 0x70 */
+
+
+    P6SEL = 0xFF;    /* Port6 Select: 11111111 = 0xFF */
+    P6OUT = 0x00;    /* Port6 Output: 00000000 = 0x00 */
+    P6DIR = 0xFF;    /* Port6 Direction: 11111000 = 0xFF */
+
+}
+
+/*---------------------------------------------------------------------------*/
+/* taken from Contiki code */
+void msp430_init_dco(void)
+{
+    /* This code taken from the FU Berlin sources and reformatted. */
+#define DELTA    (F_CPU / (F_RC_OSCILLATOR / 8))
+
+    unsigned int compare, oldcapture = 0;
+    unsigned int i;
+
+    /* 10100100 = XT2 is off, ACLK divided by 4, RSELx=4 */
+    BCSCTL1 = XT2OFF | DIVA_2 | RSEL2;
+
+    /* Init undivided DCO with internal resistor for MCLK and SMCLK
+     * DCO = 32762Hz -> FLL = 2,4576 MHz */
+    BCSCTL2 = 0x00;
+
+    BCSCTL1 |= DIVA1 + DIVA0;             /* ACLK = LFXT1CLK/8 */
+
+    for (i = 0xFFFF; i > 0; i--) {        /* Delay for XTAL to settle */
+        asm("nop");
+    }
+
+    CCTL2 = CCIS0 + CM0 + CAP;            /* Define CCR2, CAP, ACLK */
+    TACTL = TASSEL1 + TACLR + MC1;        /* SMCLK, continous mode */
+
+    while (1) {
+        while ((CCTL2 & CCIFG) != CCIFG);   /* Wait until capture occured!*/
+
+        CCTL2 &= ~CCIFG;                    /* Capture occured, clear flag */
+        compare = CCR2;                     /* Get current captured SMCLK */
+        compare = compare - oldcapture;     /* SMCLK difference */
+        oldcapture = CCR2;                  /* Save current captured SMCLK */
+
+        if (DELTA == compare) {
+            break;                            /* if equal, leave "while (1)" */
+        }
+        else if (DELTA < compare) {        /* DCO is too fast, slow it down */
+            DCOCTL--;
+
+            if (DCOCTL == 0xFF) {             /* Did DCO role under? */
+                BCSCTL1--;
+            }
+        }
+        else {                            /* -> Select next lower RSEL */
+            DCOCTL++;
+
+            if (DCOCTL == 0x00) {             /* Did DCO role over? */
+                BCSCTL1++;
+            }
+
+            /* -> Select next higher RSEL  */
+        }
+    }
+
+    CCTL2 = 0;                            /* Stop CCR2 function */
+    TACTL = 0;                            /* Stop Timer_A */
+
+    BCSCTL1 &= ~(DIVA1 + DIVA0);          /* remove /8 divisor from ACLK again */
+}
+
+
+//=========================== public ==========================================
+
+void board_init(void)
+{
+    msp430_cpu_init();
+    /* disable watchdog timer */
+    WDTCTL     =  WDTPW + WDTHOLD;
+
+    telosb_ports_init();
+
+    msp430_init_dco();
+
+    /* initialize bsp modules */
+    uart_init();
+
+    /* enable interrupts */
+    __bis_SR_register(GIE);
+}
diff --git a/telosb/include/board-conf.h b/telosb/include/board-conf.h
new file mode 100644
index 0000000000..f65074da9d
--- /dev/null
+++ b/telosb/include/board-conf.h
@@ -0,0 +1,13 @@
+/**
+  * board-conf.h.
+  *
+  * This source code is licensed under the GNU Lesser General Public License,
+  * Version 2.  See the file LICENSE for more details.
+  */
+
+#ifndef BOARD_CONF_H
+#define BOARD_CONF_H
+
+#define INFOMEM     (0x1000)
+
+#endif /* BOARD-CONF_H */
diff --git a/telosb/include/board.h b/telosb/include/board.h
new file mode 100644
index 0000000000..d9ab4a6675
--- /dev/null
+++ b/telosb/include/board.h
@@ -0,0 +1,58 @@
+/**
+  * board.h - TelosB Board.
+  * Copyright (C) 2013 INRIA
+  *
+  * This source code is licensed under the GNU Lesser General Public License,
+  * Version 2.  See the file LICENSE for more details.
+  */
+
+#ifndef _TELOSB_BOARD_H
+#define _TELOSB_BOARD_H
+
+/**
+ * @defgroup    TelosB
+ * @ingroup		TelosB
+ *
+<h2>Compontents</h2>
+\li MSP430
+\li CC2420
+
+* @{
+*/
+
+/**
+ * @file
+ * @brief		TelosB Board
+ *
+ * @author      Oliver Hahm <oliver.hahm@inria.fr>
+ *
+ */
+
+//MSB430 core
+#define MSP430_INITIAL_CPU_SPEED    800000uL
+#define MSP430_HAS_DCOR             0
+#define MSP430_HAS_EXTERNAL_CRYSTAL 1
+
+/* LEDs ports MSB430 */
+#define LEDS_PxDIR P5DIR
+#define LEDS_PxOUT P5OUT
+#define LEDS_CONF_RED		0x10
+#define LEDS_CONF_GREEN		0x20
+#define LEDS_CONF_BLUE	0x40
+
+#define LED_RED_ON      LEDS_PxOUT &=~LEDS_CONF_RED
+#define LED_RED_OFF     LEDS_PxOUT |= LEDS_CONF_RED
+#define LED_RED_TOGGLE     LEDS_PxOUT ^= LEDS_CONF_RED
+
+#define LED_GREEN_ON      LEDS_PxOUT &=~LEDS_CONF_GREEN
+#define LED_GREEN_OFF     LEDS_PxOUT |= LEDS_CONF_GREEN
+#define LED_GREEN_TOGGLE     LEDS_PxOUT ^= LEDS_CONF_GREEN
+
+#define LED_BLUE_ON      LEDS_PxOUT &=~LEDS_CONF_BLUE
+#define LED_BLUE_OFF     LEDS_PxOUT |= LEDS_CONF_BLUE
+#define LED_BLUE_TOGGLE     LEDS_PxOUT ^= LEDS_CONF_BLUE
+
+#include <msp430x16x.h>
+
+/** @} */
+#endif // _TELOSB_BOARD_H
diff --git a/telosb/uart.c b/telosb/uart.c
new file mode 100644
index 0000000000..b8cdcdcd76
--- /dev/null
+++ b/telosb/uart.c
@@ -0,0 +1,45 @@
+  /*
+   * uart.c - Implementation for the TelosB UART
+   * Copyright (C) 2013 Oliver Hahm <oliver.hahm@inria.fr>
+   *
+   * This file subject to the terms and conditions of the GLGPLv2 License. See the file LICENSE in the  
+   * top level directory for more details.
+   */
+
+#include <stdio.h>
+#include <stdint.h>
+#include "cpu.h"
+#include "board.h"
+#include "kernel.h"
+
+#define   UART1_TX                  U1TXBUF
+#define   UART1_WAIT_TXDONE()       while ( (U1TCTL & TXEPT) == 0 ) { _NOP(); }
+
+#define BAUDRATE    (115200ul)
+
+void uart_init(void)
+{
+    UCTL1     =  SWRST;           /* hold UART1 module in reset */
+    UCTL1    |=  CHAR;            /* 8-bit character */
+
+    /* 115200 baud, clocked from 4.8MHz SMCLK */
+    UTCTL1   |= SSEL1;        /* UCLK = SCLK */
+    UBR01     = F_CPU / BAUDRATE;
+    UBR11     = (F_CPU / BAUDRATE) >> 8;
+    UMCTL1    =  0x4A;            /* modulation */
+
+    ME2      |=  UTXE1 + URXE1;   /* enable UART1 TX/RX */
+    UCTL1    &= ~SWRST;           /* clear UART1 reset bit */
+}
+
+int putchar(int c)
+{
+    UART1_TX = c;
+    UART1_WAIT_TXDONE();
+    return c;
+}
+
+uint8_t uart_readByte(void)
+{
+    return U1RXBUF;
+}
-- 
GitLab