2 * Copyright (c) 2005, Swedish Institute of Computer Science
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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 $
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
);
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 */
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.
79 if(count
% CLOCK_CONF_SECOND
== 0) {
83 } while((TACCR1
- TAR
) > INTERVAL
);
87 if(etimer_pending() &&
88 (etimer_next_expiration_time() - count
- 1) > MAX_TICKS
) {
89 etimer_request_poll();
94 /* if(process_nevents() >= 0) {
98 ENERGEST_OFF(ENERGEST_TYPE_IRQ
);
100 /*---------------------------------------------------------------------------*/
111 /*---------------------------------------------------------------------------*/
113 clock_set(clock_time_t clock
, clock_time_t fclock
)
116 TACCR1
= fclock
+ INTERVAL
;
119 /*---------------------------------------------------------------------------*/
125 /*---------------------------------------------------------------------------*/
130 /* Assign last_tar to local varible that can not be changed by interrupt */
132 /* perform calc based on t, TAR will not be changed during interrupt */
133 return (unsigned short) (TAR
- t
);
135 /*---------------------------------------------------------------------------*/
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. */
151 /* Interrupt after X ms. */
154 /* Start Timer_A in continuous mode. */
159 /* Enable interrupts. */
163 /*---------------------------------------------------------------------------*/
165 * Delay the CPU for a multiple of 2.83 us.
168 clock_delay(unsigned int i
)
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
182 /*---------------------------------------------------------------------------*/
184 * Wait for a multiple of 10 ms.
192 start
= clock_time();
193 while(clock_time() - start
< (clock_time_t
)i
);
195 /*---------------------------------------------------------------------------*/
197 clock_set_seconds(unsigned long sec
)
201 /*---------------------------------------------------------------------------*/
205 unsigned long t1
, t2
;
212 /*---------------------------------------------------------------------------*/
218 /*---------------------------------------------------------------------------*/