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