Contiki 3.x
rtimer-arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Eistec AB.
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 copyright holder 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 COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND 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 COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * This file is part of the Mulle platform port of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * K60 specific rtimer library implementation.
36  * \author
37  * Joakim Gebart <joakim.gebart@eistec.se>
38  */
39 
40 #include "rtimer-arch.h"
41 #include "K60.h"
42 
43 #define DEBUG 1
44 #if DEBUG
45 #include <stdio.h>
46 #define PRINTF(...) printf(__VA_ARGS__)
47 #else
48 #define PRINTF(...)
49 #endif
50 
51 /* Convenience macro to access the channel struct directly. */
52 #define RTIMER_ARCH_CHANNEL (RTIMER_ARCH_PIT_DEV->CHANNEL[RTIMER_ARCH_PIT_CHANNEL])
53 /* Convenience macro to enable and disable the rtimer timer hardware. */
54 #define RTIMER_ARCH_TIMER_ENABLE() (BITBAND_REG(RTIMER_ARCH_CHANNEL.TCTRL, PIT_TCTRL_TEN_SHIFT) = 1)
55 #define RTIMER_ARCH_TIMER_DISABLE() (BITBAND_REG(RTIMER_ARCH_CHANNEL.TCTRL, PIT_TCTRL_TEN_SHIFT) = 0)
56 
57 /** Offset between current counter/timer and t=0 (boot instant) */
58 static rtimer_clock_t offset;
59 /** set to 0 while a task is scheduled */
60 static int free_running;
61 /** LDVAL of the free running timer */
62 #define RTIMER_ARCH_FREE_RUNNING_LDVAL (PIT_LDVAL_TSV_MASK >> PIT_LDVAL_TSV_SHIFT)
63 
64 void
65 rtimer_arch_init(void) {
66  offset = 0;
67 
68  /* Free running when not waiting for any rtimer tasks. */
69  free_running = 1;
70 
71  /* Enable module clock */
72  BITBAND_REG(SIM->SCGC6, SIM_SCGC6_PIT_SHIFT) = 1;
73 
74  /* Disable timer until we have something to schedule. */
75  RTIMER_ARCH_TIMER_DISABLE();
76 
77  /* Enable interrupt generation */
78  BITBAND_REG(RTIMER_ARCH_CHANNEL.TCTRL, PIT_TCTRL_TIE_SHIFT) = 1;
79 
80  /* Clear interrupt flag */
81  BITBAND_REG(RTIMER_ARCH_CHANNEL.TFLG, PIT_TFLG_TIF_MASK) = 1;
82 
83  /* Load largest possible value to begin counting time for RTIMER_NOW. */
84  RTIMER_ARCH_CHANNEL.LDVAL = RTIMER_ARCH_FREE_RUNNING_LDVAL;
85 
86  /* Enable to load the value. */
87  RTIMER_ARCH_TIMER_ENABLE();
88 
89  /* Enable interrupts for PIT module in NVIC */
90  NVIC_EnableIRQ(RTIMER_ARCH_PIT_IRQn);
91 
92 #if DEBUG
93  int64_t err = ((int64_t)SystemBusClock) - ((int64_t)RTIMER_SECOND);
94  err *= (int64_t)1000000ull;
95  err /= (int64_t)(RTIMER_SECOND);
96  PRINTF("rtimer_arch_init: Real rtimer frequency = %lu, RTIMER_SECOND = %lu\n", (unsigned long)SystemBusClock, (unsigned long)RTIMER_SECOND);
97  PRINTF("rtimer_arch_init: Expect ca %ld ppm error on scheduled task times.\n", (signed long)err);
98 #endif
99  PRINTF("rtimer_arch_init: Done\n");
100 }
101 
102 void
103 rtimer_arch_schedule(rtimer_clock_t t) {
104  if (t > 0xffffffffull) {
105  PRINTF("rtimer_arch_schedule: Schedule out of range! This task will run much sooner than expected.\n");
106  }
107  /* Increase time base */
108  offset = rtimer_arch_now();
109 
110  /* Set timer value */
111  RTIMER_ARCH_CHANNEL.LDVAL = PIT_LDVAL_TSV(t);
112 
113  /* No longer free running */
114  free_running = 0;
115 
116  /* Disable timer and re-enable to load the new value. */
117  RTIMER_ARCH_TIMER_DISABLE();
118  RTIMER_ARCH_TIMER_ENABLE();
119 
120  PRINTF("rtimer_arch_schedule: %lu\n", (unsigned long)t);
121 }
122 
123 rtimer_clock_t
124 rtimer_arch_now(void) {
125  /* Timer is down-counting, we flip it here. */
126  return offset + ((rtimer_clock_t)(
127  ((RTIMER_ARCH_CHANNEL.LDVAL & PIT_LDVAL_TSV_MASK) >> PIT_LDVAL_TSV_SHIFT) -
128  ((RTIMER_ARCH_CHANNEL.CVAL & PIT_CVAL_TVL_MASK) >> PIT_CVAL_TVL_SHIFT)));
129 }
130 
131 /* Interrupt handler for rtimer triggers */
132 void
133 _isr_pit0(void) {
134  /* Save old timeout */
135  static uint32_t prev_timeout;
136  prev_timeout = ((RTIMER_ARCH_CHANNEL.LDVAL & PIT_LDVAL_TSV_MASK) >> PIT_LDVAL_TSV_SHIFT);
137 
138  /* Clear interrupt flag */
139  BITBAND_REG(RTIMER_ARCH_CHANNEL.TFLG, PIT_TFLG_TIF_SHIFT) = 1;
140 
141  /* Update offset with the time that has passed since the counter reached zero.
142  * This time offset could potentially be significant, for example when
143  * multiple interrupts occur right before the PIT interrupt causing the PIT
144  * interrupt to become pending while the other interrupts are handled, or
145  * interrupts with a higher priority cause this ISR to be */
146  /* After triggering this interrupt we are currently in, the counter will start
147  * over at LDVAL and continue counting down. This new time is added to the
148  * offset by the line below, however, we still have not added the initial
149  * length of the timer that actually triggered the interrupt. */
150  offset = rtimer_arch_now(); /* offset = [old offset] + [ISR processing time] */
151 
152  /* rtimer schedulings are one-shot, go back to counting time for RTIMER_NOW */
153  /* Load largest possible value */
154  RTIMER_ARCH_CHANNEL.LDVAL = RTIMER_ARCH_FREE_RUNNING_LDVAL;
155 
156  RTIMER_ARCH_TIMER_DISABLE();
157  RTIMER_ARCH_TIMER_ENABLE();
158  /* We do the above few lines as close as possible to the
159  * offset = rtimer_arch_now(); in order to avoid losing a lot of ticks. */
160 
161  /* Increase time base for RTIMER_NOW by the initial timer amount. */
162  offset += (rtimer_clock_t)prev_timeout;
163  /* offset is now [offset before ISR] + [ISR processing time] + [timer interval] */
164 
165  /** \todo compensate for time between offset = rtimer_arch_now() and RTIMER_ARCH_TIMER_ENABLE in rtimer ISR. */
166 
167  if (free_running == 0) {
168  /* We hit a scheduled task time. */
169  free_running = 1;
170  /* Call higher level rtimer module */
171  rtimer_run_next();
172  }
173  /* Else: Timeout while counting time (happens after 89 seconds at 48MHz).
174  * nothing to do here then but to continue counting. */
175 }
__STATIC_INLINE void NVIC_EnableIRQ(IRQn_Type IRQn)
Enable External Interrupt.
Definition: core_cm0.h:535
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
#define RTIMER_ARCH_FREE_RUNNING_LDVAL
LDVAL of the free running timer.
Definition: rtimer-arch.c:62
#define SIM
Peripheral SIM base pointer.
Definition: MK60D10.h:7650
K60 hardware register header wrapper.
#define rtimer_arch_now()
Definition: rtimer-arch.h:40
void _isr_pit0(void)
Definition: rtimer-arch.c:133
#define BITBAND_REG(Reg, Bit)
Macro to access a single bit of a peripheral register (bit band region 0x40000000 to 0x400FFFFF) usin...
Definition: MK60D10.h:71
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
uint32_t SystemBusClock
Current bus clock frequency.