Contiki 2.5
clock.c
1 /*
2  * Copyright (c) 2010, Mariano Alvira <mar@devl.org> and other contributors
3  * to the MC1322x project (http://mc1322x.devl.org) and Contiki.
4  *
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the Institute nor the names of its contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * This file is part of the Contiki OS.
32  *
33  *
34  */
35 
36 #include <sys/clock.h>
37 #include <sys/cc.h>
38 #include <sys/etimer.h>
39 #include "dev/leds.h"
40 
41 #include "contiki-conf.h"
42 #include "mc1322x.h"
43 
44 #include "contiki-conf.h"
45 
46 #define MAX_TICKS (~((clock_time_t)0) / 2)
47 
48 static volatile clock_time_t current_clock = 0;
49 
50 volatile unsigned long seconds = 0;
51 
52 #define TCF 15
53 #define TCF1 4
54 #define TCF2 5
55 
56 void
58 {
59  /* timer setup */
60  /* CTRL */
61 #define COUNT_MODE 1 /* use rising edge of primary source */
62 #define PRIME_SRC 0xf /* Perip. clock with 128 prescale (for 24Mhz = 187500Hz)*/
63 #define SEC_SRC 0 /* don't need this */
64 #define ONCE 0 /* keep counting */
65 #define LEN 1 /* count until compare then reload with value in LOAD */
66 #define DIR 0 /* count up */
67 #define CO_INIT 0 /* other counters cannot force a re-initialization of this counter */
68 #define OUT_MODE 0 /* OFLAG is asserted while counter is active */
69 
70  *TMR_ENBL = 0; /* tmrs reset to enabled */
71  *TMR0_SCTRL = 0;
72  *TMR0_CSCTRL =0x0040;
73  *TMR0_LOAD = 0; /* reload to zero */
74  *TMR0_COMP_UP = 1875; /* trigger a reload at the end */
75  *TMR0_CMPLD1 = 1875; /* compare 1 triggered reload level, 10HZ maybe? */
76  *TMR0_CNTR = 0; /* reset count register */
77  *TMR0_CTRL = (COUNT_MODE<<13) | (PRIME_SRC<<9) | (SEC_SRC<<7) | (ONCE<<6) | (LEN<<5) | (DIR<<4) | (CO_INIT<<3) | (OUT_MODE);
78  *TMR_ENBL = 0xf; /* enable all the timers --- why not? */
79 
80  enable_irq(TMR);
81 
82 }
83 
84 void tmr0_isr(void) {
85  if(bit_is_set(*TMR(0,CSCTRL),TCF1)) {
86  current_clock++;
87  if((current_clock % CLOCK_CONF_SECOND) == 0) {
88  seconds++;
89 #if BLINK_SECONDS
90  leds_toggle(LEDS_GREEN);
91 #endif
92  }
93 
94  if(etimer_pending() &&
95  (etimer_next_expiration_time() - current_clock - 1) > MAX_TICKS) {
97  }
98 
99 
100  /* clear the compare flags */
101  clear_bit(*TMR(0,SCTRL),TCF);
102  clear_bit(*TMR(0,CSCTRL),TCF1);
103  clear_bit(*TMR(0,CSCTRL),TCF2);
104  return;
105  } else {
106  /* this timer didn't create an interrupt condition */
107  return;
108  }
109 }
110 
111 clock_time_t
113 {
114  return current_clock;
115 }
116 
117 unsigned long
118 clock_seconds(void)
119 {
120  return seconds;
121 }
122 
123 /* clock delay from cc2430 */
124 /* I don't see any documentation about how this routine is suppose to behave */
125 void
126 clock_delay(unsigned int len)
127 {
128  unsigned int i;
129  for(i = 0; i< len; i++) {
130  asm("nop");
131  }
132 }