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