d_time.c
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright 2014-2019 Inertial Sense, Inc. - http://inertialsense.com
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
7 
8 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9 
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT, IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 #include <asf.h>
14 #include "d_time.h"
15 
16 
17 #include "conf_interrupts.h"
18 static volatile uint32_t g_rollover = 0;
19 static volatile uint32_t g_timer = 0;
20 
21 
22 void RTT_Handler(void)
23 {
24  uint32_t status = rtt_get_status(RTT);
25 
26  // time has changed
27  if (status & RTT_SR_RTTINC)
28  {
30  }
31 
32  // alarm
33  if (status & RTT_SR_ALMS)
34  {
35  g_rollover++;
36  }
37 }
38 
39 
40 void time_init(void)
41 {
42  static int initialized = 0; if (initialized) { return; } initialized = 1;
43 
44  // configure RTT to increment as frequently as possible
45  rtt_sel_source(RTT, false);
46 
52 
53 #ifdef NDEBUG
54 
55  // interrupt for each tick - release mode only, makes debugging impossible
57 
58 #endif
59 
60  // notify us when we rollover
62 }
63 
64 
65 inline volatile uint64_t time_ticks(void)
66 {
67 
68 #ifndef NDEBUG
69 
70  // in debug no timer interrupt so read directly from timer register, unsafe as this can cause corrupt time
71  // but is the only way to debug and step code
73 
74 #endif
75 
76  // this assumes little endian
77  volatile ticks_t ticks;
78  ticks.u32[1] = g_rollover;
79  ticks.u32[0] = g_timer;
80  return ticks.u64;
81 }
82 
83 
84 void time_delay(uint32_t ms)
85 {
86  if (ms != 0)
87  {
88  volatile uint64_t start = time_ticks();
89  volatile uint64_t ms64 = (uint64_t)ms * TIME_TICKS_PER_MS;
90  while (time_ticks() - start < ms64);
91  }
92 }
93 
94 
95 inline uint32_t time_msec(void)
96 {
97 
98 #ifndef NDEBUG
99 
100  // in debug no timer interrupt so read directly from timer register, unsafe as this can cause corrupt time
101  // but is the only way to debug and step code
103 
104 #endif
105 
106  return (uint32_t)((uint64_t)((double)g_timer * TIME_MS_PER_TICK_LF) & 0x00000000FFFFFFFF);
107 }
108 
109 
110 inline uint32_t time_usec(void)
111 {
112 
113 #ifndef NDEBUG
114 
115  // in debug no timer interrupt so read directly from timer register, unsafe as this can cause corrupt time
116  // but is the only way to debug and step code
118 
119 #endif
120 
121  return (uint32_t)((uint64_t)((double)g_timer * TIME_US_PER_TICK_LF) & 0x00000000FFFFFFFF);
122 }
123 
124 
125 inline float time_secf(void)
126 {
127  return TIME_SECS_PER_TICK_F * (float)time_ticks();
128 }
129 
130 
131 inline float time_msecf(void)
132 {
133  return TIME_MS_PER_TICK_F * (float)time_ticks();
134 }
135 
136 
137 inline float time_usecf(void)
138 {
139  return TIME_US_PER_TICK_F * (float)time_ticks();
140 }
141 
142 
143 inline double time_seclf(void)
144 {
145  return TIME_SECS_PER_TICK_LF * (double)time_ticks();
146 }
147 
148 
149 inline double time_mseclf(void)
150 {
151  return TIME_MS_PER_TICK_LF * (double)time_ticks();
152 }
153 
154 
155 inline double time_useclf(void)
156 {
157  return TIME_US_PER_TICK_LF * (double)time_ticks();
158 }
#define RTT_MR_RTTINCIEN
(RTT_MR) Real-time Timer Increment Interrupt Enable
double time_seclf(void)
Definition: d_time.c:143
double time_mseclf(void)
Definition: d_time.c:149
uint32_t time_msec(void)
Definition: d_time.c:95
#define RTT_SR_ALMS
(RTT_SR) Real-time Alarm Status (cleared on read)
ROSCPP_DECL void start()
#define TIME_US_PER_TICK_F
Definition: d_time.h:50
#define TIME_MS_PER_TICK_LF
Definition: d_time.h:45
void RTT_Handler(void)
Definition: d_time.c:22
uint32_t rtt_init(Rtt *p_rtt, uint16_t us_prescaler)
Initialize the given RTT.
Definition: rtt.c:78
void rtt_enable_interrupt(Rtt *p_rtt, uint32_t ul_sources)
Enable RTT interrupts.
Definition: rtt.c:157
double time_useclf(void)
Definition: d_time.c:155
#define TIME_TICKS_PER_MS
Definition: d_time.h:41
__STATIC_INLINE void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority)
Set Interrupt Priority.
Definition: core_cm7.h:1766
#define INT_PRIORITY_RTT
#define TIME_US_PER_TICK_LF
Definition: d_time.h:46
static volatile uint32_t g_timer
Definition: d_time.c:19
__STATIC_INLINE void NVIC_DisableIRQ(IRQn_Type IRQn)
Disable External Interrupt.
Definition: core_cm7.h:1696
uint32_t rtt_read_timer_value(Rtt *p_rtt)
Read the current value of the RTT timer value.
Definition: rtt.c:194
uint32_t u32[2]
Definition: d_time.h:55
#define RTT
(RTT ) Base Address
Definition: same70j19.h:534
#define TIME_SECS_PER_TICK_F
Definition: d_time.h:48
volatile uint64_t time_ticks(void)
Definition: d_time.c:65
#define RTT_SR_RTTINC
(RTT_SR) Prescaler Roll-over Status (cleared on read)
__STATIC_INLINE void NVIC_EnableIRQ(IRQn_Type IRQn)
Enable External Interrupt.
Definition: core_cm7.h:1683
Definition: d_time.h:53
#define RTPRES
Definition: d_time.h:37
uint32_t rtt_write_alarm_time(Rtt *p_rtt, uint32_t ul_alarm_time)
Configure the RTT to generate an alarm at the given time. alarm happens when CRTV value equals ALMV+1...
Definition: rtt.c:227
void time_init(void)
Definition: d_time.c:40
uint64_t u64
Definition: d_time.h:56
float time_msecf(void)
Definition: d_time.c:131
__STATIC_INLINE void NVIC_ClearPendingIRQ(IRQn_Type IRQn)
Clear Pending Interrupt.
Definition: core_cm7.h:1736
#define TIME_SECS_PER_TICK_LF
Definition: d_time.h:44
float time_usecf(void)
Definition: d_time.c:137
#define TIME_MS_PER_TICK_F
Definition: d_time.h:49
uint32_t time_usec(void)
Definition: d_time.c:110
uint32_t rtt_get_status(Rtt *p_rtt)
Get the status register value of the given RTT.
Definition: rtt.c:212
float time_secf(void)
Definition: d_time.c:125
void time_delay(uint32_t ms)
Definition: d_time.c:84
Autogenerated API include file for the Atmel Software Framework (ASF)
static volatile uint32_t g_rollover
Definition: d_time.c:18


inertial_sense_ros
Author(s):
autogenerated on Sat Sep 19 2020 03:19:04