BSPACM  20150113
Board Support Package for ARM Cortex-M Microcontrollers
uptime.h
Go to the documentation of this file.
1 /* Copyright 2015, Peter A. Bigot
2  *
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  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * * Neither the name of the software nor the names of its contributors may be
16  * used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
49 #ifndef BSPACM_DEVICE_NRF51_INTERNAL_UTILITY_UPTIME_H
50 #define BSPACM_DEVICE_NRF51_INTERNAL_UTILITY_UPTIME_H
51 
52 #include <bspacm/core.h>
53 
54 #ifdef __cplusplus
55 extern "C" {
56 #endif /* __cplusplus */
57 
58 #ifndef BSPACM_UPTIME_RTC_BASE
59 
64 #define BSPACM_UPTIME_RTC_BASE NRF_RTC1_BASE
65 #endif /* BSPACM_UPTIME_RTC_BASE */
66 
85 #if NRF_RTC0_BASE == BSPACM_UPTIME_RTC_BASE
86 #define BSPACM_UPTIME_RTC NRF_RTC0
87 #define BSPACM_UPTIME_CC_COUNT 3
88 #elif NRF_RTC1_BASE == BSPACM_UPTIME_RTC_BASE
89 #define BSPACM_UPTIME_RTC NRF_RTC1
90 #define BSPACM_UPTIME_CC_COUNT 4
91 #else /* BSPACM_UPTIME_RTC_BASE */
92 #error Unrecognized uptime RTC
93 #endif /* BSPACM_UPTIME_RTC_BASE */
94 
96 #define BSPACM_UPTIME_Hz 32768U
97 
100 #define BSPACM_UPTIME_SLEEP_MINIMUM 2
101 
103 inline
104 unsigned int
105 uiBSPACMuptimeConvert_us_utt (unsigned int dur_us)
106 {
107  const unsigned int us_per_s = 1000000U;
108  return (dur_us * BSPACM_UPTIME_Hz + us_per_s - 1) / us_per_s;
109 }
110 
112 inline
113 unsigned int
114 uiBSPACMuptimeConvert_ms_utt (unsigned int dur_ms)
115 {
116  const unsigned int ms_per_s = 1000U;
117  return (dur_ms * BSPACM_UPTIME_Hz + ms_per_s - 1) / ms_per_s;
118 }
119 
121 inline
122 unsigned int
123 uiBSPACMuptimeConvert_utt_us (unsigned int dur_utt)
124 {
125  return ((1000000ULL * dur_utt) + BSPACM_UPTIME_Hz - 1) / BSPACM_UPTIME_Hz;
126 }
127 
129 inline
130 unsigned int
131 uiBSPACMuptimeConvert_utt_ms (unsigned int dur_utt)
132 {
133  return ((1000ULL * dur_utt) + BSPACM_UPTIME_Hz - 1) / BSPACM_UPTIME_Hz;
134 }
135 
136 /* Forward declaration */
137 struct sBSPACMuptimeAlarm;
138 
154 typedef void (* vBSPACMuptimeAlarmCallback_flih) (int ccidx,
155  struct sBSPACMuptimeAlarm * ap);
156 
158 typedef struct sBSPACMuptimeAlarm {
162 
170  unsigned int interval_utt;
172 
175 
181 typedef struct sBSPACMuptimeState {
188  volatile unsigned int overflows;
189 
196  unsigned int last_overflows;
197 
199  hBSPACMuptimeAlarm volatile alarm[BSPACM_UPTIME_CC_COUNT];
200 
202  bool enabled;
204 
206 /* This doesn't exist. */
207 extern sBSPACMuptimeState xBSPACMuptimeState_;
225 int
226 iBSPACMuptimeAlarmSet (int ccidx,
227  unsigned int when_utt,
228  hBSPACMuptimeAlarm ap);
229 
242 hBSPACMuptimeAlarm
243 hBSPACMuptimeAlarmClear (int ccidx,
244  bool * pendingp);
245 
247 inline
248 unsigned long long
250 {
251  unsigned int prev_ofl;
252  unsigned int ctr24;
253 
254  /* Get a consistent pair of counter and overflow values */
255  do {
256  prev_ofl = xBSPACMuptimeState_.last_overflows;
257  ctr24 = BSPACM_UPTIME_RTC->COUNTER;
258  xBSPACMuptimeState_.last_overflows = xBSPACMuptimeState_.overflows;
259  } while (prev_ofl != xBSPACMuptimeState_.last_overflows);
260  return (((uint64_t)xBSPACMuptimeState_.last_overflows) << 24) | ctr24;
261 }
262 
264 inline
265 unsigned int
267 {
268  return ullBSPACMuptime();
269 }
270 
272 void
274 
277 inline
278 bool
280 {
281  return xBSPACMuptimeState_.enabled;
282 }
283 
288 void
290 
308 bool
309 bBSPACMuptimeSleep (unsigned int duration_utt);
310 
311 #ifdef __cplusplus
312 }
313 #endif /* __cplusplus */
314 
315 #endif /* BSPACM_DEVICE_NRF51_INTERNAL_UTILITY_UPTIME_H */
#define BSPACM_UPTIME_RTC
Definition: uptime.h:86
unsigned int uiBSPACMuptimeConvert_utt_ms(unsigned int dur_utt)
Definition: uptime.h:131
bool enabled
Definition: uptime.h:202
Common header included by all BSPACM leaf headers.
vBSPACMuptimeAlarmCallback_flih callback_flih
Definition: uptime.h:161
Definition: uptime.h:181
struct sBSPACMuptimeAlarm sBSPACMuptimeAlarm
sBSPACMuptimeAlarm * hBSPACMuptimeAlarm
Definition: uptime.h:174
unsigned int uiBSPACMuptime()
Definition: uptime.h:266
unsigned int uiBSPACMuptimeConvert_us_utt(unsigned int dur_us)
Definition: uptime.h:105
bool bBSPACMuptimeSleep(unsigned int duration_utt)
#define BSPACM_UPTIME_Hz
Definition: uptime.h:96
Definition: uptime.h:158
int iBSPACMuptimeAlarmSet(int ccidx, unsigned int when_utt, hBSPACMuptimeAlarm ap)
#define BSPACM_UPTIME_CC_COUNT
Definition: uptime.h:87
void(* vBSPACMuptimeAlarmCallback_flih)(int ccidx, struct sBSPACMuptimeAlarm *ap)
Definition: uptime.h:154
struct sBSPACMuptimeState sBSPACMuptimeState
void vBSPACMuptimeStart()
unsigned int last_overflows
Definition: uptime.h:196
unsigned long long ullBSPACMuptime()
Definition: uptime.h:249
unsigned int uiBSPACMuptimeConvert_ms_utt(unsigned int dur_ms)
Definition: uptime.h:114
bool bBSPACMuptimeEnabled()
Definition: uptime.h:279
void vBSPACMuptimeSleepCancel(void)
volatile unsigned int overflows
Definition: uptime.h:188
hBSPACMuptimeAlarm hBSPACMuptimeAlarmClear(int ccidx, bool *pendingp)
hBSPACMuptimeAlarm volatile alarm[BSPACM_UPTIME_CC_COUNT]
Definition: uptime.h:199
unsigned int interval_utt
Definition: uptime.h:170
unsigned int uiBSPACMuptimeConvert_utt_us(unsigned int dur_utt)
Definition: uptime.h:123