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