Contiki-NG
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/process.h"
48 
49 #if !RTIMER_USE_32KHZ
50 
51 #define DEBUG 0
52 #if DEBUG
53 #include <stdio.h>
54 #define PRINTF(...) printf(__VA_ARGS__)
55 #else
56 #define PRINTF(...)
57 #endif
58 
59 #define RTIMER_TIMER_ISR_DEV E_AHI_DEVICE_TICK_TIMER
60 
61 static volatile rtimer_clock_t scheduled_time;
62 static volatile uint8_t has_next;
63 
64 void
65 rtimer_arch_run_next(uint32 u32DeviceId, uint32 u32ItemBitmap)
66 {
67  uint32_t delta;
68 
69  if(u32DeviceId != RTIMER_TIMER_ISR_DEV) {
70  return;
71  }
72 
73  vAHI_TickTimerIntPendClr();
74  vAHI_TickTimerIntEnable(0);
75  /*
76  * compare register is only 28bits wide so make sure the upper 4bits match
77  * the set compare point
78  */
79  delta = u32AHI_TickTimerRead() - scheduled_time;
80  if(delta >> 28 == 0) {
81  /* run scheduled */
82  has_next = 0;
86  } else {
87  /* No match. Schedule again. */
88  vAHI_TickTimerIntEnable(1);
89  vAHI_TickTimerInterval(scheduled_time);
90  }
91 }
92 /*---------------------------------------------------------------------------*/
93 void
95 {
96  /* Initialise tick timer to run continuously */
97  vAHI_TickTimerConfigure(E_AHI_TICK_TIMER_DISABLE);
98  vAHI_TickTimerIntEnable(0);
99  vAHI_TickTimerRegisterCallback(rtimer_arch_run_next);
100  vAHI_TickTimerWrite(0);
101  vAHI_TickTimerConfigure(E_AHI_TICK_TIMER_CONT);
102 
103  /* enable wakeup timers, but keep interrupts disabled */
104  vAHI_WakeTimerEnable(WAKEUP_TIMER, FALSE);
105  vAHI_WakeTimerEnable(TICK_TIMER, FALSE);
106  /* count down from zero (2, as values 0 and 1 must not be used) */
107  vAHI_WakeTimerStartLarge(TICK_TIMER, 2);
108 
109  (void)u32AHI_Init();
110 }
111 /*---------------------------------------------------------------------------*/
112 void
113 rtimer_arch_reinit(rtimer_clock_t sleep_start, rtimer_clock_t sleep_ticks)
114 {
115  uint64_t t;
116  /* Initialise tick timer to run continuously */
117  vAHI_TickTimerConfigure(E_AHI_TICK_TIMER_DISABLE);
118  vAHI_TickTimerIntEnable(0);
119  /* set the highest priority for the rtimer interrupt */
120  vAHI_InterruptSetPriority(MICRO_ISR_MASK_TICK_TMR, 15);
121  vAHI_TickTimerRegisterCallback(rtimer_arch_run_next);
122  WAIT_FOR_EDGE(t);
123  vAHI_TickTimerWrite(sleep_start + sleep_ticks);
124  vAHI_TickTimerConfigure(E_AHI_TICK_TIMER_CONT);
125 
126  /* call pending interrupts */
127  u32AHI_Init();
128 
129  if(has_next) {
130  vAHI_TickTimerIntPendClr();
131  vAHI_TickTimerIntEnable(1);
132  vAHI_TickTimerInterval(scheduled_time);
133  }
134 }
135 /*---------------------------------------------------------------------------*/
136 rtimer_clock_t
138 {
139  return u32AHI_TickTimerRead();
140 }
141 /*---------------------------------------------------------------------------*/
142 void
143 rtimer_arch_schedule(rtimer_clock_t t)
144 {
145  PRINTF("rtimer_arch_schedule time %lu\n", t);
146  vAHI_TickTimerIntPendClr();
147  vAHI_TickTimerIntEnable(1);
148  vAHI_TickTimerInterval(t);
149  has_next = 1;
150  scheduled_time = t;
151 }
152 /*---------------------------------------------------------------------------*/
153 rtimer_clock_t
154 rtimer_arch_time_to_rtimer(void)
155 {
156  rtimer_clock_t now = RTIMER_NOW();
157  if(has_next) {
158  return scheduled_time >= now ? scheduled_time - now : 0;
159  }
160  /* if no wakeup is scheduled yet return maximum time */
161  return (rtimer_clock_t)-1;
162 }
163 /*---------------------------------------------------------------------------*/
164 #endif /* !RTIMER_USE_32KHZ */
rtimer_clock_t rtimer_arch_now()
Returns the current real-time clock time.
Definition: rtimer-arch.c:115
void rtimer_arch_schedule(rtimer_clock_t t)
Schedules an rtimer task to be triggered at time t.
Definition: rtimer-arch.c:71
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:59
#define RTIMER_NOW()
Get the current clock time.
Definition: rtimer.h:185
void rtimer_run_next(void)
Execute the next real-time task and schedule the next task, if any.
Definition: rtimer.c:92
void watchdog_start(void)
Starts the WDT in watchdog mode if enabled by user configuration, maximum interval.
Definition: watchdog.c:72
Header file for the real-time timer module.
Header file for the Contiki process interface.
int process_nevents(void)
Number of events waiting to be processed.
Definition: process.c:316