]> git.sur5r.net Git - freertos/blob - Demo/Cortex_STM32L152_IAR/main.c
Still updating the STM32L demo project - still a work in progress.
[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 \r
67 /* ST driver includes. */\r
68 #include "stm32l1xx_usart.h"\r
69 \r
70 /* Eval board includes. */\r
71 #include "stm32_eval.h"\r
72 #include "stm32l152_eval_lcd.h"\r
73 \r
74 #define mainFLASH_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 1 )\r
75 #define mainLCD_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
76 \r
77 #define mainLCD_TASK_STACK_SIZE                 ( configMINIMAL_STACK_SIZE * 2 )\r
78 \r
79 #define mainQUEUE_LENGTH                                ( 5 )\r
80 \r
81 #define mainMESSAGE_BUTTON_UP                   ( 1 )\r
82 #define mainMESSAGE_BUTTON_DOWN                 ( 2 )\r
83 #define mainMESSAGE_BUTTON_LEFT                 ( 3 )\r
84 #define mainMESSAGE_BUTTON_RIGHT                ( 4 )\r
85 #define mainMESSAGE_BUTTON_SEL                  ( 5 )\r
86 #define mainMESSAGE_STATUS                              ( 6 )\r
87 \r
88 /*\r
89  * System configuration is performed prior to main() being called, this function\r
90  * configures the peripherals used by the demo application.\r
91  */\r
92 static void prvSetupHardware( void );\r
93 static void prvLCDTask( void *pvParameters );\r
94 static void vTempTask( void *pv );\r
95 \r
96 unsigned long ulTIM6_OverflowCount = 0UL;\r
97 \r
98 static xQueueHandle xLCDQueue = NULL;\r
99 \r
100 typedef struct\r
101 {\r
102         char cMessageID;\r
103         long lMessageValue;\r
104 } xQueueMessage;\r
105 \r
106 void main( void )\r
107 {\r
108         prvSetupHardware();\r
109         \r
110         /* Create the queue used by tasks and interrupts to send strings to the LCD\r
111         task. */\r
112         xLCDQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( xQueueMessage ) );\r
113         \r
114         if( xLCDQueue != NULL )\r
115         {\r
116                 vQueueAddToRegistry( xLCDQueue, "LCDQueue" );\r
117                 xTaskCreate( prvLCDTask, ( signed char * ) "LCD", mainLCD_TASK_STACK_SIZE, NULL, mainLCD_TASK_PRIORITY, NULL );\r
118                 xTaskCreate( vTempTask, ( signed char * ) "Temp", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
119                 vStartDynamicPriorityTasks();\r
120                 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
121                 \r
122                 vTaskStartScheduler();\r
123         }\r
124         \r
125         for( ;; );\r
126 }\r
127 /*-----------------------------------------------------------*/\r
128 unsigned long ulTempArray[ 10 ], ulx = 0;\r
129 static void prvLCDTask( void *pvParameters )\r
130 {\r
131 xQueueMessage xReceivedMessage;\r
132 long lLine = Line1;\r
133 const long lFontHeight = (((sFONT *)LCD_GetFont())->Height);\r
134 static char cBuffer[ 256 ];\r
135 \r
136         for( ;; )\r
137         {\r
138                 xQueueReceive( xLCDQueue, &xReceivedMessage, portMAX_DELAY );\r
139 \r
140                 if( lLine > Line9 )\r
141                 {\r
142                         LCD_Clear( Blue );\r
143                         lLine = 0;\r
144                 }\r
145                                 \r
146                 switch( xReceivedMessage.cMessageID )\r
147                 {\r
148                         case mainMESSAGE_BUTTON_UP              :       sprintf( cBuffer, "Button up = %d", xReceivedMessage.lMessageValue );\r
149                                                                                                 break;\r
150                         case mainMESSAGE_BUTTON_DOWN    :       sprintf( cBuffer, "Button down = %d", xReceivedMessage.lMessageValue );\r
151                                                                                                 break;\r
152                         case mainMESSAGE_BUTTON_LEFT    :       sprintf( cBuffer, "Button left = %d", xReceivedMessage.lMessageValue );\r
153                                                                                                 break;\r
154                         case mainMESSAGE_BUTTON_RIGHT   :       sprintf( cBuffer, "Button right = %d", xReceivedMessage.lMessageValue );\r
155                                                                                                 break;\r
156                         case mainMESSAGE_BUTTON_SEL             :       printf( "\nTask\t     Abs Time\t     %%Time\n*****************************************\n" );\r
157                                                                                                 vTaskGetRunTimeStats( ( signed char * ) cBuffer );\r
158                                                                                                 printf( cBuffer );\r
159                                                                                                 \r
160                                                                                                 /* The select button passes its\r
161                                                                                                 own string to print out. */\r
162                                                                                                 sprintf( cBuffer, "%s", ( char * ) xReceivedMessage.lMessageValue );\r
163                                                                                                 break;\r
164                         case mainMESSAGE_STATUS                 :       sprintf( cBuffer, "Task status = %s", ( ( xReceivedMessage.lMessageValue ) ? "PASS" : "FAIL" ) );\r
165                                                                                                 break;\r
166                         default                                                 :       sprintf( cBuffer, "Unknown message" );\r
167                                                                                                 break;\r
168                 }\r
169                 \r
170                 LCD_DisplayStringLine( lLine, ( uint8_t * ) cBuffer );\r
171                 lLine += lFontHeight;\r
172         }\r
173 }\r
174 /*-----------------------------------------------------------*/\r
175 \r
176 void EXTI9_5_IRQHandler( void )\r
177 {\r
178 const xQueueMessage xMessage = { mainMESSAGE_BUTTON_SEL, ( unsigned long ) "Select Interrupt!" };\r
179 long lHigherPriorityTaskWoken = pdFALSE;\r
180 \r
181         xQueueSendFromISR( xLCDQueue, &xMessage, &lHigherPriorityTaskWoken );\r
182         EXTI_ClearITPendingBit( SEL_BUTTON_EXTI_LINE );\r
183         portEND_SWITCHING_ISR( lHigherPriorityTaskWoken );\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 void vApplicationTickHook( void )\r
188 {\r
189 static unsigned long ulCounter = 0;\r
190 static const unsigned long ulCheckFrequency = 5000UL / portTICK_RATE_MS;\r
191 static xQueueMessage xStatusMessage = { mainMESSAGE_STATUS, pdPASS };\r
192 long lHigherPriorityTaskWoken = pdFALSE; /* Not used in this case as this is the tick hook. */\r
193 \r
194         ulCounter++;\r
195         if( ulCounter >= ulCheckFrequency )\r
196         {\r
197                 if( xAreDynamicPriorityTasksStillRunning() != pdPASS )\r
198                 {\r
199                         xStatusMessage.lMessageValue = pdFAIL;\r
200                 }\r
201                 \r
202                 xQueueSendFromISR( xLCDQueue, &xStatusMessage, &lHigherPriorityTaskWoken );\r
203                 ulCounter = 0;\r
204         }\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 static void vTempTask( void *pv )\r
209 {\r
210 long lLastState = pdTRUE;\r
211 long lState;\r
212 xQueueMessage xMessage;\r
213 \r
214         for( ;; )\r
215         {\r
216                 lState = STM_EVAL_PBGetState( BUTTON_UP );\r
217                 if( lState != lLastState )\r
218                 {\r
219                         xMessage.cMessageID = mainMESSAGE_BUTTON_UP;\r
220                         xMessage.lMessageValue = lState;\r
221                         lLastState = lState;\r
222                         xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );\r
223                         vTaskDelay( 10 );\r
224                 }\r
225         }\r
226 }\r
227 /*-----------------------------------------------------------*/\r
228 \r
229 static void prvSetupHardware( void )\r
230 {\r
231         /* Initialise the LEDs. */\r
232         vParTestInitialise();\r
233 \r
234         /* Initialise the joystick inputs. */\r
235         STM_EVAL_PBInit( BUTTON_UP, BUTTON_MODE_GPIO );\r
236         STM_EVAL_PBInit( BUTTON_DOWN, BUTTON_MODE_GPIO );\r
237         STM_EVAL_PBInit( BUTTON_LEFT, BUTTON_MODE_GPIO );\r
238         STM_EVAL_PBInit( BUTTON_RIGHT, BUTTON_MODE_GPIO );\r
239         \r
240         /* The select button in the middle of the joystick is configured to generate\r
241         an interrupt.  The Eval board library will configure the interrupt\r
242         priority to be the lowest priority available - this is important as the\r
243         interrupt service routine makes use of a FreeRTOS API function so must\r
244         therefore use a priority equal to or below that set by the\r
245         configMAX_SYSCALL_INTERRUPT_PRIORITY() value set in FreeRTOSConfig.h. */\r
246         STM_EVAL_PBInit( BUTTON_SEL, BUTTON_MODE_EXTI );\r
247 \r
248 #if 0   \r
249 USART_InitTypeDef USART_InitStructure;\r
250 \r
251         USART_InitStructure.USART_BaudRate = 115200;\r
252         USART_InitStructure.USART_WordLength = USART_WordLength_8b;\r
253         USART_InitStructure.USART_StopBits = USART_StopBits_1;\r
254         USART_InitStructure.USART_Parity = USART_Parity_No;\r
255         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;\r
256         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;\r
257         \r
258         STM_EVAL_COMInit( COM1, &USART_InitStructure );\r
259 #endif\r
260         \r
261         /* Initialize the LCD */\r
262         STM32L152_LCD_Init();\r
263         \r
264         LCD_Clear(Blue);\r
265         LCD_SetBackColor(Blue);\r
266         LCD_SetTextColor(White);\r
267         LCD_DisplayStringLine(Line0, "  www.FreeRTOS.org");\r
268 }\r
269 /*-----------------------------------------------------------*/\r
270 \r
271 void vConfigureTimerForRunTimeStats( void )\r
272 {\r
273 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;\r
274 NVIC_InitTypeDef NVIC_InitStructure;\r
275 \r
276         /* TIM6 clock enable */\r
277         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);\r
278 \r
279         /* The 32MHz clock divided by 5000 should tick (very) approximately every\r
280         150uS and overflow a 16bit timer (very) approximately every 10 seconds. */\r
281         TIM_TimeBaseStructure.TIM_Period = 65535;\r
282         TIM_TimeBaseStructure.TIM_Prescaler = 5000;\r
283         TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;\r
284         TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;\r
285         \r
286         TIM_TimeBaseInit( TIM6, &TIM_TimeBaseStructure );\r
287         \r
288         /* Only interrupt on overflow events. */\r
289         TIM6->CR1 |= TIM_CR1_URS;\r
290         //TIM6->CR1 &= (uint16_t)~((uint16_t)TIM_CR1_URS);\r
291         \r
292         TIM_ITConfig( TIM6, TIM_IT_Update, ENABLE );\r
293         \r
294         /* Enable the TIM6 gloabal Interrupt */\r
295         NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;\r
296         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0f;\r
297         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0f;\r
298         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;\r
299         \r
300         TIM_ClearITPendingBit( TIM6, TIM_IT_Update );\r
301         NVIC_Init(&NVIC_InitStructure);\r
302         TIM_Cmd( TIM6, ENABLE );\r
303 }\r
304 /*-----------------------------------------------------------*/\r
305 \r
306 unsigned long ulGetRunTimeStatsCounterValue( void )\r
307 {\r
308 unsigned long ulReturn;\r
309 \r
310         TIM6->CR1 &= (uint16_t)(~((uint16_t)TIM_CR1_CEN));\r
311         ulReturn = ( ( ulTIM6_OverflowCount << 16UL ) | ( unsigned long ) TIM6->CNT );\r
312         TIM6->CR1 |= TIM_CR1_CEN;\r
313         \r
314         return ulReturn;\r
315 }\r
316 /*-----------------------------------------------------------*/\r
317 \r
318 void TIM6_IRQHandler( void )\r
319 {\r
320         if( TIM_GetITStatus( TIM6, TIM_IT_Update) != RESET)\r
321         {\r
322                 ulTIM6_OverflowCount++;\r
323                 TIM_ClearITPendingBit( TIM6, TIM_IT_Update );\r
324         }\r
325 }\r
326 \r
327 \r