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