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