Contiki 2.6

rtimer-arch.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010, Loughborough University - Computer Science
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  */
00032 
00033 /**
00034  * \file
00035  *         Hardware-dependent functions used to support the
00036  *         contiki rtimer module.
00037  *
00038  *         clock_init() has set our tick speed prescaler already, so we
00039  *         are ticking with 500 kHz freq.
00040  *
00041  *         Contiki typedefs rtimer_clock_t as unsigned short (16bit)
00042  *         It thus makes sense to use the 16bit timer (Timer 1)
00043  *
00044  *         This file contains an ISR and must reside in the HOME bank
00045  *
00046  * \author
00047  *         George Oikonomou - <oikonomou@users.sourceforge.net>
00048  */
00049 
00050 #include "sys/rtimer.h"
00051 #include "sfr-bits.h"
00052 #include "cc253x.h"
00053 #include "sys/energest.h"
00054 
00055 #include "debug.h"
00056 #include <stdio.h>
00057 
00058 #define RT_MODE_COMPARE() do { T1CCTL1 |= T1CCTL_MODE; } while(0)
00059 #define RT_MODE_CAPTURE() do { T1CCTL1 &= ~T1CCTL_MODE; } while(0)
00060 /*---------------------------------------------------------------------------*/
00061 void
00062 rtimer_arch_init(void)
00063 {
00064   /*
00065    * - Free running mode
00066    * - Prescale by 32:
00067    *   Tick Speed has been prescaled to 500 kHz already in clock_init()
00068    *   We further prescale by 32 resulting in 15625 Hz for this timer.
00069    */
00070   T1CTL = (T1CTL_DIV1 | T1CTL_MODE0);
00071 
00072   T1STAT = 0;
00073 
00074   /* Timer 1, Channel 1. Compare Mode (0x04), Interrupt mask on (0x40) */
00075   T1CCTL1 = T1CCTL_MODE | T1CCTL_IM;
00076 
00077   /* Interrupt Mask Flags: No interrupt on overflow */
00078   OVFIM = 0;
00079 
00080   /* Acknowledge Timer 1 Interrupts */
00081   T1IE = 1;
00082 }
00083 /*---------------------------------------------------------------------------*/
00084 void
00085 rtimer_arch_schedule(rtimer_clock_t t)
00086 {
00087   /* Switch to capture mode before writing T1CC1x and
00088    * set the compare mode values so we can get an interrupt after t */
00089   RT_MODE_CAPTURE();
00090   T1CC1L = (unsigned char) t;
00091   T1CC1H = (unsigned char) (t >> 8);
00092   RT_MODE_COMPARE();
00093 
00094   /* Turn on compare mode interrupt */
00095   T1STAT = 0;
00096   T1CCTL1 |= T1CCTL_IM;
00097 }
00098 /*---------------------------------------------------------------------------*/
00099 void
00100 rtimer_isr(void) __interrupt(T1_VECTOR)
00101 {
00102   T1IE = 0; /* Ignore Timer 1 Interrupts */
00103   ENERGEST_ON(ENERGEST_TYPE_IRQ);
00104 
00105   /* No more interrupts from Channel 1 till next rtimer_arch_schedule() call */
00106   T1STAT &= ~T1STAT_CH1IF;
00107   T1CCTL1 &= ~T1CCTL_IM;
00108 
00109   rtimer_run_next();
00110 
00111   ENERGEST_OFF(ENERGEST_TYPE_IRQ);
00112   T1IE = 1; /* Acknowledge Timer 1 Interrupts */
00113 }