]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_MB9B500_IAR_Keil/main-full.c
Added the digit counter timer to the FM3/IAR demo.
[freertos] / Demo / CORTEX_MB9B500_IAR_Keil / main-full.c
1 /*\r
2     FreeRTOS V7.0.0 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3 \r
4 \r
5         FreeRTOS supports many tools and architectures. V7.0.0 is sponsored by:\r
6         Atollic AB - Atollic provides professional embedded systems development\r
7         tools for C/C++ development, code analysis and test automation.\r
8         See http://www.atollic.com\r
9 \r
10 \r
11     ***************************************************************************\r
12      *                                                                       *\r
13      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
14      *    Complete, revised, and edited pdf reference manuals are also       *\r
15      *    available.                                                         *\r
16      *                                                                       *\r
17      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
18      *    ensuring you get running as quickly as possible and with an        *\r
19      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
20      *    the FreeRTOS project to continue with its mission of providing     *\r
21      *    professional grade, cross platform, de facto standard solutions    *\r
22      *    for microcontrollers - completely free of charge!                  *\r
23      *                                                                       *\r
24      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
25      *                                                                       *\r
26      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
27      *                                                                       *\r
28     ***************************************************************************\r
29 \r
30 \r
31     This file is part of the FreeRTOS distribution.\r
32 \r
33     FreeRTOS is free software; you can redistribute it and/or modify it under\r
34     the terms of the GNU General Public License (version 2) as published by the\r
35     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
36     >>>NOTE<<< The modification to the GPL is included to allow you to\r
37     distribute a combined work that includes FreeRTOS without being obliged to\r
38     provide the source code for proprietary components outside of the FreeRTOS\r
39     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
40     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
41     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
42     more details. You should have received a copy of the GNU General Public\r
43     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
44     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
45     by writing to Richard Barry, contact details for whom are available on the\r
46     FreeRTOS WEB site.\r
47 \r
48     1 tab == 4 spaces!\r
49 \r
50     http://www.FreeRTOS.org - Documentation, latest information, license and\r
51     contact details.\r
52 \r
53     http://www.SafeRTOS.com - A version that is certified for use in safety\r
54     critical systems.\r
55 \r
56     http://www.OpenRTOS.com - Commercial support, development, porting,\r
57     licensing and training services.\r
58 */\r
59 \r
60 /*\r
61  * main-blinky.c is included when the "Blinky" build configuration is used.\r
62  * main-full.c is included when the "Full" build configuration is used.\r
63  *\r
64  * main-full.c (this file) defines a comprehensive demo that creates many\r
65  * tasks, queues, semaphores and timers.  It also demonstrates how Cortex-M3\r
66  * interrupts can interact with FreeRTOS tasks/timers, and implements a simple\r
67  * and small interactive web server.\r
68  *\r
69  * This project runs on the SmartFusion A2F-EVAL-KIT evaluation board, which\r
70  * is populated with an A2F200M3F SmartFusion mixed signal FPGA.  The A2F200M3F\r
71  * incorporates a Cortex-M3 microcontroller.\r
72  *\r
73  * The main() Function:\r
74  * main() creates two demo specific software timers, one demo specific queue,\r
75  * and three demo specific tasks.  It then creates a whole host of 'standard\r
76  * demo' tasks/queues/semaphores, before starting the scheduler.  The demo\r
77  * specific tasks and timers are described in the comments here.  The standard\r
78  * demo tasks are described on the FreeRTOS.org web site.\r
79  *\r
80  * The standard demo tasks provide no specific functionality.  They are\r
81  * included to both test the FreeRTOS port, and provide examples of how the\r
82  * various FreeRTOS API functions can be used.\r
83  *\r
84  * The Demo Specific Queue Send Task:\r
85  * The queue send task is implemented by the prvQueueSendTask() function in\r
86  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly\r
87  * block for 200 milliseconds, before sending the value 100 to the queue that\r
88  * was created within main().  Once the value is sent, the task loops back\r
89  * around to block for another 200 milliseconds.\r
90  *\r
91  * The Demo Specific Queue Receive Task:\r
92  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
93  * in this file.  prvQueueReceiveTask() sits in a loop that causes it to\r
94  * repeatedly attempt to read data from the queue that was created within\r
95  * main().  When data is received, the task checks the value of the data, and\r
96  * if the value equals the expected 100, toggles the green LED.  The 'block\r
97  * time' parameter passed to the queue receive function specifies that the task\r
98  * should be held in the Blocked state indefinitely to wait for data to be\r
99  * available on the queue.  The queue receive task will only leave the Blocked\r
100  * state when the queue send task writes to the queue.  As the queue send task\r
101  * writes to the queue every 200 milliseconds, the queue receive task leaves\r
102  * the Blocked state every 200 milliseconds, and therefore toggles the LED\r
103  * every 200 milliseconds.\r
104  *\r
105  * The Demo Specific LED Software Timer and the Button Interrupt:\r
106  * The user button SW1 is configured to generate an interrupt each time it is\r
107  * pressed.  The interrupt service routine switches an LED on, and resets the\r
108  * LED software timer.  The LED timer has a 5000 millisecond (5 second) period,\r
109  * and uses a callback function that is defined to just turn the LED off again.\r
110  * Therefore, pressing the user button will turn the LED on, and the LED will\r
111  * remain on until a full five seconds pass without the button being pressed.\r
112  *\r
113  * The Demo Specific "Check" Callback Function:\r
114  * This is called each time the 'check' timer expires.  The check timer\r
115  * callback function inspects all the standard demo tasks to see if they are\r
116  * all executing as expected.  The check timer is initially configured to\r
117  * expire every three seconds, but will shorted this to every 500ms if an error\r
118  * is ever discovered.  The check timer callback toggles the LED defined by\r
119  * the mainCHECK_LED definition each time it executes.  Therefore, if LED\r
120  * mainCHECK_LED is toggling every three seconds, then no error have been found.\r
121  * If LED mainCHECK_LED is toggling every 500ms, then at least one errors has\r
122  * been found.  The task in which the error was discovered is displayed at the\r
123  * bottom of the "task stats" page that is served by the embedded web server.\r
124  *\r
125  * The Demo Specific Idle Hook Function:\r
126  * The idle hook function demonstrates how to query the amount of FreeRTOS heap\r
127  * space that is remaining (see vApplicationIdleHook() defined in this file).\r
128  *\r
129  * The Web Server Task:\r
130  * The IP address used by the SmartFusion target is configured by the\r
131  * definitions configIP_ADDR0 to configIP_ADDR3, which are located in the\r
132  * FreeRTOSConfig.h header file.  See the documentation page for this example\r
133  * on the http://www.FreeRTOS.org web site for further connection information.\r
134  */\r
135 \r
136 /* Kernel includes. */\r
137 #include "FreeRTOS.h"\r
138 #include "task.h"\r
139 #include "queue.h"\r
140 #include "timers.h"\r
141 \r
142 /* Fujitsu drivers/libraries. */\r
143 #include "mb9bf506n.h"\r
144 #include "system_mb9bf50x.h"\r
145 \r
146 /* Common demo includes. */\r
147 #include "partest.h"\r
148 #include "flash.h"\r
149 #include "BlockQ.h"\r
150 #include "death.h"\r
151 #include "blocktim.h"\r
152 #include "semtest.h"\r
153 #include "GenQTest.h"\r
154 #include "QPeek.h"\r
155 #include "recmutex.h"\r
156 #include "TimerDemo.h"\r
157 \r
158 /* Priorities at which the tasks are created. */\r
159 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
160 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
161 \r
162 /* The rate at which data is sent to the queue, specified in milliseconds, and\r
163 converted to ticks using the portTICK_RATE_MS constant. */\r
164 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_RATE_MS )\r
165 \r
166 /* The number of items the queue can hold.  This is 1 as the receive task\r
167 will remove items as they are added, meaning the send task should always find\r
168 the queue empty. */\r
169 #define mainQUEUE_LENGTH                        ( 1 )\r
170 \r
171 /* The LED toggled by the check timer callback function. */\r
172 #define mainCHECK_LED                           0x07UL\r
173 \r
174 /* The LED toggle by the queue receive task. */\r
175 #define mainTASK_CONTROLLED_LED         0x04UL\r
176 \r
177 /* The LED turned on by the button interrupt, and turned off by the LED timer. */\r
178 #define mainTIMER_CONTROLLED_LED        0x05UL\r
179 \r
180 /* Constant used by the standard timer test functions. */\r
181 #define mainTIMER_TEST_PERIOD           ( 50 )\r
182 \r
183 /* Priorities used by the various different tasks. */\r
184 #define mainCHECK_TASK_PRIORITY         ( configMAX_PRIORITIES - 1 )\r
185 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
186 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
187 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
188 #define mainCREATOR_TASK_PRIORITY   ( tskIDLE_PRIORITY + 3 )\r
189 #define mainFLASH_TASK_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
190 #define mainINTEGER_TASK_PRIORITY   ( tskIDLE_PRIORITY )\r
191 #define mainGEN_QUEUE_TASK_PRIORITY     ( tskIDLE_PRIORITY )\r
192 \r
193 /* The period at which the check timer will expire, in ms, provided no errors\r
194 have been reported by any of the standard demo tasks. */\r
195 #define mainCHECK_TIMER_PERIOD_MS       ( 3000UL / portTICK_RATE_MS )\r
196 \r
197 /* The period at which the check timer will expire, in ms, if an error has been\r
198 reported in one of the standard demo tasks. */\r
199 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 500UL / portTICK_RATE_MS )\r
200 \r
201 /* The period at which the digit counter timer will expire, in ms, and converted\r
202 to ticks using the portTICK_RATE_MS constant. */\r
203 #define mainDIGIT_COUNTER_TIMER_PERIOD_MS ( 250UL / portTICK_RATE_MS )\r
204 \r
205 /* The LED will remain on until the button has not been pushed for a full\r
206 5000ms. */\r
207 #define mainLED_TIMER_PERIOD_MS         ( 5000UL / portTICK_RATE_MS )\r
208 \r
209 /* A zero block time. */\r
210 #define mainDONT_BLOCK                          ( 0UL )\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 /*\r
214  * Setup the NVIC, LED outputs, and button inputs.\r
215  */\r
216 static void prvSetupHardware( void );\r
217 \r
218 /*\r
219  * The tasks as described in the comments at the top of this file.\r
220  */\r
221 static void prvQueueReceiveTask( void *pvParameters );\r
222 static void prvQueueSendTask( void *pvParameters );\r
223 \r
224 /*\r
225  * The LED timer callback function.  This does nothing but switch the red LED\r
226  * off.\r
227  */\r
228 static void prvLEDTimerCallback( xTimerHandle xTimer );\r
229 \r
230 /*\r
231  * The check timer callback function, as described at the top of this file.\r
232  */\r
233 static void prvCheckTimerCallback( xTimerHandle xTimer );\r
234 \r
235 /*\r
236  * The digit counter callback function, as described at the top of this file.\r
237  */\r
238 static void prvDigitCounterTimerCallback( xTimerHandle xTimer );\r
239 \r
240 /*\r
241  * This is not a 'standard' partest function, so the prototype is not in\r
242  * partest.h, and is instead included here.\r
243  */\r
244 void vParTestSetLEDFromISR( unsigned portBASE_TYPE uxLED, signed portBASE_TYPE xValue );\r
245 \r
246 /*-----------------------------------------------------------*/\r
247 \r
248 /* The queue used by both application specific demo tasks defined in this file. */\r
249 static xQueueHandle xQueue = NULL;\r
250 \r
251 /* The LED software timer.  This uses prvLEDTimerCallback() as it's callback\r
252 function. */\r
253 static xTimerHandle xLEDTimer = NULL;\r
254 \r
255 /* The counter software timer.  This displays a counting digit on one of the\r
256 seven segment displays. */\r
257 static xTimerHandle xDigitCounterTimer = NULL;\r
258 \r
259 /* The check timer.  This uses prvCheckTimerCallback() as it's callback\r
260 function. */\r
261 static xTimerHandle xCheckTimer = NULL;\r
262 \r
263 /* If an error is detected in a standard demo task, then pcStatusMessage will\r
264 be set to point to a string that identifies the offending task.  This is just\r
265 to make debugging easier. */\r
266 static const char *pcStatusMessage = NULL;\r
267 \r
268 /*-----------------------------------------------------------*/\r
269 \r
270 int main(void)\r
271 {\r
272         /* Configure the NVIC, LED outputs and button inputs. */\r
273         prvSetupHardware();\r
274 \r
275         /* Create the queue. */\r
276         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
277 \r
278         if( xQueue != NULL )\r
279         {\r
280                 /* Start the three application specific demo tasks, as described in the\r
281                 comments at the top of this     file. */\r
282                 xTaskCreate( prvQueueReceiveTask, ( signed char * ) "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
283                 xTaskCreate( prvQueueSendTask, ( signed char * ) "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
284 \r
285                 /* Create the software timer that is responsible for turning off the LED\r
286                 if the button is not pushed within 5000ms, as described at the top of\r
287                 this file. */\r
288                 xLEDTimer = xTimerCreate(       ( const signed char * ) "LEDTimer", /* A text name, purely to help debugging. */\r
289                                                                         ( mainLED_TIMER_PERIOD_MS ),            /* The timer period, in this case 5000ms (5s). */\r
290                                                                         pdFALSE,                                                        /* This is a one shot timer, so xAutoReload is set to pdFALSE. */\r
291                                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
292                                                                         prvLEDTimerCallback                                     /* The callback function that switches the LED off. */\r
293                                                                 );\r
294 \r
295                 /* Create the software timer that performs the 'check' functionality,\r
296                 as described at the top of this file. */\r
297                 xCheckTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */\r
298                                                                         ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
299                                                                         pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
300                                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
301                                                                         prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
302                                                                   );\r
303 \r
304                 /* Create the software timer that performs the 'digit counting'\r
305                 functionality, as described at the top of this file. */\r
306                 xDigitCounterTimer = xTimerCreate( ( const signed char * ) "DigitCounter",      /* A text name, purely to help debugging. */\r
307                                                                         ( mainDIGIT_COUNTER_TIMER_PERIOD_MS ),                  /* The timer period, in this case 3000ms (3s). */\r
308                                                                         pdTRUE,                                                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
309                                                                         ( void * ) 0,                                                                   /* The ID is not used, so can be set to anything. */\r
310                                                                         prvDigitCounterTimerCallback                                    /* The callback function that inspects the status of all the other tasks. */\r
311                                                                   );            \r
312                 \r
313                 /* Create a lot of 'standard demo' tasks. */\r
314                 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
315                 vCreateBlockTimeTasks();\r
316                 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
317                 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
318                 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
319                 vStartQueuePeekTasks();\r
320                 vStartRecursiveMutexTasks();\r
321                 vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
322 \r
323                 /* The suicide tasks must be created last, as they need to know how many\r
324                 tasks were running prior to their creation in order to ascertain whether\r
325                 or not the correct/expected number of tasks are running at any given\r
326                 time. */\r
327                 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
328 \r
329                 /* Start the tasks and timer running. */\r
330                 vTaskStartScheduler();\r
331         }\r
332 \r
333         /* If all is well, the scheduler will now be running, and the following line\r
334         will never be reached.  If the following line does execute, then there was\r
335         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
336         to be created.  See the memory management section on the FreeRTOS web site\r
337         for more details. */\r
338         for( ;; );\r
339 }\r
340 /*-----------------------------------------------------------*/\r
341 \r
342 static void prvCheckTimerCallback( xTimerHandle xTimer )\r
343 {\r
344         /* Check the standard demo tasks are running without error.   Latch the\r
345         latest reported error in the pcStatusMessage character pointer. */\r
346         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
347         {\r
348                 pcStatusMessage = "Error: GenQueue";\r
349         }\r
350 \r
351         if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
352         {\r
353                 pcStatusMessage = "Error: QueuePeek\r\n";\r
354         }\r
355 \r
356         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
357         {\r
358                 pcStatusMessage = "Error: BlockQueue\r\n";\r
359         }\r
360 \r
361         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
362         {\r
363                 pcStatusMessage = "Error: BlockTime\r\n";\r
364         }\r
365 \r
366         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
367         {\r
368                 pcStatusMessage = "Error: SemTest\r\n";\r
369         }\r
370 \r
371         if( xIsCreateTaskStillRunning() != pdTRUE )\r
372         {\r
373                 pcStatusMessage = "Error: Death\r\n";\r
374         }\r
375 \r
376         if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
377         {\r
378                 pcStatusMessage = "Error: RecMutex\r\n";\r
379         }\r
380 \r
381         if( xAreTimerDemoTasksStillRunning( ( mainCHECK_TIMER_PERIOD_MS ) ) != pdTRUE )\r
382         {\r
383                 pcStatusMessage = "Error: TimerDemo";\r
384         }\r
385 \r
386         /* Toggle the check LED to give an indication of the system status.  If\r
387         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
388         everything is ok.  A faster toggle indicates an error. */\r
389         vParTestToggleLED( mainCHECK_LED );\r
390 \r
391         /* Have any errors been latch in pcStatusMessage?  If so, shorten the\r
392         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
393         This will result in an increase in the rate at which mainCHECK_LED\r
394         toggles. */\r
395         if( pcStatusMessage != NULL )\r
396         {\r
397                 /* This call to xTimerChangePeriod() uses a zero block time.  Functions\r
398                 called from inside of a timer callback function must *never* attempt\r
399                 to block. */\r
400                 xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
401         }\r
402 }\r
403 /*-----------------------------------------------------------*/\r
404 \r
405 static void prvLEDTimerCallback( xTimerHandle xTimer )\r
406 {\r
407         /* The timer has expired - so no button pushes have occurred in the last\r
408         five seconds - turn the LED off. */\r
409         vParTestSetLED( mainTIMER_CONTROLLED_LED, pdFALSE );\r
410 }\r
411 /*-----------------------------------------------------------*/\r
412 \r
413 static void prvDigitCounterTimerCallback( xTimerHandle xTimer )\r
414 {\r
415 /* Define the bit patterns that display numbers on the seven segment display. */\r
416 static const unsigned short usNumbersPatterns[] = { 0xC000U, 0xF900U, 0xA400U, 0xB000U, 0x9900U, 0x9200U, 0x8200U, 0xF800U, 0x8000U, 0x9000U };\r
417 static long lCounter = 0L;\r
418 const long lNumberOfDigits = 10L;\r
419 \r
420         /* Display the next number, counting up. */\r
421         FM3_GPIO->PDOR1 = usNumbersPatterns[ lCounter ];\r
422 \r
423         /* Move onto the next digit. */ \r
424         lCounter++;\r
425         \r
426         /* Ensure the counter does not go off the end of the array. */\r
427         if( lCounter >= lNumberOfDigits )\r
428         {\r
429                 lCounter = 0L;\r
430         }\r
431 }\r
432 /*-----------------------------------------------------------*/\r
433 \r
434 /* The ISR executed when the user button is pushed. */\r
435 void INT0_7_Handler( void )\r
436 {\r
437 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
438 \r
439         /* The button was pushed, so ensure the LED is on before resetting the\r
440         LED timer.  The LED timer will turn the LED off if the button is not\r
441         pushed within 5000ms. */\r
442         vParTestSetLEDFromISR( mainTIMER_CONTROLLED_LED, pdTRUE );\r
443 \r
444         /* This interrupt safe FreeRTOS function can be called from this interrupt\r
445         because the interrupt priority is below the\r
446         configMAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */\r
447         xTimerResetFromISR( xLEDTimer, &xHigherPriorityTaskWoken );\r
448 \r
449         /* Clear the interrupt before leaving.  This just clears all the interrupts\r
450         for simplicity, as only one is actually used in this simple demo anyway. */\r
451         FM3_EXTI->EICL = 0x0000;\r
452 \r
453         /* If calling xTimerResetFromISR() caused a task (in this case the timer\r
454         service/daemon task) to unblock, and the unblocked task has a priority\r
455         higher than or equal to the task that was interrupted, then\r
456         xHigherPriorityTaskWoken will now be set to pdTRUE, and calling\r
457         portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */\r
458         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
459 }\r
460 /*-----------------------------------------------------------*/\r
461 \r
462 static void prvQueueSendTask( void *pvParameters )\r
463 {\r
464 portTickType xNextWakeTime;\r
465 const unsigned long ulValueToSend = 100UL;\r
466 \r
467         /* The timer command queue will have been filled when the timer test tasks\r
468         were created in main() (this is part of the test they perform).  Therefore,\r
469         while the check and digit counter timers can be created in main(), they\r
470         cannot be started from main().  Once the scheduler has started, the timer\r
471         service task will drain the command queue, and now the check and digit\r
472         counter timers can be started successfully. */\r
473         xTimerStart( xCheckTimer, portMAX_DELAY );\r
474         xTimerStart( xDigitCounterTimer, portMAX_DELAY );\r
475 \r
476         /* Initialise xNextWakeTime - this only needs to be done once. */\r
477         xNextWakeTime = xTaskGetTickCount();\r
478 \r
479         for( ;; )\r
480         {\r
481                 /* Place this task in the blocked state until it is time to run again.\r
482                 The block time is specified in ticks, the constant used converts ticks\r
483                 to ms.  While in the Blocked state this task will not consume any CPU\r
484                 time. */\r
485                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
486 \r
487                 /* Send to the queue - causing the queue receive task to unblock and\r
488                 toggle an LED.  0 is used as the block time so the sending operation\r
489                 will not block - it shouldn't need to block as the queue should always\r
490                 be empty at this point in the code. */\r
491                 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );\r
492         }\r
493 }\r
494 /*-----------------------------------------------------------*/\r
495 \r
496 static void prvQueueReceiveTask( void *pvParameters )\r
497 {\r
498 unsigned long ulReceivedValue;\r
499 \r
500         for( ;; )\r
501         {\r
502                 /* Wait until something arrives in the queue - this task will block\r
503                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
504                 FreeRTOSConfig.h. */\r
505                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
506 \r
507                 /*  To get here something must have been received from the queue, but\r
508                 is it the expected value?  If it is, toggle the LED. */\r
509                 if( ulReceivedValue == 100UL )\r
510                 {\r
511                         vParTestToggleLED( mainTASK_CONTROLLED_LED );\r
512                 }\r
513         }\r
514 }\r
515 /*-----------------------------------------------------------*/\r
516 \r
517 static void prvSetupHardware( void )\r
518 {\r
519 const unsigned short usButtonInputBit = 0x01U;\r
520 \r
521         SystemInit();\r
522         SystemCoreClockUpdate();\r
523 \r
524         /* Initialise the IO used for the LEDs on the 7 segment displays. */\r
525         vParTestInitialise();   \r
526         \r
527         /* Set the switches to input (P18->P1F). */\r
528         FM3_GPIO->DDR5 = 0x0000;\r
529         FM3_GPIO->PFR5 = 0x0000;\r
530 \r
531         /* Assign the button input as GPIO. */\r
532         FM3_GPIO->PFR1 |= usButtonInputBit;\r
533         \r
534         /* Button interrupt on falling edge. */\r
535         FM3_EXTI->ELVR  = 0x0003;\r
536 \r
537         /* Clear all external interrupts. */\r
538         FM3_EXTI->EICL  = 0x0000;\r
539 \r
540         /* Enable the button interrupt. */\r
541         FM3_EXTI->ENIR |= usButtonInputBit;\r
542         \r
543         /* Setup the GPIO and the NVIC for the switch used in this simple demo. */\r
544         NVIC_SetPriority( EXINT0_7_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );\r
545     NVIC_EnableIRQ( EXINT0_7_IRQn );\r
546 }\r
547 /*-----------------------------------------------------------*/\r
548 \r
549 void vApplicationMallocFailedHook( void )\r
550 {\r
551         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
552         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
553         internally by FreeRTOS API functions that create tasks, queues, software\r
554         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
555         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
556         for( ;; );\r
557 }\r
558 /*-----------------------------------------------------------*/\r
559 \r
560 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )\r
561 {\r
562         ( void ) pcTaskName;\r
563         ( void ) pxTask;\r
564 \r
565         /* Run time stack overflow checking is performed if\r
566         configconfigCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
567         function is called if a stack overflow is detected. */\r
568         taskDISABLE_INTERRUPTS();\r
569         for( ;; );\r
570 }\r
571 /*-----------------------------------------------------------*/\r
572 \r
573 void vApplicationIdleHook( void )\r
574 {\r
575 volatile size_t xFreeStackSpace;\r
576 \r
577         /* This function is called on each cycle of the idle task.  In this case it\r
578         does nothing useful, other than report the amount of FreeRTOS heap that\r
579         remains unallocated. */\r
580         xFreeStackSpace = xPortGetFreeHeapSize();\r
581 \r
582         if( xFreeStackSpace > 100 )\r
583         {\r
584                 /* By now, the kernel has allocated everything it is going to, so\r
585                 if there is a lot of heap remaining unallocated then\r
586                 the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be\r
587                 reduced accordingly. */\r
588         }\r
589 }\r
590 /*-----------------------------------------------------------*/\r
591 \r
592 char *pcGetTaskStatusMessage( void )\r
593 {\r
594         /* Not bothered about a critical section here although technically because\r
595         of the task priorities the pointer could change it will be atomic if not\r
596         near atomic and its not critical. */\r
597         if( pcStatusMessage == NULL )\r
598         {\r
599                 return "All tasks running without error";\r
600         }\r
601         else\r
602         {\r
603                 return ( char * ) pcStatusMessage;\r
604         }\r
605 }\r
606 /*-----------------------------------------------------------*/\r
607 \r