Skip to content

Commit

Permalink
Reinstate "Fix inaccurate ticks in windows port"
Browse files Browse the repository at this point in the history
Reinstates PR #142 that was reverted in #143
  • Loading branch information
Ben Nicholls committed Jan 8, 2025
1 parent 3a7b308 commit 47c47a5
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions portable/MSVC-MingW/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )
{
TickType_t xMinimumWindowsBlockTime;
TIMECAPS xTimeCaps;
TickType_t xWaitTimeBetweenTicks = portTICK_PERIOD_MS;
HANDLE hTimer = NULL;
LARGE_INTEGER liDueTime;

/* Set the timer resolution to the maximum possible. */
if( timeGetDevCaps( &xTimeCaps, sizeof( xTimeCaps ) ) == MMSYSERR_NOERROR )
Expand All @@ -160,22 +163,33 @@ static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )
/* Just to prevent compiler warnings. */
( void ) lpParameter;

/* Tick time for the timer is adjusted with the maximum available
resolution. */
if( portTICK_PERIOD_MS < xMinimumWindowsBlockTime )
{
xWaitTimeBetweenTicks = xMinimumWindowsBlockTime;
}

/* Convert the tick time in milliseconds to nanoseconds resolution
for the Waitable Timer. */
liDueTime.u.LowPart = xWaitTimeBetweenTicks * 1000 * 1000;
liDueTime.u.HighPart = 0;

/* Create a synchronization Waitable Timer.*/
hTimer = CreateWaitableTimer( NULL, FALSE, NULL );

configASSERT( hTimer != NULL );

/* Set the Waitable Timer. The timer is set to run periodically at every
xWaitTimeBetweenTicks milliseconds. */
configASSERT( SetWaitableTimer( hTimer, &liDueTime, xWaitTimeBetweenTicks, NULL, NULL, 0 ) );

while( xPortRunning == pdTRUE )
{
/* Wait until the timer expires and we can access the simulated interrupt
* variables. *NOTE* this is not a 'real time' way of generating tick
* events as the next wake time should be relative to the previous wake
* time, not the time that Sleep() is called. It is done this way to
* prevent overruns in this very non real time simulated/emulated
* environment. */
if( portTICK_PERIOD_MS < xMinimumWindowsBlockTime )
{
Sleep( xMinimumWindowsBlockTime );
}
else
{
Sleep( portTICK_PERIOD_MS );
}
* variables. */

WaitForSingleObject( hTimer, INFINITE );

vPortGenerateSimulatedInterruptFromWindowsThread( portINTERRUPT_TICK );
}
Expand Down

0 comments on commit 47c47a5

Please sign in to comment.