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