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