]> git.sur5r.net Git - freertos/blob - Demo/Cortex_STM32L152_IAR/main.c
Complete the STM32L152 demo application functionality - it is still to be 'production...
[freertos] / Demo / Cortex_STM32L152_IAR / main.c
1 /*\r
2     FreeRTOS V6.1.0 - Copyright (C) 2010 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS books - available as PDF or paperback  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /* Standard includes. */\r
55 #include <stdio.h>\r
56 \r
57 /* Kernel includes. */\r
58 #include "FreeRTOS.h"\r
59 #include "task.h"\r
60 #include "queue.h"\r
61 \r
62 /* Demo application includes. */\r
63 #include "partest.h"\r
64 #include "flash.h"\r
65 #include "dynamic.h"\r
66 #include "comtest2.h"\r
67 #include "GenQTest.h"\r
68 \r
69 /* ST driver includes. */\r
70 #include "stm32l1xx_usart.h"\r
71 \r
72 /* Eval board includes. */\r
73 #include "stm32_eval.h"\r
74 #include "stm32l152_eval_lcd.h"\r
75 \r
76 #define mainFLASH_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 1 )\r
77 #define mainLCD_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
78 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 2 )\r
79 #define mainGENERIC_QUEUE_TEST_PRIORITY ( tskIDLE_PRIORITY )\r
80 \r
81 #define mainLCD_TASK_STACK_SIZE                 ( configMINIMAL_STACK_SIZE * 2 )\r
82 \r
83 #define mainQUEUE_LENGTH                                ( 5 )\r
84 \r
85 #define mainMESSAGE_BUTTON_UP                   ( 1 )\r
86 #define mainMESSAGE_BUTTON_DOWN                 ( 2 )\r
87 #define mainMESSAGE_BUTTON_LEFT                 ( 3 )\r
88 #define mainMESSAGE_BUTTON_RIGHT                ( 4 )\r
89 #define mainMESSAGE_BUTTON_SEL                  ( 5 )\r
90 #define mainMESSAGE_STATUS                              ( 6 )\r
91 \r
92 #define mainERROR_DYNAMIC_TASKS                 ( 2 )\r
93 #define mainERROR_COM_TEST                              ( 3 )\r
94 #define mainERROR_GEN_QUEUE_TEST                ( 4 )\r
95 \r
96 /* Baud rate used by the comtest tasks. */\r
97 #define mainCOM_TEST_BAUD_RATE          ( 9600 )\r
98 \r
99 /* The LED used by the comtest tasks. See the comtest.c file for more\r
100 information. */\r
101 #define mainCOM_TEST_LED                        ( 3 )\r
102 \r
103 \r
104 /*\r
105  * System configuration is performed prior to main() being called, this function\r
106  * configures the peripherals used by the demo application.\r
107  */\r
108 static void prvSetupHardware( void );\r
109 static void prvLCDTask( void *pvParameters );\r
110 static void vTempTask( void *pv );\r
111 static void prvGenerateStatusMessage( char *pcBuffer, long lStatusValue );\r
112 \r
113 unsigned long ulTIM6_OverflowCount = 0UL;\r
114 \r
115 static xQueueHandle xLCDQueue = NULL;\r
116 \r
117 typedef struct\r
118 {\r
119         char cMessageID;\r
120         long lMessageValue;\r
121 } xQueueMessage;\r
122 \r
123 void main( void )\r
124 {\r
125         prvSetupHardware();\r
126         \r
127         /* Create the queue used by tasks and interrupts to send strings to the LCD\r
128         task. */\r
129         xLCDQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( xQueueMessage ) );\r
130         \r
131         if( xLCDQueue != NULL )\r
132         {\r
133                 vQueueAddToRegistry( xLCDQueue, "LCDQueue" );\r
134                 xTaskCreate( prvLCDTask, ( signed char * ) "LCD", mainLCD_TASK_STACK_SIZE, NULL, mainLCD_TASK_PRIORITY, NULL );\r
135                 xTaskCreate( vTempTask, ( signed char * ) "Temp", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
136                 vStartDynamicPriorityTasks();\r
137                 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
138                 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
139                 vStartGenericQueueTasks( mainGENERIC_QUEUE_TEST_PRIORITY );\r
140                 \r
141                 vTaskStartScheduler();\r
142         }\r
143         \r
144         for( ;; );\r
145 }\r
146 /*-----------------------------------------------------------*/\r
147 \r
148 static void prvLCDTask( void *pvParameters )\r
149 {\r
150 xQueueMessage xReceivedMessage;\r
151 long lLine = Line1;\r
152 const long lFontHeight = (((sFONT *)LCD_GetFont())->Height);\r
153 static char cBuffer[ 512 ];\r
154 \r
155         /* This function is the only function that uses printf().  If printf() is\r
156         used from any other function then some sort of mutual exclusion on stdout\r
157         will be necessary. */\r
158 \r
159         printf( "%d bytes of heap space remain unallocated\n", xPortGetFreeHeapSize() );\r
160 \r
161         for( ;; )\r
162         {\r
163                 xQueueReceive( xLCDQueue, &xReceivedMessage, portMAX_DELAY );\r
164 \r
165                 if( lLine > Line9 )\r
166                 {\r
167                         LCD_Clear( Blue );\r
168                         lLine = 0;\r
169                 }\r
170                                 \r
171                 switch( xReceivedMessage.cMessageID )\r
172                 {\r
173                         case mainMESSAGE_BUTTON_UP              :       sprintf( cBuffer, "Button up = %d", xReceivedMessage.lMessageValue );\r
174                                                                                                 break;\r
175                         case mainMESSAGE_BUTTON_DOWN    :       sprintf( cBuffer, "Button down = %d", xReceivedMessage.lMessageValue );\r
176                                                                                                 break;\r
177                         case mainMESSAGE_BUTTON_LEFT    :       sprintf( cBuffer, "Button left = %d", xReceivedMessage.lMessageValue );\r
178                                                                                                 break;\r
179                         case mainMESSAGE_BUTTON_RIGHT   :       sprintf( cBuffer, "Button right = %d", xReceivedMessage.lMessageValue );\r
180                                                                                                 break;\r
181                         case mainMESSAGE_BUTTON_SEL             :       printf( "\nTask\t     Abs Time\t     %%Time\n*****************************************" );\r
182                                                                                                 vTaskGetRunTimeStats( ( signed char * ) cBuffer );\r
183                                                                                                 printf( cBuffer );\r
184                                                                                                 \r
185                                                                                                 /* The select button passes its\r
186                                                                                                 own string to print out. */\r
187                                                                                                 sprintf( cBuffer, "%s", ( char * ) xReceivedMessage.lMessageValue );\r
188                                                                                                 break;\r
189                         case mainMESSAGE_STATUS                 :       prvGenerateStatusMessage( cBuffer, xReceivedMessage.lMessageValue );\r
190                                                                                                 break;\r
191                         default                                                 :       sprintf( cBuffer, "Unknown message" );\r
192                                                                                                 break;\r
193                 }\r
194                 \r
195                 LCD_DisplayStringLine( lLine, ( uint8_t * ) cBuffer );\r
196                 lLine += lFontHeight;\r
197         }\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 static void prvGenerateStatusMessage( char *pcBuffer, long lStatusValue )\r
202 {\r
203         switch( lStatusValue )\r
204         {\r
205                 case pdPASS                                             :       sprintf( pcBuffer, "Task status = PASS" );\r
206                                                                                         break;\r
207                 case mainERROR_DYNAMIC_TASKS    :       sprintf( pcBuffer, "Error: Dynamic tasks" );\r
208                                                                                         break;\r
209                 case mainERROR_COM_TEST                 :       sprintf( pcBuffer, "Error: COM test" );\r
210                                                                                         break;\r
211                 case mainERROR_GEN_QUEUE_TEST   :       sprintf( pcBuffer, "Error: Gen Q test" );\r
212                                                                                         break;\r
213                 default                                                 :       sprintf( pcBuffer, "Unknown status" );\r
214                                                                                         break;\r
215         }\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 void EXTI9_5_IRQHandler( void )\r
220 {\r
221 const xQueueMessage xMessage = { mainMESSAGE_BUTTON_SEL, ( unsigned long ) "Select Interrupt!" };\r
222 long lHigherPriorityTaskWoken = pdFALSE;\r
223 \r
224         xQueueSendFromISR( xLCDQueue, &xMessage, &lHigherPriorityTaskWoken );\r
225         EXTI_ClearITPendingBit( SEL_BUTTON_EXTI_LINE );\r
226         portEND_SWITCHING_ISR( lHigherPriorityTaskWoken );\r
227 }\r
228 /*-----------------------------------------------------------*/\r
229 \r
230 void vApplicationTickHook( void )\r
231 {\r
232 static unsigned long ulCounter = 0;\r
233 static const unsigned long ulCheckFrequency = 5000UL / portTICK_RATE_MS;\r
234 static xQueueMessage xStatusMessage = { mainMESSAGE_STATUS, pdPASS };\r
235 long lHigherPriorityTaskWoken = pdFALSE; /* Not used in this case as this is the tick hook. */\r
236 \r
237         ulCounter++;\r
238         if( ulCounter >= ulCheckFrequency )\r
239         {\r
240                 if( xAreDynamicPriorityTasksStillRunning() != pdPASS )\r
241                 {\r
242                         xStatusMessage.lMessageValue = mainERROR_DYNAMIC_TASKS;\r
243                 }\r
244                 \r
245                 if( xAreComTestTasksStillRunning() != pdPASS )\r
246                 {\r
247                         xStatusMessage.lMessageValue = mainERROR_COM_TEST;\r
248                 }\r
249                 \r
250                 if( xAreGenericQueueTasksStillRunning() != pdPASS )\r
251                 {\r
252                         xStatusMessage.lMessageValue = mainERROR_GEN_QUEUE_TEST;\r
253                 }\r
254                 \r
255                 xQueueSendFromISR( xLCDQueue, &xStatusMessage, &lHigherPriorityTaskWoken );\r
256                 ulCounter = 0;\r
257         }\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 static void vTempTask( void *pv )\r
262 {\r
263 long lLastState = pdTRUE;\r
264 long lState;\r
265 xQueueMessage xMessage;\r
266 \r
267         for( ;; )\r
268         {\r
269                 lState = STM_EVAL_PBGetState( BUTTON_UP );\r
270                 if( lState != lLastState )\r
271                 {\r
272                         xMessage.cMessageID = mainMESSAGE_BUTTON_UP;\r
273                         xMessage.lMessageValue = lState;\r
274                         lLastState = lState;\r
275                         xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );\r
276                         vTaskDelay( 10 );\r
277                 }\r
278         }\r
279 }\r
280 /*-----------------------------------------------------------*/\r
281 \r
282 static void prvSetupHardware( void )\r
283 {\r
284         NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );\r
285         \r
286         /* Initialise the LEDs. */\r
287         vParTestInitialise();\r
288 \r
289         /* Initialise the joystick inputs. */\r
290         STM_EVAL_PBInit( BUTTON_UP, BUTTON_MODE_GPIO );\r
291         STM_EVAL_PBInit( BUTTON_DOWN, BUTTON_MODE_GPIO );\r
292         STM_EVAL_PBInit( BUTTON_LEFT, BUTTON_MODE_GPIO );\r
293         STM_EVAL_PBInit( BUTTON_RIGHT, BUTTON_MODE_GPIO );\r
294         \r
295         /* The select button in the middle of the joystick is configured to generate\r
296         an interrupt.  The Eval board library will configure the interrupt\r
297         priority to be the lowest priority available - this is important as the\r
298         interrupt service routine makes use of a FreeRTOS API function so must\r
299         therefore use a priority equal to or below that set by the\r
300         configMAX_SYSCALL_INTERRUPT_PRIORITY() value set in FreeRTOSConfig.h. */\r
301         STM_EVAL_PBInit( BUTTON_SEL, BUTTON_MODE_EXTI );\r
302 \r
303         /* Initialize the LCD */\r
304         STM32L152_LCD_Init();\r
305         \r
306         LCD_Clear(Blue);\r
307         LCD_SetBackColor(Blue);\r
308         LCD_SetTextColor(White);\r
309         LCD_DisplayStringLine(Line0, "  www.FreeRTOS.org");\r
310 }\r
311 /*-----------------------------------------------------------*/\r
312 \r
313 void vConfigureTimerForRunTimeStats( void )\r
314 {\r
315 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;\r
316 NVIC_InitTypeDef NVIC_InitStructure;\r
317 \r
318         /* TIM6 clock enable */\r
319         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);\r
320 \r
321         /* The 32MHz clock divided by 5000 should tick (very) approximately every\r
322         150uS and overflow a 16bit timer (very) approximately every 10 seconds. */\r
323         TIM_TimeBaseStructure.TIM_Period = 65535;\r
324         TIM_TimeBaseStructure.TIM_Prescaler = 5000;\r
325         TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;\r
326         TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;\r
327         \r
328         TIM_TimeBaseInit( TIM6, &TIM_TimeBaseStructure );\r
329         \r
330         /* Only interrupt on overflow events. */\r
331         TIM6->CR1 |= TIM_CR1_URS;\r
332         \r
333         TIM_ITConfig( TIM6, TIM_IT_Update, ENABLE );\r
334         \r
335         /* Enable the TIM6 gloabal Interrupt */\r
336         NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;\r
337         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_LOWEST_INTERRUPT_PRIORITY;\r
338         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; /* Not used as 4 bits are used for the pre-emption priority. */\r
339         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;\r
340         \r
341         TIM_ClearITPendingBit( TIM6, TIM_IT_Update );\r
342         NVIC_Init(&NVIC_InitStructure);\r
343         TIM_Cmd( TIM6, ENABLE );\r
344 }\r
345 /*-----------------------------------------------------------*/\r
346 \r
347 void TIM6_IRQHandler( void )\r
348 {\r
349         if( TIM_GetITStatus( TIM6, TIM_IT_Update) != RESET)\r
350         {\r
351                 ulTIM6_OverflowCount++;\r
352                 TIM_ClearITPendingBit( TIM6, TIM_IT_Update );\r
353         }\r
354 }\r
355 /*-----------------------------------------------------------*/\r
356 \r
357 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )\r
358 {\r
359         ( void ) pcTaskName;\r
360         ( void ) pxTask;\r
361         \r
362         for( ;; );\r
363 }\r
364 /*-----------------------------------------------------------*/\r
365 \r
366 void vApplicationMallocFailedHook( void )\r
367 {\r
368         for( ;; );\r
369 }\r
370 \r
371 \r