]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_STM32F103_IAR/main.c
Prepare for V5.3.1 release.
[freertos] / Demo / CORTEX_STM32F103_IAR / main.c
1 /*\r
2         FreeRTOS.org V5.3.1 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify it\r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS.org without being obliged to provide\r
11         the source code for any proprietary components.  Alternative commercial\r
12         license and support terms are also available upon request.  See the \r
13         licensing section of http://www.FreeRTOS.org for full details.\r
14 \r
15         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
16         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
17         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
18         more details.\r
19 \r
20         You should have received a copy of the GNU General Public License along\r
21         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
22         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 /*\r
53  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
54  * documentation provides more details of the standard demo application tasks.\r
55  * In addition to the standard demo tasks, the following tasks and tests are\r
56  * defined and/or created within this file:\r
57  *\r
58  * "Fast Interrupt Test" - A high frequency periodic interrupt is generated\r
59  * using a free running timer to demonstrate the use of the\r
60  * configKERNEL_INTERRUPT_PRIORITY configuration constant.  The interrupt\r
61  * service routine measures the number of processor clocks that occur between\r
62  * each interrupt - and in so doing measures the jitter in the interrupt timing.\r
63  * The maximum measured jitter time is latched in the ulMaxJitter variable, and\r
64  * displayed on the LCD by the 'Check' task as described below.  The\r
65  * fast interrupt is configured and handled in the timertest.c source file.\r
66  *\r
67  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that\r
68  * is permitted to access the display directly.  Other tasks wishing to write a\r
69  * message to the LCD send the message on a queue to the LCD task instead of\r
70  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting\r
71  * for messages - waking and displaying the messages as they arrive.\r
72  *\r
73  * "Check" task -  This only executes every five seconds but has the highest\r
74  * priority so is guaranteed to get processor time.  Its main function is to\r
75  * check that all the standard demo tasks are still operational.  Should any\r
76  * unexpected behaviour within a demo task be discovered the 'check' task will\r
77  * write an error to the LCD (via the LCD task).  If all the demo tasks are\r
78  * executing with their expected behaviour then the check task writes PASS\r
79  * along with the max jitter time to the LCD (again via the LCD task), as\r
80  * described above.\r
81  *\r
82  */\r
83 \r
84 /* Standard includes. */\r
85 #include <stdio.h>\r
86 \r
87 /* Scheduler includes. */\r
88 #include "FreeRTOS.h"\r
89 #include "task.h"\r
90 #include "queue.h"\r
91 \r
92 /* Library includes. */\r
93 #include "stm32f10x_it.h"\r
94 \r
95 /* Demo app includes. */\r
96 #include "lcd.h"\r
97 #include "LCD_Message.h"\r
98 #include "BlockQ.h"\r
99 #include "death.h"\r
100 #include "integer.h"\r
101 #include "blocktim.h"\r
102 #include "partest.h"\r
103 #include "semtest.h"\r
104 #include "PollQ.h"\r
105 #include "flash.h"\r
106 #include "comtest2.h"\r
107 \r
108 /* Task priorities. */\r
109 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
110 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
111 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
112 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
113 #define mainCREATOR_TASK_PRIORITY           ( tskIDLE_PRIORITY + 3 )\r
114 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 1 )\r
115 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
116 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
117 \r
118 /* Constants related to the LCD. */\r
119 #define mainMAX_LINE                                            ( 240 )\r
120 #define mainROW_INCREMENT                                       ( 24 )\r
121 #define mainMAX_COLUMN                                          ( 20 )\r
122 #define mainCOLUMN_START                                        ( 319 )\r
123 #define mainCOLUMN_INCREMENT                            ( 16 )\r
124 \r
125 /* The maximum number of message that can be waiting for display at any one\r
126 time. */\r
127 #define mainLCD_QUEUE_SIZE                                      ( 3 )\r
128 \r
129 /* The check task uses the sprintf function so requires a little more stack. */\r
130 #define mainCHECK_TASK_STACK_SIZE                       ( configMINIMAL_STACK_SIZE + 50 )\r
131 \r
132 /* Dimensions the buffer into which the jitter time is written. */\r
133 #define mainMAX_MSG_LEN                                         25\r
134 \r
135 /* The time between cycles of the 'check' task. */\r
136 #define mainCHECK_DELAY                                         ( ( portTickType ) 5000 / portTICK_RATE_MS )\r
137 \r
138 /* The number of nano seconds between each processor clock. */\r
139 #define mainNS_PER_CLOCK ( ( unsigned portLONG ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )\r
140 \r
141 /* Baud rate used by the comtest tasks. */\r
142 #define mainCOM_TEST_BAUD_RATE          ( 115200 )\r
143 \r
144 /* The LED used by the comtest tasks. See the comtest.c file for more\r
145 information. */\r
146 #define mainCOM_TEST_LED                        ( 3 )\r
147 \r
148 /*-----------------------------------------------------------*/\r
149 \r
150 /*\r
151  * Configure the clocks, GPIO and other peripherals as required by the demo.\r
152  */\r
153 static void prvSetupHardware( void );\r
154 \r
155 /*\r
156  * Configure the LCD as required by the demo.\r
157  */\r
158 static void prvConfigureLCD( void );\r
159 \r
160 /*\r
161  * The LCD is written two by more than one task so is controlled by a\r
162  * 'gatekeeper' task.  This is the only task that is actually permitted to\r
163  * access the LCD directly.  Other tasks wanting to display a message send\r
164  * the message to the gatekeeper.\r
165  */\r
166 static void vLCDTask( void *pvParameters );\r
167 \r
168 /*\r
169  * Retargets the C library printf function to the USART.\r
170  */\r
171 int fputc( int ch, FILE *f );\r
172 \r
173 /*\r
174  * Checks the status of all the demo tasks then prints a message to the\r
175  * display.  The message will be either PASS - and include in brackets the\r
176  * maximum measured jitter time (as described at the to of the file), or a\r
177  * message that describes which of the standard demo tasks an error has been\r
178  * discovered in.\r
179  *\r
180  * Messages are not written directly to the terminal, but passed to vLCDTask\r
181  * via a queue.\r
182  */\r
183 static void vCheckTask( void *pvParameters );\r
184 \r
185 /*\r
186  * Configures the timers and interrupts for the fast interrupt test as\r
187  * described at the top of this file.\r
188  */\r
189 extern void vSetupTimerTest( void );\r
190 \r
191 /*-----------------------------------------------------------*/\r
192 \r
193 /* The queue used to send messages to the LCD task. */\r
194 xQueueHandle xLCDQueue;\r
195 \r
196 /*-----------------------------------------------------------*/\r
197 \r
198 int main( void )\r
199 {\r
200 #ifdef DEBUG\r
201   debug();\r
202 #endif\r
203 \r
204         prvSetupHardware();\r
205 \r
206         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
207         are received via this queue. */\r
208         xLCDQueue = xQueueCreate( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );\r
209         \r
210         /* Start the standard demo tasks. */\r
211         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
212     vCreateBlockTimeTasks();\r
213     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
214     vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
215     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
216         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
217         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
218 \r
219         /* Start the tasks defined within this file/specific to this demo. */\r
220     xTaskCreate( vCheckTask, ( signed portCHAR * ) "Check", mainCHECK_TASK_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );   \r
221         xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
222 \r
223         /* The suicide tasks must be created last as they need to know how many\r
224         tasks were running prior to their creation in order to ascertain whether\r
225         or not the correct/expected number of tasks are running at any given time. */\r
226     vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
227         \r
228         /* Configure the timers used by the fast interrupt timer test. */\r
229         vSetupTimerTest();\r
230         \r
231         /* Start the scheduler. */\r
232         vTaskStartScheduler();\r
233         \r
234         /* Will only get here if there was not enough heap space to create the\r
235         idle task. */\r
236         return 0;\r
237 }\r
238 /*-----------------------------------------------------------*/\r
239 \r
240 void vLCDTask( void *pvParameters )\r
241 {\r
242 xLCDMessage xMessage;\r
243 \r
244         /* Initialise the LCD and display a startup message. */\r
245         prvConfigureLCD();\r
246         LCD_DrawMonoPict( ( unsigned portLONG * ) pcBitmap );\r
247 \r
248         for( ;; )\r
249         {\r
250                 /* Wait for a message to arrive that requires displaying. */\r
251                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );\r
252 \r
253                 /* Display the message.  Print each message to a different position. */\r
254                 printf( ( portCHAR const * ) xMessage.pcMessage );\r
255         }\r
256 }\r
257 /*-----------------------------------------------------------*/\r
258 \r
259 static void vCheckTask( void *pvParameters )\r
260 {\r
261 portTickType xLastExecutionTime;\r
262 xLCDMessage xMessage;\r
263 static signed portCHAR cPassMessage[ mainMAX_MSG_LEN ];\r
264 extern unsigned portSHORT usMaxJitter;\r
265 \r
266         xLastExecutionTime = xTaskGetTickCount();\r
267         xMessage.pcMessage = cPassMessage;\r
268         \r
269     for( ;; )\r
270         {\r
271                 /* Perform this check every mainCHECK_DELAY milliseconds. */\r
272                 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );\r
273 \r
274                 /* Has an error been found in any task? */\r
275 \r
276         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
277                 {\r
278                         xMessage.pcMessage = "ERROR IN BLOCK Q\n";\r
279                 }\r
280                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
281                 {\r
282                         xMessage.pcMessage = "ERROR IN BLOCK TIME\n";\r
283                 }\r
284         else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
285         {\r
286             xMessage.pcMessage = "ERROR IN SEMAPHORE\n";\r
287         }\r
288         else if( xArePollingQueuesStillRunning() != pdTRUE )\r
289         {\r
290             xMessage.pcMessage = "ERROR IN POLL Q\n";\r
291         }\r
292         else if( xIsCreateTaskStillRunning() != pdTRUE )\r
293         {\r
294             xMessage.pcMessage = "ERROR IN CREATE\n";\r
295         }\r
296         else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
297         {\r
298             xMessage.pcMessage = "ERROR IN MATH\n";\r
299         }\r
300                 else if( xAreComTestTasksStillRunning() != pdTRUE )\r
301                 {\r
302                         xMessage.pcMessage = "ERROR IN COM TEST\n";\r
303                 }                               \r
304                 else\r
305                 {\r
306                         sprintf( ( portCHAR * ) cPassMessage, "PASS [%uns]\n", ( ( unsigned portLONG ) usMaxJitter ) * mainNS_PER_CLOCK );\r
307                 }\r
308 \r
309                 /* Send the message to the LCD gatekeeper for display. */\r
310                 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );\r
311         }\r
312 }\r
313 /*-----------------------------------------------------------*/\r
314 \r
315 static void prvSetupHardware( void )\r
316 {\r
317         /* Start with the clocks in their expected state. */\r
318         RCC_DeInit();\r
319 \r
320         /* Enable HSE (high speed external clock). */\r
321         RCC_HSEConfig( RCC_HSE_ON );\r
322 \r
323         /* Wait till HSE is ready. */\r
324         while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )\r
325         {\r
326         }\r
327 \r
328         /* 2 wait states required on the flash. */\r
329         *( ( unsigned portLONG * ) 0x40022000 ) = 0x02;\r
330 \r
331         /* HCLK = SYSCLK */\r
332         RCC_HCLKConfig( RCC_SYSCLK_Div1 );\r
333 \r
334         /* PCLK2 = HCLK */\r
335         RCC_PCLK2Config( RCC_HCLK_Div1 );\r
336 \r
337         /* PCLK1 = HCLK/2 */\r
338         RCC_PCLK1Config( RCC_HCLK_Div2 );\r
339 \r
340         /* PLLCLK = 8MHz * 9 = 72 MHz. */\r
341         RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );\r
342 \r
343         /* Enable PLL. */\r
344         RCC_PLLCmd( ENABLE );\r
345 \r
346         /* Wait till PLL is ready. */\r
347         while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)\r
348         {\r
349         }\r
350 \r
351         /* Select PLL as system clock source. */\r
352         RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );\r
353 \r
354         /* Wait till PLL is used as system clock source. */\r
355         while( RCC_GetSYSCLKSource() != 0x08 )\r
356         {\r
357         }\r
358 \r
359         /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */\r
360         RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC\r
361                                                         | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );\r
362 \r
363         /* SPI2 Periph clock enable */\r
364         RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );\r
365 \r
366 \r
367         /* Set the Vector Table base address at 0x08000000 */\r
368         NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );\r
369 \r
370         NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );\r
371         \r
372         /* Configure HCLK clock as SysTick clock source. */\r
373         SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );\r
374         \r
375         vParTestInitialise();\r
376 }\r
377 /*-----------------------------------------------------------*/\r
378 \r
379 static void prvConfigureLCD( void )\r
380 {\r
381 GPIO_InitTypeDef GPIO_InitStructure;\r
382 \r
383         /* Configure LCD Back Light (PA8) as output push-pull */\r
384         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;\r
385         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;\r
386         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;\r
387         GPIO_Init( GPIOA, &GPIO_InitStructure );\r
388 \r
389         /* Set the Backlight Pin */\r
390         GPIO_WriteBit(GPIOA, GPIO_Pin_8, Bit_SET);\r
391 \r
392         /* Initialize the LCD */\r
393         LCD_Init();\r
394 \r
395         /* Set the Back Color */\r
396         LCD_SetBackColor( White );\r
397 \r
398         /* Set the Text Color */\r
399         LCD_SetTextColor( 0x051F );\r
400 \r
401         LCD_Clear();\r
402 }\r
403 /*-----------------------------------------------------------*/\r
404 \r
405 int fputc( int ch, FILE *f )\r
406 {\r
407 static unsigned portSHORT usColumn = 0, usRefColumn = mainCOLUMN_START;\r
408 static unsigned portCHAR ucLine = 0;\r
409 \r
410         if( ( usColumn == 0 ) && ( ucLine == 0 ) )\r
411         {\r
412                 LCD_Clear();\r
413         }\r
414 \r
415         if( ch != '\n' )\r
416         {\r
417                 /* Display one character on LCD */\r
418                 LCD_DisplayChar( ucLine, usRefColumn, (u8) ch );\r
419                 \r
420                 /* Decrement the column position by 16 */\r
421                 usRefColumn -= mainCOLUMN_INCREMENT;\r
422                 \r
423                 /* Increment the character counter */\r
424                 usColumn++;\r
425                 if( usColumn == mainMAX_COLUMN )\r
426                 {\r
427                         ucLine += mainROW_INCREMENT;\r
428                         usRefColumn = mainCOLUMN_START;\r
429                         usColumn = 0;\r
430                 }\r
431         }\r
432         else\r
433         {\r
434                 /* Move back to the first column of the next line. */\r
435                 ucLine += mainROW_INCREMENT;\r
436                 usRefColumn = mainCOLUMN_START;\r
437                 usColumn = 0;   \r
438         }\r
439 \r
440         /* Wrap back to the top of the display. */\r
441         if( ucLine >= mainMAX_LINE )\r
442         {\r
443                 ucLine = 0;\r
444         }\r
445         \r
446         return ch;\r
447 }\r
448 /*-----------------------------------------------------------*/\r
449 \r
450 #ifdef  DEBUG\r
451 /* Keep the linker happy. */\r
452 void assert_failed( unsigned portCHAR* pcFile, unsigned portLONG ulLine )\r
453 {\r
454         for( ;; )\r
455         {\r
456         }\r
457 }\r
458 #endif\r