2 FreeRTOS V7.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\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 modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
67 /* Scheduler includes. */
\r
68 #include "FreeRTOS.h"
\r
72 #define portMAX_INTERRUPTS ( ( unsigned long ) sizeof( unsigned long ) * 8UL ) /* The number of bits in an unsigned long. */
\r
73 #define portNO_CRITICAL_NESTING ( ( unsigned long ) 0 )
\r
76 * Created as a high priority thread, this function uses a timer to simulate
\r
77 * a tick interrupt being generated on an embedded target. In this Windows
\r
78 * environment the timer does not achieve anything approaching real time
\r
79 * performance though.
\r
81 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter );
\r
84 * Process all the simulated interrupts - each represented by a bit in
\r
85 * ulPendingInterrupts variable.
\r
87 static void prvProcessSimulatedInterrupts( void );
\r
90 * Interrupt handlers used by the kernel itself. These are executed from the
\r
91 * simulated interrupt handler thread.
\r
93 static unsigned long prvProcessYieldInterrupt( void );
\r
94 static unsigned long prvProcessTickInterrupt( void );
\r
96 /*-----------------------------------------------------------*/
\r
98 /* The WIN32 simulator runs each task in a thread. The context switching is
\r
99 managed by the threads, so the task stack does not have to be managed directly,
\r
100 although the task stack is still used to hold an xThreadState structure this is
\r
101 the only thing it will ever hold. The structure indirectly maps the task handle
\r
102 to a thread handle. */
\r
105 /* Handle of the thread that executes the task. */
\r
110 /* Simulated interrupts waiting to be processed. This is a bit mask where each
\r
111 bit represents one interrupt, so a maximum of 32 interrupts can be simulated. */
\r
112 static volatile unsigned long ulPendingInterrupts = 0UL;
\r
114 /* An event used to inform the simulated interrupt processing thread (a high
\r
115 priority thread that simulated interrupt processing) that an interrupt is
\r
117 static void *pvInterruptEvent = NULL;
\r
119 /* Mutex used to protect all the simulated interrupt variables that are accessed
\r
120 by multiple threads. */
\r
121 static void *pvInterruptEventMutex = NULL;
\r
123 /* The critical nesting count for the currently executing task. This is
\r
124 initialised to a non-zero value so interrupts do not become enabled during
\r
125 the initialisation phase. As each task has its own critical nesting value
\r
126 ulCriticalNesting will get set to zero when the first task runs. This
\r
127 initialisation is probably not critical in this simulated environment as the
\r
128 simulated interrupt handlers do not get created until the FreeRTOS scheduler is
\r
130 static unsigned long ulCriticalNesting = 9999UL;
\r
132 /* Handlers for all the simulated software interrupts. The first two positions
\r
133 are used for the Yield and Tick interrupts so are handled slightly differently,
\r
134 all the other interrupts can be user defined. */
\r
135 static unsigned long (*ulIsrHandler[ portMAX_INTERRUPTS ])( void ) = { 0 };
\r
137 /* Pointer to the TCB of the currently executing task. */
\r
138 extern void *pxCurrentTCB;
\r
140 /*-----------------------------------------------------------*/
\r
142 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )
\r
144 portTickType xMinimumWindowsBlockTime = ( portTickType ) 20;
\r
146 /* Just to prevent compiler warnings. */
\r
147 ( void ) lpParameter;
\r
151 /* Wait until the timer expires and we can access the simulated interrupt
\r
152 variables. *NOTE* this is not a 'real time' way of generating tick
\r
153 events as the next wake time should be relative to the previous wake
\r
154 time, not the time that Sleep() is called. It is done this way to
\r
155 prevent overruns in this very non real time simulated/emulated
\r
157 if( portTICK_RATE_MS < xMinimumWindowsBlockTime )
\r
159 Sleep( xMinimumWindowsBlockTime );
\r
163 Sleep( portTICK_RATE_MS );
\r
166 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
168 /* The timer has expired, generate the simulated tick event. */
\r
169 ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );
\r
171 /* The interrupt is now pending - notify the simulated interrupt
\r
173 SetEvent( pvInterruptEvent );
\r
175 /* Give back the mutex so the simulated interrupt handler unblocks
\r
176 and can access the interrupt handler variables. */
\r
177 ReleaseMutex( pvInterruptEventMutex );
\r
181 /* Should never reach here - MingW complains if you leave this line out,
\r
182 MSVC complains if you put it in. */
\r
186 /*-----------------------------------------------------------*/
\r
188 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
190 xThreadState *pxThreadState = NULL;
\r
192 /* In this simulated case a stack is not initialised, but instead a thread
\r
193 is created that will execute the task being created. The thread handles
\r
194 the context switching itself. The xThreadState object is placed onto
\r
195 the stack that was created for the task - so the stack buffer is still
\r
196 used, just not in the conventional way. It will not be used for anything
\r
197 other than holding this structure. */
\r
198 pxThreadState = ( xThreadState * ) ( pxTopOfStack - sizeof( xThreadState ) );
\r
200 /* Create the thread itself. */
\r
201 pxThreadState->pvThread = CreateThread( NULL, 0, ( LPTHREAD_START_ROUTINE ) pxCode, pvParameters, CREATE_SUSPENDED, NULL );
\r
202 SetThreadAffinityMask( pxThreadState->pvThread, 0x01 );
\r
203 SetThreadPriorityBoost( pxThreadState->pvThread, TRUE );
\r
204 SetThreadPriority( pxThreadState->pvThread, THREAD_PRIORITY_IDLE );
\r
206 return ( portSTACK_TYPE * ) pxThreadState;
\r
208 /*-----------------------------------------------------------*/
\r
210 portBASE_TYPE xPortStartScheduler( void )
\r
213 long lSuccess = pdPASS;
\r
214 xThreadState *pxThreadState;
\r
216 /* Install the interrupt handlers used by the scheduler itself. */
\r
217 vPortSetInterruptHandler( portINTERRUPT_YIELD, prvProcessYieldInterrupt );
\r
218 vPortSetInterruptHandler( portINTERRUPT_TICK, prvProcessTickInterrupt );
\r
220 /* Create the events and mutexes that are used to synchronise all the
\r
222 pvInterruptEventMutex = CreateMutex( NULL, FALSE, NULL );
\r
223 pvInterruptEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
\r
225 if( ( pvInterruptEventMutex == NULL ) || ( pvInterruptEvent == NULL ) )
\r
230 /* Set the priority of this thread such that it is above the priority of
\r
231 the threads that run tasks. This higher priority is required to ensure
\r
232 simulated interrupts take priority over tasks. */
\r
233 pvHandle = GetCurrentThread();
\r
234 if( pvHandle == NULL )
\r
239 if( lSuccess == pdPASS )
\r
241 if( SetThreadPriority( pvHandle, THREAD_PRIORITY_NORMAL ) == 0 )
\r
245 SetThreadPriorityBoost( pvHandle, TRUE );
\r
246 SetThreadAffinityMask( pvHandle, 0x01 );
\r
249 if( lSuccess == pdPASS )
\r
251 /* Start the thread that simulates the timer peripheral to generate
\r
252 tick interrupts. The priority is set below that of the simulated
\r
253 interrupt handler so the interrupt event mutex is used for the
\r
254 handshake / overrun protection. */
\r
255 pvHandle = CreateThread( NULL, 0, prvSimulatedPeripheralTimer, NULL, 0, NULL );
\r
256 if( pvHandle != NULL )
\r
258 SetThreadPriority( pvHandle, THREAD_PRIORITY_BELOW_NORMAL );
\r
259 SetThreadPriorityBoost( pvHandle, TRUE );
\r
260 SetThreadAffinityMask( pvHandle, 0x01 );
\r
263 /* Start the highest priority task by obtaining its associated thread
\r
264 state structure, in which is stored the thread handle. */
\r
265 pxThreadState = ( xThreadState * ) *( ( unsigned long * ) pxCurrentTCB );
\r
266 ulCriticalNesting = portNO_CRITICAL_NESTING;
\r
268 /* Bump up the priority of the thread that is going to run, in the
\r
269 hope that this will asist in getting the Windows thread scheduler to
\r
270 behave as an embedded engineer might expect. */
\r
271 ResumeThread( pxThreadState->pvThread );
\r
273 /* Handle all simulated interrupts - including yield requests and
\r
274 simulated ticks. */
\r
275 prvProcessSimulatedInterrupts();
\r
278 /* Would not expect to return from prvProcessSimulatedInterrupts(), so should
\r
282 /*-----------------------------------------------------------*/
\r
284 static unsigned long prvProcessYieldInterrupt( void )
\r
288 /*-----------------------------------------------------------*/
\r
290 static unsigned long prvProcessTickInterrupt( void )
\r
292 unsigned long ulSwitchRequired;
\r
294 /* Process the tick itself. */
\r
295 vTaskIncrementTick();
\r
296 #if( configUSE_PREEMPTION != 0 )
\r
298 /* A context switch is only automatically performed from the tick
\r
299 interrupt if the pre-emptive scheduler is being used. */
\r
300 ulSwitchRequired = pdTRUE;
\r
304 ulSwitchRequired = pdFALSE;
\r
308 return ulSwitchRequired;
\r
310 /*-----------------------------------------------------------*/
\r
312 static void prvProcessSimulatedInterrupts( void )
\r
314 unsigned long ulSwitchRequired, i;
\r
315 xThreadState *pxThreadState;
\r
316 void *pvObjectList[ 2 ];
\r
318 /* Going to block on the mutex that ensured exclusive access to the simulated
\r
319 interrupt objects, and the event that signals that a simulated interrupt
\r
320 should be processed. */
\r
321 pvObjectList[ 0 ] = pvInterruptEventMutex;
\r
322 pvObjectList[ 1 ] = pvInterruptEvent;
\r
326 WaitForMultipleObjects( sizeof( pvObjectList ) / sizeof( void * ), pvObjectList, TRUE, INFINITE );
\r
328 /* Used to indicate whether the simulated interrupt processing has
\r
329 necessitated a context switch to another task/thread. */
\r
330 ulSwitchRequired = pdFALSE;
\r
332 /* For each interrupt we are interested in processing, each of which is
\r
333 represented by a bit in the 32bit ulPendingInterrupts variable. */
\r
334 for( i = 0; i < portMAX_INTERRUPTS; i++ )
\r
336 /* Is the simulated interrupt pending? */
\r
337 if( ulPendingInterrupts & ( 1UL << i ) )
\r
339 /* Is a handler installed? */
\r
340 if( ulIsrHandler[ i ] != NULL )
\r
342 /* Run the actual handler. */
\r
343 if( ulIsrHandler[ i ]() != pdFALSE )
\r
345 ulSwitchRequired |= ( 1 << i );
\r
349 /* Clear the interrupt pending bit. */
\r
350 ulPendingInterrupts &= ~( 1UL << i );
\r
354 if( ulSwitchRequired != pdFALSE )
\r
356 void *pvOldCurrentTCB;
\r
358 pvOldCurrentTCB = pxCurrentTCB;
\r
360 /* Select the next task to run. */
\r
361 vTaskSwitchContext();
\r
363 /* If the task selected to enter the running state is not the task
\r
364 that is already in the running state. */
\r
365 if( pvOldCurrentTCB != pxCurrentTCB )
\r
367 /* Suspend the old thread. */
\r
368 pxThreadState = ( xThreadState *) *( ( unsigned long * ) pvOldCurrentTCB );
\r
369 SuspendThread( pxThreadState->pvThread );
\r
371 /* Obtain the state of the task now selected to enter the
\r
373 pxThreadState = ( xThreadState * ) ( *( unsigned long *) pxCurrentTCB );
\r
374 ResumeThread( pxThreadState->pvThread );
\r
378 ReleaseMutex( pvInterruptEventMutex );
\r
381 /*-----------------------------------------------------------*/
\r
383 void vPortDeleteThread( void *pvTaskToDelete )
\r
385 xThreadState *pxThreadState;
\r
387 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
389 /* Find the handle of the thread being deleted. */
\r
390 pxThreadState = ( xThreadState * ) ( *( unsigned long *) pvTaskToDelete );
\r
391 TerminateThread( pxThreadState->pvThread, 0 );
\r
393 ReleaseMutex( pvInterruptEventMutex );
\r
395 /*-----------------------------------------------------------*/
\r
397 void vPortEndScheduler( void )
\r
399 /* This function IS NOT TESTED! */
\r
400 TerminateProcess( GetCurrentProcess(), 0 );
\r
402 /*-----------------------------------------------------------*/
\r
404 void vPortGenerateSimulatedInterrupt( unsigned long ulInterruptNumber )
\r
406 xThreadState *pxThreadState;
\r
408 if( ( ulInterruptNumber < portMAX_INTERRUPTS ) && ( pvInterruptEventMutex != NULL ) )
\r
410 /* Yield interrupts are processed even when critical nesting is non-zero. */
\r
411 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
412 ulPendingInterrupts |= ( 1 << ulInterruptNumber );
\r
414 /* The simulated interrupt is now held pending, but don't actually process it
\r
415 yet if this call is within a critical section. It is possible for this to
\r
416 be in a critical section as calls to wait for mutexes are accumulative. */
\r
417 if( ulCriticalNesting == 0 )
\r
419 /* The event handler needs to know to signal the interrupt acknowledge event
\r
420 the next time this task runs. */
\r
421 pxThreadState = ( xThreadState * ) *( ( unsigned long * ) pxCurrentTCB );
\r
422 SetEvent( pvInterruptEvent );
\r
425 ReleaseMutex( pvInterruptEventMutex );
\r
428 /*-----------------------------------------------------------*/
\r
430 void vPortSetInterruptHandler( unsigned long ulInterruptNumber, unsigned long (*pvHandler)( void ) )
\r
432 if( ulInterruptNumber < portMAX_INTERRUPTS )
\r
434 if( pvInterruptEventMutex != NULL )
\r
436 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
437 ulIsrHandler[ ulInterruptNumber ] = pvHandler;
\r
438 ReleaseMutex( pvInterruptEventMutex );
\r
442 ulIsrHandler[ ulInterruptNumber ] = pvHandler;
\r
446 /*-----------------------------------------------------------*/
\r
448 void vPortEnterCritical( void )
\r
450 if( xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED )
\r
452 /* The interrupt event mutex is held for the entire critical section,
\r
453 effectively disabling (simulated) interrupts. */
\r
454 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
455 ulCriticalNesting++;
\r
459 ulCriticalNesting++;
\r
462 /*-----------------------------------------------------------*/
\r
464 void vPortExitCritical( void )
\r
466 xThreadState *pxThreadState;
\r
467 long lMutexNeedsReleasing;
\r
469 /* The interrupt event mutex should already be held by this thread as it was
\r
470 obtained on entry to the critical section. */
\r
472 lMutexNeedsReleasing = pdTRUE;
\r
474 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
\r
476 if( ulCriticalNesting == ( portNO_CRITICAL_NESTING + 1 ) )
\r
478 ulCriticalNesting--;
\r
480 /* Were any interrupts set to pending while interrupts were
\r
481 (simulated) disabled? */
\r
482 if( ulPendingInterrupts != 0UL )
\r
484 SetEvent( pvInterruptEvent );
\r
486 /* The event handler needs to know to signal the interrupt
\r
487 acknowledge event the next time this task runs. */
\r
488 pxThreadState = ( xThreadState * ) *( ( unsigned long * ) pxCurrentTCB );
\r
490 /* Mutex will be released now, so does not require releasing
\r
491 on function exit. */
\r
492 lMutexNeedsReleasing = pdFALSE;
\r
493 ReleaseMutex( pvInterruptEventMutex );
\r
498 /* Tick interrupts will still not be processed as the critical
\r
499 nesting depth will not be zero. */
\r
500 ulCriticalNesting--;
\r
504 if( lMutexNeedsReleasing == pdTRUE )
\r
506 ReleaseMutex( pvInterruptEventMutex );
\r
509 /*-----------------------------------------------------------*/
\r