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