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