2 FreeRTOS V9.0.0rc2 - Copyright (C) 2016 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 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
71 * This file contains a demo created to execute on the Rowley Associates
\r
72 * LPC2138 CrossFire development board.
\r
74 * main() creates all the demo application tasks, then starts the scheduler.
\r
75 * The WEB documentation provides more details of the standard demo application
\r
78 * Main.c also creates a task called "Check". This only executes every few
\r
79 * seconds but has a high priority so is guaranteed to get processor time.
\r
80 * Its function is to check that all the other tasks are still operational.
\r
81 * Each standard demo task maintains a unique count that is incremented each
\r
82 * time the task successfully completes its function. Should any error occur
\r
83 * within such a task the count is permanently halted. The check task inspects
\r
84 * the count of each task to ensure it has changed since the last time the
\r
85 * check task executed. If all the count variables have changed all the tasks
\r
86 * are still executing error free, and the check task writes "PASS" to the
\r
87 * CrossStudio terminal IO window. Should any task contain an error at any time
\r
88 * the error is latched and "FAIL" written to the terminal IO window.
\r
90 * Finally, main() sets up an interrupt service routine and task to handle
\r
91 * pushes of the button that is built into the CrossFire board. When the button
\r
92 * is pushed the ISR wakes the button task - which generates a table of task
\r
93 * status information which is also displayed on the terminal IO window.
\r
95 * A print task is defined to ensure exclusive and consistent access to the
\r
96 * terminal IO. This is the only task that is allowed to access the terminal.
\r
97 * The check and button task therefore do not access the terminal directly but
\r
98 * instead pass a pointer to the message they wish to display to the print task.
\r
101 /* Standard includes. */
\r
102 #include <__cross_studio_io.h>
\r
104 /* Scheduler includes. */
\r
105 #include "FreeRTOS.h"
\r
108 #include "semphr.h"
\r
110 /* Demo app includes. */
\r
111 #include "BlockQ.h"
\r
113 #include "dynamic.h"
\r
114 #include "integer.h"
\r
116 #include "blocktim.h"
\r
117 #include "recmutex.h"
\r
118 #include "semtest.h"
\r
120 /* Hardware configuration definitions. */
\r
121 #define mainBUS_CLK_FULL ( ( unsigned char ) 0x01 )
\r
122 #define mainLED_BIT 0x80000000
\r
123 #define mainP0_14__EINT_1 ( 2 << 28 )
\r
124 #define mainEINT_1_EDGE_SENSITIVE 2
\r
125 #define mainEINT_1_FALLING_EDGE_SENSITIVE 0
\r
126 #define mainEINT_1_CHANNEL 15
\r
127 #define mainEINT_1_VIC_CHANNEL_BIT ( 1 << mainEINT_1_CHANNEL )
\r
128 #define mainEINT_1_ENABLE_BIT ( 1 << 5 )
\r
130 /* Demo application definitions. */
\r
131 #define mainQUEUE_SIZE ( 3 )
\r
132 #define mainLED_DELAY ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
\r
133 #define mainERROR_LED_DELAY ( ( TickType_t ) 50 / portTICK_PERIOD_MS )
\r
134 #define mainCHECK_DELAY ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )
\r
135 #define mainLIST_BUFFER_SIZE 2048
\r
136 #define mainNO_DELAY ( 0 )
\r
137 #define mainSHORT_DELAY ( 150 / portTICK_PERIOD_MS )
\r
139 /* Task priorities. */
\r
140 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
141 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
142 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
143 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
144 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
145 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
147 /*-----------------------------------------------------------*/
\r
149 /* The semaphore used to wake the button task from within the external interrupt
\r
151 SemaphoreHandle_t xButtonSemaphore;
\r
153 /* The queue that is used to send message to vPrintTask for display in the
\r
154 terminal output window. */
\r
155 QueueHandle_t xPrintQueue;
\r
157 /* The rate at which the LED will toggle. The toggle rate increases if an
\r
158 error is detected in any task. */
\r
159 static TickType_t xLED_Delay = mainLED_DELAY;
\r
160 /*-----------------------------------------------------------*/
\r
163 * Simply flashes the on board LED every mainLED_DELAY milliseconds.
\r
165 static void vLEDTask( void *pvParameters );
\r
168 * Checks the status of all the demo tasks then prints a message to the
\r
169 * CrossStudio terminal IO windows. The message will be either PASS or FAIL
\r
170 * depending on the status of the demo applications tasks. A FAIL status will
\r
173 * Messages are not written directly to the terminal, but passed to vPrintTask
\r
176 static void vCheckTask( void *pvParameters );
\r
179 * Controls all terminal output. If a task wants to send a message to the
\r
180 * terminal IO it posts a pointer to the text to vPrintTask via a queue. This
\r
181 * ensures serial access to the terminal IO.
\r
183 static void vPrintTask( void *pvParameter );
\r
186 * Simply waits for an interrupt to be generated from the built in button, then
\r
187 * generates a table of tasks states that is then written by vPrintTask to the
\r
188 * terminal output window within CrossStudio.
\r
190 static void vButtonHandlerTask( void *pvParameters );
\r
192 /*-----------------------------------------------------------*/
\r
196 /* Setup the peripheral bus to be the same as the PLL output. */
\r
197 VPBDIV = mainBUS_CLK_FULL;
\r
199 /* Create the queue used to pass message to vPrintTask. */
\r
200 xPrintQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( char * ) );
\r
202 /* Create the semaphore used to wake vButtonHandlerTask(). */
\r
203 vSemaphoreCreateBinary( xButtonSemaphore );
\r
204 xSemaphoreTake( xButtonSemaphore, 0 );
\r
206 /* Start the standard demo tasks. */
\r
207 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
208 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
209 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
210 vStartDynamicPriorityTasks();
\r
211 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
213 #if configUSE_PREEMPTION == 1
\r
215 /* The timing of console output when not using the preemptive
\r
216 scheduler causes the block time tests to detect a timing problem. */
\r
217 vCreateBlockTimeTasks();
\r
221 vStartRecursiveMutexTasks();
\r
223 /* Start the tasks defined within this file. */
\r
224 xTaskCreate( vLEDTask, "LED", configMINIMAL_STACK_SIZE, NULL, mainLED_TASK_PRIORITY, NULL );
\r
225 xTaskCreate( vCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
226 xTaskCreate( vPrintTask, "Print", configMINIMAL_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
227 xTaskCreate( vButtonHandlerTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
229 /* Start the scheduler. */
\r
230 vTaskStartScheduler();
\r
232 /* The scheduler should now be running, so we will only ever reach here if we
\r
233 ran out of heap space. */
\r
237 /*-----------------------------------------------------------*/
\r
239 static void vLEDTask( void *pvParameters )
\r
241 /* Just to remove compiler warnings. */
\r
242 ( void ) pvParameters;
\r
244 /* Configure IO. */
\r
245 IO0DIR |= mainLED_BIT;
\r
246 IO0SET = mainLED_BIT;
\r
250 /* Not very exiting - just delay... */
\r
251 vTaskDelay( xLED_Delay );
\r
253 /* ...set the IO ... */
\r
254 IO0CLR = mainLED_BIT;
\r
256 /* ...delay again... */
\r
257 vTaskDelay( xLED_Delay );
\r
259 /* ...then clear the IO. */
\r
260 IO0SET = mainLED_BIT;
\r
263 /*-----------------------------------------------------------*/
\r
265 static void vCheckTask( void *pvParameters )
\r
267 portBASE_TYPE xErrorOccurred = pdFALSE;
\r
268 TickType_t xLastExecutionTime;
\r
269 const char * const pcPassMessage = "PASS\n";
\r
270 const char * const pcFailMessage = "FAIL\n";
\r
272 /* Just to remove compiler warnings. */
\r
273 ( void ) pvParameters;
\r
275 /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
\r
276 works correctly. */
\r
277 xLastExecutionTime = xTaskGetTickCount();
\r
281 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
282 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
284 /* Has an error been found in any task? */
\r
286 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
288 xErrorOccurred = pdTRUE;
\r
291 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
293 xErrorOccurred = pdTRUE;
\r
296 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
298 xErrorOccurred = pdTRUE;
\r
301 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
303 xErrorOccurred = pdTRUE;
\r
306 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
308 xErrorOccurred = pdTRUE;
\r
311 #if configUSE_PREEMPTION == 1
\r
313 /* The timing of console output when not using the preemptive
\r
314 scheduler causes the block time tests to detect a timing problem. */
\r
315 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
317 xErrorOccurred = pdTRUE;
\r
322 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
324 xErrorOccurred = pdTRUE;
\r
327 /* Send either a pass or fail message. If an error is found it is
\r
328 never cleared again. */
\r
329 if( xErrorOccurred == pdTRUE )
\r
331 xLED_Delay = mainERROR_LED_DELAY;
\r
332 xQueueSend( xPrintQueue, &pcFailMessage, portMAX_DELAY );
\r
336 xQueueSend( xPrintQueue, &pcPassMessage, portMAX_DELAY );
\r
340 /*-----------------------------------------------------------*/
\r
342 static void vPrintTask( void *pvParameters )
\r
346 /* Just to stop compiler warnings. */
\r
347 ( void ) pvParameters;
\r
351 /* Wait for a message to arrive. */
\r
352 while( xQueueReceive( xPrintQueue, &pcMessage, portMAX_DELAY ) != pdPASS );
\r
354 /* Write the message to the terminal IO. */
\r
356 debug_printf( "%s", pcMessage );
\r
360 /*-----------------------------------------------------------*/
\r
362 static void vButtonHandlerTask( void *pvParameters )
\r
364 static char cListBuffer[ mainLIST_BUFFER_SIZE ];
\r
365 const char *pcList = &( cListBuffer[ 0 ] );
\r
366 const char * const pcHeader = "\nTask State Priority Stack #\n************************************************";
\r
367 extern void (vButtonISRWrapper) ( void );
\r
369 /* Just to stop compiler warnings. */
\r
370 ( void ) pvParameters;
\r
372 /* Configure the interrupt. */
\r
373 portENTER_CRITICAL();
\r
375 /* Configure P0.14 to generate interrupts. */
\r
376 PINSEL0 |= mainP0_14__EINT_1;
\r
377 EXTMODE = mainEINT_1_EDGE_SENSITIVE;
\r
378 EXTPOLAR = mainEINT_1_FALLING_EDGE_SENSITIVE;
\r
380 /* Setup the VIC for EINT 1. */
\r
381 VICIntSelect &= ~mainEINT_1_VIC_CHANNEL_BIT;
\r
382 VICIntEnable |= mainEINT_1_VIC_CHANNEL_BIT;
\r
383 VICVectAddr1 = ( long ) vButtonISRWrapper;
\r
384 VICVectCntl1 = mainEINT_1_ENABLE_BIT | mainEINT_1_CHANNEL;
\r
386 portEXIT_CRITICAL();
\r
390 /* For debouncing, wait a while then clear the semaphore. */
\r
391 vTaskDelay( mainSHORT_DELAY );
\r
392 xSemaphoreTake( xButtonSemaphore, mainNO_DELAY );
\r
394 /* Wait for an interrupt. */
\r
395 xSemaphoreTake( xButtonSemaphore, portMAX_DELAY );
\r
397 /* Send the column headers to the print task for display. */
\r
398 xQueueSend( xPrintQueue, &pcHeader, portMAX_DELAY );
\r
400 /* Create the list of task states. */
\r
401 vTaskList( cListBuffer );
\r
403 /* Send the task status information to the print task for display. */
\r
404 xQueueSend( xPrintQueue, &pcList, portMAX_DELAY );
\r
407 /*-----------------------------------------------------------*/
\r
409 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
411 /* Check pcTaskName for the name of the offending task, or pxCurrentTCB
\r
412 if pcTaskName has itself been corrupted. */
\r
414 ( void ) pcTaskName;
\r