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