2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 * This file contains a demo created to execute on the Rowley Associates
\r
71 * LPC2138 CrossFire development board.
\r
73 * main() creates all the demo application tasks, then starts the scheduler.
\r
74 * The WEB documentation provides more details of the standard demo application
\r
77 * Main.c also creates a task called "Check". This only executes every few
\r
78 * seconds but has a high priority so is guaranteed to get processor time.
\r
79 * Its function is to check that all the other tasks are still operational.
\r
80 * Each standard demo task maintains a unique count that is incremented each
\r
81 * time the task successfully completes its function. Should any error occur
\r
82 * within such a task the count is permanently halted. The check task inspects
\r
83 * the count of each task to ensure it has changed since the last time the
\r
84 * check task executed. If all the count variables have changed all the tasks
\r
85 * are still executing error free, and the check task writes "PASS" to the
\r
86 * CrossStudio terminal IO window. Should any task contain an error at any time
\r
87 * the error is latched and "FAIL" written to the terminal IO window.
\r
89 * Finally, main() sets up an interrupt service routine and task to handle
\r
90 * pushes of the button that is built into the CrossFire board. When the button
\r
91 * is pushed the ISR wakes the button task - which generates a table of task
\r
92 * status information which is also displayed on the terminal IO window.
\r
94 * A print task is defined to ensure exclusive and consistent access to the
\r
95 * terminal IO. This is the only task that is allowed to access the terminal.
\r
96 * The check and button task therefore do not access the terminal directly but
\r
97 * instead pass a pointer to the message they wish to display to the print task.
\r
100 /* Standard includes. */
\r
101 #include <__cross_studio_io.h>
\r
103 /* Scheduler includes. */
\r
104 #include "FreeRTOS.h"
\r
107 #include "semphr.h"
\r
109 /* Demo app includes. */
\r
110 #include "BlockQ.h"
\r
112 #include "dynamic.h"
\r
113 #include "integer.h"
\r
115 #include "blocktim.h"
\r
116 #include "recmutex.h"
\r
117 #include "semtest.h"
\r
119 /* Hardware configuration definitions. */
\r
120 #define mainBUS_CLK_FULL ( ( unsigned char ) 0x01 )
\r
121 #define mainLED_BIT 0x80000000
\r
122 #define mainP0_14__EINT_1 ( 2 << 28 )
\r
123 #define mainEINT_1_EDGE_SENSITIVE 2
\r
124 #define mainEINT_1_FALLING_EDGE_SENSITIVE 0
\r
125 #define mainEINT_1_CHANNEL 15
\r
126 #define mainEINT_1_VIC_CHANNEL_BIT ( 1 << mainEINT_1_CHANNEL )
\r
127 #define mainEINT_1_ENABLE_BIT ( 1 << 5 )
\r
129 /* Demo application definitions. */
\r
130 #define mainQUEUE_SIZE ( 3 )
\r
131 #define mainLED_DELAY ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
132 #define mainERROR_LED_DELAY ( ( portTickType ) 50 / portTICK_RATE_MS )
\r
133 #define mainCHECK_DELAY ( ( portTickType ) 5000 / portTICK_RATE_MS )
\r
134 #define mainLIST_BUFFER_SIZE 2048
\r
135 #define mainNO_DELAY ( 0 )
\r
136 #define mainSHORT_DELAY ( 150 / portTICK_RATE_MS )
\r
138 /* Task priorities. */
\r
139 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
140 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
141 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
142 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
143 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
144 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
146 /*-----------------------------------------------------------*/
\r
148 /* The semaphore used to wake the button task from within the external interrupt
\r
150 xSemaphoreHandle xButtonSemaphore;
\r
152 /* The queue that is used to send message to vPrintTask for display in the
\r
153 terminal output window. */
\r
154 xQueueHandle xPrintQueue;
\r
156 /* The rate at which the LED will toggle. The toggle rate increases if an
\r
157 error is detected in any task. */
\r
158 static portTickType xLED_Delay = mainLED_DELAY;
\r
159 /*-----------------------------------------------------------*/
\r
162 * Simply flashes the on board LED every mainLED_DELAY milliseconds.
\r
164 static void vLEDTask( void *pvParameters );
\r
167 * Checks the status of all the demo tasks then prints a message to the
\r
168 * CrossStudio terminal IO windows. The message will be either PASS or FAIL
\r
169 * depending on the status of the demo applications tasks. A FAIL status will
\r
172 * Messages are not written directly to the terminal, but passed to vPrintTask
\r
175 static void vCheckTask( void *pvParameters );
\r
178 * Controls all terminal output. If a task wants to send a message to the
\r
179 * terminal IO it posts a pointer to the text to vPrintTask via a queue. This
\r
180 * ensures serial access to the terminal IO.
\r
182 static void vPrintTask( void *pvParameter );
\r
185 * Simply waits for an interrupt to be generated from the built in button, then
\r
186 * generates a table of tasks states that is then written by vPrintTask to the
\r
187 * terminal output window within CrossStudio.
\r
189 static void vButtonHandlerTask( void *pvParameters );
\r
191 /*-----------------------------------------------------------*/
\r
195 /* Setup the peripheral bus to be the same as the PLL output. */
\r
196 VPBDIV = mainBUS_CLK_FULL;
\r
198 /* Create the queue used to pass message to vPrintTask. */
\r
199 xPrintQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( char * ) );
\r
201 /* Create the semaphore used to wake vButtonHandlerTask(). */
\r
202 vSemaphoreCreateBinary( xButtonSemaphore );
\r
203 xSemaphoreTake( xButtonSemaphore, 0 );
\r
205 /* Start the standard demo tasks. */
\r
206 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
207 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
208 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
209 vStartDynamicPriorityTasks();
\r
210 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
212 #if configUSE_PREEMPTION == 1
\r
214 /* The timing of console output when not using the preemptive
\r
215 scheduler causes the block time tests to detect a timing problem. */
\r
216 vCreateBlockTimeTasks();
\r
220 vStartRecursiveMutexTasks();
\r
222 /* Start the tasks defined within this file. */
\r
223 xTaskCreate( vLEDTask, ( signed char * ) "LED", configMINIMAL_STACK_SIZE, NULL, mainLED_TASK_PRIORITY, NULL );
\r
224 xTaskCreate( vCheckTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
225 xTaskCreate( vPrintTask, ( signed char * ) "Print", configMINIMAL_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
226 xTaskCreate( vButtonHandlerTask, ( signed char * ) "Button", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
228 /* Start the scheduler. */
\r
229 vTaskStartScheduler();
\r
231 /* The scheduler should now be running, so we will only ever reach here if we
\r
232 ran out of heap space. */
\r
236 /*-----------------------------------------------------------*/
\r
238 static void vLEDTask( void *pvParameters )
\r
240 /* Just to remove compiler warnings. */
\r
241 ( void ) pvParameters;
\r
243 /* Configure IO. */
\r
244 IO0DIR |= mainLED_BIT;
\r
245 IO0SET = mainLED_BIT;
\r
249 /* Not very exiting - just delay... */
\r
250 vTaskDelay( xLED_Delay );
\r
252 /* ...set the IO ... */
\r
253 IO0CLR = mainLED_BIT;
\r
255 /* ...delay again... */
\r
256 vTaskDelay( xLED_Delay );
\r
258 /* ...then clear the IO. */
\r
259 IO0SET = mainLED_BIT;
\r
262 /*-----------------------------------------------------------*/
\r
264 static void vCheckTask( void *pvParameters )
\r
266 portBASE_TYPE xErrorOccurred = pdFALSE;
\r
267 portTickType xLastExecutionTime;
\r
268 const char * const pcPassMessage = "PASS\n";
\r
269 const char * const pcFailMessage = "FAIL\n";
\r
271 /* Just to remove compiler warnings. */
\r
272 ( void ) pvParameters;
\r
274 /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
\r
275 works correctly. */
\r
276 xLastExecutionTime = xTaskGetTickCount();
\r
280 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
281 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
283 /* Has an error been found in any task? */
\r
285 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
287 xErrorOccurred = pdTRUE;
\r
290 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
292 xErrorOccurred = pdTRUE;
\r
295 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
297 xErrorOccurred = pdTRUE;
\r
300 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
302 xErrorOccurred = pdTRUE;
\r
305 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
307 xErrorOccurred = pdTRUE;
\r
310 #if configUSE_PREEMPTION == 1
\r
312 /* The timing of console output when not using the preemptive
\r
313 scheduler causes the block time tests to detect a timing problem. */
\r
314 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
316 xErrorOccurred = pdTRUE;
\r
321 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
323 xErrorOccurred = pdTRUE;
\r
326 /* Send either a pass or fail message. If an error is found it is
\r
327 never cleared again. */
\r
328 if( xErrorOccurred == pdTRUE )
\r
330 xLED_Delay = mainERROR_LED_DELAY;
\r
331 xQueueSend( xPrintQueue, &pcFailMessage, portMAX_DELAY );
\r
335 xQueueSend( xPrintQueue, &pcPassMessage, portMAX_DELAY );
\r
339 /*-----------------------------------------------------------*/
\r
341 static void vPrintTask( void *pvParameters )
\r
345 /* Just to stop compiler warnings. */
\r
346 ( void ) pvParameters;
\r
350 /* Wait for a message to arrive. */
\r
351 while( xQueueReceive( xPrintQueue, &pcMessage, portMAX_DELAY ) != pdPASS );
\r
353 /* Write the message to the terminal IO. */
\r
355 debug_printf( "%s", pcMessage );
\r
359 /*-----------------------------------------------------------*/
\r
361 static void vButtonHandlerTask( void *pvParameters )
\r
363 static signed char cListBuffer[ mainLIST_BUFFER_SIZE ];
\r
364 const signed char *pcList = &( cListBuffer[ 0 ] );
\r
365 const char * const pcHeader = "\nTask State Priority Stack #\n************************************************";
\r
366 extern void (vButtonISRWrapper) ( void );
\r
368 /* Just to stop compiler warnings. */
\r
369 ( void ) pvParameters;
\r
371 /* Configure the interrupt. */
\r
372 portENTER_CRITICAL();
\r
374 /* Configure P0.14 to generate interrupts. */
\r
375 PINSEL0 |= mainP0_14__EINT_1;
\r
376 EXTMODE = mainEINT_1_EDGE_SENSITIVE;
\r
377 EXTPOLAR = mainEINT_1_FALLING_EDGE_SENSITIVE;
\r
379 /* Setup the VIC for EINT 1. */
\r
380 VICIntSelect &= ~mainEINT_1_VIC_CHANNEL_BIT;
\r
381 VICIntEnable |= mainEINT_1_VIC_CHANNEL_BIT;
\r
382 VICVectAddr1 = ( long ) vButtonISRWrapper;
\r
383 VICVectCntl1 = mainEINT_1_ENABLE_BIT | mainEINT_1_CHANNEL;
\r
385 portEXIT_CRITICAL();
\r
389 /* For debouncing, wait a while then clear the semaphore. */
\r
390 vTaskDelay( mainSHORT_DELAY );
\r
391 xSemaphoreTake( xButtonSemaphore, mainNO_DELAY );
\r
393 /* Wait for an interrupt. */
\r
394 xSemaphoreTake( xButtonSemaphore, portMAX_DELAY );
\r
396 /* Send the column headers to the print task for display. */
\r
397 xQueueSend( xPrintQueue, &pcHeader, portMAX_DELAY );
\r
399 /* Create the list of task states. */
\r
400 vTaskList( cListBuffer );
\r
402 /* Send the task status information to the print task for display. */
\r
403 xQueueSend( xPrintQueue, &pcList, portMAX_DELAY );
\r
406 /*-----------------------------------------------------------*/
\r
408 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
\r
410 /* Check pcTaskName for the name of the offending task, or pxCurrentTCB
\r
411 if pcTaskName has itself been corrupted. */
\r
413 ( void ) pcTaskName;
\r