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