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