2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
69 /* Scheduler includes. */
\r
70 #include "FreeRTOS.h"
\r
74 #define portMAX_INTERRUPTS ( ( unsigned long ) sizeof( unsigned long ) * 8UL ) /* The number of bits in an unsigned long. */
\r
75 #define portNO_CRITICAL_NESTING ( ( unsigned long ) 0 )
\r
78 * Created as a high priority thread, this function uses a timer to simulate
\r
79 * a tick interrupt being generated on an embedded target. In this Windows
\r
80 * environment the timer does not achieve anything approaching real time
\r
81 * performance though.
\r
83 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter );
\r
86 * Process all the simulated interrupts - each represented by a bit in
\r
87 * ulPendingInterrupts variable.
\r
89 static void prvProcessSimulatedInterrupts( void );
\r
92 * Interrupt handlers used by the kernel itself. These are executed from the
\r
93 * simulated interrupt handler thread.
\r
95 static unsigned long prvProcessYieldInterrupt( void );
\r
96 static unsigned long prvProcessTickInterrupt( void );
\r
98 /*-----------------------------------------------------------*/
\r
100 /* The WIN32 simulator runs each task in a thread. The context switching is
\r
101 managed by the threads, so the task stack does not have to be managed directly,
\r
102 although the task stack is still used to hold an xThreadState structure this is
\r
103 the only thing it will ever hold. The structure indirectly maps the task handle
\r
104 to a thread handle. */
\r
107 /* Handle of the thread that executes the task. */
\r
112 /* Simulated interrupts waiting to be processed. This is a bit mask where each
\r
113 bit represents one interrupt, so a maximum of 32 interrupts can be simulated. */
\r
114 static volatile unsigned long ulPendingInterrupts = 0UL;
\r
116 /* An event used to inform the simulated interrupt processing thread (a high
\r
117 priority thread that simulated interrupt processing) that an interrupt is
\r
119 static void *pvInterruptEvent = NULL;
\r
121 /* Mutex used to protect all the simulated interrupt variables that are accessed
\r
122 by multiple threads. */
\r
123 static void *pvInterruptEventMutex = NULL;
\r
125 /* The critical nesting count for the currently executing task. This is
\r
126 initialised to a non-zero value so interrupts do not become enabled during
\r
127 the initialisation phase. As each task has its own critical nesting value
\r
128 ulCriticalNesting will get set to zero when the first task runs. This
\r
129 initialisation is probably not critical in this simulated environment as the
\r
130 simulated interrupt handlers do not get created until the FreeRTOS scheduler is
\r
132 static unsigned long ulCriticalNesting = 9999UL;
\r
134 /* Handlers for all the simulated software interrupts. The first two positions
\r
135 are used for the Yield and Tick interrupts so are handled slightly differently,
\r
136 all the other interrupts can be user defined. */
\r
137 static unsigned long (*ulIsrHandler[ portMAX_INTERRUPTS ])( void ) = { 0 };
\r
139 /* Pointer to the TCB of the currently executing task. */
\r
140 extern void *pxCurrentTCB;
\r
142 /*-----------------------------------------------------------*/
\r
144 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )
\r
146 portTickType xMinimumWindowsBlockTime = ( portTickType ) 20;
\r
148 /* Just to prevent compiler warnings. */
\r
149 ( void ) lpParameter;
\r
153 /* Wait until the timer expires and we can access the simulated interrupt
\r
154 variables. *NOTE* this is not a 'real time' way of generating tick
\r
155 events as the next wake time should be relative to the previous wake
\r
156 time, not the time that Sleep() is called. It is done this way to
\r
157 prevent overruns in this very non real time simulated/emulated
\r
159 if( portTICK_RATE_MS < xMinimumWindowsBlockTime )
\r
161 Sleep( xMinimumWindowsBlockTime );
\r
165 Sleep( portTICK_RATE_MS );
\r
168 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
170 /* The timer has expired, generate the simulated tick event. */
\r
171 ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );
\r
173 /* The interrupt is now pending - notify the simulated interrupt
\r
175 SetEvent( pvInterruptEvent );
\r
177 /* Give back the mutex so the simulated interrupt handler unblocks
\r
178 and can access the interrupt handler variables. */
\r
179 ReleaseMutex( pvInterruptEventMutex );
\r
183 /* Should never reach here - MingW complains if you leave this line out,
\r
184 MSVC complains if you put it in. */
\r
188 /*-----------------------------------------------------------*/
\r
190 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
192 xThreadState *pxThreadState = NULL;
\r
194 /* In this simulated case a stack is not initialised, but instead a thread
\r
195 is created that will execute the task being created. The thread handles
\r
196 the context switching itself. The xThreadState object is placed onto
\r
197 the stack that was created for the task - so the stack buffer is still
\r
198 used, just not in the conventional way. It will not be used for anything
\r
199 other than holding this structure. */
\r
200 pxThreadState = ( xThreadState * ) ( pxTopOfStack - sizeof( xThreadState ) );
\r
202 /* Create the thread itself. */
\r
203 pxThreadState->pvThread = CreateThread( NULL, 0, ( LPTHREAD_START_ROUTINE ) pxCode, pvParameters, CREATE_SUSPENDED, NULL );
\r
204 SetThreadAffinityMask( pxThreadState->pvThread, 0x01 );
\r
205 SetThreadPriorityBoost( pxThreadState->pvThread, TRUE );
\r
206 SetThreadPriority( pxThreadState->pvThread, THREAD_PRIORITY_IDLE );
\r
208 return ( portSTACK_TYPE * ) pxThreadState;
\r
210 /*-----------------------------------------------------------*/
\r
212 portBASE_TYPE xPortStartScheduler( void )
\r
215 long lSuccess = pdPASS;
\r
216 xThreadState *pxThreadState;
\r
218 /* Install the interrupt handlers used by the scheduler itself. */
\r
219 vPortSetInterruptHandler( portINTERRUPT_YIELD, prvProcessYieldInterrupt );
\r
220 vPortSetInterruptHandler( portINTERRUPT_TICK, prvProcessTickInterrupt );
\r
222 /* Create the events and mutexes that are used to synchronise all the
\r
224 pvInterruptEventMutex = CreateMutex( NULL, FALSE, NULL );
\r
225 pvInterruptEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
\r
227 if( ( pvInterruptEventMutex == NULL ) || ( pvInterruptEvent == NULL ) )
\r
232 /* Set the priority of this thread such that it is above the priority of
\r
233 the threads that run tasks. This higher priority is required to ensure
\r
234 simulated interrupts take priority over tasks. */
\r
235 pvHandle = GetCurrentThread();
\r
236 if( pvHandle == NULL )
\r
241 if( lSuccess == pdPASS )
\r
243 if( SetThreadPriority( pvHandle, THREAD_PRIORITY_NORMAL ) == 0 )
\r
247 SetThreadPriorityBoost( pvHandle, TRUE );
\r
248 SetThreadAffinityMask( pvHandle, 0x01 );
\r
251 if( lSuccess == pdPASS )
\r
253 /* Start the thread that simulates the timer peripheral to generate
\r
254 tick interrupts. The priority is set below that of the simulated
\r
255 interrupt handler so the interrupt event mutex is used for the
\r
256 handshake / overrun protection. */
\r
257 pvHandle = CreateThread( NULL, 0, prvSimulatedPeripheralTimer, NULL, 0, NULL );
\r
258 if( pvHandle != NULL )
\r
260 SetThreadPriority( pvHandle, THREAD_PRIORITY_BELOW_NORMAL );
\r
261 SetThreadPriorityBoost( pvHandle, TRUE );
\r
262 SetThreadAffinityMask( pvHandle, 0x01 );
\r
265 /* Start the highest priority task by obtaining its associated thread
\r
266 state structure, in which is stored the thread handle. */
\r
267 pxThreadState = ( xThreadState * ) *( ( unsigned long * ) pxCurrentTCB );
\r
268 ulCriticalNesting = portNO_CRITICAL_NESTING;
\r
270 /* Bump up the priority of the thread that is going to run, in the
\r
271 hope that this will asist in getting the Windows thread scheduler to
\r
272 behave as an embedded engineer might expect. */
\r
273 ResumeThread( pxThreadState->pvThread );
\r
275 /* Handle all simulated interrupts - including yield requests and
\r
276 simulated ticks. */
\r
277 prvProcessSimulatedInterrupts();
\r
280 /* Would not expect to return from prvProcessSimulatedInterrupts(), so should
\r
284 /*-----------------------------------------------------------*/
\r
286 static unsigned long prvProcessYieldInterrupt( void )
\r
290 /*-----------------------------------------------------------*/
\r
292 static unsigned long prvProcessTickInterrupt( void )
\r
294 unsigned long ulSwitchRequired;
\r
296 /* Process the tick itself. */
\r
297 vTaskIncrementTick();
\r
298 #if( configUSE_PREEMPTION != 0 )
\r
300 /* A context switch is only automatically performed from the tick
\r
301 interrupt if the pre-emptive scheduler is being used. */
\r
302 ulSwitchRequired = pdTRUE;
\r
306 ulSwitchRequired = pdFALSE;
\r
310 return ulSwitchRequired;
\r
312 /*-----------------------------------------------------------*/
\r
314 static void prvProcessSimulatedInterrupts( void )
\r
316 unsigned long ulSwitchRequired, i;
\r
317 xThreadState *pxThreadState;
\r
318 void *pvObjectList[ 2 ];
\r
320 /* Going to block on the mutex that ensured exclusive access to the simulated
\r
321 interrupt objects, and the event that signals that a simulated interrupt
\r
322 should be processed. */
\r
323 pvObjectList[ 0 ] = pvInterruptEventMutex;
\r
324 pvObjectList[ 1 ] = pvInterruptEvent;
\r
328 WaitForMultipleObjects( sizeof( pvObjectList ) / sizeof( void * ), pvObjectList, TRUE, INFINITE );
\r
330 /* Used to indicate whether the simulated interrupt processing has
\r
331 necessitated a context switch to another task/thread. */
\r
332 ulSwitchRequired = pdFALSE;
\r
334 /* For each interrupt we are interested in processing, each of which is
\r
335 represented by a bit in the 32bit ulPendingInterrupts variable. */
\r
336 for( i = 0; i < portMAX_INTERRUPTS; i++ )
\r
338 /* Is the simulated interrupt pending? */
\r
339 if( ulPendingInterrupts & ( 1UL << i ) )
\r
341 /* Is a handler installed? */
\r
342 if( ulIsrHandler[ i ] != NULL )
\r
344 /* Run the actual handler. */
\r
345 if( ulIsrHandler[ i ]() != pdFALSE )
\r
347 ulSwitchRequired |= ( 1 << i );
\r
351 /* Clear the interrupt pending bit. */
\r
352 ulPendingInterrupts &= ~( 1UL << i );
\r
356 if( ulSwitchRequired != pdFALSE )
\r
358 void *pvOldCurrentTCB;
\r
360 pvOldCurrentTCB = pxCurrentTCB;
\r
362 /* Select the next task to run. */
\r
363 vTaskSwitchContext();
\r
365 /* If the task selected to enter the running state is not the task
\r
366 that is already in the running state. */
\r
367 if( pvOldCurrentTCB != pxCurrentTCB )
\r
369 /* Suspend the old thread. */
\r
370 pxThreadState = ( xThreadState *) *( ( unsigned long * ) pvOldCurrentTCB );
\r
371 SuspendThread( pxThreadState->pvThread );
\r
373 /* Obtain the state of the task now selected to enter the
\r
375 pxThreadState = ( xThreadState * ) ( *( unsigned long *) pxCurrentTCB );
\r
376 ResumeThread( pxThreadState->pvThread );
\r
380 ReleaseMutex( pvInterruptEventMutex );
\r
383 /*-----------------------------------------------------------*/
\r
385 void vPortDeleteThread( void *pvTaskToDelete )
\r
387 xThreadState *pxThreadState;
\r
389 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
391 /* Find the handle of the thread being deleted. */
\r
392 pxThreadState = ( xThreadState * ) ( *( unsigned long *) pvTaskToDelete );
\r
393 TerminateThread( pxThreadState->pvThread, 0 );
\r
395 ReleaseMutex( pvInterruptEventMutex );
\r
397 /*-----------------------------------------------------------*/
\r
399 void vPortEndScheduler( void )
\r
401 /* This function IS NOT TESTED! */
\r
402 TerminateProcess( GetCurrentProcess(), 0 );
\r
404 /*-----------------------------------------------------------*/
\r
406 void vPortGenerateSimulatedInterrupt( unsigned long ulInterruptNumber )
\r
408 xThreadState *pxThreadState;
\r
410 if( ( ulInterruptNumber < portMAX_INTERRUPTS ) && ( pvInterruptEventMutex != NULL ) )
\r
412 /* Yield interrupts are processed even when critical nesting is non-zero. */
\r
413 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
414 ulPendingInterrupts |= ( 1 << ulInterruptNumber );
\r
416 /* The simulated interrupt is now held pending, but don't actually process it
\r
417 yet if this call is within a critical section. It is possible for this to
\r
418 be in a critical section as calls to wait for mutexes are accumulative. */
\r
419 if( ulCriticalNesting == 0 )
\r
421 /* The event handler needs to know to signal the interrupt acknowledge event
\r
422 the next time this task runs. */
\r
423 pxThreadState = ( xThreadState * ) *( ( unsigned long * ) pxCurrentTCB );
\r
424 SetEvent( pvInterruptEvent );
\r
427 ReleaseMutex( pvInterruptEventMutex );
\r
430 /*-----------------------------------------------------------*/
\r
432 void vPortSetInterruptHandler( unsigned long ulInterruptNumber, unsigned long (*pvHandler)( void ) )
\r
434 if( ulInterruptNumber < portMAX_INTERRUPTS )
\r
436 if( pvInterruptEventMutex != NULL )
\r
438 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
439 ulIsrHandler[ ulInterruptNumber ] = pvHandler;
\r
440 ReleaseMutex( pvInterruptEventMutex );
\r
444 ulIsrHandler[ ulInterruptNumber ] = pvHandler;
\r
448 /*-----------------------------------------------------------*/
\r
450 void vPortEnterCritical( void )
\r
452 if( xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED )
\r
454 /* The interrupt event mutex is held for the entire critical section,
\r
455 effectively disabling (simulated) interrupts. */
\r
456 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
457 ulCriticalNesting++;
\r
461 ulCriticalNesting++;
\r
464 /*-----------------------------------------------------------*/
\r
466 void vPortExitCritical( void )
\r
468 xThreadState *pxThreadState;
\r
469 long lMutexNeedsReleasing;
\r
471 /* The interrupt event mutex should already be held by this thread as it was
\r
472 obtained on entry to the critical section. */
\r
474 lMutexNeedsReleasing = pdTRUE;
\r
476 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
\r
478 if( ulCriticalNesting == ( portNO_CRITICAL_NESTING + 1 ) )
\r
480 ulCriticalNesting--;
\r
482 /* Were any interrupts set to pending while interrupts were
\r
483 (simulated) disabled? */
\r
484 if( ulPendingInterrupts != 0UL )
\r
486 SetEvent( pvInterruptEvent );
\r
488 /* The event handler needs to know to signal the interrupt
\r
489 acknowledge event the next time this task runs. */
\r
490 pxThreadState = ( xThreadState * ) *( ( unsigned long * ) pxCurrentTCB );
\r
492 /* Mutex will be released now, so does not require releasing
\r
493 on function exit. */
\r
494 lMutexNeedsReleasing = pdFALSE;
\r
495 ReleaseMutex( pvInterruptEventMutex );
\r
500 /* Tick interrupts will still not be processed as the critical
\r
501 nesting depth will not be zero. */
\r
502 ulCriticalNesting--;
\r
506 if( lMutexNeedsReleasing == pdTRUE )
\r
508 ReleaseMutex( pvInterruptEventMutex );
\r
511 /*-----------------------------------------------------------*/
\r