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