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