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