2 FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS books - available as PDF or paperback *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
54 /* Scheduler includes. */
\r
55 #include "FreeRTOS.h"
\r
59 #define portMAX_INTERRUPTS ( ( unsigned long ) sizeof( unsigned long ) * 8UL ) /* The number of bits in an unsigned long. */
\r
60 #define portNO_CRITICAL_NESTING ( ( unsigned long ) 0 )
\r
63 * Created as a high priority thread, this function uses a timer to simulate
\r
64 * a tick interrupt being generated on an embedded target. In this Windows
\r
65 * environment the timer does not achieve anything approaching real time
\r
66 * performance though.
\r
68 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter );
\r
71 * Process all the simulated interrupts - each represented by a bit in
\r
72 * ulPendingInterrupts variable.
\r
74 static void prvProcessSimulatedInterrupts( void );
\r
77 * Interrupt handlers used by the kernel itself. These are executed from the
\r
78 * simulated interrupt handler thread.
\r
80 static unsigned long prvProcessDeleteThreadInterrupt( void );
\r
81 static unsigned long prvProcessYieldInterrupt( void );
\r
82 static unsigned long prvProcessTickInterrupt( void );
\r
84 /*-----------------------------------------------------------*/
\r
86 /* The WIN32 simulator runs each task in a thread. The context switching is
\r
87 managed by the threads, so the task stack does not have to be managed directly,
\r
88 although the task stack is still used to hold an xThreadState structure this is
\r
89 the only thing it will ever hold. The structure indirectly maps the task handle
\r
90 to a thread handle. */
\r
93 /* Handle of the thread that executes the task. */
\r
98 /* Simulated interrupts waiting to be processed. This is a bit mask where each
\r
99 bit represents one interrupt, so a maximum of 32 interrupts can be simulated. */
\r
100 static volatile unsigned long ulPendingInterrupts = 0UL;
\r
102 /* An event used to inform the simulated interrupt processing thread (a high
\r
103 priority thread that simulated interrupt processing) that an interrupt is
\r
105 static void *pvInterruptEvent = NULL;
\r
107 /* Mutex used to protect all the simulated interrupt variables that are accessed
\r
108 by multiple threads. */
\r
109 static void *pvInterruptEventMutex = NULL;
\r
111 /* The critical nesting count for the currently executing task. This is
\r
112 initialised to a non-zero value so interrupts do not become enabled during
\r
113 the initialisation phase. As each task has its own critical nesting value
\r
114 ulCriticalNesting will get set to zero when the first task runs. This
\r
115 initialisation is probably not critical in this simulated environment as the
\r
116 simulated interrupt handlers do not get created until the FreeRTOS scheduler is
\r
118 static unsigned long ulCriticalNesting = 9999UL;
\r
120 /* Handlers for all the simulated software interrupts. The first two positions
\r
121 are used for the Yield and Tick interrupts so are handled slightly differently,
\r
122 all the other interrupts can be user defined. */
\r
123 static unsigned long (*ulIsrHandler[ portMAX_INTERRUPTS ])( void ) = { 0 };
\r
125 /* Pointer to the TCB of the currently executing task. */
\r
126 extern void *pxCurrentTCB;
\r
128 /*-----------------------------------------------------------*/
\r
130 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )
\r
132 /* Just to prevent compiler warnings. */
\r
133 ( void ) lpParameter;
\r
137 /* Wait until the timer expires and we can access the simulated interrupt
\r
138 variables. *NOTE* this is not a 'real time' way of generating tick
\r
139 events as the next wake time should be relative to the previous wake
\r
140 time, not the time that Sleep() is called. It is done this way to
\r
141 prevent overruns in this very non real time simulated/emulated
\r
143 Sleep( portTICK_RATE_MS );
\r
145 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
147 /* The timer has expired, generate the simulated tick event. */
\r
148 ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );
\r
150 /* The interrupt is now pending - notify the simulated interrupt
\r
152 SetEvent( pvInterruptEvent );
\r
154 /* Give back the mutex so the simulated interrupt handler unblocks
\r
155 and can access the interrupt handler variables. */
\r
156 ReleaseMutex( pvInterruptEventMutex );
\r
160 /* Should never reach here - MingW complains if you leave this line out,
\r
161 MSVC complains if you put it in. */
\r
165 /*-----------------------------------------------------------*/
\r
167 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
169 xThreadState *pxThreadState = NULL;
\r
171 /* In this simulated case a stack is not initialised, but instead a thread
\r
172 is created that will execute the task being created. The thread handles
\r
173 the context switching itself. The xThreadState object is placed onto
\r
174 the stack that was created for the task - so the stack buffer is still
\r
175 used, just not in the conventional way. It will not be used for anything
\r
176 other than holding this structure. */
\r
177 pxThreadState = ( xThreadState * ) ( pxTopOfStack - sizeof( xThreadState ) );
\r
179 /* Create the thread itself. */
\r
180 pxThreadState->pvThread = CreateThread( NULL, 0, ( LPTHREAD_START_ROUTINE ) pxCode, pvParameters, CREATE_SUSPENDED, NULL );
\r
181 SetThreadAffinityMask( pxThreadState->pvThread, 0x01 );
\r
182 SetThreadPriorityBoost( pxThreadState->pvThread, TRUE );
\r
183 SetThreadPriority( pxThreadState->pvThread, THREAD_PRIORITY_IDLE );
\r
185 return ( portSTACK_TYPE * ) pxThreadState;
\r
187 /*-----------------------------------------------------------*/
\r
189 portBASE_TYPE xPortStartScheduler( void )
\r
192 long lSuccess = pdPASS;
\r
193 xThreadState *pxThreadState;
\r
195 /* Install the interrupt handlers used by the scheduler itself. */
\r
196 vPortSetInterruptHandler( portINTERRUPT_YIELD, prvProcessYieldInterrupt );
\r
197 vPortSetInterruptHandler( portINTERRUPT_TICK, prvProcessTickInterrupt );
\r
198 vPortSetInterruptHandler( portINTERRUPT_DELETE_THREAD, prvProcessDeleteThreadInterrupt );
\r
200 /* Create the events and mutexes that are used to synchronise all the
\r
202 pvInterruptEventMutex = CreateMutex( NULL, FALSE, NULL );
\r
203 pvInterruptEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
\r
205 if( ( pvInterruptEventMutex == NULL ) || ( pvInterruptEvent == NULL ) )
\r
210 /* Set the priority of this thread such that it is above the priority of
\r
211 the threads that run tasks. This higher priority is required to ensure
\r
212 simulated interrupts take priority over tasks. */
\r
213 pvHandle = GetCurrentThread();
\r
214 if( pvHandle == NULL )
\r
219 if( lSuccess == pdPASS )
\r
221 if( SetThreadPriority( pvHandle, THREAD_PRIORITY_NORMAL ) == 0 )
\r
225 SetThreadPriorityBoost( pvHandle, TRUE );
\r
226 SetThreadAffinityMask( pvHandle, 0x01 );
\r
229 if( lSuccess == pdPASS )
\r
231 /* Start the thread that simulates the timer peripheral to generate
\r
232 tick interrupts. The priority is set below that of the simulated
\r
233 interrupt handler so the interrupt event mutex is used for the
\r
234 handshake / overrun protection. */
\r
235 pvHandle = CreateThread( NULL, 0, prvSimulatedPeripheralTimer, NULL, 0, NULL );
\r
236 if( pvHandle != NULL )
\r
238 SetThreadPriority( pvHandle, THREAD_PRIORITY_BELOW_NORMAL );
\r
239 SetThreadPriorityBoost( pvHandle, TRUE );
\r
240 SetThreadAffinityMask( pvHandle, 0x01 );
\r
243 /* Start the highest priority task by obtaining its associated thread
\r
244 state structure, in which is stored the thread handle. */
\r
245 pxThreadState = ( xThreadState * ) *( ( unsigned long * ) pxCurrentTCB );
\r
246 ulCriticalNesting = portNO_CRITICAL_NESTING;
\r
248 /* Bump up the priority of the thread that is going to run, in the
\r
249 hope that this will asist in getting the Windows thread scheduler to
\r
250 behave as an embedded engineer might expect. */
\r
251 ResumeThread( pxThreadState->pvThread );
\r
253 /* Handle all simulated interrupts - including yield requests and
\r
254 simulated ticks. */
\r
255 prvProcessSimulatedInterrupts();
\r
258 /* Would not expect to return from prvProcessSimulatedInterrupts(), so should
\r
262 /*-----------------------------------------------------------*/
\r
264 static unsigned long prvProcessDeleteThreadInterrupt( void )
\r
268 /*-----------------------------------------------------------*/
\r
270 static unsigned long prvProcessYieldInterrupt( void )
\r
274 /*-----------------------------------------------------------*/
\r
276 static unsigned long prvProcessTickInterrupt( void )
\r
278 unsigned long ulSwitchRequired;
\r
280 /* Process the tick itself. */
\r
281 vTaskIncrementTick();
\r
282 #if( configUSE_PREEMPTION != 0 )
\r
284 /* A context switch is only automatically performed from the tick
\r
285 interrupt if the pre-emptive scheduler is being used. */
\r
286 ulSwitchRequired = pdTRUE;
\r
290 ulSwitchRequired = pdFALSE;
\r
294 return ulSwitchRequired;
\r
296 /*-----------------------------------------------------------*/
\r
298 static void prvProcessSimulatedInterrupts( void )
\r
300 unsigned long ulSwitchRequired, i;
\r
301 xThreadState *pxThreadState;
\r
302 void *pvObjectList[ 2 ];
\r
304 /* Going to block on the mutex that ensured exclusive access to the simulated
\r
305 interrupt objects, and the event that signals that a simulated interrupt
\r
306 should be processed. */
\r
307 pvObjectList[ 0 ] = pvInterruptEventMutex;
\r
308 pvObjectList[ 1 ] = pvInterruptEvent;
\r
312 WaitForMultipleObjects( sizeof( pvObjectList ) / sizeof( void * ), pvObjectList, TRUE, INFINITE );
\r
314 /* Used to indicate whether the simulated interrupt processing has
\r
315 necessitated a context switch to another task/thread. */
\r
316 ulSwitchRequired = pdFALSE;
\r
318 /* For each interrupt we are interested in processing, each of which is
\r
319 represented by a bit in the 32bit ulPendingInterrupts variable. */
\r
320 for( i = 0; i < portMAX_INTERRUPTS; i++ )
\r
322 /* Is the simulated interrupt pending? */
\r
323 if( ulPendingInterrupts & ( 1UL << i ) )
\r
325 /* Is a handler installed? */
\r
326 if( ulIsrHandler[ i ] != NULL )
\r
328 /* Run the actual handler. */
\r
329 if( ulIsrHandler[ i ]() != pdFALSE )
\r
331 ulSwitchRequired |= ( 1 << i );
\r
335 /* Clear the interrupt pending bit. */
\r
336 ulPendingInterrupts &= ~( 1UL << i );
\r
340 if( ulSwitchRequired != pdFALSE )
\r
342 void *pvOldCurrentTCB;
\r
344 pvOldCurrentTCB = pxCurrentTCB;
\r
346 /* Select the next task to run. */
\r
347 vTaskSwitchContext();
\r
349 /* If the task selected to enter the running state is not the task
\r
350 that is already in the running state. */
\r
351 if( pvOldCurrentTCB != pxCurrentTCB )
\r
353 /* Suspend the old thread. */
\r
354 pxThreadState = ( xThreadState *) *( ( unsigned long * ) pvOldCurrentTCB );
\r
356 if( ( ulSwitchRequired & ( 1 << portINTERRUPT_DELETE_THREAD ) ) != pdFALSE )
\r
358 TerminateThread( pxThreadState->pvThread, 0 );
\r
362 SuspendThread( pxThreadState->pvThread );
\r
365 /* Obtain the state of the task now selected to enter the
\r
367 pxThreadState = ( xThreadState * ) ( *( unsigned long *) pxCurrentTCB );
\r
368 ResumeThread( pxThreadState->pvThread );
\r
372 ReleaseMutex( pvInterruptEventMutex );
\r
375 /*-----------------------------------------------------------*/
\r
377 void vPortDeleteThread( void *pvTaskToDelete )
\r
379 xThreadState *pxThreadState;
\r
381 if( pvTaskToDelete == pxCurrentTCB )
\r
383 /* The task is deleting itself, and so the thread that is running now
\r
384 is also to be deleted. This has to be deferred until this thread is
\r
385 no longer running, so its done in the simulated interrupt handler thread. */
\r
386 vPortGenerateSimulatedInterrupt( portINTERRUPT_DELETE_THREAD );
\r
390 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
392 /* Find the handle of the thread being deleted. */
\r
393 pxThreadState = ( xThreadState * ) ( *( unsigned long *) pvTaskToDelete );
\r
394 TerminateThread( pxThreadState->pvThread, 0 );
\r
396 ReleaseMutex( pvInterruptEventMutex );
\r
399 /*-----------------------------------------------------------*/
\r
401 void vPortEndScheduler( void )
\r
403 /* This function IS NOT TESTED! */
\r
404 TerminateProcess( GetCurrentProcess(), 0 );
\r
406 /*-----------------------------------------------------------*/
\r
408 void vPortGenerateSimulatedInterrupt( unsigned long ulInterruptNumber )
\r
410 xThreadState *pxThreadState;
\r
412 if( ( ulInterruptNumber < portMAX_INTERRUPTS ) && ( pvInterruptEventMutex != NULL ) )
\r
414 /* Yield interrupts are processed even when critical nesting is non-zero. */
\r
415 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
416 ulPendingInterrupts |= ( 1 << ulInterruptNumber );
\r
418 /* The simulated interrupt is now held pending, but don't actually process it
\r
419 yet if this call is within a critical section. It is possible for this to
\r
420 be in a critical section as calls to wait for mutexes are accumulative. */
\r
421 if( ulCriticalNesting == 0 )
\r
423 /* The event handler needs to know to signal the interrupt acknowledge event
\r
424 the next time this task runs. */
\r
425 pxThreadState = ( xThreadState * ) *( ( unsigned long * ) pxCurrentTCB );
\r
426 SetEvent( pvInterruptEvent );
\r
429 ReleaseMutex( pvInterruptEventMutex );
\r
432 /*-----------------------------------------------------------*/
\r
434 void vPortSetInterruptHandler( unsigned long ulInterruptNumber, unsigned long (*pvHandler)( void ) )
\r
436 if( ulInterruptNumber < portMAX_INTERRUPTS )
\r
438 if( pvInterruptEventMutex != NULL )
\r
440 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
441 ulIsrHandler[ ulInterruptNumber ] = pvHandler;
\r
442 ReleaseMutex( pvInterruptEventMutex );
\r
446 ulIsrHandler[ ulInterruptNumber ] = pvHandler;
\r
450 /*-----------------------------------------------------------*/
\r
452 void vPortEnterCritical( void )
\r
454 if( xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED )
\r
456 /* The interrupt event mutex is held for the entire critical section,
\r
457 effectively disabling (simulated) interrupts. */
\r
458 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
459 ulCriticalNesting++;
\r
463 ulCriticalNesting++;
\r
466 /*-----------------------------------------------------------*/
\r
468 void vPortExitCritical( void )
\r
470 xThreadState *pxThreadState;
\r
471 long lMutexNeedsReleasing;
\r
473 /* The interrupt event mutex should already be held by this thread as it was
\r
474 obtained on entry to the critical section. */
\r
476 lMutexNeedsReleasing = pdTRUE;
\r
478 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
\r
480 if( ulCriticalNesting == ( portNO_CRITICAL_NESTING + 1 ) )
\r
482 ulCriticalNesting--;
\r
484 /* Were any interrupts set to pending while interrupts were
\r
485 (simulated) disabled? */
\r
486 if( ulPendingInterrupts != 0UL )
\r
488 SetEvent( pvInterruptEvent );
\r
490 /* The event handler needs to know to signal the interrupt
\r
491 acknowledge event the next time this task runs. */
\r
492 pxThreadState = ( xThreadState * ) *( ( unsigned long * ) pxCurrentTCB );
\r
494 /* Mutex will be released now, so does not require releasing
\r
495 on function exit. */
\r
496 lMutexNeedsReleasing = pdFALSE;
\r
497 ReleaseMutex( pvInterruptEventMutex );
\r
502 /* Tick interrupts will still not be processed as the critical
\r
503 nesting depth will not be zero. */
\r
504 ulCriticalNesting--;
\r
508 if( lMutexNeedsReleasing == pdTRUE )
\r
510 ReleaseMutex( pvInterruptEventMutex );
\r
513 /*-----------------------------------------------------------*/
\r