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