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