Skip to content

Commit 7c56708

Browse files
cobusveAlfred Gedeon
authored and
Alfred Gedeon
committed
Fix inaccurate ticks in windows port (FreeRTOS#142)
1 parent e76762f commit 7c56708

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

portable/MSVC-MingW/port.c

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
* https://www.FreeRTOS.org
2323
* https://github.com/FreeRTOS
2424
*
25-
* 1 tab == 4 spaces!
2625
*/
2726

2827
/* Standard includes. */
@@ -140,6 +139,9 @@ static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )
140139
{
141140
TickType_t xMinimumWindowsBlockTime;
142141
TIMECAPS xTimeCaps;
142+
TickType_t xWaitTimeBetweenTicks = portTICK_PERIOD_MS;
143+
HANDLE hTimer = NULL;
144+
LARGE_INTEGER liDueTime;
143145

144146
/* Set the timer resolution to the maximum possible. */
145147
if( timeGetDevCaps( &xTimeCaps, sizeof( xTimeCaps ) ) == MMSYSERR_NOERROR )
@@ -159,22 +161,32 @@ TIMECAPS xTimeCaps;
159161
/* Just to prevent compiler warnings. */
160162
( void ) lpParameter;
161163

164+
/* Tick time for the timer is adjusted with the maximum available
165+
resolution. */
166+
if( portTICK_PERIOD_MS < xMinimumWindowsBlockTime )
167+
{
168+
xWaitTimeBetweenTicks = xMinimumWindowsBlockTime;
169+
}
170+
171+
/* Convert the tick time in milliseconds to nanoseconds resolution
172+
for the Waitable Timer. */
173+
liDueTime.u.LowPart = xWaitTimeBetweenTicks * 1000 * 1000;
174+
liDueTime.u.HighPart = 0;
175+
176+
/* Create a synchronization Waitable Timer.*/
177+
hTimer = CreateWaitableTimer( NULL, FALSE, NULL );
178+
179+
configASSERT( hTimer != NULL );
180+
181+
/* Set the Waitable Timer. The timer is set to run periodically at every
182+
xWaitTimeBetweenTicks milliseconds. */
183+
configASSERT( SetWaitableTimer( hTimer, &liDueTime, xWaitTimeBetweenTicks, NULL, NULL, 0 ) );
184+
162185
for( ;; )
163186
{
164187
/* Wait until the timer expires and we can access the simulated interrupt
165-
variables. *NOTE* this is not a 'real time' way of generating tick
166-
events as the next wake time should be relative to the previous wake
167-
time, not the time that Sleep() is called. It is done this way to
168-
prevent overruns in this very non real time simulated/emulated
169-
environment. */
170-
if( portTICK_PERIOD_MS < xMinimumWindowsBlockTime )
171-
{
172-
Sleep( xMinimumWindowsBlockTime );
173-
}
174-
else
175-
{
176-
Sleep( portTICK_PERIOD_MS );
177-
}
188+
variables. */
189+
WaitForSingleObject( hTimer, INFINITE );
178190

179191
configASSERT( xPortRunning );
180192

0 commit comments

Comments
 (0)