1 /*
2 * Copyright (c) 2016, The OpenThread Authors.
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 are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /**
30 * @file
31 * This file implements the OpenThread platform abstraction for the alarm.
32 *
33 */
34
35 #include <stdbool.h>
36 #include <stdint.h>
37
38 #include <openthread/config.h>
39 #include <openthread/platform/alarm-milli.h>
40 #include <openthread/platform/diag.h>
41
42 #include "platform-cc2538.h"
43
44 enum
45 {
46 kSystemClock = 32000000, ///< MHz
47 kTicksPerSec = 1000, ///< Ticks per second
48 };
49
50 static uint32_t sCounter = 0;
51 static uint32_t sAlarmT0 = 0;
52 static uint32_t sAlarmDt = 0;
53 static bool sIsRunning = false;
54
55 static uint8_t sTimersIsRunning = 0;
56 static uint32_t sTimersExpireAt[OT_CC2538_TIMERS_COUNT];
57
58 extern void cc2538EnergyScanTimerHandler(void);
59
cc2538SetTimer(otCC2538Timer aTimer,uint32_t aDelay)60 void cc2538SetTimer(otCC2538Timer aTimer, uint32_t aDelay)
61 {
62 sTimersIsRunning |= (1 << aTimer);
63 sTimersExpireAt[aTimer] = sCounter + aDelay;
64 }
65
cc2538AlarmInit(void)66 void cc2538AlarmInit(void)
67 {
68 HWREG(NVIC_ST_RELOAD) = kSystemClock / kTicksPerSec;
69 HWREG(NVIC_ST_CTRL) = NVIC_ST_CTRL_CLK_SRC | NVIC_ST_CTRL_INTEN | NVIC_ST_CTRL_ENABLE;
70 }
71
otPlatAlarmMilliGetNow(void)72 uint32_t otPlatAlarmMilliGetNow(void)
73 {
74 return sCounter;
75 }
76
otPlatAlarmMilliStartAt(otInstance * aInstance,uint32_t t0,uint32_t dt)77 void otPlatAlarmMilliStartAt(otInstance *aInstance, uint32_t t0, uint32_t dt)
78 {
79 OT_UNUSED_VARIABLE(aInstance);
80
81 sAlarmT0 = t0;
82 sAlarmDt = dt;
83 sIsRunning = true;
84 }
85
otPlatAlarmMilliStop(otInstance * aInstance)86 void otPlatAlarmMilliStop(otInstance *aInstance)
87 {
88 OT_UNUSED_VARIABLE(aInstance);
89
90 sIsRunning = false;
91 }
92
cc2538AlarmProcess(otInstance * aInstance)93 void cc2538AlarmProcess(otInstance *aInstance)
94 {
95 uint32_t expires;
96 bool fire = false;
97
98 if (sTimersIsRunning)
99 {
100 if ((int32_t)(sTimersExpireAt[OT_CC2538_TIMER_ENERGY_SCAN] - sCounter) < 0)
101 {
102 sTimersIsRunning &= ~(1 << OT_CC2538_TIMER_ENERGY_SCAN);
103 cc2538EnergyScanTimerHandler();
104 }
105 }
106
107 if (sIsRunning)
108 {
109 expires = sAlarmT0 + sAlarmDt;
110
111 if (sAlarmT0 <= sCounter)
112 {
113 if (expires >= sAlarmT0 && expires <= sCounter)
114 {
115 fire = true;
116 }
117 }
118 else
119 {
120 if (expires >= sAlarmT0 || expires <= sCounter)
121 {
122 fire = true;
123 }
124 }
125
126 if (fire)
127 {
128 sIsRunning = false;
129
130 #if OPENTHREAD_CONFIG_DIAG_ENABLE
131
132 if (otPlatDiagModeGet())
133 {
134 otPlatDiagAlarmFired(aInstance);
135 }
136 else
137 #endif
138 {
139 otPlatAlarmMilliFired(aInstance);
140 }
141 }
142 }
143 }
144
SysTick_Handler()145 void SysTick_Handler()
146 {
147 sCounter++;
148 }
149