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