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