]> begriffs open source - freertos/blob - Demo/CORTEX_LPC1766_GCC_RedSuite/main.c
Add new LPC1768 RedSuite demo.
[freertos] / Demo / CORTEX_LPC1766_GCC_RedSuite / main.c
1 /*\r
2         FreeRTOS.org V5.4.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify it\r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the 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.org without being obliged to provide\r
11         the source code for any proprietary components.  Alternative commercial\r
12         license and support terms are also available upon request.  See the \r
13         licensing section of http://www.FreeRTOS.org for full details.\r
14 \r
15         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
16         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
17         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
18         more details.\r
19 \r
20         You should have received a copy of the GNU General Public License along\r
21         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
22         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 \r
53 /*\r
54  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
55  * documentation provides more details of the standard demo application tasks\r
56  * (which just exist to test the kernel port and provide an example of how to use\r
57  * each FreeRTOS API function).\r
58  * \r
59  * In addition to the standard demo tasks, the following tasks and tests are\r
60  * defined and/or created within this file:\r
61  *\r
62  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that\r
63  * is permitted to access the display directly.  Other tasks wishing to write a\r
64  * message to the LCD send the message on a queue to the LCD task instead of\r
65  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting\r
66  * for messages - waking and displaying the messages as they arrive.  The use\r
67  * of a gatekeeper in this manner permits both tasks and interrupts to write to \r
68  * the LCD without worrying about mutual exclusion.  This is demonstrated by the \r
69  * check hook (see below) which sends messages to the display even though it \r
70  * executes from an interrupt context.\r
71  *\r
72  * "Check" hook -  This only executes fully every five seconds from the tick \r
73  * hook.  Its main function is to check that all the standard demo tasks are \r
74  * still operational.  Should any unexpected behaviour be discovered within a \r
75  * demo task then the tick hook will write an error to the LCD (via the LCD task).  \r
76  * If all the demo tasks are executing with their expected behaviour then the \r
77  * check task writes PASS to the LCD (again via the LCD task), as described above.\r
78  *\r
79  * LED tasks - These just demonstrate how multiple instances of a single task\r
80  * definition can be created.  Each LED task simply toggles an LED.  The task\r
81  * parameter is used to pass the number of the LED to be toggled into the task.\r
82  * \r
83  * "uIP" task -  This is the task that handles the uIP stack.  All TCP/IP\r
84  * processing is performed in this task.\r
85  */\r
86 \r
87 /* Standard includes. */\r
88 #include <stdio.h>\r
89 \r
90 /* Scheduler includes. */\r
91 #include "FreeRTOS.h"\r
92 #include "task.h"\r
93 #include "queue.h"\r
94 #include "semphr.h"\r
95 \r
96 /* Hardware library includes. */\r
97 #include "LPC17xx_defs.h"\r
98 \r
99 /* Demo app includes. */\r
100 #include "BlockQ.h"\r
101 #include "integer.h"\r
102 #include "blocktim.h"\r
103 #include "flash.h"\r
104 #include "partest.h"\r
105 #include "semtest.h"\r
106 #include "PollQ.h"\r
107 #include "GenQTest.h"\r
108 #include "QPeek.h"\r
109 #include "recmutex.h"\r
110 #include "lcd/portlcd.h"\r
111 #include "LED.h"\r
112 \r
113 /*-----------------------------------------------------------*/\r
114 \r
115 /* The number of LED tasks that will be created. */\r
116 #define mainNUM_LED_TASKS                                       ( 6 )\r
117 \r
118 /* The time between cycles of the 'check' functionality (defined within the\r
119 tick hook. */\r
120 #define mainCHECK_DELAY                                         ( ( portTickType ) 5000 / portTICK_RATE_MS )\r
121 \r
122 /* Task priorities. */\r
123 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
124 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
125 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
126 #define mainUIP_TASK_PRIORITY                           ( tskIDLE_PRIORITY + 3 )\r
127 #define mainLCD_TASK_PRIORITY                           ( tskIDLE_PRIORITY + 2 )\r
128 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
129 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
130 \r
131 /* The WEB server has a larger stack as it utilises stack hungry string\r
132 handling library calls. */\r
133 #define mainBASIC_WEB_STACK_SIZE            ( configMINIMAL_STACK_SIZE * 4 )\r
134 \r
135 /* The length of the queue used to send messages to the LCD task. */\r
136 #define mainQUEUE_SIZE                                          ( 3 )\r
137 \r
138 /*-----------------------------------------------------------*/\r
139 \r
140 /*\r
141  * Configure the hardware for the demo.\r
142  */\r
143 static void prvSetupHardware( void );\r
144 \r
145 /*\r
146  * Very simple task that toggles an LED.\r
147  */\r
148 static void vLEDTask( void *pvParameters );\r
149 \r
150 /* \r
151  * The task that handles the uIP stack.  All TCP/IP processing is performed in\r
152  * this task.\r
153  */\r
154 extern void vuIP_Task( void *pvParameters );\r
155 \r
156 /*\r
157  * The LCD gatekeeper task as described in the comments at the top of this file. \r
158  * */\r
159 static void vLCDTask( void *pvParameters );\r
160 \r
161 /*-----------------------------------------------------------*/\r
162 \r
163 /* The queue used to send messages to the LCD task. */\r
164 xQueueHandle xLCDQueue;\r
165 \r
166 \r
167 \r
168 /*-----------------------------------------------------------*/\r
169 \r
170 int main( void )\r
171 {\r
172 long l;\r
173 \r
174         /* Configure the hardware for use by this demo. */\r
175         prvSetupHardware();\r
176 \r
177         /* Start the standard demo tasks.  These are just here to exercise the\r
178         kernel port and provide examples of how the FreeRTOS API can be used. */\r
179         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
180     vCreateBlockTimeTasks();\r
181     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
182     vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
183     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
184     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
185     vStartQueuePeekTasks();\r
186     vStartRecursiveMutexTasks();\r
187 \r
188         /* Start the tasks that toggle LEDs - the LED to toggle is passed in as the\r
189         task parameter. */\r
190         for( l = 0; l < mainNUM_LED_TASKS; l++ )\r
191         {\r
192                 xTaskCreate( vLEDTask, (signed char *) "LED", configMINIMAL_STACK_SIZE, ( void * ) l, tskIDLE_PRIORITY, NULL );\r
193         }\r
194     \r
195         /* Create the uIP task.  The WEB server runs in this task. */\r
196     xTaskCreate( vuIP_Task, ( signed char * ) "uIP", mainBASIC_WEB_STACK_SIZE, ( void * ) NULL, mainUIP_TASK_PRIORITY, NULL );\r
197         \r
198         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
199         are received via this queue. */\r
200         xLCDQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( xLCDMessage ) );\r
201 \r
202         /* Start the LCD gatekeeper task - as described in the comments at the top\r
203         of this file. */\r
204         xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE * 2, NULL, mainLCD_TASK_PRIORITY, NULL );\r
205     \r
206     /* Start the scheduler. */\r
207         vTaskStartScheduler();\r
208 \r
209     /* Will only get here if there was insufficient memory to create the idle\r
210     task.  The idle task is created within vTaskStartScheduler(). */\r
211         for( ;; );\r
212 }\r
213 /*-----------------------------------------------------------*/\r
214 \r
215 static void vLEDTask( void *pvParameters ) \r
216 {\r
217 /* The LED to toggle is passed in as the task parameter. */\r
218 long lLED = ( long ) pvParameters;\r
219 unsigned long ulLEDToToggle = 1 << lLED;\r
220 \r
221 /* Calculate a delay period based on the LED number. */\r
222 unsigned long ulDelayPeriod = 100 * ( lLED + 1 );\r
223 \r
224         for( ;; )\r
225         {\r
226                 /* Delay for the calculated time. */\r
227                 vTaskDelay( ulDelayPeriod );\r
228                 \r
229                 /* Toggle the LED before going back to delay again. */\r
230                 vToggleLED( ulLEDToToggle );\r
231         }\r
232 }\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 void vLCDTask( void *pvParameters )\r
236 {\r
237 xLCDMessage xMessage;\r
238 unsigned long ulRow = 0;\r
239 char cIPAddr[ 17 ]; /* To fit max IP address length of xxx.xxx.xxx.xxx\0 */\r
240 \r
241         ( void ) pvParameters;\r
242 \r
243         /* The LCD gatekeeper task as described in the comments at the top of this\r
244         file. */\r
245         \r
246         /* Initialise the LCD and display a startup message that includes the\r
247         configured IP address. */\r
248         LCD_init();\r
249         LCD_cur_off();\r
250     LCD_cls();    \r
251     LCD_gotoxy( 1, 1 );\r
252     LCD_puts( "www.FreeRTOS.org" );\r
253     LCD_gotoxy( 1, 2 );\r
254     sprintf( cIPAddr, "%d.%d.%d.%d", configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
255     LCD_puts( cIPAddr );\r
256 \r
257         for( ;; )\r
258         {\r
259                 /* Wait for a message to arrive to be displayed. */\r
260                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );\r
261                 \r
262                 /* Clear the old message. */\r
263                 LCD_cls();\r
264                 \r
265                 /* Switch LCD rows, jut to make it obvious that messages are arriving. */\r
266                 ulRow++;                \r
267                 LCD_gotoxy( 1, ( ulRow & 0x01 ) + 1 );\r
268                 \r
269                 /* Display the received text. */\r
270                 LCD_puts( xMessage.pcMessage );\r
271         }\r
272 }\r
273 /*-----------------------------------------------------------*/\r
274 \r
275 void vApplicationTickHook( void )\r
276 {\r
277 static xLCDMessage xMessage = { "PASS" };\r
278 static unsigned portLONG ulTicksSinceLastDisplay = 0;\r
279 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
280 \r
281         /* Called from every tick interrupt as described in the comments at the top\r
282         of this file.  \r
283          \r
284         Have enough ticks passed to make it     time to perform our health status \r
285         check again? */\r
286         ulTicksSinceLastDisplay++;\r
287         if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )\r
288         {\r
289                 /* Reset the counter so these checks run again in mainCHECK_DELAY\r
290                 ticks time. */\r
291                 ulTicksSinceLastDisplay = 0;\r
292 \r
293                 /* Has an error been found in any task? */\r
294                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
295                 {\r
296                         xMessage.pcMessage = "ERROR: GEN Q";\r
297                 }\r
298                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
299                 {\r
300                         xMessage.pcMessage = "ERROR: PEEK Q";\r
301                 }\r
302                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
303                 {\r
304                         xMessage.pcMessage = "ERROR: BLOCK Q";\r
305                 }\r
306                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
307                 {\r
308                         xMessage.pcMessage = "ERROR: BLOCK TIME";\r
309                 }\r
310             else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
311             {\r
312                 xMessage.pcMessage = "ERROR: SEMAPHR";\r
313             }\r
314             else if( xArePollingQueuesStillRunning() != pdTRUE )\r
315             {\r
316                 xMessage.pcMessage = "ERROR: POLL Q";\r
317             }\r
318             else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
319             {\r
320                 xMessage.pcMessage = "ERROR: INT MATH";\r
321             }\r
322             else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
323             {\r
324                 xMessage.pcMessage = "ERROR: REC MUTEX";\r
325             }\r
326 \r
327                 /* Send the message to the OLED gatekeeper for display.  The\r
328                 xHigherPriorityTaskWoken parameter is not actually used here\r
329                 as this function is running in the tick interrupt anyway - but\r
330                 it must still be supplied. */\r
331                 xHigherPriorityTaskWoken = pdFALSE;\r
332                 xQueueSendFromISR( xLCDQueue, &xMessage, &xHigherPriorityTaskWoken );\r
333         }\r
334 }\r
335 /*-----------------------------------------------------------*/\r
336 \r
337 void prvSetupHardware( void )\r
338 {\r
339         /* Disable peripherals power. */\r
340         PCONP = 0;                                                      \r
341         \r
342         /* Enable GPIO power. */\r
343         PCONP = PCONP_PCGPIO;                           \r
344         \r
345         /* Disable TPIU. */\r
346         PINSEL10 = 0;                                           \r
347         \r
348         /* Disconnect the main PLL. */\r
349         PLL0CON &= ~PLLCON_PLLC;                        \r
350         PLL0FEED = PLLFEED_FEED1;\r
351         PLL0FEED = PLLFEED_FEED2;\r
352         while ((PLL0STAT & PLLSTAT_PLLC) != 0); \r
353         \r
354         /* Turn off the main PLL. */\r
355         PLL0CON &= ~PLLCON_PLLE;                        \r
356         PLL0FEED = PLLFEED_FEED1;\r
357         PLL0FEED = PLLFEED_FEED2;\r
358         while ((PLL0STAT & PLLSTAT_PLLE) != 0);    \r
359         \r
360         /* No CPU clock divider. */\r
361         CCLKCFG = 0;\r
362         \r
363         /* OSCEN. */\r
364         SCS = 0x20;                                                     \r
365         while ((SCS & 0x40) == 0);\r
366         \r
367         /* Use main oscillator. */\r
368         CLKSRCSEL = 1;                                          \r
369         PLL0CFG = (PLLCFG_MUL16 | PLLCFG_DIV1);\r
370         \r
371         PLL0FEED = PLLFEED_FEED1;\r
372         PLL0FEED = PLLFEED_FEED2;  \r
373         \r
374         /*  Activate the PLL by turning it on then feeding the correct \r
375         sequence of bytes. */\r
376         PLL0CON  = PLLCON_PLLE;\r
377         PLL0FEED = PLLFEED_FEED1;\r
378         PLL0FEED = PLLFEED_FEED2;\r
379         \r
380         /* 6x CPU clock divider (64 MHz) */\r
381         CCLKCFG = 5;                                            \r
382         \r
383         /*  Wait for the PLL to lock. */\r
384         while ((PLL0STAT & PLLSTAT_PLOCK) == 0);\r
385         \r
386         /*  Connect the PLL. */\r
387         PLL0CON  = PLLCON_PLLC | PLLCON_PLLE;\r
388         PLL0FEED = PLLFEED_FEED1;\r
389         PLL0FEED = PLLFEED_FEED2;\r
390         \r
391         /*  Setup the peripheral bus to be the same as the PLL output (64 MHz). */\r
392         PCLKSEL0 = 0x05555555;\r
393 \r
394         /* Configure LED GPIOs as outputs. */\r
395         FIO2DIR  = 0xff;\r
396         FIO2CLR  = 0xff;\r
397         FIO2MASK = 0;\r
398 }\r
399 /*-----------------------------------------------------------*/\r
400 \r
401 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed portCHAR *pcTaskName )\r
402 {\r
403         /* This function will get called if a task overflows its stack. */\r
404         \r
405         ( void ) pxTask;\r
406         ( void ) pcTaskName;\r
407         \r
408         for( ;; );\r
409 }\r
410 /*-----------------------------------------------------------*/\r
411 \r
412 void vConfigureTimerForRunTimeStats( void )\r
413 {\r
414 const unsigned long TCR_COUNT_RESET = 2, CTCR_CTM_TIMER = 0x00, TCR_COUNT_ENABLE = 0x01;\r
415 \r
416         /* This function configures a timer that is used as the time base when\r
417         collecting run time statistical information - basically the percentage\r
418         of CPU time that each task is utilising.  It is called automatically when\r
419         the scheduler is started (assuming configGENERATE_RUN_TIME_STATS is set\r
420         to 1. */\r
421         \r
422         /* Power up and feed the timer. */\r
423         PCONP |= 0x02UL;\r
424         PCLKSEL0 = (PCLKSEL0 & (~(0x3<<2))) | (0x01 << 2);\r
425         \r
426         /* Reset Timer 0 */\r
427         T0TCR = TCR_COUNT_RESET;\r
428         \r
429         /* Just count up. */\r
430         T0CTCR = CTCR_CTM_TIMER;\r
431 \r
432         /* Prescale to a frequency that is good enough to get a decent resolution,\r
433         but not too fast so as to overflow all the time. */\r
434         T0PR =  ( configCPU_CLOCK_HZ / 10000UL ) - 1UL;\r
435 \r
436         /* Start the counter. */\r
437         T0TCR = TCR_COUNT_ENABLE;\r
438 }\r
439 /*-----------------------------------------------------------*/\r
440 \r