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