Skip to content
Snippets Groups Projects
Commit 919bd675 authored by Thibaut VIARD's avatar Thibaut VIARD
Browse files

Adding Delay API

parent 15bae0de
No related branches found
No related tags found
No related merge requests found
......@@ -5,12 +5,13 @@
extern "C" {
#endif
/** Tick Counter united by ms */
static volatile uint32_t _ulTickCount=0 ;
uint32_t millis( void )
{
/* TODO
// todo: ensure no interrupts
return GetTickCount() ;
*/ return 0ul ;
return _ulTickCount ;
}
// Interrupt-compatible version of micros
......@@ -19,28 +20,27 @@ uint32_t millis( void )
// values to calculate micros. If there is a pending SysTick, add one to the millis counter in the calculation.
uint32_t micros( void )
{
/* TODO
uint32_t ticks, ticks2;
uint32_t pend, pend2;
uint32_t count, count2;
uint32_t ticks, ticks2;
uint32_t pend, pend2;
uint32_t count, count2;
ticks2 = SysTick->VAL;
pend2 = !!((SCB->ICSR & SCB_ICSR_PENDSTSET_Msk)||((SCB->SHCSR & SCB_SHCSR_SYSTICKACT_Msk))) ;
count2 = GetTickCount();
ticks2 = SysTick->VAL;
pend2 = !!(SCB->ICSR & SCB_ICSR_PENDSTSET_Msk) ;
count2 = _ulTickCount ;
do {
ticks=ticks2;
pend=pend2;
count=count2;
ticks2 = SysTick->VAL;
pend2 = !!((SCB->ICSR & SCB_ICSR_PENDSTSET_Msk)||((SCB->SHCSR & SCB_SHCSR_SYSTICKACT_Msk))) ;
count2 = GetTickCount();
} while ((pend != pend2) || (count != count2) || (ticks < ticks2));
do
{
ticks=ticks2;
pend=pend2;
count=count2;
ticks2 = SysTick->VAL;
pend2 = !!(SCB->ICSR & SCB_ICSR_PENDSTSET_Msk) ;
count2 = _ulTickCount ;
} while ((pend != pend2) || (count != count2) || (ticks < ticks2));
return ((count+pend) * 1000) + (((SysTick->LOAD - ticks)*(1048576/(F_CPU/1000000)))>>20) ;
// this is an optimization to turn a runtime division into two compile-time divisions and
// a runtime multiplication and shift, saving a few cycles
*/ return 0ul ;
return ((count+pend) * 1000) + (((SysTick->LOAD - ticks)*(1048576/(VARIANT_MCK/1000000)))>>20) ;
// this is an optimization to turn a runtime division into two compile-time divisions and
// a runtime multiplication and shift, saving a few cycles
}
// original function:
......@@ -61,14 +61,17 @@ uint32_t micros( void )
void delay( uint32_t ms )
{
/* TODO
if (ms == 0)
return;
uint32_t start = GetTickCount();
do {
yield();
} while (GetTickCount() - start < ms);
*/
if ( ms == 0 )
{
return ;
}
uint32_t start = _ulTickCount ;
do
{
yield() ;
} while ( _ulTickCount - start < ms ) ;
}
#ifdef __cplusplus
......
......@@ -64,16 +64,31 @@ static inline void delayMicroseconds(uint32_t) __attribute__((always_inline, unu
static inline void delayMicroseconds(uint32_t usec){
if (usec == 0) return;
uint32_t n = usec * (VARIANT_MCK / 3000000);
/*
asm volatile(
__asm__ volatile(
"L_%=_delayMicroseconds:" "\n\t"
"subs %0, #1" "\n\t"
"bne L_%=_delayMicroseconds" "\n"
: "+r" (n) :
);
*/
}
/*
__attribute__((naked)) static void delay_loop(unsigned n)
{
__asm volatile ("1: subs r0, r0, #1");
__asm volatile (" bne 1b");
__asm volatile (" bx lr");
}
void delay_microseconds(unsigned n)
{
// Bogus assumption:
// Assume 8 cycles/iteration and running at 80MHz
delay_loop(n * 10);
}
*/
#ifdef __cplusplus
}
#endif
......
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