]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32L152_IAR/main.c
Update version number ready to release the FAT file system demo.
[freertos] / FreeRTOS / Demo / CORTEX_STM32L152_IAR / main.c
1 /*\r
2     FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*\r
76  * The documentation page for this demo available on http://www.FreeRTOS.org\r
77  * documents the hardware configuration required to run this demo.  It also\r
78  * provides more information on the expected demo application behaviour.\r
79  *\r
80  * main() creates all the demo application tasks, then starts the scheduler.\r
81  * A lot of the created tasks are from the pool of "standard demo" tasks.  The\r
82  * web documentation provides more details of the standard demo tasks, which\r
83  * provide no particular functionality but do provide good examples of how to\r
84  * use the FreeRTOS API.\r
85  *\r
86  * In addition to the standard demo tasks, the following tasks, interrupts and\r
87  * tests are defined and/or created within this file:\r
88  *\r
89  * "LCD" task - The LCD task is a 'gatekeeper' task.  It is the only task that\r
90  * is permitted to access the LCD and therefore ensures access to the LCD is\r
91  * always serialised and there are no mutual exclusion issues.  When a task or\r
92  * an interrupt wants to write to the LCD, it does not access the LCD directly\r
93  * but instead sends the message to the LCD task.  The LCD task then performs\r
94  * the actual LCD output.  This mechanism also allows interrupts to, in effect,\r
95  * write to the LCD by sending messages to the LCD task.\r
96  *\r
97  * The LCD task is also a demonstration of a 'controller' task design pattern.\r
98  * Some tasks do not actually send a string to the LCD task directly, but\r
99  * instead send a command that is interpreted by the LCD task.  In a normal\r
100  * application these commands can be control values or set points, in this\r
101  * simple example the commands just result in messages being displayed on the\r
102  * LCD.\r
103  *\r
104  * "Button Poll" task - This task polls the state of the 'up' key on the\r
105  * joystick input device.  It uses the vTaskDelay() API function to control\r
106  * the poll rate to ensure debouncing is not necessary and that the task does\r
107  * not use all the available CPU processing time.\r
108  *\r
109  * Button Interrupt and run time stats display - The select button on the\r
110  * joystick input device is configured to generate an external interrupt.  The\r
111  * handler for this interrupt sends a message to LCD task, which interprets the\r
112  * message to mean, firstly write a message to the LCD, and secondly, generate\r
113  * a table of run time statistics.  The run time statistics are displayed as a\r
114  * table that contains information on how much processing time each task has\r
115  * been allocated since the application started to execute.  This information\r
116  * is provided both as an absolute time, and as a percentage of the total run\r
117  * time.  The information is displayed in the terminal IO window of the IAR\r
118  * embedded workbench.  The online documentation for this demo shows a screen\r
119  * shot demonstrating where the run time stats can be viewed.\r
120  *\r
121  * Idle Hook - The idle hook is a function that is called on each iteration of\r
122  * the idle task.  In this case it is used to place the processor into a low\r
123  * power mode.  Note however that this application is implemented using standard\r
124  * components, and is therefore not optimised for low power operation.  Lower\r
125  * power consumption would be achieved by converting polling tasks into event\r
126  * driven tasks, and slowing the tick interrupt frequency.\r
127  *\r
128  * "Check" function called from the tick hook - The tick hook is called during\r
129  * each tick interrupt.  It is called from an interrupt context so must execute\r
130  * quickly, not attempt to block, and not call any FreeRTOS API functions that\r
131  * do not end in "FromISR".  In this case the tick hook executes a 'check'\r
132  * function.  This only executes every five seconds.  Its main function is to\r
133  * check that all the standard demo tasks are still operational.  Each time it\r
134  * executes it sends a status code to the LCD task.  The LCD task interprets the\r
135  * code and displays an appropriate message - which will be PASS if no tasks\r
136  * have reported any errors, or a message stating which task has reported an\r
137  * error.\r
138 */\r
139 \r
140 /* Standard includes. */\r
141 #include <stdio.h>\r
142 \r
143 /* Kernel includes. */\r
144 #include "FreeRTOS.h"\r
145 #include "task.h"\r
146 #include "queue.h"\r
147 \r
148 /* Demo application includes. */\r
149 #include "partest.h"\r
150 #include "flash.h"\r
151 #include "dynamic.h"\r
152 #include "comtest2.h"\r
153 #include "GenQTest.h"\r
154 \r
155 /* Eval board includes. */\r
156 #include "stm32_eval.h"\r
157 #include "stm32l152_eval_lcd.h"\r
158 \r
159 /* The priorities assigned to the tasks. */\r
160 #define mainFLASH_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 1 )\r
161 #define mainLCD_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
162 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 2 )\r
163 #define mainGENERIC_QUEUE_TEST_PRIORITY ( tskIDLE_PRIORITY )\r
164 \r
165 /* The length of the queue (the number of items the queue can hold) that is used\r
166 to send messages from tasks and interrupts the the LCD task. */\r
167 #define mainQUEUE_LENGTH                                ( 5 )\r
168 \r
169 /* Codes sent within messages to the LCD task so the LCD task can interpret\r
170 exactly what the message it just received was.  These are sent in the\r
171 cMessageID member of the message structure (defined below). */\r
172 #define mainMESSAGE_BUTTON_UP                   ( 1 )\r
173 #define mainMESSAGE_BUTTON_SEL                  ( 2 )\r
174 #define mainMESSAGE_STATUS                              ( 3 )\r
175 \r
176 /* When the cMessageID member of the message sent to the LCD task is\r
177 mainMESSAGE_STATUS then these definitions are sent in the lMessageValue member\r
178 of the same message and indicate what the status actually is. */\r
179 #define mainERROR_DYNAMIC_TASKS                 ( pdPASS + 1 )\r
180 #define mainERROR_COM_TEST                              ( pdPASS + 2 )\r
181 #define mainERROR_GEN_QUEUE_TEST                ( pdPASS + 3 )\r
182 \r
183 /* Baud rate used by the comtest tasks. */\r
184 #define mainCOM_TEST_BAUD_RATE                  ( 115200 )\r
185 \r
186 /* The LED used by the comtest tasks. See the comtest.c file for more\r
187 information. */\r
188 #define mainCOM_TEST_LED                                ( 3 )\r
189 \r
190 /* The LCD task uses printf() so requires more stack than most of the other\r
191 tasks. */\r
192 #define mainLCD_TASK_STACK_SIZE                 ( configMINIMAL_STACK_SIZE * 2 )\r
193 \r
194 /*-----------------------------------------------------------*/\r
195 \r
196 /*\r
197  * System configuration is performed prior to main() being called, this function\r
198  * configures the peripherals used by the demo application.\r
199  */\r
200 static void prvSetupHardware( void );\r
201 \r
202 /*\r
203  * Definition of the LCD/controller task described in the comments at the top\r
204  * of this file.\r
205  */\r
206 static void prvLCDTask( void *pvParameters );\r
207 \r
208 /*\r
209  * Definition of the button poll task described in the comments at the top of\r
210  * this file.\r
211  */\r
212 static void prvButtonPollTask( void *pvParameters );\r
213 \r
214 /*\r
215  * Converts a status message value into an appropriate string for display on\r
216  * the LCD.  The string is written to pcBuffer.\r
217  */\r
218 static void prvGenerateStatusMessage( char *pcBuffer, long lStatusValue );\r
219 \r
220 /*-----------------------------------------------------------*/\r
221 \r
222 /* The time base for the run time stats is generated by the 16 bit timer 6.\r
223 Each time the timer overflows ulTIM6_OverflowCount is incremented.  Therefore,\r
224 when converting the total run time to a 32 bit number, the most significant two\r
225 bytes are given by ulTIM6_OverflowCount and the least significant two bytes are\r
226 given by the current TIM6 counter value.  Care must be taken with data\r
227 consistency when combining the two in case a timer overflow occurs as the\r
228 value is being read. */\r
229 unsigned long ulTIM6_OverflowCount = 0UL;\r
230 \r
231 /* The handle of the queue used to send messages from tasks and interrupts to\r
232 the LCD task. */\r
233 static xQueueHandle xLCDQueue = NULL;\r
234 \r
235 /* The definition of each message sent from tasks and interrupts to the LCD\r
236 task. */\r
237 typedef struct\r
238 {\r
239         char cMessageID;        /* << States what the message is. */\r
240         long lMessageValue; /* << States the message value (can be an integer, string pointer, etc. depending on the value of cMessageID). */\r
241 } xQueueMessage;\r
242 \r
243 /*-----------------------------------------------------------*/\r
244 \r
245 void main( void )\r
246 {\r
247         /* Configure the peripherals used by this demo application.  This includes\r
248         configuring the joystick input select button to generate interrupts. */\r
249         prvSetupHardware();\r
250         \r
251         /* Create the queue used by tasks and interrupts to send strings to the LCD\r
252         task. */\r
253         xLCDQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( xQueueMessage ) );\r
254         \r
255         /* If the queue could not be created then don't create any tasks that might\r
256         attempt to use the queue. */\r
257         if( xLCDQueue != NULL )\r
258         {\r
259                 /* Add the created queue to the queue registry so it can be viewed in\r
260                 the IAR FreeRTOS state viewer plug-in. */\r
261                 vQueueAddToRegistry( xLCDQueue, "LCDQueue" );\r
262                 \r
263                 /* Create the LCD and button poll tasks, as described at the top of this\r
264                 file. */\r
265                 xTaskCreate( prvLCDTask, ( signed char * ) "LCD", mainLCD_TASK_STACK_SIZE, NULL, mainLCD_TASK_PRIORITY, NULL );\r
266                 xTaskCreate( prvButtonPollTask, ( signed char * ) "ButPoll", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
267                 \r
268                 /* Create a subset of the standard demo tasks. */\r
269                 vStartDynamicPriorityTasks();\r
270                 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
271                 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
272                 vStartGenericQueueTasks( mainGENERIC_QUEUE_TEST_PRIORITY );\r
273                 \r
274                 /* Start the scheduler. */\r
275                 vTaskStartScheduler();\r
276         }\r
277         \r
278         /* If all is well then this line will never be reached.  If it is reached\r
279         then it is likely that there was insufficient (FreeRTOS) heap memory space\r
280         to create the idle task.  This may have been trapped by the malloc() failed\r
281         hook function, if one is configured. */\r
282         for( ;; );\r
283 }\r
284 /*-----------------------------------------------------------*/\r
285 \r
286 static void prvLCDTask( void *pvParameters )\r
287 {\r
288 xQueueMessage xReceivedMessage;\r
289 long lLine = Line1;\r
290 const long lFontHeight = (((sFONT *)LCD_GetFont())->Height);\r
291 \r
292 /* Buffer into which strings are formatted and placed ready for display on the\r
293 LCD.  Note this is a static variable to prevent it being allocated on the task\r
294 stack, which is too small to hold such a variable.  The stack size is configured\r
295 when the task is created. */\r
296 static char cBuffer[ 512 ];\r
297 \r
298         /* This function is the only function that uses printf().  If printf() is\r
299         used from any other function then some sort of mutual exclusion on stdout\r
300         will be necessary.\r
301         \r
302         This is also the only function that is permitted to access the LCD.\r
303         \r
304         First print out the number of bytes that remain in the FreeRTOS heap.  This\r
305         can be viewed in the terminal IO window within the IAR Embedded Workbench. */\r
306         printf( "%d bytes of heap space remain unallocated\n", xPortGetFreeHeapSize() );\r
307 \r
308         for( ;; )\r
309         {\r
310                 /* Wait for a message to be received.  Using portMAX_DELAY as the block\r
311                 time will result in an indefinite wait provided INCLUDE_vTaskSuspend is\r
312                 set to 1 in FreeRTOSConfig.h, therefore there is no need to check the\r
313                 function return value and the function will only return when a value\r
314                 has been received. */\r
315                 xQueueReceive( xLCDQueue, &xReceivedMessage, portMAX_DELAY );\r
316 \r
317                 /* Clear the LCD if no room remains for any more text output. */\r
318                 if( lLine > Line9 )\r
319                 {\r
320                         LCD_Clear( Blue );\r
321                         lLine = 0;\r
322                 }\r
323                                 \r
324                 /* What is this message?  What does it contain? */\r
325                 switch( xReceivedMessage.cMessageID )\r
326                 {\r
327                         case mainMESSAGE_BUTTON_UP              :       /* The button poll task has just\r
328                                                                                                 informed this task that the up\r
329                                                                                                 button on the joystick input has\r
330                                                                                                 been pressed or released. */\r
331                                                                                                 sprintf( cBuffer, "Button up = %d", xReceivedMessage.lMessageValue );\r
332                                                                                                 break;\r
333 \r
334                         case mainMESSAGE_BUTTON_SEL             :       /* The select button interrupt\r
335                                                                                                 just informed this task that the\r
336                                                                                                 select button was pressed.\r
337                                                                                                 Generate a table of task run time\r
338                                                                                                 statistics and output this to\r
339                                                                                                 the terminal IO window in the IAR\r
340                                                                                                 embedded workbench. */\r
341                                                                                                 printf( "\nTask\t     Abs Time\t     %%Time\n*****************************************" );\r
342                                                                                                 vTaskGetRunTimeStats( ( signed char * ) cBuffer );\r
343                                                                                                 printf( cBuffer );\r
344                                                                                                 \r
345                                                                                                 /* Also print out a message to\r
346                                                                                                 the LCD - in this case the\r
347                                                                                                 pointer to the string to print\r
348                                                                                                 is sent directly in the\r
349                                                                                                 lMessageValue member of the\r
350                                                                                                 message.  This just demonstrates\r
351                                                                                                 a different communication\r
352                                                                                                 technique. */\r
353                                                                                                 sprintf( cBuffer, "%s", ( char * ) xReceivedMessage.lMessageValue );\r
354                                                                                                 break;\r
355                                                                                                 \r
356                         case mainMESSAGE_STATUS                 :       /* The tick interrupt hook\r
357                                                                                                 function has just informed this\r
358                                                                                                 task of the system status.\r
359                                                                                                 Generate a string in accordance\r
360                                                                                                 with the status value. */\r
361                                                                                                 prvGenerateStatusMessage( cBuffer, xReceivedMessage.lMessageValue );\r
362                                                                                                 break;\r
363                                                                                                 \r
364                         default                                                 :       sprintf( cBuffer, "Unknown message" );\r
365                                                                                                 break;\r
366                 }\r
367                 \r
368                 /* Output the message that was placed into the cBuffer array within the\r
369                 switch statement above. */\r
370                 LCD_DisplayStringLine( lLine, ( uint8_t * ) cBuffer );\r
371                 \r
372                 /* Move onto the next LCD line, ready for the next iteration of this\r
373                 loop. */\r
374                 lLine += lFontHeight;\r
375         }\r
376 }\r
377 /*-----------------------------------------------------------*/\r
378 \r
379 static void prvGenerateStatusMessage( char *pcBuffer, long lStatusValue )\r
380 {\r
381         /* Just a utility function to convert a status value into a meaningful\r
382         string for output onto the LCD. */\r
383         switch( lStatusValue )\r
384         {\r
385                 case pdPASS                                             :       sprintf( pcBuffer, "Task status = PASS" );\r
386                                                                                         break;\r
387                 case mainERROR_DYNAMIC_TASKS    :       sprintf( pcBuffer, "Error: Dynamic tasks" );\r
388                                                                                         break;\r
389                 case mainERROR_COM_TEST                 :       sprintf( pcBuffer, "Err: loop connected?" ); /* Error in COM test - is the Loopback connector connected? */                                                                                                             \r
390                                                                                         break;\r
391                 case mainERROR_GEN_QUEUE_TEST   :       sprintf( pcBuffer, "Error: Gen Q test" );\r
392                                                                                         break;\r
393                 default                                                 :       sprintf( pcBuffer, "Unknown status" );\r
394                                                                                         break;\r
395         }\r
396 }\r
397 /*-----------------------------------------------------------*/\r
398 \r
399 void EXTI9_5_IRQHandler( void )\r
400 {\r
401 /* Define the message sent to the LCD task from this interrupt. */\r
402 const xQueueMessage xMessage = { mainMESSAGE_BUTTON_SEL, ( unsigned long ) "Select Interrupt!" };\r
403 long lHigherPriorityTaskWoken = pdFALSE;\r
404 \r
405         /* This is the interrupt handler for the joystick select button input.\r
406         The button has been pushed, write a message to the LCD via the LCD task. */\r
407         xQueueSendFromISR( xLCDQueue, &xMessage, &lHigherPriorityTaskWoken );\r
408         \r
409         EXTI_ClearITPendingBit( SEL_BUTTON_EXTI_LINE );\r
410         \r
411         /* If writing to xLCDQueue caused a task to unblock, and the unblocked task\r
412         has a priority equal to or above the task that this interrupt interrupted,\r
413         then lHigherPriorityTaskWoken will have been set to pdTRUE internally within\r
414         xQueuesendFromISR(), and portEND_SWITCHING_ISR() will ensure that this\r
415         interrupt returns directly to the higher priority unblocked task. */\r
416         portEND_SWITCHING_ISR( lHigherPriorityTaskWoken );\r
417 }\r
418 /*-----------------------------------------------------------*/\r
419 \r
420 void vApplicationTickHook( void )\r
421 {\r
422 static unsigned long ulCounter = 0;\r
423 static const unsigned long ulCheckFrequency = 5000UL / portTICK_RATE_MS;\r
424 long lHigherPriorityTaskWoken = pdFALSE;\r
425 \r
426 /* Define the status message that is sent to the LCD task.  By default the\r
427 status is PASS. */\r
428 static xQueueMessage xStatusMessage = { mainMESSAGE_STATUS, pdPASS };\r
429 \r
430         /* This is called from within the tick interrupt and performs the 'check'\r
431         functionality as described in the comments at the top of this file.\r
432 \r
433         Is it time to perform the 'check' functionality again? */\r
434         ulCounter++;\r
435         if( ulCounter >= ulCheckFrequency )\r
436         {\r
437                 /* See if the standard demo tasks are executing as expected, changing\r
438                 the message that is sent to the LCD task from PASS to an error code if\r
439                 any tasks set reports an error. */\r
440                 if( xAreDynamicPriorityTasksStillRunning() != pdPASS )\r
441                 {\r
442                         xStatusMessage.lMessageValue = mainERROR_DYNAMIC_TASKS;\r
443                 }\r
444                 \r
445                 if( xAreComTestTasksStillRunning() != pdPASS )\r
446                 {\r
447                         xStatusMessage.lMessageValue = mainERROR_COM_TEST;\r
448                 }\r
449                 \r
450                 if( xAreGenericQueueTasksStillRunning() != pdPASS )\r
451                 {\r
452                         xStatusMessage.lMessageValue = mainERROR_GEN_QUEUE_TEST;\r
453                 }\r
454                 \r
455                 /* As this is the tick hook the lHigherPriorityTaskWoken parameter is not\r
456                 needed (a context switch is going to be performed anyway), but it must\r
457                 still be provided. */\r
458                 xQueueSendFromISR( xLCDQueue, &xStatusMessage, &lHigherPriorityTaskWoken );\r
459                 ulCounter = 0;\r
460         }\r
461 }\r
462 /*-----------------------------------------------------------*/\r
463 \r
464 static void prvButtonPollTask( void *pvParameters )\r
465 {\r
466 long lLastState = pdTRUE;\r
467 long lState;\r
468 xQueueMessage xMessage;\r
469 \r
470         /* This tasks performs the button polling functionality as described at the\r
471         top of this file. */\r
472         for( ;; )\r
473         {\r
474                 /* Check the button state. */\r
475                 lState = STM_EVAL_PBGetState( BUTTON_UP );\r
476                 if( lState != lLastState )\r
477                 {\r
478                         /* The state has changed, send a message to the LCD task. */\r
479                         xMessage.cMessageID = mainMESSAGE_BUTTON_UP;\r
480                         xMessage.lMessageValue = lState;\r
481                         lLastState = lState;\r
482                         xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );\r
483                 }\r
484                 \r
485                 /* Block for 10 milliseconds so this task does not utilise all the CPU\r
486                 time and debouncing of the button is not necessary. */\r
487                 vTaskDelay( 10 / portTICK_RATE_MS );\r
488         }\r
489 }\r
490 /*-----------------------------------------------------------*/\r
491 \r
492 static void prvSetupHardware( void )\r
493 {\r
494         /* Ensure that all 4 interrupt priority bits are used as the pre-emption\r
495         priority. */\r
496         NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );\r
497         \r
498         /* Initialise the LEDs. */\r
499         vParTestInitialise();\r
500 \r
501         /* Initialise the joystick inputs. */\r
502         STM_EVAL_PBInit( BUTTON_UP, BUTTON_MODE_GPIO );\r
503         STM_EVAL_PBInit( BUTTON_DOWN, BUTTON_MODE_GPIO );\r
504         STM_EVAL_PBInit( BUTTON_LEFT, BUTTON_MODE_GPIO );\r
505         STM_EVAL_PBInit( BUTTON_RIGHT, BUTTON_MODE_GPIO );\r
506         \r
507         /* The select button in the middle of the joystick is configured to generate\r
508         an interrupt.  The Eval board library will configure the interrupt\r
509         priority to be the lowest priority available so the priority need not be\r
510         set here explicitly.  It is important that the priority is equal to or\r
511         below that set by the configMAX_SYSCALL_INTERRUPT_PRIORITY value set in\r
512         FreeRTOSConfig.h. */\r
513         STM_EVAL_PBInit( BUTTON_SEL, BUTTON_MODE_EXTI );\r
514 \r
515         /* Initialize the LCD */\r
516         STM32L152_LCD_Init();   \r
517         LCD_Clear( Blue );\r
518         LCD_SetBackColor( Blue );\r
519         LCD_SetTextColor( White );\r
520         LCD_DisplayStringLine( Line0, "  www.FreeRTOS.org" );\r
521 }\r
522 /*-----------------------------------------------------------*/\r
523 \r
524 void vConfigureTimerForRunTimeStats( void )\r
525 {\r
526 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;\r
527 NVIC_InitTypeDef NVIC_InitStructure;\r
528 \r
529         /* The time base for the run time stats is generated by the 16 bit timer 6.\r
530         Each time the timer overflows ulTIM6_OverflowCount is incremented.\r
531         Therefore, when converting the total run time to a 32 bit number, the most\r
532         significant two bytes are given by ulTIM6_OverflowCount and the least\r
533         significant two bytes are given by the current TIM6 counter value.  Care\r
534         must be taken with data consistency when combining the two in case a timer\r
535         overflow occurs as the value is being read.\r
536         \r
537         The portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() macro (in FreeRTOSConfig.h) is\r
538         defined to call this function, so the kernel will call this function\r
539         automatically at the appropriate time. */\r
540 \r
541         /* TIM6 clock enable */\r
542         RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM6, ENABLE );\r
543 \r
544         /* The 32MHz clock divided by 5000 should tick (very) approximately every\r
545         150uS and overflow a 16bit timer (very) approximately every 10 seconds. */\r
546         TIM_TimeBaseStructure.TIM_Period = 65535;\r
547         TIM_TimeBaseStructure.TIM_Prescaler = 5000;\r
548         TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;\r
549         TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;\r
550         \r
551         TIM_TimeBaseInit( TIM6, &TIM_TimeBaseStructure );\r
552         \r
553         /* Only interrupt on overflow events. */\r
554         TIM6->CR1 |= TIM_CR1_URS;\r
555         \r
556         /* Enable the interrupt. */\r
557         TIM_ITConfig( TIM6, TIM_IT_Update, ENABLE );\r
558         \r
559         /* Enable the TIM6 global Interrupt */\r
560         NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;\r
561         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_LOWEST_INTERRUPT_PRIORITY;\r
562         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; /* Not used as 4 bits are used for the pre-emption priority. */\r
563         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;\r
564         NVIC_Init(&NVIC_InitStructure);\r
565         \r
566         TIM_ClearITPendingBit( TIM6, TIM_IT_Update );\r
567         TIM_Cmd( TIM6, ENABLE );\r
568 }\r
569 /*-----------------------------------------------------------*/\r
570 \r
571 void TIM6_IRQHandler( void )\r
572 {\r
573         /* Interrupt handler for TIM 6\r
574         \r
575         The time base for the run time stats is generated by the 16 bit timer 6.\r
576         Each time the timer overflows ulTIM6_OverflowCount is incremented.\r
577         Therefore, when converting the total run time to a 32 bit number, the most\r
578         significant two bytes are given by ulTIM6_OverflowCount and the least\r
579         significant two bytes are given by the current TIM6 counter value.  Care\r
580         must be taken with data consistency when combining the two in case a timer\r
581         overflow occurs as the value is being read. */\r
582         if( TIM_GetITStatus( TIM6, TIM_IT_Update) != RESET)\r
583         {\r
584                 ulTIM6_OverflowCount++;\r
585                 TIM_ClearITPendingBit( TIM6, TIM_IT_Update );\r
586         }\r
587 }\r
588 /*-----------------------------------------------------------*/\r
589 \r
590 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )\r
591 {\r
592         ( void ) pcTaskName;\r
593         ( void ) pxTask;\r
594         \r
595         /* Run time stack overflow checking is performed if\r
596         configconfigCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
597         function is called if a stack overflow is detected. */\r
598         for( ;; );\r
599 }\r
600 /*-----------------------------------------------------------*/\r
601 \r
602 void vApplicationMallocFailedHook( void )\r
603 {\r
604         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
605         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
606         internally by FreeRTOS API functions that create tasks, queues or\r
607         semaphores. */\r
608         for( ;; );\r
609 }\r
610 /*-----------------------------------------------------------*/\r
611 \r
612 void vApplicationIdleHook( void )\r
613 {\r
614         /* Called on each iteration of the idle task.  In this case the idle task\r
615         just enters a low(ish) power mode. */\r
616         PWR_EnterSleepMode( PWR_Regulator_ON, PWR_SLEEPEntry_WFI );\r
617 }\r
618 \r
619 \r
620 \r