2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
97 * This file contains a demo created to execute on the Rowley Associates
\r
98 * LPC2138 CrossFire development board.
\r
100 * main() creates all the demo application tasks, then starts the scheduler.
\r
101 * The WEB documentation provides more details of the standard demo application
\r
104 * Main.c also creates a task called "Check". This only executes every few
\r
105 * seconds but has a high priority so is guaranteed to get processor time.
\r
106 * Its function is to check that all the other tasks are still operational.
\r
107 * Each standard demo task maintains a unique count that is incremented each
\r
108 * time the task successfully completes its function. Should any error occur
\r
109 * within such a task the count is permanently halted. The check task inspects
\r
110 * the count of each task to ensure it has changed since the last time the
\r
111 * check task executed. If all the count variables have changed all the tasks
\r
112 * are still executing error free, and the check task writes "PASS" to the
\r
113 * CrossStudio terminal IO window. Should any task contain an error at any time
\r
114 * the error is latched and "FAIL" written to the terminal IO window.
\r
116 * Finally, main() sets up an interrupt service routine and task to handle
\r
117 * pushes of the button that is built into the CrossFire board. When the button
\r
118 * is pushed the ISR wakes the button task - which generates a table of task
\r
119 * status information which is also displayed on the terminal IO window.
\r
121 * A print task is defined to ensure exclusive and consistent access to the
\r
122 * terminal IO. This is the only task that is allowed to access the terminal.
\r
123 * The check and button task therefore do not access the terminal directly but
\r
124 * instead pass a pointer to the message they wish to display to the print task.
\r
127 /* Standard includes. */
\r
128 #include <__cross_studio_io.h>
\r
130 /* Scheduler includes. */
\r
131 #include "FreeRTOS.h"
\r
134 #include "semphr.h"
\r
136 /* Demo app includes. */
\r
137 #include "BlockQ.h"
\r
139 #include "dynamic.h"
\r
140 #include "integer.h"
\r
142 #include "blocktim.h"
\r
143 #include "recmutex.h"
\r
144 #include "semtest.h"
\r
146 /* Hardware configuration definitions. */
\r
147 #define mainBUS_CLK_FULL ( ( unsigned char ) 0x01 )
\r
148 #define mainLED_BIT 0x80000000
\r
149 #define mainP0_14__EINT_1 ( 2 << 28 )
\r
150 #define mainEINT_1_EDGE_SENSITIVE 2
\r
151 #define mainEINT_1_FALLING_EDGE_SENSITIVE 0
\r
152 #define mainEINT_1_CHANNEL 15
\r
153 #define mainEINT_1_VIC_CHANNEL_BIT ( 1 << mainEINT_1_CHANNEL )
\r
154 #define mainEINT_1_ENABLE_BIT ( 1 << 5 )
\r
156 /* Demo application definitions. */
\r
157 #define mainQUEUE_SIZE ( 3 )
\r
158 #define mainLED_DELAY ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
\r
159 #define mainERROR_LED_DELAY ( ( TickType_t ) 50 / portTICK_PERIOD_MS )
\r
160 #define mainCHECK_DELAY ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )
\r
161 #define mainLIST_BUFFER_SIZE 2048
\r
162 #define mainNO_DELAY ( 0 )
\r
163 #define mainSHORT_DELAY ( 150 / portTICK_PERIOD_MS )
\r
165 /* Task priorities. */
\r
166 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
167 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
168 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
169 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
170 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
171 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
173 /*-----------------------------------------------------------*/
\r
175 /* The semaphore used to wake the button task from within the external interrupt
\r
177 SemaphoreHandle_t xButtonSemaphore;
\r
179 /* The queue that is used to send message to vPrintTask for display in the
\r
180 terminal output window. */
\r
181 QueueHandle_t xPrintQueue;
\r
183 /* The rate at which the LED will toggle. The toggle rate increases if an
\r
184 error is detected in any task. */
\r
185 static TickType_t xLED_Delay = mainLED_DELAY;
\r
186 /*-----------------------------------------------------------*/
\r
189 * Simply flashes the on board LED every mainLED_DELAY milliseconds.
\r
191 static void vLEDTask( void *pvParameters );
\r
194 * Checks the status of all the demo tasks then prints a message to the
\r
195 * CrossStudio terminal IO windows. The message will be either PASS or FAIL
\r
196 * depending on the status of the demo applications tasks. A FAIL status will
\r
199 * Messages are not written directly to the terminal, but passed to vPrintTask
\r
202 static void vCheckTask( void *pvParameters );
\r
205 * Controls all terminal output. If a task wants to send a message to the
\r
206 * terminal IO it posts a pointer to the text to vPrintTask via a queue. This
\r
207 * ensures serial access to the terminal IO.
\r
209 static void vPrintTask( void *pvParameter );
\r
212 * Simply waits for an interrupt to be generated from the built in button, then
\r
213 * generates a table of tasks states that is then written by vPrintTask to the
\r
214 * terminal output window within CrossStudio.
\r
216 static void vButtonHandlerTask( void *pvParameters );
\r
218 /*-----------------------------------------------------------*/
\r
222 /* Setup the peripheral bus to be the same as the PLL output. */
\r
223 VPBDIV = mainBUS_CLK_FULL;
\r
225 /* Create the queue used to pass message to vPrintTask. */
\r
226 xPrintQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( char * ) );
\r
228 /* Create the semaphore used to wake vButtonHandlerTask(). */
\r
229 vSemaphoreCreateBinary( xButtonSemaphore );
\r
230 xSemaphoreTake( xButtonSemaphore, 0 );
\r
232 /* Start the standard demo tasks. */
\r
233 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
234 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
235 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
236 vStartDynamicPriorityTasks();
\r
237 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
239 #if configUSE_PREEMPTION == 1
\r
241 /* The timing of console output when not using the preemptive
\r
242 scheduler causes the block time tests to detect a timing problem. */
\r
243 vCreateBlockTimeTasks();
\r
247 vStartRecursiveMutexTasks();
\r
249 /* Start the tasks defined within this file. */
\r
250 xTaskCreate( vLEDTask, "LED", configMINIMAL_STACK_SIZE, NULL, mainLED_TASK_PRIORITY, NULL );
\r
251 xTaskCreate( vCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
252 xTaskCreate( vPrintTask, "Print", configMINIMAL_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
253 xTaskCreate( vButtonHandlerTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
255 /* Start the scheduler. */
\r
256 vTaskStartScheduler();
\r
258 /* The scheduler should now be running, so we will only ever reach here if we
\r
259 ran out of heap space. */
\r
263 /*-----------------------------------------------------------*/
\r
265 static void vLEDTask( void *pvParameters )
\r
267 /* Just to remove compiler warnings. */
\r
268 ( void ) pvParameters;
\r
270 /* Configure IO. */
\r
271 IO0DIR |= mainLED_BIT;
\r
272 IO0SET = mainLED_BIT;
\r
276 /* Not very exiting - just delay... */
\r
277 vTaskDelay( xLED_Delay );
\r
279 /* ...set the IO ... */
\r
280 IO0CLR = mainLED_BIT;
\r
282 /* ...delay again... */
\r
283 vTaskDelay( xLED_Delay );
\r
285 /* ...then clear the IO. */
\r
286 IO0SET = mainLED_BIT;
\r
289 /*-----------------------------------------------------------*/
\r
291 static void vCheckTask( void *pvParameters )
\r
293 portBASE_TYPE xErrorOccurred = pdFALSE;
\r
294 TickType_t xLastExecutionTime;
\r
295 const char * const pcPassMessage = "PASS\n";
\r
296 const char * const pcFailMessage = "FAIL\n";
\r
298 /* Just to remove compiler warnings. */
\r
299 ( void ) pvParameters;
\r
301 /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
\r
302 works correctly. */
\r
303 xLastExecutionTime = xTaskGetTickCount();
\r
307 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
308 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
310 /* Has an error been found in any task? */
\r
312 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
314 xErrorOccurred = pdTRUE;
\r
317 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
319 xErrorOccurred = pdTRUE;
\r
322 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
324 xErrorOccurred = pdTRUE;
\r
327 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
329 xErrorOccurred = pdTRUE;
\r
332 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
334 xErrorOccurred = pdTRUE;
\r
337 #if configUSE_PREEMPTION == 1
\r
339 /* The timing of console output when not using the preemptive
\r
340 scheduler causes the block time tests to detect a timing problem. */
\r
341 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
343 xErrorOccurred = pdTRUE;
\r
348 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
350 xErrorOccurred = pdTRUE;
\r
353 /* Send either a pass or fail message. If an error is found it is
\r
354 never cleared again. */
\r
355 if( xErrorOccurred == pdTRUE )
\r
357 xLED_Delay = mainERROR_LED_DELAY;
\r
358 xQueueSend( xPrintQueue, &pcFailMessage, portMAX_DELAY );
\r
362 xQueueSend( xPrintQueue, &pcPassMessage, portMAX_DELAY );
\r
366 /*-----------------------------------------------------------*/
\r
368 static void vPrintTask( void *pvParameters )
\r
372 /* Just to stop compiler warnings. */
\r
373 ( void ) pvParameters;
\r
377 /* Wait for a message to arrive. */
\r
378 while( xQueueReceive( xPrintQueue, &pcMessage, portMAX_DELAY ) != pdPASS );
\r
380 /* Write the message to the terminal IO. */
\r
382 debug_printf( "%s", pcMessage );
\r
386 /*-----------------------------------------------------------*/
\r
388 static void vButtonHandlerTask( void *pvParameters )
\r
390 static char cListBuffer[ mainLIST_BUFFER_SIZE ];
\r
391 const char *pcList = &( cListBuffer[ 0 ] );
\r
392 const char * const pcHeader = "\nTask State Priority Stack #\n************************************************";
\r
393 extern void (vButtonISRWrapper) ( void );
\r
395 /* Just to stop compiler warnings. */
\r
396 ( void ) pvParameters;
\r
398 /* Configure the interrupt. */
\r
399 portENTER_CRITICAL();
\r
401 /* Configure P0.14 to generate interrupts. */
\r
402 PINSEL0 |= mainP0_14__EINT_1;
\r
403 EXTMODE = mainEINT_1_EDGE_SENSITIVE;
\r
404 EXTPOLAR = mainEINT_1_FALLING_EDGE_SENSITIVE;
\r
406 /* Setup the VIC for EINT 1. */
\r
407 VICIntSelect &= ~mainEINT_1_VIC_CHANNEL_BIT;
\r
408 VICIntEnable |= mainEINT_1_VIC_CHANNEL_BIT;
\r
409 VICVectAddr1 = ( long ) vButtonISRWrapper;
\r
410 VICVectCntl1 = mainEINT_1_ENABLE_BIT | mainEINT_1_CHANNEL;
\r
412 portEXIT_CRITICAL();
\r
416 /* For debouncing, wait a while then clear the semaphore. */
\r
417 vTaskDelay( mainSHORT_DELAY );
\r
418 xSemaphoreTake( xButtonSemaphore, mainNO_DELAY );
\r
420 /* Wait for an interrupt. */
\r
421 xSemaphoreTake( xButtonSemaphore, portMAX_DELAY );
\r
423 /* Send the column headers to the print task for display. */
\r
424 xQueueSend( xPrintQueue, &pcHeader, portMAX_DELAY );
\r
426 /* Create the list of task states. */
\r
427 vTaskList( cListBuffer );
\r
429 /* Send the task status information to the print task for display. */
\r
430 xQueueSend( xPrintQueue, &pcList, portMAX_DELAY );
\r
433 /*-----------------------------------------------------------*/
\r
435 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
437 /* Check pcTaskName for the name of the offending task, or pxCurrentTCB
\r
438 if pcTaskName has itself been corrupted. */
\r
440 ( void ) pcTaskName;
\r