2 FreeRTOS.org V4.0.4 - Copyright (C) 2003-2006 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\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
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
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
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
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
34 * Creates the demo application tasks, then starts the scheduler. The WEB
\r
35 * documentation provides more details of the demo application tasks.
\r
37 * Main. c also creates four other tasks:
\r
40 * This only executes every few seconds but has the highest priority so is
\r
41 * guaranteed to get processor time. Its main function is to check that all
\r
42 * the standard demo application tasks are still operational and have not
\r
43 * experienced any errors. vErrorChecks() will toggle the on board LED
\r
44 * every mainNO_ERROR_FLASH_PERIOD milliseconds if none of the demo application
\r
45 * tasks have reported an error. Should any task report an error at any time
\r
46 * the rate at which the on board LED is toggled is increased to
\r
47 * mainERROR_FLASH_PERIOD - providing visual feedback that something has gone
\r
50 * 2) vRegisterCheck()
\r
51 * This is a very simple task that checks that all the registers are always
\r
52 * in their expected state. The task only makes use of the A register, so
\r
53 * all the other registers should always contain their initial values.
\r
54 * An incorrect value indicates an error in the context switch mechanism.
\r
55 * The task operates at the idle priority so will be preempted regularly.
\r
56 * Any error will cause the toggle rate of the on board LED to increase to
\r
57 * mainERROR_FLASH_PERIOD milliseconds.
\r
59 * 3 and 4) vFLOPCheck1() and vFLOPCheck2()
\r
60 * These are very basic versions of the standard FLOP tasks. They are good
\r
61 * at detecting errors in the context switch mechanism, and also check that
\r
62 * the floating point libraries are correctly built to be re-enterant. The
\r
63 * stack restrictions of the 8051 prevent the use of the standard FLOP demo
\r
67 /* Standard includes. */
\r
70 /* Scheduler includes. */
\r
71 #include "FreeRTOS.h"
\r
74 /* Demo application includes. */
\r
75 #include "partest.h"
\r
77 #include "integer.h"
\r
79 #include "comtest2.h"
\r
80 #include "semtest.h"
\r
82 /* Demo task priorities. */
\r
83 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
84 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
85 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
86 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
87 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
88 #define mainINTEGER_PRIORITY tskIDLE_PRIORITY
\r
90 /* Constants required to disable the watchdog. */
\r
91 #define mainDISABLE_BYTE_1 ( ( unsigned portCHAR ) 0xde )
\r
92 #define mainDISABLE_BYTE_2 ( ( unsigned portCHAR ) 0xad )
\r
94 /* Constants to setup and use the on board LED. */
\r
95 #define ucLED_BIT ( ( unsigned portCHAR ) 0x40 )
\r
96 #define mainPORT_1_BIT_6 ( ( unsigned portCHAR ) 0x40 )
\r
97 #define mainENABLE_CROSS_BAR ( ( unsigned portCHAR ) 0x40 )
\r
99 /* Constants to set the clock frequency. */
\r
100 #define mainSELECT_INTERNAL_OSC ( ( unsigned portCHAR ) 0x80 )
\r
101 #define mainDIVIDE_CLOCK_BY_1 ( ( unsigned portCHAR ) 0x03 )
\r
102 #define mainPLL_USES_INTERNAL_OSC ( ( unsigned portCHAR ) 0x04 )
\r
103 #define mainFLASH_READ_TIMING ( ( unsigned portCHAR ) 0x30 )
\r
104 #define mainPLL_POWER_ON ( ( unsigned portCHAR ) 0x01 )
\r
105 #define mainPLL_NO_PREDIVIDE ( ( unsigned portCHAR ) 0x01 )
\r
106 #define mainPLL_FILTER ( ( unsigned portCHAR ) 0x01 )
\r
107 #define mainPLL_MULTIPLICATION ( ( unsigned portCHAR ) 0x04 )
\r
108 #define mainENABLE_PLL ( ( unsigned portCHAR ) 0x02 )
\r
109 #define mainPLL_LOCKED ( ( unsigned portCHAR ) 0x10 )
\r
110 #define mainSELECT_PLL_AS_SOURCE ( ( unsigned portCHAR ) 0x02 )
\r
112 /* Toggle rate for the on board LED - which is dependent on whether or not
\r
113 an error has been detected. */
\r
114 #define mainNO_ERROR_FLASH_PERIOD ( ( portTickType ) 5000 )
\r
115 #define mainERROR_FLASH_PERIOD ( ( portTickType ) 250 )
\r
117 /* Baud rate used by the serial port tasks. */
\r
118 #define mainCOM_TEST_BAUD_RATE ( ( unsigned portLONG ) 115200 )
\r
120 /* Pass an invalid LED number to the COM test task as we don't want it to flash
\r
121 an LED. There are only 8 LEDs (excluding the on board LED) wired in and these
\r
122 are all used by the flash tasks. */
\r
123 #define mainCOM_TEST_LED ( 200 )
\r
125 /* We want the Cygnal to act as much as possible as a standard 8052. */
\r
126 #define mainAUTO_SFR_OFF ( ( unsigned portCHAR ) 0 )
\r
128 /* Constants required to setup the IO pins for serial comms. */
\r
129 #define mainENABLE_COMS ( ( unsigned portCHAR ) 0x04 )
\r
130 #define mainCOMS_LINES_TO_PUSH_PULL ( ( unsigned portCHAR ) 0x03 )
\r
132 /* Pointer passed as a parameter to vRegisterCheck() just so it has some know
\r
133 values to check for in the DPH, DPL and B registers. */
\r
134 #define mainDUMMY_POINTER ( ( xdata void * ) 0xabcd )
\r
136 /* Macro that lets vErrorChecks() know that one of the tasks defined in
\r
137 main. c has detected an error. A critical region is used around xLatchError
\r
138 as it is accessed from vErrorChecks(), which has a higher priority. */
\r
139 #define mainLATCH_ERROR() \
\r
141 portENTER_CRITICAL(); \
\r
142 xLatchedError = pdTRUE; \
\r
143 portEXIT_CRITICAL(); \
\r
147 * Setup the Cygnal microcontroller for its fastest operation.
\r
149 static void prvSetupSystemClock( void );
\r
152 * Setup the peripherals, including the on board LED.
\r
154 static void prvSetupHardware( void );
\r
157 * Toggle the state of the on board LED.
\r
159 static void prvToggleOnBoardLED( void );
\r
162 * See comments at the top of the file for details.
\r
164 static void vErrorChecks( void *pvParameters );
\r
167 * See comments at the top of the file for details.
\r
169 static void vRegisterCheck( void *pvParameters );
\r
172 * See comments at the top of the file for details.
\r
174 static void vFLOPCheck1( void *pvParameters );
\r
177 * See comments at the top of the file for details.
\r
179 static void vFLOPCheck2( void *pvParameters );
\r
181 /* File scope variable used to communicate the occurrence of an error between
\r
183 static portBASE_TYPE xLatchedError = pdFALSE;
\r
185 /*-----------------------------------------------------------*/
\r
188 * Starts all the other tasks, then starts the scheduler.
\r
192 /* Initialise the hardware including the system clock and on board
\r
194 prvSetupHardware();
\r
196 /* Initialise the port that controls the external LED's utilized by the
\r
198 vParTestInitialise();
\r
200 /* Start the used standard demo tasks. */
\r
201 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
202 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
203 vStartIntegerMathTasks( mainINTEGER_PRIORITY );
\r
204 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
205 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
207 /* Start the tasks defined in this file. The first three never block so
\r
208 must not be used with the co-operative scheduler. */
\r
209 #if configUSE_PREEMPTION == 1
\r
211 xTaskCreate( vRegisterCheck, "RegChck", configMINIMAL_STACK_SIZE, mainDUMMY_POINTER, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
212 xTaskCreate( vFLOPCheck1, "FLOP", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
213 xTaskCreate( vFLOPCheck2, "FLOP", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
217 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, ( xTaskHandle * ) NULL );
\r
219 /* Finally kick off the scheduler. This function should never return. */
\r
220 vTaskStartScheduler();
\r
222 /* Should never reach here as the tasks will now be executing under control
\r
223 of the scheduler. */
\r
225 /*-----------------------------------------------------------*/
\r
228 * Setup the hardware prior to using the scheduler. Most of the Cygnal
\r
229 * specific initialisation is performed here leaving standard 8052 setup
\r
230 * only in the driver code.
\r
232 static void prvSetupHardware( void )
\r
234 unsigned portCHAR ucOriginalSFRPage;
\r
236 /* Remember the SFR page before it is changed so it can get set back
\r
237 before the function exits. */
\r
238 ucOriginalSFRPage = SFRPAGE;
\r
240 /* Setup the SFR page to access the config SFR's. */
\r
241 SFRPAGE = CONFIG_PAGE;
\r
243 /* Don't allow the microcontroller to automatically switch SFR page, as the
\r
244 SFR page is not stored as part of the task context. */
\r
245 SFRPGCN = mainAUTO_SFR_OFF;
\r
247 /* Disable the watchdog. */
\r
248 WDTCN = mainDISABLE_BYTE_1;
\r
249 WDTCN = mainDISABLE_BYTE_2;
\r
251 /* Set the on board LED to push pull. */
\r
252 P1MDOUT |= mainPORT_1_BIT_6;
\r
254 /* Setup the cross bar to enable serial comms here as it is not part of the
\r
255 standard 8051 setup and therefore is not in the driver code. */
\r
256 XBR0 |= mainENABLE_COMS;
\r
257 P0MDOUT |= mainCOMS_LINES_TO_PUSH_PULL;
\r
259 /* Enable the cross bar so our hardware setup takes effect. */
\r
260 XBR2 = mainENABLE_CROSS_BAR;
\r
262 /* Setup a fast system clock. */
\r
263 prvSetupSystemClock();
\r
265 /* Return the SFR page. */
\r
266 SFRPAGE = ucOriginalSFRPage;
\r
268 /*-----------------------------------------------------------*/
\r
270 static void prvSetupSystemClock( void )
\r
272 volatile unsigned portSHORT usWait;
\r
273 const unsigned portSHORT usWaitTime = ( unsigned portSHORT ) 0x2ff;
\r
274 unsigned portCHAR ucOriginalSFRPage;
\r
276 /* Remember the SFR page so we can set it back at the end. */
\r
277 ucOriginalSFRPage = SFRPAGE;
\r
278 SFRPAGE = CONFIG_PAGE;
\r
280 /* Use the internal oscillator set to its fasted frequency. */
\r
281 OSCICN = mainSELECT_INTERNAL_OSC | mainDIVIDE_CLOCK_BY_1;
\r
283 /* Ensure the clock is stable. */
\r
284 for( usWait = 0; usWait < usWaitTime; usWait++ );
\r
286 /* Setup the clock source for the PLL. */
\r
287 PLL0CN &= ~mainPLL_USES_INTERNAL_OSC;
\r
289 /* Change the read timing for the flash ready for the fast clock. */
\r
290 SFRPAGE = LEGACY_PAGE;
\r
291 FLSCL |= mainFLASH_READ_TIMING;
\r
293 /* Turn on the PLL power. */
\r
294 SFRPAGE = CONFIG_PAGE;
\r
295 PLL0CN |= mainPLL_POWER_ON;
\r
297 /* Don't predivide the clock. */
\r
298 PLL0DIV = mainPLL_NO_PREDIVIDE;
\r
300 /* Set filter for fastest clock. */
\r
301 PLL0FLT = mainPLL_FILTER;
\r
302 PLL0MUL = mainPLL_MULTIPLICATION;
\r
304 /* Ensure the clock is stable. */
\r
305 for( usWait = 0; usWait < usWaitTime; usWait++ );
\r
307 /* Enable the PLL and wait for it to lock. */
\r
308 PLL0CN |= mainENABLE_PLL;
\r
309 for( usWait = 0; usWait < usWaitTime; usWait++ )
\r
311 if( PLL0CN & mainPLL_LOCKED )
\r
317 /* Select the PLL as the clock source. */
\r
318 CLKSEL |= mainSELECT_PLL_AS_SOURCE;
\r
320 /* Return the SFR back to its original value. */
\r
321 SFRPAGE = ucOriginalSFRPage;
\r
323 /*-----------------------------------------------------------*/
\r
325 static void prvToggleOnBoardLED( void )
\r
327 /* If the on board LED is on, turn it off and visa versa. */
\r
328 if( P1 & ucLED_BIT )
\r
337 /*-----------------------------------------------------------*/
\r
340 * See the documentation at the top of this file.
\r
342 static void vErrorChecks( void *pvParameters )
\r
344 portBASE_TYPE xErrorHasOccurred = pdFALSE;
\r
346 /* Just to prevent compiler warnings. */
\r
347 ( void ) pvParameters;
\r
349 /* Cycle for ever, delaying then checking all the other tasks are still
\r
350 operating without error. The delay period depends on whether an error
\r
351 has ever been detected. */
\r
354 if( xLatchedError == pdFALSE )
\r
356 /* No errors have been detected so delay for a longer period. The
\r
357 on board LED will get toggled every mainNO_ERROR_FLASH_PERIOD ms. */
\r
358 vTaskDelay( mainNO_ERROR_FLASH_PERIOD );
\r
362 /* We have at some time recognised an error in one of the demo
\r
363 application tasks, delay for a shorter period. The on board LED
\r
364 will get toggled every mainERROR_FLASH_PERIOD ms. */
\r
365 vTaskDelay( mainERROR_FLASH_PERIOD );
\r
370 /* Check the demo application tasks for errors. */
\r
372 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
374 xErrorHasOccurred = pdTRUE;
\r
377 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
379 xErrorHasOccurred = pdTRUE;
\r
382 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
384 xErrorHasOccurred = pdTRUE;
\r
387 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
389 xErrorHasOccurred = pdTRUE;
\r
392 /* If an error has occurred, latch it to cause the LED flash rate to
\r
394 if( xErrorHasOccurred == pdTRUE )
\r
396 xLatchedError = pdTRUE;
\r
399 /* Toggle the LED to indicate the completion of a check cycle. The
\r
400 frequency of check cycles is dependent on whether or not we have
\r
401 latched an error. */
\r
402 prvToggleOnBoardLED();
\r
405 /*-----------------------------------------------------------*/
\r
408 * See the documentation at the top of this file. Also see the standard FLOP
\r
409 * demo task documentation for the rationale of these tasks.
\r
411 static void vFLOPCheck1( void *pvParameters )
\r
413 volatile portFLOAT fVal1, fVal2, fResult;
\r
415 ( void ) pvParameters;
\r
419 fVal1 = ( portFLOAT ) -1234.5678;
\r
420 fVal2 = ( portFLOAT ) 2345.6789;
\r
422 fResult = fVal1 + fVal2;
\r
423 if( ( fResult > ( portFLOAT ) 1111.15 ) || ( fResult < ( portFLOAT ) 1111.05 ) )
\r
428 fResult = fVal1 / fVal2;
\r
429 if( ( fResult > ( portFLOAT ) -0.51 ) || ( fResult < ( portFLOAT ) -0.53 ) )
\r
435 /*-----------------------------------------------------------*/
\r
438 * See the documentation at the top of this file.
\r
440 static void vFLOPCheck2( void *pvParameters )
\r
442 volatile portFLOAT fVal1, fVal2, fResult;
\r
444 ( void ) pvParameters;
\r
448 fVal1 = ( portFLOAT ) -12340.5678;
\r
449 fVal2 = ( portFLOAT ) 23450.6789;
\r
451 fResult = fVal1 + fVal2;
\r
452 if( ( fResult > ( portFLOAT ) 11110.15 ) || ( fResult < ( portFLOAT ) 11110.05 ) )
\r
457 fResult = fVal1 / -fVal2;
\r
458 if( ( fResult > ( portFLOAT ) 0.53 ) || ( fResult < ( portFLOAT ) 0.51 ) )
\r
464 /*-----------------------------------------------------------*/
\r
467 * See the documentation at the top of this file.
\r
469 static void vRegisterCheck( void *pvParameters )
\r
471 ( void ) pvParameters;
\r
475 if( SP != configSTACK_START )
\r