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