]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_LPC2368_Eclipse/RTOSDemo/main.c
b52d1dfe2191d76be848f0d37a261bba529c17f8
[freertos] / FreeRTOS / Demo / ARM7_LPC2368_Eclipse / RTOSDemo / main.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
30  * documentation provides more details of the standard demo application tasks.\r
31  * In addition to the standard demo tasks, the following tasks and tests are\r
32  * defined and/or created within this file:\r
33  *\r
34  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that\r
35  * is permitted to access the display directly.  Other tasks wishing to write a\r
36  * message to the LCD send the message on a queue to the LCD task instead of\r
37  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting\r
38  * for messages - waking and displaying the messages as they arrive.\r
39  *\r
40  * "Check" hook -  This only executes every five seconds from the tick hook.\r
41  * Its main function is to check that all the standard demo tasks are still\r
42  * operational.  Should any unexpected behaviour within a demo task be discovered\r
43  * the tick hook will write an error to the LCD (via the LCD task).  If all the\r
44  * demo tasks are executing with their expected behaviour then the check task\r
45  * writes PASS to the LCD (again via the LCD task), as described above.\r
46  *\r
47  * "uIP" task -  This is the task that handles the uIP stack.  All TCP/IP\r
48  * processing is performed in this task.\r
49  */\r
50 \r
51 /* Scheduler includes. */\r
52 #include "FreeRTOS.h"\r
53 #include "task.h"\r
54 #include "queue.h"\r
55 #include "semphr.h"\r
56 \r
57 /* Demo app includes. */\r
58 #include "BlockQ.h"\r
59 #include "death.h"\r
60 #include "blocktim.h"\r
61 #include "LCD/portlcd.h"\r
62 #include "flash.h"\r
63 #include "partest.h"\r
64 #include "GenQTest.h"\r
65 #include "QPeek.h"\r
66 #include "dynamic.h"\r
67 \r
68 /* Demo application definitions. */\r
69 #define mainQUEUE_SIZE                                          ( 3 )\r
70 #define mainCHECK_DELAY                                         ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )\r
71 #define mainBASIC_WEB_STACK_SIZE            ( configMINIMAL_STACK_SIZE * 6 )\r
72 \r
73 /* Task priorities. */\r
74 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
75 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
76 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
77 #define mainFLASH_PRIORITY                  ( tskIDLE_PRIORITY + 2 )\r
78 #define mainCREATOR_TASK_PRIORITY           ( tskIDLE_PRIORITY + 3 )\r
79 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
80 \r
81 /* Constants to setup the PLL. */\r
82 #define mainPLL_MUL                     ( ( unsigned long ) ( 8 - 1 ) )\r
83 #define mainPLL_DIV                     ( ( unsigned long ) 0x0000 )\r
84 #define mainCPU_CLK_DIV         ( ( unsigned long ) 0x0003 )\r
85 #define mainPLL_ENABLE          ( ( unsigned long ) 0x0001 )\r
86 #define mainPLL_CONNECT         ( ( ( unsigned long ) 0x0002 ) | mainPLL_ENABLE )\r
87 #define mainPLL_FEED_BYTE1      ( ( unsigned long ) 0xaa )\r
88 #define mainPLL_FEED_BYTE2      ( ( unsigned long ) 0x55 )\r
89 #define mainPLL_LOCK            ( ( unsigned long ) 0x4000000 )\r
90 #define mainPLL_CONNECTED       ( ( unsigned long ) 0x2000000 )\r
91 #define mainOSC_ENABLE          ( ( unsigned long ) 0x20 )\r
92 #define mainOSC_STAT            ( ( unsigned long ) 0x40 )\r
93 #define mainOSC_SELECT          ( ( unsigned long ) 0x01 )\r
94 \r
95 /* Constants to setup the MAM. */\r
96 #define mainMAM_TIM_3           ( ( unsigned char ) 0x03 )\r
97 #define mainMAM_MODE_FULL       ( ( unsigned char ) 0x02 )\r
98 \r
99 /*\r
100  * The task that handles the uIP stack.  All TCP/IP processing is performed in\r
101  * this task.\r
102  */\r
103 extern void vuIP_Task( void *pvParameters );\r
104 \r
105 /*\r
106  * The LCD is written two by more than one task so is controlled by a\r
107  * 'gatekeeper' task.  This is the only task that is actually permitted to\r
108  * access the LCD directly.  Other tasks wanting to display a message send\r
109  * the message to the gatekeeper.\r
110  */\r
111 static void vLCDTask( void *pvParameters );\r
112 \r
113 /* Configure the hardware as required by the demo. */\r
114 static void prvSetupHardware( void );\r
115 \r
116 /* The queue used to send messages to the LCD task. */\r
117 QueueHandle_t xLCDQueue;\r
118 \r
119 /*-----------------------------------------------------------*/\r
120 \r
121 int main( void )\r
122 {\r
123         prvSetupHardware();\r
124 \r
125         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
126         are received via this queue. */\r
127         xLCDQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( xLCDMessage ) );\r
128 \r
129         /* Create the uIP task.  This uses the lwIP RTOS abstraction layer.*/\r
130     xTaskCreate( vuIP_Task, "uIP", mainBASIC_WEB_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY - 1, NULL );\r
131 \r
132         /* Start the standard demo tasks. */\r
133         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
134     vCreateBlockTimeTasks();\r
135     vStartLEDFlashTasks( mainFLASH_PRIORITY );\r
136     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
137     vStartQueuePeekTasks();\r
138     vStartDynamicPriorityTasks();\r
139 \r
140         /* Start the tasks defined within this file/specific to this demo. */\r
141         xTaskCreate( vLCDTask, "LCD", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY - 1, NULL );\r
142 \r
143         /* Start the scheduler. */\r
144         vTaskStartScheduler();\r
145 \r
146     /* Will only get here if there was insufficient memory to create the idle\r
147     task. */\r
148         return 0;\r
149 }\r
150 /*-----------------------------------------------------------*/\r
151 \r
152 void vApplicationTickHook( void )\r
153 {\r
154 unsigned portBASE_TYPE uxColumn = 0;\r
155 static xLCDMessage xMessage = { 0, "PASS" };\r
156 static unsigned long ulTicksSinceLastDisplay = 0;\r
157 static portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
158 \r
159         /* Called from every tick interrupt.  Have enough ticks passed to make it\r
160         time to perform our health status check again? */\r
161         ulTicksSinceLastDisplay++;\r
162         if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )\r
163         {\r
164                 ulTicksSinceLastDisplay = 0;\r
165 \r
166                 /* Has an error been found in any task? */\r
167 \r
168         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
169                 {\r
170                         xMessage.pcMessage = "ERROR - BLOCKQ";\r
171                 }\r
172 \r
173                 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
174                 {\r
175                         xMessage.pcMessage = "ERROR - BLOCKTIM";\r
176                 }\r
177 \r
178                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
179                 {\r
180                         xMessage.pcMessage = "ERROR - GENQ";\r
181                 }\r
182 \r
183                 if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
184                 {\r
185                         xMessage.pcMessage = "ERROR - PEEKQ";\r
186                 }\r
187 \r
188                 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
189                 {\r
190                         xMessage.pcMessage = "ERROR - DYNAMIC";\r
191                 }\r
192 \r
193         xMessage.xColumn++;\r
194 \r
195                 /* Send the message to the LCD gatekeeper for display. */\r
196                 xHigherPriorityTaskWoken = pdFALSE;\r
197                 xQueueSendToBackFromISR( xLCDQueue, &xMessage, &xHigherPriorityTaskWoken );\r
198         }\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 void vLCDTask( void *pvParameters )\r
203 {\r
204 xLCDMessage xMessage;\r
205 \r
206         /* Initialise the LCD and display a startup message. */\r
207         LCD_init();\r
208         LCD_cur_off();\r
209     LCD_cls();\r
210     LCD_gotoxy( 1, 1 );\r
211     LCD_puts( "www.FreeRTOS.org" );\r
212 \r
213         for( ;; )\r
214         {\r
215                 /* Wait for a message to arrive that requires displaying. */\r
216                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );\r
217 \r
218                 /* Display the message.  Print each message to a different position. */\r
219                 LCD_cls();\r
220                 LCD_gotoxy( ( xMessage.xColumn & 0x07 ) + 1, ( xMessage.xColumn & 0x01 ) + 1 );\r
221                 LCD_puts( xMessage.pcMessage );\r
222         }\r
223 \r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 static void prvSetupHardware( void )\r
228 {\r
229         #ifdef RUN_FROM_RAM\r
230                 /* Remap the interrupt vectors to RAM if we are are running from RAM. */\r
231                 SCB_MEMMAP = 2;\r
232         #endif\r
233 \r
234         /* Disable the PLL. */\r
235         PLLCON = 0;\r
236         PLLFEED = mainPLL_FEED_BYTE1;\r
237         PLLFEED = mainPLL_FEED_BYTE2;\r
238 \r
239         /* Configure clock source. */\r
240         SCS |= mainOSC_ENABLE;\r
241         while( !( SCS & mainOSC_STAT ) );\r
242         CLKSRCSEL = mainOSC_SELECT;\r
243 \r
244         /* Setup the PLL to multiply the XTAL input by 4. */\r
245         PLLCFG = ( mainPLL_MUL | mainPLL_DIV );\r
246         PLLFEED = mainPLL_FEED_BYTE1;\r
247         PLLFEED = mainPLL_FEED_BYTE2;\r
248 \r
249         /* Turn on and wait for the PLL to lock... */\r
250         PLLCON = mainPLL_ENABLE;\r
251         PLLFEED = mainPLL_FEED_BYTE1;\r
252         PLLFEED = mainPLL_FEED_BYTE2;\r
253         CCLKCFG = mainCPU_CLK_DIV;\r
254         while( !( PLLSTAT & mainPLL_LOCK ) );\r
255 \r
256         /* Connecting the clock. */\r
257         PLLCON = mainPLL_CONNECT;\r
258         PLLFEED = mainPLL_FEED_BYTE1;\r
259         PLLFEED = mainPLL_FEED_BYTE2;\r
260         while( !( PLLSTAT & mainPLL_CONNECTED ) );\r
261 \r
262         /*\r
263         This code is commented out as the MAM does not work on the original revision\r
264         LPC2368 chips.  If using Rev B chips then you can increase the speed though\r
265         the use of the MAM.\r
266 \r
267         Setup and turn on the MAM.  Three cycle access is used due to the fast\r
268         PLL used.  It is possible faster overall performance could be obtained by\r
269         tuning the MAM and PLL settings.\r
270         MAMCR = 0;\r
271         MAMTIM = mainMAM_TIM_3;\r
272         MAMCR = mainMAM_MODE_FULL;\r
273         */\r
274 \r
275         /* Setup the led's on the MCB2300 board */\r
276         vParTestInitialise();\r
277 }\r
278 \r
279 \r
280 \r
281 \r
282 \r
283 \r
284 \r