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