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