]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_LPC2138_Rowley/main.c
Change 'signed char *pcTaskName) to 'char *pcTaskName' in vApplicationStackOverflowHo...
[freertos] / FreeRTOS / Demo / ARM7_LPC2138_Rowley / main.c
1 /*\r
2     FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*\r
67  * This file contains a demo created to execute on the Rowley Associates\r
68  * LPC2138 CrossFire development board.\r
69  *\r
70  * main() creates all the demo application tasks, then starts the scheduler.\r
71  * The WEB documentation provides more details of the standard demo application\r
72  * tasks.\r
73  *\r
74  * Main.c also creates a task called "Check".  This only executes every few\r
75  * seconds but has a high priority so is guaranteed to get processor time.\r
76  * Its function is to check that all the other tasks are still operational.\r
77  * Each standard demo task maintains a unique count that is incremented each\r
78  * time the task successfully completes its function.  Should any error occur\r
79  * within such a task the count is permanently halted.  The check task inspects\r
80  * the count of each task to ensure it has changed since the last time the\r
81  * check task executed.  If all the count variables have changed all the tasks\r
82  * are still executing error free, and the check task writes "PASS" to the\r
83  * CrossStudio terminal IO window.  Should any task contain an error at any time\r
84  * the error is latched and "FAIL" written to the terminal IO window.\r
85  *\r
86  * Finally, main() sets up an interrupt service routine and task to handle\r
87  * pushes of the button that is built into the CrossFire board.  When the button\r
88  * is pushed the ISR wakes the button task - which generates a table of task\r
89  * status information which is also displayed on the terminal IO window.\r
90  *\r
91  * A print task is defined to ensure exclusive and consistent access to the\r
92  * terminal IO.  This is the only task that is allowed to access the terminal.\r
93  * The check and button task therefore do not access the terminal directly but\r
94  * instead pass a pointer to the message they wish to display to the print task.\r
95  */\r
96 \r
97 /* Standard includes. */\r
98 #include <__cross_studio_io.h>\r
99 \r
100 /* Scheduler includes. */\r
101 #include "FreeRTOS.h"\r
102 #include "task.h"\r
103 #include "queue.h"\r
104 #include "semphr.h"\r
105 \r
106 /* Demo app includes. */\r
107 #include "BlockQ.h"\r
108 #include "death.h"\r
109 #include "dynamic.h"\r
110 #include "integer.h"\r
111 #include "PollQ.h"\r
112 #include "blocktim.h"\r
113 #include "recmutex.h"\r
114 #include "semtest.h"\r
115 \r
116 /* Hardware configuration definitions. */\r
117 #define mainBUS_CLK_FULL                                        ( ( unsigned char ) 0x01 )\r
118 #define mainLED_BIT                                                     0x80000000\r
119 #define mainP0_14__EINT_1                                       ( 2 << 28 )\r
120 #define mainEINT_1_EDGE_SENSITIVE                       2\r
121 #define mainEINT_1_FALLING_EDGE_SENSITIVE       0\r
122 #define mainEINT_1_CHANNEL                                      15\r
123 #define mainEINT_1_VIC_CHANNEL_BIT                      ( 1 << mainEINT_1_CHANNEL )\r
124 #define mainEINT_1_ENABLE_BIT                           ( 1 << 5 )\r
125 \r
126 /* Demo application definitions. */\r
127 #define mainQUEUE_SIZE                                          ( 3 )\r
128 #define mainLED_DELAY                                           ( ( portTickType ) 500 / portTICK_RATE_MS )\r
129 #define mainERROR_LED_DELAY                                     ( ( portTickType ) 50 / portTICK_RATE_MS )\r
130 #define mainCHECK_DELAY                                         ( ( portTickType ) 5000 / portTICK_RATE_MS )\r
131 #define mainLIST_BUFFER_SIZE                            2048\r
132 #define mainNO_DELAY                                            ( 0 )\r
133 #define mainSHORT_DELAY                                         ( 150 / portTICK_RATE_MS )\r
134 \r
135 /* Task priorities. */\r
136 #define mainLED_TASK_PRIORITY                           ( tskIDLE_PRIORITY + 2 )\r
137 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
138 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
139 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
140 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
141 #define mainPRINT_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 0 )\r
142 \r
143 /*-----------------------------------------------------------*/\r
144 \r
145 /* The semaphore used to wake the button task from within the external interrupt\r
146 handler. */\r
147 xSemaphoreHandle xButtonSemaphore;\r
148 \r
149 /* The queue that is used to send message to vPrintTask for display in the\r
150 terminal output window. */\r
151 xQueueHandle xPrintQueue;\r
152 \r
153 /* The rate at which the LED will toggle.  The toggle rate increases if an\r
154 error is detected in any task. */\r
155 static portTickType xLED_Delay = mainLED_DELAY;\r
156 /*-----------------------------------------------------------*/\r
157 \r
158 /*\r
159  * Simply flashes the on board LED every mainLED_DELAY milliseconds.\r
160  */\r
161 static void vLEDTask( void *pvParameters );\r
162 \r
163 /*\r
164  * Checks the status of all the demo tasks then prints a message to the\r
165  * CrossStudio terminal IO windows.  The message will be either PASS or FAIL\r
166  * depending on the status of the demo applications tasks.  A FAIL status will\r
167  * be latched.\r
168  *\r
169  * Messages are not written directly to the terminal, but passed to vPrintTask\r
170  * via a queue.\r
171  */\r
172 static void vCheckTask( void *pvParameters );\r
173 \r
174 /*\r
175  * Controls all terminal output.  If a task wants to send a message to the\r
176  * terminal IO it posts a pointer to the text to vPrintTask via a queue.  This\r
177  * ensures serial access to the terminal IO.\r
178  */\r
179 static void vPrintTask( void *pvParameter );\r
180 \r
181 /*\r
182  * Simply waits for an interrupt to be generated from the built in button, then\r
183  * generates a table of tasks states that is then written by vPrintTask to the\r
184  * terminal output window within CrossStudio.\r
185  */\r
186 static void vButtonHandlerTask( void *pvParameters );\r
187 \r
188 /*-----------------------------------------------------------*/\r
189 \r
190 int main( void )\r
191 {\r
192         /* Setup the peripheral bus to be the same as the PLL output. */\r
193         VPBDIV = mainBUS_CLK_FULL;\r
194 \r
195         /* Create the queue used to pass message to vPrintTask. */\r
196         xPrintQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( char * ) );\r
197 \r
198         /* Create the semaphore used to wake vButtonHandlerTask(). */\r
199         vSemaphoreCreateBinary( xButtonSemaphore );\r
200         xSemaphoreTake( xButtonSemaphore, 0 );\r
201 \r
202         /* Start the standard demo tasks. */\r
203         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
204         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
205         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
206         vStartDynamicPriorityTasks();\r
207         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
208 \r
209         #if configUSE_PREEMPTION == 1\r
210         {\r
211                 /* The timing of console output when not using the preemptive\r
212                 scheduler causes the block time tests to detect a timing problem. */\r
213                 vCreateBlockTimeTasks();\r
214         }\r
215         #endif\r
216 \r
217     vStartRecursiveMutexTasks();\r
218 \r
219         /* Start the tasks defined within this file. */\r
220         xTaskCreate( vLEDTask, "LED", configMINIMAL_STACK_SIZE, NULL, mainLED_TASK_PRIORITY, NULL );\r
221     xTaskCreate( vCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
222     xTaskCreate( vPrintTask, "Print", configMINIMAL_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );\r
223     xTaskCreate( vButtonHandlerTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
224 \r
225         /* Start the scheduler. */\r
226         vTaskStartScheduler();\r
227 \r
228         /* The scheduler should now be running, so we will only ever reach here if we\r
229         ran out of heap space. */\r
230 \r
231         return 0;\r
232 }\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 static void vLEDTask( void *pvParameters )\r
236 {\r
237         /* Just to remove compiler warnings. */\r
238         ( void ) pvParameters;\r
239 \r
240         /* Configure IO. */\r
241         IO0DIR |= mainLED_BIT;\r
242         IO0SET = mainLED_BIT;\r
243 \r
244         for( ;; )\r
245         {\r
246                 /* Not very exiting - just delay... */\r
247                 vTaskDelay( xLED_Delay );\r
248 \r
249                 /* ...set the IO ... */\r
250         IO0CLR = mainLED_BIT;\r
251 \r
252                 /* ...delay again... */\r
253                 vTaskDelay( xLED_Delay );\r
254 \r
255                 /* ...then clear the IO. */\r
256                 IO0SET = mainLED_BIT;\r
257         }\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 static void vCheckTask( void *pvParameters )\r
262 {\r
263 portBASE_TYPE xErrorOccurred = pdFALSE;\r
264 portTickType xLastExecutionTime;\r
265 const char * const pcPassMessage = "PASS\n";\r
266 const char * const pcFailMessage = "FAIL\n";\r
267 \r
268         /* Just to remove compiler warnings. */\r
269         ( void ) pvParameters;\r
270 \r
271         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()\r
272         works correctly. */\r
273         xLastExecutionTime = xTaskGetTickCount();\r
274 \r
275         for( ;; )\r
276         {\r
277                 /* Perform this check every mainCHECK_DELAY milliseconds. */\r
278                 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );\r
279 \r
280                 /* Has an error been found in any task? */\r
281 \r
282                 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
283                 {\r
284                         xErrorOccurred = pdTRUE;\r
285                 }\r
286 \r
287                 if( xArePollingQueuesStillRunning() != pdTRUE )\r
288                 {\r
289                         xErrorOccurred = pdTRUE;\r
290                 }\r
291 \r
292                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
293                 {\r
294                         xErrorOccurred = pdTRUE;\r
295                 }\r
296 \r
297                 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
298                 {\r
299                         xErrorOccurred = pdTRUE;\r
300                 }\r
301 \r
302                 if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
303                 {\r
304                         xErrorOccurred = pdTRUE;\r
305                 }\r
306 \r
307                 #if configUSE_PREEMPTION == 1\r
308                 {\r
309                         /* The timing of console output when not using the preemptive\r
310                         scheduler causes the block time tests to detect a timing problem. */\r
311                         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
312                         {\r
313                                 xErrorOccurred = pdTRUE;\r
314                         }\r
315                 }\r
316                 #endif\r
317 \r
318                 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
319                 {\r
320                         xErrorOccurred = pdTRUE;\r
321                 }\r
322 \r
323                 /* Send either a pass or fail message.  If an error is found it is\r
324                 never cleared again. */\r
325                 if( xErrorOccurred == pdTRUE )\r
326                 {\r
327                         xLED_Delay = mainERROR_LED_DELAY;\r
328                         xQueueSend( xPrintQueue, &pcFailMessage, portMAX_DELAY );\r
329                 }\r
330                 else\r
331                 {\r
332                         xQueueSend( xPrintQueue, &pcPassMessage, portMAX_DELAY );\r
333                 }\r
334         }\r
335 }\r
336 /*-----------------------------------------------------------*/\r
337 \r
338 static void vPrintTask( void *pvParameters )\r
339 {\r
340 char *pcMessage;\r
341 \r
342         /* Just to stop compiler warnings. */\r
343         ( void ) pvParameters;\r
344 \r
345         for( ;; )\r
346         {\r
347                 /* Wait for a message to arrive. */\r
348                 while( xQueueReceive( xPrintQueue, &pcMessage, portMAX_DELAY ) != pdPASS );\r
349 \r
350                 /* Write the message to the terminal IO. */\r
351                 #ifndef NDEBUG\r
352                         debug_printf( "%s", pcMessage );\r
353                 #endif\r
354         }\r
355 }\r
356 /*-----------------------------------------------------------*/\r
357 \r
358 static void vButtonHandlerTask( void *pvParameters )\r
359 {\r
360 static char cListBuffer[ mainLIST_BUFFER_SIZE ];\r
361 const char *pcList = &( cListBuffer[ 0 ] );\r
362 const char * const pcHeader = "\nTask          State  Priority  Stack   #\n************************************************";\r
363 extern void (vButtonISRWrapper) ( void );\r
364 \r
365         /* Just to stop compiler warnings. */\r
366         ( void ) pvParameters;\r
367 \r
368         /* Configure the interrupt. */\r
369         portENTER_CRITICAL();\r
370         {\r
371                 /* Configure P0.14 to generate interrupts. */\r
372                 PINSEL0 |= mainP0_14__EINT_1;\r
373                 EXTMODE = mainEINT_1_EDGE_SENSITIVE;\r
374                 EXTPOLAR = mainEINT_1_FALLING_EDGE_SENSITIVE;\r
375 \r
376                 /* Setup the VIC for EINT 1. */\r
377                 VICIntSelect &= ~mainEINT_1_VIC_CHANNEL_BIT;\r
378                 VICIntEnable |= mainEINT_1_VIC_CHANNEL_BIT;\r
379                 VICVectAddr1 = ( long ) vButtonISRWrapper;\r
380                 VICVectCntl1 = mainEINT_1_ENABLE_BIT | mainEINT_1_CHANNEL;\r
381         }\r
382         portEXIT_CRITICAL();\r
383 \r
384         for( ;; )\r
385         {\r
386                 /* For debouncing, wait a while then clear the semaphore. */\r
387                 vTaskDelay( mainSHORT_DELAY );\r
388                 xSemaphoreTake( xButtonSemaphore, mainNO_DELAY );\r
389 \r
390                 /* Wait for an interrupt. */\r
391                 xSemaphoreTake( xButtonSemaphore, portMAX_DELAY );\r
392 \r
393                 /* Send the column headers to the print task for display. */\r
394                 xQueueSend( xPrintQueue, &pcHeader, portMAX_DELAY );\r
395 \r
396                 /* Create the list of task states. */\r
397                 vTaskList( cListBuffer );\r
398 \r
399                 /* Send the task status information to the print task for display. */\r
400                 xQueueSend( xPrintQueue, &pcList, portMAX_DELAY );\r
401         }\r
402 }\r
403 /*-----------------------------------------------------------*/\r
404 \r
405 void vApplicationStackOverflowHook( xTaskHandle pxTask, char *pcTaskName )\r
406 {\r
407         /* Check pcTaskName for the name of the offending task, or pxCurrentTCB\r
408         if pcTaskName has itself been corrupted. */\r
409         ( void ) pxTask;\r
410         ( void ) pcTaskName;\r
411         for( ;; );\r
412 }\r
413 \r
414 \r
415 \r
416 \r
417 \r
418 \r