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