Skip to content
Snippets Groups Projects
Commit 56c096bd authored by Cristian Maglie's avatar Cristian Maglie Committed by Cristian Maglie
Browse files

Fixed delayMicrosecond for non-constant case.

parent 2cba1137
No related branches found
No related tags found
No related merge requests found
...@@ -61,40 +61,40 @@ extern void delay( uint32_t dwMs ) ; ...@@ -61,40 +61,40 @@ extern void delay( uint32_t dwMs ) ;
* \param dwUs the number of microseconds to pause (uint32_t) * \param dwUs the number of microseconds to pause (uint32_t)
*/ */
static __inline__ void delayMicroseconds( uint32_t ) __attribute__((always_inline, unused)) ; static __inline__ void delayMicroseconds( uint32_t ) __attribute__((always_inline, unused)) ;
static __inline__ void delayMicroseconds( uint32_t ul_usec ) static __inline__ void delayMicroseconds( uint32_t usec )
{ {
if ( ul_usec == 0 ) if ( usec == 0 )
{ {
return ; return ;
} }
uint32_t ul = ul_usec * (VARIANT_MCK / 3000000); // = ul_usec * 16 with VARIANT_MCK @ 48MHz /*
* The following loop:
/* following 'for' loop will generate this: *
* loop: * for (; ul; ul--) {
* subs r3, #1 // 1 Core cycle * __asm__ volatile("");
* bne.n loop // 1 or 2 Core cycles, depends on pipeline break (50/50?) * }
*
for ( ; ul != 0 ; ul-- ) * produce the following assembly code:
{ *
__asm__ volatile(""); * loop:
} * subs r3, #1 // 1 Core cycle
*/ * bne.n loop // 1 Core cycle + 1 if branch is taken
*/
/*
* __asm__ [__volatile__] ( AssemblerTemplate : [OutputOperands] [ : [InputOperands] [ : [Clobbers] ] ] ) // VARIANT_MCK / 1000000 == cycles needed to delay 1uS
* // 3 == cycles used in a loop
* __asm__ documentation for GCC: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html uint32_t n = usec * (VARIANT_MCK / 1000000) / 3;
* __volatile__ documentation for GCC: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Volatile __asm__ __volatile__(
* "1: \n"
*/ " sub %0, #1 \n" // substract 1 from %0 (n)
__asm__ __volatile__( "1: \n" " bne 1b \n" // if result is not 0 jump to 1
"sub %0, #1 \n" /* substract 1 from %0 (ul) */ : "+r" (n) // '%0' is n variable with RW constraints
"bne 1b \n" : // no input
: /* no outputs */ : // no clobber
: "r" (ul) /* 'ul' variable is '%0', '+' means R/W constraint on this variable/register, 'r' means 'register' (versus 'm' for 'memory' */ );
: /* no clobber, ie nothing should be damaged by this asm code */ // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html
) ; // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Volatile
} }
#ifdef __cplusplus #ifdef __cplusplus
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment