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