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