2 FreeRTOS V5.4.0 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * Looking for a quick start? Then check out the FreeRTOS eBook! *
\r
29 * See http://www.FreeRTOS.org/Documentation for details *
\r
31 ***************************************************************************
\r
35 Please ensure to read the configuration and relevant port sections of the
\r
36 online documentation.
\r
38 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
41 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
44 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
45 licensing and training services.
\r
49 * Creates the demo application tasks, then starts the scheduler. The WEB
\r
50 * documentation provides more details of the demo application tasks.
\r
52 * Main. c also creates four other tasks:
\r
55 * This only executes every few seconds but has the highest priority so is
\r
56 * guaranteed to get processor time. Its main function is to check that all
\r
57 * the standard demo application tasks are still operational and have not
\r
58 * experienced any errors. vErrorChecks() will toggle the on board LED
\r
59 * every mainNO_ERROR_FLASH_PERIOD milliseconds if none of the demo application
\r
60 * tasks have reported an error. Should any task report an error at any time
\r
61 * the rate at which the on board LED is toggled is increased to
\r
62 * mainERROR_FLASH_PERIOD - providing visual feedback that something has gone
\r
65 * 2) vRegisterCheck()
\r
66 * This is a very simple task that checks that all the registers are always
\r
67 * in their expected state. The task only makes use of the A register, so
\r
68 * all the other registers should always contain their initial values.
\r
69 * An incorrect value indicates an error in the context switch mechanism.
\r
70 * The task operates at the idle priority so will be preempted regularly.
\r
71 * Any error will cause the toggle rate of the on board LED to increase to
\r
72 * mainERROR_FLASH_PERIOD milliseconds.
\r
74 * 3 and 4) vFLOPCheck1() and vFLOPCheck2()
\r
75 * These are very basic versions of the standard FLOP tasks. They are good
\r
76 * at detecting errors in the context switch mechanism, and also check that
\r
77 * the floating point libraries are correctly built to be re-enterant. The
\r
78 * stack restrictions of the 8051 prevent the use of the standard FLOP demo
\r
82 /* Standard includes. */
\r
85 /* Scheduler includes. */
\r
86 #include "FreeRTOS.h"
\r
89 /* Demo application includes. */
\r
90 #include "partest.h"
\r
92 #include "integer.h"
\r
94 #include "comtest2.h"
\r
95 #include "semtest.h"
\r
97 /* Demo task priorities. */
\r
98 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
99 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
100 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
101 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
102 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
103 #define mainINTEGER_PRIORITY tskIDLE_PRIORITY
\r
105 /* Constants required to disable the watchdog. */
\r
106 #define mainDISABLE_BYTE_1 ( ( unsigned portCHAR ) 0xde )
\r
107 #define mainDISABLE_BYTE_2 ( ( unsigned portCHAR ) 0xad )
\r
109 /* Constants to setup and use the on board LED. */
\r
110 #define ucLED_BIT ( ( unsigned portCHAR ) 0x40 )
\r
111 #define mainPORT_1_BIT_6 ( ( unsigned portCHAR ) 0x40 )
\r
112 #define mainENABLE_CROSS_BAR ( ( unsigned portCHAR ) 0x40 )
\r
114 /* Constants to set the clock frequency. */
\r
115 #define mainSELECT_INTERNAL_OSC ( ( unsigned portCHAR ) 0x80 )
\r
116 #define mainDIVIDE_CLOCK_BY_1 ( ( unsigned portCHAR ) 0x03 )
\r
117 #define mainPLL_USES_INTERNAL_OSC ( ( unsigned portCHAR ) 0x04 )
\r
118 #define mainFLASH_READ_TIMING ( ( unsigned portCHAR ) 0x30 )
\r
119 #define mainPLL_POWER_ON ( ( unsigned portCHAR ) 0x01 )
\r
120 #define mainPLL_NO_PREDIVIDE ( ( unsigned portCHAR ) 0x01 )
\r
121 #define mainPLL_FILTER ( ( unsigned portCHAR ) 0x01 )
\r
122 #define mainPLL_MULTIPLICATION ( ( unsigned portCHAR ) 0x04 )
\r
123 #define mainENABLE_PLL ( ( unsigned portCHAR ) 0x02 )
\r
124 #define mainPLL_LOCKED ( ( unsigned portCHAR ) 0x10 )
\r
125 #define mainSELECT_PLL_AS_SOURCE ( ( unsigned portCHAR ) 0x02 )
\r
127 /* Toggle rate for the on board LED - which is dependent on whether or not
\r
128 an error has been detected. */
\r
129 #define mainNO_ERROR_FLASH_PERIOD ( ( portTickType ) 5000 )
\r
130 #define mainERROR_FLASH_PERIOD ( ( portTickType ) 250 )
\r
132 /* Baud rate used by the serial port tasks. */
\r
133 #define mainCOM_TEST_BAUD_RATE ( ( unsigned portLONG ) 115200 )
\r
135 /* Pass an invalid LED number to the COM test task as we don't want it to flash
\r
136 an LED. There are only 8 LEDs (excluding the on board LED) wired in and these
\r
137 are all used by the flash tasks. */
\r
138 #define mainCOM_TEST_LED ( 200 )
\r
140 /* We want the Cygnal to act as much as possible as a standard 8052. */
\r
141 #define mainAUTO_SFR_OFF ( ( unsigned portCHAR ) 0 )
\r
143 /* Constants required to setup the IO pins for serial comms. */
\r
144 #define mainENABLE_COMS ( ( unsigned portCHAR ) 0x04 )
\r
145 #define mainCOMS_LINES_TO_PUSH_PULL ( ( unsigned portCHAR ) 0x03 )
\r
147 /* Pointer passed as a parameter to vRegisterCheck() just so it has some know
\r
148 values to check for in the DPH, DPL and B registers. */
\r
149 #define mainDUMMY_POINTER ( ( xdata void * ) 0xabcd )
\r
151 /* Macro that lets vErrorChecks() know that one of the tasks defined in
\r
152 main. c has detected an error. A critical region is used around xLatchError
\r
153 as it is accessed from vErrorChecks(), which has a higher priority. */
\r
154 #define mainLATCH_ERROR() \
\r
156 portENTER_CRITICAL(); \
\r
157 xLatchedError = pdTRUE; \
\r
158 portEXIT_CRITICAL(); \
\r
162 * Setup the Cygnal microcontroller for its fastest operation.
\r
164 static void prvSetupSystemClock( void );
\r
167 * Setup the peripherals, including the on board LED.
\r
169 static void prvSetupHardware( void );
\r
172 * Toggle the state of the on board LED.
\r
174 static void prvToggleOnBoardLED( void );
\r
177 * See comments at the top of the file for details.
\r
179 static void vErrorChecks( void *pvParameters );
\r
182 * See comments at the top of the file for details.
\r
184 static void vRegisterCheck( void *pvParameters );
\r
187 * See comments at the top of the file for details.
\r
189 static void vFLOPCheck1( void *pvParameters );
\r
192 * See comments at the top of the file for details.
\r
194 static void vFLOPCheck2( void *pvParameters );
\r
196 /* File scope variable used to communicate the occurrence of an error between
\r
198 static portBASE_TYPE xLatchedError = pdFALSE;
\r
200 /*-----------------------------------------------------------*/
\r
203 * Starts all the other tasks, then starts the scheduler.
\r
207 /* Initialise the hardware including the system clock and on board
\r
209 prvSetupHardware();
\r
211 /* Initialise the port that controls the external LED's utilized by the
\r
213 vParTestInitialise();
\r
215 /* Start the used standard demo tasks. */
\r
216 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
217 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
218 vStartIntegerMathTasks( mainINTEGER_PRIORITY );
\r
219 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
220 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
222 /* Start the tasks defined in this file. The first three never block so
\r
223 must not be used with the co-operative scheduler. */
\r
224 #if configUSE_PREEMPTION == 1
\r
226 xTaskCreate( vRegisterCheck, "RegChck", configMINIMAL_STACK_SIZE, mainDUMMY_POINTER, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
227 xTaskCreate( vFLOPCheck1, "FLOP", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
228 xTaskCreate( vFLOPCheck2, "FLOP", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
232 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, ( xTaskHandle * ) NULL );
\r
234 /* Finally kick off the scheduler. This function should never return. */
\r
235 vTaskStartScheduler();
\r
237 /* Should never reach here as the tasks will now be executing under control
\r
238 of the scheduler. */
\r
240 /*-----------------------------------------------------------*/
\r
243 * Setup the hardware prior to using the scheduler. Most of the Cygnal
\r
244 * specific initialisation is performed here leaving standard 8052 setup
\r
245 * only in the driver code.
\r
247 static void prvSetupHardware( void )
\r
249 unsigned portCHAR ucOriginalSFRPage;
\r
251 /* Remember the SFR page before it is changed so it can get set back
\r
252 before the function exits. */
\r
253 ucOriginalSFRPage = SFRPAGE;
\r
255 /* Setup the SFR page to access the config SFR's. */
\r
256 SFRPAGE = CONFIG_PAGE;
\r
258 /* Don't allow the microcontroller to automatically switch SFR page, as the
\r
259 SFR page is not stored as part of the task context. */
\r
260 SFRPGCN = mainAUTO_SFR_OFF;
\r
262 /* Disable the watchdog. */
\r
263 WDTCN = mainDISABLE_BYTE_1;
\r
264 WDTCN = mainDISABLE_BYTE_2;
\r
266 /* Set the on board LED to push pull. */
\r
267 P1MDOUT |= mainPORT_1_BIT_6;
\r
269 /* Setup the cross bar to enable serial comms here as it is not part of the
\r
270 standard 8051 setup and therefore is not in the driver code. */
\r
271 XBR0 |= mainENABLE_COMS;
\r
272 P0MDOUT |= mainCOMS_LINES_TO_PUSH_PULL;
\r
274 /* Enable the cross bar so our hardware setup takes effect. */
\r
275 XBR2 = mainENABLE_CROSS_BAR;
\r
277 /* Setup a fast system clock. */
\r
278 prvSetupSystemClock();
\r
280 /* Return the SFR page. */
\r
281 SFRPAGE = ucOriginalSFRPage;
\r
283 /*-----------------------------------------------------------*/
\r
285 static void prvSetupSystemClock( void )
\r
287 volatile unsigned portSHORT usWait;
\r
288 const unsigned portSHORT usWaitTime = ( unsigned portSHORT ) 0x2ff;
\r
289 unsigned portCHAR ucOriginalSFRPage;
\r
291 /* Remember the SFR page so we can set it back at the end. */
\r
292 ucOriginalSFRPage = SFRPAGE;
\r
293 SFRPAGE = CONFIG_PAGE;
\r
295 /* Use the internal oscillator set to its fasted frequency. */
\r
296 OSCICN = mainSELECT_INTERNAL_OSC | mainDIVIDE_CLOCK_BY_1;
\r
298 /* Ensure the clock is stable. */
\r
299 for( usWait = 0; usWait < usWaitTime; usWait++ );
\r
301 /* Setup the clock source for the PLL. */
\r
302 PLL0CN &= ~mainPLL_USES_INTERNAL_OSC;
\r
304 /* Change the read timing for the flash ready for the fast clock. */
\r
305 SFRPAGE = LEGACY_PAGE;
\r
306 FLSCL |= mainFLASH_READ_TIMING;
\r
308 /* Turn on the PLL power. */
\r
309 SFRPAGE = CONFIG_PAGE;
\r
310 PLL0CN |= mainPLL_POWER_ON;
\r
312 /* Don't predivide the clock. */
\r
313 PLL0DIV = mainPLL_NO_PREDIVIDE;
\r
315 /* Set filter for fastest clock. */
\r
316 PLL0FLT = mainPLL_FILTER;
\r
317 PLL0MUL = mainPLL_MULTIPLICATION;
\r
319 /* Ensure the clock is stable. */
\r
320 for( usWait = 0; usWait < usWaitTime; usWait++ );
\r
322 /* Enable the PLL and wait for it to lock. */
\r
323 PLL0CN |= mainENABLE_PLL;
\r
324 for( usWait = 0; usWait < usWaitTime; usWait++ )
\r
326 if( PLL0CN & mainPLL_LOCKED )
\r
332 /* Select the PLL as the clock source. */
\r
333 CLKSEL |= mainSELECT_PLL_AS_SOURCE;
\r
335 /* Return the SFR back to its original value. */
\r
336 SFRPAGE = ucOriginalSFRPage;
\r
338 /*-----------------------------------------------------------*/
\r
340 static void prvToggleOnBoardLED( void )
\r
342 /* If the on board LED is on, turn it off and visa versa. */
\r
343 if( P1 & ucLED_BIT )
\r
352 /*-----------------------------------------------------------*/
\r
355 * See the documentation at the top of this file.
\r
357 static void vErrorChecks( void *pvParameters )
\r
359 portBASE_TYPE xErrorHasOccurred = pdFALSE;
\r
361 /* Just to prevent compiler warnings. */
\r
362 ( void ) pvParameters;
\r
364 /* Cycle for ever, delaying then checking all the other tasks are still
\r
365 operating without error. The delay period depends on whether an error
\r
366 has ever been detected. */
\r
369 if( xLatchedError == pdFALSE )
\r
371 /* No errors have been detected so delay for a longer period. The
\r
372 on board LED will get toggled every mainNO_ERROR_FLASH_PERIOD ms. */
\r
373 vTaskDelay( mainNO_ERROR_FLASH_PERIOD );
\r
377 /* We have at some time recognised an error in one of the demo
\r
378 application tasks, delay for a shorter period. The on board LED
\r
379 will get toggled every mainERROR_FLASH_PERIOD ms. */
\r
380 vTaskDelay( mainERROR_FLASH_PERIOD );
\r
385 /* Check the demo application tasks for errors. */
\r
387 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
389 xErrorHasOccurred = pdTRUE;
\r
392 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
394 xErrorHasOccurred = pdTRUE;
\r
397 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
399 xErrorHasOccurred = pdTRUE;
\r
402 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
404 xErrorHasOccurred = pdTRUE;
\r
407 /* If an error has occurred, latch it to cause the LED flash rate to
\r
409 if( xErrorHasOccurred == pdTRUE )
\r
411 xLatchedError = pdTRUE;
\r
414 /* Toggle the LED to indicate the completion of a check cycle. The
\r
415 frequency of check cycles is dependent on whether or not we have
\r
416 latched an error. */
\r
417 prvToggleOnBoardLED();
\r
420 /*-----------------------------------------------------------*/
\r
423 * See the documentation at the top of this file. Also see the standard FLOP
\r
424 * demo task documentation for the rationale of these tasks.
\r
426 static void vFLOPCheck1( void *pvParameters )
\r
428 volatile portFLOAT fVal1, fVal2, fResult;
\r
430 ( void ) pvParameters;
\r
434 fVal1 = ( portFLOAT ) -1234.5678;
\r
435 fVal2 = ( portFLOAT ) 2345.6789;
\r
437 fResult = fVal1 + fVal2;
\r
438 if( ( fResult > ( portFLOAT ) 1111.15 ) || ( fResult < ( portFLOAT ) 1111.05 ) )
\r
443 fResult = fVal1 / fVal2;
\r
444 if( ( fResult > ( portFLOAT ) -0.51 ) || ( fResult < ( portFLOAT ) -0.53 ) )
\r
450 /*-----------------------------------------------------------*/
\r
453 * See the documentation at the top of this file.
\r
455 static void vFLOPCheck2( void *pvParameters )
\r
457 volatile portFLOAT fVal1, fVal2, fResult;
\r
459 ( void ) pvParameters;
\r
463 fVal1 = ( portFLOAT ) -12340.5678;
\r
464 fVal2 = ( portFLOAT ) 23450.6789;
\r
466 fResult = fVal1 + fVal2;
\r
467 if( ( fResult > ( portFLOAT ) 11110.15 ) || ( fResult < ( portFLOAT ) 11110.05 ) )
\r
472 fResult = fVal1 / -fVal2;
\r
473 if( ( fResult > ( portFLOAT ) 0.53 ) || ( fResult < ( portFLOAT ) 0.51 ) )
\r
479 /*-----------------------------------------------------------*/
\r
482 * See the documentation at the top of this file.
\r
484 static void vRegisterCheck( void *pvParameters )
\r
486 ( void ) pvParameters;
\r
490 if( SP != configSTACK_START )
\r