Skip to content

Commit 6443bed

Browse files
committed
[Time] Use DWT cycle counter for delayMicroseconds
Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 6fd56ab commit 6443bed

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

cores/arduino/wiring_time.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define _WIRING_TIME_H_
2222

2323
#include "clock.h"
24+
#include "dwt.h"
2425

2526
#ifdef __cplusplus
2627
extern "C" {
@@ -57,14 +58,20 @@ extern void delay(uint32_t dwMs) ;
5758
/**
5859
* \brief Pauses the program for the amount of time (in microseconds) specified as parameter.
5960
*
60-
* \param dwUs the number of microseconds to pause (uint32_t)
61+
* \param us the number of microseconds to pause (uint32_t)
6162
*/
6263
static inline void delayMicroseconds(uint32_t) __attribute__((always_inline, unused));
63-
static inline void delayMicroseconds(uint32_t usec)
64+
static inline void delayMicroseconds(uint32_t us)
6465
{
65-
uint32_t start = GetCurrentMicro();
66+
#if defined(DWT_BASE) && !defined(DWT_DELAY_DISABLED)
67+
int32_t start = dwt_getCycles();
68+
int32_t cycles = us * (SystemCoreClock / 1000000);
6669

70+
while ((int32_t)dwt_getCycles() - start < cycles);
71+
#else
72+
uint32_t start = GetCurrentMicro();
6773
while ((start + usec) > GetCurrentMicro());
74+
#endif
6875
}
6976

7077
#ifdef __cplusplus

0 commit comments

Comments
 (0)