]> git.sur5r.net Git - freertos/blob - Source/portable/MSVC-MingW/port.c
Start of new Win32 emulator project. Currently working but not well tested, and...
[freertos] / Source / portable / MSVC-MingW / port.c
1 /*\r
2     FreeRTOS V6.1.0 - Copyright (C) 2010 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\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
12     *                                                                         *\r
13     * then take a look at the FreeRTOS books - available as PDF or paperback  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\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
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \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
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /* Scheduler includes. */\r
55 #include "FreeRTOS.h"\r
56 #include "task.h"\r
57 #include <stdio.h>\r
58 \r
59 \r
60 \r
61 \r
62 \r
63 typedef struct xTaskControlBlock\r
64 {\r
65         volatile portSTACK_TYPE *pxTopOfStack;          /*< Points to the location of the last item placed on the tasks stack.  THIS MUST BE THE FIRST MEMBER OF THE STRUCT. */\r
66 \r
67         #if ( portUSING_MPU_WRAPPERS == 1 )\r
68                 xMPU_SETTINGS xMPUSettings;                             /*< The MPU settings are defined as part of the port layer.  THIS MUST BE THE SECOND MEMBER OF THE STRUCT. */\r
69         #endif  \r
70         \r
71         xListItem                               xGenericListItem;       /*< List item used to place the TCB in ready and blocked queues. */\r
72         xListItem                               xEventListItem;         /*< List item used to place the TCB in event lists. */\r
73         unsigned portBASE_TYPE  uxPriority;                     /*< The priority of the task where 0 is the lowest priority. */\r
74         portSTACK_TYPE                  *pxStack;                       /*< Points to the start of the stack. */\r
75         signed char                             pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created.  Facilitates debugging only. */\r
76 \r
77         #if ( portSTACK_GROWTH > 0 )\r
78                 portSTACK_TYPE *pxEndOfStack;                   /*< Used for stack overflow checking on architectures where the stack grows up from low memory. */\r
79         #endif\r
80 \r
81         #if ( portCRITICAL_NESTING_IN_TCB == 1 )\r
82                 unsigned portBASE_TYPE uxCriticalNesting;\r
83         #endif\r
84 \r
85         #if ( configUSE_TRACE_FACILITY == 1 )\r
86                 unsigned portBASE_TYPE  uxTCBNumber;    /*< This is used for tracing the scheduler and making debugging easier only. */\r
87         #endif\r
88 \r
89         #if ( configUSE_MUTEXES == 1 )\r
90                 unsigned portBASE_TYPE uxBasePriority;  /*< The priority last assigned to the task - used by the priority inheritance mechanism. */\r
91         #endif\r
92 \r
93         #if ( configUSE_APPLICATION_TASK_TAG == 1 )\r
94                 pdTASK_HOOK_CODE pxTaskTag;\r
95         #endif\r
96 \r
97         #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
98                 unsigned long ulRunTimeCounter;         /*< Used for calculating how much CPU time each task is utilising. */\r
99         #endif\r
100 \r
101 } xTCB;\r
102 \r
103 \r
104 \r
105 \r
106 \r
107 \r
108 \r
109 \r
110 \r
111 FILE *pfTraceFile = NULL;\r
112 //#define vPortTrace( x ) if( pfTraceFile == NULL ) pfTraceFile = fopen( "c:/temp/trace.txt", "w" ); if( pfTraceFile != NULL ) fprintf( pfTraceFile, x )\r
113 #define vPortTrace( x ) ( void ) x\r
114 \r
115 #define portMAX_INTERRUPTS                              ( ( unsigned long ) sizeof( unsigned long ) * 8UL ) /* The number of bits in an unsigned long. */\r
116 #define portNO_CRITICAL_NESTING                 ( ( unsigned long ) 0 )\r
117 \r
118 /*\r
119  * Created as a high priority thread, this function uses a timer to simulate\r
120  * a tick interrupt being generated on an embedded target.  In this Windows\r
121  * environment the timer does not achieve real time performance though.\r
122  */\r
123 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter );\r
124 \r
125 /* \r
126  * Process all the simulated interrupts - each represented by a bit in \r
127  * ulPendingInterrupts variable.\r
128  */\r
129 static void prvProcessEvents( void );\r
130 \r
131 /*-----------------------------------------------------------*/\r
132 \r
133 /* The WIN32 simulator runs each task in a thread.  The context switching is\r
134 managed by the threads, so the task stack does not have to be managed directly,\r
135 although the task stack is still used to hold an xThreadState structure this is\r
136 the only thing it will ever hold.  The structure indirectly maps the task handle \r
137 to a thread handle. */\r
138 typedef struct\r
139 {\r
140         portSTACK_TYPE ulCriticalNesting;       /* Critical nesting count of the task. */\r
141         void * pvThread;                                        /* Handle of the thread that executes the task. */\r
142 } xThreadState;\r
143 \r
144 /* The parameters passed to a thread when it is created. */\r
145 typedef struct XPARAMS\r
146 {\r
147         pdTASK_CODE pxCode;             /* The entry point of the task (rather than thread) code. */\r
148         void *pvParameters;             /* The parameters that are passed to the task (rather than thread. */\r
149 } xParams;\r
150 \r
151 /* Pseudo interrupts waiting to be processed.  This is a bit mask where each\r
152 bit represents one interrupt, so a maximum of 32 interrupts can be simulated. */\r
153 static volatile unsigned long ulPendingInterrupts = 0UL;\r
154 \r
155 /* An event used to inform the interrupt dispatch thread (a high priority thread\r
156 that simulated interrupt processing) that an IRQ or SWI type interrupt is\r
157 pending. */\r
158 static void *pvInterruptEvent = NULL;\r
159 \r
160 /* Mutex used to protect all the pseudo interrupt variables that are accessed by\r
161 multiple threads. */\r
162 static void *pvInterruptEventMutex = NULL;\r
163 \r
164 /* The main thread, which also acts as the pseudo interrupt handler. */\r
165 static void *pvMainThreadAndInterrupHandler;\r
166 \r
167 /* Events used to manage sequencing. */\r
168 static void *pvTickAcknowledgeEvent = NULL, *pvInterruptAcknowledgeEvent = NULL;\r
169 \r
170 /* The critical nesting count for the currently executing task.  This is \r
171 initialised to a non-zero value so interrupts do not become enabled during \r
172 the initialisation phase.  As each task has its own critical nesting value \r
173 ulCriticalNesting will get set to zero when the first task runs.  This \r
174 initialisation is probably not critical in this simulated environment as the\r
175 pseudo interrupt handlers/dispatchers do not get created until the FreeRTOS\r
176 scheduler is started. */\r
177 static unsigned portLONG ulCriticalNesting = 9999UL;\r
178 \r
179 /* Handlers for all the simulated software interrupts.  The first two positions\r
180 are used for the Yield and Tick interrupts so are handled slightly differently,\r
181 all the other interrupts can be user defined. */\r
182 static void (*vIsrHandler[ portMAX_INTERRUPTS ])( void ) = { 0 };\r
183 \r
184 /* Pointer to the TCB of the currently executing task. */\r
185 extern void *pxCurrentTCB;\r
186 \r
187 /*-----------------------------------------------------------*/\r
188 \r
189 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )\r
190 {\r
191 void *pvTimer;\r
192 LARGE_INTEGER liDueTime;\r
193 void *pvObjectList[ 2 ];\r
194 const long long ll_ms_In_100ns_units = ( long long ) -1000;\r
195 extern volatile unsigned long ulTicks;\r
196 \r
197         /* Just to prevent compiler warnings. */\r
198         ( void ) lpParameter;\r
199 \r
200         /* The timer is created as a one shot timer even though we want it to repeat\r
201         at a given frequency.  This is because Windows is not a real time environment,\r
202         and attempting to set a high frequency periodic timer will result in event\r
203         overruns.  Therefore the timer is just reset after each time the pseudo \r
204         interrupt handler has processed each tick event. */\r
205         pvTimer = CreateWaitableTimer( NULL, TRUE, NULL );\r
206         \r
207         liDueTime.QuadPart = ( long long ) portTICK_RATE_MS * ll_ms_In_100ns_units;\r
208 \r
209         /* Block on the timer itself and the event mutex that grants access to the \r
210         interrupt variables. */\r
211         pvObjectList[ 0 ] = pvInterruptEventMutex;\r
212         pvObjectList[ 1 ] = pvTimer;\r
213 \r
214         for(;;)\r
215         {\r
216                 ulTicks++;\r
217 \r
218                 /* The timer is reset on each itteration of this loop rather than being set\r
219                 to function periodicallys - this is for the reasons stated in the comments\r
220                 where the timer is created. */\r
221                 vPortTrace( "prvSimulatedPeripheralTimer: Tick acked, setting new tick timer\r\n" );\r
222                 SetWaitableTimer( pvTimer, &liDueTime, 0, NULL, NULL, TRUE );\r
223 \r
224                 /* Wait until the timer expires and we can access the pseudo interrupt \r
225                 variables. */\r
226                 //WaitForMultipleObjects( ( sizeof( pvObjectList ) / sizeof( void * ) ), pvObjectList, TRUE, INFINITE );\r
227                 WaitForSingleObject( pvTimer, INFINITE );\r
228                 vPortTrace( "prvSimulatedPeripheralTimer: Timer signalled, waiting interrupt event mutex\r\n" );\r
229                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
230                 vPortTrace( "prvSimulatedPeripheralTimer: Got interrupt event mutex\r\n" );\r
231                 \r
232                 /* The timer has expired, generate the simulated tick event. */\r
233                 ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );\r
234                 if( pvInterruptEvent != NULL )\r
235                 {\r
236                         vPortTrace( "prvSimulatedPeripheralTimer: Setting interrupt event to signal tick\r\n" );\r
237                         SetEvent( pvInterruptEvent );\r
238                 }\r
239 \r
240                 /* Give back the mutex so the pseudo interrupt handler unblocks and can\r
241                 access the interrupt handler variables.  This high priority task will then\r
242                 loop back round to wait for the lower priority psuedo interrupt handler \r
243                 thread to acknowledge the tick. */\r
244                 if( pvInterruptEventMutex != NULL )\r
245                 {\r
246                         vPortTrace( "prvSimulatedPeripheralTimer: Releasing interrupt event mutex so tick can be processed\r\n" );\r
247                         ReleaseMutex( pvInterruptEventMutex );\r
248                 }\r
249 \r
250                 /* Wait for the previous tick to be acknowledged before resetting the timer.\r
251                 As mentioned above this is done to prevent timer overruns in the non real-\r
252                 time environment.  THIS IS NOT HOW A REAL PORT SHOULD USE TIMERS! */\r
253                 WaitForSingleObject( pvTickAcknowledgeEvent, INFINITE );\r
254         }\r
255 }\r
256 /*-----------------------------------------------------------*/\r
257 \r
258 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
259 {\r
260 xThreadState *pxThreadState = NULL;\r
261 xParams *pxThreadParams = ( void * ) pvPortMalloc( sizeof( xParams ) );\r
262 \r
263         if( pxThreadParams != NULL )\r
264         {\r
265                 /* In this simulated case a stack is not initialised, but instead a thread\r
266                 is created that will execute the task being created.  The thread handles\r
267                 the context switching itself.  The xThreadState object is placed onto\r
268                 the stack that was created for the task - so the stack buffer is still\r
269                 used, just not in the conventional way.  It will not be used for anything\r
270                 other than holding this structure. */\r
271                 pxThreadState = ( xThreadState * ) ( pxTopOfStack - sizeof( xThreadState ) );\r
272 \r
273                 /* The parameters that are passed into the thread so it knows how to\r
274                 start the task executing. */\r
275                 pxThreadParams->pxCode = pxCode;\r
276                 pxThreadParams->pvParameters = pvParameters;\r
277 \r
278                 /* Create the thread itself. */\r
279                 //pxThreadState->pvThread = ( void * ) CreateThread( NULL, 0, ( LPTHREAD_START_ROUTINE ) prvThreadEntryPoint, pxThreadParams, CREATE_SUSPENDED, NULL );\r
280                 pxThreadState->pvThread = ( void * ) CreateThread( NULL, 0, ( LPTHREAD_START_ROUTINE ) pxCode, pvParameters, CREATE_SUSPENDED, NULL );\r
281                 pxThreadState->ulCriticalNesting = portNO_CRITICAL_NESTING;\r
282                 SetThreadPriority( pxThreadState->pvThread, THREAD_PRIORITY_IDLE );\r
283         }\r
284         \r
285         return ( portSTACK_TYPE * ) pxThreadState;\r
286 }\r
287 /*-----------------------------------------------------------*/\r
288 \r
289 portBASE_TYPE xPortStartScheduler( void )\r
290 {\r
291 void *pvHandle;\r
292 long lSuccess = pdPASS;\r
293 xThreadState *pxThreadState;\r
294 \r
295         /* Set the priority of this thread such that it is above the priority of the\r
296         threads that run tasks, but below the priority of the thread that generates\r
297         the pseudo tick interrupts.  This priority is chosen because this is the\r
298         thread that actually handles the psuedo interrupts. */\r
299         pvHandle = GetCurrentThread();\r
300         if( pvHandle == NULL )\r
301         {\r
302                 lSuccess = pdFAIL;\r
303         }\r
304         \r
305         if( lSuccess == pdPASS )\r
306         {\r
307                 if( SetThreadPriority( pvHandle, THREAD_PRIORITY_BELOW_NORMAL ) == 0 )\r
308                 {\r
309                         lSuccess = pdFAIL;\r
310                 }\r
311         }\r
312 \r
313         if( lSuccess == pdPASS )\r
314         {\r
315                 /* Create the events and mutexes that are used to synchronise all the\r
316                 threads. */\r
317                 pvInterruptEventMutex = CreateMutex( NULL, FALSE, NULL );\r
318                 pvInterruptEvent = CreateEvent( NULL, FALSE, FALSE, NULL );\r
319                 pvTickAcknowledgeEvent = CreateEvent( NULL, FALSE, FALSE, NULL );\r
320                 pvInterruptAcknowledgeEvent = CreateEvent( NULL, FALSE, FALSE, NULL );\r
321 \r
322                 /* Start the thread that simulates the timer peripheral to generate\r
323                 tick interrupts. */\r
324                 pvHandle = CreateThread( NULL, 0, prvSimulatedPeripheralTimer, NULL, 0, NULL );\r
325                 if( pvHandle != NULL )\r
326                 {\r
327                         SetThreadPriority( pvHandle, THREAD_PRIORITY_ABOVE_NORMAL );\r
328                 }\r
329                 \r
330                 /* Start the highest priority task by obtaining its associated thread state\r
331                 structure, in which is stored the thread handle. */\r
332                 pxThreadState = ( xThreadState * ) *( ( unsigned long * ) pxCurrentTCB );\r
333                 ulCriticalNesting = portNO_CRITICAL_NESTING;\r
334 \r
335                 vPortTrace( "Created system threads, starting task" );\r
336 \r
337                 ResumeThread( pxThreadState->pvThread );\r
338         }       \r
339         \r
340         /* Handle all pseudo interrupts - including yield requests and simulated ticks. */\r
341         prvProcessEvents();\r
342 \r
343         /* Would not expect to return from prvProcessEvents(), so should not get here. */\r
344         return 0;\r
345 }\r
346 /*-----------------------------------------------------------*/\r
347 \r
348 static void prvProcessEvents( void )\r
349 {\r
350 long lSwitchRequired, lAcknowledgeTick, lAcknowledgeInterrupt;\r
351 xThreadState *pxThreadState;\r
352 void *pvObjectList[ 2 ];\r
353 unsigned long i;\r
354 char cTraceBuffer[ 256 ];\r
355 \r
356         vPortTrace( "Entering prvProcessEvents\r\n" );\r
357 \r
358         /* Going to block on the mutex that ensured exclusive access to the pdeudo \r
359         interrupt objects, and the event that signals that an interrupt is waiting\r
360         to be processed. */\r
361         pvObjectList[ 0 ] = pvInterruptEventMutex;\r
362         pvObjectList[ 1 ] = pvInterruptEvent;\r
363 \r
364         for(;;)\r
365         {\r
366                 vPortTrace( "prvProcessEvents: Waiting for next interrupt event\r\n" );\r
367                 WaitForMultipleObjects( sizeof( pvObjectList ) / sizeof( void * ), pvObjectList, TRUE, INFINITE );\r
368                 vPortTrace( "prvProcessEvents: Got interrupt event and mutex\r\n" );\r
369                 //vPortTrace( "prvProcessEvents: Waiting for next interrupt event\r\n" );\r
370                 //WaitForSingleObject( pvInterruptEvent, INFINITE );\r
371                 //vPortTrace( "prvProcessEvents: Waiting interrupt event mutex to access interrupt data\r\n" );\r
372                 //WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
373 \r
374                 lSwitchRequired = pdFALSE;\r
375                 lAcknowledgeTick = pdFALSE;\r
376                 lAcknowledgeInterrupt = pdFALSE;\r
377 \r
378                 /* For each interrupt we are interested in processing, each of which is \r
379                 represented by a bit in the 32bit ulPendingInterrupts variable. */\r
380                 for( i = 0; i < portMAX_INTERRUPTS; i++ )\r
381                 {\r
382                         /* Is the pseudo interrupt pending? */\r
383                         if( ulPendingInterrupts & ( 1UL << i ) )\r
384                         {\r
385                                 switch( i )\r
386                                 {\r
387                                         case portINTERRUPT_YIELD:\r
388 \r
389                                                 vPortTrace( "prvProcessEvents: Processing Yield\r\n" );\r
390                                                 /* Yield interrupts occur no matter what the critical nesting count. */\r
391                                                 lSwitchRequired = pdTRUE;\r
392 \r
393                                                 /* Clear the interrupt pending bit. */\r
394                                                 ulPendingInterrupts &= ~( 1UL << portINTERRUPT_YIELD );\r
395 \r
396                                                 lAcknowledgeInterrupt = pdTRUE;\r
397                                                 break;\r
398 \r
399                                         case portINTERRUPT_TICK:\r
400                                         \r
401                                                 /* Tick interrupts should only be processed if the critical nesting count\r
402                                                 is zero.  The critical nesting count represents the interrupt mask on \r
403                                                 real target hardware. */\r
404                                                 vPortTrace( "prvProcessEvents: Processing tick event\r\n" );\r
405                                                 if( ulCriticalNesting == 0 )\r
406                                                 {\r
407                                                         /* Process the tick itself. */\r
408                                                         vPortTrace( "prvProcessEvents: Incrementing tick\r\n" );\r
409                                                         vTaskIncrementTick();\r
410                                                         #if( configUSE_PREEMPTION != 0 )\r
411                                                         {\r
412                                                                 /* A context switch is only automatically performed from the tick\r
413                                                                 interrupt if the pre-emptive scheduler is being used. */\r
414                                                                 lSwitchRequired = pdTRUE;\r
415                                                         }\r
416                                                         #endif\r
417                                                         \r
418                                                         lAcknowledgeTick = pdTRUE;\r
419 \r
420                                                         /* Clear the interrupt pending bit. */\r
421                                                         ulPendingInterrupts &= ~( 1UL << portINTERRUPT_TICK );\r
422                                                 }\r
423                                                 break;\r
424 \r
425                                         default:\r
426 \r
427                                                 /* Is a handler installed? */\r
428                                                 if( vIsrHandler[ i ] != NULL )\r
429                                                 {\r
430                                                         lSwitchRequired = pdTRUE;\r
431 \r
432                                                         /* Run the actual handler. */\r
433                                                         vIsrHandler[ i ]();\r
434 \r
435                                                         /* Clear the interrupt pending bit. */\r
436                                                         ulPendingInterrupts &= ~( 1UL << i );\r
437 \r
438                                                         lAcknowledgeInterrupt = pdTRUE;\r
439                                                 }\r
440                                                 break;\r
441                                 }\r
442                         }\r
443                 }\r
444 \r
445                 if( lSwitchRequired != pdFALSE )\r
446                 {\r
447                         void *pvOldCurrentTCB;\r
448 \r
449                         pvOldCurrentTCB = pxCurrentTCB;\r
450 \r
451                         /* Save the state of the current thread before suspending it. */\r
452                         pxThreadState = ( xThreadState *) *( ( unsigned long * ) pxCurrentTCB );\r
453                         pxThreadState->ulCriticalNesting = ulCriticalNesting ;\r
454                         \r
455                         /* Select the next task to run. */\r
456                         vTaskSwitchContext();\r
457                         \r
458                         /* If the task selected to enter the running state is not the task\r
459                         that is already in the running state. */\r
460                         if( pvOldCurrentTCB != pxCurrentTCB )\r
461                         {\r
462                                 /* Suspend the old thread. */\r
463                                 SuspendThread( pxThreadState->pvThread );\r
464                                 sprintf( cTraceBuffer, "Event processor: suspending %s, resuming %s\r\n", ((xTCB*)pvOldCurrentTCB)->pcTaskName, ((xTCB*)pxCurrentTCB)->pcTaskName );\r
465                                 vPortTrace( cTraceBuffer );\r
466 \r
467                                 /* Obtain the state of the task now selected to enter the Running state. */\r
468                                 pxThreadState = ( xThreadState * ) ( *( unsigned long *) pxCurrentTCB );\r
469                                 ulCriticalNesting = pxThreadState->ulCriticalNesting;\r
470                                 ResumeThread( pxThreadState->pvThread );\r
471                         }\r
472                 }\r
473 \r
474                 /* Was a tick processed? */\r
475                 if( lAcknowledgeTick != pdFALSE )\r
476                 {\r
477                         vPortTrace( "prvProcessEvents: Acking tick\r\n" );\r
478                         SetEvent( pvTickAcknowledgeEvent );\r
479                 }\r
480 \r
481                 if( lAcknowledgeInterrupt != pdFALSE )\r
482                 {\r
483                         vPortTrace( "prvProcessEvents: Acking interrupt\r\n" );\r
484                         SetEvent( pvInterruptAcknowledgeEvent );\r
485                 }\r
486 \r
487                 ReleaseMutex( pvInterruptEventMutex );\r
488         }\r
489 }\r
490 /*-----------------------------------------------------------*/\r
491 \r
492 void vPortEndScheduler( void )\r
493 {\r
494 }\r
495 /*-----------------------------------------------------------*/\r
496 \r
497 void vPortGeneratePseudoInterrupt( unsigned long ulInterruptNumber )\r
498 {\r
499         if( ( ulInterruptNumber < portMAX_INTERRUPTS ) && ( pvInterruptEventMutex != NULL ) )\r
500         {\r
501                 /* Yield interrupts are processed even when critical nesting is non-zero. */\r
502                 if( ( ulCriticalNesting == 0 ) || ( ulInterruptNumber == portINTERRUPT_YIELD ) )\r
503                 {\r
504                         /* In case this task has just started running, reset the interrupt\r
505                         acknowledge event as it might have been set due to the activities\r
506                         of a thread that has already been executed and suspended. */\r
507                         ResetEvent( pvInterruptAcknowledgeEvent );\r
508 \r
509                         WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
510                         ulPendingInterrupts |= ( 1 << ulInterruptNumber );\r
511                         vPortTrace( "vPortGeneratePseudoInterrupt: Got interrupt mutex, about to signal interrupt event\r\n" );\r
512                         SetEvent( pvInterruptEvent );\r
513                         vPortTrace( "vPortGeneratePseudoInterrupt: About to release interrupt event mutex\r\n" );\r
514                         ReleaseMutex( pvInterruptEventMutex );\r
515                         vPortTrace( "vPortGeneratePseudoInterrupt: Interrupt event mutex released, going to wait for next interrupt input\r\n" );\r
516 \r
517                         WaitForSingleObject( pvInterruptAcknowledgeEvent, INFINITE );\r
518                         vPortTrace( "vPortGeneratePseudoInterrupt: Interrupt acknowledged, leaving vPortGeneratePseudoInterrupt()\r\n" );\r
519                 }\r
520         }\r
521 }\r
522 /*-----------------------------------------------------------*/\r
523 \r
524 void vPortSetInterruptHandler( unsigned long ulInterruptNumber, void (*pvHandler)( void ) )\r
525 {\r
526         if( ulInterruptNumber < portMAX_INTERRUPTS )\r
527         {\r
528                 if( pvInterruptEventMutex != NULL )\r
529                 {\r
530                         WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
531                         vIsrHandler[ ulInterruptNumber ] = pvHandler;\r
532                         ReleaseMutex( pvInterruptEventMutex );\r
533                 }\r
534                 else\r
535                 {\r
536                         vIsrHandler[ ulInterruptNumber ] = pvHandler;\r
537                 }\r
538         }\r
539 }\r
540 /*-----------------------------------------------------------*/\r
541 \r
542 void vPortEnterCritical( void )\r
543 {\r
544         ulCriticalNesting++;\r
545 }\r
546 /*-----------------------------------------------------------*/\r
547 \r
548 void vPortExitCritical( void )\r
549 {\r
550         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
551         {\r
552                 ulCriticalNesting--;\r
553 \r
554                 if( ulCriticalNesting == 0 )\r
555                 {\r
556                         /* Were any interrupts set to pending while interrupts were \r
557                         (pseudo) disabled? */\r
558                         if( ulPendingInterrupts != 0UL )\r
559                         {\r
560                                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
561                                 vPortTrace( "vPortExitCritical:  Setting interrupt event\r\n" );\r
562                                 SetEvent( pvInterruptEvent );\r
563                                 ReleaseMutex( pvInterruptEventMutex );\r
564 \r
565                                 vPortTrace( "vPortExitCritical:  Waiting interrupt ack\r\n" );\r
566                                 WaitForSingleObject( pvInterruptAcknowledgeEvent, INFINITE );\r
567                                 vPortTrace( "vPortExitCritical: Interrupt acknowledged, leaving critical section code\r\n" );\r
568 \r
569                                 /* Just in case the Yield does not happen immediately.  This\r
570                                 line could be dangerious if not all interrupts are being\r
571                                 processed. */\r
572 //                              while( ulPendingInterrupts != 0UL );\r
573                         }\r
574                 }\r
575         }\r
576 }\r