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