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