Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #16: advance clock in millis() if needed #19

Merged
merged 1 commit into from
Nov 19, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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