]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_MB9B500_IAR_Keil/main-full.c
New MicroBlaze port: Added a FreeRTOS exception handler, and installed it in each...
[freertos] / Demo / CORTEX_MB9B500_IAR_Keil / main-full.c
1 /*\r
2     FreeRTOS V7.0.1 - 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.\r
67  *\r
68  * This project runs on the SK-FM3-100PMC evaluation board, which is populated\r
69  * with an MB9BF5006N Cortex-M3 based microcontroller.\r
70  *\r
71  * The main() Function:\r
72  * main() creates three demo specific software timers, one demo specific queue,\r
73  * and two demo specific tasks.  It then creates a whole host of 'standard\r
74  * demo' tasks/queues/semaphores, before starting the scheduler.  The demo\r
75  * specific tasks and timers are described in the comments here.  The standard\r
76  * demo tasks are described on the FreeRTOS.org web site.\r
77  *\r
78  * The standard demo tasks provide no specific functionality.  They are\r
79  * included to both test the FreeRTOS port, and provide examples of how the\r
80  * various FreeRTOS API functions can be used.\r
81  *\r
82  * This demo creates 43 tasks in total.  If you want a simpler demo, use the\r
83  * Blinky build configuration.\r
84  *\r
85  * The Demo Specific Queue Send Task:\r
86  * The queue send task is implemented by the prvQueueSendTask() function in\r
87  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly\r
88  * block for 200 milliseconds, before sending the value 100 to the queue that\r
89  * was created within main().  Once the value is sent, the task loops back\r
90  * around to block for another 200 milliseconds.\r
91  *\r
92  * The Demo Specific Queue Receive Task:\r
93  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
94  * in this file.  prvQueueReceiveTask() sits in a loop that causes it to\r
95  * repeatedly attempt to read data from the queue that was created within\r
96  * main().  When data is received, the task checks the value of the data, and\r
97  * if the value equals the expected 100, toggles an LED in the 7 segment display\r
98  * (see the documentation page for this demo on the FreeRTOS.org site to see\r
99  * which LED is used).  The 'block time' parameter passed to the queue receive\r
100  * function specifies that the task should be held in the Blocked state\r
101  * indefinitely to wait for data to be available on the queue.  The queue\r
102  * receive task will only leave the Blocked state when the queue send task\r
103  * writes to the queue.  As the queue send task writes to the queue every 200\r
104  * milliseconds, the queue receive task leaves the Blocked state every 200\r
105  * milliseconds, and therefore toggles the LED every 200 milliseconds.\r
106  *\r
107  * The Demo Specific LED Software Timer and the Button Interrupt:\r
108  * The user button SW2 is configured to generate an interrupt each time it is\r
109  * pressed.  The interrupt service routine switches an LED on, and resets the\r
110  * LED software timer.  The LED timer has a 5000 millisecond (5 second) period,\r
111  * and uses a callback function that is defined to just turn the LED off again.\r
112  * Therefore, pressing the user button will turn the LED on, and the LED will\r
113  * remain on until a full five seconds pass without the button being pressed.\r
114  * See the documentation page for this demo on the FreeRTOS.org web site to see\r
115  * which LED is used.\r
116  *\r
117  * The Demo Specific "Check" Callback Function:\r
118  * This is called each time the 'check' timer expires.  The check timer\r
119  * callback function inspects all the standard demo tasks to see if they are\r
120  * all executing as expected.  The check timer is initially configured to\r
121  * expire every three seconds, but will shorted this to every 500ms if an error\r
122  * is ever discovered.  The check timer callback toggles the LED defined by\r
123  * the mainCHECK_LED definition each time it executes.  Therefore, if LED\r
124  * mainCHECK_LED is toggling every three seconds, then no error have been found.\r
125  * If LED mainCHECK_LED is toggling every 500ms, then at least one errors has\r
126  * been found.  The variable pcStatusMessage is set to a string that indicates\r
127  * which task reported an error.  See the documentation page for this demo on\r
128  * the FreeRTOS.org web site to see which LED in the 7 segment display is used.\r
129  *\r
130  * The Demo Specific "Digit Counter" Callback Function:\r
131  * This is called each time the 'digit counter' timer expires.  It causes the\r
132  * digits 0 to 9 to be displayed in turn as the first character of the two\r
133  * character display.  The LEDs in the other digit of the two character\r
134  * display are used as general purpose LEDs, as described in this comment block.\r
135  *\r
136  * The Demo Specific Idle Hook Function:\r
137  * The idle hook function demonstrates how to query the amount of FreeRTOS heap\r
138  * space that is remaining (see vApplicationIdleHook() defined in this file).\r
139  *\r
140  * The Demo Specific Tick Hook Function:\r
141  * The tick hook function is used to test the interrupt safe software timer\r
142  * functionality.\r
143  */\r
144 \r
145 /* Kernel includes. */\r
146 #include "FreeRTOS.h"\r
147 #include "task.h"\r
148 #include "queue.h"\r
149 #include "timers.h"\r
150 \r
151 /* Fujitsu drivers/libraries. */\r
152 #include "mb9bf506n.h"\r
153 #include "system_mb9bf50x.h"\r
154 \r
155 /* Common demo includes. */\r
156 #include "partest.h"\r
157 #include "flash.h"\r
158 #include "BlockQ.h"\r
159 #include "death.h"\r
160 #include "blocktim.h"\r
161 #include "semtest.h"\r
162 #include "GenQTest.h"\r
163 #include "QPeek.h"\r
164 #include "recmutex.h"\r
165 #include "TimerDemo.h"\r
166 #include "comtest2.h"\r
167 #include "PollQ.h"\r
168 #include "countsem.h"\r
169 #include "dynamic.h"\r
170 \r
171 /* The rate at which data is sent to the queue, specified in milliseconds, and\r
172 converted to ticks using the portTICK_RATE_MS constant. */\r
173 #define mainQUEUE_SEND_FREQUENCY_MS     ( 200 / portTICK_RATE_MS )\r
174 \r
175 /* The number of items the queue can hold.  This is 1 as the receive task\r
176 will remove items as they are added, meaning the send task should always find\r
177 the queue empty. */\r
178 #define mainQUEUE_LENGTH                        ( 1 )\r
179 \r
180 /* The LED toggled by the check timer callback function.  This is an LED in the\r
181 second digit of the two digit 7 segment display.  See the documentation page\r
182 for this demo on the FreeRTOS.org web site to see which LED this relates to. */\r
183 #define mainCHECK_LED                           0x07UL\r
184 \r
185 /* The LED toggle by the queue receive task.  This is an LED in the second digit\r
186 of the two digit 7 segment display.  See the documentation page for this demo on\r
187 the FreeRTOS.org web site to see which LED this relates to. */\r
188 #define mainTASK_CONTROLLED_LED         0x06UL\r
189 \r
190 /* The LED turned on by the button interrupt, and turned off by the LED timer.\r
191 This is an LED in the second digit of the two digit 7 segment display.  See the\r
192 documentation page for this demo on the FreeRTOS.org web site to see which LED\r
193 this relates to. */\r
194 #define mainTIMER_CONTROLLED_LED        0x05UL\r
195 \r
196 /* The LED used by the comtest tasks. See the comtest.c file for more\r
197 information.  The LEDs used by the comtest task are in the second digit of the\r
198 two digit 7 segment display.  See the documentation page for this demo on the\r
199 FreeRTOS.org web site to see which LEDs this relates to. */\r
200 #define mainCOM_TEST_LED                        ( 3 )\r
201 \r
202 /* Constant used by the standard timer test functions. */\r
203 #define mainTIMER_TEST_PERIOD           ( 50 )\r
204 \r
205 /* Priorities used by the various different standard demo tasks. */\r
206 #define mainCHECK_TASK_PRIORITY         ( configMAX_PRIORITIES - 1 )\r
207 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
208 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
209 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
210 #define mainCREATOR_TASK_PRIORITY   ( tskIDLE_PRIORITY + 3 )\r
211 #define mainFLASH_TASK_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
212 #define mainINTEGER_TASK_PRIORITY   ( tskIDLE_PRIORITY )\r
213 #define mainGEN_QUEUE_TASK_PRIORITY     ( tskIDLE_PRIORITY )\r
214 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
215 \r
216 /* Priorities defined in this main-full.c file. */\r
217 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
218 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
219 \r
220 /* The period at which the check timer will expire, in ms, provided no errors\r
221 have been reported by any of the standard demo tasks.  ms are converted to the\r
222 equivalent in ticks using the portTICK_RATE_MS constant. */\r
223 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_RATE_MS )\r
224 \r
225 /* The period at which the check timer will expire, in ms, if an error has been\r
226 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
227 in ticks using the portTICK_RATE_MS constant. */\r
228 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 500UL / portTICK_RATE_MS )\r
229 \r
230 /* The period at which the digit counter timer will expire, in ms, and converted\r
231 to ticks using the portTICK_RATE_MS constant. */\r
232 #define mainDIGIT_COUNTER_TIMER_PERIOD_MS       ( 250UL / portTICK_RATE_MS )\r
233 \r
234 /* The LED will remain on until the button has not been pushed for a full\r
235 5000ms. */\r
236 #define mainLED_TIMER_PERIOD_MS                         ( 5000UL / portTICK_RATE_MS )\r
237 \r
238 /* A zero block time. */\r
239 #define mainDONT_BLOCK                                          ( 0UL )\r
240 \r
241 /* Baud rate used by the comtest tasks. */\r
242 #define mainCOM_TEST_BAUD_RATE                          ( 115200UL )\r
243 \r
244 /*-----------------------------------------------------------*/\r
245 \r
246 /*\r
247  * Setup the NVIC, LED outputs, and button inputs.\r
248  */\r
249 static void prvSetupHardware( void );\r
250 \r
251 /*\r
252  * The application specific (not common demo) tasks as described in the comments\r
253  * at the top of this file.\r
254  */\r
255 static void prvQueueReceiveTask( void *pvParameters );\r
256 static void prvQueueSendTask( void *pvParameters );\r
257 \r
258 /*\r
259  * The LED timer callback function.  This does nothing but switch an LED off.\r
260  */\r
261 static void prvLEDTimerCallback( xTimerHandle xTimer );\r
262 \r
263 /*\r
264  * The check timer callback function, as described at the top of this file.\r
265  */\r
266 static void prvCheckTimerCallback( xTimerHandle xTimer );\r
267 \r
268 /*\r
269  * The digit counter callback function, as described at the top of this file.\r
270  */\r
271 static void prvDigitCounterTimerCallback( xTimerHandle xTimer );\r
272 \r
273 /*\r
274  * This is not a 'standard' partest function, so the prototype is not in\r
275  * partest.h, and is instead included here.\r
276  */\r
277 void vParTestSetLEDFromISR( unsigned portBASE_TYPE uxLED, signed portBASE_TYPE xValue );\r
278 \r
279 /*-----------------------------------------------------------*/\r
280 \r
281 /* The queue used by both application specific demo tasks defined in this file. */\r
282 static xQueueHandle xQueue = NULL;\r
283 \r
284 /* The LED software timer.  This uses prvLEDTimerCallback() as it's callback\r
285 function. */\r
286 static xTimerHandle xLEDTimer = NULL;\r
287 \r
288 /* The digit counter software timer.  This displays a counting digit on one half\r
289 of the seven segment displays. */\r
290 static xTimerHandle xDigitCounterTimer = NULL;\r
291 \r
292 /* The check timer.  This uses prvCheckTimerCallback() as its callback\r
293 function. */\r
294 static xTimerHandle xCheckTimer = NULL;\r
295 \r
296 /* If an error is detected in a standard demo task, then pcStatusMessage will\r
297 be set to point to a string that identifies the offending task.  This is just\r
298 to make debugging easier. */\r
299 static const char *pcStatusMessage = NULL;\r
300 \r
301 /*-----------------------------------------------------------*/\r
302 \r
303 int main(void)\r
304 {\r
305         /* Configure the NVIC, LED outputs and button inputs. */\r
306         prvSetupHardware();\r
307 \r
308         /* Create the queue. */\r
309         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
310 \r
311         if( xQueue != NULL )\r
312         {\r
313                 /* Start the two application specific demo tasks, as described in the\r
314                 comments at the top of this     file. */\r
315                 xTaskCreate( prvQueueReceiveTask, ( signed char * ) "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
316                 xTaskCreate( prvQueueSendTask, ( signed char * ) "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
317 \r
318                 /* Create the software timer that is responsible for turning off the LED\r
319                 if the button is not pushed within 5000ms, as described at the top of\r
320                 this file. */\r
321                 xLEDTimer = xTimerCreate(       ( const signed char * ) "LEDTimer", /* A text name, purely to help debugging. */\r
322                                                                         ( mainLED_TIMER_PERIOD_MS ),            /* The timer period, in this case 5000ms (5s). */\r
323                                                                         pdFALSE,                                                        /* This is a one shot timer, so xAutoReload is set to pdFALSE. */\r
324                                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
325                                                                         prvLEDTimerCallback                                     /* The callback function that switches the LED off. */\r
326                                                                 );\r
327 \r
328                 /* Create the software timer that performs the 'check' functionality,\r
329                 as described at the top of this file. */\r
330                 xCheckTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */\r
331                                                                         ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
332                                                                         pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
333                                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
334                                                                         prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
335                                                                   );\r
336 \r
337                 /* Create the software timer that performs the 'digit counting'\r
338                 functionality, as described at the top of this file. */\r
339                 xDigitCounterTimer = xTimerCreate( ( const signed char * ) "DigitCounter",      /* A text name, purely to help debugging. */\r
340                                                                         ( mainDIGIT_COUNTER_TIMER_PERIOD_MS ),                  /* The timer period, in this case 3000ms (3s). */\r
341                                                                         pdTRUE,                                                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
342                                                                         ( void * ) 0,                                                                   /* The ID is not used, so can be set to anything. */\r
343                                                                         prvDigitCounterTimerCallback                                    /* The callback function that inspects the status of all the other tasks. */\r
344                                                                   );            \r
345                 \r
346                 /* Create a lot of 'standard demo' tasks.  Over 40 tasks are created in\r
347                 this demo.  For a much simpler demo, select the 'blinky' build\r
348                 configuration. */\r
349                 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
350                 vCreateBlockTimeTasks();\r
351                 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
352                 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
353                 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
354                 vStartQueuePeekTasks();\r
355                 vStartRecursiveMutexTasks();\r
356                 vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
357                 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
358                 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
359                 vStartCountingSemaphoreTasks();\r
360                 vStartDynamicPriorityTasks();\r
361                 \r
362                 /* The suicide tasks must be created last, as they need to know how many\r
363                 tasks were running prior to their creation in order to ascertain whether\r
364                 or not the correct/expected number of tasks are running at any given\r
365                 time. */\r
366                 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
367 \r
368                 /* Start the tasks and timer running. */\r
369                 vTaskStartScheduler();\r
370         }\r
371 \r
372         /* If all is well, the scheduler will now be running, and the following line\r
373         will never be reached.  If the following line does execute, then there was\r
374         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
375         to be created.  See the memory management section on the FreeRTOS web site\r
376         for more details. */\r
377         for( ;; );\r
378 }\r
379 /*-----------------------------------------------------------*/\r
380 \r
381 static void prvCheckTimerCallback( xTimerHandle xTimer )\r
382 {\r
383         /* Check the standard demo tasks are running without error.   Latch the\r
384         latest reported error in the pcStatusMessage character pointer. */\r
385         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
386         {\r
387                 pcStatusMessage = "Error: GenQueue";\r
388         }\r
389 \r
390         if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
391         {\r
392                 pcStatusMessage = "Error: QueuePeek\r\n";\r
393         }\r
394 \r
395         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
396         {\r
397                 pcStatusMessage = "Error: BlockQueue\r\n";\r
398         }\r
399 \r
400         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
401         {\r
402                 pcStatusMessage = "Error: BlockTime\r\n";\r
403         }\r
404 \r
405         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
406         {\r
407                 pcStatusMessage = "Error: SemTest\r\n";\r
408         }\r
409 \r
410         if( xIsCreateTaskStillRunning() != pdTRUE )\r
411         {\r
412                 pcStatusMessage = "Error: Death\r\n";\r
413         }\r
414 \r
415         if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
416         {\r
417                 pcStatusMessage = "Error: RecMutex\r\n";\r
418         }\r
419 \r
420         if( xAreComTestTasksStillRunning() != pdPASS )\r
421         {\r
422                 pcStatusMessage = "Error: ComTest\r\n";\r
423         }\r
424         \r
425         if( xAreTimerDemoTasksStillRunning( ( mainCHECK_TIMER_PERIOD_MS ) ) != pdTRUE )\r
426         {\r
427                 pcStatusMessage = "Error: TimerDemo";\r
428         }\r
429 \r
430         if( xArePollingQueuesStillRunning() != pdTRUE )\r
431         {\r
432                 pcStatusMessage = "Error: PollQueue";\r
433         }\r
434 \r
435         if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
436         {\r
437                 pcStatusMessage = "Error: CountSem";\r
438         }\r
439         \r
440         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
441         {\r
442                 pcStatusMessage = "Error: DynamicPriority";\r
443         }\r
444         \r
445         /* Toggle the check LED to give an indication of the system status.  If\r
446         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
447         everything is ok.  A faster toggle indicates an error. */\r
448         vParTestToggleLED( mainCHECK_LED );\r
449 \r
450         /* Have any errors been latch in pcStatusMessage?  If so, shorten the\r
451         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
452         This will result in an increase in the rate at which mainCHECK_LED\r
453         toggles. */\r
454         if( pcStatusMessage != NULL )\r
455         {\r
456                 /* This call to xTimerChangePeriod() uses a zero block time.  Functions\r
457                 called from inside of a timer callback function must *never* attempt\r
458                 to block. */\r
459                 xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
460         }\r
461 }\r
462 /*-----------------------------------------------------------*/\r
463 \r
464 static void prvLEDTimerCallback( xTimerHandle xTimer )\r
465 {\r
466         /* The timer has expired - so no button pushes have occurred in the last\r
467         five seconds - turn the LED off. */\r
468         vParTestSetLED( mainTIMER_CONTROLLED_LED, pdFALSE );\r
469 }\r
470 /*-----------------------------------------------------------*/\r
471 \r
472 static void prvDigitCounterTimerCallback( xTimerHandle xTimer )\r
473 {\r
474 /* Define the bit patterns that display numbers on the seven segment display. */\r
475 static const unsigned short usNumbersPatterns[] = { 0xC000U, 0xF900U, 0xA400U, 0xB000U, 0x9900U, 0x9200U, 0x8200U, 0xF800U, 0x8000U, 0x9000U };\r
476 static long lCounter = 0L;\r
477 const long lNumberOfDigits = 10L;\r
478 \r
479         /* Display the next number, counting up. */\r
480         FM3_GPIO->PDOR1 = usNumbersPatterns[ lCounter ];\r
481 \r
482         /* Move onto the next digit. */ \r
483         lCounter++;\r
484         \r
485         /* Ensure the counter does not go off the end of the array. */\r
486         if( lCounter >= lNumberOfDigits )\r
487         {\r
488                 lCounter = 0L;\r
489         }\r
490 }\r
491 /*-----------------------------------------------------------*/\r
492 \r
493 /* The ISR executed when the user button is pushed. */\r
494 void INT0_7_Handler( void )\r
495 {\r
496 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
497 \r
498         /* The button was pushed, so ensure the LED is on before resetting the\r
499         LED timer.  The LED timer will turn the LED off if the button is not\r
500         pushed within 5000ms. */\r
501         vParTestSetLEDFromISR( mainTIMER_CONTROLLED_LED, pdTRUE );\r
502 \r
503         /* This interrupt safe FreeRTOS function can be called from this interrupt\r
504         because the interrupt priority is below the\r
505         configMAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */\r
506         xTimerResetFromISR( xLEDTimer, &xHigherPriorityTaskWoken );\r
507 \r
508         /* Clear the interrupt before leaving.  This just clears all the interrupts\r
509         for simplicity, as only one is actually used in this simple demo anyway. */\r
510         FM3_EXTI->EICL = 0x0000;\r
511 \r
512         /* If calling xTimerResetFromISR() caused a task (in this case the timer\r
513         service/daemon task) to unblock, and the unblocked task has a priority\r
514         higher than or equal to the task that was interrupted, then\r
515         xHigherPriorityTaskWoken will now be set to pdTRUE, and calling\r
516         portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */\r
517         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
518 }\r
519 /*-----------------------------------------------------------*/\r
520 \r
521 static void prvQueueSendTask( void *pvParameters )\r
522 {\r
523 portTickType xNextWakeTime;\r
524 const unsigned long ulValueToSend = 100UL;\r
525 \r
526         /* The timer command queue will have been filled when the timer test tasks\r
527         were created in main() (this is part of the test they perform).  Therefore,\r
528         while the check and digit counter timers can be created in main(), they\r
529         cannot be started from main().  Once the scheduler has started, the timer\r
530         service task will drain the command queue, and now the check and digit\r
531         counter timers can be started successfully. */\r
532         xTimerStart( xCheckTimer, portMAX_DELAY );\r
533         xTimerStart( xDigitCounterTimer, portMAX_DELAY );\r
534 \r
535         /* Initialise xNextWakeTime - this only needs to be done once. */\r
536         xNextWakeTime = xTaskGetTickCount();\r
537 \r
538         for( ;; )\r
539         {\r
540                 /* Place this task in the blocked state until it is time to run again.\r
541                 The block time is specified in ticks, the constant used converts ticks\r
542                 to ms.  While in the Blocked state this task will not consume any CPU\r
543                 time. */\r
544                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
545 \r
546                 /* Send to the queue - causing the queue receive task to unblock and\r
547                 toggle an LED.  0 is used as the block time so the sending operation\r
548                 will not block - it shouldn't need to block as the queue should always\r
549                 be empty at this point in the code. */\r
550                 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );\r
551         }\r
552 }\r
553 /*-----------------------------------------------------------*/\r
554 \r
555 static void prvQueueReceiveTask( void *pvParameters )\r
556 {\r
557 unsigned long ulReceivedValue;\r
558 \r
559         for( ;; )\r
560         {\r
561                 /* Wait until something arrives in the queue - this task will block\r
562                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
563                 FreeRTOSConfig.h. */\r
564                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
565 \r
566                 /*  To get here something must have been received from the queue, but\r
567                 is it the expected value?  If it is, toggle the LED. */\r
568                 if( ulReceivedValue == 100UL )\r
569                 {\r
570                         vParTestToggleLED( mainTASK_CONTROLLED_LED );\r
571                 }\r
572         }\r
573 }\r
574 /*-----------------------------------------------------------*/\r
575 \r
576 static void prvSetupHardware( void )\r
577 {\r
578 const unsigned short usButtonInputBit = 0x01U;\r
579 \r
580         SystemInit();\r
581         SystemCoreClockUpdate();\r
582 \r
583         /* Initialise the IO used for the LEDs on the 7 segment displays. */\r
584         vParTestInitialise();   \r
585         \r
586         /* Set the switches to input (P18->P1F). */\r
587         FM3_GPIO->DDR5 = 0x0000;\r
588         FM3_GPIO->PFR5 = 0x0000;\r
589 \r
590         /* Assign the button input as GPIO. */\r
591         FM3_GPIO->PFR1 |= usButtonInputBit;\r
592         \r
593         /* Button interrupt on falling edge. */\r
594         FM3_EXTI->ELVR  = 0x0003;\r
595 \r
596         /* Clear all external interrupts. */\r
597         FM3_EXTI->EICL  = 0x0000;\r
598 \r
599         /* Enable the button interrupt. */\r
600         FM3_EXTI->ENIR |= usButtonInputBit;\r
601         \r
602         /* Setup the GPIO and the NVIC for the switch used in this simple demo. */\r
603         NVIC_SetPriority( EXINT0_7_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );\r
604     NVIC_EnableIRQ( EXINT0_7_IRQn );\r
605 }\r
606 /*-----------------------------------------------------------*/\r
607 \r
608 void vApplicationMallocFailedHook( void )\r
609 {\r
610         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
611         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
612         internally by FreeRTOS API functions that create tasks, queues, software\r
613         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
614         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
615         for( ;; );\r
616 }\r
617 /*-----------------------------------------------------------*/\r
618 \r
619 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )\r
620 {\r
621         ( void ) pcTaskName;\r
622         ( void ) pxTask;\r
623 \r
624         /* Run time stack overflow checking is performed if\r
625         configconfigCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
626         function is called if a stack overflow is detected. */\r
627         taskDISABLE_INTERRUPTS();\r
628         for( ;; );\r
629 }\r
630 /*-----------------------------------------------------------*/\r
631 \r
632 void vApplicationIdleHook( void )\r
633 {\r
634 volatile size_t xFreeStackSpace;\r
635 \r
636         /* This function is called on each cycle of the idle task.  In this case it\r
637         does nothing useful, other than report the amount of FreeRTOS heap that\r
638         remains unallocated. */\r
639         xFreeStackSpace = xPortGetFreeHeapSize();\r
640 \r
641         if( xFreeStackSpace > 100 )\r
642         {\r
643                 /* By now, the kernel has allocated everything it is going to, so\r
644                 if there is a lot of heap remaining unallocated then\r
645                 the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be\r
646                 reduced accordingly. */\r
647         }\r
648 }\r
649 /*-----------------------------------------------------------*/\r
650 \r
651 void vApplicationTickHook( void )\r
652 {\r
653         /* Call the periodic timer test, which tests the timer API functions that\r
654         can be called from an ISR. */\r
655         vTimerPeriodicISRTests();\r
656 }       \r
657 /*-----------------------------------------------------------*/\r
658 \r