2 * This file is part of INAV.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
6 * You can obtain one at http://mozilla.org/MPL/2.0/.
8 * Alternatively, the contents of this file may be used under the terms
9 * of the GNU General Public License Version 3, as described below:
11 * This file is free software: you may copy, redistribute and/or modify
12 * it under the terms of the GNU General Public License as published by the
13 * Free Software Foundation, either version 3 of the License, or (at your
14 * option) any later version.
16 * This file is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19 * Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see http://www.gnu.org/licenses/.
31 #include "build/atomic.h"
33 #include "drivers/nvic.h"
34 #include "drivers/time.h"
36 // cycles per microsecond, this is deliberately uint32_t to avoid type conversions
37 // This is not static so system.c can set it up for us.
40 // current uptime for 1kHz systick timer. will rollover after 49 days. hopefully we won't care.
41 STATIC_UNIT_TESTED
volatile timeMs_t sysTickUptime
= 0;
42 STATIC_UNIT_TESTED
volatile uint32_t sysTickValStamp
= 0;
44 // Return system uptime in milliseconds (rollover in 49 days)
52 static volatile int sysTickPending
= 0;
54 void SysTick_Handler(void)
56 ATOMIC_BLOCK(NVIC_PRIO_MAX
) {
58 sysTickValStamp
= SysTick
->VAL
;
60 (void)(SysTick
->CTRL
);
63 // used by the HAL for some timekeeping and timeouts, should always be 1ms
77 void delayNanos(timeDelta_t ns
)
79 const uint32_t startTicks
= ticks();
80 const uint32_t ticksToWait
= (ns
* usTicks
) / 1000;
81 while (ticks() - startTicks
<= ticksToWait
);
84 // Return system uptime in microseconds
85 timeUs_t
microsISR(void)
87 register uint32_t ms
, pending
, cycle_cnt
;
89 ATOMIC_BLOCK(NVIC_PRIO_MAX
) {
90 cycle_cnt
= SysTick
->VAL
;
92 if (SysTick
->CTRL
& SysTick_CTRL_COUNTFLAG_Msk
) {
94 // Record it for multiple calls within the same rollover period
95 // (Will be cleared when serviced).
96 // Note that multiple rollovers are not considered.
100 // Read VAL again to ensure the value is read after the rollover.
102 cycle_cnt
= SysTick
->VAL
;
106 pending
= sysTickPending
;
109 // XXX: Be careful to not trigger 64 bit division
110 const uint32_t partial
= (usTicks
* 1000U - cycle_cnt
) / usTicks
;
111 return ((timeUs_t
)(ms
+ pending
) * 1000LL) + ((timeUs_t
)partial
);
114 timeUs_t
micros(void)
116 register uint32_t ms
, cycle_cnt
;
118 // Call microsISR() in interrupt and elevated (non-zero) BASEPRI context
121 if ((SCB
->ICSR
& SCB_ICSR_VECTACTIVE_Msk
) || (__get_BASEPRI())) {
128 cycle_cnt
= SysTick
->VAL
;
129 } while (ms
!= sysTickUptime
|| cycle_cnt
> sysTickValStamp
);
131 // XXX: Be careful to not trigger 64 bit division
132 const uint32_t partial
= (usTicks
* 1000U - cycle_cnt
) / usTicks
;
133 return ((timeUs_t
)ms
* 1000LL) + ((timeUs_t
)partial
);
137 void delayMicroseconds(timeUs_t us
)
139 timeUs_t now
= micros();
140 while (micros() - now
< us
);
143 void delayMicroseconds(timeUs_t us
)
145 uint32_t elapsed
= 0;
146 uint32_t lastCount
= SysTick
->VAL
;
149 register uint32_t current_count
= SysTick
->VAL
;
152 // measure the time elapsed since the last time we checked
153 elapsed
+= current_count
- lastCount
;
154 lastCount
= current_count
;
156 // convert to microseconds
157 elapsed_us
= elapsed
/ usTicks
;
158 if (elapsed_us
>= us
)
161 // reduce the delay by the elapsed time
164 // keep fractional microseconds for the next iteration
170 void delay(timeMs_t ms
)
173 delayMicroseconds(1000);