2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 * This file contains a demo created to execute on the Rowley Associates
\r
31 * LPC2138 CrossFire development board.
\r
33 * main() creates all the demo application tasks, then starts the scheduler.
\r
34 * The WEB documentation provides more details of the standard demo application
\r
37 * Main.c also creates a task called "Check". This only executes every few
\r
38 * seconds but has a high priority so is guaranteed to get processor time.
\r
39 * Its function is to check that all the other tasks are still operational.
\r
40 * Each standard demo task maintains a unique count that is incremented each
\r
41 * time the task successfully completes its function. Should any error occur
\r
42 * within such a task the count is permanently halted. The check task inspects
\r
43 * the count of each task to ensure it has changed since the last time the
\r
44 * check task executed. If all the count variables have changed all the tasks
\r
45 * are still executing error free, and the check task writes "PASS" to the
\r
46 * CrossStudio terminal IO window. Should any task contain an error at any time
\r
47 * the error is latched and "FAIL" written to the terminal IO window.
\r
49 * Finally, main() sets up an interrupt service routine and task to handle
\r
50 * pushes of the button that is built into the CrossFire board. When the button
\r
51 * is pushed the ISR wakes the button task - which generates a table of task
\r
52 * status information which is also displayed on the terminal IO window.
\r
54 * A print task is defined to ensure exclusive and consistent access to the
\r
55 * terminal IO. This is the only task that is allowed to access the terminal.
\r
56 * The check and button task therefore do not access the terminal directly but
\r
57 * instead pass a pointer to the message they wish to display to the print task.
\r
60 /* Standard includes. */
\r
61 #include <__cross_studio_io.h>
\r
63 /* Scheduler includes. */
\r
64 #include "FreeRTOS.h"
\r
69 /* Demo app includes. */
\r
72 #include "dynamic.h"
\r
73 #include "integer.h"
\r
75 #include "blocktim.h"
\r
76 #include "recmutex.h"
\r
77 #include "semtest.h"
\r
79 /* Hardware configuration definitions. */
\r
80 #define mainBUS_CLK_FULL ( ( unsigned char ) 0x01 )
\r
81 #define mainLED_BIT 0x80000000
\r
82 #define mainP0_14__EINT_1 ( 2 << 28 )
\r
83 #define mainEINT_1_EDGE_SENSITIVE 2
\r
84 #define mainEINT_1_FALLING_EDGE_SENSITIVE 0
\r
85 #define mainEINT_1_CHANNEL 15
\r
86 #define mainEINT_1_VIC_CHANNEL_BIT ( 1 << mainEINT_1_CHANNEL )
\r
87 #define mainEINT_1_ENABLE_BIT ( 1 << 5 )
\r
89 /* Demo application definitions. */
\r
90 #define mainQUEUE_SIZE ( 3 )
\r
91 #define mainLED_DELAY ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
\r
92 #define mainERROR_LED_DELAY ( ( TickType_t ) 50 / portTICK_PERIOD_MS )
\r
93 #define mainCHECK_DELAY ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )
\r
94 #define mainLIST_BUFFER_SIZE 2048
\r
95 #define mainNO_DELAY ( 0 )
\r
96 #define mainSHORT_DELAY ( 150 / portTICK_PERIOD_MS )
\r
98 /* Task priorities. */
\r
99 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
100 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
101 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
102 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
103 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
104 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
106 /*-----------------------------------------------------------*/
\r
108 /* The semaphore used to wake the button task from within the external interrupt
\r
110 SemaphoreHandle_t xButtonSemaphore;
\r
112 /* The queue that is used to send message to vPrintTask for display in the
\r
113 terminal output window. */
\r
114 QueueHandle_t xPrintQueue;
\r
116 /* The rate at which the LED will toggle. The toggle rate increases if an
\r
117 error is detected in any task. */
\r
118 static TickType_t xLED_Delay = mainLED_DELAY;
\r
119 /*-----------------------------------------------------------*/
\r
122 * Simply flashes the on board LED every mainLED_DELAY milliseconds.
\r
124 static void vLEDTask( void *pvParameters );
\r
127 * Checks the status of all the demo tasks then prints a message to the
\r
128 * CrossStudio terminal IO windows. The message will be either PASS or FAIL
\r
129 * depending on the status of the demo applications tasks. A FAIL status will
\r
132 * Messages are not written directly to the terminal, but passed to vPrintTask
\r
135 static void vCheckTask( void *pvParameters );
\r
138 * Controls all terminal output. If a task wants to send a message to the
\r
139 * terminal IO it posts a pointer to the text to vPrintTask via a queue. This
\r
140 * ensures serial access to the terminal IO.
\r
142 static void vPrintTask( void *pvParameter );
\r
145 * Simply waits for an interrupt to be generated from the built in button, then
\r
146 * generates a table of tasks states that is then written by vPrintTask to the
\r
147 * terminal output window within CrossStudio.
\r
149 static void vButtonHandlerTask( void *pvParameters );
\r
151 /*-----------------------------------------------------------*/
\r
155 /* Setup the peripheral bus to be the same as the PLL output. */
\r
156 VPBDIV = mainBUS_CLK_FULL;
\r
158 /* Create the queue used to pass message to vPrintTask. */
\r
159 xPrintQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( char * ) );
\r
161 /* Create the semaphore used to wake vButtonHandlerTask(). */
\r
162 vSemaphoreCreateBinary( xButtonSemaphore );
\r
163 xSemaphoreTake( xButtonSemaphore, 0 );
\r
165 /* Start the standard demo tasks. */
\r
166 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
167 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
168 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
169 vStartDynamicPriorityTasks();
\r
170 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
172 #if configUSE_PREEMPTION == 1
\r
174 /* The timing of console output when not using the preemptive
\r
175 scheduler causes the block time tests to detect a timing problem. */
\r
176 vCreateBlockTimeTasks();
\r
180 vStartRecursiveMutexTasks();
\r
182 /* Start the tasks defined within this file. */
\r
183 xTaskCreate( vLEDTask, "LED", configMINIMAL_STACK_SIZE, NULL, mainLED_TASK_PRIORITY, NULL );
\r
184 xTaskCreate( vCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
185 xTaskCreate( vPrintTask, "Print", configMINIMAL_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
186 xTaskCreate( vButtonHandlerTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
188 /* Start the scheduler. */
\r
189 vTaskStartScheduler();
\r
191 /* The scheduler should now be running, so we will only ever reach here if we
\r
192 ran out of heap space. */
\r
196 /*-----------------------------------------------------------*/
\r
198 static void vLEDTask( void *pvParameters )
\r
200 /* Just to remove compiler warnings. */
\r
201 ( void ) pvParameters;
\r
203 /* Configure IO. */
\r
204 IO0DIR |= mainLED_BIT;
\r
205 IO0SET = mainLED_BIT;
\r
209 /* Not very exiting - just delay... */
\r
210 vTaskDelay( xLED_Delay );
\r
212 /* ...set the IO ... */
\r
213 IO0CLR = mainLED_BIT;
\r
215 /* ...delay again... */
\r
216 vTaskDelay( xLED_Delay );
\r
218 /* ...then clear the IO. */
\r
219 IO0SET = mainLED_BIT;
\r
222 /*-----------------------------------------------------------*/
\r
224 static void vCheckTask( void *pvParameters )
\r
226 portBASE_TYPE xErrorOccurred = pdFALSE;
\r
227 TickType_t xLastExecutionTime;
\r
228 const char * const pcPassMessage = "PASS\n";
\r
229 const char * const pcFailMessage = "FAIL\n";
\r
231 /* Just to remove compiler warnings. */
\r
232 ( void ) pvParameters;
\r
234 /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
\r
235 works correctly. */
\r
236 xLastExecutionTime = xTaskGetTickCount();
\r
240 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
241 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
243 /* Has an error been found in any task? */
\r
245 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
247 xErrorOccurred = pdTRUE;
\r
250 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
252 xErrorOccurred = pdTRUE;
\r
255 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
257 xErrorOccurred = pdTRUE;
\r
260 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
262 xErrorOccurred = pdTRUE;
\r
265 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
267 xErrorOccurred = pdTRUE;
\r
270 #if configUSE_PREEMPTION == 1
\r
272 /* The timing of console output when not using the preemptive
\r
273 scheduler causes the block time tests to detect a timing problem. */
\r
274 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
276 xErrorOccurred = pdTRUE;
\r
281 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
283 xErrorOccurred = pdTRUE;
\r
286 /* Send either a pass or fail message. If an error is found it is
\r
287 never cleared again. */
\r
288 if( xErrorOccurred == pdTRUE )
\r
290 xLED_Delay = mainERROR_LED_DELAY;
\r
291 xQueueSend( xPrintQueue, &pcFailMessage, portMAX_DELAY );
\r
295 xQueueSend( xPrintQueue, &pcPassMessage, portMAX_DELAY );
\r
299 /*-----------------------------------------------------------*/
\r
301 static void vPrintTask( void *pvParameters )
\r
305 /* Just to stop compiler warnings. */
\r
306 ( void ) pvParameters;
\r
310 /* Wait for a message to arrive. */
\r
311 while( xQueueReceive( xPrintQueue, &pcMessage, portMAX_DELAY ) != pdPASS );
\r
313 /* Write the message to the terminal IO. */
\r
315 debug_printf( "%s", pcMessage );
\r
319 /*-----------------------------------------------------------*/
\r
321 static void vButtonHandlerTask( void *pvParameters )
\r
323 static char cListBuffer[ mainLIST_BUFFER_SIZE ];
\r
324 const char *pcList = &( cListBuffer[ 0 ] );
\r
325 const char * const pcHeader = "\nTask State Priority Stack #\n************************************************";
\r
326 extern void (vButtonISRWrapper) ( void );
\r
328 /* Just to stop compiler warnings. */
\r
329 ( void ) pvParameters;
\r
331 /* Configure the interrupt. */
\r
332 portENTER_CRITICAL();
\r
334 /* Configure P0.14 to generate interrupts. */
\r
335 PINSEL0 |= mainP0_14__EINT_1;
\r
336 EXTMODE = mainEINT_1_EDGE_SENSITIVE;
\r
337 EXTPOLAR = mainEINT_1_FALLING_EDGE_SENSITIVE;
\r
339 /* Setup the VIC for EINT 1. */
\r
340 VICIntSelect &= ~mainEINT_1_VIC_CHANNEL_BIT;
\r
341 VICIntEnable |= mainEINT_1_VIC_CHANNEL_BIT;
\r
342 VICVectAddr1 = ( long ) vButtonISRWrapper;
\r
343 VICVectCntl1 = mainEINT_1_ENABLE_BIT | mainEINT_1_CHANNEL;
\r
345 portEXIT_CRITICAL();
\r
349 /* For debouncing, wait a while then clear the semaphore. */
\r
350 vTaskDelay( mainSHORT_DELAY );
\r
351 xSemaphoreTake( xButtonSemaphore, mainNO_DELAY );
\r
353 /* Wait for an interrupt. */
\r
354 xSemaphoreTake( xButtonSemaphore, portMAX_DELAY );
\r
356 /* Send the column headers to the print task for display. */
\r
357 xQueueSend( xPrintQueue, &pcHeader, portMAX_DELAY );
\r
359 /* Create the list of task states. */
\r
360 vTaskList( cListBuffer );
\r
362 /* Send the task status information to the print task for display. */
\r
363 xQueueSend( xPrintQueue, &pcList, portMAX_DELAY );
\r
366 /*-----------------------------------------------------------*/
\r
368 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
370 /* Check pcTaskName for the name of the offending task, or pxCurrentTCB
\r
371 if pcTaskName has itself been corrupted. */
\r
373 ( void ) pcTaskName;
\r