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