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