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 and etimer are using the sleep timer on the cc2430
00039  *
00040  *         clock_init() has set our tick speed prescaler already, so we
00041  *         are ticking with 500 kHz freq.
00042  *
00043  *         rtimer_clock_t is unsigned short (16bit on the cc2430)
00044  *         It thus makes sense to use the 16bit clock (Timer 1)
00045  *
00046  *         This file contains an ISR and must reside in the HOME bank
00047  *
00048  * \author
00049  *         George Oikonomou - <oikonomou@users.sourceforge.net>
00050  */
00051 
00052 #include "sys/rtimer.h" /* Includes rtimer-arch.h for us */
00053 #include "cc2430_sfr.h"
00054 #include "sys/energest.h"
00055 
00056 #define DEBUG 0
00057 #if DEBUG
00058 #include <stdio.h>
00059 #define PRINTF(...) printf(__VA_ARGS__)
00060 #else
00061 #define PRINTF(...)
00062 #endif
00063 
00064 /*---------------------------------------------------------------------------*/
00065 void
00066 rtimer_arch_init(void)
00067 {
00068   PRINTF("rtimer_arch_init() ");
00069   /*
00070    * - Free running mode
00071    * - Prescale by 32:
00072    *   Tick Speed has been prescaled to 500 kHz already in clock_init()
00073    *   We further prescale by 32 resulting in 15625 Hz for this timer.
00074    */
00075   T1CTL = (T1DIV1 | T1MODE0); /* 00001001 */
00076   PRINTF("T1CTL=0x%02x\n", T1CTL);
00077   /* Acknowledge Timer 1 Interrupts */
00078   IEN1_T1IE = 1;
00079   PRINTF("IEN1_T1IE=0x%02x\n", IEN1_T1IE);
00080 
00081   /* Timer 1, Channel 1. Compare Mode (0x04), Interrupt mask on (0x40) */
00082   T1CCTL1 = T1MODE + T1IM;
00083   PRINTF("T1CCTL1=0x%02x\n", T1CCTL1);
00084 
00085   /* Interrupt Mask Flags: No interrupt on overflow */
00086   TIMIF &= ~OVFIM;
00087   PRINTF("TIMIF=0x%02x\n", TIMIF);
00088 
00089   PRINTF("done\n");
00090 }
00091 /*---------------------------------------------------------------------------*/
00092 void
00093 rtimer_arch_schedule(rtimer_clock_t t)
00094 {
00095   PRINTF("rtimer_arch_schedule(%u)\n", t);
00096 
00097   /* set the compare mode values so we can get an interrupt after t */
00098   T1CC1L = (unsigned char) t;
00099   T1CC1H = (unsigned char) (t >> 8);
00100   PRINTF("T1CC1=%u, t=%u\n", (T1CC1L + (T1CC1H << 8)), t);
00101 
00102   /* Turn on compare mode interrupt */
00103   PRINTF("T1CTL=0x%02x\n", T1CTL);
00104   T1CCTL1 |= T1IM;
00105 }
00106 /*---------------------------------------------------------------------------*/
00107 void
00108 cc2430_timer_1_ISR(void) __interrupt (T1_VECTOR)
00109 {
00110   IEN1_T1IE = 0; /* Ignore Timer 1 Interrupts */
00111   ENERGEST_ON(ENERGEST_TYPE_IRQ);
00112   /* No more interrupts from Channel 1 till next rtimer_arch_schedule() call.
00113    * Setting the mask will instantly generate an interrupt so we clear the
00114    * flag first. */
00115   T1CTL &= ~(CH1IF);
00116   T1CCTL1 &= ~T1IM;
00117 
00118   rtimer_run_next();
00119 
00120   ENERGEST_OFF(ENERGEST_TYPE_IRQ);
00121   IEN1_T1IE = 1; /* Acknowledge Timer 1 Interrupts */
00122 }