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