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