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