]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MSVC-MingW/port.c
Prepare for V7.3.0 release.
[freertos] / FreeRTOS / Source / portable / MSVC-MingW / port.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \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
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\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
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \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
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \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
67 */\r
68 \r
69 /* Scheduler includes. */\r
70 #include "FreeRTOS.h"\r
71 #include "task.h"\r
72 #include <stdio.h>\r
73 \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
76 \r
77 /*\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
82  */\r
83 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter );\r
84 \r
85 /* \r
86  * Process all the simulated interrupts - each represented by a bit in \r
87  * ulPendingInterrupts variable.\r
88  */\r
89 static void prvProcessSimulatedInterrupts( void );\r
90 \r
91 /*\r
92  * Interrupt handlers used by the kernel itself.  These are executed from the\r
93  * simulated interrupt handler thread.\r
94  */\r
95 static unsigned long prvProcessYieldInterrupt( void );\r
96 static unsigned long prvProcessTickInterrupt( void );\r
97 \r
98 /*-----------------------------------------------------------*/\r
99 \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
105 typedef struct\r
106 {\r
107         /* Handle of the thread that executes the task. */\r
108         void *pvThread;\r
109 \r
110 } xThreadState;\r
111 \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
115 \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
118 pending. */\r
119 static void *pvInterruptEvent = NULL;\r
120 \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
124 \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
131 started anyway. */\r
132 static unsigned long ulCriticalNesting = 9999UL;\r
133 \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
138 \r
139 /* Pointer to the TCB of the currently executing task. */\r
140 extern void *pxCurrentTCB;\r
141 \r
142 /*-----------------------------------------------------------*/\r
143 \r
144 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )\r
145 {\r
146 portTickType xMinimumWindowsBlockTime = ( portTickType ) 20;\r
147 \r
148         /* Just to prevent compiler warnings. */\r
149         ( void ) lpParameter;\r
150 \r
151         for(;;)\r
152         {\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
158                 environment. */\r
159                 if( portTICK_RATE_MS < xMinimumWindowsBlockTime )\r
160                 {\r
161                         Sleep( xMinimumWindowsBlockTime );\r
162                 }\r
163                 else\r
164                 {\r
165                         Sleep( portTICK_RATE_MS );\r
166                 }\r
167 \r
168                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
169 \r
170                 /* The timer has expired, generate the simulated tick event. */\r
171                 ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );\r
172 \r
173                 /* The interrupt is now pending - notify the simulated interrupt \r
174                 handler thread. */\r
175                 SetEvent( pvInterruptEvent );\r
176 \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
180         }\r
181 \r
182         #ifdef __GNUC__\r
183                 /* Should never reach here - MingW complains if you leave this line out,\r
184                 MSVC complains if you put it in. */\r
185                 return 0;\r
186         #endif\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
191 {\r
192 xThreadState *pxThreadState = NULL;\r
193 \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
201 \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
207         \r
208         return ( portSTACK_TYPE * ) pxThreadState;\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 portBASE_TYPE xPortStartScheduler( void )\r
213 {\r
214 void *pvHandle;\r
215 long lSuccess = pdPASS;\r
216 xThreadState *pxThreadState;\r
217 \r
218         /* Install the interrupt handlers used by the scheduler itself. */\r
219         vPortSetInterruptHandler( portINTERRUPT_YIELD, prvProcessYieldInterrupt );\r
220         vPortSetInterruptHandler( portINTERRUPT_TICK, prvProcessTickInterrupt );\r
221 \r
222         /* Create the events and mutexes that are used to synchronise all the\r
223         threads. */\r
224         pvInterruptEventMutex = CreateMutex( NULL, FALSE, NULL );\r
225         pvInterruptEvent = CreateEvent( NULL, FALSE, FALSE, NULL );\r
226 \r
227         if( ( pvInterruptEventMutex == NULL ) || ( pvInterruptEvent == NULL ) )\r
228         {\r
229                 lSuccess = pdFAIL;\r
230         }\r
231 \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
237         {\r
238                 lSuccess = pdFAIL;\r
239         }\r
240         \r
241         if( lSuccess == pdPASS )\r
242         {\r
243                 if( SetThreadPriority( pvHandle, THREAD_PRIORITY_NORMAL ) == 0 )\r
244                 {\r
245                         lSuccess = pdFAIL;\r
246                 }\r
247                 SetThreadPriorityBoost( pvHandle, TRUE );\r
248                 SetThreadAffinityMask( pvHandle, 0x01 );\r
249         }\r
250 \r
251         if( lSuccess == pdPASS )\r
252         {\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
259                 {\r
260                         SetThreadPriority( pvHandle, THREAD_PRIORITY_BELOW_NORMAL );\r
261                         SetThreadPriorityBoost( pvHandle, TRUE );\r
262                         SetThreadAffinityMask( pvHandle, 0x01 );\r
263                 }\r
264                 \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
269 \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
274 \r
275                 /* Handle all simulated interrupts - including yield requests and \r
276                 simulated ticks. */\r
277                 prvProcessSimulatedInterrupts();\r
278         }       \r
279         \r
280         /* Would not expect to return from prvProcessSimulatedInterrupts(), so should \r
281         not get here. */\r
282         return 0;\r
283 }\r
284 /*-----------------------------------------------------------*/\r
285 \r
286 static unsigned long prvProcessYieldInterrupt( void )\r
287 {\r
288         return pdTRUE;\r
289 }\r
290 /*-----------------------------------------------------------*/\r
291 \r
292 static unsigned long prvProcessTickInterrupt( void )\r
293 {\r
294 unsigned long ulSwitchRequired;\r
295 \r
296         /* Process the tick itself. */\r
297         vTaskIncrementTick();\r
298         #if( configUSE_PREEMPTION != 0 )\r
299         {\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
303         }\r
304         #else\r
305         {\r
306                 ulSwitchRequired = pdFALSE;\r
307         }\r
308         #endif\r
309 \r
310         return ulSwitchRequired;\r
311 }\r
312 /*-----------------------------------------------------------*/\r
313 \r
314 static void prvProcessSimulatedInterrupts( void )\r
315 {\r
316 unsigned long ulSwitchRequired, i;\r
317 xThreadState *pxThreadState;\r
318 void *pvObjectList[ 2 ];\r
319 \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
325 \r
326         for(;;)\r
327         {\r
328                 WaitForMultipleObjects( sizeof( pvObjectList ) / sizeof( void * ), pvObjectList, TRUE, INFINITE );\r
329 \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
333 \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
337                 {\r
338                         /* Is the simulated interrupt pending? */\r
339                         if( ulPendingInterrupts & ( 1UL << i ) )\r
340                         {\r
341                                 /* Is a handler installed? */\r
342                                 if( ulIsrHandler[ i ] != NULL )\r
343                                 {\r
344                                         /* Run the actual handler. */\r
345                                         if( ulIsrHandler[ i ]() != pdFALSE )\r
346                                         {\r
347                                                 ulSwitchRequired |= ( 1 << i );\r
348                                         }\r
349                                 }\r
350 \r
351                                 /* Clear the interrupt pending bit. */\r
352                                 ulPendingInterrupts &= ~( 1UL << i );\r
353                         }\r
354                 }\r
355 \r
356                 if( ulSwitchRequired != pdFALSE )\r
357                 {\r
358                         void *pvOldCurrentTCB;\r
359 \r
360                         pvOldCurrentTCB = pxCurrentTCB;\r
361 \r
362                         /* Select the next task to run. */\r
363                         vTaskSwitchContext();\r
364 \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
368                         {\r
369                                 /* Suspend the old thread. */\r
370                                 pxThreadState = ( xThreadState *) *( ( unsigned long * ) pvOldCurrentTCB );\r
371                                 SuspendThread( pxThreadState->pvThread );\r
372 \r
373                                 /* Obtain the state of the task now selected to enter the \r
374                                 Running state. */\r
375                                 pxThreadState = ( xThreadState * ) ( *( unsigned long *) pxCurrentTCB );\r
376                                 ResumeThread( pxThreadState->pvThread );\r
377                         }\r
378                 }\r
379 \r
380                 ReleaseMutex( pvInterruptEventMutex );\r
381         }\r
382 }\r
383 /*-----------------------------------------------------------*/\r
384 \r
385 void vPortDeleteThread( void *pvTaskToDelete )\r
386 {\r
387 xThreadState *pxThreadState;\r
388 \r
389         WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
390 \r
391         /* Find the handle of the thread being deleted. */\r
392         pxThreadState = ( xThreadState * ) ( *( unsigned long *) pvTaskToDelete );\r
393         TerminateThread( pxThreadState->pvThread, 0 );\r
394 \r
395         ReleaseMutex( pvInterruptEventMutex );\r
396 }\r
397 /*-----------------------------------------------------------*/\r
398 \r
399 void vPortEndScheduler( void )\r
400 {\r
401         /* This function IS NOT TESTED! */\r
402         TerminateProcess( GetCurrentProcess(), 0 );\r
403 }\r
404 /*-----------------------------------------------------------*/\r
405 \r
406 void vPortGenerateSimulatedInterrupt( unsigned long ulInterruptNumber )\r
407 {\r
408 xThreadState *pxThreadState;\r
409 \r
410         if( ( ulInterruptNumber < portMAX_INTERRUPTS ) && ( pvInterruptEventMutex != NULL ) )\r
411         {\r
412                 /* Yield interrupts are processed even when critical nesting is non-zero. */\r
413                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
414                 ulPendingInterrupts |= ( 1 << ulInterruptNumber );\r
415 \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
420                 {\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
425                 }\r
426 \r
427                 ReleaseMutex( pvInterruptEventMutex );\r
428         }\r
429 }\r
430 /*-----------------------------------------------------------*/\r
431 \r
432 void vPortSetInterruptHandler( unsigned long ulInterruptNumber, unsigned long (*pvHandler)( void ) )\r
433 {\r
434         if( ulInterruptNumber < portMAX_INTERRUPTS )\r
435         {\r
436                 if( pvInterruptEventMutex != NULL )\r
437                 {\r
438                         WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
439                         ulIsrHandler[ ulInterruptNumber ] = pvHandler;\r
440                         ReleaseMutex( pvInterruptEventMutex );\r
441                 }\r
442                 else\r
443                 {\r
444                         ulIsrHandler[ ulInterruptNumber ] = pvHandler;\r
445                 }\r
446         }\r
447 }\r
448 /*-----------------------------------------------------------*/\r
449 \r
450 void vPortEnterCritical( void )\r
451 {\r
452         if( xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED )\r
453         {\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
458         }\r
459         else\r
460         {\r
461                 ulCriticalNesting++;\r
462         }       \r
463 }\r
464 /*-----------------------------------------------------------*/\r
465 \r
466 void vPortExitCritical( void )\r
467 {\r
468 xThreadState *pxThreadState;\r
469 long lMutexNeedsReleasing;\r
470 \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
473 \r
474         lMutexNeedsReleasing = pdTRUE;\r
475 \r
476         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
477         {\r
478                 if( ulCriticalNesting == ( portNO_CRITICAL_NESTING + 1 ) )\r
479                 {\r
480                         ulCriticalNesting--;\r
481 \r
482                         /* Were any interrupts set to pending while interrupts were \r
483                         (simulated) disabled? */\r
484                         if( ulPendingInterrupts != 0UL )\r
485                         {\r
486                                 SetEvent( pvInterruptEvent );\r
487 \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
491 \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
496                         }\r
497                 }\r
498                 else\r
499                 {\r
500                         /* Tick interrupts will still not be processed as the critical\r
501                         nesting depth will not be zero. */\r
502                         ulCriticalNesting--;\r
503                 }\r
504         }\r
505 \r
506         if( lMutexNeedsReleasing == pdTRUE )\r
507         {\r
508                 ReleaseMutex( pvInterruptEventMutex );\r
509         }\r
510 }\r
511 /*-----------------------------------------------------------*/\r
512 \r