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