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