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