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