2 FreeRTOS.org V4.7.2 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
28 Please ensure to read the configuration and relevant port sections of the
\r
29 online documentation.
\r
31 +++ http://www.FreeRTOS.org +++
\r
32 Documentation, latest information, license and contact details.
\r
34 +++ http://www.SafeRTOS.com +++
\r
35 A version that is certified for use in safety critical systems.
\r
37 +++ http://www.OpenRTOS.com +++
\r
38 Commercial support, development, porting, licensing and training services.
\r
40 ***************************************************************************
\r
44 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
45 * documentation provides more details of the standard demo application tasks.
\r
46 * In addition to the standard demo tasks, the following tasks and tests are
\r
47 * defined and/or created within this file:
\r
49 * "Fast Interrupt Test" - A high frequency periodic interrupt is generated
\r
50 * using a free running timer to demonstrate the use of the
\r
51 * configKERNEL_INTERRUPT_PRIORITY configuration constant. The interrupt
\r
52 * service routine measures the number of processor clocks that occur between
\r
53 * each interrupt - and in so doing measures the jitter in the interrupt timing.
\r
54 * The maximum measured jitter time is latched in the ulMaxJitter variable, and
\r
55 * displayed on the LCD by the 'Check' task as described below. The
\r
56 * fast interrupt is configured and handled in the timertest.c source file.
\r
58 * "LCD" task - the LCD task is a 'gatekeeper' task. It is the only task that
\r
59 * is permitted to access the display directly. Other tasks wishing to write a
\r
60 * message to the LCD send the message on a queue to the LCD task instead of
\r
61 * accessing the LCD themselves. The LCD task just blocks on the queue waiting
\r
62 * for messages - waking and displaying the messages as they arrive. Messages
\r
63 * can either be a text string to display, or an instruction to update MEMS
\r
64 * input. The MEMS input is used to display a ball that can be moved around
\r
65 * LCD by tilting the STM32 Primer. 45% is taken as the neutral position.
\r
67 * "Check" task - This only executes every five seconds but has the highest
\r
68 * priority so is guaranteed to get processor time. Its main function is to
\r
69 * check that all the standard demo tasks are still operational. Should any
\r
70 * unexpected behaviour within a demo task be discovered the 'check' task will
\r
71 * write an error to the LCD (via the LCD task). If all the demo tasks are
\r
72 * executing with their expected behaviour then the check task writes PASS
\r
73 * along with the max jitter time to the LCD (again via the LCD task), as
\r
76 * Tick Hook - A tick hook is provided just for demonstration purposes. In
\r
77 * this case it is used to periodically send an instruction to updated the
\r
78 * MEMS input to the LCD task.
\r
82 /* CircleOS includes. Some of the CircleOS peripheral functionality is
\r
83 utilised, although CircleOS itself is not used. */
\r
86 /* Standard includes. */
\r
89 /* Scheduler includes. */
\r
90 #include "FreeRTOS.h"
\r
94 /* Demo app includes. */
\r
96 #include "blocktim.h"
\r
97 #include "GenQTest.h"
\r
98 #include "partest.h"
\r
101 /* The bitmap used to display the FreeRTOS.org logo is stored in 16bit format
\r
102 and therefore takes up a large proportion of the Flash space. Setting this
\r
103 parameter to 0 excludes the bitmap from the build, freeing up Flash space for
\r
105 #define mainINCLUDE_BITMAP 1
\r
107 #if mainINCLUDE_BITMAP == 1
\r
108 #include "bitmap.h"
\r
111 /* Task priorities. */
\r
112 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
113 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
114 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
115 #define mainGEN_Q_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
116 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
118 /* Splash screen related constants. */
\r
119 #define mainBITMAP_Y ( 38 )
\r
120 #define mainBITMAP_X ( 18 )
\r
121 #define mainURL_Y ( 8 )
\r
122 #define mainURL_X ( 78 )
\r
123 #define mainSPLASH_SCREEN_DELAY ( 2000 / portTICK_RATE_MS )
\r
125 /* Text drawing related constants. */
\r
126 #define mainLCD_CHAR_HEIGHT ( 13 )
\r
127 #define mainLCD_MAX_Y ( 110 )
\r
129 /* The maximum number of message that can be waiting for display at any one
\r
131 #define mainLCD_QUEUE_SIZE ( 3 )
\r
133 /* The check task uses the sprintf function so requires a little more stack. */
\r
134 #define mainCHECK_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE + 50 )
\r
136 /* The LCD task calls some of the CircleOS functions (for MEMS and LCD access),
\r
137 these can require a larger stack. */
\r
138 #define configLCD_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE + 50 )
\r
140 /* Dimensions the buffer into which the jitter time is written. */
\r
141 #define mainMAX_MSG_LEN 25
\r
143 /* The time between cycles of the 'check' task. */
\r
144 #define mainCHECK_DELAY ( ( portTickType ) 5000 / portTICK_RATE_MS )
\r
146 /* The period at which the MEMS input should be updated. */
\r
147 #define mainMEMS_DELAY ( ( portTickType ) 100 / portTICK_RATE_MS )
\r
149 /* The rate at which the flash task toggles the LED. */
\r
150 #define mainFLASH_DELAY ( ( portTickType ) 1000 / portTICK_RATE_MS )
\r
152 /* The number of nano seconds between each processor clock. */
\r
153 #define mainNS_PER_CLOCK ( ( unsigned portLONG ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )
\r
155 /* The two types of message that can be sent to the LCD task. */
\r
156 #define mainUPDATE_BALL_MESSAGE ( 0 )
\r
157 #define mainWRITE_STRING_MESSAGE ( 1 )
\r
159 /* Type of the message sent to the LCD task. */
\r
162 portBASE_TYPE xMessageType;
\r
163 signed char *pcMessage;
\r
166 /*-----------------------------------------------------------*/
\r
169 * Configure the clocks, GPIO and other peripherals as required by the demo.
\r
171 static void prvSetupHardware( void );
\r
174 * The LCD is written two by more than one task so is controlled by a
\r
175 * 'gatekeeper' task. This is the only task that is actually permitted to
\r
176 * access the LCD directly. Other tasks wanting to display a message send
\r
177 * the message to the gatekeeper.
\r
179 static void prvLCDTask( void *pvParameters );
\r
182 * Checks the status of all the demo tasks then prints a message to the
\r
183 * display. The message will be either PASS - and include in brackets the
\r
184 * maximum measured jitter time (as described at the to of the file), or a
\r
185 * message that describes which of the standard demo tasks an error has been
\r
188 * Messages are not written directly to the terminal, but passed to prvLCDTask
\r
191 * The check task also receives instructions to update the MEMS input, which
\r
192 * in turn can also lead to the LCD being updated.
\r
194 static void prvCheckTask( void *pvParameters );
\r
197 * Configures the timers and interrupts for the fast interrupt test as
\r
198 * described at the top of this file.
\r
200 extern void vSetupTimerTest( void );
\r
203 * A cut down version of sprintf() used to percent the HUGE GCC library
\r
204 * equivalent from being included in the binary image.
\r
206 extern int sprintf(char *out, const char *format, ...);
\r
209 * Simple toggle the LED periodically for timing verification.
\r
211 static void prvFlashTask( void *pvParameters );
\r
213 /*-----------------------------------------------------------*/
\r
215 /* The queue used to send messages to the LCD task. */
\r
216 xQueueHandle xLCDQueue;
\r
218 /*-----------------------------------------------------------*/
\r
226 prvSetupHardware();
\r
228 /* Create the queue used by the LCD task. Messages for display on the LCD
\r
229 are received via this queue. */
\r
230 xLCDQueue = xQueueCreate( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );
\r
232 /* Start the standard demo tasks. */
\r
233 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
234 vCreateBlockTimeTasks();
\r
235 vStartGenericQueueTasks( mainGEN_Q_PRIORITY );
\r
236 vStartQueuePeekTasks();
\r
237 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
239 /* Start the tasks defined within this file/specific to this demo. */
\r
240 xTaskCreate( prvCheckTask, ( signed portCHAR * ) "Check", mainCHECK_TASK_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
241 xTaskCreate( prvLCDTask, ( signed portCHAR * ) "LCD", configLCD_TASK_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
242 xTaskCreate( prvFlashTask, ( signed portCHAR * ) "Flash", configMINIMAL_STACK_SIZE, NULL, mainFLASH_TASK_PRIORITY, NULL );
\r
244 /* Configure the timers used by the fast interrupt timer test. */
\r
247 /* Start the scheduler. */
\r
248 vTaskStartScheduler();
\r
250 /* Will only get here if there was not enough heap space to create the
\r
254 /*-----------------------------------------------------------*/
\r
256 void prvLCDTask( void *pvParameters )
\r
258 xLCDMessage xMessage;
\r
259 portCHAR cY = mainLCD_CHAR_HEIGHT;
\r
260 const portCHAR * const pcString = "www.FreeRTOS.org";
\r
261 const portCHAR * const pcBlankLine = " ";
\r
265 #if mainINCLUDE_BITMAP == 1
\r
266 DRAW_SetImage( pucImage, mainBITMAP_Y, mainBITMAP_X, bmpBITMAP_HEIGHT, bmpBITMAP_WIDTH );
\r
269 LCD_SetScreenOrientation( V9 );
\r
270 DRAW_DisplayString( mainURL_Y, mainURL_X, pcString, strlen( pcString ) );
\r
271 vTaskDelay( mainSPLASH_SCREEN_DELAY );
\r
272 LCD_FillRect( 0, 0, CHIP_SCREEN_WIDTH, CHIP_SCREEN_HEIGHT, RGB_WHITE );
\r
276 /* Wait for a message to arrive that requires displaying. */
\r
277 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
\r
279 /* Check the message type. */
\r
280 if( xMessage.xMessageType == mainUPDATE_BALL_MESSAGE )
\r
282 /* Read the MEMS and update the ball display on the LCD if required. */
\r
288 /* A text string was sent. First blank off the old text string, then
\r
289 draw the new text on the next line down. */
\r
290 DRAW_DisplayString( 0, cY, pcBlankLine, strlen( pcBlankLine ) );
\r
292 cY -= mainLCD_CHAR_HEIGHT;
\r
293 if( cY <= ( mainLCD_CHAR_HEIGHT - 1 ) )
\r
295 /* Wrap the line onto which we are going to write the text. */
\r
296 cY = mainLCD_MAX_Y;
\r
299 /* Display the message. */
\r
300 DRAW_DisplayString( 0, cY, xMessage.pcMessage, strlen( xMessage.pcMessage ) );
\r
304 /*-----------------------------------------------------------*/
\r
306 static void prvCheckTask( void *pvParameters )
\r
308 portTickType xLastExecutionTime;
\r
309 xLCDMessage xMessage;
\r
310 static signed portCHAR cPassMessage[ mainMAX_MSG_LEN ];
\r
311 extern unsigned portSHORT usMaxJitter;
\r
313 /* Initialise the xLastExecutionTime variable on task entry. */
\r
314 xLastExecutionTime = xTaskGetTickCount();
\r
316 /* Setup the message we are going to send to the LCD task. */
\r
317 xMessage.xMessageType = mainWRITE_STRING_MESSAGE;
\r
318 xMessage.pcMessage = cPassMessage;
\r
322 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
323 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
325 /* Has an error been found in any task? If so then point the text
\r
326 we are going to send to the LCD task to an error message instead of
\r
327 the PASS message. */
\r
328 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
330 xMessage.pcMessage = "ERROR IN GEN Q";
\r
332 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
334 xMessage.pcMessage = "ERROR IN BLOCK Q";
\r
336 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
338 xMessage.pcMessage = "ERROR IN BLOCK TIME";
\r
340 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
342 xMessage.pcMessage = "ERROR IN POLL Q";
\r
344 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
346 xMessage.pcMessage = "ERROR IN PEEK Q";
\r
350 /* No errors were found in any task, so send a pass message
\r
351 with the max measured jitter time also included (as per the
\r
352 fast interrupt test described at the top of this file and on
\r
353 the online documentation page for this demo application). */
\r
354 sprintf( ( portCHAR * ) cPassMessage, "PASS [%uns]", ( ( unsigned portLONG ) usMaxJitter ) * mainNS_PER_CLOCK );
\r
357 /* Send the message to the LCD gatekeeper for display. */
\r
358 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );
\r
361 /*-----------------------------------------------------------*/
\r
363 void vApplicationTickHook( void )
\r
365 static unsigned portLONG ulCallCount;
\r
366 static const xLCDMessage xMemsMessage = { mainUPDATE_BALL_MESSAGE, NULL };
\r
368 /* Periodically send a message to the LCD task telling it to update
\r
369 the MEMS input, and then if necessary the LCD. */
\r
371 if( ulCallCount >= mainMEMS_DELAY )
\r
374 xQueueSendFromISR( xLCDQueue, &xMemsMessage, pdFALSE );
\r
377 /*-----------------------------------------------------------*/
\r
379 static void prvSetupHardware( void )
\r
381 /* Start with the clocks in their expected state. */
\r
384 /* Enable HSE (high speed external clock). */
\r
385 RCC_HSEConfig( RCC_HSE_ON );
\r
387 /* Wait till HSE is ready. */
\r
388 while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )
\r
392 /* 2 wait states required on the flash. */
\r
393 *( ( unsigned portLONG * ) 0x40022000 ) = 0x02;
\r
395 /* HCLK = SYSCLK */
\r
396 RCC_HCLKConfig( RCC_SYSCLK_Div1 );
\r
399 RCC_PCLK2Config( RCC_HCLK_Div1 );
\r
401 /* PCLK1 = HCLK/2 */
\r
402 RCC_PCLK1Config( RCC_HCLK_Div2 );
\r
404 /* PLLCLK = 12MHz * 6 = 72 MHz. */
\r
405 RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_6 );
\r
408 RCC_PLLCmd( ENABLE );
\r
410 /* Wait till PLL is ready. */
\r
411 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
\r
415 /* Select PLL as system clock source. */
\r
416 RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
\r
418 /* Wait till PLL is used as system clock source. */
\r
419 while( RCC_GetSYSCLKSource() != 0x08 )
\r
423 /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
\r
424 RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
\r
425 | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );
\r
427 /* SPI2 Periph clock enable */
\r
428 RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );
\r
431 /* Set the Vector Table base address at 0x08000000 */
\r
432 NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );
\r
434 NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
\r
436 /* Configure HCLK clock as SysTick clock source. */
\r
437 SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
\r
439 /* Misc initialisation, including some of the CircleOS features. Note
\r
440 that CircleOS itself is not used. */
\r
441 vParTestInitialise();
\r
444 POINTER_SetMode( POINTER_RESTORE_LESS );
\r
446 /*-----------------------------------------------------------*/
\r
448 static void prvFlashTask( void *pvParameters )
\r
450 portTickType xLastExecutionTime;
\r
452 /* Initialise the xLastExecutionTime variable on task entry. */
\r
453 xLastExecutionTime = xTaskGetTickCount();
\r
457 /* Simple toggle the LED periodically. This just provides some timing
\r
459 vTaskDelayUntil( &xLastExecutionTime, mainFLASH_DELAY );
\r
460 vParTestToggleLED( 0 );
\r
463 /*-----------------------------------------------------------*/
\r
465 void starting_delay( unsigned long ul )
\r
467 vTaskDelay( ( portTickType ) ul );
\r