]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_LPC1768_GCC_Rowley/main.c
This is a FreeRTOS header, not RTX.
[cmsis-freertos] / Demo / CORTEX_LPC1768_GCC_Rowley / main.c
1 /*
2  * FreeRTOS Kernel V10.0.1
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28
29 /*
30  * Creates all the demo application tasks, then starts the scheduler.  The WEB
31  * documentation provides more details of the standard demo application tasks
32  * (which just exist to test the kernel port and provide an example of how to use
33  * each FreeRTOS API function).
34  *
35  * In addition to the standard demo tasks, the following tasks and tests are
36  * defined and/or created within this file:
37  *
38  * "Check" hook -  This only executes fully every five seconds from the tick
39  * hook.  Its main function is to check that all the standard demo tasks are
40  * still operational.  The status can be viewed using on the Task Stats page
41  * served by the WEB server.
42  *
43  * "uIP" task -  This is the task that handles the uIP stack.  All TCP/IP
44  * processing is performed in this task.
45  *
46  * "USB" task - Enumerates the USB device as a CDC class, then echoes back all
47  * received characters with a configurable offset (for example, if the offset
48  * is 1 and 'A' is received then 'B' will be sent back).  A dumb terminal such
49  * as Hyperterminal can be used to talk to the USB task.
50  */
51
52 /* Scheduler includes. */
53 #include "FreeRTOS.h"
54 #include "task.h"
55
56 /* Demo app includes. */
57 #include "BlockQ.h"
58 #include "integer.h"
59 #include "blocktim.h"
60 #include "flash.h"
61 #include "partest.h"
62 #include "semtest.h"
63 #include "PollQ.h"
64 #include "GenQTest.h"
65 #include "QPeek.h"
66 #include "recmutex.h"
67
68 /*-----------------------------------------------------------*/
69
70 /* The time between cycles of the 'check' functionality (defined within the
71 tick hook). */
72 #define mainCHECK_DELAY                                         ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )
73
74 /* Task priorities. */
75 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )
76 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )
77 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )
78 #define mainUIP_TASK_PRIORITY                           ( tskIDLE_PRIORITY + 3 )
79 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )
80 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )
81 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 2 )
82
83 /* The WEB server has a larger stack as it utilises stack hungry string
84 handling library calls. */
85 #define mainBASIC_WEB_STACK_SIZE            ( configMINIMAL_STACK_SIZE * 4 )
86
87 /* The message displayed by the WEB server when all tasks are executing
88 without an error being reported. */
89 #define mainPASS_STATUS_MESSAGE                         "All tasks are executing without error."
90
91 /*-----------------------------------------------------------*/
92
93 /*
94  * Configure the hardware for the demo.
95  */
96 static void prvSetupHardware( void );
97
98 /*
99  * The task that handles the uIP stack.  All TCP/IP processing is performed in
100  * this task.
101  */
102 extern void vuIP_Task( void *pvParameters );
103
104 /*
105  * The task that handles the USB stack.
106  */
107 extern void vUSBTask( void *pvParameters );
108
109 /*
110  * Simply returns the current status message for display on served WEB pages.
111  */
112 char *pcGetTaskStatusMessage( void );
113
114 /*-----------------------------------------------------------*/
115
116 /* Holds the status message displayed by the WEB server. */
117 static char *pcStatusMessage = mainPASS_STATUS_MESSAGE;
118
119 /*-----------------------------------------------------------*/
120
121 int main( void )
122 {
123         /* Configure the hardware for use by this demo. */
124         prvSetupHardware();
125
126         /* Start the standard demo tasks.  These are just here to exercise the
127         kernel port and provide examples of how the FreeRTOS API can be used. */
128         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
129     vCreateBlockTimeTasks();
130     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
131     vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
132     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
133     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
134     vStartQueuePeekTasks();
135     vStartRecursiveMutexTasks();
136         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
137
138     /* Create the USB task. */
139     xTaskCreate( vUSBTask, "USB", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );
140
141         /* Create the uIP task.  The WEB server runs in this task. */
142     xTaskCreate( vuIP_Task, "uIP", mainBASIC_WEB_STACK_SIZE, ( void * ) NULL, mainUIP_TASK_PRIORITY, NULL );
143
144     /* Start the scheduler. */
145         vTaskStartScheduler();
146
147     /* Will only get here if there was insufficient memory to create the idle
148     task.  The idle task is created within vTaskStartScheduler(). */
149         for( ;; );
150 }
151 /*-----------------------------------------------------------*/
152
153 void vApplicationTickHook( void )
154 {
155 static unsigned long ulTicksSinceLastDisplay = 0;
156
157         /* Called from every tick interrupt as described in the comments at the top
158         of this file.
159
160         Have enough ticks passed to make it     time to perform our health status
161         check again? */
162         ulTicksSinceLastDisplay++;
163         if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )
164         {
165                 /* Reset the counter so these checks run again in mainCHECK_DELAY
166                 ticks time. */
167                 ulTicksSinceLastDisplay = 0;
168
169                 /* Has an error been found in any task? */
170                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
171                 {
172                         pcStatusMessage = "An error has been detected in the Generic Queue test/demo.";
173                 }
174                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
175                 {
176                         pcStatusMessage = "An error has been detected in the Peek Queue test/demo.";
177                 }
178                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
179                 {
180                         pcStatusMessage = "An error has been detected in the Block Queue test/demo.";
181                 }
182                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
183                 {
184                         pcStatusMessage = "An error has been detected in the Block Time test/demo.";
185                 }
186             else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
187             {
188                 pcStatusMessage = "An error has been detected in the Semaphore test/demo.";
189             }
190             else if( xArePollingQueuesStillRunning() != pdTRUE )
191             {
192                 pcStatusMessage = "An error has been detected in the Poll Queue test/demo.";
193             }
194             else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
195             {
196                 pcStatusMessage = "An error has been detected in the Int Math test/demo.";
197             }
198             else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
199             {
200                 pcStatusMessage = "An error has been detected in the Mutex test/demo.";
201             }
202         }
203 }
204 /*-----------------------------------------------------------*/
205
206 char *pcGetTaskStatusMessage( void )
207 {
208         /* Not bothered about a critical section here. */
209         return pcStatusMessage;
210 }
211 /*-----------------------------------------------------------*/
212
213 void prvSetupHardware( void )
214 {
215         /* Disable peripherals power. */
216         SC->PCONP = 0;
217
218         /* Enable GPIO power. */
219         SC->PCONP = PCONP_PCGPIO;
220
221         /* Disable TPIU. */
222         PINCON->PINSEL10 = 0;
223
224         /*  Setup the peripheral bus to be the same as the CPU output (100 MHz). */
225         SC->PCLKSEL0 = 0x05555555;
226
227         /* Configure the LEDs. */
228         vParTestInitialise();
229 }
230 /*-----------------------------------------------------------*/
231
232 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
233 {
234         /* This function will get called if a task overflows its stack. */
235
236         ( void ) pxTask;
237         ( void ) pcTaskName;
238
239         for( ;; );
240 }
241 /*-----------------------------------------------------------*/
242
243 void vConfigureTimerForRunTimeStats( void )
244 {
245 const unsigned long TCR_COUNT_RESET = 2, CTCR_CTM_TIMER = 0x00, TCR_COUNT_ENABLE = 0x01;
246
247         /* This function configures a timer that is used as the time base when
248         collecting run time statistical information - basically the percentage
249         of CPU time that each task is utilising.  It is called automatically when
250         the scheduler is started (assuming configGENERATE_RUN_TIME_STATS is set
251         to 1). */
252
253         /* Power up and feed the timer. */
254         SC->PCONP |= 0x02UL;
255         SC->PCLKSEL0 = (SC->PCLKSEL0 & (~(0x3<<2))) | (0x01 << 2);
256
257         /* Reset Timer 0 */
258         TIM0->TCR = TCR_COUNT_RESET;
259
260         /* Just count up. */
261         TIM0->CTCR = CTCR_CTM_TIMER;
262
263         /* Prescale to a frequency that is good enough to get a decent resolution,
264         but not too fast so as to overflow all the time. */
265         TIM0->PR =  ( configCPU_CLOCK_HZ / 10000UL ) - 1UL;
266
267         /* Start the counter. */
268         TIM0->TCR = TCR_COUNT_ENABLE;
269 }
270 /*-----------------------------------------------------------*/
271