2 * FreeRTOS Kernel V10.1.1
3 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
29 * This file contains a demo created to execute on the Rowley Associates
30 * LPC2138 CrossFire development board.
32 * main() creates all the demo application tasks, then starts the scheduler.
33 * The WEB documentation provides more details of the standard demo application
36 * Main.c also creates a task called "Check". This only executes every few
37 * seconds but has a high priority so is guaranteed to get processor time.
38 * Its function is to check that all the other tasks are still operational.
39 * Each standard demo task maintains a unique count that is incremented each
40 * time the task successfully completes its function. Should any error occur
41 * within such a task the count is permanently halted. The check task inspects
42 * the count of each task to ensure it has changed since the last time the
43 * check task executed. If all the count variables have changed all the tasks
44 * are still executing error free, and the check task writes "PASS" to the
45 * CrossStudio terminal IO window. Should any task contain an error at any time
46 * the error is latched and "FAIL" written to the terminal IO window.
48 * Finally, main() sets up an interrupt service routine and task to handle
49 * pushes of the button that is built into the CrossFire board. When the button
50 * is pushed the ISR wakes the button task - which generates a table of task
51 * status information which is also displayed on the terminal IO window.
53 * A print task is defined to ensure exclusive and consistent access to the
54 * terminal IO. This is the only task that is allowed to access the terminal.
55 * The check and button task therefore do not access the terminal directly but
56 * instead pass a pointer to the message they wish to display to the print task.
59 /* Standard includes. */
60 #include <__cross_studio_io.h>
62 /* Scheduler includes. */
68 /* Demo app includes. */
78 /* Hardware configuration definitions. */
79 #define mainBUS_CLK_FULL ( ( unsigned char ) 0x01 )
80 #define mainLED_BIT 0x80000000
81 #define mainP0_14__EINT_1 ( 2 << 28 )
82 #define mainEINT_1_EDGE_SENSITIVE 2
83 #define mainEINT_1_FALLING_EDGE_SENSITIVE 0
84 #define mainEINT_1_CHANNEL 15
85 #define mainEINT_1_VIC_CHANNEL_BIT ( 1 << mainEINT_1_CHANNEL )
86 #define mainEINT_1_ENABLE_BIT ( 1 << 5 )
88 /* Demo application definitions. */
89 #define mainQUEUE_SIZE ( 3 )
90 #define mainLED_DELAY ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
91 #define mainERROR_LED_DELAY ( ( TickType_t ) 50 / portTICK_PERIOD_MS )
92 #define mainCHECK_DELAY ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )
93 #define mainLIST_BUFFER_SIZE 2048
94 #define mainNO_DELAY ( 0 )
95 #define mainSHORT_DELAY ( 150 / portTICK_PERIOD_MS )
97 /* Task priorities. */
98 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
99 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
100 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
101 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
102 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
103 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 )
105 /*-----------------------------------------------------------*/
107 /* The semaphore used to wake the button task from within the external interrupt
109 SemaphoreHandle_t xButtonSemaphore;
111 /* The queue that is used to send message to vPrintTask for display in the
112 terminal output window. */
113 QueueHandle_t xPrintQueue;
115 /* The rate at which the LED will toggle. The toggle rate increases if an
116 error is detected in any task. */
117 static TickType_t xLED_Delay = mainLED_DELAY;
118 /*-----------------------------------------------------------*/
121 * Simply flashes the on board LED every mainLED_DELAY milliseconds.
123 static void vLEDTask( void *pvParameters );
126 * Checks the status of all the demo tasks then prints a message to the
127 * CrossStudio terminal IO windows. The message will be either PASS or FAIL
128 * depending on the status of the demo applications tasks. A FAIL status will
131 * Messages are not written directly to the terminal, but passed to vPrintTask
134 static void vCheckTask( void *pvParameters );
137 * Controls all terminal output. If a task wants to send a message to the
138 * terminal IO it posts a pointer to the text to vPrintTask via a queue. This
139 * ensures serial access to the terminal IO.
141 static void vPrintTask( void *pvParameter );
144 * Simply waits for an interrupt to be generated from the built in button, then
145 * generates a table of tasks states that is then written by vPrintTask to the
146 * terminal output window within CrossStudio.
148 static void vButtonHandlerTask( void *pvParameters );
150 /*-----------------------------------------------------------*/
154 /* Setup the peripheral bus to be the same as the PLL output. */
155 VPBDIV = mainBUS_CLK_FULL;
157 /* Create the queue used to pass message to vPrintTask. */
158 xPrintQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( char * ) );
160 /* Create the semaphore used to wake vButtonHandlerTask(). */
161 vSemaphoreCreateBinary( xButtonSemaphore );
162 xSemaphoreTake( xButtonSemaphore, 0 );
164 /* Start the standard demo tasks. */
165 vStartIntegerMathTasks( tskIDLE_PRIORITY );
166 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
167 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
168 vStartDynamicPriorityTasks();
169 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
171 #if configUSE_PREEMPTION == 1
173 /* The timing of console output when not using the preemptive
174 scheduler causes the block time tests to detect a timing problem. */
175 vCreateBlockTimeTasks();
179 vStartRecursiveMutexTasks();
181 /* Start the tasks defined within this file. */
182 xTaskCreate( vLEDTask, "LED", configMINIMAL_STACK_SIZE, NULL, mainLED_TASK_PRIORITY, NULL );
183 xTaskCreate( vCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
184 xTaskCreate( vPrintTask, "Print", configMINIMAL_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
185 xTaskCreate( vButtonHandlerTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
187 /* Start the scheduler. */
188 vTaskStartScheduler();
190 /* The scheduler should now be running, so we will only ever reach here if we
191 ran out of heap space. */
195 /*-----------------------------------------------------------*/
197 static void vLEDTask( void *pvParameters )
199 /* Just to remove compiler warnings. */
200 ( void ) pvParameters;
203 IO0DIR |= mainLED_BIT;
204 IO0SET = mainLED_BIT;
208 /* Not very exiting - just delay... */
209 vTaskDelay( xLED_Delay );
211 /* ...set the IO ... */
212 IO0CLR = mainLED_BIT;
214 /* ...delay again... */
215 vTaskDelay( xLED_Delay );
217 /* ...then clear the IO. */
218 IO0SET = mainLED_BIT;
221 /*-----------------------------------------------------------*/
223 static void vCheckTask( void *pvParameters )
225 portBASE_TYPE xErrorOccurred = pdFALSE;
226 TickType_t xLastExecutionTime;
227 const char * const pcPassMessage = "PASS\n";
228 const char * const pcFailMessage = "FAIL\n";
230 /* Just to remove compiler warnings. */
231 ( void ) pvParameters;
233 /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
235 xLastExecutionTime = xTaskGetTickCount();
239 /* Perform this check every mainCHECK_DELAY milliseconds. */
240 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
242 /* Has an error been found in any task? */
244 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
246 xErrorOccurred = pdTRUE;
249 if( xArePollingQueuesStillRunning() != pdTRUE )
251 xErrorOccurred = pdTRUE;
254 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
256 xErrorOccurred = pdTRUE;
259 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
261 xErrorOccurred = pdTRUE;
264 if( xAreBlockingQueuesStillRunning() != pdTRUE )
266 xErrorOccurred = pdTRUE;
269 #if configUSE_PREEMPTION == 1
271 /* The timing of console output when not using the preemptive
272 scheduler causes the block time tests to detect a timing problem. */
273 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
275 xErrorOccurred = pdTRUE;
280 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
282 xErrorOccurred = pdTRUE;
285 /* Send either a pass or fail message. If an error is found it is
286 never cleared again. */
287 if( xErrorOccurred == pdTRUE )
289 xLED_Delay = mainERROR_LED_DELAY;
290 xQueueSend( xPrintQueue, &pcFailMessage, portMAX_DELAY );
294 xQueueSend( xPrintQueue, &pcPassMessage, portMAX_DELAY );
298 /*-----------------------------------------------------------*/
300 static void vPrintTask( void *pvParameters )
304 /* Just to stop compiler warnings. */
305 ( void ) pvParameters;
309 /* Wait for a message to arrive. */
310 while( xQueueReceive( xPrintQueue, &pcMessage, portMAX_DELAY ) != pdPASS );
312 /* Write the message to the terminal IO. */
314 debug_printf( "%s", pcMessage );
318 /*-----------------------------------------------------------*/
320 static void vButtonHandlerTask( void *pvParameters )
322 static char cListBuffer[ mainLIST_BUFFER_SIZE ];
323 const char *pcList = &( cListBuffer[ 0 ] );
324 const char * const pcHeader = "\nTask State Priority Stack #\n************************************************";
325 extern void (vButtonISRWrapper) ( void );
327 /* Just to stop compiler warnings. */
328 ( void ) pvParameters;
330 /* Configure the interrupt. */
331 portENTER_CRITICAL();
333 /* Configure P0.14 to generate interrupts. */
334 PINSEL0 |= mainP0_14__EINT_1;
335 EXTMODE = mainEINT_1_EDGE_SENSITIVE;
336 EXTPOLAR = mainEINT_1_FALLING_EDGE_SENSITIVE;
338 /* Setup the VIC for EINT 1. */
339 VICIntSelect &= ~mainEINT_1_VIC_CHANNEL_BIT;
340 VICIntEnable |= mainEINT_1_VIC_CHANNEL_BIT;
341 VICVectAddr1 = ( long ) vButtonISRWrapper;
342 VICVectCntl1 = mainEINT_1_ENABLE_BIT | mainEINT_1_CHANNEL;
348 /* For debouncing, wait a while then clear the semaphore. */
349 vTaskDelay( mainSHORT_DELAY );
350 xSemaphoreTake( xButtonSemaphore, mainNO_DELAY );
352 /* Wait for an interrupt. */
353 xSemaphoreTake( xButtonSemaphore, portMAX_DELAY );
355 /* Send the column headers to the print task for display. */
356 xQueueSend( xPrintQueue, &pcHeader, portMAX_DELAY );
358 /* Create the list of task states. */
359 vTaskList( cListBuffer );
361 /* Send the task status information to the print task for display. */
362 xQueueSend( xPrintQueue, &pcList, portMAX_DELAY );
365 /*-----------------------------------------------------------*/
367 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
369 /* Check pcTaskName for the name of the offending task, or pxCurrentTCB
370 if pcTaskName has itself been corrupted. */