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