BSP430  20141115
Board Support Package for MSP430 microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
uptime.h
Go to the documentation of this file.
1 /* Copyright 2012-2014, 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 
111 #ifndef BSP430_UTILITY_UPTIME_H
112 #define BSP430_UTILITY_UPTIME_H
113 
114 #include <bsp430/periph/timer.h>
115 #include <bsp430/clock.h>
116 
125 #ifndef configBSP430_UPTIME
126 #define configBSP430_UPTIME 0
127 #endif /* configBSP430_UPTIME */
128 
134 #if defined(BSP430_DOXYGEN)
135 #define BSP430_UPTIME include <bsp430/platform.h>
136 #endif /* BSP430_DOXYGEN */
137 
151 #ifndef configBSP430_UPTIME_EPOCH
152 #define configBSP430_UPTIME_EPOCH 0
153 #endif /* configBSP430_UPTIME_EPOCH */
154 
168 #ifndef configBSP430_UPTIME_DELAY
169 #define configBSP430_UPTIME_DELAY 0
170 #endif /* configBSP430_UPTIME_DELAY */
171 
181 #if defined(BSP430_DOXYGEN)
182 #define BSP430_UPTIME_TIMER_PERIPH_CPPID include "bsp430_config.h"
183 #endif /* BSP430_DOXYGEN */
184 
195 #if defined(BSP430_DOXYGEN)
196 #define BSP430_UPTIME_TIMER_PERIPH_HANDLE platform or application specific
197 /* !BSP430! instance=@timers functional=uptime_timer subst=functional insert=periph_sethandle */
198 /* BEGIN AUTOMATICALLY GENERATED CODE---DO NOT MODIFY [periph_sethandle] */
199 
200 #elif BSP430_UPTIME_TIMER_PERIPH_CPPID == BSP430_PERIPH_CPPID_TA0
201 #define BSP430_UPTIME_TIMER_PERIPH_HANDLE BSP430_PERIPH_TA0
202 
203 #elif BSP430_UPTIME_TIMER_PERIPH_CPPID == BSP430_PERIPH_CPPID_TA1
204 #define BSP430_UPTIME_TIMER_PERIPH_HANDLE BSP430_PERIPH_TA1
205 
206 #elif BSP430_UPTIME_TIMER_PERIPH_CPPID == BSP430_PERIPH_CPPID_TA2
207 #define BSP430_UPTIME_TIMER_PERIPH_HANDLE BSP430_PERIPH_TA2
208 
209 #elif BSP430_UPTIME_TIMER_PERIPH_CPPID == BSP430_PERIPH_CPPID_TA3
210 #define BSP430_UPTIME_TIMER_PERIPH_HANDLE BSP430_PERIPH_TA3
211 
212 #elif BSP430_UPTIME_TIMER_PERIPH_CPPID == BSP430_PERIPH_CPPID_TB0
213 #define BSP430_UPTIME_TIMER_PERIPH_HANDLE BSP430_PERIPH_TB0
214 
215 #elif BSP430_UPTIME_TIMER_PERIPH_CPPID == BSP430_PERIPH_CPPID_TB1
216 #define BSP430_UPTIME_TIMER_PERIPH_HANDLE BSP430_PERIPH_TB1
217 
218 #elif BSP430_UPTIME_TIMER_PERIPH_CPPID == BSP430_PERIPH_CPPID_TB2
219 #define BSP430_UPTIME_TIMER_PERIPH_HANDLE BSP430_PERIPH_TB2
220 /* END AUTOMATICALLY GENERATED CODE [periph_sethandle] */
221 /* !BSP430! end=periph_sethandle */
222 #endif /* BSP430_UPTIME_TIMER_PERIPH_CPPID */
223 
245 #if defined(BSP430_DOXYGEN)
246 #define configBSP430_UPTIME_TIMER_HAL_CC0_ISR indirectly defaulted
247 #endif /* BSP430_DOXYGEN */
248 
255 #ifndef BSP430_UPTIME_TASSEL
256 #define BSP430_UPTIME_TASSEL TASSEL_1 /* == TASSEL__ACLK */
257 #endif /* BSP430_UPTIME_TASSEL */
258 
263 #ifndef BSP430_UPTIME_DIVIDING_SHIFT
264 #define BSP430_UPTIME_DIVIDING_SHIFT 0
265 #endif /* BSP430_UPTIME_DIVIDING_SHIFT */
266 
268 extern hBSP430halTIMER xBSP430uptimeTIMER_;
277 #if defined(BSP430_DOXYGEN) || (BSP430_UPTIME - 0)
278 static BSP430_CORE_INLINE
281 {
282  return xBSP430uptimeTIMER_;
283 }
284 #endif /* DOXYGEN or function available */
285 
286 #if (BSP430_UPTIME - 0)
287 
291 extern unsigned long ulBSP430uptimeConversionFrequency_Hz_ni_;
293 #endif /* BSP430_UPTIME */
294 
312 unsigned long ulBSP430uptimeConversionFrequency_Hz (void);
313 
330 unsigned long ulBSP430uptimeSetConversionFrequency_ni (unsigned long frequency_Hz);
331 
332 #if defined(BSP430_DOXYGEN) || (BSP430_UPTIME - 0)
333 
335 #define BSP430_UPTIME_MS_TO_UTT(ms_) BSP430_CORE_MS_TO_TICKS((ms_), ulBSP430uptimeConversionFrequency_Hz())
336 
338 #define BSP430_UPTIME_UTT_TO_MS(utt_) BSP430_CORE_TICKS_TO_MS((utt_), ulBSP430uptimeConversionFrequency_Hz())
339 
341 #define BSP430_UPTIME_US_TO_UTT(us_) BSP430_CORE_US_TO_TICKS((us_), ulBSP430uptimeConversionFrequency_Hz())
342 
344 #define BSP430_UPTIME_UTT_TO_US(utt_) BSP430_CORE_TICKS_TO_US((utt_), ulBSP430uptimeConversionFrequency_Hz())
345 #endif /* BSP430_UPTIME */
346 
347 #if defined(BSP430_DOXYGEN) || (BSP430_UPTIME - 0)
348 
359 static BSP430_CORE_INLINE
360 unsigned long
362 {
364 }
365 
367 static BSP430_CORE_INLINE
368 unsigned long
370 {
372 }
373 
379 static BSP430_CORE_INLINE
380 unsigned long long
382 {
384  unsigned int overflow;
385  unsigned long ul;
386 
388  do {
389  ul = ulBSP430timerCounter_ni(hBSP430uptimeTimer(), &overflow);
390  } while (0);
392  return (((unsigned long long)overflow) << (8 * sizeof(ul))) | ul;
393 }
394 
403 static BSP430_CORE_INLINE
404 unsigned long long
405 ullBSP430uptimeCorrected (unsigned int ctr)
406 {
408 }
409 
415 static BSP430_CORE_INLINE
416 unsigned int
418 {
421 }
422 
423 #endif /* BSP430_UPTIME */
424 
433 static BSP430_CORE_INLINE
434 bool bBSP430uptimeIsValid (unsigned long duration_utt)
435 {
436  return (0 != duration_utt) && (~0UL != duration_utt);
437 }
438 
450 static BSP430_CORE_INLINE
451 unsigned long ulBSP430uptimeValidated (unsigned long duration_utt)
452 {
453  if (! bBSP430uptimeIsValid(duration_utt)) {
454  duration_utt ^= 1;
455  }
456  return duration_utt;
457 }
458 
468 void vBSP430uptimeStart_ni (void);
469 
480 void vBSP430uptimeSuspend_ni (void);
481 
489 void vBSP430uptimeResume_ni (void);
490 
499 #define BSP430_UPTIME_AS_TEXT_LENGTH sizeof("HHH:MM:SS.mmm")
500 
517 const char * xBSP430uptimeAsText (unsigned long duration_utt,
518  char * buffer);
519 
532 const char * xBSP430uptimeAsText_ni (unsigned long duration_utt);
533 
557 unsigned int uiBSP430uptimeScaleForDisplay (unsigned long long duration_utt,
558  const char ** unitp);
559 
560 
572  unsigned long long awake_utt;
574  unsigned long long sleep_utt;
576  unsigned long sleeps;
579  unsigned long last_wake_utt;
581  unsigned long last_sleep_utt;
583 
617  bool from_now);
618 
619 #if defined(BSP430_DOXYGEN) || (BSP430_UPTIME - 0)
620 
626 static BSP430_CORE_INLINE
628 {
629  sp->sleep_utt += sp->last_wake_utt - sp->last_sleep_utt;
631  sp->awake_utt += sp->last_sleep_utt - sp->last_wake_utt;
632  sp->sleeps += 1;
633 }
634 
640 static BSP430_CORE_INLINE
642 {
644 }
645 
646 #endif /* BSP430_UPTIME */
647 
654 static BSP430_CORE_INLINE
656 {
657  unsigned long long period_tck = sp->awake_utt + sp->sleep_utt;
658  unsigned int duty_ppt = 0;
659  if (0 < period_tck) {
660  duty_ppt = (1000 * sp->awake_utt + (period_tck / 2)) / period_tck;
661  }
662  return duty_ppt;
663 }
664 
685 #if defined(BSP430_DOXYGEN) || (configBSP430_UPTIME_DELAY - 0)
686 #ifndef BSP430_UPTIME_DELAY_CCIDX
687 #define BSP430_UPTIME_DELAY_CCIDX 1
688 #endif /* BSP430_UPTIME_DELAY_CCIDX */
689 #endif /* BSP430_DOXYGEN */
690 
736 #if defined(BSP430_DOXYGEN) || (configBSP430_UPTIME_DELAY - 0)
737 long lBSP430uptimeSleepUntil (unsigned long setting_utt,
738  unsigned int lpm_bits);
739 #endif /* configBSP430_UPTIME_DELAY */
740 
749 #if defined(BSP430_DOXYGEN) || (configBSP430_UPTIME_DELAY - 0)
750 int iBSP430uptimeDelaySetEnabled_ni (int enablep);
751 #endif /* configBSP430_UPTIME_DELAY */
752 
769 #if defined(BSP430_DOXYGEN) || (configBSP430_UPTIME_DELAY - 0)
770 #define BSP430_UPTIME_DELAY_UTT_NI(delay_utt_, lpm_bits_, exit_expr_) do { \
771  unsigned long wake_utt; \
772  wake_utt = ulBSP430uptime_ni() + (delay_utt_); \
773  while ((! (exit_expr_)) && (0 < lBSP430uptimeSleepUntil(wake_utt, lpm_bits_))) { \
774  /* nop */ \
775  } \
776  } while (0)
777 #endif /* configBSP430_UPTIME_DELAY */
778 
783 #if defined(BSP430_DOXYGEN) || (configBSP430_UPTIME_DELAY - 0)
784 #define BSP430_UPTIME_DELAY_UTT(delay_utt_, lpm_bits_, exit_expr_) do { \
785  BSP430_CORE_SAVED_INTERRUPT_STATE(istate); \
786  BSP430_CORE_DISABLE_INTERRUPT(); \
787  BSP430_UPTIME_DELAY_UTT_NI(BSP430_UPTIME_UTT_TO_UTT(delay_utt_), lpm_bits_, exit_expr_); \
788  BSP430_CORE_RESTORE_INTERRUPT_STATE(istate); \
789  } while (0)
790 #endif /* configBSP430_UPTIME_DELAY */
791 
806 #if defined(BSP430_DOXYGEN) || (configBSP430_UPTIME_DELAY - 0)
807 #define BSP430_UPTIME_DELAY_MS_NI(delay_ms_, lpm_bits_, exit_expr_) do { \
808  BSP430_UPTIME_DELAY_UTT_NI(BSP430_UPTIME_MS_TO_UTT(delay_ms_), lpm_bits_, exit_expr_); \
809  } while (0)
810 #endif /* configBSP430_UPTIME_DELAY */
811 
816 #if defined(BSP430_DOXYGEN) || (configBSP430_UPTIME_DELAY - 0)
817 #define BSP430_UPTIME_DELAY_MS(delay_ms_, lpm_bits_, exit_expr_) do { \
818  BSP430_CORE_SAVED_INTERRUPT_STATE(istate); \
819  BSP430_CORE_DISABLE_INTERRUPT(); \
820  BSP430_UPTIME_DELAY_UTT_NI(BSP430_UPTIME_MS_TO_UTT(delay_ms_), lpm_bits_, exit_expr_); \
821  BSP430_CORE_RESTORE_INTERRUPT_STATE(istate); \
822  } while (0)
823 #endif /* configBSP430_UPTIME_DELAY */
824 
825 #if defined(BSP430_DOXYGEN) || (configBSP430_UPTIME_EPOCH - 0)
826 
827 #include <sys/time.h>
828 
834 #define BSP430_UPTIME_POSIX_EPOCH_NTPIS 2208988800UL
835 
847 #define BSP430_UPTIME_BYPASS_EPOCH_NTP ((uint64_t)(BSP430_UPTIME_POSIX_EPOCH_NTPIS + 1388534400UL) << 32)
848 
856  uint16_t integral;
857  uint16_t fractional;
859 
867  uint32_t integral;
868  uint32_t fractional;
870 
883  uint8_t li_vn_mode;
884  uint8_t stratum;
885  uint8_t ppoll;
886  int8_t precision;
889  uint32_t refid;
895 
902 
907 unsigned long ulBSP430uptimeLastEpochUpdate ();
908 
916 long lBSP430uptimeEpochAge ();
917 
923 #define BSP430_UPTIME_EPOCH_UPDATE_INTERVAL_UTT 0x40000000L
924 
931 #define BSP430_UPTIME_EPOCH_VALID_OFFSET_UTT 0x60000000L
932 
951 int iBSP430uptimeEpochEra (unsigned long time_utt);
952 
964 
982  unsigned long * putt);
983 
988 int iBSP430uptimeSetEpochFromNTP (uint64_t epoch_ntp);
989 
998 int iBSP430uptimeAdjustEpochFromNTP (int64_t adjustment_ntp);
999 
1011 int iBSP430uptimeSetEpochFromTimeval (const struct timeval * tv,
1012  unsigned long when_utt);
1013 
1036 int iBSP430uptimeAsNTP (unsigned long utt,
1037  uint64_t * ntpp,
1038  int bypass_validation);
1039 
1053 int iBSP430uptimeAsTimeval (unsigned long utt,
1054  struct timeval * tv);
1055 
1067 time_t xBSP430uptimeAsPOSIXTime (unsigned long utt);
1068 
1124  const sBSP430uptimeNTPPacketHeader * resp,
1125  uint64_t rec_ntp,
1126  int64_t * adjustment_ntp,
1127  long * adjustment_ms,
1128  unsigned long * rtt_us);
1129 
1130 #endif /* configBSP430_UPTIME_EPOCH */
1131 
1132 #endif /* BSP430_UTILITY_UPTIME_H */
sBSP430uptimeNTPShortFormat rootdelay
Definition: uptime.h:887
unsigned long ulBSP430timerCounter_ni(hBSP430halTIMER timer, unsigned int *overflowp)
unsigned long ulBSP430uptimeSetConversionFrequency_ni(unsigned long frequency_Hz)
unsigned long sleeps
Definition: uptime.h:576
void vBSP430uptimeResume_ni(void)
static BSP430_CORE_INLINE unsigned int uiBSP430uptimeCounter_ni(void)
Definition: uptime.h:417
#define BSP430_CORE_RESTORE_INTERRUPT_STATE(state_)
Definition: core.h:731
struct sBSP430uptimeNTPPacketHeader sBSP430uptimeNTPPacketHeader
int iBSP430uptimeSetEpochFromTimeval(const struct timeval *tv, unsigned long when_utt)
unsigned long long awake_utt
Definition: uptime.h:572
unsigned long ulBSP430uptimeLastEpochUpdate()
sBSP430uptimeNTPTimestamp org
Definition: uptime.h:891
static BSP430_CORE_INLINE unsigned int uiBSP430uptimeActivity_ppt(sBSP430uptimeActivityTotals *sp)
Definition: uptime.h:655
unsigned long last_sleep_utt
Definition: uptime.h:581
Definition: uptime.h:882
struct sBSP430uptimeNTPTimestamp sBSP430uptimeNTPTimestamp
unsigned int uiBSP430uptimeScaleForDisplay(unsigned long long duration_utt, const char **unitp)
static BSP430_CORE_INLINE unsigned long long ullBSP430timerCorrected(hBSP430halTIMER timer, unsigned int ctr)
Definition: timer.h:1308
sBSP430uptimeNTPTimestamp rec
Definition: uptime.h:892
void vBSP430uptimeActivityReset(sBSP430uptimeActivityTotals *sp, bool from_now)
unsigned long long sleep_utt
Definition: uptime.h:574
static BSP430_CORE_INLINE unsigned long ulBSP430timerCounter(hBSP430halTIMER timer, unsigned int *overflowp)
Definition: timer.h:1279
struct sBSP430uptimeActivityTotals sBSP430uptimeActivityTotals
Clock-related functions implemented on all MSP430 MCUs.
Hardware presentation/abstraction for generic timers (Timer_A/Timer_B)
uint8_t stratum
Definition: uptime.h:884
Definition: uptime.h:855
int iBSP430uptimeAsNTP(unsigned long utt, uint64_t *ntpp, int bypass_validation)
void vBSP430uptimeStart_ni(void)
sBSP430uptimeNTPShortFormat rootdisp
Definition: uptime.h:888
const char * xBSP430uptimeAsText(unsigned long duration_utt, char *buffer)
int iBSP430uptimeSetEpochFromNTP(uint64_t epoch_ntp)
long lBSP430uptimeEpochAge()
static BSP430_CORE_INLINE hBSP430halTIMER hBSP430uptimeTimer(void)
Definition: uptime.h:280
uint32_t fractional
Definition: uptime.h:868
static BSP430_CORE_INLINE unsigned long ulBSP430uptimeValidated(unsigned long duration_utt)
Definition: uptime.h:451
int8_t precision
Definition: uptime.h:886
uint16_t fractional
Definition: uptime.h:857
static BSP430_CORE_INLINE void vBSP430uptimeActivityStartActive_ni(sBSP430uptimeActivityTotals *sp)
Definition: uptime.h:641
static BSP430_CORE_INLINE unsigned long long ullBSP430uptime(void)
Definition: uptime.h:381
#define BSP430_CORE_INLINE
Definition: core.h:439
sBSP430hplHALStatePrefix hal_state
Definition: timer.h:1133
long lBSP430uptimeSleepUntil(unsigned long setting_utt, unsigned int lpm_bits)
static BSP430_CORE_INLINE void vBSP430uptimeActivityStartSleep_ni(sBSP430uptimeActivityTotals *sp)
Definition: uptime.h:627
#define BSP430_CORE_SAVED_INTERRUPT_STATE(var_)
Definition: core.h:719
volatile unsigned char flags
Definition: periph.h:285
sBSP430uptimeNTPTimestamp xmt
Definition: uptime.h:893
uint32_t integral
Definition: uptime.h:867
static BSP430_CORE_INLINE unsigned long long ullBSP430uptimeCorrected(unsigned int ctr)
Definition: uptime.h:405
unsigned long ulBSP430uptimeConversionFrequency_Hz(void)
static BSP430_CORE_INLINE_FORCED unsigned int uiBSP430timerBestCounterRead_ni(volatile sBSP430hplTIMER *const hpl, int flags)
Definition: timer.h:3578
Definition: uptime.h:570
int iBSP430uptimeProcessNTPResponse(const sBSP430uptimeNTPPacketHeader *req, const sBSP430uptimeNTPPacketHeader *resp, uint64_t rec_ntp, int64_t *adjustment_ntp, long *adjustment_ms, unsigned long *rtt_us)
void vBSP430uptimeSuspend_ni(void)
uint32_t refid
Definition: uptime.h:889
int iBSP430uptimeDelaySetEnabled_ni(int enablep)
uint8_t ppoll
Definition: uptime.h:885
int iBSP430uptimeAsTimeval(unsigned long utt, struct timeval *tv)
struct sBSP430uptimeNTPShortFormat sBSP430uptimeNTPShortFormat
#define BSP430_CORE_DISABLE_INTERRUPT()
Definition: core.h:762
static BSP430_CORE_INLINE bool bBSP430uptimeIsValid(unsigned long duration_utt)
Definition: uptime.h:434
int iBSP430uptimeSetNTPXmtField(sBSP430uptimeNTPPacketHeader *ntpp, unsigned long *putt)
volatile sBSP430hplTIMER *const hpl
Definition: timer.h:1136
Definition: uptime.h:866
uint16_t integral
Definition: uptime.h:856
unsigned long last_wake_utt
Definition: uptime.h:579
int iBSP430uptimeAdjustEpochFromNTP(int64_t adjustment_ntp)
int iBSP430uptimeCheckEpochValidity()
int iBSP430uptimeEpochEra(unsigned long time_utt)
static BSP430_CORE_INLINE unsigned long ulBSP430uptime_ni(void)
Definition: uptime.h:361
time_t xBSP430uptimeAsPOSIXTime(unsigned long utt)
int iBSP430uptimeInitializeNTPRequest(sBSP430uptimeNTPPacketHeader *ntpp)
Definition: timer.h:1129
const char * xBSP430uptimeAsText_ni(unsigned long duration_utt)
uint8_t li_vn_mode
Definition: uptime.h:883
sBSP430uptimeNTPTimestamp reftime
Definition: uptime.h:890
static BSP430_CORE_INLINE unsigned long ulBSP430uptime(void)
Definition: uptime.h:369