2 FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /* Scheduler includes. */
\r
67 #include "FreeRTOS.h"
\r
72 #include "mmsystem.h"
\r
74 #pragma comment(lib, "winmm.lib")
\r
77 #define portMAX_INTERRUPTS ( ( unsigned long ) sizeof( unsigned long ) * 8UL ) /* The number of bits in an unsigned long. */
\r
78 #define portNO_CRITICAL_NESTING ( ( unsigned long ) 0 )
\r
81 * Created as a high priority thread, this function uses a timer to simulate
\r
82 * a tick interrupt being generated on an embedded target. In this Windows
\r
83 * environment the timer does not achieve anything approaching real time
\r
84 * performance though.
\r
86 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter );
\r
89 * Process all the simulated interrupts - each represented by a bit in
\r
90 * ulPendingInterrupts variable.
\r
92 static void prvProcessSimulatedInterrupts( void );
\r
95 * Interrupt handlers used by the kernel itself. These are executed from the
\r
96 * simulated interrupt handler thread.
\r
98 static unsigned long prvProcessYieldInterrupt( void );
\r
99 static unsigned long prvProcessTickInterrupt( void );
\r
102 * Called when the process exits to let Windows know the high timer resolution
\r
103 * is no longer required.
\r
105 static BOOL WINAPI prvEndProcess( DWORD dwCtrlType );
\r
107 /*-----------------------------------------------------------*/
\r
109 /* The WIN32 simulator runs each task in a thread. The context switching is
\r
110 managed by the threads, so the task stack does not have to be managed directly,
\r
111 although the task stack is still used to hold an xThreadState structure this is
\r
112 the only thing it will ever hold. The structure indirectly maps the task handle
\r
113 to a thread handle. */
\r
116 /* Handle of the thread that executes the task. */
\r
121 /* Simulated interrupts waiting to be processed. This is a bit mask where each
\r
122 bit represents one interrupt, so a maximum of 32 interrupts can be simulated. */
\r
123 static volatile unsigned long ulPendingInterrupts = 0UL;
\r
125 /* An event used to inform the simulated interrupt processing thread (a high
\r
126 priority thread that simulated interrupt processing) that an interrupt is
\r
128 static void *pvInterruptEvent = NULL;
\r
130 /* Mutex used to protect all the simulated interrupt variables that are accessed
\r
131 by multiple threads. */
\r
132 static void *pvInterruptEventMutex = NULL;
\r
134 /* The critical nesting count for the currently executing task. This is
\r
135 initialised to a non-zero value so interrupts do not become enabled during
\r
136 the initialisation phase. As each task has its own critical nesting value
\r
137 ulCriticalNesting will get set to zero when the first task runs. This
\r
138 initialisation is probably not critical in this simulated environment as the
\r
139 simulated interrupt handlers do not get created until the FreeRTOS scheduler is
\r
141 static unsigned long ulCriticalNesting = 9999UL;
\r
143 /* Handlers for all the simulated software interrupts. The first two positions
\r
144 are used for the Yield and Tick interrupts so are handled slightly differently,
\r
145 all the other interrupts can be user defined. */
\r
146 static unsigned long (*ulIsrHandler[ portMAX_INTERRUPTS ])( void ) = { 0 };
\r
148 /* Pointer to the TCB of the currently executing task. */
\r
149 extern void *pxCurrentTCB;
\r
151 /*-----------------------------------------------------------*/
\r
153 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )
\r
155 portTickType xMinimumWindowsBlockTime;
\r
156 TIMECAPS xTimeCaps;
\r
158 /* Set the timer resolution to the maximum possible. */
\r
159 if( timeGetDevCaps( &xTimeCaps, sizeof( xTimeCaps ) ) == MMSYSERR_NOERROR )
\r
161 xMinimumWindowsBlockTime = ( portTickType ) xTimeCaps.wPeriodMin;
\r
162 timeBeginPeriod( xTimeCaps.wPeriodMin );
\r
164 /* Register an exit handler so the timeBeginPeriod() function can be
\r
165 matched with a timeEndPeriod() when the application exits. */
\r
166 SetConsoleCtrlHandler( prvEndProcess, TRUE );
\r
170 xMinimumWindowsBlockTime = ( portTickType ) 20;
\r
173 /* Just to prevent compiler warnings. */
\r
174 ( void ) lpParameter;
\r
178 /* Wait until the timer expires and we can access the simulated interrupt
\r
179 variables. *NOTE* this is not a 'real time' way of generating tick
\r
180 events as the next wake time should be relative to the previous wake
\r
181 time, not the time that Sleep() is called. It is done this way to
\r
182 prevent overruns in this very non real time simulated/emulated
\r
184 if( portTICK_RATE_MS < xMinimumWindowsBlockTime )
\r
186 Sleep( xMinimumWindowsBlockTime );
\r
190 Sleep( portTICK_RATE_MS );
\r
193 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
195 /* The timer has expired, generate the simulated tick event. */
\r
196 ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );
\r
198 /* The interrupt is now pending - notify the simulated interrupt
\r
200 SetEvent( pvInterruptEvent );
\r
202 /* Give back the mutex so the simulated interrupt handler unblocks
\r
203 and can access the interrupt handler variables. */
\r
204 ReleaseMutex( pvInterruptEventMutex );
\r
208 /* Should never reach here - MingW complains if you leave this line out,
\r
209 MSVC complains if you put it in. */
\r
213 /*-----------------------------------------------------------*/
\r
215 static BOOL WINAPI prvEndProcess( DWORD dwCtrlType )
\r
217 TIMECAPS xTimeCaps;
\r
219 ( void ) dwCtrlType;
\r
221 if( timeGetDevCaps( &xTimeCaps, sizeof( xTimeCaps ) ) == MMSYSERR_NOERROR )
\r
223 /* Match the call to timeBeginPeriod( xTimeCaps.wPeriodMin ) made when
\r
224 the process started with a timeEndPeriod() as the process exits. */
\r
225 timeEndPeriod( xTimeCaps.wPeriodMin );
\r
230 /*-----------------------------------------------------------*/
\r
232 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
234 xThreadState *pxThreadState = NULL;
\r
235 char *pcTopOfStack = ( char * ) pxTopOfStack;
\r
237 /* In this simulated case a stack is not initialised, but instead a thread
\r
238 is created that will execute the task being created. The thread handles
\r
239 the context switching itself. The xThreadState object is placed onto
\r
240 the stack that was created for the task - so the stack buffer is still
\r
241 used, just not in the conventional way. It will not be used for anything
\r
242 other than holding this structure. */
\r
243 pxThreadState = ( xThreadState * ) ( pcTopOfStack - sizeof( xThreadState ) );
\r
245 /* Create the thread itself. */
\r
246 pxThreadState->pvThread = CreateThread( NULL, 0, ( LPTHREAD_START_ROUTINE ) pxCode, pvParameters, CREATE_SUSPENDED, NULL );
\r
247 SetThreadAffinityMask( pxThreadState->pvThread, 0x01 );
\r
248 SetThreadPriorityBoost( pxThreadState->pvThread, TRUE );
\r
249 SetThreadPriority( pxThreadState->pvThread, THREAD_PRIORITY_IDLE );
\r
251 return ( portSTACK_TYPE * ) pxThreadState;
\r
253 /*-----------------------------------------------------------*/
\r
255 portBASE_TYPE xPortStartScheduler( void )
\r
258 long lSuccess = pdPASS;
\r
259 xThreadState *pxThreadState;
\r
261 /* Install the interrupt handlers used by the scheduler itself. */
\r
262 vPortSetInterruptHandler( portINTERRUPT_YIELD, prvProcessYieldInterrupt );
\r
263 vPortSetInterruptHandler( portINTERRUPT_TICK, prvProcessTickInterrupt );
\r
265 /* Create the events and mutexes that are used to synchronise all the
\r
267 pvInterruptEventMutex = CreateMutex( NULL, FALSE, NULL );
\r
268 pvInterruptEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
\r
270 if( ( pvInterruptEventMutex == NULL ) || ( pvInterruptEvent == NULL ) )
\r
275 /* Set the priority of this thread such that it is above the priority of
\r
276 the threads that run tasks. This higher priority is required to ensure
\r
277 simulated interrupts take priority over tasks. */
\r
278 pvHandle = GetCurrentThread();
\r
279 if( pvHandle == NULL )
\r
284 if( lSuccess == pdPASS )
\r
286 if( SetThreadPriority( pvHandle, THREAD_PRIORITY_NORMAL ) == 0 )
\r
290 SetThreadPriorityBoost( pvHandle, TRUE );
\r
291 SetThreadAffinityMask( pvHandle, 0x01 );
\r
294 if( lSuccess == pdPASS )
\r
296 /* Start the thread that simulates the timer peripheral to generate
\r
297 tick interrupts. The priority is set below that of the simulated
\r
298 interrupt handler so the interrupt event mutex is used for the
\r
299 handshake / overrun protection. */
\r
300 pvHandle = CreateThread( NULL, 0, prvSimulatedPeripheralTimer, NULL, 0, NULL );
\r
301 if( pvHandle != NULL )
\r
303 SetThreadPriority( pvHandle, THREAD_PRIORITY_BELOW_NORMAL );
\r
304 SetThreadPriorityBoost( pvHandle, TRUE );
\r
305 SetThreadAffinityMask( pvHandle, 0x01 );
\r
308 /* Start the highest priority task by obtaining its associated thread
\r
309 state structure, in which is stored the thread handle. */
\r
310 pxThreadState = ( xThreadState * ) *( ( unsigned long * ) pxCurrentTCB );
\r
311 ulCriticalNesting = portNO_CRITICAL_NESTING;
\r
313 /* Bump up the priority of the thread that is going to run, in the
\r
314 hope that this will asist in getting the Windows thread scheduler to
\r
315 behave as an embedded engineer might expect. */
\r
316 ResumeThread( pxThreadState->pvThread );
\r
318 /* Handle all simulated interrupts - including yield requests and
\r
319 simulated ticks. */
\r
320 prvProcessSimulatedInterrupts();
\r
323 /* Would not expect to return from prvProcessSimulatedInterrupts(), so should
\r
327 /*-----------------------------------------------------------*/
\r
329 static unsigned long prvProcessYieldInterrupt( void )
\r
333 /*-----------------------------------------------------------*/
\r
335 static unsigned long prvProcessTickInterrupt( void )
\r
337 unsigned long ulSwitchRequired;
\r
339 /* Process the tick itself. */
\r
340 ulSwitchRequired = ( unsigned long ) xTaskIncrementTick();
\r
342 return ulSwitchRequired;
\r
344 /*-----------------------------------------------------------*/
\r
346 static void prvProcessSimulatedInterrupts( void )
\r
348 unsigned long ulSwitchRequired, i;
\r
349 xThreadState *pxThreadState;
\r
350 void *pvObjectList[ 2 ];
\r
352 /* Going to block on the mutex that ensured exclusive access to the simulated
\r
353 interrupt objects, and the event that signals that a simulated interrupt
\r
354 should be processed. */
\r
355 pvObjectList[ 0 ] = pvInterruptEventMutex;
\r
356 pvObjectList[ 1 ] = pvInterruptEvent;
\r
360 WaitForMultipleObjects( sizeof( pvObjectList ) / sizeof( void * ), pvObjectList, TRUE, INFINITE );
\r
362 /* Used to indicate whether the simulated interrupt processing has
\r
363 necessitated a context switch to another task/thread. */
\r
364 ulSwitchRequired = pdFALSE;
\r
366 /* For each interrupt we are interested in processing, each of which is
\r
367 represented by a bit in the 32bit ulPendingInterrupts variable. */
\r
368 for( i = 0; i < portMAX_INTERRUPTS; i++ )
\r
370 /* Is the simulated interrupt pending? */
\r
371 if( ulPendingInterrupts & ( 1UL << i ) )
\r
373 /* Is a handler installed? */
\r
374 if( ulIsrHandler[ i ] != NULL )
\r
376 /* Run the actual handler. */
\r
377 if( ulIsrHandler[ i ]() != pdFALSE )
\r
379 ulSwitchRequired |= ( 1 << i );
\r
383 /* Clear the interrupt pending bit. */
\r
384 ulPendingInterrupts &= ~( 1UL << i );
\r
388 if( ulSwitchRequired != pdFALSE )
\r
390 void *pvOldCurrentTCB;
\r
392 pvOldCurrentTCB = pxCurrentTCB;
\r
394 /* Select the next task to run. */
\r
395 vTaskSwitchContext();
\r
397 /* If the task selected to enter the running state is not the task
\r
398 that is already in the running state. */
\r
399 if( pvOldCurrentTCB != pxCurrentTCB )
\r
401 /* Suspend the old thread. */
\r
402 pxThreadState = ( xThreadState *) *( ( unsigned long * ) pvOldCurrentTCB );
\r
403 SuspendThread( pxThreadState->pvThread );
\r
405 /* Obtain the state of the task now selected to enter the
\r
407 pxThreadState = ( xThreadState * ) ( *( unsigned long *) pxCurrentTCB );
\r
408 ResumeThread( pxThreadState->pvThread );
\r
412 ReleaseMutex( pvInterruptEventMutex );
\r
415 /*-----------------------------------------------------------*/
\r
417 void vPortDeleteThread( void *pvTaskToDelete )
\r
419 xThreadState *pxThreadState;
\r
421 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
423 /* Find the handle of the thread being deleted. */
\r
424 pxThreadState = ( xThreadState * ) ( *( unsigned long *) pvTaskToDelete );
\r
425 TerminateThread( pxThreadState->pvThread, 0 );
\r
427 ReleaseMutex( pvInterruptEventMutex );
\r
429 /*-----------------------------------------------------------*/
\r
431 void vPortEndScheduler( void )
\r
433 /* This function IS NOT TESTED! */
\r
434 TerminateProcess( GetCurrentProcess(), 0 );
\r
436 /*-----------------------------------------------------------*/
\r
438 void vPortGenerateSimulatedInterrupt( unsigned long ulInterruptNumber )
\r
440 if( ( ulInterruptNumber < portMAX_INTERRUPTS ) && ( pvInterruptEventMutex != NULL ) )
\r
442 /* Yield interrupts are processed even when critical nesting is non-zero. */
\r
443 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
444 ulPendingInterrupts |= ( 1 << ulInterruptNumber );
\r
446 /* The simulated interrupt is now held pending, but don't actually process it
\r
447 yet if this call is within a critical section. It is possible for this to
\r
448 be in a critical section as calls to wait for mutexes are accumulative. */
\r
449 if( ulCriticalNesting == 0 )
\r
451 SetEvent( pvInterruptEvent );
\r
454 ReleaseMutex( pvInterruptEventMutex );
\r
457 /*-----------------------------------------------------------*/
\r
459 void vPortSetInterruptHandler( unsigned long ulInterruptNumber, unsigned long (*pvHandler)( void ) )
\r
461 if( ulInterruptNumber < portMAX_INTERRUPTS )
\r
463 if( pvInterruptEventMutex != NULL )
\r
465 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
466 ulIsrHandler[ ulInterruptNumber ] = pvHandler;
\r
467 ReleaseMutex( pvInterruptEventMutex );
\r
471 ulIsrHandler[ ulInterruptNumber ] = pvHandler;
\r
475 /*-----------------------------------------------------------*/
\r
477 void vPortEnterCritical( void )
\r
479 if( xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED )
\r
481 /* The interrupt event mutex is held for the entire critical section,
\r
482 effectively disabling (simulated) interrupts. */
\r
483 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
484 ulCriticalNesting++;
\r
488 ulCriticalNesting++;
\r
491 /*-----------------------------------------------------------*/
\r
493 void vPortExitCritical( void )
\r
495 long lMutexNeedsReleasing;
\r
497 /* The interrupt event mutex should already be held by this thread as it was
\r
498 obtained on entry to the critical section. */
\r
500 lMutexNeedsReleasing = pdTRUE;
\r
502 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
\r
504 if( ulCriticalNesting == ( portNO_CRITICAL_NESTING + 1 ) )
\r
506 ulCriticalNesting--;
\r
508 /* Were any interrupts set to pending while interrupts were
\r
509 (simulated) disabled? */
\r
510 if( ulPendingInterrupts != 0UL )
\r
512 SetEvent( pvInterruptEvent );
\r
514 /* Mutex will be released now, so does not require releasing
\r
515 on function exit. */
\r
516 lMutexNeedsReleasing = pdFALSE;
\r
517 ReleaseMutex( pvInterruptEventMutex );
\r
522 /* Tick interrupts will still not be processed as the critical
\r
523 nesting depth will not be zero. */
\r
524 ulCriticalNesting--;
\r
528 if( lMutexNeedsReleasing == pdTRUE )
\r
530 ReleaseMutex( pvInterruptEventMutex );
\r
533 /*-----------------------------------------------------------*/
\r