Contiki 2.5
clock.c
1 /*
2  * Copyright (c) 2009, University of Colombo School of Computing
3  * All rights reserved.
4  *
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.
16  *
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.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * @(#)$$
32  */
33 
34 #include "sys/clock.h"
35 #include "sys/etimer.h"
36 
37 #include <avr/io.h>
38 #include <avr/interrupt.h>
39 
40 static volatile clock_time_t count, scount;
41 static volatile unsigned long seconds;
42 
43 /*---------------------------------------------------------------------------*/
44 ISR(TIMER0_COMP_vect)
45 {
46  count++;
47  if(++scount == CLOCK_SECOND) {
48  scount = 0;
49  seconds++;
50  }
51  if(etimer_pending()) {
53  }
54 }
55 
56 /*---------------------------------------------------------------------------*/
57 void
59 {
60  /* Disable interrupts*/
61  cli();
62 
63  /* Disable compare match interrupts and overflow interrupts. */
64  TIMSK &= ~( _BV(TOIE0) | _BV(OCIE0) );
65 
66  /**
67  * set Timer/Counter0 to be asynchronous
68  * from the CPU clock with a second external
69  * clock(32,768kHz) driving it.
70  */
71  ASSR |= _BV(AS0);
72 
73  /*
74  * Set timer control register:
75  * - prescale: 32 (CS00 and CS01)
76  * - counter reset via comparison register (WGM01)
77  */
78  TCCR0 = _BV(CS00) | _BV(CS01) | _BV(WGM01);
79 
80  /* Set counter to zero */
81  TCNT0 = 0;
82 
83  /*
84  * 128 clock ticks per second.
85  * 32,768 = 32 * 8 * 128
86  */
87  OCR0 = 8;
88 
89  /* Clear interrupt flag register */
90  TIFR = 0x00;
91 
92  /**
93  * Wait for TCN0UB, OCR0UB, and TCR0UB.
94  *
95  */
96  while(ASSR & 0x07);
97 
98  /* Raise interrupt when value in OCR0 is reached. */
99  TIMSK |= _BV(OCIE0);
100 
101  count = 0;
102 
103  /* enable all interrupts*/
104  sei();
105 
106 }
107 /*---------------------------------------------------------------------------*/
108 clock_time_t
110 {
111  clock_time_t tmp;
112  do {
113  tmp = count;
114  } while(tmp != count);
115  return tmp;
116 }
117 /*---------------------------------------------------------------------------*/
118 /**
119  * Delay the CPU for a multiple of TODO
120  */
121 void
122 clock_delay(unsigned int i)
123 {
124  for (; i > 0; i--) { /* Needs fixing XXX */
125  unsigned j;
126  for (j = 50; j > 0; j--)
127  asm volatile("nop");
128  }
129 }
130 
131 /*---------------------------------------------------------------------------*/
132 /**
133  * Wait for a multiple of 1 / 128 sec = 7.8125 ms.
134  *
135  */
136 void
137 clock_wait(int i)
138 {
139  clock_time_t start;
140 
141  start = clock_time();
142  while(clock_time() - start < (clock_time_t)i);
143 }
144 /*---------------------------------------------------------------------------*/
145 void
146 clock_set_seconds(unsigned long sec)
147 {
148  // TODO
149 }
150 /*---------------------------------------------------------------------------*/
151 unsigned long
152 clock_seconds(void)
153 {
154  unsigned long tmp;
155  do {
156  tmp = seconds;
157  } while(tmp != seconds);
158  return tmp;
159 }
160 /*---------------------------------------------------------------------------*/