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