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