Contiki-NG
rtimer-arch.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012, Texas Instruments Incorporated - http://www.ti.com/
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 *
14 * 3. Neither the name of the copyright holder nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31/**
32 * \addtogroup cc2538-rtimer
33 * @{
34 *
35 * \file
36 * Implementation of the arch-specific rtimer functions for the cc2538
37 *
38 */
39#include "contiki.h"
40#include "sys/rtimer.h"
41#include "dev/nvic.h"
42#include "dev/smwdthrosc.h"
43#include "cpu.h"
44#include "lpm.h"
45
46#include <stdint.h>
47/*---------------------------------------------------------------------------*/
48static volatile rtimer_clock_t next_trigger;
49/*---------------------------------------------------------------------------*/
50/**
51 * \brief We don't need to explicitly initialise anything but this
52 * routine is required by the API.
53 *
54 * The Sleep Timer starts ticking automatically as soon as the device
55 * turns on. We don't need to turn on interrupts before the first call
56 * to rtimer_arch_schedule()
57 */
58void
60{
61 return;
62}
63/*---------------------------------------------------------------------------*/
64void
65rtimer_arch_schedule(rtimer_clock_t t)
66{
67 rtimer_clock_t now;
68
69 /* STLOAD must be 1 */
71
73
74 now = RTIMER_NOW();
75
76 /*
77 * New value must be 5 ticks in the future. The ST may tick once while we're
78 * writing the registers. We play it safe here and we add a bit of leeway
79 */
80 if(!RTIMER_CLOCK_LT(now, t - RTIMER_GUARD_TIME)) {
81 t = now + RTIMER_GUARD_TIME;
82 }
83
84 /* ST0 latches ST[1:3] and must be written last */
85 REG(SMWDTHROSC_ST3) = (t >> 24) & 0x000000FF;
86 REG(SMWDTHROSC_ST2) = (t >> 16) & 0x000000FF;
87 REG(SMWDTHROSC_ST1) = (t >> 8) & 0x000000FF;
88 REG(SMWDTHROSC_ST0) = t & 0x000000FF;
89
91
92 /* Store the value. The LPM module will query us for it */
93 next_trigger = t;
94
95 NVIC_EnableIRQ(SMT_IRQn);
96}
97/*---------------------------------------------------------------------------*/
98rtimer_clock_t
100{
101 return next_trigger;
102}
103/*---------------------------------------------------------------------------*/
104/**
105 * \brief Returns the current real-time clock time
106 * \return The current rtimer time in ticks
107 */
108rtimer_clock_t
110{
111 rtimer_clock_t rv;
112
113 /* SMWDTHROSC_ST0 latches ST[1:3] and must be read first */
114 rv = REG(SMWDTHROSC_ST0);
115 rv |= (REG(SMWDTHROSC_ST1) << 8);
116 rv |= (REG(SMWDTHROSC_ST2) << 16);
117 rv |= (REG(SMWDTHROSC_ST3) << 24);
118
119 return rv;
120}
121/*---------------------------------------------------------------------------*/
122/**
123 * \brief The rtimer ISR
124 *
125 * Interrupts are only turned on when we have an rtimer task to schedule
126 * Once the interrupt fires, the task is called and then interrupts no
127 * longer get acknowledged until the next task needs scheduled.
128 */
129void
131{
132 /*
133 * If we were in PM1+, call the wake-up sequence first. This will make sure
134 * that the 32MHz OSC is selected as the clock source. We need to do this
135 * before calling the next rtimer_task, since the task may need the RF.
136 */
137 lpm_exit();
138
139 next_trigger = 0;
140
141 NVIC_ClearPendingIRQ(SMT_IRQn);
142 NVIC_DisableIRQ(SMT_IRQn);
143
145}
146/*---------------------------------------------------------------------------*/
147/** @} */
Header file with prototypes for interrupt control on the cc2538 Cortex-M3 micro.
@ SMT_IRQn
SM Timer Interrupt.
Definition: cc2538_cm3.h:113
#define INTERRUPTS_DISABLE()
Disables all CPU interrupts.
Definition: cpu.h:54
#define INTERRUPTS_ENABLE()
Enables all CPU interrupts.
Definition: cpu.h:51
rtimer_clock_t rtimer_arch_next_trigger()
Get the time of the next scheduled rtimer trigger.
Definition: rtimer-arch.c:99
void rtimer_isr()
The rtimer ISR.
Definition: rtimer-arch.c:130
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:59
rtimer_clock_t rtimer_arch_now()
Returns the current real-time clock time.
Definition: rtimer-arch.c:109
void rtimer_arch_schedule(rtimer_clock_t t)
Schedules an rtimer task to be triggered at time t.
Definition: rtimer-arch.c:65
#define SMWDTHROSC_ST0
ST count/compare value 0.
Definition: smwdthrosc.h:51
#define SMWDTHROSC_ST3
ST count/compare value 3.
Definition: smwdthrosc.h:54
#define SMWDTHROSC_ST1
ST count/compare value 1.
Definition: smwdthrosc.h:52
#define SMWDTHROSC_ST2
ST count/compare value 2.
Definition: smwdthrosc.h:53
#define SMWDTHROSC_STLOAD
Compare value load status.
Definition: smwdthrosc.h:55
#define SMWDTHROSC_STLOAD_STLOAD
STx upload status signal.
Definition: smwdthrosc.h:89
void rtimer_run_next(void)
Execute the next real-time task and schedule the next task, if any.
Definition: rtimer.c:92
#define RTIMER_NOW()
Get the current clock time.
Definition: rtimer.h:185
Header file for the ARM Nested Vectored Interrupt Controller.
Configuration for the nrf platform.
Header file for the real-time timer module.
Header file with register declarations and bit masks for the cc2538 Sleep Timer and Watchdog.