Allow build with original radio driver using $make RF230BB=0
[contiki-2.x.git] / cpu / msp430 / clock.c
blob1f5c79eb5d68073fdf973b09c893b6aea4eb8bb7
1 /*
2 * Copyright (c) 2005, Swedish Institute of Computer Science
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * This file is part of the Contiki operating system.
31 * @(#)$Id: clock.c,v 1.21 2010/01/30 14:03:35 adamdunkels Exp $
35 #include <io.h>
36 #include <signal.h>
38 #include "contiki-conf.h"
40 #include "sys/energest.h"
41 #include "sys/clock.h"
42 #include "sys/etimer.h"
43 #include "rtimer-arch.h"
45 #define INTERVAL (RTIMER_ARCH_SECOND / CLOCK_SECOND)
47 #define MAX_TICKS (~((clock_time_t)0) / 2)
49 static volatile unsigned long seconds;
51 static volatile clock_time_t count = 0;
52 /* last_tar is used for calculating clock_fine, last_ccr might be better? */
53 static unsigned short last_tar = 0;
54 /*---------------------------------------------------------------------------*/
55 interrupt(TIMERA1_VECTOR) timera1 (void) {
56 ENERGEST_ON(ENERGEST_TYPE_IRQ);
58 if(TAIV == 2) {
60 /* HW timer bug fix: Interrupt handler called before TR==CCR.
61 * Occurrs when timer state is toggled between STOP and CONT. */
62 while(TACTL & MC1 && TACCR1 - TAR == 1);
64 /* Make sure interrupt time is future */
65 do {
66 TACCR1 += INTERVAL;
67 ++count;
69 /* Make sure the CLOCK_CONF_SECOND is a power of two, to ensure
70 that the modulo operation below becomes a logical and and not
71 an expensive divide. Algorithm from Wikipedia:
72 http://en.wikipedia.org/wiki/Power_of_two */
73 #if (CLOCK_CONF_SECOND & (CLOCK_CONF_SECOND - 1)) != 0
74 #error CLOCK_CONF_SECOND must be a power of two (i.e., 1, 2, 4, 8, 16, 32, 64, ...).
75 #error Change CLOCK_CONF_SECOND in contiki-conf.h.
76 #endif
77 if(count % CLOCK_CONF_SECOND == 0) {
78 ++seconds;
80 } while((TACCR1 - TAR) > INTERVAL);
82 last_tar = TAR;
84 if(etimer_pending() &&
85 (etimer_next_expiration_time() - count - 1) > MAX_TICKS) {
86 etimer_request_poll();
87 LPM4_EXIT;
91 /* if(process_nevents() >= 0) {
92 LPM4_EXIT;
93 }*/
95 ENERGEST_OFF(ENERGEST_TYPE_IRQ);
97 /*---------------------------------------------------------------------------*/
98 clock_time_t
99 clock_time(void)
101 return count;
103 /*---------------------------------------------------------------------------*/
104 void
105 clock_set(clock_time_t clock, clock_time_t fclock)
107 TAR = fclock;
108 TACCR1 = fclock + INTERVAL;
109 count = clock;
111 /*---------------------------------------------------------------------------*/
113 clock_fine_max(void)
115 return INTERVAL;
117 /*---------------------------------------------------------------------------*/
118 unsigned short
119 clock_fine(void)
121 unsigned short t;
122 /* Assign last_tar to local varible that can not be changed by interrupt */
123 t = last_tar;
124 /* perform calc based on t, TAR will not be changed during interrupt */
125 return (unsigned short) (TAR - t);
127 /*---------------------------------------------------------------------------*/
128 void
129 clock_init(void)
131 dint();
133 /* Select SMCLK (2.4576MHz), clear TAR */
134 /* TACTL = TASSEL1 | TACLR | ID_3; */
136 /* Select ACLK 32768Hz clock, divide by 4 */
137 TACTL = TASSEL0 | TACLR | ID_2;
139 /* Initialize ccr1 to create the X ms interval. */
140 /* CCR1 interrupt enabled, interrupt occurs when timer equals CCR1. */
141 TACCTL1 = CCIE;
143 /* Interrupt after X ms. */
144 TACCR1 = INTERVAL;
146 /* Start Timer_A in continuous mode. */
147 TACTL |= MC1;
149 count = 0;
151 /* Enable interrupts. */
152 eint();
155 /*---------------------------------------------------------------------------*/
157 * Delay the CPU for a multiple of 2.83 us.
159 void
160 clock_delay(unsigned int i)
162 asm("add #-1, r15");
163 asm("jnz $-2");
165 * This means that delay(i) will delay the CPU for CONST + 3x
166 * cycles. On a 2.4756 CPU, this means that each i adds 1.22us of
167 * delay.
169 * do {
170 * --i;
171 * } while(i > 0);
174 /*---------------------------------------------------------------------------*/
176 * Wait for a multiple of 10 ms.
179 void
180 clock_wait(int i)
182 clock_time_t start;
184 start = clock_time();
185 while(clock_time() - start < (clock_time_t)i);
187 /*---------------------------------------------------------------------------*/
188 void
189 clock_set_seconds(unsigned long sec)
193 /*---------------------------------------------------------------------------*/
194 unsigned long
195 clock_seconds(void)
197 unsigned long t1, t2;
198 do {
199 t1 = seconds;
200 t2 = seconds;
201 } while(t1 != t2);
202 return t1;
204 /*---------------------------------------------------------------------------*/
205 rtimer_clock_t
206 clock_counter(void)
208 return TAR;
210 /*---------------------------------------------------------------------------*/