2 FreeRTOS V7.5.3 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 * This file contains a demo created to execute on the Rowley Associates
\r
68 * LPC2138 CrossFire development board.
\r
70 * main() creates all the demo application tasks, then starts the scheduler.
\r
71 * The WEB documentation provides more details of the standard demo application
\r
74 * Main.c also creates a task called "Check". This only executes every few
\r
75 * seconds but has a high priority so is guaranteed to get processor time.
\r
76 * Its function is to check that all the other tasks are still operational.
\r
77 * Each standard demo task maintains a unique count that is incremented each
\r
78 * time the task successfully completes its function. Should any error occur
\r
79 * within such a task the count is permanently halted. The check task inspects
\r
80 * the count of each task to ensure it has changed since the last time the
\r
81 * check task executed. If all the count variables have changed all the tasks
\r
82 * are still executing error free, and the check task writes "PASS" to the
\r
83 * CrossStudio terminal IO window. Should any task contain an error at any time
\r
84 * the error is latched and "FAIL" written to the terminal IO window.
\r
86 * Finally, main() sets up an interrupt service routine and task to handle
\r
87 * pushes of the button that is built into the CrossFire board. When the button
\r
88 * is pushed the ISR wakes the button task - which generates a table of task
\r
89 * status information which is also displayed on the terminal IO window.
\r
91 * A print task is defined to ensure exclusive and consistent access to the
\r
92 * terminal IO. This is the only task that is allowed to access the terminal.
\r
93 * The check and button task therefore do not access the terminal directly but
\r
94 * instead pass a pointer to the message they wish to display to the print task.
\r
97 /* Standard includes. */
\r
98 #include <__cross_studio_io.h>
\r
100 /* Scheduler includes. */
\r
101 #include "FreeRTOS.h"
\r
104 #include "semphr.h"
\r
106 /* Demo app includes. */
\r
107 #include "BlockQ.h"
\r
109 #include "dynamic.h"
\r
110 #include "integer.h"
\r
112 #include "blocktim.h"
\r
113 #include "recmutex.h"
\r
114 #include "semtest.h"
\r
116 /* Hardware configuration definitions. */
\r
117 #define mainBUS_CLK_FULL ( ( unsigned char ) 0x01 )
\r
118 #define mainLED_BIT 0x80000000
\r
119 #define mainP0_14__EINT_1 ( 2 << 28 )
\r
120 #define mainEINT_1_EDGE_SENSITIVE 2
\r
121 #define mainEINT_1_FALLING_EDGE_SENSITIVE 0
\r
122 #define mainEINT_1_CHANNEL 15
\r
123 #define mainEINT_1_VIC_CHANNEL_BIT ( 1 << mainEINT_1_CHANNEL )
\r
124 #define mainEINT_1_ENABLE_BIT ( 1 << 5 )
\r
126 /* Demo application definitions. */
\r
127 #define mainQUEUE_SIZE ( 3 )
\r
128 #define mainLED_DELAY ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
129 #define mainERROR_LED_DELAY ( ( portTickType ) 50 / portTICK_RATE_MS )
\r
130 #define mainCHECK_DELAY ( ( portTickType ) 5000 / portTICK_RATE_MS )
\r
131 #define mainLIST_BUFFER_SIZE 2048
\r
132 #define mainNO_DELAY ( 0 )
\r
133 #define mainSHORT_DELAY ( 150 / portTICK_RATE_MS )
\r
135 /* Task priorities. */
\r
136 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
137 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
138 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
139 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
140 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
141 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
143 /*-----------------------------------------------------------*/
\r
145 /* The semaphore used to wake the button task from within the external interrupt
\r
147 xSemaphoreHandle xButtonSemaphore;
\r
149 /* The queue that is used to send message to vPrintTask for display in the
\r
150 terminal output window. */
\r
151 xQueueHandle xPrintQueue;
\r
153 /* The rate at which the LED will toggle. The toggle rate increases if an
\r
154 error is detected in any task. */
\r
155 static portTickType xLED_Delay = mainLED_DELAY;
\r
156 /*-----------------------------------------------------------*/
\r
159 * Simply flashes the on board LED every mainLED_DELAY milliseconds.
\r
161 static void vLEDTask( void *pvParameters );
\r
164 * Checks the status of all the demo tasks then prints a message to the
\r
165 * CrossStudio terminal IO windows. The message will be either PASS or FAIL
\r
166 * depending on the status of the demo applications tasks. A FAIL status will
\r
169 * Messages are not written directly to the terminal, but passed to vPrintTask
\r
172 static void vCheckTask( void *pvParameters );
\r
175 * Controls all terminal output. If a task wants to send a message to the
\r
176 * terminal IO it posts a pointer to the text to vPrintTask via a queue. This
\r
177 * ensures serial access to the terminal IO.
\r
179 static void vPrintTask( void *pvParameter );
\r
182 * Simply waits for an interrupt to be generated from the built in button, then
\r
183 * generates a table of tasks states that is then written by vPrintTask to the
\r
184 * terminal output window within CrossStudio.
\r
186 static void vButtonHandlerTask( void *pvParameters );
\r
188 /*-----------------------------------------------------------*/
\r
192 /* Setup the peripheral bus to be the same as the PLL output. */
\r
193 VPBDIV = mainBUS_CLK_FULL;
\r
195 /* Create the queue used to pass message to vPrintTask. */
\r
196 xPrintQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( char * ) );
\r
198 /* Create the semaphore used to wake vButtonHandlerTask(). */
\r
199 vSemaphoreCreateBinary( xButtonSemaphore );
\r
200 xSemaphoreTake( xButtonSemaphore, 0 );
\r
202 /* Start the standard demo tasks. */
\r
203 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
204 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
205 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
206 vStartDynamicPriorityTasks();
\r
207 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
209 #if configUSE_PREEMPTION == 1
\r
211 /* The timing of console output when not using the preemptive
\r
212 scheduler causes the block time tests to detect a timing problem. */
\r
213 vCreateBlockTimeTasks();
\r
217 vStartRecursiveMutexTasks();
\r
219 /* Start the tasks defined within this file. */
\r
220 xTaskCreate( vLEDTask, ( signed char * ) "LED", configMINIMAL_STACK_SIZE, NULL, mainLED_TASK_PRIORITY, NULL );
\r
221 xTaskCreate( vCheckTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
222 xTaskCreate( vPrintTask, ( signed char * ) "Print", configMINIMAL_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
223 xTaskCreate( vButtonHandlerTask, ( signed char * ) "Button", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
225 /* Start the scheduler. */
\r
226 vTaskStartScheduler();
\r
228 /* The scheduler should now be running, so we will only ever reach here if we
\r
229 ran out of heap space. */
\r
233 /*-----------------------------------------------------------*/
\r
235 static void vLEDTask( void *pvParameters )
\r
237 /* Just to remove compiler warnings. */
\r
238 ( void ) pvParameters;
\r
240 /* Configure IO. */
\r
241 IO0DIR |= mainLED_BIT;
\r
242 IO0SET = mainLED_BIT;
\r
246 /* Not very exiting - just delay... */
\r
247 vTaskDelay( xLED_Delay );
\r
249 /* ...set the IO ... */
\r
250 IO0CLR = mainLED_BIT;
\r
252 /* ...delay again... */
\r
253 vTaskDelay( xLED_Delay );
\r
255 /* ...then clear the IO. */
\r
256 IO0SET = mainLED_BIT;
\r
259 /*-----------------------------------------------------------*/
\r
261 static void vCheckTask( void *pvParameters )
\r
263 portBASE_TYPE xErrorOccurred = pdFALSE;
\r
264 portTickType xLastExecutionTime;
\r
265 const char * const pcPassMessage = "PASS\n";
\r
266 const char * const pcFailMessage = "FAIL\n";
\r
268 /* Just to remove compiler warnings. */
\r
269 ( void ) pvParameters;
\r
271 /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
\r
272 works correctly. */
\r
273 xLastExecutionTime = xTaskGetTickCount();
\r
277 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
278 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
280 /* Has an error been found in any task? */
\r
282 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
284 xErrorOccurred = pdTRUE;
\r
287 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
289 xErrorOccurred = pdTRUE;
\r
292 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
294 xErrorOccurred = pdTRUE;
\r
297 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
299 xErrorOccurred = pdTRUE;
\r
302 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
304 xErrorOccurred = pdTRUE;
\r
307 #if configUSE_PREEMPTION == 1
\r
309 /* The timing of console output when not using the preemptive
\r
310 scheduler causes the block time tests to detect a timing problem. */
\r
311 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
313 xErrorOccurred = pdTRUE;
\r
318 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
320 xErrorOccurred = pdTRUE;
\r
323 /* Send either a pass or fail message. If an error is found it is
\r
324 never cleared again. */
\r
325 if( xErrorOccurred == pdTRUE )
\r
327 xLED_Delay = mainERROR_LED_DELAY;
\r
328 xQueueSend( xPrintQueue, &pcFailMessage, portMAX_DELAY );
\r
332 xQueueSend( xPrintQueue, &pcPassMessage, portMAX_DELAY );
\r
336 /*-----------------------------------------------------------*/
\r
338 static void vPrintTask( void *pvParameters )
\r
342 /* Just to stop compiler warnings. */
\r
343 ( void ) pvParameters;
\r
347 /* Wait for a message to arrive. */
\r
348 while( xQueueReceive( xPrintQueue, &pcMessage, portMAX_DELAY ) != pdPASS );
\r
350 /* Write the message to the terminal IO. */
\r
352 debug_printf( "%s", pcMessage );
\r
356 /*-----------------------------------------------------------*/
\r
358 static void vButtonHandlerTask( void *pvParameters )
\r
360 static signed char cListBuffer[ mainLIST_BUFFER_SIZE ];
\r
361 const signed char *pcList = &( cListBuffer[ 0 ] );
\r
362 const char * const pcHeader = "\nTask State Priority Stack #\n************************************************";
\r
363 extern void (vButtonISRWrapper) ( void );
\r
365 /* Just to stop compiler warnings. */
\r
366 ( void ) pvParameters;
\r
368 /* Configure the interrupt. */
\r
369 portENTER_CRITICAL();
\r
371 /* Configure P0.14 to generate interrupts. */
\r
372 PINSEL0 |= mainP0_14__EINT_1;
\r
373 EXTMODE = mainEINT_1_EDGE_SENSITIVE;
\r
374 EXTPOLAR = mainEINT_1_FALLING_EDGE_SENSITIVE;
\r
376 /* Setup the VIC for EINT 1. */
\r
377 VICIntSelect &= ~mainEINT_1_VIC_CHANNEL_BIT;
\r
378 VICIntEnable |= mainEINT_1_VIC_CHANNEL_BIT;
\r
379 VICVectAddr1 = ( long ) vButtonISRWrapper;
\r
380 VICVectCntl1 = mainEINT_1_ENABLE_BIT | mainEINT_1_CHANNEL;
\r
382 portEXIT_CRITICAL();
\r
386 /* For debouncing, wait a while then clear the semaphore. */
\r
387 vTaskDelay( mainSHORT_DELAY );
\r
388 xSemaphoreTake( xButtonSemaphore, mainNO_DELAY );
\r
390 /* Wait for an interrupt. */
\r
391 xSemaphoreTake( xButtonSemaphore, portMAX_DELAY );
\r
393 /* Send the column headers to the print task for display. */
\r
394 xQueueSend( xPrintQueue, &pcHeader, portMAX_DELAY );
\r
396 /* Create the list of task states. */
\r
397 vTaskList( cListBuffer );
\r
399 /* Send the task status information to the print task for display. */
\r
400 xQueueSend( xPrintQueue, &pcList, portMAX_DELAY );
\r
403 /*-----------------------------------------------------------*/
\r
405 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
\r
407 /* Check pcTaskName for the name of the offending task, or pxCurrentTCB
\r
408 if pcTaskName has itself been corrupted. */
\r
410 ( void ) pcTaskName;
\r