Contiki 3.x
rtimer-arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, SICS Swedish ICT.
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  * RTIMER for NXP jn516x
36  * \author
37  * Beshr Al Nahas <beshr@sics.se>
38  * Atis Elsts <atis.elsts@sics.se>
39  */
40 
41 #include "sys/rtimer.h"
42 #include "sys/clock.h"
43 #include <AppHardwareApi.h>
44 #include <PeripheralRegs.h>
45 #include <MicroSpecific.h>
46 #include "dev/watchdog.h"
47 #include "sys/energest.h"
48 #include "sys/process.h"
49 
50 #if !RTIMER_USE_32KHZ
51 
52 #define DEBUG 0
53 #if DEBUG
54 #include <stdio.h>
55 #define PRINTF(...) printf(__VA_ARGS__)
56 #else
57 #define PRINTF(...)
58 #endif
59 
60 #define RTIMER_TIMER_ISR_DEV E_AHI_DEVICE_TICK_TIMER
61 
62 static volatile rtimer_clock_t scheduled_time;
63 static volatile uint8_t has_next;
64 
65 void
66 rtimer_arch_run_next(uint32 u32DeviceId, uint32 u32ItemBitmap)
67 {
68  uint32_t delta;
69 
70  if(u32DeviceId != RTIMER_TIMER_ISR_DEV) {
71  return;
72  }
73 
74  ENERGEST_ON(ENERGEST_TYPE_IRQ);
75  vAHI_TickTimerIntPendClr();
76  vAHI_TickTimerIntEnable(0);
77  /*
78  * compare register is only 28bits wide so make sure the upper 4bits match
79  * the set compare point
80  */
81  delta = u32AHI_TickTimerRead() - scheduled_time;
82  if(delta >> 28 == 0) {
83  /* run scheduled */
84  has_next = 0;
88  } else {
89  /* No match. Schedule again. */
90  vAHI_TickTimerIntEnable(1);
91  vAHI_TickTimerInterval(scheduled_time);
92  }
93  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
94 }
95 /*---------------------------------------------------------------------------*/
96 void
98 {
99  /* Initialise tick timer to run continuously */
100  vAHI_TickTimerConfigure(E_AHI_TICK_TIMER_DISABLE);
101  vAHI_TickTimerIntEnable(0);
102  vAHI_TickTimerRegisterCallback(rtimer_arch_run_next);
103  vAHI_TickTimerWrite(0);
104  vAHI_TickTimerConfigure(E_AHI_TICK_TIMER_CONT);
105 
106  /* enable wakeup timers, but keep interrupts disabled */
107  vAHI_WakeTimerEnable(WAKEUP_TIMER, FALSE);
108  vAHI_WakeTimerEnable(TICK_TIMER, FALSE);
109  /* count down from zero (2, as values 0 and 1 must not be used) */
110  vAHI_WakeTimerStartLarge(TICK_TIMER, 2);
111 
112  (void)u32AHI_Init();
113 }
114 /*---------------------------------------------------------------------------*/
115 void
116 rtimer_arch_reinit(rtimer_clock_t sleep_start, rtimer_clock_t sleep_ticks)
117 {
118  uint64_t t;
119  /* Initialise tick timer to run continuously */
120  vAHI_TickTimerConfigure(E_AHI_TICK_TIMER_DISABLE);
121  vAHI_TickTimerIntEnable(0);
122  /* set the highest priority for the rtimer interrupt */
123  vAHI_InterruptSetPriority(MICRO_ISR_MASK_TICK_TMR, 15);
124  vAHI_TickTimerRegisterCallback(rtimer_arch_run_next);
125  WAIT_FOR_EDGE(t);
126  vAHI_TickTimerWrite(sleep_start + sleep_ticks);
127  vAHI_TickTimerConfigure(E_AHI_TICK_TIMER_CONT);
128 
129  /* call pending interrupts */
130  u32AHI_Init();
131 
132  if(has_next) {
133  vAHI_TickTimerIntPendClr();
134  vAHI_TickTimerIntEnable(1);
135  vAHI_TickTimerInterval(scheduled_time);
136  }
137 }
138 /*---------------------------------------------------------------------------*/
139 rtimer_clock_t
141 {
142  return u32AHI_TickTimerRead();
143 }
144 /*---------------------------------------------------------------------------*/
145 void
146 rtimer_arch_schedule(rtimer_clock_t t)
147 {
148  PRINTF("rtimer_arch_schedule time %lu\n", t);
149  vAHI_TickTimerIntPendClr();
150  vAHI_TickTimerIntEnable(1);
151  vAHI_TickTimerInterval(t);
152  has_next = 1;
153  scheduled_time = t;
154 }
155 /*---------------------------------------------------------------------------*/
156 rtimer_clock_t
157 rtimer_arch_time_to_rtimer(void)
158 {
159  rtimer_clock_t now = RTIMER_NOW();
160  if(has_next) {
161  return scheduled_time >= now ? scheduled_time - now : 0;
162  }
163  /* if no wakeup is scheduled yet return maximum time */
164  return (rtimer_clock_t)-1;
165 }
166 /*---------------------------------------------------------------------------*/
167 #endif /* !RTIMER_USE_32KHZ */
Header file for the real-time timer module.
#define RTIMER_NOW()
Get the current clock time.
Definition: rtimer.h:135
Header file for the energy estimation mechanism
rtimer_clock_t rtimer_arch_now()
Returns the current real-time clock time.
Definition: rtimer-arch.c:116
void rtimer_arch_init(void)
We don't need to explicitly initialise anything but this routine is required by the API...
Definition: rtimer-arch.c:60
void rtimer_run_next(void)
Execute the next real-time task and schedule the next task, if any.
Definition: rtimer.c:92
Header file for the Contiki process interface.
#define FALSE
An alias for zero, used for clarity.
int process_nevents(void)
Number of events waiting to be processed.
Definition: process.c:316
void rtimer_arch_schedule(rtimer_clock_t t)
Schedules an rtimer task to be triggered at time t.
Definition: rtimer-arch.c:72
void watchdog_start(void)
Starts the WDT in watchdog mode if enabled by user configuration, maximum interval.
Definition: watchdog.c:49