JM9 XRCU Board 0.1.1
Libraries API Reference
Loading...
Searching...
No Matches
wiring_time.h
1/*
2 Copyright (c) 2011 Arduino. All right reserved.
3 Copyright (c) 2013 by Paul Stoffregen <paul@pjrc.com> (delayMicroseconds)
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19
20#ifndef _WIRING_TIME_H_
21#define _WIRING_TIME_H_
22
23#include "clock.h"
24#include "dwt.h"
25#include <sys/time.h> // for struct timeval
26
27#ifdef __cplusplus
28extern "C" {
29#endif
37extern uint32_t millis(void) ;
38
49extern uint32_t micros(void) ;
50
57extern void delay(uint32_t ms) ;
58
64static inline void delayMicroseconds(uint32_t) __attribute__((always_inline, unused));
65static inline void delayMicroseconds(uint32_t us)
66{
67#if defined(DWT_BASE) && !defined(DWT_DELAY_DISABLED)
68 int32_t start = dwt_getCycles();
69 int32_t cycles = us * (SystemCoreClock / 1000000);
70
71 while ((int32_t)dwt_getCycles() - start < cycles);
72#else
73 __IO uint32_t currentTicks = SysTick->VAL;
74 /* Number of ticks per millisecond */
75 const uint32_t tickPerMs = SysTick->LOAD + 1;
76 /* Number of ticks to count */
77 const uint32_t nbTicks = ((us - ((us > 0) ? 1 : 0)) * tickPerMs) / 1000;
78 /* Number of elapsed ticks */
79 uint32_t elapsedTicks = 0;
80 __IO uint32_t oldTicks = currentTicks;
81 do {
82 currentTicks = SysTick->VAL;
83 elapsedTicks += (oldTicks < currentTicks) ? tickPerMs + oldTicks - currentTicks :
84 oldTicks - currentTicks;
85 oldTicks = currentTicks;
86 } while (nbTicks > elapsedTicks);
87#endif
88}
89
103int __attribute__((weak)) _gettimeofday(struct timeval *tv, void *tz)
104{
105 (void)tz;
106 tv->tv_sec = getCurrentMillis() / 1000;
107 tv->tv_usec = getCurrentMicros() - (tv->tv_sec * 1000000); // get remaining microseconds
108 return 0;
109}
110
111#ifdef __cplusplus
112}
113#endif
114
115#endif /* _WIRING_TIME_H_ */