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