]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_LPC1768_GCC_Rowley/main.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_LPC1768_GCC_Rowley / 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 /*\r
31  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
32  * documentation provides more details of the standard demo application tasks\r
33  * (which just exist to test the kernel port and provide an example of how to use\r
34  * each FreeRTOS API function).\r
35  *\r
36  * In addition to the standard demo tasks, the following tasks and tests are\r
37  * defined and/or created within this file:\r
38  *\r
39  * "Check" hook -  This only executes fully every five seconds from the tick\r
40  * hook.  Its main function is to check that all the standard demo tasks are\r
41  * still operational.  The status can be viewed using on the Task Stats page\r
42  * served by the WEB server.\r
43  *\r
44  * "uIP" task -  This is the task that handles the uIP stack.  All TCP/IP\r
45  * processing is performed in this task.\r
46  *\r
47  * "USB" task - Enumerates the USB device as a CDC class, then echoes back all\r
48  * received characters with a configurable offset (for example, if the offset\r
49  * is 1 and 'A' is received then 'B' will be sent back).  A dumb terminal such\r
50  * as Hyperterminal can be used to talk to the USB task.\r
51  */\r
52 \r
53 /* Scheduler includes. */\r
54 #include "FreeRTOS.h"\r
55 #include "task.h"\r
56 \r
57 /* Demo app includes. */\r
58 #include "BlockQ.h"\r
59 #include "integer.h"\r
60 #include "blocktim.h"\r
61 #include "flash.h"\r
62 #include "partest.h"\r
63 #include "semtest.h"\r
64 #include "PollQ.h"\r
65 #include "GenQTest.h"\r
66 #include "QPeek.h"\r
67 #include "recmutex.h"\r
68 \r
69 /*-----------------------------------------------------------*/\r
70 \r
71 /* The time between cycles of the 'check' functionality (defined within the\r
72 tick hook). */\r
73 #define mainCHECK_DELAY                                         ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )\r
74 \r
75 /* Task priorities. */\r
76 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
77 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
78 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
79 #define mainUIP_TASK_PRIORITY                           ( tskIDLE_PRIORITY + 3 )\r
80 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
81 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
82 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
83 \r
84 /* The WEB server has a larger stack as it utilises stack hungry string\r
85 handling library calls. */\r
86 #define mainBASIC_WEB_STACK_SIZE            ( configMINIMAL_STACK_SIZE * 4 )\r
87 \r
88 /* The message displayed by the WEB server when all tasks are executing\r
89 without an error being reported. */\r
90 #define mainPASS_STATUS_MESSAGE                         "All tasks are executing without error."\r
91 \r
92 /*-----------------------------------------------------------*/\r
93 \r
94 /*\r
95  * Configure the hardware for the demo.\r
96  */\r
97 static void prvSetupHardware( void );\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 task that handles the USB stack.\r
107  */\r
108 extern void vUSBTask( void *pvParameters );\r
109 \r
110 /*\r
111  * Simply returns the current status message for display on served WEB pages.\r
112  */\r
113 char *pcGetTaskStatusMessage( void );\r
114 \r
115 /*-----------------------------------------------------------*/\r
116 \r
117 /* Holds the status message displayed by the WEB server. */\r
118 static char *pcStatusMessage = mainPASS_STATUS_MESSAGE;\r
119 \r
120 /*-----------------------------------------------------------*/\r
121 \r
122 int main( void )\r
123 {\r
124         /* Configure the hardware for use by this demo. */\r
125         prvSetupHardware();\r
126 \r
127         /* Start the standard demo tasks.  These are just here to exercise the\r
128         kernel port and provide examples of how the FreeRTOS API can be used. */\r
129         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
130     vCreateBlockTimeTasks();\r
131     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
132     vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
133     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
134     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
135     vStartQueuePeekTasks();\r
136     vStartRecursiveMutexTasks();\r
137         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
138 \r
139     /* Create the USB task. */\r
140     xTaskCreate( vUSBTask, "USB", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );\r
141 \r
142         /* Create the uIP task.  The WEB server runs in this task. */\r
143     xTaskCreate( vuIP_Task, "uIP", mainBASIC_WEB_STACK_SIZE, ( void * ) NULL, mainUIP_TASK_PRIORITY, NULL );\r
144 \r
145     /* Start the scheduler. */\r
146         vTaskStartScheduler();\r
147 \r
148     /* Will only get here if there was insufficient memory to create the idle\r
149     task.  The idle task is created within vTaskStartScheduler(). */\r
150         for( ;; );\r
151 }\r
152 /*-----------------------------------------------------------*/\r
153 \r
154 void vApplicationTickHook( void )\r
155 {\r
156 static unsigned long ulTicksSinceLastDisplay = 0;\r
157 \r
158         /* Called from every tick interrupt as described in the comments at the top\r
159         of this file.\r
160 \r
161         Have enough ticks passed to make it     time to perform our health status\r
162         check again? */\r
163         ulTicksSinceLastDisplay++;\r
164         if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )\r
165         {\r
166                 /* Reset the counter so these checks run again in mainCHECK_DELAY\r
167                 ticks time. */\r
168                 ulTicksSinceLastDisplay = 0;\r
169 \r
170                 /* Has an error been found in any task? */\r
171                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
172                 {\r
173                         pcStatusMessage = "An error has been detected in the Generic Queue test/demo.";\r
174                 }\r
175                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
176                 {\r
177                         pcStatusMessage = "An error has been detected in the Peek Queue test/demo.";\r
178                 }\r
179                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
180                 {\r
181                         pcStatusMessage = "An error has been detected in the Block Queue test/demo.";\r
182                 }\r
183                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
184                 {\r
185                         pcStatusMessage = "An error has been detected in the Block Time test/demo.";\r
186                 }\r
187             else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
188             {\r
189                 pcStatusMessage = "An error has been detected in the Semaphore test/demo.";\r
190             }\r
191             else if( xArePollingQueuesStillRunning() != pdTRUE )\r
192             {\r
193                 pcStatusMessage = "An error has been detected in the Poll Queue test/demo.";\r
194             }\r
195             else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
196             {\r
197                 pcStatusMessage = "An error has been detected in the Int Math test/demo.";\r
198             }\r
199             else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
200             {\r
201                 pcStatusMessage = "An error has been detected in the Mutex test/demo.";\r
202             }\r
203         }\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 char *pcGetTaskStatusMessage( void )\r
208 {\r
209         /* Not bothered about a critical section here. */\r
210         return pcStatusMessage;\r
211 }\r
212 /*-----------------------------------------------------------*/\r
213 \r
214 void prvSetupHardware( void )\r
215 {\r
216         /* Disable peripherals power. */\r
217         SC->PCONP = 0;\r
218 \r
219         /* Enable GPIO power. */\r
220         SC->PCONP = PCONP_PCGPIO;\r
221 \r
222         /* Disable TPIU. */\r
223         PINCON->PINSEL10 = 0;\r
224 \r
225         /*  Setup the peripheral bus to be the same as the CPU output (100 MHz). */\r
226         SC->PCLKSEL0 = 0x05555555;\r
227 \r
228         /* Configure the LEDs. */\r
229         vParTestInitialise();\r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
234 {\r
235         /* This function will get called if a task overflows its stack. */\r
236 \r
237         ( void ) pxTask;\r
238         ( void ) pcTaskName;\r
239 \r
240         for( ;; );\r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 void vConfigureTimerForRunTimeStats( void )\r
245 {\r
246 const unsigned long TCR_COUNT_RESET = 2, CTCR_CTM_TIMER = 0x00, TCR_COUNT_ENABLE = 0x01;\r
247 \r
248         /* This function configures a timer that is used as the time base when\r
249         collecting run time statistical information - basically the percentage\r
250         of CPU time that each task is utilising.  It is called automatically when\r
251         the scheduler is started (assuming configGENERATE_RUN_TIME_STATS is set\r
252         to 1). */\r
253 \r
254         /* Power up and feed the timer. */\r
255         SC->PCONP |= 0x02UL;\r
256         SC->PCLKSEL0 = (SC->PCLKSEL0 & (~(0x3<<2))) | (0x01 << 2);\r
257 \r
258         /* Reset Timer 0 */\r
259         TIM0->TCR = TCR_COUNT_RESET;\r
260 \r
261         /* Just count up. */\r
262         TIM0->CTCR = CTCR_CTM_TIMER;\r
263 \r
264         /* Prescale to a frequency that is good enough to get a decent resolution,\r
265         but not too fast so as to overflow all the time. */\r
266         TIM0->PR =  ( configCPU_CLOCK_HZ / 10000UL ) - 1UL;\r
267 \r
268         /* Start the counter. */\r
269         TIM0->TCR = TCR_COUNT_ENABLE;\r
270 }\r
271 /*-----------------------------------------------------------*/\r
272 \r