]> begriffs open source - cmsis-freertos/blob - Demo/ColdFire_MCF52233_Eclipse/RTOSDemo/main.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / ColdFire_MCF52233_Eclipse / RTOSDemo / main.c
1 /*
2     FreeRTOS V8.1.1 - Copyright (C) 2014 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     ***************************************************************************
8      *                                                                       *
9      *    FreeRTOS provides completely free yet professionally developed,    *
10      *    robust, strictly quality controlled, supported, and cross          *
11      *    platform software that has become a de facto standard.             *
12      *                                                                       *
13      *    Help yourself get started quickly and support the FreeRTOS         *
14      *    project by purchasing a FreeRTOS tutorial book, reference          *
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *
16      *                                                                       *
17      *    Thank you!                                                         *
18      *                                                                       *
19     ***************************************************************************
20
21     This file is part of the FreeRTOS distribution.
22
23     FreeRTOS is free software; you can redistribute it and/or modify it under
24     the terms of the GNU General Public License (version 2) as published by the
25     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
26
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<
29     >>!   obliged to provide the source code for proprietary components     !<<
30     >>!   outside of the FreeRTOS kernel.                                   !<<
31
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following
35     link: http://www.freertos.org/a00114.html
36
37     1 tab == 4 spaces!
38
39     ***************************************************************************
40      *                                                                       *
41      *    Having a problem?  Start by reading the FAQ "My application does   *
42      *    not run, what could be wrong?"                                     *
43      *                                                                       *
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *
45      *                                                                       *
46     ***************************************************************************
47
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,
49     license and Real Time Engineers Ltd. contact details.
50
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.
54
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS
57     licenses offer ticketed support, indemnification and middleware.
58
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
60     engineered and independently SIL3 certified version for use in safety and
61     mission critical applications that require provable dependability.
62
63     1 tab == 4 spaces!
64 */
65
66
67 /*
68  * Creates all the demo application tasks, then starts the scheduler.  The WEB
69  * documentation provides more details of the standard demo application tasks.
70  * In addition to the standard demo tasks, the following tasks and tests are
71  * defined and/or created within this file:
72  *
73  * "uIP" task -  This is the task that handles the uIP stack.  All TCP/IP
74  * processing is performed in this task.  It manages the WEB server functionality.
75  *
76  * "Check" task -  This only executes every five seconds but has a high priority
77  * to ensure it gets processor time.  Its main function is to check that all the
78  * standard demo tasks are still operational.  An error found in any task will be
79  * latched in the ulErrorCode variable for display through the WEB server (the
80  * error code is displayed at the foot of the table that contains information on
81  * the state of each task).
82  *
83  * "Reg test" tasks - These fill the registers with known values, then check
84  * that each register still contains its expected value.  Each task uses
85  * different values.  The tasks run with very low priority so get preempted very
86  * frequently.  A register containing an unexpected value is indicative of an
87  * error in the context switching mechanism.
88  *
89  */
90
91 /* Standard includes. */
92 #include <stdio.h>
93
94 /* Scheduler includes. */
95 #include "FreeRTOS.h"
96 #include "task.h"
97 #include "queue.h"
98 #include "semphr.h"
99
100 /* Demo app includes. */
101 #include "BlockQ.h"
102 #include "death.h"
103 #include "blocktim.h"
104 #include "flash.h"
105 #include "partest.h"
106 #include "semtest.h"
107 #include "PollQ.h"
108 #include "GenQTest.h"
109 #include "QPeek.h"
110 #include "recmutex.h"
111 #include "IntQueue.h"
112 #include "comtest2.h"
113
114 /*-----------------------------------------------------------*/
115
116 /* The time between cycles of the 'check' functionality - as described at the
117 top of this file. */
118 #define mainCHECK_TASK_PERIOD                                   ( ( portTickType ) 5000 / portTICK_RATE_MS )
119
120 /* Task priorities. */
121 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )
122 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )
123 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )
124 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )
125 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )
126
127 /* The WEB server task uses more stack than most other tasks because of its
128 reliance on using sprintf(). */
129 #define mainBASIC_WEB_STACK_SIZE                        ( configMINIMAL_STACK_SIZE * 2 )
130
131 /*
132  * Configure the hardware for the demo.
133  */
134 static void prvSetupHardware( void );
135
136 /*
137  * Implements the 'check' task functionality as described at the top of this
138  * file.
139  */
140 static void prvCheckTask( void *pvParameters );
141
142 /*
143  * The task that implements the WEB server.
144  */
145 extern void vuIP_Task( void *pvParameters );
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
155 /* Counters used to detect errors within the reg test tasks. */
156 static volatile unsigned long ulRegTest1Counter = 0x11111111, ulRegTest2Counter = 0x22222222;
157
158 /* Any errors that the check task finds in any tasks are latched into
159 ulErrorCode, and then displayed via the WEB server. */
160 static unsigned long ulErrorCode = 0UL;
161
162 /*-----------------------------------------------------------*/
163
164 int main( void )
165 {
166         /* Setup the hardware ready for this demo. */
167         prvSetupHardware();
168
169         /* Create the WEB server task. */
170         xTaskCreate( vuIP_Task, "uIP", mainBASIC_WEB_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY - 1, NULL );
171
172         /* Start the standard demo tasks. */
173         vStartLEDFlashTasks( tskIDLE_PRIORITY );
174         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
175     vCreateBlockTimeTasks();
176         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
177         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
178         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
179         vStartQueuePeekTasks();
180     vStartRecursiveMutexTasks();
181
182         /* Start the reg test tasks - defined in this file. */
183         xTaskCreate( vRegTest1Task, "Reg1", configMINIMAL_STACK_SIZE, ( void * ) &ulRegTest1Counter, tskIDLE_PRIORITY, NULL );
184         xTaskCreate( vRegTest2Task, "Reg2", configMINIMAL_STACK_SIZE, ( void * ) &ulRegTest2Counter, tskIDLE_PRIORITY, NULL );
185
186         /* Create the check task. */
187         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
188
189         /* Start the scheduler. */
190         vTaskStartScheduler();
191
192     /* Will only get here if there was insufficient heap to create the idle
193     task. */
194         for( ;; );
195 }
196 /*-----------------------------------------------------------*/
197
198 static void prvCheckTask( void *pvParameters )
199 {
200 unsigned ulLastRegTest1Count = 0, ulLastRegTest2Count = 0;
201 portTickType xLastExecutionTime;
202
203         /* To prevent compiler warnings. */
204         ( void ) pvParameters;
205
206         /* Initialise the variable used to control our iteration rate prior to
207         its first use. */
208         xLastExecutionTime = xTaskGetTickCount();
209
210         for( ;; )
211         {
212                 /* Wait until it is time to run the tests again. */
213                 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_TASK_PERIOD );
214
215                 /* Has an error been found in any task? */
216                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
217                 {
218                         ulErrorCode |= 0x01UL;
219                 }
220
221                 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
222                 {
223                         ulErrorCode |= 0x02UL;
224                 }
225
226                 if( xAreBlockingQueuesStillRunning() != pdTRUE )
227                 {
228                         ulErrorCode |= 0x04UL;
229                 }
230
231                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
232             {
233                 ulErrorCode |= 0x20UL;
234             }
235
236                 if( xArePollingQueuesStillRunning() != pdTRUE )
237             {
238                 ulErrorCode |= 0x40UL;
239             }
240
241                 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
242                 {
243                         ulErrorCode |= 0x80UL;
244                 }
245
246             if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
247             {
248                 ulErrorCode |= 0x100UL;
249             }
250
251                 if( ulLastRegTest1Count == ulRegTest1Counter )
252                 {
253                         ulErrorCode |= 0x200UL;
254                 }
255
256                 if( ulLastRegTest2Count == ulRegTest2Counter )
257                 {
258                         ulErrorCode |= 0x200UL;
259                 }
260
261                 /* Remember the reg test counts so a stall in their values can be
262                 detected next time around. */
263                 ulLastRegTest1Count = ulRegTest1Counter;
264                 ulLastRegTest2Count = ulRegTest2Counter;
265         }
266 }
267 /*-----------------------------------------------------------*/
268
269 unsigned long ulGetErrorCode( void )
270 {
271         /* Returns the error code for display via the WEB server. */
272         return ulErrorCode;
273 }
274 /*-----------------------------------------------------------*/
275
276 void prvSetupHardware( void )
277 {
278 __attribute__ ((section(".cfmconfig")))
279 static const unsigned long _cfm[6] = {
280         0, /* KEY_UPPER 0x00000400 */
281         0, /* KEY_LOWER 0x00000404 */
282         0, /* CFMPROT 0x00000408 */
283         0, /* CFMSACC 0x0000040C */
284         0, /* CFMDACC 0x00000410 */
285         0, /* CFMSEC 0x00000414 */
286 };
287
288         /* Just to stop compiler warnings. */
289         ( void ) _cfm;
290
291         /* Ensure the watchdog is disabled. */
292         MCF_SCM_CWCR = 0;
293
294     /* Initialize IPSBAR (0x40000000). */
295         asm volatile(
296                 "move.l  #0x40000000,%d0        \n"
297                 "andi.l  #0xC0000000,%d0        \n"
298                 "add.l   #0x1,%d0                       \n"
299                 "move.l  %d0,0x40000000         "
300         );
301
302     /* Initialize FLASHBAR (0x00) */
303         asm volatile(
304                 "move.l  #0x00,%d0                      \n"
305                 "andi.l  #0xFFF80000,%d0        \n"
306                 "add.l   #0x41,%d0                      \n"
307                 "movec   %d0,%FLASHBAR          "
308         );
309
310         portDISABLE_INTERRUPTS();
311
312         /* RAMBAR. */
313         MCF_SCM_RAMBAR = MCF_SCM_RAMBAR_BA( RAMBAR_ADDRESS ) | MCF_SCM_RAMBAR_BDE;
314
315         /* Multiply 25MHz crystal by 12 to get 60MHz clock. */
316         MCF_CLOCK_SYNCR = MCF_CLOCK_SYNCR_MFD(4) | MCF_CLOCK_SYNCR_CLKSRC| MCF_CLOCK_SYNCR_PLLMODE | MCF_CLOCK_SYNCR_PLLEN ;
317         while (!(MCF_CLOCK_SYNSR & MCF_CLOCK_SYNSR_LOCK))
318         {
319         }
320
321         /* Setup the port used to toggle LEDs. */
322         vParTestInitialise();
323 }
324 /*-----------------------------------------------------------*/
325
326 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
327 {
328         /* This will get called if a stack overflow is detected during the context
329         switch.  Set configCHECK_FOR_STACK_OVERFLOWS to 2 to also check for stack
330         problems within nested interrupts, but only do this for debug purposes as
331         it will increase the context switch time. */
332
333         ( void ) pxTask;
334         ( void ) pcTaskName;
335
336         for( ;; );
337 }
338 /*-----------------------------------------------------------*/
339
340 static void vRegTest1Task( void *pvParameters )
341 {
342         /* Sanity check - did we receive the parameter expected? */
343         if( pvParameters != &ulRegTest1Counter )
344         {
345                 /* Change here so the check task can detect that an error occurred. */
346                 for( ;; );
347         }
348
349         /* Set all the registers to known values, then check that each retains its
350         expected value - as described at the top of this file.  If an error is
351         found then the loop counter will no longer be incremented allowing the check
352         task to recognise the error. */
353         asm volatile    (       "reg_test_1_start:                                              \n\t"
354                                                 "       moveq           #1, %d0                                 \n\t"
355                                                 "       moveq           #2, %d1                                 \n\t"
356                                                 "       moveq           #3, %d2                                 \n\t"
357                                                 "       moveq           #4, %d3                                 \n\t"
358                                                 "       moveq           #5, %d4                                 \n\t"
359                                                 "       moveq           #6, %d5                                 \n\t"
360                                                 "       moveq           #7, %d6                                 \n\t"
361                                                 "       moveq           #8, %d7                                 \n\t"
362                                                 "       move            #9, %a0                                 \n\t"
363                                                 "       move            #10, %a1                                \n\t"
364                                                 "       move            #11, %a2                                \n\t"
365                                                 "       move            #12, %a3                                \n\t"
366                                                 "       move            #13, %a4                                \n\t"
367                                                 "       move            #14, %a5                                \n\t"
368                                                 "       move            #15, %a6                                \n\t"
369                                                 "                                                                               \n\t"
370                                                 "       cmpi.l          #1, %d0                                 \n\t"
371                                                 "       bne                     reg_test_1_error                \n\t"
372                                                 "       cmpi.l          #2, %d1                                 \n\t"
373                                                 "       bne                     reg_test_1_error                \n\t"
374                                                 "       cmpi.l          #3, %d2                                 \n\t"
375                                                 "       bne                     reg_test_1_error                \n\t"
376                                                 "       cmpi.l          #4, %d3                                 \n\t"
377                                                 "       bne                     reg_test_1_error                \n\t"
378                                                 "       cmpi.l          #5, %d4                                 \n\t"
379                                                 "       bne                     reg_test_1_error                \n\t"
380                                                 "       cmpi.l          #6, %d5                                 \n\t"
381                                                 "       bne                     reg_test_1_error                \n\t"
382                                                 "       cmpi.l          #7, %d6                                 \n\t"
383                                                 "       bne                     reg_test_1_error                \n\t"
384                                                 "       cmpi.l          #8, %d7                                 \n\t"
385                                                 "       bne                     reg_test_1_error                \n\t"
386                                                 "       move            %a0, %d0                                \n\t"
387                                                 "       cmpi.l          #9, %d0                                 \n\t"
388                                                 "       bne                     reg_test_1_error                \n\t"
389                                                 "       move            %a1, %d0                                \n\t"
390                                                 "       cmpi.l          #10, %d0                                \n\t"
391                                                 "       bne                     reg_test_1_error                \n\t"
392                                                 "       move            %a2, %d0                                \n\t"
393                                                 "       cmpi.l          #11, %d0                                \n\t"
394                                                 "       bne                     reg_test_1_error                \n\t"
395                                                 "       move            %a3, %d0                                \n\t"
396                                                 "       cmpi.l          #12, %d0                                \n\t"
397                                                 "       bne                     reg_test_1_error                \n\t"
398                                                 "       move            %a4, %d0                                \n\t"
399                                                 "       cmpi.l          #13, %d0                                \n\t"
400                                                 "       bne                     reg_test_1_error                \n\t"
401                                                 "       move            %a5, %d0                                \n\t"
402                                                 "       cmpi.l          #14, %d0                                \n\t"
403                                                 "       bne                     reg_test_1_error                \n\t"
404                                                 "       move            %a6, %d0                                \n\t"
405                                                 "       cmpi.l          #15, %d0                                \n\t"
406                                                 "       bne                     reg_test_1_error                \n\t"
407                                                 "       movel           ulRegTest1Counter, %d0  \n\t"
408                                                 "       addql           #1, %d0                                 \n\t"
409                                                 "       movel           %d0, ulRegTest1Counter  \n\t"
410                                                 "       bra                     reg_test_1_start                \n\t"
411                                                 "reg_test_1_error:                                              \n\t"
412                                                 "       bra                     reg_test_1_error                \n\t"
413                                         );
414 }
415 /*-----------------------------------------------------------*/
416
417 static void vRegTest2Task( void *pvParameters )
418 {
419         /* Sanity check - did we receive the parameter expected? */
420         if( pvParameters != &ulRegTest2Counter )
421         {
422                 /* Change here so the check task can detect that an error occurred. */
423                 for( ;; );
424         }
425
426         /* Set all the registers to known values, then check that each retains its
427         expected value - as described at the top of this file.  If an error is
428         found then the loop counter will no longer be incremented allowing the check
429         task to recognise the error. */
430         asm volatile    (       "reg_test_2_start:                                              \n\t"
431                                                 "       moveq           #10, %d0                                \n\t"
432                                                 "       moveq           #20, %d1                                \n\t"
433                                                 "       moveq           #30, %d2                                \n\t"
434                                                 "       moveq           #40, %d3                                \n\t"
435                                                 "       moveq           #50, %d4                                \n\t"
436                                                 "       moveq           #60, %d5                                \n\t"
437                                                 "       moveq           #70, %d6                                \n\t"
438                                                 "       moveq           #80, %d7                                \n\t"
439                                                 "       move            #90, %a0                                \n\t"
440                                                 "       move            #100, %a1                               \n\t"
441                                                 "       move            #110, %a2                               \n\t"
442                                                 "       move            #120, %a3                               \n\t"
443                                                 "       move            #130, %a4                               \n\t"
444                                                 "       move            #140, %a5                               \n\t"
445                                                 "       move            #150, %a6                               \n\t"
446                                                 "                                                                               \n\t"
447                                                 "       cmpi.l          #10, %d0                                \n\t"
448                                                 "       bne                     reg_test_2_error                \n\t"
449                                                 "       cmpi.l          #20, %d1                                \n\t"
450                                                 "       bne                     reg_test_2_error                \n\t"
451                                                 "       cmpi.l          #30, %d2                                \n\t"
452                                                 "       bne                     reg_test_2_error                \n\t"
453                                                 "       cmpi.l          #40, %d3                                \n\t"
454                                                 "       bne                     reg_test_2_error                \n\t"
455                                                 "       cmpi.l          #50, %d4                                \n\t"
456                                                 "       bne                     reg_test_2_error                \n\t"
457                                                 "       cmpi.l          #60, %d5                                \n\t"
458                                                 "       bne                     reg_test_2_error                \n\t"
459                                                 "       cmpi.l          #70, %d6                                \n\t"
460                                                 "       bne                     reg_test_2_error                \n\t"
461                                                 "       cmpi.l          #80, %d7                                \n\t"
462                                                 "       bne                     reg_test_2_error                \n\t"
463                                                 "       move            %a0, %d0                                \n\t"
464                                                 "       cmpi.l          #90, %d0                                \n\t"
465                                                 "       bne                     reg_test_2_error                \n\t"
466                                                 "       move            %a1, %d0                                \n\t"
467                                                 "       cmpi.l          #100, %d0                               \n\t"
468                                                 "       bne                     reg_test_2_error                \n\t"
469                                                 "       move            %a2, %d0                                \n\t"
470                                                 "       cmpi.l          #110, %d0                               \n\t"
471                                                 "       bne                     reg_test_2_error                \n\t"
472                                                 "       move            %a3, %d0                                \n\t"
473                                                 "       cmpi.l          #120, %d0                               \n\t"
474                                                 "       bne                     reg_test_2_error                \n\t"
475                                                 "       move            %a4, %d0                                \n\t"
476                                                 "       cmpi.l          #130, %d0                               \n\t"
477                                                 "       bne                     reg_test_2_error                \n\t"
478                                                 "       move            %a5, %d0                                \n\t"
479                                                 "       cmpi.l          #140, %d0                               \n\t"
480                                                 "       bne                     reg_test_2_error                \n\t"
481                                                 "       move            %a6, %d0                                \n\t"
482                                                 "       cmpi.l          #150, %d0                               \n\t"
483                                                 "       bne                     reg_test_2_error                \n\t"
484                                                 "       movel           ulRegTest1Counter, %d0  \n\t"
485                                                 "       addql           #1, %d0                                 \n\t"
486                                                 "       movel           %d0, ulRegTest2Counter  \n\t"
487                                                 "       bra                     reg_test_2_start                \n\t"
488                                                 "reg_test_2_error:                                              \n\t"
489                                                 "       bra                     reg_test_2_error                \n\t"
490                                         );
491 }
492 /*-----------------------------------------------------------*/
493