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