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