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