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