2 FreeRTOS.org V5.1.2 - Copyright (C) 2003-2009 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
27 ***************************************************************************
\r
29 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
31 * This is a concise, step by step, 'hands on' guide that describes both *
\r
32 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
33 * explains numerous examples that are written using the FreeRTOS API. *
\r
34 * Full source code for all the examples is provided in an accompanying *
\r
37 ***************************************************************************
\r
38 ***************************************************************************
\r
40 Please ensure to read the configuration and relevant port sections of the
\r
41 online documentation.
\r
43 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
46 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
49 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
50 licensing and training services.
\r
54 * This file contains a demo created to execute on the Rowley Associates
\r
55 * LPC2138 CrossFire development board.
\r
57 * main() creates all the demo application tasks, then starts the scheduler.
\r
58 * The WEB documentation provides more details of the standard demo application
\r
61 * Main.c also creates a task called "Check". This only executes every few
\r
62 * seconds but has a high priority so is guaranteed to get processor time.
\r
63 * Its function is to check that all the other tasks are still operational.
\r
64 * Each standard demo task maintains a unique count that is incremented each
\r
65 * time the task successfully completes its function. Should any error occur
\r
66 * within such a task the count is permanently halted. The check task inspects
\r
67 * the count of each task to ensure it has changed since the last time the
\r
68 * check task executed. If all the count variables have changed all the tasks
\r
69 * are still executing error free, and the check task writes "PASS" to the
\r
70 * CrossStudio terminal IO window. Should any task contain an error at any time
\r
71 * the error is latched and "FAIL" written to the terminal IO window.
\r
73 * Finally, main() sets up an interrupt service routine and task to handle
\r
74 * pushes of the button that is built into the CrossFire board. When the button
\r
75 * is pushed the ISR wakes the button task - which generates a table of task
\r
76 * status information which is also displayed on the terminal IO window.
\r
78 * A print task is defined to ensure exclusive and consistent access to the
\r
79 * terminal IO. This is the only task that is allowed to access the terminal.
\r
80 * The check and button task therefore do not access the terminal directly but
\r
81 * instead pass a pointer to the message they wish to display to the print task.
\r
84 /* Standard includes. */
\r
85 #include <__cross_studio_io.h>
\r
87 /* Scheduler includes. */
\r
88 #include "FreeRTOS.h"
\r
93 /* Demo app includes. */
\r
96 #include "dynamic.h"
\r
97 #include "integer.h"
\r
99 #include "blocktim.h"
\r
100 #include "recmutex.h"
\r
102 /* Hardware configuration definitions. */
\r
103 #define mainBUS_CLK_FULL ( ( unsigned portCHAR ) 0x01 )
\r
104 #define mainLED_BIT 0x80000000
\r
105 #define mainP0_14__EINT_1 ( 2 << 28 )
\r
106 #define mainEINT_1_EDGE_SENSITIVE 2
\r
107 #define mainEINT_1_FALLING_EDGE_SENSITIVE 0
\r
108 #define mainEINT_1_CHANNEL 15
\r
109 #define mainEINT_1_VIC_CHANNEL_BIT ( 1 << mainEINT_1_CHANNEL )
\r
110 #define mainEINT_1_ENABLE_BIT ( 1 << 5 )
\r
112 /* Demo application definitions. */
\r
113 #define mainQUEUE_SIZE ( 3 )
\r
114 #define mainLED_DELAY ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
115 #define mainERROR_LED_DELAY ( ( portTickType ) 50 / portTICK_RATE_MS )
\r
116 #define mainCHECK_DELAY ( ( portTickType ) 5000 / portTICK_RATE_MS )
\r
117 #define mainLIST_BUFFER_SIZE 2048
\r
118 #define mainNO_DELAY ( 0 )
\r
119 #define mainSHORT_DELAY ( 150 / portTICK_RATE_MS )
\r
121 /* Task priorities. */
\r
122 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
123 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
124 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
125 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
126 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
127 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
129 /*-----------------------------------------------------------*/
\r
131 /* The semaphore used to wake the button task from within the external interrupt
\r
133 xSemaphoreHandle xButtonSemaphore;
\r
135 /* The queue that is used to send message to vPrintTask for display in the
\r
136 terminal output window. */
\r
137 xQueueHandle xPrintQueue;
\r
139 /* The rate at which the LED will toggle. The toggle rate increases if an
\r
140 error is detected in any task. */
\r
141 static portTickType xLED_Delay = mainLED_DELAY;
\r
142 /*-----------------------------------------------------------*/
\r
145 * Simply flashes the on board LED every mainLED_DELAY milliseconds.
\r
147 static void vLEDTask( void *pvParameters );
\r
150 * Checks the status of all the demo tasks then prints a message to the
\r
151 * CrossStudio terminal IO windows. The message will be either PASS or FAIL
\r
152 * depending on the status of the demo applications tasks. A FAIL status will
\r
155 * Messages are not written directly to the terminal, but passed to vPrintTask
\r
158 static void vCheckTask( void *pvParameters );
\r
161 * Controls all terminal output. If a task wants to send a message to the
\r
162 * terminal IO it posts a pointer to the text to vPrintTask via a queue. This
\r
163 * ensures serial access to the terminal IO.
\r
165 static void vPrintTask( void *pvParameter );
\r
168 * Simply waits for an interrupt to be generated from the built in button, then
\r
169 * generates a table of tasks states that is then written by vPrintTask to the
\r
170 * terminal output window within CrossStudio.
\r
172 static void vButtonHandlerTask( void *pvParameters );
\r
174 /*-----------------------------------------------------------*/
\r
178 /* Setup the peripheral bus to be the same as the PLL output. */
\r
179 VPBDIV = mainBUS_CLK_FULL;
\r
181 /* Create the queue used to pass message to vPrintTask. */
\r
182 xPrintQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( portCHAR * ) );
\r
184 /* Create the semaphore used to wake vButtonHandlerTask(). */
\r
185 vSemaphoreCreateBinary( xButtonSemaphore );
\r
186 xSemaphoreTake( xButtonSemaphore, 0 );
\r
188 /* Start the standard demo tasks. */
\r
189 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
190 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
191 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
192 vStartDynamicPriorityTasks();
\r
193 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
195 #if configUSE_PREEMPTION == 1
\r
197 /* The timing of console output when not using the preemptive
\r
198 scheduler causes the block time tests to detect a timing problem. */
\r
199 vCreateBlockTimeTasks();
\r
203 vStartRecursiveMutexTasks();
\r
205 /* Start the tasks defined within this file. */
\r
206 xTaskCreate( vLEDTask, "LED", configMINIMAL_STACK_SIZE, NULL, mainLED_TASK_PRIORITY, NULL );
\r
207 xTaskCreate( vCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
208 xTaskCreate( vPrintTask, "Print", configMINIMAL_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
209 xTaskCreate( vButtonHandlerTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
211 /* Start the scheduler. */
\r
212 vTaskStartScheduler();
\r
214 /* The scheduler should now be running, so we will only ever reach here if we
\r
215 ran out of heap space. */
\r
219 /*-----------------------------------------------------------*/
\r
221 static void vLEDTask( void *pvParameters )
\r
223 /* Configure IO. */
\r
224 IO0DIR |= mainLED_BIT;
\r
225 IO0SET = mainLED_BIT;
\r
229 /* Not very exiting - just delay... */
\r
230 vTaskDelay( xLED_Delay );
\r
232 /* ...set the IO ... */
\r
233 IO0CLR = mainLED_BIT;
\r
235 /* ...delay again... */
\r
236 vTaskDelay( xLED_Delay );
\r
238 /* ...then clear the IO. */
\r
239 IO0SET = mainLED_BIT;
\r
242 /*-----------------------------------------------------------*/
\r
244 static void vCheckTask( void *pvParameters )
\r
246 portBASE_TYPE xErrorOccurred = pdFALSE;
\r
247 portTickType xLastExecutionTime;
\r
248 const portCHAR * const pcPassMessage = "PASS\n";
\r
249 const portCHAR * const pcFailMessage = "FAIL\n";
\r
251 /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
\r
252 works correctly. */
\r
253 xLastExecutionTime = xTaskGetTickCount();
\r
257 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
258 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
260 /* Has an error been found in any task? */
\r
262 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
264 xErrorOccurred = pdTRUE;
\r
267 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
269 xErrorOccurred = pdTRUE;
\r
272 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
274 xErrorOccurred = pdTRUE;
\r
277 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
279 xErrorOccurred = pdTRUE;
\r
282 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
284 xErrorOccurred = pdTRUE;
\r
287 #if configUSE_PREEMPTION == 1
\r
289 /* The timing of console output when not using the preemptive
\r
290 scheduler causes the block time tests to detect a timing problem. */
\r
291 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
293 xErrorOccurred = pdTRUE;
\r
298 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
300 xErrorOccurred = pdTRUE;
\r
303 /* Send either a pass or fail message. If an error is found it is
\r
304 never cleared again. */
\r
305 if( xErrorOccurred == pdTRUE )
\r
307 xLED_Delay = mainERROR_LED_DELAY;
\r
308 xQueueSend( xPrintQueue, &pcFailMessage, portMAX_DELAY );
\r
312 xQueueSend( xPrintQueue, &pcPassMessage, portMAX_DELAY );
\r
316 /*-----------------------------------------------------------*/
\r
318 static void vPrintTask( void *pvParameters )
\r
320 portCHAR *pcMessage;
\r
324 /* Wait for a message to arrive. */
\r
325 while( xQueueReceive( xPrintQueue, &pcMessage, portMAX_DELAY ) != pdPASS );
\r
327 /* Write the message to the terminal IO. */
\r
329 debug_printf( "%s", pcMessage );
\r
333 /*-----------------------------------------------------------*/
\r
335 static void vButtonHandlerTask( void *pvParameters )
\r
337 static portCHAR cListBuffer[ mainLIST_BUFFER_SIZE ];
\r
338 const portCHAR *pcList = &( cListBuffer[ 0 ] );
\r
339 const portCHAR * const pcHeader = "\nTask State Priority Stack #\n************************************************";
\r
340 extern void (vButtonISRWrapper) ( void );
\r
342 /* Configure the interrupt. */
\r
343 portENTER_CRITICAL();
\r
345 /* Configure P0.14 to generate interrupts. */
\r
346 PINSEL0 |= mainP0_14__EINT_1;
\r
347 EXTMODE = mainEINT_1_EDGE_SENSITIVE;
\r
348 EXTPOLAR = mainEINT_1_FALLING_EDGE_SENSITIVE;
\r
350 /* Setup the VIC for EINT 1. */
\r
351 VICIntSelect &= ~mainEINT_1_VIC_CHANNEL_BIT;
\r
352 VICIntEnable |= mainEINT_1_VIC_CHANNEL_BIT;
\r
353 VICVectAddr1 = ( portLONG ) vButtonISRWrapper;
\r
354 VICVectCntl1 = mainEINT_1_ENABLE_BIT | mainEINT_1_CHANNEL;
\r
356 portEXIT_CRITICAL();
\r
360 /* For debouncing, wait a while then clear the semaphore. */
\r
361 vTaskDelay( mainSHORT_DELAY );
\r
362 xSemaphoreTake( xButtonSemaphore, mainNO_DELAY );
\r
364 /* Wait for an interrupt. */
\r
365 xSemaphoreTake( xButtonSemaphore, portMAX_DELAY );
\r
367 /* Send the column headers to the print task for display. */
\r
368 xQueueSend( xPrintQueue, &pcHeader, portMAX_DELAY );
\r
370 /* Create the list of task states. */
\r
371 vTaskList( cListBuffer );
\r
373 /* Send the task status information to the print task for display. */
\r
374 xQueueSend( xPrintQueue, &pcList, portMAX_DELAY );
\r
377 /*-----------------------------------------------------------*/
\r