]> git.sur5r.net Git - freertos/blob - Demo/ARM7_LPC2368_Eclipse/RTOSDemo/main.c
0a5be4c192c4db137b6efbed4ebcd90029d68268
[freertos] / Demo / ARM7_LPC2368_Eclipse / RTOSDemo / main.c
1 /*\r
2     FreeRTOS V7.0.0 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
56  * documentation provides more details of the standard demo application tasks.\r
57  * In addition to the standard demo tasks, the following tasks and tests are\r
58  * defined and/or created within this file:\r
59  *\r
60  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that\r
61  * is permitted to access the display directly.  Other tasks wishing to write a\r
62  * message to the LCD send the message on a queue to the LCD task instead of\r
63  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting\r
64  * for messages - waking and displaying the messages as they arrive.\r
65  *\r
66  * "Check" hook -  This only executes every five seconds from the tick hook.\r
67  * Its main function is to check that all the standard demo tasks are still \r
68  * operational.  Should any unexpected behaviour within a demo task be discovered \r
69  * the tick hook will write an error to the LCD (via the LCD task).  If all the \r
70  * demo tasks are executing with their expected behaviour then the check task \r
71  * writes PASS to the LCD (again via the LCD task), as described above.\r
72  *\r
73  * "uIP" task -  This is the task that handles the uIP stack.  All TCP/IP\r
74  * processing is performed in this task.\r
75  */\r
76 \r
77 /* Scheduler includes. */\r
78 #include "FreeRTOS.h"\r
79 #include "task.h"\r
80 #include "queue.h"\r
81 #include "semphr.h"\r
82 \r
83 /* Demo app includes. */\r
84 #include "BlockQ.h"\r
85 #include "death.h"\r
86 #include "blocktim.h"\r
87 #include "LCD/portlcd.h"\r
88 #include "flash.h"\r
89 #include "partest.h"\r
90 #include "GenQTest.h"\r
91 #include "QPeek.h"\r
92 #include "dynamic.h"\r
93 \r
94 /* Demo application definitions. */\r
95 #define mainQUEUE_SIZE                                          ( 3 )\r
96 #define mainCHECK_DELAY                                         ( ( portTickType ) 5000 / portTICK_RATE_MS )\r
97 #define mainBASIC_WEB_STACK_SIZE            ( configMINIMAL_STACK_SIZE * 6 )\r
98 \r
99 /* Task priorities. */\r
100 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
101 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
102 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
103 #define mainFLASH_PRIORITY                  ( tskIDLE_PRIORITY + 2 )\r
104 #define mainCREATOR_TASK_PRIORITY           ( tskIDLE_PRIORITY + 3 )\r
105 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY ) \r
106 \r
107 /* Constants to setup the PLL. */\r
108 #define mainPLL_MUL                     ( ( unsigned portLONG ) ( 8 - 1 ) )\r
109 #define mainPLL_DIV                     ( ( unsigned portLONG ) 0x0000 )\r
110 #define mainCPU_CLK_DIV         ( ( unsigned portLONG ) 0x0003 )\r
111 #define mainPLL_ENABLE          ( ( unsigned portLONG ) 0x0001 )\r
112 #define mainPLL_CONNECT         ( ( ( unsigned portLONG ) 0x0002 ) | mainPLL_ENABLE )\r
113 #define mainPLL_FEED_BYTE1      ( ( unsigned portLONG ) 0xaa )\r
114 #define mainPLL_FEED_BYTE2      ( ( unsigned portLONG ) 0x55 )\r
115 #define mainPLL_LOCK            ( ( unsigned portLONG ) 0x4000000 )\r
116 #define mainPLL_CONNECTED       ( ( unsigned portLONG ) 0x2000000 )\r
117 #define mainOSC_ENABLE          ( ( unsigned portLONG ) 0x20 )\r
118 #define mainOSC_STAT            ( ( unsigned portLONG ) 0x40 )\r
119 #define mainOSC_SELECT          ( ( unsigned portLONG ) 0x01 )\r
120 \r
121 /* Constants to setup the MAM. */\r
122 #define mainMAM_TIM_3           ( ( unsigned portCHAR ) 0x03 )\r
123 #define mainMAM_MODE_FULL       ( ( unsigned portCHAR ) 0x02 )\r
124 \r
125 /* \r
126  * The task that handles the uIP stack.  All TCP/IP processing is performed in\r
127  * this task.\r
128  */\r
129 extern void vuIP_Task( void *pvParameters );\r
130 \r
131 /*\r
132  * The LCD is written two by more than one task so is controlled by a \r
133  * 'gatekeeper' task.  This is the only task that is actually permitted to \r
134  * access the LCD directly.  Other tasks wanting to display a message send\r
135  * the message to the gatekeeper.\r
136  */\r
137 static void vLCDTask( void *pvParameters );\r
138 \r
139 /* Configure the hardware as required by the demo. */\r
140 static void prvSetupHardware( void );\r
141 \r
142 /* The queue used to send messages to the LCD task. */\r
143 xQueueHandle xLCDQueue;\r
144 \r
145 /*-----------------------------------------------------------*/\r
146 \r
147 int main( void )\r
148 {\r
149         prvSetupHardware();\r
150         \r
151         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
152         are received via this queue. */\r
153         xLCDQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( xLCDMessage ) );\r
154 \r
155         /* Create the uIP task.  This uses the lwIP RTOS abstraction layer.*/\r
156     xTaskCreate( vuIP_Task, ( signed portCHAR * ) "uIP", mainBASIC_WEB_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY - 1, NULL );\r
157 \r
158         /* Start the standard demo tasks. */\r
159         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
160     vCreateBlockTimeTasks();\r
161     vStartLEDFlashTasks( mainFLASH_PRIORITY );\r
162     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
163     vStartQueuePeekTasks();   \r
164     vStartDynamicPriorityTasks();\r
165 \r
166         /* Start the tasks defined within this file/specific to this demo. */\r
167         xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY - 1, NULL );\r
168 \r
169         /* Start the scheduler. */\r
170         vTaskStartScheduler();\r
171 \r
172     /* Will only get here if there was insufficient memory to create the idle\r
173     task. */\r
174         return 0; \r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 void vApplicationTickHook( void )\r
179 {\r
180 unsigned portBASE_TYPE uxColumn = 0;\r
181 static xLCDMessage xMessage = { 0, "PASS" };\r
182 static unsigned portLONG ulTicksSinceLastDisplay = 0;\r
183 static portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
184 \r
185         /* Called from every tick interrupt.  Have enough ticks passed to make it\r
186         time to perform our health status check again? */\r
187         ulTicksSinceLastDisplay++;\r
188         if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )\r
189         {\r
190                 ulTicksSinceLastDisplay = 0;\r
191                 \r
192                 /* Has an error been found in any task? */\r
193 \r
194         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
195                 {\r
196                         xMessage.pcMessage = "ERROR - BLOCKQ";\r
197                 }\r
198 \r
199                 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
200                 {\r
201                         xMessage.pcMessage = "ERROR - BLOCKTIM";\r
202                 }\r
203 \r
204                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
205                 {\r
206                         xMessage.pcMessage = "ERROR - GENQ";\r
207                 }\r
208                 \r
209                 if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
210                 {\r
211                         xMessage.pcMessage = "ERROR - PEEKQ";\r
212                 }       \r
213                 \r
214                 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
215                 {\r
216                         xMessage.pcMessage = "ERROR - DYNAMIC";\r
217                 }\r
218         \r
219         xMessage.xColumn++;\r
220 \r
221                 /* Send the message to the LCD gatekeeper for display. */\r
222                 xHigherPriorityTaskWoken = pdFALSE;\r
223                 xQueueSendToBackFromISR( xLCDQueue, &xMessage, &xHigherPriorityTaskWoken );\r
224         }\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 void vLCDTask( void *pvParameters )\r
229 {\r
230 xLCDMessage xMessage;\r
231 \r
232         /* Initialise the LCD and display a startup message. */\r
233         LCD_init();\r
234         LCD_cur_off();\r
235     LCD_cls();    \r
236     LCD_gotoxy( 1, 1 );\r
237     LCD_puts( "www.FreeRTOS.org" );\r
238 \r
239         for( ;; )\r
240         {\r
241                 /* Wait for a message to arrive that requires displaying. */\r
242                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );\r
243                 \r
244                 /* Display the message.  Print each message to a different position. */\r
245                 LCD_cls();\r
246                 LCD_gotoxy( ( xMessage.xColumn & 0x07 ) + 1, ( xMessage.xColumn & 0x01 ) + 1 );\r
247                 LCD_puts( xMessage.pcMessage );\r
248         }\r
249 \r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 static void prvSetupHardware( void )\r
254 {\r
255         #ifdef RUN_FROM_RAM\r
256                 /* Remap the interrupt vectors to RAM if we are are running from RAM. */\r
257                 SCB_MEMMAP = 2;\r
258         #endif\r
259         \r
260         /* Disable the PLL. */\r
261         PLLCON = 0;\r
262         PLLFEED = mainPLL_FEED_BYTE1;\r
263         PLLFEED = mainPLL_FEED_BYTE2;\r
264         \r
265         /* Configure clock source. */\r
266         SCS |= mainOSC_ENABLE;\r
267         while( !( SCS & mainOSC_STAT ) );\r
268         CLKSRCSEL = mainOSC_SELECT; \r
269         \r
270         /* Setup the PLL to multiply the XTAL input by 4. */\r
271         PLLCFG = ( mainPLL_MUL | mainPLL_DIV );\r
272         PLLFEED = mainPLL_FEED_BYTE1;\r
273         PLLFEED = mainPLL_FEED_BYTE2;\r
274 \r
275         /* Turn on and wait for the PLL to lock... */\r
276         PLLCON = mainPLL_ENABLE;\r
277         PLLFEED = mainPLL_FEED_BYTE1;\r
278         PLLFEED = mainPLL_FEED_BYTE2;\r
279         CCLKCFG = mainCPU_CLK_DIV;      \r
280         while( !( PLLSTAT & mainPLL_LOCK ) );\r
281         \r
282         /* Connecting the clock. */\r
283         PLLCON = mainPLL_CONNECT;\r
284         PLLFEED = mainPLL_FEED_BYTE1;\r
285         PLLFEED = mainPLL_FEED_BYTE2;\r
286         while( !( PLLSTAT & mainPLL_CONNECTED ) ); \r
287         \r
288         /* \r
289         This code is commented out as the MAM does not work on the original revision\r
290         LPC2368 chips.  If using Rev B chips then you can increase the speed though\r
291         the use of the MAM.\r
292         \r
293         Setup and turn on the MAM.  Three cycle access is used due to the fast\r
294         PLL used.  It is possible faster overall performance could be obtained by\r
295         tuning the MAM and PLL settings.\r
296         MAMCR = 0;\r
297         MAMTIM = mainMAM_TIM_3;\r
298         MAMCR = mainMAM_MODE_FULL;\r
299         */\r
300         \r
301         /* Setup the led's on the MCB2300 board */\r
302         vParTestInitialise();\r
303 }\r
304 \r
305 \r
306 \r
307 \r
308 \r
309 \r
310 \r