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