Contiki 3.x
rtimer-arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Loughborough University - Computer Science
3  * All rights reserved.
4  *
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.
16  *
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.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Hardware-dependent functions used to support the
36  * contiki rtimer module.
37  *
38  * clock and etimer are using the sleep timer on the cc2430
39  *
40  * clock_init() has set our tick speed prescaler already, so we
41  * are ticking with 500 kHz freq.
42  *
43  * rtimer_clock_t is unsigned short (16bit on the cc2430)
44  * It thus makes sense to use the 16bit clock (Timer 1)
45  *
46  * This file contains an ISR and must reside in the HOME bank
47  *
48  * \author
49  * George Oikonomou - <oikonomou@users.sourceforge.net>
50  */
51 
52 #include "sys/rtimer.h" /* Includes rtimer-arch.h for us */
53 #include "cc2430_sfr.h"
54 #include "sys/energest.h"
55 
56 #define DEBUG 0
57 #if DEBUG
58 #include <stdio.h>
59 #define PRINTF(...) printf(__VA_ARGS__)
60 #else
61 #define PRINTF(...)
62 #endif
63 
64 /*---------------------------------------------------------------------------*/
65 void
66 rtimer_arch_init(void)
67 {
68  PRINTF("rtimer_arch_init() ");
69  /*
70  * - Free running mode
71  * - Prescale by 32:
72  * Tick Speed has been prescaled to 500 kHz already in clock_init()
73  * We further prescale by 32 resulting in 15625 Hz for this timer.
74  */
75  T1CTL = (T1DIV1 | T1MODE0); /* 00001001 */
76  PRINTF("T1CTL=0x%02x\n", T1CTL);
77  /* Acknowledge Timer 1 Interrupts */
78  IEN1_T1IE = 1;
79  PRINTF("IEN1_T1IE=0x%02x\n", IEN1_T1IE);
80 
81  /* Timer 1, Channel 1. Compare Mode (0x04), Interrupt mask on (0x40) */
82  T1CCTL1 = T1MODE + T1IM;
83  PRINTF("T1CCTL1=0x%02x\n", T1CCTL1);
84 
85  /* Interrupt Mask Flags: No interrupt on overflow */
86  TIMIF &= ~OVFIM;
87  PRINTF("TIMIF=0x%02x\n", TIMIF);
88 
89  PRINTF("done\n");
90 }
91 /*---------------------------------------------------------------------------*/
92 void
93 rtimer_arch_schedule(rtimer_clock_t t)
94 {
95  PRINTF("rtimer_arch_schedule(%u)\n", t);
96 
97  /* set the compare mode values so we can get an interrupt after t */
98  T1CC1L = (unsigned char)t;
99  T1CC1H = (unsigned char)(t >> 8);
100  PRINTF("T1CC1=%u, t=%u\n", (T1CC1L + (T1CC1H << 8)), t);
101 
102  /* Turn on compare mode interrupt */
103  PRINTF("T1CTL=0x%02x\n", T1CTL);
104  T1CCTL1 |= T1IM;
105 }
106 /*---------------------------------------------------------------------------*/
107 #pragma save
108 #if CC_CONF_OPTIMIZE_STACK_SIZE
109 #pragma exclude bits
110 #endif
111 void
112 cc2430_timer_1_ISR(void) __interrupt(T1_VECTOR)
113 {
114  IEN1_T1IE = 0; /* Ignore Timer 1 Interrupts */
115  ENERGEST_ON(ENERGEST_TYPE_IRQ);
116  /* No more interrupts from Channel 1 till next rtimer_arch_schedule() call.
117  * Setting the mask will instantly generate an interrupt so we clear the
118  * flag first. */
119  T1CTL &= ~(CH1IF);
120  T1CCTL1 &= ~T1IM;
121 
122  rtimer_run_next();
123 
124  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
125  IEN1_T1IE = 1; /* Acknowledge Timer 1 Interrupts */
126 }
127 #pragma restore
CC2430 registers header file for CC2430.
void rtimer_arch_init(void)
We don&#39;t need to explicitly initialise anything but this routine is required by the API...
Definition: rtimer-arch.c:78
Header file for the energy estimation mechanism
Header file for the real-time timer module.
void rtimer_arch_schedule(rtimer_clock_t t)
Schedules an rtimer task to be triggered at time t.
Definition: rtimer-arch.c:115
void rtimer_run_next(void)
Execute the next real-time task and schedule the next task, if any.
Definition: rtimer.c:92