Contiki-Inga 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_init() has set our tick speed prescaler already, so we
39  * are ticking with 500 kHz freq.
40  *
41  * Contiki typedefs rtimer_clock_t as unsigned short (16bit)
42  * It thus makes sense to use the 16bit timer (Timer 1)
43  *
44  * This file contains an ISR and must reside in the HOME bank
45  *
46  * \author
47  * George Oikonomou - <oikonomou@users.sourceforge.net>
48  */
49 
50 #include "sys/rtimer.h"
51 #include "sfr-bits.h"
52 #include "cc253x.h"
53 #include "sys/energest.h"
54 
55 #include "debug.h"
56 #include <stdio.h>
57 
58 #define RT_MODE_COMPARE() do { T1CCTL1 |= T1CCTL_MODE; } while(0)
59 #define RT_MODE_CAPTURE() do { T1CCTL1 &= ~T1CCTL_MODE; } while(0)
60 /*---------------------------------------------------------------------------*/
61 void
62 rtimer_arch_init(void)
63 {
64  /*
65  * - Free running mode
66  * - Prescale by 32:
67  * Tick Speed has been prescaled to 500 kHz already in clock_init()
68  * We further prescale by 32 resulting in 15625 Hz for this timer.
69  */
70  T1CTL = (T1CTL_DIV1 | T1CTL_MODE0);
71 
72  T1STAT = 0;
73 
74  /* Timer 1, Channel 1. Compare Mode (0x04), Interrupt mask on (0x40) */
75  T1CCTL1 = T1CCTL_MODE | T1CCTL_IM;
76 
77  /* Interrupt Mask Flags: No interrupt on overflow */
78  OVFIM = 0;
79 
80  /* Acknowledge Timer 1 Interrupts */
81  T1IE = 1;
82 }
83 /*---------------------------------------------------------------------------*/
84 void
85 rtimer_arch_schedule(rtimer_clock_t t)
86 {
87  /* Switch to capture mode before writing T1CC1x and
88  * set the compare mode values so we can get an interrupt after t */
89  RT_MODE_CAPTURE();
90  T1CC1L = (unsigned char)t;
91  T1CC1H = (unsigned char)(t >> 8);
92  RT_MODE_COMPARE();
93 
94  /* Turn on compare mode interrupt */
95  T1STAT = 0;
96  T1CCTL1 |= T1CCTL_IM;
97 }
98 /*---------------------------------------------------------------------------*/
99 /* avoid referencing bits, we don't call code which use them */
100 #pragma save
101 #if CC_CONF_OPTIMIZE_STACK_SIZE
102 #pragma exclude bits
103 #endif
104 void
105 rtimer_isr(void) __interrupt(T1_VECTOR)
106 {
107  T1IE = 0; /* Ignore Timer 1 Interrupts */
108  ENERGEST_ON(ENERGEST_TYPE_IRQ);
109 
110  /* No more interrupts from Channel 1 till next rtimer_arch_schedule() call */
111  T1STAT &= ~T1STAT_CH1IF;
112  T1CCTL1 &= ~T1CCTL_IM;
113 
114  rtimer_run_next();
115 
116  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
117  T1IE = 1; /* Acknowledge Timer 1 Interrupts */
118 }
119 #pragma restore