]> begriffs open source - cmsis-freertos/blob - Demo/Cygnal/main.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / Cygnal / 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  * Creates the demo application tasks, then starts the scheduler.  The WEB
72  * documentation provides more details of the demo application tasks.
73  * 
74  * Main. c also creates four other tasks:
75  * 
76  * 1) vErrorChecks()
77  * This only executes every few seconds but has the highest priority so is 
78  * guaranteed to get processor time.  Its main function is to check that all 
79  * the standard demo application tasks are still operational and have not
80  * experienced any errors.  vErrorChecks() will toggle the on board LED
81  * every mainNO_ERROR_FLASH_PERIOD milliseconds if none of the demo application
82  * tasks have reported an error.  Should any task report an error at any time
83  * the rate at which the on board LED is toggled is increased to 
84  * mainERROR_FLASH_PERIOD - providing visual feedback that something has gone
85  * wrong.
86  *
87  * 2) vRegisterCheck()
88  * This is a very simple task that checks that all the registers are always
89  * in their expected state.  The task only makes use of the A register, so
90  * all the other registers should always contain their initial values.
91  * An incorrect value indicates an error in the context switch mechanism.
92  * The task operates at the idle priority so will be preempted regularly.
93  * Any error will cause the toggle rate of the on board LED to increase to
94  * mainERROR_FLASH_PERIOD milliseconds.
95  *
96  * 3 and 4) vFLOPCheck1() and vFLOPCheck2()
97  * These are very basic versions of the standard FLOP tasks.  They are good
98  * at detecting errors in the context switch mechanism, and also check that
99  * the floating point libraries are correctly built to be re-enterant.  The
100  * stack restrictions of the 8051 prevent the use of the standard FLOP demo
101  * tasks.
102  */
103
104 /* Standard includes. */
105 #include <stdlib.h>
106
107 /* Scheduler includes. */
108 #include "FreeRTOS.h"
109 #include "task.h"
110
111 /* Demo application includes. */
112 #include "partest.h"
113 #include "flash.h"
114 #include "integer.h"
115 #include "PollQ.h"
116 #include "comtest2.h"
117 #include "semtest.h"
118
119 /* Demo task priorities. */
120 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )
121 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )
122 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )
123 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 3 )
124 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )
125 #define mainINTEGER_PRIORITY            tskIDLE_PRIORITY
126
127 /* Constants required to disable the watchdog. */
128 #define mainDISABLE_BYTE_1                      ( ( unsigned char ) 0xde )
129 #define mainDISABLE_BYTE_2                      ( ( unsigned char ) 0xad )
130
131 /* Constants to setup and use the on board LED. */
132 #define ucLED_BIT                                       ( ( unsigned char ) 0x40 )
133 #define mainPORT_1_BIT_6                        ( ( unsigned char ) 0x40 )
134 #define mainENABLE_CROSS_BAR            ( ( unsigned char ) 0x40 )
135
136 /* Constants to set the clock frequency. */
137 #define mainSELECT_INTERNAL_OSC         ( ( unsigned char ) 0x80 )
138 #define mainDIVIDE_CLOCK_BY_1           ( ( unsigned char ) 0x03 )
139 #define mainPLL_USES_INTERNAL_OSC       ( ( unsigned char ) 0x04 )
140 #define mainFLASH_READ_TIMING           ( ( unsigned char ) 0x30 )
141 #define mainPLL_POWER_ON                        ( ( unsigned char ) 0x01 )
142 #define mainPLL_NO_PREDIVIDE            ( ( unsigned char ) 0x01 )
143 #define mainPLL_FILTER                          ( ( unsigned char ) 0x01 )
144 #define mainPLL_MULTIPLICATION          ( ( unsigned char ) 0x04 )
145 #define mainENABLE_PLL                          ( ( unsigned char ) 0x02 )
146 #define mainPLL_LOCKED                          ( ( unsigned char ) 0x10 )
147 #define mainSELECT_PLL_AS_SOURCE        ( ( unsigned char ) 0x02 )
148
149 /* Toggle rate for the on board LED - which is dependent on whether or not
150 an error has been detected. */
151 #define mainNO_ERROR_FLASH_PERIOD       ( ( TickType_t ) 5000 )
152 #define mainERROR_FLASH_PERIOD          ( ( TickType_t ) 250 )
153
154 /* Baud rate used by the serial port tasks. */
155 #define mainCOM_TEST_BAUD_RATE          ( ( unsigned long ) 115200 )
156
157 /* Pass an invalid LED number to the COM test task as we don't want it to flash
158 an LED.  There are only 8 LEDs (excluding the on board LED) wired in and these
159 are all used by the flash tasks. */
160 #define mainCOM_TEST_LED                        ( 200 )
161
162 /* We want the Cygnal to act as much as possible as a standard 8052. */
163 #define mainAUTO_SFR_OFF                        ( ( unsigned char ) 0 )
164
165 /* Constants required to setup the IO pins for serial comms. */
166 #define mainENABLE_COMS                         ( ( unsigned char ) 0x04 )
167 #define mainCOMS_LINES_TO_PUSH_PULL ( ( unsigned char ) 0x03 )
168
169 /* Pointer passed as a parameter to vRegisterCheck() just so it has some know
170 values to check for in the DPH, DPL and B registers. */
171 #define mainDUMMY_POINTER               ( ( xdata void * ) 0xabcd )
172
173 /* Macro that lets vErrorChecks() know that one of the tasks defined in
174 main. c has detected an error.  A critical region is used around xLatchError
175 as it is accessed from vErrorChecks(), which has a higher priority. */ 
176 #define mainLATCH_ERROR()                       \
177 {                                                                       \
178         portENTER_CRITICAL();                   \
179                 xLatchedError = pdTRUE;         \
180         portEXIT_CRITICAL();                    \
181 }
182
183 /*
184  * Setup the Cygnal microcontroller for its fastest operation. 
185  */
186 static void prvSetupSystemClock( void );
187
188 /*
189  * Setup the peripherals, including the on board LED. 
190  */
191 static void prvSetupHardware( void );
192
193 /*
194  * Toggle the state of the on board LED. 
195  */
196 static void prvToggleOnBoardLED( void );
197
198 /*
199  * See comments at the top of the file for details. 
200  */
201 static void vErrorChecks( void *pvParameters );
202
203 /*
204  * See comments at the top of the file for details. 
205  */
206 static void vRegisterCheck( void *pvParameters );
207
208 /*
209  * See comments at the top of the file for details. 
210  */
211 static void vFLOPCheck1( void *pvParameters );
212
213 /*
214  * See comments at the top of the file for details. 
215  */
216 static void vFLOPCheck2( void *pvParameters );
217
218 /* File scope variable used to communicate the occurrence of an error between
219 tasks. */
220 static portBASE_TYPE xLatchedError = pdFALSE;
221
222 /*-----------------------------------------------------------*/
223
224 /*
225  * Starts all the other tasks, then starts the scheduler. 
226  */
227 void main( void )
228 {
229         /* Initialise the hardware including the system clock and on board
230         LED. */
231         prvSetupHardware();
232
233         /* Initialise the port that controls the external LED's utilized by the
234         flash tasks. */
235         vParTestInitialise();
236
237         /* Start the used standard demo tasks. */
238         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
239         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
240         vStartIntegerMathTasks( mainINTEGER_PRIORITY );
241         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
242         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
243
244         /* Start the tasks defined in this file.  The first three never block so
245         must not be used with the co-operative scheduler. */
246         #if configUSE_PREEMPTION == 1
247         {
248                 xTaskCreate( vRegisterCheck, "RegChck", configMINIMAL_STACK_SIZE, mainDUMMY_POINTER, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );
249                 xTaskCreate( vFLOPCheck1, "FLOP", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );
250                 xTaskCreate( vFLOPCheck2, "FLOP", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );
251         }
252         #endif 
253
254         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, ( TaskHandle_t * ) NULL );
255
256         /* Finally kick off the scheduler.  This function should never return. */
257         vTaskStartScheduler();
258
259         /* Should never reach here as the tasks will now be executing under control
260         of the scheduler. */
261 }
262 /*-----------------------------------------------------------*/
263
264 /*
265  * Setup the hardware prior to using the scheduler.  Most of the Cygnal
266  * specific initialisation is performed here leaving standard 8052 setup
267  * only in the driver code.
268  */
269 static void prvSetupHardware( void )
270 {
271 unsigned char ucOriginalSFRPage;
272
273         /* Remember the SFR page before it is changed so it can get set back
274         before the function exits. */
275         ucOriginalSFRPage = SFRPAGE;
276
277         /* Setup the SFR page to access the config SFR's. */
278         SFRPAGE = CONFIG_PAGE;
279
280         /* Don't allow the microcontroller to automatically switch SFR page, as the
281         SFR page is not stored as part of the task context. */
282         SFRPGCN = mainAUTO_SFR_OFF;
283
284         /* Disable the watchdog. */
285         WDTCN = mainDISABLE_BYTE_1;
286         WDTCN = mainDISABLE_BYTE_2;
287
288         /* Set the on board LED to push pull. */
289         P1MDOUT |= mainPORT_1_BIT_6;
290
291         /* Setup the cross bar to enable serial comms here as it is not part of the 
292         standard 8051 setup and therefore is not in the driver code. */
293         XBR0 |= mainENABLE_COMS;
294         P0MDOUT |= mainCOMS_LINES_TO_PUSH_PULL;
295
296         /* Enable the cross bar so our hardware setup takes effect. */
297         XBR2 = mainENABLE_CROSS_BAR;
298
299         /* Setup a fast system clock. */
300         prvSetupSystemClock();
301
302         /* Return the SFR page. */
303         SFRPAGE = ucOriginalSFRPage;
304 }
305 /*-----------------------------------------------------------*/
306
307 static void prvSetupSystemClock( void )
308 {
309 volatile unsigned short usWait;
310 const unsigned short usWaitTime = ( unsigned short ) 0x2ff;
311 unsigned char ucOriginalSFRPage;
312
313         /* Remember the SFR page so we can set it back at the end. */
314         ucOriginalSFRPage = SFRPAGE;
315         SFRPAGE = CONFIG_PAGE;
316
317         /* Use the internal oscillator set to its fasted frequency. */
318         OSCICN = mainSELECT_INTERNAL_OSC | mainDIVIDE_CLOCK_BY_1;
319
320         /* Ensure the clock is stable. */
321         for( usWait = 0; usWait < usWaitTime; usWait++ );
322
323         /* Setup the clock source for the PLL. */
324         PLL0CN &= ~mainPLL_USES_INTERNAL_OSC;
325
326         /* Change the read timing for the flash ready for the fast clock. */
327         SFRPAGE = LEGACY_PAGE;
328         FLSCL |= mainFLASH_READ_TIMING;
329
330         /* Turn on the PLL power. */
331         SFRPAGE = CONFIG_PAGE;
332         PLL0CN |= mainPLL_POWER_ON;
333
334         /* Don't predivide the clock. */
335         PLL0DIV = mainPLL_NO_PREDIVIDE;
336
337         /* Set filter for fastest clock. */
338         PLL0FLT = mainPLL_FILTER;
339         PLL0MUL = mainPLL_MULTIPLICATION;
340
341         /* Ensure the clock is stable. */
342         for( usWait = 0; usWait < usWaitTime; usWait++ );
343
344         /* Enable the PLL and wait for it to lock. */
345         PLL0CN |= mainENABLE_PLL;
346         for( usWait = 0; usWait < usWaitTime; usWait++ )
347         {
348                 if( PLL0CN & mainPLL_LOCKED )
349                 {
350                         break;
351                 }
352         }
353
354         /* Select the PLL as the clock source. */
355         CLKSEL |= mainSELECT_PLL_AS_SOURCE;
356
357         /* Return the SFR back to its original value. */
358         SFRPAGE = ucOriginalSFRPage;
359 }
360 /*-----------------------------------------------------------*/
361
362 static void prvToggleOnBoardLED( void )
363 {
364         /* If the on board LED is on, turn it off and vice versa. */
365         if( P1 & ucLED_BIT )
366         {
367                 P1 &= ~ucLED_BIT;
368         }
369         else
370         {
371                 P1 |= ucLED_BIT;
372         }
373 }
374 /*-----------------------------------------------------------*/
375
376 /*
377  * See the documentation at the top of this file. 
378  */
379 static void vErrorChecks( void *pvParameters )
380 {
381 portBASE_TYPE xErrorHasOccurred = pdFALSE;
382         
383         /* Just to prevent compiler warnings. */
384         ( void ) pvParameters;
385         
386         /* Cycle for ever, delaying then checking all the other tasks are still
387         operating without error.   The delay period depends on whether an error
388         has ever been detected. */
389         for( ;; )
390         {
391                 if( xLatchedError == pdFALSE )
392                 {               
393                         /* No errors have been detected so delay for a longer period.  The
394                         on board LED will get toggled every mainNO_ERROR_FLASH_PERIOD ms. */
395                         vTaskDelay( mainNO_ERROR_FLASH_PERIOD );
396                 }
397                 else
398                 {
399                         /* We have at some time recognised an error in one of the demo
400                         application tasks, delay for a shorter period.  The on board LED
401                         will get toggled every mainERROR_FLASH_PERIOD ms. */
402                         vTaskDelay( mainERROR_FLASH_PERIOD );
403                 }
404
405                 
406                 
407                 /* Check the demo application tasks for errors. */
408
409                 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
410                 {
411                         xErrorHasOccurred = pdTRUE;
412                 }
413
414                 if( xArePollingQueuesStillRunning() != pdTRUE )
415                 {
416                         xErrorHasOccurred = pdTRUE;
417                 }
418
419                 if( xAreComTestTasksStillRunning() != pdTRUE )
420                 {
421                         xErrorHasOccurred = pdTRUE;
422                 }
423
424                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
425                 {
426                         xErrorHasOccurred = pdTRUE;
427                 }
428
429                 /* If an error has occurred, latch it to cause the LED flash rate to 
430                 increase. */
431                 if( xErrorHasOccurred == pdTRUE )
432                 {
433                         xLatchedError = pdTRUE;
434                 }
435
436                 /* Toggle the LED to indicate the completion of a check cycle.  The
437                 frequency of check cycles is dependent on whether or not we have 
438                 latched an error. */
439                 prvToggleOnBoardLED();
440         }
441 }
442 /*-----------------------------------------------------------*/
443
444 /*
445  * See the documentation at the top of this file.  Also see the standard FLOP
446  * demo task documentation for the rationale of these tasks.
447  */
448 static void vFLOPCheck1( void *pvParameters )
449 {
450 volatile portFLOAT fVal1, fVal2, fResult;
451
452         ( void ) pvParameters;
453
454         for( ;; )
455         {
456                 fVal1 = ( portFLOAT ) -1234.5678;
457                 fVal2 = ( portFLOAT ) 2345.6789;
458
459                 fResult = fVal1 + fVal2;
460                 if( ( fResult > ( portFLOAT )  1111.15 ) || ( fResult < ( portFLOAT ) 1111.05 ) )
461                 {
462                         mainLATCH_ERROR();
463                 }
464
465                 fResult = fVal1 / fVal2;
466                 if( ( fResult > ( portFLOAT ) -0.51 ) || ( fResult < ( portFLOAT ) -0.53 ) )
467                 {
468                         mainLATCH_ERROR();
469                 }
470         }
471 }
472 /*-----------------------------------------------------------*/
473
474 /*
475  * See the documentation at the top of this file.
476  */
477 static void vFLOPCheck2( void *pvParameters )
478 {
479 volatile portFLOAT fVal1, fVal2, fResult;
480
481         ( void ) pvParameters;
482
483         for( ;; )
484         {
485                 fVal1 = ( portFLOAT ) -12340.5678;
486                 fVal2 = ( portFLOAT ) 23450.6789;
487
488                 fResult = fVal1 + fVal2;
489                 if( ( fResult > ( portFLOAT ) 11110.15 ) || ( fResult < ( portFLOAT ) 11110.05 ) )
490                 {
491                         mainLATCH_ERROR();
492                 }
493
494                 fResult = fVal1 / -fVal2;
495                 if( ( fResult > ( portFLOAT ) 0.53 ) || ( fResult < ( portFLOAT ) 0.51 ) )
496                 {
497                         mainLATCH_ERROR();
498                 }
499         }
500 }
501 /*-----------------------------------------------------------*/
502
503 /*
504  * See the documentation at the top of this file. 
505  */
506 static void vRegisterCheck( void *pvParameters )
507 {
508         ( void ) pvParameters;
509
510         for( ;; )
511         {
512                 if( SP != configSTACK_START )
513                 {
514                         mainLATCH_ERROR();
515                 }
516
517                 _asm
518                         MOV ACC, ar0
519                 _endasm;
520
521                 if( ACC != 0 )
522                 {
523                         mainLATCH_ERROR();
524                 }
525
526                 _asm
527                         MOV ACC, ar1
528                 _endasm;
529
530                 if( ACC != 1 )
531                 {
532                         mainLATCH_ERROR();
533                 }
534                 _asm
535                         MOV ACC, ar2
536                 _endasm;
537
538                 if( ACC != 2 )
539                 {
540                         mainLATCH_ERROR();
541                 }
542                 _asm
543                         MOV ACC, ar3
544                 _endasm;
545
546                 if( ACC != 3 )
547                 {
548                         mainLATCH_ERROR();
549                 }
550                 _asm
551                         MOV ACC, ar4
552                 _endasm;
553
554                 if( ACC != 4 )
555                 {
556                         mainLATCH_ERROR();
557                 }
558                 _asm
559                         MOV ACC, ar5
560                 _endasm;
561
562                 if( ACC != 5 )
563                 {
564                         mainLATCH_ERROR();
565                 }
566                 _asm
567                         MOV ACC, ar6
568                 _endasm;
569
570                 if( ACC != 6 )
571                 {
572                         mainLATCH_ERROR();
573                 }
574                 _asm
575                         MOV ACC, ar7
576                 _endasm;
577
578                 if( ACC != 7 )
579                 {
580                         mainLATCH_ERROR();
581                 }
582
583                 if( DPL != 0xcd )
584                 {
585                         mainLATCH_ERROR();
586                 }
587
588                 if( DPH != 0xab )
589                 {
590                         mainLATCH_ERROR();
591                 }
592
593                 if( B != 0x01 )
594                 {
595                         mainLATCH_ERROR();
596                 }                       
597         }
598 }
599
600