2 FreeRTOS V7.4.0 - Copyright (C) 2013 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
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not itcan be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
76 * This file contains a demo created to execute on the Rowley Associates
\r
77 * LPC2138 CrossFire development board.
\r
79 * main() creates all the demo application tasks, then starts the scheduler.
\r
80 * The WEB documentation provides more details of the standard demo application
\r
83 * Main.c also creates a task called "Check". This only executes every few
\r
84 * seconds but has a high priority so is guaranteed to get processor time.
\r
85 * Its function is to check that all the other tasks are still operational.
\r
86 * Each standard demo task maintains a unique count that is incremented each
\r
87 * time the task successfully completes its function. Should any error occur
\r
88 * within such a task the count is permanently halted. The check task inspects
\r
89 * the count of each task to ensure it has changed since the last time the
\r
90 * check task executed. If all the count variables have changed all the tasks
\r
91 * are still executing error free, and the check task writes "PASS" to the
\r
92 * CrossStudio terminal IO window. Should any task contain an error at any time
\r
93 * the error is latched and "FAIL" written to the terminal IO window.
\r
95 * Finally, main() sets up an interrupt service routine and task to handle
\r
96 * pushes of the button that is built into the CrossFire board. When the button
\r
97 * is pushed the ISR wakes the button task - which generates a table of task
\r
98 * status information which is also displayed on the terminal IO window.
\r
100 * A print task is defined to ensure exclusive and consistent access to the
\r
101 * terminal IO. This is the only task that is allowed to access the terminal.
\r
102 * The check and button task therefore do not access the terminal directly but
\r
103 * instead pass a pointer to the message they wish to display to the print task.
\r
106 /* Standard includes. */
\r
107 #include <__cross_studio_io.h>
\r
109 /* Scheduler includes. */
\r
110 #include "FreeRTOS.h"
\r
113 #include "semphr.h"
\r
115 /* Demo app includes. */
\r
116 #include "BlockQ.h"
\r
118 #include "dynamic.h"
\r
119 #include "integer.h"
\r
121 #include "blocktim.h"
\r
122 #include "recmutex.h"
\r
123 #include "semtest.h"
\r
125 /* Hardware configuration definitions. */
\r
126 #define mainBUS_CLK_FULL ( ( unsigned char ) 0x01 )
\r
127 #define mainLED_BIT 0x80000000
\r
128 #define mainP0_14__EINT_1 ( 2 << 28 )
\r
129 #define mainEINT_1_EDGE_SENSITIVE 2
\r
130 #define mainEINT_1_FALLING_EDGE_SENSITIVE 0
\r
131 #define mainEINT_1_CHANNEL 15
\r
132 #define mainEINT_1_VIC_CHANNEL_BIT ( 1 << mainEINT_1_CHANNEL )
\r
133 #define mainEINT_1_ENABLE_BIT ( 1 << 5 )
\r
135 /* Demo application definitions. */
\r
136 #define mainQUEUE_SIZE ( 3 )
\r
137 #define mainLED_DELAY ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
138 #define mainERROR_LED_DELAY ( ( portTickType ) 50 / portTICK_RATE_MS )
\r
139 #define mainCHECK_DELAY ( ( portTickType ) 5000 / portTICK_RATE_MS )
\r
140 #define mainLIST_BUFFER_SIZE 2048
\r
141 #define mainNO_DELAY ( 0 )
\r
142 #define mainSHORT_DELAY ( 150 / portTICK_RATE_MS )
\r
144 /* Task priorities. */
\r
145 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
146 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
147 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
148 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
149 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
150 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
152 /*-----------------------------------------------------------*/
\r
154 /* The semaphore used to wake the button task from within the external interrupt
\r
156 xSemaphoreHandle xButtonSemaphore;
\r
158 /* The queue that is used to send message to vPrintTask for display in the
\r
159 terminal output window. */
\r
160 xQueueHandle xPrintQueue;
\r
162 /* The rate at which the LED will toggle. The toggle rate increases if an
\r
163 error is detected in any task. */
\r
164 static portTickType xLED_Delay = mainLED_DELAY;
\r
165 /*-----------------------------------------------------------*/
\r
168 * Simply flashes the on board LED every mainLED_DELAY milliseconds.
\r
170 static void vLEDTask( void *pvParameters );
\r
173 * Checks the status of all the demo tasks then prints a message to the
\r
174 * CrossStudio terminal IO windows. The message will be either PASS or FAIL
\r
175 * depending on the status of the demo applications tasks. A FAIL status will
\r
178 * Messages are not written directly to the terminal, but passed to vPrintTask
\r
181 static void vCheckTask( void *pvParameters );
\r
184 * Controls all terminal output. If a task wants to send a message to the
\r
185 * terminal IO it posts a pointer to the text to vPrintTask via a queue. This
\r
186 * ensures serial access to the terminal IO.
\r
188 static void vPrintTask( void *pvParameter );
\r
191 * Simply waits for an interrupt to be generated from the built in button, then
\r
192 * generates a table of tasks states that is then written by vPrintTask to the
\r
193 * terminal output window within CrossStudio.
\r
195 static void vButtonHandlerTask( void *pvParameters );
\r
197 /*-----------------------------------------------------------*/
\r
201 /* Setup the peripheral bus to be the same as the PLL output. */
\r
202 VPBDIV = mainBUS_CLK_FULL;
\r
204 /* Create the queue used to pass message to vPrintTask. */
\r
205 xPrintQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( char * ) );
\r
207 /* Create the semaphore used to wake vButtonHandlerTask(). */
\r
208 vSemaphoreCreateBinary( xButtonSemaphore );
\r
209 xSemaphoreTake( xButtonSemaphore, 0 );
\r
211 /* Start the standard demo tasks. */
\r
212 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
213 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
214 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
215 vStartDynamicPriorityTasks();
\r
216 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
218 #if configUSE_PREEMPTION == 1
\r
220 /* The timing of console output when not using the preemptive
\r
221 scheduler causes the block time tests to detect a timing problem. */
\r
222 vCreateBlockTimeTasks();
\r
226 vStartRecursiveMutexTasks();
\r
228 /* Start the tasks defined within this file. */
\r
229 xTaskCreate( vLEDTask, ( signed char * ) "LED", configMINIMAL_STACK_SIZE, NULL, mainLED_TASK_PRIORITY, NULL );
\r
230 xTaskCreate( vCheckTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
231 xTaskCreate( vPrintTask, ( signed char * ) "Print", configMINIMAL_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
232 xTaskCreate( vButtonHandlerTask, ( signed char * ) "Button", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
234 /* Start the scheduler. */
\r
235 vTaskStartScheduler();
\r
237 /* The scheduler should now be running, so we will only ever reach here if we
\r
238 ran out of heap space. */
\r
242 /*-----------------------------------------------------------*/
\r
244 static void vLEDTask( void *pvParameters )
\r
246 /* Just to remove compiler warnings. */
\r
247 ( void ) pvParameters;
\r
249 /* Configure IO. */
\r
250 IO0DIR |= mainLED_BIT;
\r
251 IO0SET = mainLED_BIT;
\r
255 /* Not very exiting - just delay... */
\r
256 vTaskDelay( xLED_Delay );
\r
258 /* ...set the IO ... */
\r
259 IO0CLR = mainLED_BIT;
\r
261 /* ...delay again... */
\r
262 vTaskDelay( xLED_Delay );
\r
264 /* ...then clear the IO. */
\r
265 IO0SET = mainLED_BIT;
\r
268 /*-----------------------------------------------------------*/
\r
270 static void vCheckTask( void *pvParameters )
\r
272 portBASE_TYPE xErrorOccurred = pdFALSE;
\r
273 portTickType xLastExecutionTime;
\r
274 const char * const pcPassMessage = "PASS\n";
\r
275 const char * const pcFailMessage = "FAIL\n";
\r
277 /* Just to remove compiler warnings. */
\r
278 ( void ) pvParameters;
\r
280 /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
\r
281 works correctly. */
\r
282 xLastExecutionTime = xTaskGetTickCount();
\r
286 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
287 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
289 /* Has an error been found in any task? */
\r
291 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
293 xErrorOccurred = pdTRUE;
\r
296 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
298 xErrorOccurred = pdTRUE;
\r
301 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
303 xErrorOccurred = pdTRUE;
\r
306 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
308 xErrorOccurred = pdTRUE;
\r
311 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
313 xErrorOccurred = pdTRUE;
\r
316 #if configUSE_PREEMPTION == 1
\r
318 /* The timing of console output when not using the preemptive
\r
319 scheduler causes the block time tests to detect a timing problem. */
\r
320 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
322 xErrorOccurred = pdTRUE;
\r
327 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
329 xErrorOccurred = pdTRUE;
\r
332 /* Send either a pass or fail message. If an error is found it is
\r
333 never cleared again. */
\r
334 if( xErrorOccurred == pdTRUE )
\r
336 xLED_Delay = mainERROR_LED_DELAY;
\r
337 xQueueSend( xPrintQueue, &pcFailMessage, portMAX_DELAY );
\r
341 xQueueSend( xPrintQueue, &pcPassMessage, portMAX_DELAY );
\r
345 /*-----------------------------------------------------------*/
\r
347 static void vPrintTask( void *pvParameters )
\r
351 /* Just to stop compiler warnings. */
\r
352 ( void ) pvParameters;
\r
356 /* Wait for a message to arrive. */
\r
357 while( xQueueReceive( xPrintQueue, &pcMessage, portMAX_DELAY ) != pdPASS );
\r
359 /* Write the message to the terminal IO. */
\r
361 debug_printf( "%s", pcMessage );
\r
365 /*-----------------------------------------------------------*/
\r
367 static void vButtonHandlerTask( void *pvParameters )
\r
369 static signed char cListBuffer[ mainLIST_BUFFER_SIZE ];
\r
370 const signed char *pcList = &( cListBuffer[ 0 ] );
\r
371 const char * const pcHeader = "\nTask State Priority Stack #\n************************************************";
\r
372 extern void (vButtonISRWrapper) ( void );
\r
374 /* Just to stop compiler warnings. */
\r
375 ( void ) pvParameters;
\r
377 /* Configure the interrupt. */
\r
378 portENTER_CRITICAL();
\r
380 /* Configure P0.14 to generate interrupts. */
\r
381 PINSEL0 |= mainP0_14__EINT_1;
\r
382 EXTMODE = mainEINT_1_EDGE_SENSITIVE;
\r
383 EXTPOLAR = mainEINT_1_FALLING_EDGE_SENSITIVE;
\r
385 /* Setup the VIC for EINT 1. */
\r
386 VICIntSelect &= ~mainEINT_1_VIC_CHANNEL_BIT;
\r
387 VICIntEnable |= mainEINT_1_VIC_CHANNEL_BIT;
\r
388 VICVectAddr1 = ( long ) vButtonISRWrapper;
\r
389 VICVectCntl1 = mainEINT_1_ENABLE_BIT | mainEINT_1_CHANNEL;
\r
391 portEXIT_CRITICAL();
\r
395 /* For debouncing, wait a while then clear the semaphore. */
\r
396 vTaskDelay( mainSHORT_DELAY );
\r
397 xSemaphoreTake( xButtonSemaphore, mainNO_DELAY );
\r
399 /* Wait for an interrupt. */
\r
400 xSemaphoreTake( xButtonSemaphore, portMAX_DELAY );
\r
402 /* Send the column headers to the print task for display. */
\r
403 xQueueSend( xPrintQueue, &pcHeader, portMAX_DELAY );
\r
405 /* Create the list of task states. */
\r
406 vTaskList( cListBuffer );
\r
408 /* Send the task status information to the print task for display. */
\r
409 xQueueSend( xPrintQueue, &pcList, portMAX_DELAY );
\r
412 /*-----------------------------------------------------------*/
\r
414 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
\r
416 /* Check pcTaskName for the name of the offending task, or pxCurrentTCB
\r
417 if pcTaskName has itself been corrupted. */
\r
419 ( void ) pcTaskName;
\r