Skip to content

Commit

Permalink
Fix #16: advance clock in millis() if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
terrillmoore committed Nov 18, 2018
1 parent 3a668be commit 3ff6267
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions cores/arduino/delay.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ static volatile uint32_t _ulTickCount=0 ;
uint32_t millis( void )
{
// todo: ensure no interrupts
if (__get_PRIMASK())
{
if (SCB->ICSR & SCB_ICSR_PENDSTSET_Msk)
{
SCB->ICSR = SCB_ICSR_PENDSTCLR_Msk; // clear interrupt
++_ulTickCount;
}
}
return _ulTickCount ;
}

Expand Down Expand Up @@ -67,7 +75,7 @@ uint32_t micros( void )

ticks2 = SysTick->VAL;
pend2 = !!(SCB->ICSR & SCB_ICSR_PENDSTSET_Msk) ;
count2 = _ulTickCount ;
count2 = millis() ;

do
{
Expand All @@ -76,7 +84,7 @@ uint32_t micros( void )
count=count2;
ticks2 = SysTick->VAL;
pend2 = !!(SCB->ICSR & SCB_ICSR_PENDSTSET_Msk) ;
count2 = _ulTickCount ;
count2 = millis() ;
} while ((pend != pend2) || (count != count2) || (ticks < ticks2));

return ((count+pend) * 1000) + (((SysTick->LOAD - ticks)*(1048576/(VARIANT_MCK/1000000)))>>20) ;
Expand All @@ -91,12 +99,12 @@ void delay( uint32_t ms )
return ;
}

uint32_t start = _ulTickCount ;
uint32_t start = millis() ;

do
{
yield() ;
} while ( _ulTickCount - start < ms ) ;
} while ( millis() - start < ms ) ;
}

#include "Reset.h" // for tickReset()
Expand Down

0 comments on commit 3ff6267

Please sign in to comment.