]> begriffs open source - cmsis-freertos/blob - Demo/ColdFire_MCF51CN128_CodeWarrior/Sources/main.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / ColdFire_MCF51CN128_CodeWarrior / Sources / main.c
1 /*
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
9     FreeRTOS is free software; you can redistribute it and/or modify it under
10     the terms of the GNU General Public License (version 2) as published by the
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
12
13     ***************************************************************************
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<
16     >>!   obliged to provide the source code for proprietary components     !<<
17     >>!   outside of the FreeRTOS kernel.                                   !<<
18     ***************************************************************************
19
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
23     link: http://www.freertos.org/a00114.html
24
25     ***************************************************************************
26      *                                                                       *
27      *    FreeRTOS provides completely free yet professionally developed,    *
28      *    robust, strictly quality controlled, supported, and cross          *
29      *    platform software that is more than just the market leader, it     *
30      *    is the industry's de facto standard.                               *
31      *                                                                       *
32      *    Help yourself get started quickly while simultaneously helping     *
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
34      *    tutorial book, reference manual, or both:                          *
35      *    http://www.FreeRTOS.org/Documentation                              *
36      *                                                                       *
37     ***************************************************************************
38
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
40     the FAQ page "My application does not run, what could be wrong?".  Have you
41     defined configASSERT()?
42
43     http://www.FreeRTOS.org/support - In return for receiving this top quality
44     embedded software for free we request you assist our global community by
45     participating in the support forum.
46
47     http://www.FreeRTOS.org/training - Investing in training allows your team to
48     be as productive as possible as early as possible.  Now you can receive
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50     Ltd, and the world's leading authority on the world's leading RTOS.
51
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.
55
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
61     licenses offer ticketed support, indemnification and commercial middleware.
62
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64     engineered and independently SIL3 certified version for use in safety and
65     mission critical applications that require provable dependability.
66
67     1 tab == 4 spaces!
68 */
69
70
71 /*
72  * Creates all the demo application tasks, then starts the scheduler.  The WEB
73  * documentation provides more details of the standard demo application tasks.
74  * In addition to the standard demo tasks, the following tasks and tests are
75  * defined and/or created within this file:
76  *
77  * "Web server" - Very basic demonstration of the uIP stack.  The WEB server
78  * simply generates a page that shows the current state of all the tasks within
79  * the system, including the high water mark of each task stack. The high water
80  * mark is displayed as the amount of stack that has never been used, so the
81  * closer the value is to zero the closer the task has come to overflowing its
82  * stack.  The IP address and net mask are set within FreeRTOSConfig.h.  Sub
83  * pages display some TCP/IP status information and permit LED3 to be turned on
84  * and off using a check box.
85  *
86  * Tick hook function that implements a "Check" function -  This is executed
87  * every 5 seconds from the tick hook function.  It checks to ensure that all
88  * the standard demo tasks are still operational and running without error.
89  * The system status (pass/fail) is then displayed underneith the task table on
90  * the served WEB pages.
91  *
92  * "Reg test" tasks - These fill the registers with known values, then check
93  * that each register still contains its expected value.  Each task uses
94  * different values.  The tasks run with very low priority so get preempted very
95  * frequently.  A register containing an unexpected value is indicative of an
96  * error in the context switching mechanism.
97  *
98  */
99
100 /* Standard includes. */
101 #include <stdio.h>
102
103 /* Scheduler includes. */
104 #include "FreeRTOS.h"
105 #include "task.h"
106 #include "queue.h"
107 #include "semphr.h"
108
109 /* Demo app includes. */
110 #include "BlockQ.h"
111 #include "death.h"
112 #include "flash.h"
113 #include "partest.h"
114 #include "GenQTest.h"
115 #include "QPeek.h"
116 #include "recmutex.h"
117
118 /*-----------------------------------------------------------*/
119
120 /* ComTest constants - there is no free LED for the comtest tasks. */
121 #define mainCOM_TEST_BAUD_RATE                          ( ( unsigned long ) 19200 )
122 #define mainCOM_TEST_LED                                        ( 5 )
123
124 /* Task priorities. */
125 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )
126 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )
127 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )
128 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )
129 #define mainCREATOR_TASK_PRIORITY           ( tskIDLE_PRIORITY + 2 )
130 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )
131 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )
132 #define mainWEB_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 2 )
133
134 /* WEB server requires enough stack for the string handling functions. */
135 #define mainBASIC_WEB_STACK_SIZE            ( configMINIMAL_STACK_SIZE * 2 )
136
137 /*
138  * Configure the hardware for the demo.
139  */
140 static void prvSetupHardware( void );
141
142 /*
143  * Implements the 'check' function as described at the top of this file.
144  */
145 static void prvCheckFunction( void );
146
147 /*
148  * Implement the 'Reg test' functionality as described at the top of this file.
149  */
150 static void vRegTest1Task( void *pvParameters );
151 static void vRegTest2Task( void *pvParameters );
152
153 /*
154  * The task that handles the uIP stack.  All TCP/IP processing is performed in
155  * this task.
156  */
157 extern void vuIP_Task( void *pvParameters );
158
159 /*-----------------------------------------------------------*/
160
161 /* Counters used to detect errors within the reg test tasks. */
162 static volatile unsigned long ulRegTest1Counter = 0x11111111, ulRegTest2Counter = 0x22222222;
163
164 /* Flag that latches any errors detected in the system. */
165 unsigned long ulCheckErrors = 0;
166
167 /*-----------------------------------------------------------*/
168
169 int main( void )
170 {
171 extern void vBasicWEBServer( void *pv );
172
173         /* Setup the hardware ready for this demo. */
174         prvSetupHardware();
175
176         xTaskCreate( vuIP_Task, "uIP", mainBASIC_WEB_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY - 1, NULL );
177
178         /* Start the standard demo tasks. */
179         vStartLEDFlashTasks( tskIDLE_PRIORITY );
180         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
181         vStartQueuePeekTasks();
182         vStartRecursiveMutexTasks();
183         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
184
185         /* Start the reg test tasks - defined in this file. */
186         xTaskCreate( vRegTest1Task, "Reg1", configMINIMAL_STACK_SIZE, ( void * ) &ulRegTest1Counter, tskIDLE_PRIORITY, NULL );
187         xTaskCreate( vRegTest2Task, "Reg2", configMINIMAL_STACK_SIZE, ( void * ) &ulRegTest2Counter, tskIDLE_PRIORITY, NULL );
188
189         /* Start the scheduler. */
190         vTaskStartScheduler();
191
192     /* Will only get here if there was insufficient memory to create the idle
193     task. */
194         for( ;; )
195         {
196         }
197 }
198 /*-----------------------------------------------------------*/
199
200 void vApplicationTickHook( void )
201 {
202 static unsigned long ulExecutionCount = 0, ulLastRegTest1Count = 0, ulLastRegTest2Count = 0;
203 const unsigned long ulExecutionRate = 5000 / portTICK_PERIOD_MS;
204
205     /* Increment the count of how many times the tick hook has been called. */
206     ulExecutionCount++;
207
208     /* Is it time to perform the check again? */
209         if( ulExecutionCount >= ulExecutionRate )
210         {
211                 /* Reset the execution count so this function is called again in 5
212                 seconds time. */
213                 ulExecutionCount = 0;
214
215                 /* Has an error been found in any task? */
216                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
217                 {
218                         ulCheckErrors |= 0x01UL;
219                 }
220
221                 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
222                 {
223                         ulCheckErrors |= 0x02UL;
224                 }
225
226                 if( xAreBlockingQueuesStillRunning() != pdTRUE )
227                 {
228                         ulCheckErrors |= 0x04UL;
229                 }
230
231                 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
232             {
233                 ulCheckErrors |= 0x200UL;
234             }
235
236                 if( ulLastRegTest1Count == ulRegTest1Counter )
237                 {
238                         ulCheckErrors |= 0x1000UL;
239                 }
240
241                 if( ulLastRegTest2Count == ulRegTest2Counter )
242                 {
243                         ulCheckErrors |= 0x1000UL;
244                 }
245
246                 ulLastRegTest1Count = ulRegTest1Counter;
247                 ulLastRegTest2Count = ulRegTest2Counter;
248         }
249 }
250 /*-----------------------------------------------------------*/
251
252 static void prvSetupHardware( void )
253 {
254         /* Disable the watchdog, STOP and WAIT modes. */
255         SOPT1 = 0;
256
257         /* --- Setup clock to use external 25MHz source. --- */
258
259         /* Extal and xtal pin ON. */
260         PTDPF1_D4 = 0x03;
261         PTDPF1_D5 = 0x03;
262
263         /* Switch from FEI to FBE (FLL bypassed external)
264         enable external clock source */
265         MCGC2 = MCGC2_ERCLKEN_MASK  /* Activate external reference clock */
266               | MCGC2_EREFS_MASK    /* Because crystal is being used */
267               | MCGC2_RANGE_MASK;   /* High range */
268
269         /* Select clock mode and clear IREFs. */
270         MCGC1 = (0x02 << 6 )        /* CLKS = 10 -> external reference clock. */
271               | (0x04 << 3 )        /* RDIV = 2^4 -> 25MHz/16 = 1.5625 MHz */
272               | MCGC1_IRCLKEN_MASK; /* IRCLK to RTC enabled */
273
274         /* Wait for Reference and Clock status bits to update. */
275         while( MCGSC_IREFST | ( MCGSC_CLKST != 0x02 ) )
276         {
277                 /* Nothing to do here. */
278         }
279
280         /* Switch from FBE to PBE (PLL bypassed internal) mode. */
281         MCGC3 =  0x08               /* Set PLL multi 50MHz. */
282               |  MCGC3_PLLS_MASK;   /* Select PLL. */
283
284         /* Wait for PLL status and lock bits to update. */
285         while( !MCGSC_PLLST | !MCGSC_LOCK )
286         {
287                 /* Nothing to do here. */
288         }
289
290
291         /* Now in PBE Mode, finally switch from PBE to PEE (PLL enabled external
292         mode). */
293         MCGC1_CLKS  = 0b00; /* PLL clock to system (MCGOUT) */
294
295         /* Wait for the clock status bits to update. */
296         while( MCGSC_CLKST != 0x03 )
297         {
298                 /* Nothing to do here. */
299         }
300
301         /* Setup the IO for the LED outputs. */
302         vParTestInitialise();
303 }
304 /*-----------------------------------------------------------*/
305
306 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
307 {
308         /* This will get called if a stack overflow is detected during the context
309         switch.  Set configCHECK_FOR_STACK_OVERFLOWS to 2 to also check for stack
310         problems within nested interrupts, but only do this for debug purposes as
311         it will increase the context switch time. */
312
313         ( void ) pxTask;
314         ( void ) pcTaskName;
315
316         for( ;; )
317         {
318         }
319 }
320 /*-----------------------------------------------------------*/
321
322 static void vRegTest1Task( void *pvParameters )
323 {
324   /* Just to remove compiler warnings. */
325   ( void ) pvParameters;
326
327         /* Set all the registers to known values, then check that each retains its
328         expected value - as described at the top of this file.  If an error is
329         found then the loop counter will no longer be incremented allowing the check
330         task to recognise the error. */
331         asm volatile    (       "reg_test_1_start:                                              \n\t"
332                                                 "       moveq           #1, d0                                  \n\t"
333                                                 "       moveq           #2, d1                                  \n\t"
334                                                 "       moveq           #3, d2                                  \n\t"
335                                                 "       moveq           #4, d3                                  \n\t"
336                                                 "       moveq           #5, d4                                  \n\t"
337                                                 "       moveq           #6, d5                                  \n\t"
338                                                 "       moveq           #7, d6                                  \n\t"
339                                                 "       moveq           #8, d7                                  \n\t"
340                                                 "       move            #9, a0                                  \n\t"
341                                                 "       move            #10, a1                                 \n\t"
342                                                 "       move            #11, a2                                 \n\t"
343                                                 "       move            #12, a3                                 \n\t"
344                                                 "       move            #13, a4                                 \n\t"
345                                                 "       move            #15, a6                                 \n\t"
346                                                 "                                                                               \n\t"
347                                                 "       cmpi.l          #1, d0                                  \n\t"
348                                                 "       bne                     reg_test_1_error                \n\t"
349                                                 "       cmpi.l          #2, d1                                  \n\t"
350                                                 "       bne                     reg_test_1_error                \n\t"
351                                                 "       cmpi.l          #3, d2                                  \n\t"
352                                                 "       bne                     reg_test_1_error                \n\t"
353                                                 "       cmpi.l          #4, d3                                  \n\t"
354                                                 "       bne                     reg_test_1_error                \n\t"
355                                                 "       cmpi.l          #5, d4                                  \n\t"
356                                                 "       bne                     reg_test_1_error                \n\t"
357                                                 "       cmpi.l          #6, d5                                  \n\t"
358                                                 "       bne                     reg_test_1_error                \n\t"
359                                                 "       cmpi.l          #7, d6                                  \n\t"
360                                                 "       bne                     reg_test_1_error                \n\t"
361                                                 "       cmpi.l          #8, d7                                  \n\t"
362                                                 "       bne                     reg_test_1_error                \n\t"
363                                                 "       move            a0, d0                                  \n\t"
364                                                 "       cmpi.l          #9, d0                                  \n\t"
365                                                 "       bne                     reg_test_1_error                \n\t"
366                                                 "       move            a1, d0                                  \n\t"
367                                                 "       cmpi.l          #10, d0                                 \n\t"
368                                                 "       bne                     reg_test_1_error                \n\t"
369                                                 "       move            a2, d0                                  \n\t"
370                                                 "       cmpi.l          #11, d0                                 \n\t"
371                                                 "       bne                     reg_test_1_error                \n\t"
372                                                 "       move            a3, d0                                  \n\t"
373                                                 "       cmpi.l          #12, d0                                 \n\t"
374                                                 "       bne                     reg_test_1_error                \n\t"
375                                                 "       move            a4, d0                                  \n\t"
376                                                 "       cmpi.l          #13, d0                                 \n\t"
377                                                 "       bne                     reg_test_1_error                \n\t"
378                                                 "       move            a6, d0                                  \n\t"
379                                                 "       cmpi.l          #15, d0                                 \n\t"
380                                                 "       bne                     reg_test_1_error                \n\t"
381                                                 "       move            ulRegTest1Counter, d0   \n\t"
382                                                 "       addq            #1, d0                                  \n\t"
383                                                 "       move            d0, ulRegTest1Counter   \n\t"
384                                                 "       bra                     reg_test_1_start                \n\t"
385                                                 "reg_test_1_error:                                              \n\t"
386                                                 "       bra                     reg_test_1_error                \n\t"
387                                         );
388 }
389 /*-----------------------------------------------------------*/
390
391 static void vRegTest2Task( void *pvParameters )
392 {
393   /* Just to remove compiler warnings. */
394   ( void ) pvParameters;
395
396         /* Set all the registers to known values, then check that each retains its
397         expected value - as described at the top of this file.  If an error is
398         found then the loop counter will no longer be incremented allowing the check
399         task to recognise the error. */
400         asm volatile    (       "reg_test_2_start:                                              \n\t"
401                                                 "       moveq           #10, d0                                 \n\t"
402                                                 "       moveq           #20, d1                                 \n\t"
403                                                 "       moveq           #30, d2                                 \n\t"
404                                                 "       moveq           #40, d3                                 \n\t"
405                                                 "       moveq           #50, d4                                 \n\t"
406                                                 "       moveq           #60, d5                                 \n\t"
407                                                 "       moveq           #70, d6                                 \n\t"
408                                                 "       moveq           #80, d7                                 \n\t"
409                                                 "       move            #90, a0                                 \n\t"
410                                                 "       move            #100, a1                                \n\t"
411                                                 "       move            #110, a2                                \n\t"
412                                                 "       move            #120, a3                                \n\t"
413                                                 "       move            #130, a4                                \n\t"
414                                                 "       move            #150, a6                                \n\t"
415                                                 "                                                                               \n\t"
416                                                 "       cmpi.l          #10, d0                                 \n\t"
417                                                 "       bne                     reg_test_2_error                \n\t"
418                                                 "       cmpi.l          #20, d1                                 \n\t"
419                                                 "       bne                     reg_test_2_error                \n\t"
420                                                 "       cmpi.l          #30, d2                                 \n\t"
421                                                 "       bne                     reg_test_2_error                \n\t"
422                                                 "       cmpi.l          #40, d3                                 \n\t"
423                                                 "       bne                     reg_test_2_error                \n\t"
424                                                 "       cmpi.l          #50, d4                                 \n\t"
425                                                 "       bne                     reg_test_2_error                \n\t"
426                                                 "       cmpi.l          #60, d5                                 \n\t"
427                                                 "       bne                     reg_test_2_error                \n\t"
428                                                 "       cmpi.l          #70, d6                                 \n\t"
429                                                 "       bne                     reg_test_2_error                \n\t"
430                                                 "       cmpi.l          #80, d7                                 \n\t"
431                                                 "       bne                     reg_test_2_error                \n\t"
432                                                 "       move            a0, d0                                  \n\t"
433                                                 "       cmpi.l          #90, d0                                 \n\t"
434                                                 "       bne                     reg_test_2_error                \n\t"
435                                                 "       move            a1, d0                                  \n\t"
436                                                 "       cmpi.l          #100, d0                                \n\t"
437                                                 "       bne                     reg_test_2_error                \n\t"
438                                                 "       move            a2, d0                                  \n\t"
439                                                 "       cmpi.l          #110, d0                                \n\t"
440                                                 "       bne                     reg_test_2_error                \n\t"
441                                                 "       move            a3, d0                                  \n\t"
442                                                 "       cmpi.l          #120, d0                                \n\t"
443                                                 "       bne                     reg_test_2_error                \n\t"
444                                                 "       move            a4, d0                                  \n\t"
445                                                 "       cmpi.l          #130, d0                                \n\t"
446                                                 "       bne                     reg_test_2_error                \n\t"
447                                                 "       move            a6, d0                                  \n\t"
448                                                 "       cmpi.l          #150, d0                                \n\t"
449                                                 "       bne                     reg_test_2_error                \n\t"
450                                                 "       move            ulRegTest1Counter, d0   \n\t"
451                                                 "       addq            #1, d0                                  \n\t"
452                                                 "       move            d0, ulRegTest2Counter   \n\t"
453                                                 "       bra                     reg_test_2_start                \n\t"
454                                                 "reg_test_2_error:                                              \n\t"
455                                                 "       bra                     reg_test_2_error                \n\t"
456                                         );
457 }
458 /*-----------------------------------------------------------*/
459