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