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