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