2 FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 * This file contains a demo created to execute on the Rowley Associates
\r
67 * LPC2138 CrossFire development board.
\r
69 * main() creates all the demo application tasks, then starts the scheduler.
\r
70 * The WEB documentation provides more details of the standard demo application
\r
73 * Main.c also creates a task called "Check". This only executes every few
\r
74 * seconds but has a high priority so is guaranteed to get processor time.
\r
75 * Its function is to check that all the other tasks are still operational.
\r
76 * Each standard demo task maintains a unique count that is incremented each
\r
77 * time the task successfully completes its function. Should any error occur
\r
78 * within such a task the count is permanently halted. The check task inspects
\r
79 * the count of each task to ensure it has changed since the last time the
\r
80 * check task executed. If all the count variables have changed all the tasks
\r
81 * are still executing error free, and the check task writes "PASS" to the
\r
82 * CrossStudio terminal IO window. Should any task contain an error at any time
\r
83 * the error is latched and "FAIL" written to the terminal IO window.
\r
85 * Finally, main() sets up an interrupt service routine and task to handle
\r
86 * pushes of the button that is built into the CrossFire board. When the button
\r
87 * is pushed the ISR wakes the button task - which generates a table of task
\r
88 * status information which is also displayed on the terminal IO window.
\r
90 * A print task is defined to ensure exclusive and consistent access to the
\r
91 * terminal IO. This is the only task that is allowed to access the terminal.
\r
92 * The check and button task therefore do not access the terminal directly but
\r
93 * instead pass a pointer to the message they wish to display to the print task.
\r
96 /* Standard includes. */
\r
97 #include <__cross_studio_io.h>
\r
99 /* Scheduler includes. */
\r
100 #include "FreeRTOS.h"
\r
103 #include "semphr.h"
\r
105 /* Demo app includes. */
\r
106 #include "BlockQ.h"
\r
108 #include "dynamic.h"
\r
109 #include "integer.h"
\r
111 #include "blocktim.h"
\r
112 #include "recmutex.h"
\r
113 #include "semtest.h"
\r
115 /* Hardware configuration definitions. */
\r
116 #define mainBUS_CLK_FULL ( ( unsigned char ) 0x01 )
\r
117 #define mainLED_BIT 0x80000000
\r
118 #define mainP0_14__EINT_1 ( 2 << 28 )
\r
119 #define mainEINT_1_EDGE_SENSITIVE 2
\r
120 #define mainEINT_1_FALLING_EDGE_SENSITIVE 0
\r
121 #define mainEINT_1_CHANNEL 15
\r
122 #define mainEINT_1_VIC_CHANNEL_BIT ( 1 << mainEINT_1_CHANNEL )
\r
123 #define mainEINT_1_ENABLE_BIT ( 1 << 5 )
\r
125 /* Demo application definitions. */
\r
126 #define mainQUEUE_SIZE ( 3 )
\r
127 #define mainLED_DELAY ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
128 #define mainERROR_LED_DELAY ( ( portTickType ) 50 / portTICK_RATE_MS )
\r
129 #define mainCHECK_DELAY ( ( portTickType ) 5000 / portTICK_RATE_MS )
\r
130 #define mainLIST_BUFFER_SIZE 2048
\r
131 #define mainNO_DELAY ( 0 )
\r
132 #define mainSHORT_DELAY ( 150 / portTICK_RATE_MS )
\r
134 /* Task priorities. */
\r
135 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
136 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
137 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
138 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
139 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
140 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
142 /*-----------------------------------------------------------*/
\r
144 /* The semaphore used to wake the button task from within the external interrupt
\r
146 xSemaphoreHandle xButtonSemaphore;
\r
148 /* The queue that is used to send message to vPrintTask for display in the
\r
149 terminal output window. */
\r
150 xQueueHandle xPrintQueue;
\r
152 /* The rate at which the LED will toggle. The toggle rate increases if an
\r
153 error is detected in any task. */
\r
154 static portTickType xLED_Delay = mainLED_DELAY;
\r
155 /*-----------------------------------------------------------*/
\r
158 * Simply flashes the on board LED every mainLED_DELAY milliseconds.
\r
160 static void vLEDTask( void *pvParameters );
\r
163 * Checks the status of all the demo tasks then prints a message to the
\r
164 * CrossStudio terminal IO windows. The message will be either PASS or FAIL
\r
165 * depending on the status of the demo applications tasks. A FAIL status will
\r
168 * Messages are not written directly to the terminal, but passed to vPrintTask
\r
171 static void vCheckTask( void *pvParameters );
\r
174 * Controls all terminal output. If a task wants to send a message to the
\r
175 * terminal IO it posts a pointer to the text to vPrintTask via a queue. This
\r
176 * ensures serial access to the terminal IO.
\r
178 static void vPrintTask( void *pvParameter );
\r
181 * Simply waits for an interrupt to be generated from the built in button, then
\r
182 * generates a table of tasks states that is then written by vPrintTask to the
\r
183 * terminal output window within CrossStudio.
\r
185 static void vButtonHandlerTask( void *pvParameters );
\r
187 /*-----------------------------------------------------------*/
\r
191 /* Setup the peripheral bus to be the same as the PLL output. */
\r
192 VPBDIV = mainBUS_CLK_FULL;
\r
194 /* Create the queue used to pass message to vPrintTask. */
\r
195 xPrintQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( char * ) );
\r
197 /* Create the semaphore used to wake vButtonHandlerTask(). */
\r
198 vSemaphoreCreateBinary( xButtonSemaphore );
\r
199 xSemaphoreTake( xButtonSemaphore, 0 );
\r
201 /* Start the standard demo tasks. */
\r
202 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
203 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
204 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
205 vStartDynamicPriorityTasks();
\r
206 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
208 #if configUSE_PREEMPTION == 1
\r
210 /* The timing of console output when not using the preemptive
\r
211 scheduler causes the block time tests to detect a timing problem. */
\r
212 vCreateBlockTimeTasks();
\r
216 vStartRecursiveMutexTasks();
\r
218 /* Start the tasks defined within this file. */
\r
219 xTaskCreate( vLEDTask, ( signed char * ) "LED", configMINIMAL_STACK_SIZE, NULL, mainLED_TASK_PRIORITY, NULL );
\r
220 xTaskCreate( vCheckTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
221 xTaskCreate( vPrintTask, ( signed char * ) "Print", configMINIMAL_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
222 xTaskCreate( vButtonHandlerTask, ( signed char * ) "Button", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
224 /* Start the scheduler. */
\r
225 vTaskStartScheduler();
\r
227 /* The scheduler should now be running, so we will only ever reach here if we
\r
228 ran out of heap space. */
\r
232 /*-----------------------------------------------------------*/
\r
234 static void vLEDTask( void *pvParameters )
\r
236 /* Just to remove compiler warnings. */
\r
237 ( void ) pvParameters;
\r
239 /* Configure IO. */
\r
240 IO0DIR |= mainLED_BIT;
\r
241 IO0SET = mainLED_BIT;
\r
245 /* Not very exiting - just delay... */
\r
246 vTaskDelay( xLED_Delay );
\r
248 /* ...set the IO ... */
\r
249 IO0CLR = mainLED_BIT;
\r
251 /* ...delay again... */
\r
252 vTaskDelay( xLED_Delay );
\r
254 /* ...then clear the IO. */
\r
255 IO0SET = mainLED_BIT;
\r
258 /*-----------------------------------------------------------*/
\r
260 static void vCheckTask( void *pvParameters )
\r
262 portBASE_TYPE xErrorOccurred = pdFALSE;
\r
263 portTickType xLastExecutionTime;
\r
264 const char * const pcPassMessage = "PASS\n";
\r
265 const char * const pcFailMessage = "FAIL\n";
\r
267 /* Just to remove compiler warnings. */
\r
268 ( void ) pvParameters;
\r
270 /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
\r
271 works correctly. */
\r
272 xLastExecutionTime = xTaskGetTickCount();
\r
276 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
277 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
279 /* Has an error been found in any task? */
\r
281 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
283 xErrorOccurred = pdTRUE;
\r
286 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
288 xErrorOccurred = pdTRUE;
\r
291 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
293 xErrorOccurred = pdTRUE;
\r
296 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
298 xErrorOccurred = pdTRUE;
\r
301 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
303 xErrorOccurred = pdTRUE;
\r
306 #if configUSE_PREEMPTION == 1
\r
308 /* The timing of console output when not using the preemptive
\r
309 scheduler causes the block time tests to detect a timing problem. */
\r
310 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
312 xErrorOccurred = pdTRUE;
\r
317 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
319 xErrorOccurred = pdTRUE;
\r
322 /* Send either a pass or fail message. If an error is found it is
\r
323 never cleared again. */
\r
324 if( xErrorOccurred == pdTRUE )
\r
326 xLED_Delay = mainERROR_LED_DELAY;
\r
327 xQueueSend( xPrintQueue, &pcFailMessage, portMAX_DELAY );
\r
331 xQueueSend( xPrintQueue, &pcPassMessage, portMAX_DELAY );
\r
335 /*-----------------------------------------------------------*/
\r
337 static void vPrintTask( void *pvParameters )
\r
341 /* Just to stop compiler warnings. */
\r
342 ( void ) pvParameters;
\r
346 /* Wait for a message to arrive. */
\r
347 while( xQueueReceive( xPrintQueue, &pcMessage, portMAX_DELAY ) != pdPASS );
\r
349 /* Write the message to the terminal IO. */
\r
351 debug_printf( "%s", pcMessage );
\r
355 /*-----------------------------------------------------------*/
\r
357 static void vButtonHandlerTask( void *pvParameters )
\r
359 static signed char cListBuffer[ mainLIST_BUFFER_SIZE ];
\r
360 const signed char *pcList = &( cListBuffer[ 0 ] );
\r
361 const char * const pcHeader = "\nTask State Priority Stack #\n************************************************";
\r
362 extern void (vButtonISRWrapper) ( void );
\r
364 /* Just to stop compiler warnings. */
\r
365 ( void ) pvParameters;
\r
367 /* Configure the interrupt. */
\r
368 portENTER_CRITICAL();
\r
370 /* Configure P0.14 to generate interrupts. */
\r
371 PINSEL0 |= mainP0_14__EINT_1;
\r
372 EXTMODE = mainEINT_1_EDGE_SENSITIVE;
\r
373 EXTPOLAR = mainEINT_1_FALLING_EDGE_SENSITIVE;
\r
375 /* Setup the VIC for EINT 1. */
\r
376 VICIntSelect &= ~mainEINT_1_VIC_CHANNEL_BIT;
\r
377 VICIntEnable |= mainEINT_1_VIC_CHANNEL_BIT;
\r
378 VICVectAddr1 = ( long ) vButtonISRWrapper;
\r
379 VICVectCntl1 = mainEINT_1_ENABLE_BIT | mainEINT_1_CHANNEL;
\r
381 portEXIT_CRITICAL();
\r
385 /* For debouncing, wait a while then clear the semaphore. */
\r
386 vTaskDelay( mainSHORT_DELAY );
\r
387 xSemaphoreTake( xButtonSemaphore, mainNO_DELAY );
\r
389 /* Wait for an interrupt. */
\r
390 xSemaphoreTake( xButtonSemaphore, portMAX_DELAY );
\r
392 /* Send the column headers to the print task for display. */
\r
393 xQueueSend( xPrintQueue, &pcHeader, portMAX_DELAY );
\r
395 /* Create the list of task states. */
\r
396 vTaskList( cListBuffer );
\r
398 /* Send the task status information to the print task for display. */
\r
399 xQueueSend( xPrintQueue, &pcList, portMAX_DELAY );
\r
402 /*-----------------------------------------------------------*/
\r
404 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
\r
406 /* Check pcTaskName for the name of the offending task, or pxCurrentTCB
\r
407 if pcTaskName has itself been corrupted. */
\r
409 ( void ) pcTaskName;
\r