+/*
+ * Copyright (c) 2007, Technische Universitaet Berlin
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * - Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * - Neither the name of the Technische Universität Berlin nor the names
+ * of its contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
+ * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $Id$
+ *
+ */
+
+ /**
+ * @author Vlado Handziski <handzisk@tkn.tu-berlind.de>
+ * @author Cory Sharp <cssharp@eecs.berkeley.edu>
+ */
+
+module MoteClockP {
+ uses interface Msp430ClockInit;
+}
+
+implementation {
+
+ event void Msp430ClockInit.setupDcoCalibrate()
+ {
+
+ // --- setup ---
+
+ TACTL = TASSEL1 | MC1; // source SMCLK, continuous mode, everything else 0
+ TBCTL = TBSSEL0 | MC1;
+ BCSCTL1 = XT2OFF | RSEL2;
+ BCSCTL2 = DCOR; // enable DCOR
+ TBCCTL0 = CM0;
+ }
+
+ event void Msp430ClockInit.initClocks()
+ {
+ // BCSCTL1
+ // .XT2OFF = 1; disable the external oscillator for SCLK and MCLK
+ // .XTS = 0; set low frequency mode for LXFT1
+ // .DIVA = 0; set the divisor on ACLK to 1
+ // .RSEL, do not modify
+ BCSCTL1 = XT2OFF | (BCSCTL1 & (RSEL2|RSEL1|RSEL0));
+
+ // BCSCTL2
+ // .SELM = 0; select DCOCLK as source for MCLK
+ // .DIVM = 0; set the divisor of MCLK to 1
+ // .SELS = 0; select DCOCLK as source for SCLK
+ // .DIVS = 2; set the divisor of SCLK to 4
+ // .DCOR = 1; select internal resistor for DCO
+ BCSCTL2 = DIVS1 | DCOR;
+
+ // IE1.OFIE = 0; no interrupt for oscillator fault
+ CLR_FLAG( IE1, OFIE );
+ }
+
+ event void Msp430ClockInit.initTimerA()
+ {
+ TAR = 0;
+
+ // TACTL
+ // .TACLGRP = 0; each TACL group latched independently
+ // .CNTL = 0; 16-bit counter
+ // .TASSEL = 2; source SMCLK = DCO/4
+ // .ID = 0; input divisor of 1
+ // .MC = 0; initially disabled
+ // .TACLR = 0; reset timer A
+ // .TAIE = 1; enable timer A interrupts
+ TACTL = TASSEL1 | TAIE;
+ }
+
+ event void Msp430ClockInit.initTimerB()
+ {
+ TBR = 0;
+
+ // TBCTL
+ // .TBCLGRP = 0; each TBCL group latched independently
+ // .CNTL = 0; 16-bit counter
+ // .TBSSEL = 1; source ACLK
+ // .ID = 0; input divisor of 1
+ // .MC = 0; initially disabled
+ // .TBCLR = 0; reset timer B
+ // .TBIE = 1; enable timer B interrupts
+ TBCTL = TBSSEL0 | TBIE;
+ }
+
+}