2 FreeRTOS V6.0.3 - Copyright (C) 2010 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS eBook *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\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 exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 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 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
56 * documentation provides more details of the standard demo application tasks.
\r
57 * In addition to the standard demo tasks, the following tasks and tests are
\r
58 * defined and/or created within this file:
\r
60 * "Fast Interrupt Test" - A high frequency periodic interrupt is generated
\r
61 * using a free running timer to demonstrate the use of the
\r
62 * configKERNEL_INTERRUPT_PRIORITY configuration constant. The interrupt
\r
63 * service routine measures the number of processor clocks that occur between
\r
64 * each interrupt - and in so doing measures the jitter in the interrupt timing.
\r
65 * The maximum measured jitter time is latched in the ulMaxJitter variable, and
\r
66 * displayed on the LCD by the 'Check' task as described below. The
\r
67 * fast interrupt is configured and handled in the timertest.c source file.
\r
69 * "LCD" task - the LCD task is a 'gatekeeper' task. It is the only task that
\r
70 * is permitted to access the display directly. Other tasks wishing to write a
\r
71 * message to the LCD send the message on a queue to the LCD task instead of
\r
72 * accessing the LCD themselves. The LCD task just blocks on the queue waiting
\r
73 * for messages - waking and displaying the messages as they arrive. Messages
\r
74 * can either be a text string to display, or an instruction to update MEMS
\r
75 * input. The MEMS input is used to display a ball that can be moved around
\r
76 * LCD by tilting the STM32 Primer. 45% is taken as the neutral position.
\r
78 * "Check" task - This only executes every five seconds but has the highest
\r
79 * priority so is guaranteed to get processor time. Its main function is to
\r
80 * check that all the standard demo tasks are still operational. Should any
\r
81 * unexpected behaviour within a demo task be discovered the 'check' task will
\r
82 * write an error to the LCD (via the LCD task). If all the demo tasks are
\r
83 * executing with their expected behaviour then the check task writes PASS
\r
84 * along with the max jitter time to the LCD (again via the LCD task), as
\r
87 * Tick Hook - A tick hook is provided just for demonstration purposes. In
\r
88 * this case it is used to periodically send an instruction to updated the
\r
89 * MEMS input to the LCD task.
\r
93 /* CircleOS includes. Some of the CircleOS peripheral functionality is
\r
94 utilised, although CircleOS itself is not used. */
\r
97 /* Standard includes. */
\r
100 /* Scheduler includes. */
\r
101 #include "FreeRTOS.h"
\r
105 /* Demo app includes. */
\r
106 #include "BlockQ.h"
\r
107 #include "blocktim.h"
\r
108 #include "GenQTest.h"
\r
109 #include "partest.h"
\r
112 /* The bitmap used to display the FreeRTOS.org logo is stored in 16bit format
\r
113 and therefore takes up a large proportion of the Flash space. Setting this
\r
114 parameter to 0 excludes the bitmap from the build, freeing up Flash space for
\r
116 #define mainINCLUDE_BITMAP 0
\r
118 #if mainINCLUDE_BITMAP == 1
\r
119 #include "bitmap.h"
\r
122 /* Task priorities. */
\r
123 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
124 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
125 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
126 #define mainGEN_Q_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
127 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
129 /* Splash screen related constants. */
\r
130 #define mainBITMAP_Y ( 38 )
\r
131 #define mainBITMAP_X ( 18 )
\r
132 #define mainURL_Y ( 8 )
\r
133 #define mainURL_X ( 78 )
\r
134 #define mainSPLASH_SCREEN_DELAY ( 2000 / portTICK_RATE_MS )
\r
136 /* Text drawing related constants. */
\r
137 #define mainLCD_CHAR_HEIGHT ( 13 )
\r
138 #define mainLCD_MAX_Y ( 110 )
\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 /* The LCD task calls some of the CircleOS functions (for MEMS and LCD access),
\r
148 these can require a larger stack. */
\r
149 #define configLCD_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE + 50 )
\r
151 /* Dimensions the buffer into which the jitter time is written. */
\r
152 #define mainMAX_MSG_LEN 25
\r
154 /* The time between cycles of the 'check' task. */
\r
155 #define mainCHECK_DELAY ( ( portTickType ) 5000 / portTICK_RATE_MS )
\r
157 /* The period at which the MEMS input should be updated. */
\r
158 #define mainMEMS_DELAY ( ( portTickType ) 100 / portTICK_RATE_MS )
\r
160 /* The rate at which the flash task toggles the LED. */
\r
161 #define mainFLASH_DELAY ( ( portTickType ) 1000 / portTICK_RATE_MS )
\r
163 /* The number of nano seconds between each processor clock. */
\r
164 #define mainNS_PER_CLOCK ( ( unsigned portLONG ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )
\r
166 /* The two types of message that can be sent to the LCD task. */
\r
167 #define mainUPDATE_BALL_MESSAGE ( 0 )
\r
168 #define mainWRITE_STRING_MESSAGE ( 1 )
\r
170 /* Type of the message sent to the LCD task. */
\r
173 portBASE_TYPE xMessageType;
\r
174 signed char *pcMessage;
\r
177 /*-----------------------------------------------------------*/
\r
180 * Configure the clocks, GPIO and other peripherals as required by the demo.
\r
182 static void prvSetupHardware( void );
\r
185 * The LCD is written two by more than one task so is controlled by a
\r
186 * 'gatekeeper' task. This is the only task that is actually permitted to
\r
187 * access the LCD directly. Other tasks wanting to display a message send
\r
188 * the message to the gatekeeper.
\r
190 static void prvLCDTask( void *pvParameters );
\r
193 * Checks the status of all the demo tasks then prints a message to the
\r
194 * display. The message will be either PASS - and include in brackets the
\r
195 * maximum measured jitter time (as described at the to of the file), or a
\r
196 * message that describes which of the standard demo tasks an error has been
\r
199 * Messages are not written directly to the terminal, but passed to prvLCDTask
\r
202 * The check task also receives instructions to update the MEMS input, which
\r
203 * in turn can also lead to the LCD being updated.
\r
205 static void prvCheckTask( void *pvParameters );
\r
208 * Configures the timers and interrupts for the fast interrupt test as
\r
209 * described at the top of this file.
\r
211 extern void vSetupTimerTest( void );
\r
214 * A cut down version of sprintf() used to percent the HUGE GCC library
\r
215 * equivalent from being included in the binary image.
\r
217 extern int sprintf(char *out, const char *format, ...);
\r
220 * Simple toggle the LED periodically for timing verification.
\r
222 static void prvFlashTask( void *pvParameters );
\r
224 /*-----------------------------------------------------------*/
\r
226 /* The queue used to send messages to the LCD task. */
\r
227 xQueueHandle xLCDQueue;
\r
229 /*-----------------------------------------------------------*/
\r
237 prvSetupHardware();
\r
239 /* Create the queue used by the LCD task. Messages for display on the LCD
\r
240 are received via this queue. */
\r
241 xLCDQueue = xQueueCreate( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );
\r
243 /* Start the standard demo tasks. */
\r
244 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
245 vCreateBlockTimeTasks();
\r
246 vStartGenericQueueTasks( mainGEN_Q_PRIORITY );
\r
247 vStartQueuePeekTasks();
\r
248 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
250 /* Start the tasks defined within this file/specific to this demo. */
\r
251 xTaskCreate( prvCheckTask, ( signed portCHAR * ) "Check", mainCHECK_TASK_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
252 xTaskCreate( prvLCDTask, ( signed portCHAR * ) "LCD", configLCD_TASK_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
253 xTaskCreate( prvFlashTask, ( signed portCHAR * ) "Flash", configMINIMAL_STACK_SIZE, NULL, mainFLASH_TASK_PRIORITY, NULL );
\r
255 /* Configure the timers used by the fast interrupt timer test. */
\r
258 /* Start the scheduler. */
\r
259 vTaskStartScheduler();
\r
261 /* Will only get here if there was not enough heap space to create the
\r
265 /*-----------------------------------------------------------*/
\r
267 void prvLCDTask( void *pvParameters )
\r
269 xLCDMessage xMessage;
\r
270 portCHAR cY = mainLCD_CHAR_HEIGHT;
\r
271 const portCHAR * const pcString = "www.FreeRTOS.org";
\r
272 const portCHAR * const pcBlankLine = " ";
\r
276 #if mainINCLUDE_BITMAP == 1
\r
277 DRAW_SetImage( pucImage, mainBITMAP_Y, mainBITMAP_X, bmpBITMAP_HEIGHT, bmpBITMAP_WIDTH );
\r
280 LCD_SetScreenOrientation( V9 );
\r
281 DRAW_DisplayString( mainURL_Y, mainURL_X, pcString, strlen( pcString ) );
\r
282 vTaskDelay( mainSPLASH_SCREEN_DELAY );
\r
283 LCD_FillRect( 0, 0, CHIP_SCREEN_WIDTH, CHIP_SCREEN_HEIGHT, RGB_WHITE );
\r
287 /* Wait for a message to arrive that requires displaying. */
\r
288 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
\r
290 /* Check the message type. */
\r
291 if( xMessage.xMessageType == mainUPDATE_BALL_MESSAGE )
\r
293 /* Read the MEMS and update the ball display on the LCD if required. */
\r
299 /* A text string was sent. First blank off the old text string, then
\r
300 draw the new text on the next line down. */
\r
301 DRAW_DisplayString( 0, cY, pcBlankLine, strlen( pcBlankLine ) );
\r
303 cY -= mainLCD_CHAR_HEIGHT;
\r
304 if( cY <= ( mainLCD_CHAR_HEIGHT - 1 ) )
\r
306 /* Wrap the line onto which we are going to write the text. */
\r
307 cY = mainLCD_MAX_Y;
\r
310 /* Display the message. */
\r
311 DRAW_DisplayString( 0, cY, xMessage.pcMessage, strlen( xMessage.pcMessage ) );
\r
315 /*-----------------------------------------------------------*/
\r
317 static void prvCheckTask( void *pvParameters )
\r
319 portTickType xLastExecutionTime;
\r
320 xLCDMessage xMessage;
\r
321 static signed portCHAR cPassMessage[ mainMAX_MSG_LEN ];
\r
322 extern unsigned portSHORT usMaxJitter;
\r
324 /* Initialise the xLastExecutionTime variable on task entry. */
\r
325 xLastExecutionTime = xTaskGetTickCount();
\r
327 /* Setup the message we are going to send to the LCD task. */
\r
328 xMessage.xMessageType = mainWRITE_STRING_MESSAGE;
\r
329 xMessage.pcMessage = cPassMessage;
\r
333 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
334 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
336 /* Has an error been found in any task? If so then point the text
\r
337 we are going to send to the LCD task to an error message instead of
\r
338 the PASS message. */
\r
339 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
341 xMessage.pcMessage = "ERROR IN GEN Q";
\r
343 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
345 xMessage.pcMessage = "ERROR IN BLOCK Q";
\r
347 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
349 xMessage.pcMessage = "ERROR IN BLOCK TIME";
\r
351 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
353 xMessage.pcMessage = "ERROR IN POLL Q";
\r
355 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
357 xMessage.pcMessage = "ERROR IN PEEK Q";
\r
361 /* No errors were found in any task, so send a pass message
\r
362 with the max measured jitter time also included (as per the
\r
363 fast interrupt test described at the top of this file and on
\r
364 the online documentation page for this demo application). */
\r
365 sprintf( ( portCHAR * ) cPassMessage, "PASS [%uns]", ( ( unsigned portLONG ) usMaxJitter ) * mainNS_PER_CLOCK );
\r
368 /* Send the message to the LCD gatekeeper for display. */
\r
369 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );
\r
372 /*-----------------------------------------------------------*/
\r
374 void vApplicationTickHook( void )
\r
376 static unsigned portLONG ulCallCount;
\r
377 static const xLCDMessage xMemsMessage = { mainUPDATE_BALL_MESSAGE, NULL };
\r
378 static portBASE_TYPE xHigherPriorityTaskWoken;
\r
380 /* Periodically send a message to the LCD task telling it to update
\r
381 the MEMS input, and then if necessary the LCD. */
\r
383 if( ulCallCount >= mainMEMS_DELAY )
\r
386 xHigherPriorityTaskWoken = pdFALSE;
\r
387 xQueueSendFromISR( xLCDQueue, &xMemsMessage, &xHigherPriorityTaskWoken );
\r
390 /*-----------------------------------------------------------*/
\r
392 static void prvSetupHardware( void )
\r
394 /* Start with the clocks in their expected state. */
\r
397 /* Enable HSE (high speed external clock). */
\r
398 RCC_HSEConfig( RCC_HSE_ON );
\r
400 /* Wait till HSE is ready. */
\r
401 while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )
\r
405 /* 2 wait states required on the flash. */
\r
406 *( ( unsigned portLONG * ) 0x40022000 ) = 0x02;
\r
408 /* HCLK = SYSCLK */
\r
409 RCC_HCLKConfig( RCC_SYSCLK_Div1 );
\r
412 RCC_PCLK2Config( RCC_HCLK_Div1 );
\r
414 /* PCLK1 = HCLK/2 */
\r
415 RCC_PCLK1Config( RCC_HCLK_Div2 );
\r
417 /* PLLCLK = 12MHz * 6 = 72 MHz. */
\r
418 RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_6 );
\r
421 RCC_PLLCmd( ENABLE );
\r
423 /* Wait till PLL is ready. */
\r
424 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
\r
428 /* Select PLL as system clock source. */
\r
429 RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
\r
431 /* Wait till PLL is used as system clock source. */
\r
432 while( RCC_GetSYSCLKSource() != 0x08 )
\r
436 /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
\r
437 RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
\r
438 | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );
\r
440 /* SPI2 Periph clock enable */
\r
441 RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );
\r
444 /* Set the Vector Table base address at 0x08000000 */
\r
445 NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );
\r
447 NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
\r
449 /* Configure HCLK clock as SysTick clock source. */
\r
450 SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
\r
452 /* Misc initialisation, including some of the CircleOS features. Note
\r
453 that CircleOS itself is not used. */
\r
454 vParTestInitialise();
\r
457 POINTER_SetMode( POINTER_RESTORE_LESS );
\r
459 /*-----------------------------------------------------------*/
\r
461 static void prvFlashTask( void *pvParameters )
\r
463 portTickType xLastExecutionTime;
\r
465 /* Initialise the xLastExecutionTime variable on task entry. */
\r
466 xLastExecutionTime = xTaskGetTickCount();
\r
470 /* Simple toggle the LED periodically. This just provides some timing
\r
472 vTaskDelayUntil( &xLastExecutionTime, mainFLASH_DELAY );
\r
473 vParTestToggleLED( 0 );
\r
476 /*-----------------------------------------------------------*/
\r
478 void starting_delay( unsigned long ul )
\r
480 vTaskDelay( ( portTickType ) ul );
\r