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