Add explicit queuebuf and packetbuf to build
[contiki-2.x.git] / cpu / msp430 / clock.c
blobd4a9141a7303b50062e6355111e1792887730a41
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.25 2010/04/04 12:29:50 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 /* TACTL &= ~MC1;*/
67 TACCR1 += INTERVAL;
68 /* TACTL |= MC1;*/
69 ++count;
71 /* Make sure the CLOCK_CONF_SECOND is a power of two, to ensure
72 that the modulo operation below becomes a logical and and not
73 an expensive divide. Algorithm from Wikipedia:
74 http://en.wikipedia.org/wiki/Power_of_two */
75 #if (CLOCK_CONF_SECOND & (CLOCK_CONF_SECOND - 1)) != 0
76 #error CLOCK_CONF_SECOND must be a power of two (i.e., 1, 2, 4, 8, 16, 32, 64, ...).
77 #error Change CLOCK_CONF_SECOND in contiki-conf.h.
78 #endif
79 if(count % CLOCK_CONF_SECOND == 0) {
80 ++seconds;
81 energest_flush();
83 } while((TACCR1 - TAR) > INTERVAL);
85 last_tar = TAR;
87 if(etimer_pending() &&
88 (etimer_next_expiration_time() - count - 1) > MAX_TICKS) {
89 etimer_request_poll();
90 LPM4_EXIT;
94 /* if(process_nevents() >= 0) {
95 LPM4_EXIT;
96 }*/
98 ENERGEST_OFF(ENERGEST_TYPE_IRQ);
100 /*---------------------------------------------------------------------------*/
101 clock_time_t
102 clock_time(void)
104 clock_time_t t1, t2;
105 do {
106 t1 = count;
107 t2 = count;
108 } while(t1 != t2);
109 return t1;
111 /*---------------------------------------------------------------------------*/
112 void
113 clock_set(clock_time_t clock, clock_time_t fclock)
115 TAR = fclock;
116 TACCR1 = fclock + INTERVAL;
117 count = clock;
119 /*---------------------------------------------------------------------------*/
121 clock_fine_max(void)
123 return INTERVAL;
125 /*---------------------------------------------------------------------------*/
126 unsigned short
127 clock_fine(void)
129 unsigned short t;
130 /* Assign last_tar to local varible that can not be changed by interrupt */
131 t = last_tar;
132 /* perform calc based on t, TAR will not be changed during interrupt */
133 return (unsigned short) (TAR - t);
135 /*---------------------------------------------------------------------------*/
136 void
137 clock_init(void)
139 dint();
141 /* Select SMCLK (2.4576MHz), clear TAR */
142 /* TACTL = TASSEL1 | TACLR | ID_3; */
144 /* Select ACLK 32768Hz clock, divide by 2 */
145 TACTL = TASSEL0 | TACLR | ID_1;
147 /* Initialize ccr1 to create the X ms interval. */
148 /* CCR1 interrupt enabled, interrupt occurs when timer equals CCR1. */
149 TACCTL1 = CCIE;
151 /* Interrupt after X ms. */
152 TACCR1 = INTERVAL;
154 /* Start Timer_A in continuous mode. */
155 TACTL |= MC1;
157 count = 0;
159 /* Enable interrupts. */
160 eint();
163 /*---------------------------------------------------------------------------*/
165 * Delay the CPU for a multiple of 2.83 us.
167 void
168 clock_delay(unsigned int i)
170 asm("add #-1, r15");
171 asm("jnz $-2");
173 * This means that delay(i) will delay the CPU for CONST + 3x
174 * cycles. On a 2.4756 CPU, this means that each i adds 1.22us of
175 * delay.
177 * do {
178 * --i;
179 * } while(i > 0);
182 /*---------------------------------------------------------------------------*/
184 * Wait for a multiple of 10 ms.
187 void
188 clock_wait(int i)
190 clock_time_t start;
192 start = clock_time();
193 while(clock_time() - start < (clock_time_t)i);
195 /*---------------------------------------------------------------------------*/
196 void
197 clock_set_seconds(unsigned long sec)
201 /*---------------------------------------------------------------------------*/
202 unsigned long
203 clock_seconds(void)
205 unsigned long t1, t2;
206 do {
207 t1 = seconds;
208 t2 = seconds;
209 } while(t1 != t2);
210 return t1;
212 /*---------------------------------------------------------------------------*/
213 rtimer_clock_t
214 clock_counter(void)
216 return TAR;
218 /*---------------------------------------------------------------------------*/