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