2 FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\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
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
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
25 ***************************************************************************
\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
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
37 ***************************************************************************
\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
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
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
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
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
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
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
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
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
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
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
102 /* Standard includes. */
\r
105 /* Scheduler includes. */
\r
106 #include "FreeRTOS.h"
\r
110 /* Library includes. */
\r
111 #include "stm32f10x_it.h"
\r
113 /* Demo app includes. */
\r
115 #include "LCD_Message.h"
\r
116 #include "BlockQ.h"
\r
118 #include "integer.h"
\r
119 #include "blocktim.h"
\r
120 #include "partest.h"
\r
121 #include "semtest.h"
\r
124 #include "comtest2.h"
\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
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
143 /* The maximum number of message that can be waiting for display at any one
\r
145 #define mainLCD_QUEUE_SIZE ( 3 )
\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
150 /* Dimensions the buffer into which the jitter time is written. */
\r
151 #define mainMAX_MSG_LEN 25
\r
153 /* The time between cycles of the 'check' task. */
\r
154 #define mainCHECK_DELAY ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )
\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
159 /* Baud rate used by the comtest tasks. */
\r
160 #define mainCOM_TEST_BAUD_RATE ( 115200 )
\r
162 /* The LED used by the comtest tasks. See the comtest.c file for more
\r
164 #define mainCOM_TEST_LED ( 3 )
\r
166 /*-----------------------------------------------------------*/
\r
169 * Configure the clocks, GPIO and other peripherals as required by the demo.
\r
171 static void prvSetupHardware( void );
\r
174 * Configure the LCD as required by the demo.
\r
176 static void prvConfigureLCD( void );
\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
184 static void vLCDTask( void *pvParameters );
\r
187 * Retargets the C library printf function to the USART.
\r
189 int fputc( int ch, FILE *f );
\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
198 * Messages are not written directly to the terminal, but passed to vLCDTask
\r
201 static void vCheckTask( void *pvParameters );
\r
204 * Configures the timers and interrupts for the fast interrupt test as
\r
205 * described at the top of this file.
\r
207 extern void vSetupTimerTest( void );
\r
209 /*-----------------------------------------------------------*/
\r
211 /* The queue used to send messages to the LCD task. */
\r
212 QueueHandle_t xLCDQueue;
\r
214 /*-----------------------------------------------------------*/
\r
222 prvSetupHardware();
\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
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
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
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
246 /* Configure the timers used by the fast interrupt timer test. */
\r
249 /* Start the scheduler. */
\r
250 vTaskStartScheduler();
\r
252 /* Will only get here if there was not enough heap space to create the
\r
256 /*-----------------------------------------------------------*/
\r
258 void vLCDTask( void *pvParameters )
\r
260 xLCDMessage xMessage;
\r
262 /* Initialise the LCD and display a startup message. */
\r
264 LCD_DrawMonoPict( ( unsigned long * ) pcBitmap );
\r
268 /* Wait for a message to arrive that requires displaying. */
\r
269 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
\r
271 /* Display the message. Print each message to a different position. */
\r
272 printf( ( char const * ) xMessage.pcMessage );
\r
275 /*-----------------------------------------------------------*/
\r
277 static void vCheckTask( void *pvParameters )
\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
284 xLastExecutionTime = xTaskGetTickCount();
\r
285 xMessage.pcMessage = cPassMessage;
\r
289 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
290 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
292 /* Has an error been found in any task? */
\r
294 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
296 xMessage.pcMessage = "ERROR IN BLOCK Q\n";
\r
298 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
300 xMessage.pcMessage = "ERROR IN BLOCK TIME\n";
\r
302 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
304 xMessage.pcMessage = "ERROR IN SEMAPHORE\n";
\r
306 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
308 xMessage.pcMessage = "ERROR IN POLL Q\n";
\r
310 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
312 xMessage.pcMessage = "ERROR IN CREATE\n";
\r
314 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
316 xMessage.pcMessage = "ERROR IN MATH\n";
\r
318 else if( xAreComTestTasksStillRunning() != pdTRUE )
\r
320 xMessage.pcMessage = "ERROR IN COM TEST\n";
\r
324 sprintf( ( char * ) cPassMessage, "PASS [%uns]\n", ( ( unsigned long ) usMaxJitter ) * mainNS_PER_CLOCK );
\r
327 /* Send the message to the LCD gatekeeper for display. */
\r
328 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );
\r
331 /*-----------------------------------------------------------*/
\r
333 static void prvSetupHardware( void )
\r
335 /* Start with the clocks in their expected state. */
\r
338 /* Enable HSE (high speed external clock). */
\r
339 RCC_HSEConfig( RCC_HSE_ON );
\r
341 /* Wait till HSE is ready. */
\r
342 while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )
\r
346 /* 2 wait states required on the flash. */
\r
347 *( ( unsigned long * ) 0x40022000 ) = 0x02;
\r
349 /* HCLK = SYSCLK */
\r
350 RCC_HCLKConfig( RCC_SYSCLK_Div1 );
\r
353 RCC_PCLK2Config( RCC_HCLK_Div1 );
\r
355 /* PCLK1 = HCLK/2 */
\r
356 RCC_PCLK1Config( RCC_HCLK_Div2 );
\r
358 /* PLLCLK = 8MHz * 9 = 72 MHz. */
\r
359 RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );
\r
362 RCC_PLLCmd( ENABLE );
\r
364 /* Wait till PLL is ready. */
\r
365 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
\r
369 /* Select PLL as system clock source. */
\r
370 RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
\r
372 /* Wait till PLL is used as system clock source. */
\r
373 while( RCC_GetSYSCLKSource() != 0x08 )
\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
381 /* SPI2 Periph clock enable */
\r
382 RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );
\r
385 /* Set the Vector Table base address at 0x08000000 */
\r
386 NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );
\r
388 NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
\r
390 /* Configure HCLK clock as SysTick clock source. */
\r
391 SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
\r
393 vParTestInitialise();
\r
395 /*-----------------------------------------------------------*/
\r
397 static void prvConfigureLCD( void )
\r
399 GPIO_InitTypeDef GPIO_InitStructure;
\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
407 /* Set the Backlight Pin */
\r
408 GPIO_WriteBit(GPIOA, GPIO_Pin_8, Bit_SET);
\r
410 /* Initialize the LCD */
\r
413 /* Set the Back Color */
\r
414 LCD_SetBackColor( White );
\r
416 /* Set the Text Color */
\r
417 LCD_SetTextColor( 0x051F );
\r
421 /*-----------------------------------------------------------*/
\r
423 int fputc( int ch, FILE *f )
\r
425 static unsigned short usColumn = 0, usRefColumn = mainCOLUMN_START;
\r
426 static unsigned char ucLine = 0;
\r
428 if( ( usColumn == 0 ) && ( ucLine == 0 ) )
\r
435 /* Display one character on LCD */
\r
436 LCD_DisplayChar( ucLine, usRefColumn, (u8) ch );
\r
438 /* Decrement the column position by 16 */
\r
439 usRefColumn -= mainCOLUMN_INCREMENT;
\r
441 /* Increment the character counter */
\r
443 if( usColumn == mainMAX_COLUMN )
\r
445 ucLine += mainROW_INCREMENT;
\r
446 usRefColumn = mainCOLUMN_START;
\r
452 /* Move back to the first column of the next line. */
\r
453 ucLine += mainROW_INCREMENT;
\r
454 usRefColumn = mainCOLUMN_START;
\r
458 /* Wrap back to the top of the display. */
\r
459 if( ucLine >= mainMAX_LINE )
\r
466 /*-----------------------------------------------------------*/
\r
469 /* Keep the linker happy. */
\r
470 void assert_failed( unsigned char* pcFile, unsigned long ulLine )
\r