]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MicroBlaze_Spartan-6_EthernetLite/SDKProjects/RTOSDemo/main-full.c
a41824ea83de184b32f61ea049c0c0d9cfc1bc62
[freertos] / FreeRTOS / Demo / MicroBlaze_Spartan-6_EthernetLite / SDKProjects / RTOSDemo / main-full.c
1 /*\r
2     FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\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 modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /* ****************************************************************************\r
68  * main-blinky.c is included when the "Blinky" build configuration is used.\r
69  * main-full.c is included when the "Full" build configuration is used.\r
70  *\r
71  * main-full.c creates a lot of demo and test tasks and timers,  and is \r
72  * therefore very comprehensive but also complex.  If you would prefer a much \r
73  * simpler project to get started with, then select the 'Blinky' build \r
74  * configuration within the SDK Eclipse IDE.  See the documentation page for\r
75  * this demo on the http://www.FreeRTOS.org web site for more information.\r
76  * ****************************************************************************\r
77  *\r
78  * main() creates all the demo application tasks and timers, then starts the \r
79  * scheduler.  The web documentation provides more details of the standard demo \r
80  * application tasks, which provide no particular functionality, but do provide \r
81  * a good example of how to use the FreeRTOS API.  \r
82  *\r
83  * In addition to the standard demo tasks, the following tasks and tests are\r
84  * defined and/or created within this file:\r
85  *\r
86  * TCP/IP ("lwIP") task - lwIP is used to create a basic web server.  The web\r
87  * server uses server side includes (SSI) to generate tables of task statistics,\r
88  * and run time statistics (run time statistics show how much processing time\r
89  * each task has consumed).  See\r
90  * http://www.FreeRTOS.org/Free-RTOS-for-Xilinx-MicroBlaze-on-Spartan-6-FPGA.html\r
91  * for details on setting up and using the embedded web server.\r
92  *\r
93  * "Reg test" tasks - These test the task context switch mechanism by first \r
94  * filling the MicroBlaze registers with known values, before checking that each\r
95  * register maintains the value that was written to it as the tasks are switched\r
96  * in and out.  The two register test tasks do not use the same values, and\r
97  * execute at a very low priority, to ensure they are pre-empted regularly.\r
98  *\r
99  * "Check" timer - The check timer period is initially set to five seconds.  \r
100  * The check timer callback function checks that all the standard demo tasks,\r
101  * and the register check tasks, are not only still executing, but are executing\r
102  * without reporting any errors.  If the check timer discovers that a task has\r
103  * either stalled, or reported an error, then it changes its own period from\r
104  * the initial five seconds, to just 200ms.  The check timer callback function\r
105  * also toggles an LED each time it is called.  This provides a visual\r
106  * indication of the system status:  If the LED toggles every five seconds then\r
107  * no issues have been discovered.  If the LED toggles every 200ms then an issue\r
108  * has been discovered with at least one task.  The last reported issue is\r
109  * latched into the pcStatusMessage variable, and can also be viewed at the\r
110  * bottom of the pages served by the embedded web server.\r
111  *\r
112  * ***NOTE*** This demo uses the standard comtest tasks, which has special\r
113  * hardware requirements.  See\r
114  * http://www.FreeRTOS.org/Free-RTOS-for-Xilinx-MicroBlaze-on-Spartan-6-FPGA.html\r
115  * for more information.\r
116  *\r
117  * This file also includes example implementations of the\r
118  * vApplicationIdleHook(), vApplicationStackOverflowHook(),\r
119  * vApplicationMallocFailedHook(), vApplicationClearTimerInterrupt(), and\r
120  * vApplicationSetupTimerInterrupt() callback (hook) functions.\r
121  */\r
122 \r
123 /* Standard includes. */\r
124 #include <string.h>\r
125 #include <stdio.h>\r
126 \r
127 /* BSP includes. */\r
128 #include "xtmrctr.h"\r
129 #include "microblaze_exceptions_g.h"\r
130 \r
131 /* Kernel includes. */\r
132 #include "FreeRTOS.h"\r
133 #include "task.h"\r
134 #include "timers.h"\r
135 \r
136 /* Standard demo includes. */\r
137 #include "partest.h"\r
138 #include "flash.h"\r
139 #include "BlockQ.h"\r
140 #include "death.h"\r
141 #include "blocktim.h"\r
142 #include "semtest.h"\r
143 #include "PollQ.h"\r
144 #include "GenQTest.h"\r
145 #include "QPeek.h"\r
146 #include "recmutex.h"\r
147 #include "flop.h"\r
148 #include "dynamic.h"\r
149 #include "comtest_strings.h"\r
150 #include "TimerDemo.h"\r
151 \r
152 /* lwIP includes. */\r
153 #include "lwip/tcpip.h"\r
154 \r
155 \r
156 /* Priorities at which the various tasks are created. */\r
157 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
158 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
159 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
160 #define mainCREATOR_TASK_PRIORITY   ( tskIDLE_PRIORITY + 3 )\r
161 #define mainFLASH_TASK_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
162 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
163 #define mainINTEGER_TASK_PRIORITY   ( tskIDLE_PRIORITY )\r
164 #define mainGEN_QUEUE_TASK_PRIORITY     ( tskIDLE_PRIORITY )\r
165 #define mainFLOP_TASK_PRIORITY          ( tskIDLE_PRIORITY )\r
166 \r
167 /* The LED toggled by the check task. */\r
168 #define mainCHECK_LED                           ( 3 )\r
169 \r
170 /* The rate at which mainCHECK_LED will toggle when all the tasks are running\r
171 without error.  See the description of the check timer in the comments at the\r
172 top of this file. */\r
173 #define mainNO_ERROR_CHECK_TIMER_PERIOD         ( 5000 / portTICK_RATE_MS )\r
174 \r
175 /* The rate at which mainCHECK_LED will toggle when an error has been reported\r
176 by at least one task.  See the description of the check timer in the comments at \r
177 the top of this file. */\r
178 #define mainERROR_CHECK_TIMER_PERIOD            ( 200 / portTICK_RATE_MS )\r
179 \r
180 /* A block time of zero simply means "don't block". */\r
181 #define mainDONT_BLOCK                                          ( ( portTickType ) 0 )\r
182 \r
183 /* The LED used by the comtest tasks. See the comtest_strings.c file for more\r
184 information.  In this case an invalid LED number is provided as all four\r
185 available LEDs (LEDs 0 to 3) are already in use. */\r
186 #define mainCOM_TEST_LED                        ( 4 )\r
187 \r
188 /* Baud rate used by the comtest tasks.  The baud rate used is actually fixed in \r
189 UARTLite IP when the hardware was built, but the standard serial init function \r
190 required a baud rate parameter to be provided - in this case it is just \r
191 ignored. */\r
192 #define mainCOM_TEST_BAUD_RATE                          ( XPAR_RS232_UART_1_BAUDRATE )\r
193 \r
194 /* The timer test task generates a lot of timers that all use a different \r
195 period that is a multiple of the mainTIMER_TEST_PERIOD definition. */\r
196 #define mainTIMER_TEST_PERIOD                   ( 20 )\r
197 \r
198 /*-----------------------------------------------------------*/\r
199 \r
200 /*\r
201  * The register test tasks as described in the comments at the top of this file.\r
202  * The nature of the register test tasks means they have to be implemented in\r
203  * assembler.\r
204  */\r
205 extern void vRegisterTest1( void *pvParameters );\r
206 extern void vRegisterTest2( void *pvParameters );\r
207 \r
208 /*\r
209  * Defines the 'check' timer functionality as described at the top of this file.  \r
210  * This function is the callback function associated with the 'check' timer.\r
211  */\r
212 static void vCheckTimerCallback( xTimerHandle xTimer );\r
213 \r
214 /* \r
215  * Configure the interrupt controller, LED outputs and button inputs. \r
216  */\r
217 static void prvSetupHardware( void );\r
218 \r
219 /* Defined in lwIPApps.c. */\r
220 extern void lwIPAppsInit( void *pvArguments );\r
221 \r
222 /*-----------------------------------------------------------*/\r
223 \r
224 /* The check timer callback function sets pcStatusMessage to a string that\r
225 indicates the last reported error that it discovered. */\r
226 static const char *pcStatusMessage = NULL;\r
227 \r
228 /* Structures that hold the state of the various peripherals used by this demo.\r
229 These are used by the Xilinx peripheral driver API functions.  In this case,\r
230 only the timer/counter is used directly within this file. */\r
231 static XTmrCtr xTimer0Instance;\r
232 \r
233 /* The 'check' timer, as described at the top of this file. */\r
234 static xTimerHandle xCheckTimer = NULL;\r
235 \r
236 /* Used in the run time stats calculations. */\r
237 static unsigned long ulClocksPer10thOfAMilliSecond = 0UL;\r
238 \r
239 /* Constants used to set up the AXI timer to generate ticks. */\r
240 static const unsigned char ucTimerCounterNumber = ( unsigned char ) 0U;\r
241 static const unsigned long ulCounterReloadValue = ( ( XPAR_AXI_TIMER_0_CLOCK_FREQ_HZ / configTICK_RATE_HZ ) - 1UL );\r
242 \r
243 /*-----------------------------------------------------------*/\r
244 \r
245 int main( void )\r
246 {\r
247         /***************************************************************************\r
248         This project includes a lot of demo and test tasks and timers,  and is \r
249         therefore comprehensive, but complex.  If you would prefer a much simpler \r
250         project to get started with, then select the 'Blinky' build configuration \r
251         within the SDK Eclipse IDE.\r
252         ***************************************************************************/\r
253 \r
254         /* Configure the interrupt controller, LED outputs and button inputs. */\r
255         prvSetupHardware();\r
256 \r
257         /* This call creates the TCP/IP thread. */\r
258         tcpip_init( lwIPAppsInit, NULL );\r
259 \r
260         /* Start the reg test tasks, as described in the comments at the top of this\r
261         file. */\r
262         xTaskCreate( vRegisterTest1, ( const signed char * const ) "RegTst1", configMINIMAL_STACK_SIZE, ( void * ) 0, tskIDLE_PRIORITY, NULL );\r
263         xTaskCreate( vRegisterTest2, ( const signed char * const ) "RegTst2", configMINIMAL_STACK_SIZE, ( void * ) 0, tskIDLE_PRIORITY, NULL );\r
264 \r
265         /* Create the standard demo tasks. */\r
266         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
267         vCreateBlockTimeTasks();\r
268         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
269         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
270         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
271         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
272         vStartQueuePeekTasks();\r
273         vStartRecursiveMutexTasks();\r
274         vStartComTestStringsTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
275         vStartDynamicPriorityTasks();\r
276         vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
277 \r
278         /* Note - the set of standard demo tasks contains two versions of\r
279         vStartMathTasks.c.  One is defined in flop.c, and uses double precision\r
280         floating point numbers and variables.  The other is defined in sp_flop.c,\r
281         and uses single precision floating point numbers and variables.  The\r
282         MicroBlaze floating point unit only handles single precision floating.\r
283         Therefore, to test the floating point hardware, sp_flop.c should be included\r
284         in this project. */\r
285         vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
286 \r
287         /* The suicide tasks must be created last as they need to know how many\r
288         tasks were running prior to their creation.  This then allows them to \r
289         ascertain whether or not the correct/expected number of tasks are running at \r
290         any given time. */\r
291         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
292 \r
293         /* Create the 'check' timer - the timer that periodically calls the\r
294         check function as described in the comments at the top of this file.  Note \r
295         that, for reasons stated in the comments within vApplicationIdleHook()\r
296         (defined in this file), the check timer is not actually started until after \r
297         the scheduler has been started. */\r
298         xCheckTimer = xTimerCreate( ( const signed char * ) "Check timer", mainNO_ERROR_CHECK_TIMER_PERIOD, pdTRUE, ( void * ) 0, vCheckTimerCallback );\r
299 \r
300         /* Start the scheduler running.  From this point on, only tasks and \r
301         interrupts will be executing. */\r
302         vTaskStartScheduler();\r
303 \r
304         /* If all is well then the following line will never be reached.  If\r
305         execution does reach here, then it is highly probably that the heap size\r
306         is too small for the idle and/or timer tasks to be created within \r
307         vTaskStartScheduler(). */\r
308         taskDISABLE_INTERRUPTS();\r
309         for( ;; );\r
310 }\r
311 /*-----------------------------------------------------------*/\r
312 \r
313 static void vCheckTimerCallback( xTimerHandle xTimer )\r
314 {\r
315 extern unsigned long ulRegTest1CycleCount, ulRegTest2CycleCount;\r
316 static volatile unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;\r
317 static long lErrorAlreadyLatched = pdFALSE;\r
318 portTickType xExecutionRate = mainNO_ERROR_CHECK_TIMER_PERIOD;\r
319 \r
320         /* This is the callback function used by the 'check' timer, as described\r
321         in the comments at the top of this file. */\r
322 \r
323         /* Don't overwrite any errors that have already been latched. */\r
324         if( pcStatusMessage == NULL )\r
325         {\r
326                 /* Check the standard demo tasks are running without error. */\r
327                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
328                 {\r
329                         pcStatusMessage = "Error: GenQueue";\r
330                 }\r
331                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
332                 {\r
333                         pcStatusMessage = "Error: QueuePeek\r\n";\r
334                 }\r
335                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
336                 {\r
337                         pcStatusMessage = "Error: BlockQueue\r\n";\r
338                 }\r
339                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
340                 {\r
341                         pcStatusMessage = "Error: BlockTime\r\n";\r
342                 }\r
343                 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
344                 {\r
345                         pcStatusMessage = "Error: SemTest\r\n";\r
346                 }\r
347                 else if( xArePollingQueuesStillRunning() != pdTRUE )\r
348                 {\r
349                         pcStatusMessage = "Error: PollQueue\r\n";\r
350                 }\r
351                 else if( xIsCreateTaskStillRunning() != pdTRUE )\r
352                 {\r
353                         pcStatusMessage = "Error: Death\r\n";\r
354                 }\r
355                 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
356                 {\r
357                         pcStatusMessage = "Error: RecMutex\r\n";\r
358                 }\r
359                 else if( xAreMathsTaskStillRunning() != pdPASS )\r
360                 {\r
361                         pcStatusMessage = "Error: Flop\r\n";\r
362                 }\r
363                 else if( xAreComTestTasksStillRunning() != pdPASS )\r
364                 {\r
365                         pcStatusMessage = "Error: Comtest\r\n";\r
366                 }\r
367                 else if( xAreDynamicPriorityTasksStillRunning() != pdPASS )\r
368                 {\r
369                         pcStatusMessage = "Error: Dynamic\r\n";\r
370                 }\r
371                 else if( xAreTimerDemoTasksStillRunning( xExecutionRate ) != pdTRUE )\r
372                 {\r
373                         pcStatusMessage = "Error: TimerDemo";\r
374                 }\r
375                 else if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )\r
376                 {\r
377                         /* Check the reg test tasks are still cycling.  They will stop\r
378                         incrementing their loop counters if they encounter an error. */\r
379                         pcStatusMessage = "Error: RegTest1\r\n";\r
380                 }\r
381                 else if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )\r
382                 {\r
383                         pcStatusMessage = "Error: RegTest2\r\n";\r
384                 }\r
385         }\r
386 \r
387         /* Store a local copy of the current reg test loop counters.  If these have\r
388         not incremented the next time this callback function is executed then the\r
389         reg test tasks have either stalled or discovered an error. */\r
390         ulLastRegTest1CycleCount = ulRegTest1CycleCount;\r
391         ulLastRegTest2CycleCount = ulRegTest2CycleCount;\r
392 \r
393         /* Toggle the check LED to give an indication of the system status.  If\r
394         the LED toggles every 5 seconds then everything is ok.  A faster toggle\r
395         indicates an error. */\r
396         vParTestToggleLED( mainCHECK_LED );\r
397 \r
398         if( pcStatusMessage != NULL )\r
399         {\r
400                 if( lErrorAlreadyLatched == pdFALSE )\r
401                 {\r
402                         /* An error has occurred, so change the period of the timer that\r
403                         calls this callback function.  This results in the LED toggling at\r
404                         a faster rate - giving the user visual feedback that something is not\r
405                         as it should be.  This function is called from the context of the\r
406                         timer service task so must ***not*** attempt to block while calling\r
407                         this function. */\r
408                         if( xTimerChangePeriod( xTimer, mainERROR_CHECK_TIMER_PERIOD, mainDONT_BLOCK ) == pdPASS )\r
409                         {\r
410                                 /* If the command to change the timer period was sent to the\r
411                                 timer command queue successfully, then latch the fact that the\r
412                                 timer period has already been changed.  This is just done to\r
413                                 prevent xTimerChangePeriod() being called on every execution of\r
414                                 this function once an error has been discovered.  */\r
415                                 lErrorAlreadyLatched = pdTRUE;\r
416                         }\r
417 \r
418                         /* Update the xExecutionRate variable too as the rate at which this\r
419                         callback is executed has to be passed into the\r
420                         xAreTimerDemoTasksStillRunning() function. */\r
421                         xExecutionRate = mainERROR_CHECK_TIMER_PERIOD;\r
422                 }\r
423         }\r
424 }\r
425 /*-----------------------------------------------------------*/\r
426 \r
427 /* This is an application defined callback function used to install the tick\r
428 interrupt handler.  It is provided as an application callback because the kernel\r
429 will run on lots of different MicroBlaze and FPGA configurations - not all of\r
430 which will have the same timer peripherals defined or available.  This example\r
431 uses the AXI Timer 0.  If that is available on your hardware platform then this\r
432 example callback implementation should not require modification.   The name of\r
433 the interrupt handler that should be installed is vPortTickISR(), which the \r
434 function below declares as an extern. */\r
435 void vApplicationSetupTimerInterrupt( void )\r
436 {\r
437 portBASE_TYPE xStatus;\r
438 extern void vPortTickISR( void *pvUnused );\r
439 \r
440         /* Initialise the timer/counter. */\r
441         xStatus = XTmrCtr_Initialize( &xTimer0Instance, XPAR_AXI_TIMER_0_DEVICE_ID );\r
442 \r
443         if( xStatus == XST_SUCCESS )\r
444         {\r
445                 /* Install the tick interrupt handler as the timer ISR. \r
446                 *NOTE* The xPortInstallInterruptHandler() API function must be used for\r
447                 this purpose. */\r
448                 xStatus = xPortInstallInterruptHandler( XPAR_INTC_0_TMRCTR_0_VEC_ID, vPortTickISR, NULL );\r
449         }\r
450 \r
451         if( xStatus == pdPASS )\r
452         {\r
453                 /* Enable the timer interrupt in the interrupt controller.\r
454                 *NOTE* The vPortEnableInterrupt() API function must be used for this\r
455                 purpose. */\r
456                 vPortEnableInterrupt( XPAR_INTC_0_TMRCTR_0_VEC_ID );\r
457 \r
458                 /* Configure the timer interrupt handler. */\r
459                 XTmrCtr_SetHandler( &xTimer0Instance, ( void * ) vPortTickISR, NULL );\r
460 \r
461                 /* Set the correct period for the timer. */\r
462                 XTmrCtr_SetResetValue( &xTimer0Instance, ucTimerCounterNumber, ulCounterReloadValue );\r
463 \r
464                 /* Enable the interrupts.  Auto-reload mode is used to generate a\r
465                 periodic tick.  Note that interrupts are disabled when this function is\r
466                 called, so interrupts will not start to be processed until the first\r
467                 task has started to run. */\r
468                 XTmrCtr_SetOptions( &xTimer0Instance, ucTimerCounterNumber, ( XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION | XTC_DOWN_COUNT_OPTION ) );\r
469 \r
470                 /* Start the timer. */\r
471                 XTmrCtr_Start( &xTimer0Instance, ucTimerCounterNumber );\r
472         }\r
473 \r
474         /* Sanity check that the function executed as expected. */\r
475         configASSERT( ( xStatus == pdPASS ) );\r
476 }\r
477 /*-----------------------------------------------------------*/\r
478 \r
479 /* This is an application defined callback function used to clear whichever\r
480 interrupt was installed by the the vApplicationSetupTimerInterrupt() callback\r
481 function - in this case the interrupt generated by the AXI timer.  It is \r
482 provided as an application callback because the kernel will run on lots of \r
483 different MicroBlaze and FPGA configurations - not all of which will have the \r
484 same timer peripherals defined or available.  This example uses the AXI Timer 0.  \r
485 If that is available on your hardware platform then this example callback \r
486 implementation should not require modification provided the example definition\r
487 of vApplicationSetupTimerInterrupt() is also not modified. */\r
488 void vApplicationClearTimerInterrupt( void )\r
489 {\r
490 unsigned long ulCSR;\r
491 \r
492         /* Clear the timer interrupt */\r
493         ulCSR = XTmrCtr_GetControlStatusReg( XPAR_AXI_TIMER_0_BASEADDR, 0 );\r
494         XTmrCtr_SetControlStatusReg( XPAR_AXI_TIMER_0_BASEADDR, 0, ulCSR );\r
495 }\r
496 /*-----------------------------------------------------------*/\r
497 \r
498 void vApplicationMallocFailedHook( void )\r
499 {\r
500         /* vApplicationMallocFailedHook() will only be called if\r
501         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
502         function that will get called if a call to pvPortMalloc() fails. \r
503         pvPortMalloc() is called internally by the kernel whenever a task, queue or\r
504         semaphore is created.  It is also called by various parts of the demo\r
505         application.  If heap_1.c or heap_2.c are used, then the size of the heap\r
506         available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
507         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
508         to query the size of free heap space that remains (although it does not\r
509         provide information on how the remaining heap might be fragmented). */\r
510         taskDISABLE_INTERRUPTS();\r
511         for( ;; );\r
512 }\r
513 /*-----------------------------------------------------------*/\r
514 \r
515 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )\r
516 {\r
517         ( void ) pcTaskName;\r
518         ( void ) pxTask;\r
519 \r
520         /* vApplicationStackOverflowHook() will only be called if\r
521         configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2.  The handle and name\r
522         of the offending task will be passed into the hook function via its \r
523         parameters.  However, when a stack has overflowed, it is possible that the\r
524         parameters will have been corrupted, in which case the pxCurrentTCB variable\r
525         can be inspected directly. */\r
526         taskDISABLE_INTERRUPTS();\r
527         for( ;; );\r
528 }\r
529 /*-----------------------------------------------------------*/\r
530 \r
531 void vApplicationIdleHook( void )\r
532 {\r
533 static long lCheckTimerStarted = pdFALSE;\r
534 \r
535         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set \r
536         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle \r
537         task.  It is essential that code added to this hook function never attempts \r
538         to block in any way (for example, call xQueueReceive() with a block time \r
539         specified, or call vTaskDelay()).  If the application makes use of the \r
540         vTaskDelete() API function (as this demo application does) then it is also \r
541         important that vApplicationIdleHook() is permitted to return to its calling \r
542         function, because it is the responsibility of the idle task to clean up \r
543         memory allocated by the kernel to any task that has since been deleted. */\r
544 \r
545         /* If the check timer has not already been started, then start it now.\r
546         Normally, the xTimerStart() API function can be called immediately after the\r
547         timer is created - how this demo application includes the timer demo tasks.\r
548         The timer demo tasks, as part of their test function, deliberately fill up\r
549         the timer command queue - meaning the check timer cannot be started until\r
550         after the scheduler has been started - at which point the timer command\r
551         queue will have been drained. */\r
552         if( lCheckTimerStarted == pdFALSE )\r
553         {\r
554                 xTimerStart( xCheckTimer, mainDONT_BLOCK ); \r
555                 lCheckTimerStarted = pdTRUE;\r
556         }\r
557 }\r
558 /*-----------------------------------------------------------*/\r
559 \r
560 void vApplicationExceptionRegisterDump( xPortRegisterDump *xRegisterDump )\r
561 {\r
562         ( void ) xRegisterDump;\r
563 \r
564         /* If configINSTALL_EXCEPTION_HANDLERS is set to 1 in FreeRTOSConfig.h, then \r
565         the kernel will automatically install its own exception handlers before the \r
566         kernel is started, if the application writer has not already caused them to \r
567         be installed by calling either of the vPortExceptionsInstallHandlers() \r
568         or xPortInstallInterruptHandler() API functions before that time.  The \r
569         kernels exception handler populates an xPortRegisterDump structure with\r
570         the processor state at the point that the exception was triggered - and also\r
571         includes a strings that say what the exception cause was and which task was\r
572         running at the time.  The exception handler then passes the populated\r
573         xPortRegisterDump structure into vApplicationExceptionRegisterDump() to\r
574         allow the application writer to perform any debugging that may be necessary.\r
575         However, defining vApplicationExceptionRegisterDump() within the application\r
576         itself is optional.  The kernel will use a default implementation if the\r
577         application writer chooses not to provide their own. */\r
578         for( ;; )\r
579         {\r
580                 portNOP();\r
581         }\r
582 }\r
583 /*-----------------------------------------------------------*/\r
584 \r
585 static void prvSetupHardware( void )\r
586 {\r
587         taskDISABLE_INTERRUPTS();\r
588         \r
589         /* Configure the LED outputs. */\r
590         vParTestInitialise();\r
591 \r
592         /* Tasks inherit the exception and cache configuration of the MicroBlaze\r
593         at the point that they are created. */\r
594         #if MICROBLAZE_EXCEPTIONS_ENABLED == 1\r
595                 microblaze_enable_exceptions();\r
596         #endif\r
597 \r
598         #if XPAR_MICROBLAZE_USE_ICACHE == 1\r
599                 microblaze_invalidate_icache();\r
600                 microblaze_enable_icache();\r
601         #endif\r
602 \r
603         #if XPAR_MICROBLAZE_USE_DCACHE == 1\r
604                 microblaze_invalidate_dcache();\r
605                 microblaze_enable_dcache();\r
606         #endif\r
607 \r
608 }\r
609 /*-----------------------------------------------------------*/\r
610 \r
611 void vMainConfigureTimerForRunTimeStats( void )\r
612 {\r
613         /* How many times does the counter counter increment in 10ms? */\r
614         ulClocksPer10thOfAMilliSecond = XPAR_AXI_TIMER_0_CLOCK_FREQ_HZ / 10000UL;\r
615 }\r
616 /*-----------------------------------------------------------*/\r
617 \r
618 unsigned long ulMainGetRunTimeCounterValue( void )\r
619 {\r
620 unsigned long ulTimerCounts1, ulTimerCounts2, ulTickCount, ulReturn;\r
621 \r
622         /* NOTE: This can get called from a yield, in which case interrupts are\r
623         disabled, or from a tick ISR, in which case the effect is the same as if\r
624         interrupts were disabled.  In either case, it is going to run atomically. */\r
625 \r
626         /* The timer is in down count mode.  How many clocks have passed since it\r
627         was last reloaded? */\r
628         ulTimerCounts1 = ulCounterReloadValue - XTmrCtr_GetValue( &xTimer0Instance, ucTimerCounterNumber );\r
629 \r
630         /* How many times has it overflowed? */\r
631         ulTickCount = xTaskGetTickCountFromISR();\r
632 \r
633         /* If this is being called from a yield, has the counter overflowed since\r
634         it was read?  If that is the case then ulTickCounts will need incrementing\r
635         again as it will not yet have been incremented from the tick interrupt. */\r
636         ulTimerCounts2 = ulCounterReloadValue - XTmrCtr_GetValue( &xTimer0Instance, ucTimerCounterNumber );\r
637         if( ulTimerCounts2 < ulTimerCounts1 )\r
638         {\r
639                 /* There is a tick interrupt pending but the tick count not yet\r
640                 incremented. */\r
641                 ulTickCount++;\r
642 \r
643                 /* Use the second timer reading. */\r
644                 ulTimerCounts1 = ulTimerCounts2;\r
645         }\r
646 \r
647         /* Convert the tick count into tenths of a millisecond.  THIS ASSUMES\r
648         configTICK_RATE_HZ is 1000! */\r
649         ulReturn = ( ulTickCount * 10UL );\r
650 \r
651         /* Add on the number of tenths of a millisecond that have passed since the\r
652         tick count last got updated. */\r
653         ulReturn += ( ulTimerCounts1 / ulClocksPer10thOfAMilliSecond );\r
654 \r
655         /* Some crude rounding. */\r
656         if( ( ulTimerCounts1 % ulClocksPer10thOfAMilliSecond ) > ( ulClocksPer10thOfAMilliSecond >> 1UL ) )\r
657         {\r
658                 ulReturn++;\r
659         }\r
660 \r
661         return ulReturn;\r
662 }\r
663 /*-----------------------------------------------------------*/\r
664 \r
665 char *pcMainGetTaskStatusMessage( void )\r
666 {\r
667 char * pcReturn;\r
668 \r
669         if( pcStatusMessage == NULL )\r
670         {\r
671                 pcReturn = ( char * ) "OK";\r
672         }\r
673         else\r
674         {\r
675                 pcReturn = ( char * ) pcStatusMessage;\r
676         }\r
677 \r
678         return pcReturn;\r
679 }\r
680 \r
681 \r
682 \r