]> git.sur5r.net Git - freertos/blob - Source/portable/MSVC-MingW/port.c
e880a0977808f19226e7b5865f3cef2f555ac66e
[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 //FILE *pfTraceFile = NULL;\r
60 //#define vPortTrace( x ) if( pfTraceFile == NULL ) pfTraceFile = fopen( "c:/temp/trace.txt", "w" ); if( pfTraceFile != NULL ) fprintf( pfTraceFile, x )\r
61 #define vPortTrace( x ) ( void ) x\r
62 \r
63 #define portMAX_INTERRUPTS                              ( ( unsigned long ) sizeof( unsigned long ) * 8UL ) /* The number of bits in an unsigned long. */\r
64 #define portNO_CRITICAL_NESTING                 ( ( unsigned long ) 0 )\r
65 \r
66 /*\r
67  * Created as a high priority thread, this function uses a timer to simulate\r
68  * a tick interrupt being generated on an embedded target.  In this Windows\r
69  * environment the timer does not achieve anything approaching real time \r
70  * performance though.\r
71  */\r
72 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter );\r
73 \r
74 /* \r
75  * Process all the simulated interrupts - each represented by a bit in \r
76  * ulPendingInterrupts variable.\r
77  */\r
78 static void prvProcessPseudoInterrupts( void );\r
79 \r
80 /*-----------------------------------------------------------*/\r
81 \r
82 /* The WIN32 simulator runs each task in a thread.  The context switching is\r
83 managed by the threads, so the task stack does not have to be managed directly,\r
84 although the task stack is still used to hold an xThreadState structure this is\r
85 the only thing it will ever hold.  The structure indirectly maps the task handle \r
86 to a thread handle. */\r
87 typedef struct\r
88 {\r
89         /* Set to true if the task run by the thread yielded control to the pseudo\r
90         interrupt handler manually - either by yielding or when exiting a critical\r
91         section while pseudo interrupts were pending. */\r
92         long lWaitingInterruptAck;                      \r
93 \r
94         /* Critical nesting count of the task - each task has its own. */\r
95         portSTACK_TYPE ulCriticalNesting;\r
96 \r
97         /* Handle of the thread that executes the task. */\r
98         void * pvThread;                                        \r
99 } xThreadState;\r
100 \r
101 /* Pseudo interrupts waiting to be processed.  This is a bit mask where each\r
102 bit represents one interrupt, so a maximum of 32 interrupts can be simulated. */\r
103 static volatile unsigned long ulPendingInterrupts = 0UL;\r
104 \r
105 /* An event used to inform the pseudo interrupt processing thread (a high \r
106 priority thread that simulated interrupt processing) that an interrupt is\r
107 pending. */\r
108 static void *pvInterruptEvent = NULL;\r
109 \r
110 /* Mutex used to protect all the pseudo interrupt variables that are accessed \r
111 by multiple threads. */\r
112 static void *pvInterruptEventMutex = NULL;\r
113 \r
114 /* Events used to manage sequencing. */\r
115 static void *pvTickAcknowledgeEvent = NULL, *pvInterruptAcknowledgeEvent = NULL;\r
116 \r
117 /* The critical nesting count for the currently executing task.  This is \r
118 initialised to a non-zero value so interrupts do not become enabled during \r
119 the initialisation phase.  As each task has its own critical nesting value \r
120 ulCriticalNesting will get set to zero when the first task runs.  This \r
121 initialisation is probably not critical in this simulated environment as the\r
122 pseudo interrupt handlers do not get created until the FreeRTOS scheduler is \r
123 started anyway. */\r
124 static unsigned long ulCriticalNesting = 9999UL;\r
125 \r
126 /* Handlers for all the simulated software interrupts.  The first two positions\r
127 are used for the Yield and Tick interrupts so are handled slightly differently,\r
128 all the other interrupts can be user defined. */\r
129 static void (*vIsrHandler[ portMAX_INTERRUPTS ])( void ) = { 0 };\r
130 \r
131 /* Pointer to the TCB of the currently executing task. */\r
132 extern void *pxCurrentTCB;\r
133 \r
134 /*-----------------------------------------------------------*/\r
135 \r
136 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )\r
137 {\r
138         /* Just to prevent compiler warnings. */\r
139         ( void ) lpParameter;\r
140 \r
141         for(;;)\r
142         {\r
143                 vPortTrace( "prvSimulatedPeripheralTimer: Tick acked, re-Sleeping()\r\n" );\r
144 \r
145                 /* Wait until the timer expires and we can access the pseudo interrupt \r
146                 variables.  *NOTE* this is not a 'real time' way of generating tick \r
147                 events as the next wake time should be relative to the previous wake \r
148                 time, not the time that Sleep() is called.  It is done this way to \r
149                 prevent overruns in this very non real time simulated/emulated \r
150                 environment. */\r
151                 Sleep( portTICK_RATE_MS );\r
152 \r
153                 vPortTrace( "prvSimulatedPeripheralTimer: Sleep expired, waiting interrupt event mutex\r\n" );\r
154                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
155                 vPortTrace( "prvSimulatedPeripheralTimer: Got interrupt event mutex\r\n" );\r
156                 \r
157                 /* The timer has expired, generate the simulated tick event. */\r
158                 ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );\r
159 \r
160                 /* The interrupt is now pending - but should only be processed if\r
161                 interrupts are actually enabled. */\r
162                 if( ulCriticalNesting == 0UL )\r
163                 {\r
164                         vPortTrace( "prvSimulatedPeripheralTimer: Setting interrupt event to signal tick\r\n" );\r
165                         SetEvent( pvInterruptEvent );\r
166 \r
167                         /* Give back the mutex so the pseudo interrupt handler unblocks \r
168                         and can access the interrupt handler variables.  This high priority\r
169                         task will then loop back round after waiting for the lower priority \r
170                         pseudo interrupt handler thread to acknowledge the tick. */\r
171                         vPortTrace( "prvSimulatedPeripheralTimer: Releasing interrupt event mutex so tick can be processed\r\n" );\r
172                         SignalObjectAndWait( pvInterruptEventMutex, pvTickAcknowledgeEvent, INFINITE, FALSE );\r
173                 }\r
174                 else\r
175                 {\r
176                         ReleaseMutex( pvInterruptEventMutex );\r
177                 }\r
178         }\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
183 {\r
184 xThreadState *pxThreadState = NULL;\r
185 \r
186         /* In this simulated case a stack is not initialised, but instead a thread\r
187         is created that will execute the task being created.  The thread handles\r
188         the context switching itself.  The xThreadState object is placed onto\r
189         the stack that was created for the task - so the stack buffer is still\r
190         used, just not in the conventional way.  It will not be used for anything\r
191         other than holding this structure. */\r
192         pxThreadState = ( xThreadState * ) ( pxTopOfStack - sizeof( xThreadState ) );\r
193 \r
194         /* Create the thread itself. */\r
195         pxThreadState->pvThread = ( void * ) CreateThread( NULL, 0, ( LPTHREAD_START_ROUTINE ) pxCode, pvParameters, CREATE_SUSPENDED, NULL );\r
196         SetThreadPriorityBoost( pxThreadState->pvThread, TRUE );\r
197         pxThreadState->ulCriticalNesting = portNO_CRITICAL_NESTING;\r
198         pxThreadState->lWaitingInterruptAck = pdFALSE;\r
199         SetThreadPriority( pxThreadState->pvThread, THREAD_PRIORITY_IDLE );\r
200         \r
201         return ( portSTACK_TYPE * ) pxThreadState;\r
202 }\r
203 /*-----------------------------------------------------------*/\r
204 \r
205 portBASE_TYPE xPortStartScheduler( void )\r
206 {\r
207 void *pvHandle;\r
208 long lSuccess = pdPASS;\r
209 xThreadState *pxThreadState;\r
210 \r
211         /* Create the events and mutexes that are used to synchronise all the\r
212         threads. */\r
213         pvInterruptEventMutex = CreateMutex( NULL, FALSE, NULL );\r
214         pvInterruptEvent = CreateEvent( NULL, FALSE, FALSE, NULL );\r
215         pvTickAcknowledgeEvent = CreateEvent( NULL, FALSE, FALSE, NULL );\r
216         pvInterruptAcknowledgeEvent = CreateEvent( NULL, FALSE, FALSE, NULL );\r
217 \r
218         if( ( pvInterruptEventMutex == NULL ) || ( pvInterruptEvent == NULL ) || ( pvTickAcknowledgeEvent == NULL ) || ( pvInterruptAcknowledgeEvent == NULL ) )\r
219         {\r
220                 lSuccess = pdFAIL;\r
221         }\r
222 \r
223         /* Set the priority of this thread such that it is above the priority of \r
224         the threads that run tasks.  This higher priority is required to ensure\r
225         pseudo interrupts take priority over tasks. */\r
226         SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS );\r
227         pvHandle = GetCurrentThread();\r
228         if( pvHandle == NULL )\r
229         {\r
230                 lSuccess = pdFAIL;\r
231         }\r
232         \r
233         if( lSuccess == pdPASS )\r
234         {\r
235                 if( SetThreadPriority( pvHandle, THREAD_PRIORITY_HIGHEST ) == 0 )\r
236                 {\r
237                         lSuccess = pdFAIL;\r
238                 }\r
239                 SetThreadPriorityBoost( pvHandle, TRUE );\r
240         }\r
241 \r
242         if( lSuccess == pdPASS )\r
243         {\r
244                 /* Start the thread that simulates the timer peripheral to generate\r
245                 tick interrupts. */\r
246                 pvHandle = CreateThread( NULL, 0, prvSimulatedPeripheralTimer, NULL, 0, NULL );\r
247                 if( pvHandle != NULL )\r
248                 {\r
249                         SetThreadPriority( pvHandle, THREAD_PRIORITY_HIGHEST );\r
250                         SetThreadPriorityBoost( pvHandle, TRUE );\r
251                 }\r
252                 \r
253                 /* Start the highest priority task by obtaining its associated thread \r
254                 state structure, in which is stored the thread handle. */\r
255                 pxThreadState = ( xThreadState * ) *( ( unsigned long * ) pxCurrentTCB );\r
256                 ulCriticalNesting = portNO_CRITICAL_NESTING;\r
257 \r
258                 vPortTrace( "Created system threads, starting task" );\r
259 \r
260                 /* Bump up the priority of the thread that is going to run, in the\r
261                 hope that this will asist in getting the Windows thread scheduler to\r
262                 behave as an embedded engineer might expect. */\r
263                 SetThreadPriority( pxThreadState->pvThread, THREAD_PRIORITY_ABOVE_NORMAL );\r
264                 ResumeThread( pxThreadState->pvThread );\r
265 \r
266                 /* Handle all pseudo interrupts - including yield requests and \r
267                 simulated ticks. */\r
268                 prvProcessPseudoInterrupts();\r
269         }       \r
270         \r
271         /* Would not expect to return from prvProcessPseudoInterrupts(), so should \r
272         not get here. */\r
273         return 0;\r
274 }\r
275 /*-----------------------------------------------------------*/\r
276 \r
277 static void prvProcessPseudoInterrupts( void )\r
278 {\r
279 long lSwitchRequired;\r
280 xThreadState *pxThreadState;\r
281 void *pvObjectList[ 2 ];\r
282 unsigned long i;\r
283 //char cTraceBuffer[ 256 ];\r
284 \r
285         vPortTrace( "Entering prvProcessPseudoInterrupts\r\n" );\r
286 \r
287         /* Going to block on the mutex that ensured exclusive access to the pseudo \r
288         interrupt objects, and the event that signals that a pseudo interrupt\r
289         should be processed. */\r
290         pvObjectList[ 0 ] = pvInterruptEventMutex;\r
291         pvObjectList[ 1 ] = pvInterruptEvent;\r
292 \r
293         for(;;)\r
294         {\r
295                 vPortTrace( "prvProcessPseudoInterrupts: Waiting for next interrupt event\r\n" );\r
296                 WaitForMultipleObjects( sizeof( pvObjectList ) / sizeof( void * ), pvObjectList, TRUE, INFINITE );\r
297                 vPortTrace( "prvProcessPseudoInterrupts: Got interrupt event and mutex\r\n" );\r
298 \r
299                 /* Used to indicate whether the pseudo interrupt processing has\r
300                 necessitated a context switch to another task/thread. */\r
301                 lSwitchRequired = pdFALSE;\r
302 \r
303                 /* For each interrupt we are interested in processing, each of which is\r
304                 represented by a bit in the 32bit ulPendingInterrupts variable. */\r
305                 for( i = 0; i < portMAX_INTERRUPTS; i++ )\r
306                 {\r
307                         /* Is the pseudo interrupt pending? */\r
308                         if( ulPendingInterrupts & ( 1UL << i ) )\r
309                         {\r
310                                 switch( i )\r
311                                 {\r
312                                         case portINTERRUPT_YIELD:\r
313 \r
314                                                 vPortTrace( "prvProcessPseudoInterrupts: Processing Yield\r\n" );\r
315                                                 lSwitchRequired = pdTRUE;\r
316 \r
317                                                 /* Clear the interrupt pending bit. */\r
318                                                 ulPendingInterrupts &= ~( 1UL << portINTERRUPT_YIELD );\r
319                                                 break;\r
320 \r
321                                         case portINTERRUPT_TICK:\r
322                                         \r
323                                                 /* Tick interrupts should only be processed if the \r
324                                                 critical nesting count is zero.  The critical nesting \r
325                                                 count represents the interrupt mask on real target \r
326                                                 hardware.  The thread that genereates ticks will not\r
327                                                 actually ask for the tick to be processed unless the\r
328                                                 critical nesting count is zero anyway, but it is \r
329                                                 possible that a tick is pending when a yield is \r
330                                                 performed (depending on if the simulation/emulation is\r
331                                                 set up to process yields while within a critical \r
332                                                 section. */\r
333                                                 vPortTrace( "prvProcessPseudoInterrupts: Processing tick event\r\n" );\r
334                                                 if( ulCriticalNesting == 0UL )\r
335                                                 {\r
336                                                         /* Process the tick itself. */\r
337                                                         vPortTrace( "prvProcessPseudoInterrupts: Incrementing tick\r\n" );\r
338                                                         vTaskIncrementTick();\r
339                                                         #if( configUSE_PREEMPTION != 0 )\r
340                                                         {\r
341                                                                 /* A context switch is only automatically \r
342                                                                 performed from the tick interrupt if the \r
343                                                                 pre-emptive scheduler is being used. */\r
344                                                                 lSwitchRequired = pdTRUE;\r
345                                                         }\r
346                                                         #endif\r
347                                                         \r
348                                                         /* Clear the interrupt pending bit. */\r
349                                                         ulPendingInterrupts &= ~( 1UL << portINTERRUPT_TICK );\r
350 \r
351                                                         vPortTrace( "prvProcessPseudoInterrupts: Acking tick\r\n" );\r
352                                                         SetEvent( pvTickAcknowledgeEvent );\r
353                                                 }\r
354                                                 else\r
355                                                 {\r
356                                                         /* The tick is held pending in ulCriticalNesting\r
357                                                         until such time that pseudo interrupts are enabled\r
358                                                         again. */\r
359                                                 }\r
360                                                 break;\r
361 \r
362                                         default:\r
363 \r
364                                                 if( ulCriticalNesting == 0UL )\r
365                                                 {\r
366                                                         /* Is a handler installed? */\r
367                                                         if( vIsrHandler[ i ] != NULL )\r
368                                                         {\r
369                                                                 lSwitchRequired = pdTRUE;\r
370 \r
371                                                                 /* Run the actual handler. */\r
372                                                                 vIsrHandler[ i ]();\r
373 \r
374                                                                 /* Clear the interrupt pending bit. */\r
375                                                                 ulPendingInterrupts &= ~( 1UL << i );\r
376 \r
377                                                                 /* TODO:  Need to have some sort of handshake \r
378                                                                 event here for non-tick and none yield \r
379                                                                 interrupts. */\r
380                                                         }\r
381                                                 }\r
382                                                 break;\r
383                                 }\r
384                         }\r
385                 }\r
386 \r
387                 if( lSwitchRequired != pdFALSE )\r
388                 {\r
389                         void *pvOldCurrentTCB;\r
390 \r
391                         pvOldCurrentTCB = pxCurrentTCB;\r
392 \r
393                         /* Save the state of the current thread before suspending it. */\r
394                         pxThreadState = ( xThreadState *) *( ( unsigned long * ) pxCurrentTCB );\r
395                         pxThreadState->ulCriticalNesting = ulCriticalNesting ;\r
396                         \r
397                         /* Select the next task to run. */\r
398                         vTaskSwitchContext();\r
399                         \r
400                         /* If the task selected to enter the running state is not the task\r
401                         that is already in the running state. */\r
402                         if( pvOldCurrentTCB != pxCurrentTCB )\r
403                         {\r
404                                 /* Suspend the old thread. */\r
405                                 SetThreadPriority( pxThreadState->pvThread, THREAD_PRIORITY_IDLE );\r
406                                 SuspendThread( pxThreadState->pvThread );\r
407 \r
408                                 //sprintf( cTraceBuffer, "Event processor: suspending %s, resuming %s\r\n", ((xTCB*)pvOldCurrentTCB)->pcTaskName, ((xTCB*)pxCurrentTCB)->pcTaskName );\r
409                                 //vPortTrace( cTraceBuffer );\r
410 \r
411                                 /* Obtain the state of the task now selected to enter the Running state. */\r
412                                 pxThreadState = ( xThreadState * ) ( *( unsigned long *) pxCurrentTCB );\r
413                                 ulCriticalNesting = pxThreadState->ulCriticalNesting;\r
414                                 SetThreadPriority( pxThreadState->pvThread, THREAD_PRIORITY_ABOVE_NORMAL );\r
415                                 ResumeThread( pxThreadState->pvThread );\r
416 \r
417                                 if( pxThreadState->lWaitingInterruptAck == pdTRUE )\r
418                                 {\r
419                                         pxThreadState->lWaitingInterruptAck = pdFALSE;\r
420                                         vPortTrace( "prvProcessPseudoInterrupts: Acking interrupt\r\n" );\r
421                                         SetEvent( pvInterruptAcknowledgeEvent );\r
422                                 }\r
423                         }\r
424                 }\r
425                 else\r
426                 {\r
427                         /* On exiting a critical section a task may have blocked on the\r
428                         interrupt event when only a tick needed processing, in which case\r
429                         it will not have been released from waiting on the event yet. */\r
430                         pxThreadState = ( xThreadState * ) ( *( unsigned long *) pxCurrentTCB );\r
431                         if( pxThreadState->lWaitingInterruptAck == pdTRUE )\r
432                         {\r
433                                 pxThreadState->lWaitingInterruptAck = pdFALSE;\r
434                                 vPortTrace( "prvProcessPseudoInterrupts: Acking interrupt even though a yield has not been performed.\r\n" );\r
435                                 SetEvent( pvInterruptAcknowledgeEvent );\r
436                         }\r
437                 }\r
438 \r
439                 ReleaseMutex( pvInterruptEventMutex );\r
440         }\r
441 }\r
442 /*-----------------------------------------------------------*/\r
443 \r
444 void vPortEndScheduler( void )\r
445 {\r
446 }\r
447 /*-----------------------------------------------------------*/\r
448 \r
449 void vPortGeneratePseudoInterrupt( unsigned long ulInterruptNumber )\r
450 {\r
451 xThreadState *pxThreadState;\r
452 \r
453         if( ( ulInterruptNumber < portMAX_INTERRUPTS ) && ( pvInterruptEventMutex != NULL ) )\r
454         {\r
455                 /* Yield interrupts are processed even when critical nesting is non-zero. */\r
456                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
457                 ulPendingInterrupts |= ( 1 << ulInterruptNumber );\r
458 \r
459                 if( ulCriticalNesting == 0 ) //|| ( ulInterruptNumber == portINTERRUPT_YIELD ) )\r
460                 {\r
461                         /* The event handler needs to know to signal the interrupt acknowledge event\r
462                         the next time this task runs. */\r
463                         pxThreadState = ( xThreadState * ) *( ( unsigned long * ) pxCurrentTCB );\r
464                         pxThreadState->lWaitingInterruptAck = pdTRUE;\r
465 \r
466                         vPortTrace( "vPortGeneratePseudoInterrupt: Got interrupt mutex, about to signal interrupt event\r\n" );\r
467                         SetEvent( pvInterruptEvent );\r
468 \r
469                         /* The interrupt ack event should not be signaled yet - if it is then there\r
470                         is an error in the logical simulation. */\r
471                         if( WaitForSingleObject( pvInterruptAcknowledgeEvent, 0 ) != WAIT_TIMEOUT )\r
472                         {\r
473                                 /* This line is for a break point only. */\r
474                                 __asm { NOP };\r
475                         }\r
476 \r
477                         SignalObjectAndWait( pvInterruptEventMutex, pvInterruptAcknowledgeEvent, INFINITE, FALSE );\r
478                         vPortTrace( "vPortGeneratePseudoInterrupt: About to release interrupt event mutex\r\n" );\r
479 //                      ReleaseMutex( pvInterruptEventMutex );\r
480                         vPortTrace( "vPortGeneratePseudoInterrupt: Interrupt event mutex released, going to wait for interrupt ack\r\n" );\r
481 \r
482 //                      WaitForSingleObject( pvInterruptAcknowledgeEvent, INFINITE );\r
483                         vPortTrace( "vPortGeneratePseudoInterrupt: Interrupt acknowledged, leaving vPortGeneratePseudoInterrupt()\r\n" );\r
484                 }\r
485                 else\r
486                 {\r
487                         ReleaseMutex( pvInterruptEventMutex );\r
488                 }\r
489         }\r
490 }\r
491 /*-----------------------------------------------------------*/\r
492 \r
493 void vPortSetInterruptHandler( unsigned long ulInterruptNumber, void (*pvHandler)( void ) )\r
494 {\r
495         if( ulInterruptNumber < portMAX_INTERRUPTS )\r
496         {\r
497                 if( pvInterruptEventMutex != NULL )\r
498                 {\r
499                         WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
500                         vIsrHandler[ ulInterruptNumber ] = pvHandler;\r
501                         ReleaseMutex( pvInterruptEventMutex );\r
502                 }\r
503                 else\r
504                 {\r
505                         vIsrHandler[ ulInterruptNumber ] = pvHandler;\r
506                 }\r
507         }\r
508 }\r
509 /*-----------------------------------------------------------*/\r
510 \r
511 void vPortEnterCritical( void )\r
512 {\r
513         if( xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED )\r
514         {\r
515                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
516 //              SuspendThread( pvSimulatedTimerThread );\r
517                 ulCriticalNesting++;\r
518                 ReleaseMutex( pvInterruptEventMutex );\r
519         }\r
520         else\r
521         {\r
522                 ulCriticalNesting++;\r
523         }       \r
524 }\r
525 /*-----------------------------------------------------------*/\r
526 \r
527 void vPortExitCritical( void )\r
528 {\r
529 xThreadState *pxThreadState;\r
530 \r
531         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
532         {\r
533                 if( ulCriticalNesting == ( portNO_CRITICAL_NESTING + 1 ) )\r
534                 {\r
535                         /* Wait for the interrupt event mutex prior to manipulating or \r
536                         testing the pseudo interrupt control variables. */\r
537                         WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
538                         vPortTrace( "vPortExitCritical:  Got interrupt event mutex\r\n" );\r
539 \r
540 //                      ResumeThread( pvSimulatedTimerThread );\r
541 \r
542                         /* Now it is safe to decrement the critical nesting count as no\r
543                         tick events will be processed until the interrupt event mutex is\r
544                         given back. */\r
545                         ulCriticalNesting--;\r
546 \r
547                         /* Were any interrupts set to pending while interrupts were \r
548                         (pseudo) disabled? */\r
549                         if( ulPendingInterrupts != 0UL )\r
550                         {\r
551                                 SetEvent( pvInterruptEvent );\r
552 \r
553                                 /* The interrupt ack event should not be signaled yet - if it \r
554                                 is then there is an error in the logical simulation. */\r
555                                 if( WaitForSingleObject( pvInterruptAcknowledgeEvent, 0 ) != WAIT_TIMEOUT )\r
556                                 {\r
557                                         /* This line is for a break point only. */\r
558                                         __asm { NOP };\r
559                                 }\r
560 \r
561                                 /* The event handler needs to know to signal the interrupt \r
562                                 acknowledge event the next time this task runs. */\r
563                                 pxThreadState = ( xThreadState * ) *( ( unsigned long * ) pxCurrentTCB );\r
564                                 pxThreadState->lWaitingInterruptAck = pdTRUE;\r
565 \r
566                                 SignalObjectAndWait( pvInterruptEventMutex, pvInterruptAcknowledgeEvent, INFINITE, FALSE );\r
567                                 /* Give back the interrupt event mutex so the event can be processed. */\r
568 //                              ReleaseMutex( pvInterruptEventMutex );\r
569 \r
570 //                              vPortTrace( "vPortExitCritical:  Waiting interrupt ack\r\n" );\r
571 //                              WaitForSingleObject( pvInterruptAcknowledgeEvent, INFINITE );\r
572                                 vPortTrace( "vPortExitCritical: Interrupt acknowledged, leaving critical section code\r\n" );\r
573                         }\r
574                         else\r
575                         {\r
576                                 /* Can't leave here without giving back the interrupt event\r
577                                 mutex. */\r
578                                 ReleaseMutex( pvInterruptEventMutex );\r
579                         }\r
580                 }\r
581                 else\r
582                 {\r
583                         /* Tick interrupts will still not be processed as the critical\r
584                         nesting depth will not be zero. */\r
585                         ulCriticalNesting--;\r
586                 }\r
587         }\r
588 }\r