2 FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
68 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
69 * documentation provides more details of the standard demo application tasks.
\r
70 * In addition to the standard demo tasks, the following tasks and tests are
\r
71 * defined and/or created within this file:
\r
73 * "Fast Interrupt Test" - A high frequency periodic interrupt is generated
\r
74 * using a free running timer to demonstrate the use of the
\r
75 * configKERNEL_INTERRUPT_PRIORITY configuration constant. The interrupt
\r
76 * service routine measures the number of processor clocks that occur between
\r
77 * each interrupt - and in so doing measures the jitter in the interrupt timing.
\r
78 * The maximum measured jitter time is latched in the ulMaxJitter variable, and
\r
79 * displayed on the LCD by the 'Check' task as described below. The
\r
80 * fast interrupt is configured and handled in the timertest.c source file.
\r
82 * "LCD" task - the LCD task is a 'gatekeeper' task. It is the only task that
\r
83 * is permitted to access the display directly. Other tasks wishing to write a
\r
84 * message to the LCD send the message on a queue to the LCD task instead of
\r
85 * accessing the LCD themselves. The LCD task just blocks on the queue waiting
\r
86 * for messages - waking and displaying the messages as they arrive.
\r
88 * "Check" task - This only executes every five seconds but has the highest
\r
89 * priority so is guaranteed to get processor time. Its main function is to
\r
90 * check that all the standard demo tasks are still operational. Should any
\r
91 * unexpected behaviour within a demo task be discovered the 'check' task will
\r
92 * write an error to the LCD (via the LCD task). If all the demo tasks are
\r
93 * executing with their expected behaviour then the check task writes PASS
\r
94 * along with the max jitter time to the LCD (again via the LCD task), as
\r
99 /* Standard includes. */
\r
102 /* Scheduler includes. */
\r
103 #include "FreeRTOS.h"
\r
107 /* Library includes. */
\r
108 #include "stm32f10x_it.h"
\r
110 /* Demo app includes. */
\r
112 #include "LCD_Message.h"
\r
113 #include "BlockQ.h"
\r
115 #include "integer.h"
\r
116 #include "blocktim.h"
\r
117 #include "partest.h"
\r
118 #include "semtest.h"
\r
121 #include "comtest2.h"
\r
123 /* Task priorities. */
\r
124 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
125 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
126 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
127 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
128 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
129 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
130 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
131 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
133 /* Constants related to the LCD. */
\r
134 #define mainMAX_LINE ( 240 )
\r
135 #define mainROW_INCREMENT ( 24 )
\r
136 #define mainMAX_COLUMN ( 20 )
\r
137 #define mainCOLUMN_START ( 319 )
\r
138 #define mainCOLUMN_INCREMENT ( 16 )
\r
140 /* The maximum number of message that can be waiting for display at any one
\r
142 #define mainLCD_QUEUE_SIZE ( 3 )
\r
144 /* The check task uses the sprintf function so requires a little more stack. */
\r
145 #define mainCHECK_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE + 50 )
\r
147 /* Dimensions the buffer into which the jitter time is written. */
\r
148 #define mainMAX_MSG_LEN 25
\r
150 /* The time between cycles of the 'check' task. */
\r
151 #define mainCHECK_DELAY ( ( portTickType ) 5000 / portTICK_RATE_MS )
\r
153 /* The number of nano seconds between each processor clock. */
\r
154 #define mainNS_PER_CLOCK ( ( unsigned portLONG ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )
\r
156 /* Baud rate used by the comtest tasks. */
\r
157 #define mainCOM_TEST_BAUD_RATE ( 115200 )
\r
159 /* The LED used by the comtest tasks. See the comtest.c file for more
\r
161 #define mainCOM_TEST_LED ( 3 )
\r
163 /*-----------------------------------------------------------*/
\r
166 * Configure the clocks, GPIO and other peripherals as required by the demo.
\r
168 static void prvSetupHardware( void );
\r
171 * Configure the LCD as required by the demo.
\r
173 static void prvConfigureLCD( void );
\r
176 * The LCD is written two by more than one task so is controlled by a
\r
177 * 'gatekeeper' task. This is the only task that is actually permitted to
\r
178 * access the LCD directly. Other tasks wanting to display a message send
\r
179 * the message to the gatekeeper.
\r
181 static void vLCDTask( void *pvParameters );
\r
184 * Retargets the C library printf function to the USART.
\r
186 int fputc( int ch, FILE *f );
\r
189 * Checks the status of all the demo tasks then prints a message to the
\r
190 * display. The message will be either PASS - and include in brackets the
\r
191 * maximum measured jitter time (as described at the to of the file), or a
\r
192 * message that describes which of the standard demo tasks an error has been
\r
195 * Messages are not written directly to the terminal, but passed to vLCDTask
\r
198 static void vCheckTask( void *pvParameters );
\r
201 * Configures the timers and interrupts for the fast interrupt test as
\r
202 * described at the top of this file.
\r
204 extern void vSetupTimerTest( void );
\r
206 /*-----------------------------------------------------------*/
\r
208 /* The queue used to send messages to the LCD task. */
\r
209 xQueueHandle xLCDQueue;
\r
211 /*-----------------------------------------------------------*/
\r
219 prvSetupHardware();
\r
221 /* Create the queue used by the LCD task. Messages for display on the LCD
\r
222 are received via this queue. */
\r
223 xLCDQueue = xQueueCreate( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );
\r
225 /* Start the standard demo tasks. */
\r
226 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
227 vCreateBlockTimeTasks();
\r
228 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
229 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
230 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
231 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
232 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
234 /* Start the tasks defined within this file/specific to this demo. */
\r
235 xTaskCreate( vCheckTask, ( signed portCHAR * ) "Check", mainCHECK_TASK_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
236 xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
238 /* The suicide tasks must be created last as they need to know how many
\r
239 tasks were running prior to their creation in order to ascertain whether
\r
240 or not the correct/expected number of tasks are running at any given time. */
\r
241 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
243 /* Configure the timers used by the fast interrupt timer test. */
\r
246 /* Start the scheduler. */
\r
247 vTaskStartScheduler();
\r
249 /* Will only get here if there was not enough heap space to create the
\r
253 /*-----------------------------------------------------------*/
\r
255 void vLCDTask( void *pvParameters )
\r
257 xLCDMessage xMessage;
\r
259 /* Initialise the LCD and display a startup message. */
\r
261 LCD_DrawMonoPict( ( unsigned portLONG * ) pcBitmap );
\r
265 /* Wait for a message to arrive that requires displaying. */
\r
266 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
\r
268 /* Display the message. Print each message to a different position. */
\r
269 printf( ( portCHAR const * ) xMessage.pcMessage );
\r
272 /*-----------------------------------------------------------*/
\r
274 static void vCheckTask( void *pvParameters )
\r
276 portTickType xLastExecutionTime;
\r
277 xLCDMessage xMessage;
\r
278 static signed portCHAR cPassMessage[ mainMAX_MSG_LEN ];
\r
279 extern unsigned portSHORT usMaxJitter;
\r
281 xLastExecutionTime = xTaskGetTickCount();
\r
282 xMessage.pcMessage = cPassMessage;
\r
286 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
287 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
289 /* Has an error been found in any task? */
\r
291 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
293 xMessage.pcMessage = "ERROR IN BLOCK Q\n";
\r
295 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
297 xMessage.pcMessage = "ERROR IN BLOCK TIME\n";
\r
299 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
301 xMessage.pcMessage = "ERROR IN SEMAPHORE\n";
\r
303 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
305 xMessage.pcMessage = "ERROR IN POLL Q\n";
\r
307 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
309 xMessage.pcMessage = "ERROR IN CREATE\n";
\r
311 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
313 xMessage.pcMessage = "ERROR IN MATH\n";
\r
315 else if( xAreComTestTasksStillRunning() != pdTRUE )
\r
317 xMessage.pcMessage = "ERROR IN COM TEST\n";
\r
321 sprintf( ( portCHAR * ) cPassMessage, "PASS [%uns]\n", ( ( unsigned portLONG ) usMaxJitter ) * mainNS_PER_CLOCK );
\r
324 /* Send the message to the LCD gatekeeper for display. */
\r
325 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );
\r
328 /*-----------------------------------------------------------*/
\r
330 static void prvSetupHardware( void )
\r
332 /* Start with the clocks in their expected state. */
\r
335 /* Enable HSE (high speed external clock). */
\r
336 RCC_HSEConfig( RCC_HSE_ON );
\r
338 /* Wait till HSE is ready. */
\r
339 while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )
\r
343 /* 2 wait states required on the flash. */
\r
344 *( ( unsigned portLONG * ) 0x40022000 ) = 0x02;
\r
346 /* HCLK = SYSCLK */
\r
347 RCC_HCLKConfig( RCC_SYSCLK_Div1 );
\r
350 RCC_PCLK2Config( RCC_HCLK_Div1 );
\r
352 /* PCLK1 = HCLK/2 */
\r
353 RCC_PCLK1Config( RCC_HCLK_Div2 );
\r
355 /* PLLCLK = 8MHz * 9 = 72 MHz. */
\r
356 RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );
\r
359 RCC_PLLCmd( ENABLE );
\r
361 /* Wait till PLL is ready. */
\r
362 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
\r
366 /* Select PLL as system clock source. */
\r
367 RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
\r
369 /* Wait till PLL is used as system clock source. */
\r
370 while( RCC_GetSYSCLKSource() != 0x08 )
\r
374 /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
\r
375 RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
\r
376 | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );
\r
378 /* SPI2 Periph clock enable */
\r
379 RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );
\r
382 /* Set the Vector Table base address at 0x08000000 */
\r
383 NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );
\r
385 NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
\r
387 /* Configure HCLK clock as SysTick clock source. */
\r
388 SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
\r
390 vParTestInitialise();
\r
392 /*-----------------------------------------------------------*/
\r
394 static void prvConfigureLCD( void )
\r
396 GPIO_InitTypeDef GPIO_InitStructure;
\r
398 /* Configure LCD Back Light (PA8) as output push-pull */
\r
399 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
\r
400 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
\r
401 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
\r
402 GPIO_Init( GPIOA, &GPIO_InitStructure );
\r
404 /* Set the Backlight Pin */
\r
405 GPIO_WriteBit(GPIOA, GPIO_Pin_8, Bit_SET);
\r
407 /* Initialize the LCD */
\r
410 /* Set the Back Color */
\r
411 LCD_SetBackColor( White );
\r
413 /* Set the Text Color */
\r
414 LCD_SetTextColor( 0x051F );
\r
418 /*-----------------------------------------------------------*/
\r
420 int fputc( int ch, FILE *f )
\r
422 static unsigned portSHORT usColumn = 0, usRefColumn = mainCOLUMN_START;
\r
423 static unsigned portCHAR ucLine = 0;
\r
425 if( ( usColumn == 0 ) && ( ucLine == 0 ) )
\r
432 /* Display one character on LCD */
\r
433 LCD_DisplayChar( ucLine, usRefColumn, (u8) ch );
\r
435 /* Decrement the column position by 16 */
\r
436 usRefColumn -= mainCOLUMN_INCREMENT;
\r
438 /* Increment the character counter */
\r
440 if( usColumn == mainMAX_COLUMN )
\r
442 ucLine += mainROW_INCREMENT;
\r
443 usRefColumn = mainCOLUMN_START;
\r
449 /* Move back to the first column of the next line. */
\r
450 ucLine += mainROW_INCREMENT;
\r
451 usRefColumn = mainCOLUMN_START;
\r
455 /* Wrap back to the top of the display. */
\r
456 if( ucLine >= mainMAX_LINE )
\r
463 /*-----------------------------------------------------------*/
\r
466 /* Keep the linker happy. */
\r
467 void assert_failed( unsigned portCHAR* pcFile, unsigned portLONG ulLine )
\r