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