forked from stm32duino/Arduino_Core_STM32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiring_time.h
115 lines (102 loc) · 3.86 KB
/
wiring_time.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Copyright (c) 2011 Arduino. All right reserved.
Copyright (c) 2013 by Paul Stoffregen <paul@pjrc.com> (delayMicroseconds)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _WIRING_TIME_H_
#define _WIRING_TIME_H_
#include "clock.h"
#include "dwt.h"
#include <sys/time.h> // for struct timeval
#ifdef __cplusplus
extern "C" {
#endif
/**
* \brief Returns the number of milliseconds since the Arduino board began running the current program.
*
* This number will overflow (go back to zero), after approximately 50 days.
*
* \return Number of milliseconds since the program started (uint32_t)
*/
extern uint32_t millis(void) ;
/**
* \brief Returns the number of microseconds since the Arduino board began running the current program.
*
* This number will overflow (go back to zero), after approximately 70 minutes. On 16 MHz Arduino boards
* (e.g. Duemilanove and Nano), this function has a resolution of four microseconds (i.e. the value returned is
* always a multiple of four). On 8 MHz Arduino boards (e.g. the LilyPad), this function has a resolution
* of eight microseconds.
*
* \note There are 1,000 microseconds in a millisecond and 1,000,000 microseconds in a second.
*/
extern uint32_t micros(void) ;
/**
* \brief Pauses the program for the amount of time (in milliseconds) specified as parameter.
* (There are 1000 milliseconds in a second.)
*
* \param ms the number of milliseconds to pause (uint32_t)
*/
extern void delay(uint32_t ms) ;
/**
* \brief Pauses the program for the amount of time (in microseconds) specified as parameter.
*
* \param us the number of microseconds to pause (uint32_t)
*/
static inline void delayMicroseconds(uint32_t) __attribute__((always_inline, unused));
static inline void delayMicroseconds(uint32_t us)
{
#if defined(DWT_BASE) && !defined(DWT_DELAY_DISABLED)
int32_t start = dwt_getCycles();
int32_t cycles = us * (SystemCoreClock / 1000000);
while ((int32_t)dwt_getCycles() - start < cycles);
#else
__IO uint32_t currentTicks = SysTick->VAL;
/* Number of ticks per millisecond */
const uint32_t tickPerMs = SysTick->LOAD + 1;
/* Number of ticks to count */
const uint32_t nbTicks = ((us - ((us > 0) ? 1 : 0)) * tickPerMs) / 1000;
/* Number of elapsed ticks */
uint32_t elapsedTicks = 0;
__IO uint32_t oldTicks = currentTicks;
do {
currentTicks = SysTick->VAL;
elapsedTicks += (oldTicks < currentTicks) ? tickPerMs + oldTicks - currentTicks :
oldTicks - currentTicks;
oldTicks = currentTicks;
} while (nbTicks > elapsedTicks);
#endif
}
/**
* \brief gives the number of seconds and microseconds since the Epoch
*
* based on millisecond since last power on.
*
* \note The function is declared as weak to be overwritten in case of other
* implementations in user file (using RTC values for example).
*
* \param tv argument is a struct timeval
* \param tz argument is a struct timezone (unused)
*
* \return 0
*/
int __attribute__((weak)) _gettimeofday(struct timeval *tv, void *tz)
{
(void)tz;
tv->tv_sec = getCurrentMillis() / 1000;
tv->tv_usec = getCurrentMicros() - (tv->tv_sec * 1000000); // get remaining microseconds
return 0;
}
#ifdef __cplusplus
}
#endif
#endif /* _WIRING_TIME_H_ */