Skip to content

Commit a222c14

Browse files
committed
DM: fix delay microseconds
1 parent aebe54b commit a222c14

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

cores/arduino/delay.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ static __inline__ void delayMicroseconds( uint32_t usec )
6868
return ;
6969
}
7070

71+
#if defined(__SAMD51__)
72+
uint32_t n = usec * (VARIANT_MCK / 1000000) / 12;
73+
74+
__asm__ __volatile__(
75+
"1: \n"
76+
" sub %0, #1 \n" // substract 1 from %0 (n)
77+
" cmp %0, #0 \n" // compare to 0
78+
" bne 1b \n" // if result is not 0 jump to 1
79+
: "+r" (n) // '%0' is n variable with RW constraints
80+
: // no input
81+
: // no clobber
82+
);
83+
84+
#else
7185
/*
7286
* The following loop:
7387
*
@@ -85,6 +99,7 @@ static __inline__ void delayMicroseconds( uint32_t usec )
8599
// VARIANT_MCK / 1000000 == cycles needed to delay 1uS
86100
// 3 == cycles used in a loop
87101
uint32_t n = usec * (VARIANT_MCK / 1000000) / 3;
102+
88103
__asm__ __volatile__(
89104
"1: \n"
90105
" sub %0, #1 \n" // substract 1 from %0 (n)
@@ -93,6 +108,7 @@ static __inline__ void delayMicroseconds( uint32_t usec )
93108
: // no input
94109
: // no clobber
95110
);
111+
#endif
96112
// https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html
97113
// https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Volatile
98114
}

0 commit comments

Comments
 (0)