2 FreeRTOS V7.2.0 - Copyright (C) 2012 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
68 * This file contains a demo created to execute on the Rowley Associates
\r
69 * LPC2138 CrossFire development board.
\r
71 * main() creates all the demo application tasks, then starts the scheduler.
\r
72 * The WEB documentation provides more details of the standard demo application
\r
75 * Main.c also creates a task called "Check". This only executes every few
\r
76 * seconds but has a high priority so is guaranteed to get processor time.
\r
77 * Its function is to check that all the other tasks are still operational.
\r
78 * Each standard demo task maintains a unique count that is incremented each
\r
79 * time the task successfully completes its function. Should any error occur
\r
80 * within such a task the count is permanently halted. The check task inspects
\r
81 * the count of each task to ensure it has changed since the last time the
\r
82 * check task executed. If all the count variables have changed all the tasks
\r
83 * are still executing error free, and the check task writes "PASS" to the
\r
84 * CrossStudio terminal IO window. Should any task contain an error at any time
\r
85 * the error is latched and "FAIL" written to the terminal IO window.
\r
87 * Finally, main() sets up an interrupt service routine and task to handle
\r
88 * pushes of the button that is built into the CrossFire board. When the button
\r
89 * is pushed the ISR wakes the button task - which generates a table of task
\r
90 * status information which is also displayed on the terminal IO window.
\r
92 * A print task is defined to ensure exclusive and consistent access to the
\r
93 * terminal IO. This is the only task that is allowed to access the terminal.
\r
94 * The check and button task therefore do not access the terminal directly but
\r
95 * instead pass a pointer to the message they wish to display to the print task.
\r
98 /* Standard includes. */
\r
99 #include <__cross_studio_io.h>
\r
101 /* Scheduler includes. */
\r
102 #include "FreeRTOS.h"
\r
105 #include "semphr.h"
\r
107 /* Demo app includes. */
\r
108 #include "BlockQ.h"
\r
110 #include "dynamic.h"
\r
111 #include "integer.h"
\r
113 #include "blocktim.h"
\r
114 #include "recmutex.h"
\r
115 #include "semtest.h"
\r
117 /* Hardware configuration definitions. */
\r
118 #define mainBUS_CLK_FULL ( ( unsigned char ) 0x01 )
\r
119 #define mainLED_BIT 0x80000000
\r
120 #define mainP0_14__EINT_1 ( 2 << 28 )
\r
121 #define mainEINT_1_EDGE_SENSITIVE 2
\r
122 #define mainEINT_1_FALLING_EDGE_SENSITIVE 0
\r
123 #define mainEINT_1_CHANNEL 15
\r
124 #define mainEINT_1_VIC_CHANNEL_BIT ( 1 << mainEINT_1_CHANNEL )
\r
125 #define mainEINT_1_ENABLE_BIT ( 1 << 5 )
\r
127 /* Demo application definitions. */
\r
128 #define mainQUEUE_SIZE ( 3 )
\r
129 #define mainLED_DELAY ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
130 #define mainERROR_LED_DELAY ( ( portTickType ) 50 / portTICK_RATE_MS )
\r
131 #define mainCHECK_DELAY ( ( portTickType ) 5000 / portTICK_RATE_MS )
\r
132 #define mainLIST_BUFFER_SIZE 2048
\r
133 #define mainNO_DELAY ( 0 )
\r
134 #define mainSHORT_DELAY ( 150 / portTICK_RATE_MS )
\r
136 /* Task priorities. */
\r
137 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
138 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
139 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
140 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
141 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
142 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
144 /*-----------------------------------------------------------*/
\r
146 /* The semaphore used to wake the button task from within the external interrupt
\r
148 xSemaphoreHandle xButtonSemaphore;
\r
150 /* The queue that is used to send message to vPrintTask for display in the
\r
151 terminal output window. */
\r
152 xQueueHandle xPrintQueue;
\r
154 /* The rate at which the LED will toggle. The toggle rate increases if an
\r
155 error is detected in any task. */
\r
156 static portTickType xLED_Delay = mainLED_DELAY;
\r
157 /*-----------------------------------------------------------*/
\r
160 * Simply flashes the on board LED every mainLED_DELAY milliseconds.
\r
162 static void vLEDTask( void *pvParameters );
\r
165 * Checks the status of all the demo tasks then prints a message to the
\r
166 * CrossStudio terminal IO windows. The message will be either PASS or FAIL
\r
167 * depending on the status of the demo applications tasks. A FAIL status will
\r
170 * Messages are not written directly to the terminal, but passed to vPrintTask
\r
173 static void vCheckTask( void *pvParameters );
\r
176 * Controls all terminal output. If a task wants to send a message to the
\r
177 * terminal IO it posts a pointer to the text to vPrintTask via a queue. This
\r
178 * ensures serial access to the terminal IO.
\r
180 static void vPrintTask( void *pvParameter );
\r
183 * Simply waits for an interrupt to be generated from the built in button, then
\r
184 * generates a table of tasks states that is then written by vPrintTask to the
\r
185 * terminal output window within CrossStudio.
\r
187 static void vButtonHandlerTask( void *pvParameters );
\r
189 /*-----------------------------------------------------------*/
\r
193 /* Setup the peripheral bus to be the same as the PLL output. */
\r
194 VPBDIV = mainBUS_CLK_FULL;
\r
196 /* Create the queue used to pass message to vPrintTask. */
\r
197 xPrintQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( char * ) );
\r
199 /* Create the semaphore used to wake vButtonHandlerTask(). */
\r
200 vSemaphoreCreateBinary( xButtonSemaphore );
\r
201 xSemaphoreTake( xButtonSemaphore, 0 );
\r
203 /* Start the standard demo tasks. */
\r
204 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
205 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
206 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
207 vStartDynamicPriorityTasks();
\r
208 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
210 #if configUSE_PREEMPTION == 1
\r
212 /* The timing of console output when not using the preemptive
\r
213 scheduler causes the block time tests to detect a timing problem. */
\r
214 vCreateBlockTimeTasks();
\r
218 vStartRecursiveMutexTasks();
\r
220 /* Start the tasks defined within this file. */
\r
221 xTaskCreate( vLEDTask, ( signed char * ) "LED", configMINIMAL_STACK_SIZE, NULL, mainLED_TASK_PRIORITY, NULL );
\r
222 xTaskCreate( vCheckTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
223 xTaskCreate( vPrintTask, ( signed char * ) "Print", configMINIMAL_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
224 xTaskCreate( vButtonHandlerTask, ( signed char * ) "Button", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
226 /* Start the scheduler. */
\r
227 vTaskStartScheduler();
\r
229 /* The scheduler should now be running, so we will only ever reach here if we
\r
230 ran out of heap space. */
\r
234 /*-----------------------------------------------------------*/
\r
236 static void vLEDTask( void *pvParameters )
\r
238 /* Just to remove compiler warnings. */
\r
239 ( void ) pvParameters;
\r
241 /* Configure IO. */
\r
242 IO0DIR |= mainLED_BIT;
\r
243 IO0SET = mainLED_BIT;
\r
247 /* Not very exiting - just delay... */
\r
248 vTaskDelay( xLED_Delay );
\r
250 /* ...set the IO ... */
\r
251 IO0CLR = mainLED_BIT;
\r
253 /* ...delay again... */
\r
254 vTaskDelay( xLED_Delay );
\r
256 /* ...then clear the IO. */
\r
257 IO0SET = mainLED_BIT;
\r
260 /*-----------------------------------------------------------*/
\r
262 static void vCheckTask( void *pvParameters )
\r
264 portBASE_TYPE xErrorOccurred = pdFALSE;
\r
265 portTickType xLastExecutionTime;
\r
266 const char * const pcPassMessage = "PASS\n";
\r
267 const char * const pcFailMessage = "FAIL\n";
\r
269 /* Just to remove compiler warnings. */
\r
270 ( void ) pvParameters;
\r
272 /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
\r
273 works correctly. */
\r
274 xLastExecutionTime = xTaskGetTickCount();
\r
278 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
279 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
281 /* Has an error been found in any task? */
\r
283 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
285 xErrorOccurred = pdTRUE;
\r
288 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
290 xErrorOccurred = pdTRUE;
\r
293 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
295 xErrorOccurred = pdTRUE;
\r
298 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
300 xErrorOccurred = pdTRUE;
\r
303 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
305 xErrorOccurred = pdTRUE;
\r
308 #if configUSE_PREEMPTION == 1
\r
310 /* The timing of console output when not using the preemptive
\r
311 scheduler causes the block time tests to detect a timing problem. */
\r
312 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
314 xErrorOccurred = pdTRUE;
\r
319 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
321 xErrorOccurred = pdTRUE;
\r
324 /* Send either a pass or fail message. If an error is found it is
\r
325 never cleared again. */
\r
326 if( xErrorOccurred == pdTRUE )
\r
328 xLED_Delay = mainERROR_LED_DELAY;
\r
329 xQueueSend( xPrintQueue, &pcFailMessage, portMAX_DELAY );
\r
333 xQueueSend( xPrintQueue, &pcPassMessage, portMAX_DELAY );
\r
337 /*-----------------------------------------------------------*/
\r
339 static void vPrintTask( void *pvParameters )
\r
343 /* Just to stop compiler warnings. */
\r
344 ( void ) pvParameters;
\r
348 /* Wait for a message to arrive. */
\r
349 while( xQueueReceive( xPrintQueue, &pcMessage, portMAX_DELAY ) != pdPASS );
\r
351 /* Write the message to the terminal IO. */
\r
353 debug_printf( "%s", pcMessage );
\r
357 /*-----------------------------------------------------------*/
\r
359 static void vButtonHandlerTask( void *pvParameters )
\r
361 static signed char cListBuffer[ mainLIST_BUFFER_SIZE ];
\r
362 const signed char *pcList = &( cListBuffer[ 0 ] );
\r
363 const char * const pcHeader = "\nTask State Priority Stack #\n************************************************";
\r
364 extern void (vButtonISRWrapper) ( void );
\r
366 /* Just to stop compiler warnings. */
\r
367 ( void ) pvParameters;
\r
369 /* Configure the interrupt. */
\r
370 portENTER_CRITICAL();
\r
372 /* Configure P0.14 to generate interrupts. */
\r
373 PINSEL0 |= mainP0_14__EINT_1;
\r
374 EXTMODE = mainEINT_1_EDGE_SENSITIVE;
\r
375 EXTPOLAR = mainEINT_1_FALLING_EDGE_SENSITIVE;
\r
377 /* Setup the VIC for EINT 1. */
\r
378 VICIntSelect &= ~mainEINT_1_VIC_CHANNEL_BIT;
\r
379 VICIntEnable |= mainEINT_1_VIC_CHANNEL_BIT;
\r
380 VICVectAddr1 = ( long ) vButtonISRWrapper;
\r
381 VICVectCntl1 = mainEINT_1_ENABLE_BIT | mainEINT_1_CHANNEL;
\r
383 portEXIT_CRITICAL();
\r
387 /* For debouncing, wait a while then clear the semaphore. */
\r
388 vTaskDelay( mainSHORT_DELAY );
\r
389 xSemaphoreTake( xButtonSemaphore, mainNO_DELAY );
\r
391 /* Wait for an interrupt. */
\r
392 xSemaphoreTake( xButtonSemaphore, portMAX_DELAY );
\r
394 /* Send the column headers to the print task for display. */
\r
395 xQueueSend( xPrintQueue, &pcHeader, portMAX_DELAY );
\r
397 /* Create the list of task states. */
\r
398 vTaskList( cListBuffer );
\r
400 /* Send the task status information to the print task for display. */
\r
401 xQueueSend( xPrintQueue, &pcList, portMAX_DELAY );
\r
404 /*-----------------------------------------------------------*/
\r
406 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
\r
408 /* Check pcTaskName for the name of the offending task, or pxCurrentTCB
\r
409 if pcTaskName has itself been corrupted. */
\r
411 ( void ) pcTaskName;
\r