2 FreeRTOS V7.0.1 - Copyright (C) 2011 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 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 * This file contains a demo created to execute on the Rowley Associates
\r
56 * LPC2138 CrossFire development board.
\r
58 * main() creates all the demo application tasks, then starts the scheduler.
\r
59 * The WEB documentation provides more details of the standard demo application
\r
62 * Main.c also creates a task called "Check". This only executes every few
\r
63 * seconds but has a high priority so is guaranteed to get processor time.
\r
64 * Its function is to check that all the other tasks are still operational.
\r
65 * Each standard demo task maintains a unique count that is incremented each
\r
66 * time the task successfully completes its function. Should any error occur
\r
67 * within such a task the count is permanently halted. The check task inspects
\r
68 * the count of each task to ensure it has changed since the last time the
\r
69 * check task executed. If all the count variables have changed all the tasks
\r
70 * are still executing error free, and the check task writes "PASS" to the
\r
71 * CrossStudio terminal IO window. Should any task contain an error at any time
\r
72 * the error is latched and "FAIL" written to the terminal IO window.
\r
74 * Finally, main() sets up an interrupt service routine and task to handle
\r
75 * pushes of the button that is built into the CrossFire board. When the button
\r
76 * is pushed the ISR wakes the button task - which generates a table of task
\r
77 * status information which is also displayed on the terminal IO window.
\r
79 * A print task is defined to ensure exclusive and consistent access to the
\r
80 * terminal IO. This is the only task that is allowed to access the terminal.
\r
81 * The check and button task therefore do not access the terminal directly but
\r
82 * instead pass a pointer to the message they wish to display to the print task.
\r
85 /* Standard includes. */
\r
86 #include <__cross_studio_io.h>
\r
88 /* Scheduler includes. */
\r
89 #include "FreeRTOS.h"
\r
94 /* Demo app includes. */
\r
97 #include "dynamic.h"
\r
98 #include "integer.h"
\r
100 #include "blocktim.h"
\r
101 #include "recmutex.h"
\r
102 #include "semtest.h"
\r
104 /* Hardware configuration definitions. */
\r
105 #define mainBUS_CLK_FULL ( ( unsigned char ) 0x01 )
\r
106 #define mainLED_BIT 0x80000000
\r
107 #define mainP0_14__EINT_1 ( 2 << 28 )
\r
108 #define mainEINT_1_EDGE_SENSITIVE 2
\r
109 #define mainEINT_1_FALLING_EDGE_SENSITIVE 0
\r
110 #define mainEINT_1_CHANNEL 15
\r
111 #define mainEINT_1_VIC_CHANNEL_BIT ( 1 << mainEINT_1_CHANNEL )
\r
112 #define mainEINT_1_ENABLE_BIT ( 1 << 5 )
\r
114 /* Demo application definitions. */
\r
115 #define mainQUEUE_SIZE ( 3 )
\r
116 #define mainLED_DELAY ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
117 #define mainERROR_LED_DELAY ( ( portTickType ) 50 / portTICK_RATE_MS )
\r
118 #define mainCHECK_DELAY ( ( portTickType ) 5000 / portTICK_RATE_MS )
\r
119 #define mainLIST_BUFFER_SIZE 2048
\r
120 #define mainNO_DELAY ( 0 )
\r
121 #define mainSHORT_DELAY ( 150 / portTICK_RATE_MS )
\r
123 /* Task priorities. */
\r
124 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
125 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
126 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
127 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
128 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
129 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
131 /*-----------------------------------------------------------*/
\r
133 /* The semaphore used to wake the button task from within the external interrupt
\r
135 xSemaphoreHandle xButtonSemaphore;
\r
137 /* The queue that is used to send message to vPrintTask for display in the
\r
138 terminal output window. */
\r
139 xQueueHandle xPrintQueue;
\r
141 /* The rate at which the LED will toggle. The toggle rate increases if an
\r
142 error is detected in any task. */
\r
143 static portTickType xLED_Delay = mainLED_DELAY;
\r
144 /*-----------------------------------------------------------*/
\r
147 * Simply flashes the on board LED every mainLED_DELAY milliseconds.
\r
149 static void vLEDTask( void *pvParameters );
\r
152 * Checks the status of all the demo tasks then prints a message to the
\r
153 * CrossStudio terminal IO windows. The message will be either PASS or FAIL
\r
154 * depending on the status of the demo applications tasks. A FAIL status will
\r
157 * Messages are not written directly to the terminal, but passed to vPrintTask
\r
160 static void vCheckTask( void *pvParameters );
\r
163 * Controls all terminal output. If a task wants to send a message to the
\r
164 * terminal IO it posts a pointer to the text to vPrintTask via a queue. This
\r
165 * ensures serial access to the terminal IO.
\r
167 static void vPrintTask( void *pvParameter );
\r
170 * Simply waits for an interrupt to be generated from the built in button, then
\r
171 * generates a table of tasks states that is then written by vPrintTask to the
\r
172 * terminal output window within CrossStudio.
\r
174 static void vButtonHandlerTask( void *pvParameters );
\r
176 /*-----------------------------------------------------------*/
\r
180 /* Setup the peripheral bus to be the same as the PLL output. */
\r
181 VPBDIV = mainBUS_CLK_FULL;
\r
183 /* Create the queue used to pass message to vPrintTask. */
\r
184 xPrintQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( char * ) );
\r
186 /* Create the semaphore used to wake vButtonHandlerTask(). */
\r
187 vSemaphoreCreateBinary( xButtonSemaphore );
\r
188 xSemaphoreTake( xButtonSemaphore, 0 );
\r
190 /* Start the standard demo tasks. */
\r
191 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
192 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
193 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
194 vStartDynamicPriorityTasks();
\r
195 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
197 #if configUSE_PREEMPTION == 1
\r
199 /* The timing of console output when not using the preemptive
\r
200 scheduler causes the block time tests to detect a timing problem. */
\r
201 vCreateBlockTimeTasks();
\r
205 vStartRecursiveMutexTasks();
\r
207 /* Start the tasks defined within this file. */
\r
208 xTaskCreate( vLEDTask, ( signed char * ) "LED", configMINIMAL_STACK_SIZE, NULL, mainLED_TASK_PRIORITY, NULL );
\r
209 xTaskCreate( vCheckTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
210 xTaskCreate( vPrintTask, ( signed char * ) "Print", configMINIMAL_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
211 xTaskCreate( vButtonHandlerTask, ( signed char * ) "Button", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
213 /* Start the scheduler. */
\r
214 vTaskStartScheduler();
\r
216 /* The scheduler should now be running, so we will only ever reach here if we
\r
217 ran out of heap space. */
\r
221 /*-----------------------------------------------------------*/
\r
223 static void vLEDTask( void *pvParameters )
\r
225 /* Just to remove compiler warnings. */
\r
226 ( void ) pvParameters;
\r
228 /* Configure IO. */
\r
229 IO0DIR |= mainLED_BIT;
\r
230 IO0SET = mainLED_BIT;
\r
234 /* Not very exiting - just delay... */
\r
235 vTaskDelay( xLED_Delay );
\r
237 /* ...set the IO ... */
\r
238 IO0CLR = mainLED_BIT;
\r
240 /* ...delay again... */
\r
241 vTaskDelay( xLED_Delay );
\r
243 /* ...then clear the IO. */
\r
244 IO0SET = mainLED_BIT;
\r
247 /*-----------------------------------------------------------*/
\r
249 static void vCheckTask( void *pvParameters )
\r
251 portBASE_TYPE xErrorOccurred = pdFALSE;
\r
252 portTickType xLastExecutionTime;
\r
253 const char * const pcPassMessage = "PASS\n";
\r
254 const char * const pcFailMessage = "FAIL\n";
\r
256 /* Just to remove compiler warnings. */
\r
257 ( void ) pvParameters;
\r
259 /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
\r
260 works correctly. */
\r
261 xLastExecutionTime = xTaskGetTickCount();
\r
265 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
266 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
268 /* Has an error been found in any task? */
\r
270 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
272 xErrorOccurred = pdTRUE;
\r
275 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
277 xErrorOccurred = pdTRUE;
\r
280 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
282 xErrorOccurred = pdTRUE;
\r
285 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
287 xErrorOccurred = pdTRUE;
\r
290 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
292 xErrorOccurred = pdTRUE;
\r
295 #if configUSE_PREEMPTION == 1
\r
297 /* The timing of console output when not using the preemptive
\r
298 scheduler causes the block time tests to detect a timing problem. */
\r
299 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
301 xErrorOccurred = pdTRUE;
\r
306 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
308 xErrorOccurred = pdTRUE;
\r
311 /* Send either a pass or fail message. If an error is found it is
\r
312 never cleared again. */
\r
313 if( xErrorOccurred == pdTRUE )
\r
315 xLED_Delay = mainERROR_LED_DELAY;
\r
316 xQueueSend( xPrintQueue, &pcFailMessage, portMAX_DELAY );
\r
320 xQueueSend( xPrintQueue, &pcPassMessage, portMAX_DELAY );
\r
324 /*-----------------------------------------------------------*/
\r
326 static void vPrintTask( void *pvParameters )
\r
330 /* Just to stop compiler warnings. */
\r
331 ( void ) pvParameters;
\r
335 /* Wait for a message to arrive. */
\r
336 while( xQueueReceive( xPrintQueue, &pcMessage, portMAX_DELAY ) != pdPASS );
\r
338 /* Write the message to the terminal IO. */
\r
340 debug_printf( "%s", pcMessage );
\r
344 /*-----------------------------------------------------------*/
\r
346 static void vButtonHandlerTask( void *pvParameters )
\r
348 static signed char cListBuffer[ mainLIST_BUFFER_SIZE ];
\r
349 const signed char *pcList = &( cListBuffer[ 0 ] );
\r
350 const char * const pcHeader = "\nTask State Priority Stack #\n************************************************";
\r
351 extern void (vButtonISRWrapper) ( void );
\r
353 /* Just to stop compiler warnings. */
\r
354 ( void ) pvParameters;
\r
356 /* Configure the interrupt. */
\r
357 portENTER_CRITICAL();
\r
359 /* Configure P0.14 to generate interrupts. */
\r
360 PINSEL0 |= mainP0_14__EINT_1;
\r
361 EXTMODE = mainEINT_1_EDGE_SENSITIVE;
\r
362 EXTPOLAR = mainEINT_1_FALLING_EDGE_SENSITIVE;
\r
364 /* Setup the VIC for EINT 1. */
\r
365 VICIntSelect &= ~mainEINT_1_VIC_CHANNEL_BIT;
\r
366 VICIntEnable |= mainEINT_1_VIC_CHANNEL_BIT;
\r
367 VICVectAddr1 = ( long ) vButtonISRWrapper;
\r
368 VICVectCntl1 = mainEINT_1_ENABLE_BIT | mainEINT_1_CHANNEL;
\r
370 portEXIT_CRITICAL();
\r
374 /* For debouncing, wait a while then clear the semaphore. */
\r
375 vTaskDelay( mainSHORT_DELAY );
\r
376 xSemaphoreTake( xButtonSemaphore, mainNO_DELAY );
\r
378 /* Wait for an interrupt. */
\r
379 xSemaphoreTake( xButtonSemaphore, portMAX_DELAY );
\r
381 /* Send the column headers to the print task for display. */
\r
382 xQueueSend( xPrintQueue, &pcHeader, portMAX_DELAY );
\r
384 /* Create the list of task states. */
\r
385 vTaskList( cListBuffer );
\r
387 /* Send the task status information to the print task for display. */
\r
388 xQueueSend( xPrintQueue, &pcList, portMAX_DELAY );
\r
391 /*-----------------------------------------------------------*/
\r
393 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
\r
395 /* Check pcTaskName for the name of the offending task, or pxCurrentTCB
\r
396 if pcTaskName has itself been corrupted. */
\r
398 ( void ) pcTaskName;
\r