/* * Copyright (c) 2007, Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the Institute nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * This file is part of the Contiki operating system. * */ /** * \file * AVR-specific rtimer code * Defaults to Timer3 for those ATMEGAs that have it. * If Timer3 not present Timer1 will be used. * \author * Fredrik Osterlind * Joakim Eriksson */ /* OBS: 8 seconds maximum time! */ #include #include #include #include "sys/energest.h" #include "sys/rtimer.h" #include "rtimer-arch.h" #if defined(__AVR_ATmega1284P__) #define ETIMSK TIMSK3 #define ETIFR TIFR3 #define TICIE3 ICIE3 //Has no 'C', so we just set it to B. The code doesn't really use C so this //is safe to do but lets it compile. Probably should enable the warning if //it is ever used on other platforms. //#warning no OCIE3C in timer3 architecture, hopefully it won't be needed! #define OCIE3C OCIE3B #define OCF3C OCF3B #endif #if defined(__AVR_ATmega1281__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega128RFA1__) #define ETIMSK TIMSK3 #define ETIFR TIFR3 #define TICIE3 ICIE3 #endif #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega644__) #define TIMSK TIMSK1 #define TICIE1 ICIE1 #define TIFR TIFR1 #endif /* Track flow through rtimer interrupts*/ #if DEBUGFLOWSIZE&&0 extern uint8_t debugflowsize,debugflow[DEBUGFLOWSIZE]; #define DEBUGFLOW(c) if (debugflowsize<(DEBUGFLOWSIZE-1)) debugflow[debugflowsize++]=c #else #define DEBUGFLOW(c) #endif /*---------------------------------------------------------------------------*/ #if defined(TCNT3) && RTIMER_ARCH_PRESCALER ISR (TIMER3_COMPA_vect) { DEBUGFLOW('/'); ENERGEST_ON(ENERGEST_TYPE_IRQ); /* Disable rtimer interrupts */ ETIMSK &= ~((1 << OCIE3A) | (1 << OCIE3B) | (1 << TOIE3) | (1 << TICIE3) | (1 << OCIE3C)); #if RTIMER_CONF_NESTED_INTERRUPTS /* Enable nested interrupts. Allows radio interrupt during rtimer interrupt. */ /* All interrupts are enabled including recursive rtimer, so use with caution */ sei(); #endif /* Call rtimer callback */ rtimer_run_next(); ENERGEST_OFF(ENERGEST_TYPE_IRQ); DEBUGFLOW('\\'); } #elif RTIMER_ARCH_PRESCALER #warning "No Timer3 in rtimer-arch.c - using Timer1 instead" ISR (TIMER1_COMPA_vect) { DEBUGFLOW('/'); TIMSK &= ~((1< #include uint32_t longhowlong; #if AVR_CONF_USE32KCRYSTAL /* Save TIMER2 configuration if clock.c is using it */ uint8_t savedTCNT2=TCNT2, savedTCCR2A=TCCR2A, savedTCCR2B = TCCR2B, savedOCR2A = OCR2A; #endif cli(); watchdog_stop(); set_sleep_mode(SLEEP_MODE_PWR_SAVE); /* Set TIMER2 clock asynchronus from external source, CTC mode */ ASSR |= (1 << AS2); TCCR2A =(1<