]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_LPC1768_IAR/main.c
osEventFlagsWait: Fix flag comparison
[cmsis-freertos] / Demo / CORTEX_LPC1768_IAR / main.c
1 /*
2  * FreeRTOS Kernel V10.2.1
3  * Copyright (C) 2019 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 /* The toggle rate for the LED. */
75 #define mainLED_TOGGLE_RATE                                     ( ( TickType_t ) 1000 / portTICK_PERIOD_MS )
76
77 /* Task priorities. */
78 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )
79 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )
80 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )
81 #define mainUIP_TASK_PRIORITY                           ( tskIDLE_PRIORITY + 3 )
82 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 2 )
83 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )
84 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )
85
86 /* The WEB server has a larger stack as it utilises stack hungry string
87 handling library calls. */
88 #define mainBASIC_WEB_STACK_SIZE            ( configMINIMAL_STACK_SIZE * 4 )
89
90 /* The message displayed by the WEB server when all tasks are executing
91 without an error being reported. */
92 #define mainPASS_STATUS_MESSAGE                         "All tasks are executing without error."
93
94 /*-----------------------------------------------------------*/
95
96 /*
97  * Configure the hardware for the demo.
98  */
99 static void prvSetupHardware( void );
100
101 /*
102  * The task that handles the uIP stack.  All TCP/IP processing is performed in
103  * this task.
104  */
105 extern void vuIP_Task( void *pvParameters );
106
107 /*
108  * The task that handles the USB stack.
109  */
110 extern void vUSBTask( void *pvParameters );
111
112 /*
113  * Very basic task that does nothing but use delays to flash an LED.
114  */
115 static void prvFlashTask( void *pvParameters );
116
117 /*
118  * Simply returns the current status message for display on served WEB pages.
119  */
120 char *pcGetTaskStatusMessage( void );
121
122 /*-----------------------------------------------------------*/
123
124 /* Holds the status message displayed by the WEB server. */
125 static char *pcStatusMessage = mainPASS_STATUS_MESSAGE;
126
127 /*-----------------------------------------------------------*/
128
129 int main( void )
130 {
131         /* Configure the hardware for use by this demo. */
132         prvSetupHardware();
133
134         /* Start the standard demo tasks.  These are just here to exercise the
135         kernel port and provide examples of how the FreeRTOS API can be used. */
136         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
137     vCreateBlockTimeTasks();
138     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
139     vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
140     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
141     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
142     vStartQueuePeekTasks();
143     vStartRecursiveMutexTasks();
144
145         /* Create the simple LED flash task. */
146         xTaskCreate( prvFlashTask, "Flash", configMINIMAL_STACK_SIZE, ( void * ) NULL, mainFLASH_TASK_PRIORITY, NULL );
147
148     /* Create the USB task. */
149     xTaskCreate( vUSBTask, "USB", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );
150
151         /* Create the uIP task.  The WEB server runs in this task. */
152     xTaskCreate( vuIP_Task, "uIP", mainBASIC_WEB_STACK_SIZE, ( void * ) NULL, mainUIP_TASK_PRIORITY, NULL );
153
154     /* Start the scheduler. */
155         vTaskStartScheduler();
156
157     /* Will only get here if there was insufficient memory to create the idle
158     task.  The idle task is created within vTaskStartScheduler(). */
159         for( ;; );
160 }
161 /*-----------------------------------------------------------*/
162
163 void vApplicationTickHook( void )
164 {
165 static unsigned long ulTicksSinceLastDisplay = 0;
166
167         /* Called from every tick interrupt as described in the comments at the top
168         of this file.
169
170         Have enough ticks passed to make it     time to perform our health status
171         check again? */
172         ulTicksSinceLastDisplay++;
173         if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )
174         {
175                 /* Reset the counter so these checks run again in mainCHECK_DELAY
176                 ticks time. */
177                 ulTicksSinceLastDisplay = 0;
178
179                 /* Has an error been found in any task? */
180                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
181                 {
182                         pcStatusMessage = "An error has been detected in the Generic Queue test/demo.";
183                 }
184                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
185                 {
186                         pcStatusMessage = "An error has been detected in the Peek Queue test/demo.";
187                 }
188                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
189                 {
190                         pcStatusMessage = "An error has been detected in the Block Queue test/demo.";
191                 }
192                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
193                 {
194                         pcStatusMessage = "An error has been detected in the Block Time test/demo.";
195                 }
196             else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
197             {
198                 pcStatusMessage = "An error has been detected in the Semaphore test/demo.";
199             }
200             else if( xArePollingQueuesStillRunning() != pdTRUE )
201             {
202                 pcStatusMessage = "An error has been detected in the Poll Queue test/demo.";
203             }
204             else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
205             {
206                 pcStatusMessage = "An error has been detected in the Int Math test/demo.";
207             }
208             else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
209             {
210                 pcStatusMessage = "An error has been detected in the Mutex test/demo.";
211             }
212         }
213 }
214 /*-----------------------------------------------------------*/
215
216 static void prvFlashTask( void *pvParameters )
217 {
218 TickType_t xLastFlashTime;
219
220         /* We need to initialise xLastFlashTime prior to the first call to
221         vTaskDelayUntil(). */
222         xLastFlashTime = xTaskGetTickCount();
223
224         for(;;)
225         {
226                 /* Simply toggle the LED between delays. */
227                 vTaskDelayUntil( &xLastFlashTime, mainLED_TOGGLE_RATE );
228                 vParTestToggleLED( 0 );
229         }
230 }
231 /*-----------------------------------------------------------*/
232
233 char *pcGetTaskStatusMessage( void )
234 {
235         /* Not bothered about a critical section here. */
236         return pcStatusMessage;
237 }
238 /*-----------------------------------------------------------*/
239
240 void prvSetupHardware( void )
241 {
242         /* Disable peripherals power. */
243         SC->PCONP = 0;
244
245         /* Enable GPIO power. */
246         SC->PCONP = PCONP_PCGPIO;
247
248         /* Disable TPIU. */
249         PINCON->PINSEL10 = 0;
250
251         if ( SC->PLL0STAT & ( 1 << 25 ) )
252         {
253                 /* Enable PLL, disconnected. */
254                 SC->PLL0CON = 1;
255                 SC->PLL0FEED = PLLFEED_FEED1;
256                 SC->PLL0FEED = PLLFEED_FEED2;
257         }
258
259         /* Disable PLL, disconnected. */
260         SC->PLL0CON = 0;
261         SC->PLL0FEED = PLLFEED_FEED1;
262         SC->PLL0FEED = PLLFEED_FEED2;
263
264         /* Enable main OSC. */
265         SC->SCS |= 0x20;
266         while( !( SC->SCS & 0x40 ) );
267
268         /* select main OSC, 12MHz, as the PLL clock source. */
269         SC->CLKSRCSEL = 0x1;
270
271         SC->PLL0CFG = 0x20031;
272         SC->PLL0FEED = PLLFEED_FEED1;
273         SC->PLL0FEED = PLLFEED_FEED2;
274
275         /* Enable PLL, disconnected. */
276         SC->PLL0CON = 1;
277         SC->PLL0FEED = PLLFEED_FEED1;
278         SC->PLL0FEED = PLLFEED_FEED2;
279
280         /* Set clock divider. */
281         SC->CCLKCFG = 0x03;
282
283         /* Configure flash accelerator. */
284         SC->FLASHCFG = 0x403a;
285
286         /* Check lock bit status. */
287         while( ( ( SC->PLL0STAT & ( 1 << 26 ) ) == 0 ) );
288
289         /* Enable and connect. */
290         SC->PLL0CON = 3;
291         SC->PLL0FEED = PLLFEED_FEED1;
292         SC->PLL0FEED = PLLFEED_FEED2;
293         while( ( ( SC->PLL0STAT & ( 1 << 25 ) ) == 0 ) );
294
295
296
297
298         /* Configure the clock for the USB. */
299
300         if( SC->PLL1STAT & ( 1 << 9 ) )
301         {
302                 /* Enable PLL, disconnected. */
303                 SC->PLL1CON = 1;
304                 SC->PLL1FEED = PLLFEED_FEED1;
305                 SC->PLL1FEED = PLLFEED_FEED2;
306         }
307
308         /* Disable PLL, disconnected. */
309         SC->PLL1CON = 0;
310         SC->PLL1FEED = PLLFEED_FEED1;
311         SC->PLL1FEED = PLLFEED_FEED2;
312
313         SC->PLL1CFG = 0x23;
314         SC->PLL1FEED = PLLFEED_FEED1;
315         SC->PLL1FEED = PLLFEED_FEED2;
316
317         /* Enable PLL, disconnected. */
318         SC->PLL1CON = 1;
319         SC->PLL1FEED = PLLFEED_FEED1;
320         SC->PLL1FEED = PLLFEED_FEED2;
321         while( ( ( SC->PLL1STAT & ( 1 << 10 ) ) == 0 ) );
322
323         /* Enable and connect. */
324         SC->PLL1CON = 3;
325         SC->PLL1FEED = PLLFEED_FEED1;
326         SC->PLL1FEED = PLLFEED_FEED2;
327         while( ( ( SC->PLL1STAT & ( 1 << 9 ) ) == 0 ) );
328
329         /*  Setup the peripheral bus to be the same as the CPU output (100 MHz). */
330         SC->PCLKSEL0 = 0x05555555;
331
332         /* Configure the LEDs. */
333         vParTestInitialise();
334 }
335 /*-----------------------------------------------------------*/
336
337 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
338 {
339         /* This function will get called if a task overflows its stack. */
340
341         ( void ) pxTask;
342         ( void ) pcTaskName;
343
344         for( ;; );
345 }
346 /*-----------------------------------------------------------*/
347
348 void vConfigureTimerForRunTimeStats( void )
349 {
350 const unsigned long TCR_COUNT_RESET = 2, CTCR_CTM_TIMER = 0x00, TCR_COUNT_ENABLE = 0x01;
351
352         /* This function configures a timer that is used as the time base when
353         collecting run time statistical information - basically the percentage
354         of CPU time that each task is utilising.  It is called automatically when
355         the scheduler is started (assuming configGENERATE_RUN_TIME_STATS is set
356         to 1). */
357
358         /* Power up and feed the timer. */
359         SC->PCONP |= 0x02UL;
360         SC->PCLKSEL0 = (SC->PCLKSEL0 & (~(0x3<<2))) | (0x01 << 2);
361
362         /* Reset Timer 0 */
363         TIM0->TCR = TCR_COUNT_RESET;
364
365         /* Just count up. */
366         TIM0->CTCR = CTCR_CTM_TIMER;
367
368         /* Prescale to a frequency that is good enough to get a decent resolution,
369         but not too fast so as to overflow all the time. */
370         TIM0->PR =  ( configCPU_CLOCK_HZ / 10000UL ) - 1UL;
371
372         /* Start the counter. */
373         TIM0->TCR = TCR_COUNT_ENABLE;
374 }
375 /*-----------------------------------------------------------*/
376