add rtimers for cxmac
[contiki-2.x.git] / cpu / avr / rtimer-arch.c
blob0898248eb4fc67cf8ca0eaacde6534300912d929
1 /*
2 * Copyright (c) 2007, 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: rtimer-arch.c,v 1.7 2010/02/18 17:21:44 dak664 Exp $
34 /**
35 * \file
36 * AVR-specific rtimer code
37 * Currently only works on ATMEGAs that have Timer 3.
38 * \author
39 * Fredrik Osterlind <fros@sics.se>
40 * Joakim Eriksson <joakime@sics.se>
43 /* OBS: 8 seconds maximum time! */
45 #include <avr/io.h>
46 #include <avr/interrupt.h>
47 #include <stdio.h>
49 #include "sys/energest.h"
50 #include "sys/rtimer.h"
51 #include "rtimer-arch.h"
53 #if defined(__AVR_ATmega1281__) || defined(__AVR_ATmega1284P__)
54 //#error FTH081029 test timer 3
55 #define ETIMSK TIMSK3
56 #define ETIFR TIFR3
57 #define TICIE3 ICIE3
59 //Has no 'C', so we just set it to B. The code doesn't really use C so this
60 //is safe to do but lets it compile
61 #warning No OCIE3C in architecture, hopefully it will not be needed
62 #define OCIE3C OCIE3B
63 #define OCF3C OCF3B
64 #endif
66 #if defined(__AVR_AT90USB1287__)
67 #warning AT90USB1287 rtimers not tested
68 #define ETIMSK TIMSK3
69 #define ETIFR TIFR3
70 #define TICIE3 ICIE3
71 #endif
72 uint8_t rtimerworks;
73 /*---------------------------------------------------------------------------*/
74 #ifdef TCNT3
75 ISR (TIMER3_COMPA_vect) {
76 ENERGEST_ON(ENERGEST_TYPE_IRQ);
78 ETIMSK &= ~((1 << OCIE3A) | (1 << OCIE3B) | (1 << TOIE3) |
79 (1 << TICIE3) | (1 << OCIE3C));
80 rtimerworks++;
81 /* Call rtimer callback */
82 rtimer_run_next();
84 ENERGEST_OFF(ENERGEST_TYPE_IRQ);
87 #else
88 #error "No Timer3 in rtimer-arch.c"
90 #endif
91 /*---------------------------------------------------------------------------*/
92 void
93 rtimer_arch_init(void)
95 /* Disable interrupts (store old state) */
96 uint8_t sreg;
97 sreg = SREG;
98 cli ();
100 #ifdef TCNT3
101 rtimerworks=240;
103 ETIMSK &= ~((1 << OCIE3A) | (1 << OCIE3B) | (1 << TOIE3) |
104 (1 << TICIE3) | (1 << OCIE3C));
105 ETIFR |= (1 << ICF3) | (1 << OCF3A) | (1 << OCF3B) | (1 << TOV3) |
106 (1 << OCF3C);
108 /* Default timer behaviour */
109 TCCR3A = 0;
110 TCCR3B = 0;
111 TCCR3C = 0;
113 /* Reset counter */
114 TCNT3 = 0;
116 /* Maximum prescaler */
117 TCCR3B |= 5;
119 #else
120 #error "No Timer3 in rtimer-arch.c"
122 #endif
124 /* Restore interrupt state */
125 SREG = sreg;
127 /*---------------------------------------------------------------------------*/
128 void
129 rtimer_arch_schedule(rtimer_clock_t t)
131 /* Disable interrupts (store old state) */
132 uint8_t sreg;
133 sreg = SREG;
134 cli ();
136 #ifdef TCNT3
137 rtimerworks=250;
138 /* Set compare register */
139 OCR3A = t;
140 ETIFR |= (1 << ICF3) | (1 << OCF3A) | (1 << OCF3B) | (1 << TOV3) |
141 (1 << OCF3C);
142 ETIMSK |= (1 << OCIE3A);
144 #else
145 #error "No Timer3 in rtimer-arch.c"
147 #endif
149 /* Restore interrupt state */
150 SREG = sreg;
151 printf("rs%d",t);