2 FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS books - available as PDF or paperback *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 * Creates the demo application tasks, then starts the scheduler. The WEB
\r
56 * documentation provides more details of the demo application tasks.
\r
58 * Main. c also creates four other tasks:
\r
61 * This only executes every few seconds but has the highest priority so is
\r
62 * guaranteed to get processor time. Its main function is to check that all
\r
63 * the standard demo application tasks are still operational and have not
\r
64 * experienced any errors. vErrorChecks() will toggle the on board LED
\r
65 * every mainNO_ERROR_FLASH_PERIOD milliseconds if none of the demo application
\r
66 * tasks have reported an error. Should any task report an error at any time
\r
67 * the rate at which the on board LED is toggled is increased to
\r
68 * mainERROR_FLASH_PERIOD - providing visual feedback that something has gone
\r
71 * 2) vRegisterCheck()
\r
72 * This is a very simple task that checks that all the registers are always
\r
73 * in their expected state. The task only makes use of the A register, so
\r
74 * all the other registers should always contain their initial values.
\r
75 * An incorrect value indicates an error in the context switch mechanism.
\r
76 * The task operates at the idle priority so will be preempted regularly.
\r
77 * Any error will cause the toggle rate of the on board LED to increase to
\r
78 * mainERROR_FLASH_PERIOD milliseconds.
\r
80 * 3 and 4) vFLOPCheck1() and vFLOPCheck2()
\r
81 * These are very basic versions of the standard FLOP tasks. They are good
\r
82 * at detecting errors in the context switch mechanism, and also check that
\r
83 * the floating point libraries are correctly built to be re-enterant. The
\r
84 * stack restrictions of the 8051 prevent the use of the standard FLOP demo
\r
88 /* Standard includes. */
\r
91 /* Scheduler includes. */
\r
92 #include "FreeRTOS.h"
\r
95 /* Demo application includes. */
\r
96 #include "partest.h"
\r
98 #include "integer.h"
\r
100 #include "comtest2.h"
\r
101 #include "semtest.h"
\r
103 /* Demo task priorities. */
\r
104 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
105 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
106 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
107 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
108 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
109 #define mainINTEGER_PRIORITY tskIDLE_PRIORITY
\r
111 /* Constants required to disable the watchdog. */
\r
112 #define mainDISABLE_BYTE_1 ( ( unsigned char ) 0xde )
\r
113 #define mainDISABLE_BYTE_2 ( ( unsigned char ) 0xad )
\r
115 /* Constants to setup and use the on board LED. */
\r
116 #define ucLED_BIT ( ( unsigned char ) 0x40 )
\r
117 #define mainPORT_1_BIT_6 ( ( unsigned char ) 0x40 )
\r
118 #define mainENABLE_CROSS_BAR ( ( unsigned char ) 0x40 )
\r
120 /* Constants to set the clock frequency. */
\r
121 #define mainSELECT_INTERNAL_OSC ( ( unsigned char ) 0x80 )
\r
122 #define mainDIVIDE_CLOCK_BY_1 ( ( unsigned char ) 0x03 )
\r
123 #define mainPLL_USES_INTERNAL_OSC ( ( unsigned char ) 0x04 )
\r
124 #define mainFLASH_READ_TIMING ( ( unsigned char ) 0x30 )
\r
125 #define mainPLL_POWER_ON ( ( unsigned char ) 0x01 )
\r
126 #define mainPLL_NO_PREDIVIDE ( ( unsigned char ) 0x01 )
\r
127 #define mainPLL_FILTER ( ( unsigned char ) 0x01 )
\r
128 #define mainPLL_MULTIPLICATION ( ( unsigned char ) 0x04 )
\r
129 #define mainENABLE_PLL ( ( unsigned char ) 0x02 )
\r
130 #define mainPLL_LOCKED ( ( unsigned char ) 0x10 )
\r
131 #define mainSELECT_PLL_AS_SOURCE ( ( unsigned char ) 0x02 )
\r
133 /* Toggle rate for the on board LED - which is dependent on whether or not
\r
134 an error has been detected. */
\r
135 #define mainNO_ERROR_FLASH_PERIOD ( ( portTickType ) 5000 )
\r
136 #define mainERROR_FLASH_PERIOD ( ( portTickType ) 250 )
\r
138 /* Baud rate used by the serial port tasks. */
\r
139 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )
\r
141 /* Pass an invalid LED number to the COM test task as we don't want it to flash
\r
142 an LED. There are only 8 LEDs (excluding the on board LED) wired in and these
\r
143 are all used by the flash tasks. */
\r
144 #define mainCOM_TEST_LED ( 200 )
\r
146 /* We want the Cygnal to act as much as possible as a standard 8052. */
\r
147 #define mainAUTO_SFR_OFF ( ( unsigned char ) 0 )
\r
149 /* Constants required to setup the IO pins for serial comms. */
\r
150 #define mainENABLE_COMS ( ( unsigned char ) 0x04 )
\r
151 #define mainCOMS_LINES_TO_PUSH_PULL ( ( unsigned char ) 0x03 )
\r
153 /* Pointer passed as a parameter to vRegisterCheck() just so it has some know
\r
154 values to check for in the DPH, DPL and B registers. */
\r
155 #define mainDUMMY_POINTER ( ( xdata void * ) 0xabcd )
\r
157 /* Macro that lets vErrorChecks() know that one of the tasks defined in
\r
158 main. c has detected an error. A critical region is used around xLatchError
\r
159 as it is accessed from vErrorChecks(), which has a higher priority. */
\r
160 #define mainLATCH_ERROR() \
\r
162 portENTER_CRITICAL(); \
\r
163 xLatchedError = pdTRUE; \
\r
164 portEXIT_CRITICAL(); \
\r
168 * Setup the Cygnal microcontroller for its fastest operation.
\r
170 static void prvSetupSystemClock( void );
\r
173 * Setup the peripherals, including the on board LED.
\r
175 static void prvSetupHardware( void );
\r
178 * Toggle the state of the on board LED.
\r
180 static void prvToggleOnBoardLED( void );
\r
183 * See comments at the top of the file for details.
\r
185 static void vErrorChecks( void *pvParameters );
\r
188 * See comments at the top of the file for details.
\r
190 static void vRegisterCheck( void *pvParameters );
\r
193 * See comments at the top of the file for details.
\r
195 static void vFLOPCheck1( void *pvParameters );
\r
198 * See comments at the top of the file for details.
\r
200 static void vFLOPCheck2( void *pvParameters );
\r
202 /* File scope variable used to communicate the occurrence of an error between
\r
204 static portBASE_TYPE xLatchedError = pdFALSE;
\r
206 /*-----------------------------------------------------------*/
\r
209 * Starts all the other tasks, then starts the scheduler.
\r
213 /* Initialise the hardware including the system clock and on board
\r
215 prvSetupHardware();
\r
217 /* Initialise the port that controls the external LED's utilized by the
\r
219 vParTestInitialise();
\r
221 /* Start the used standard demo tasks. */
\r
222 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
223 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
224 vStartIntegerMathTasks( mainINTEGER_PRIORITY );
\r
225 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
226 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
228 /* Start the tasks defined in this file. The first three never block so
\r
229 must not be used with the co-operative scheduler. */
\r
230 #if configUSE_PREEMPTION == 1
\r
232 xTaskCreate( vRegisterCheck, "RegChck", configMINIMAL_STACK_SIZE, mainDUMMY_POINTER, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
233 xTaskCreate( vFLOPCheck1, "FLOP", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
234 xTaskCreate( vFLOPCheck2, "FLOP", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
238 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, ( xTaskHandle * ) NULL );
\r
240 /* Finally kick off the scheduler. This function should never return. */
\r
241 vTaskStartScheduler();
\r
243 /* Should never reach here as the tasks will now be executing under control
\r
244 of the scheduler. */
\r
246 /*-----------------------------------------------------------*/
\r
249 * Setup the hardware prior to using the scheduler. Most of the Cygnal
\r
250 * specific initialisation is performed here leaving standard 8052 setup
\r
251 * only in the driver code.
\r
253 static void prvSetupHardware( void )
\r
255 unsigned char ucOriginalSFRPage;
\r
257 /* Remember the SFR page before it is changed so it can get set back
\r
258 before the function exits. */
\r
259 ucOriginalSFRPage = SFRPAGE;
\r
261 /* Setup the SFR page to access the config SFR's. */
\r
262 SFRPAGE = CONFIG_PAGE;
\r
264 /* Don't allow the microcontroller to automatically switch SFR page, as the
\r
265 SFR page is not stored as part of the task context. */
\r
266 SFRPGCN = mainAUTO_SFR_OFF;
\r
268 /* Disable the watchdog. */
\r
269 WDTCN = mainDISABLE_BYTE_1;
\r
270 WDTCN = mainDISABLE_BYTE_2;
\r
272 /* Set the on board LED to push pull. */
\r
273 P1MDOUT |= mainPORT_1_BIT_6;
\r
275 /* Setup the cross bar to enable serial comms here as it is not part of the
\r
276 standard 8051 setup and therefore is not in the driver code. */
\r
277 XBR0 |= mainENABLE_COMS;
\r
278 P0MDOUT |= mainCOMS_LINES_TO_PUSH_PULL;
\r
280 /* Enable the cross bar so our hardware setup takes effect. */
\r
281 XBR2 = mainENABLE_CROSS_BAR;
\r
283 /* Setup a fast system clock. */
\r
284 prvSetupSystemClock();
\r
286 /* Return the SFR page. */
\r
287 SFRPAGE = ucOriginalSFRPage;
\r
289 /*-----------------------------------------------------------*/
\r
291 static void prvSetupSystemClock( void )
\r
293 volatile unsigned short usWait;
\r
294 const unsigned short usWaitTime = ( unsigned short ) 0x2ff;
\r
295 unsigned char ucOriginalSFRPage;
\r
297 /* Remember the SFR page so we can set it back at the end. */
\r
298 ucOriginalSFRPage = SFRPAGE;
\r
299 SFRPAGE = CONFIG_PAGE;
\r
301 /* Use the internal oscillator set to its fasted frequency. */
\r
302 OSCICN = mainSELECT_INTERNAL_OSC | mainDIVIDE_CLOCK_BY_1;
\r
304 /* Ensure the clock is stable. */
\r
305 for( usWait = 0; usWait < usWaitTime; usWait++ );
\r
307 /* Setup the clock source for the PLL. */
\r
308 PLL0CN &= ~mainPLL_USES_INTERNAL_OSC;
\r
310 /* Change the read timing for the flash ready for the fast clock. */
\r
311 SFRPAGE = LEGACY_PAGE;
\r
312 FLSCL |= mainFLASH_READ_TIMING;
\r
314 /* Turn on the PLL power. */
\r
315 SFRPAGE = CONFIG_PAGE;
\r
316 PLL0CN |= mainPLL_POWER_ON;
\r
318 /* Don't predivide the clock. */
\r
319 PLL0DIV = mainPLL_NO_PREDIVIDE;
\r
321 /* Set filter for fastest clock. */
\r
322 PLL0FLT = mainPLL_FILTER;
\r
323 PLL0MUL = mainPLL_MULTIPLICATION;
\r
325 /* Ensure the clock is stable. */
\r
326 for( usWait = 0; usWait < usWaitTime; usWait++ );
\r
328 /* Enable the PLL and wait for it to lock. */
\r
329 PLL0CN |= mainENABLE_PLL;
\r
330 for( usWait = 0; usWait < usWaitTime; usWait++ )
\r
332 if( PLL0CN & mainPLL_LOCKED )
\r
338 /* Select the PLL as the clock source. */
\r
339 CLKSEL |= mainSELECT_PLL_AS_SOURCE;
\r
341 /* Return the SFR back to its original value. */
\r
342 SFRPAGE = ucOriginalSFRPage;
\r
344 /*-----------------------------------------------------------*/
\r
346 static void prvToggleOnBoardLED( void )
\r
348 /* If the on board LED is on, turn it off and visa versa. */
\r
349 if( P1 & ucLED_BIT )
\r
358 /*-----------------------------------------------------------*/
\r
361 * See the documentation at the top of this file.
\r
363 static void vErrorChecks( void *pvParameters )
\r
365 portBASE_TYPE xErrorHasOccurred = pdFALSE;
\r
367 /* Just to prevent compiler warnings. */
\r
368 ( void ) pvParameters;
\r
370 /* Cycle for ever, delaying then checking all the other tasks are still
\r
371 operating without error. The delay period depends on whether an error
\r
372 has ever been detected. */
\r
375 if( xLatchedError == pdFALSE )
\r
377 /* No errors have been detected so delay for a longer period. The
\r
378 on board LED will get toggled every mainNO_ERROR_FLASH_PERIOD ms. */
\r
379 vTaskDelay( mainNO_ERROR_FLASH_PERIOD );
\r
383 /* We have at some time recognised an error in one of the demo
\r
384 application tasks, delay for a shorter period. The on board LED
\r
385 will get toggled every mainERROR_FLASH_PERIOD ms. */
\r
386 vTaskDelay( mainERROR_FLASH_PERIOD );
\r
391 /* Check the demo application tasks for errors. */
\r
393 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
395 xErrorHasOccurred = pdTRUE;
\r
398 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
400 xErrorHasOccurred = pdTRUE;
\r
403 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
405 xErrorHasOccurred = pdTRUE;
\r
408 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
410 xErrorHasOccurred = pdTRUE;
\r
413 /* If an error has occurred, latch it to cause the LED flash rate to
\r
415 if( xErrorHasOccurred == pdTRUE )
\r
417 xLatchedError = pdTRUE;
\r
420 /* Toggle the LED to indicate the completion of a check cycle. The
\r
421 frequency of check cycles is dependent on whether or not we have
\r
422 latched an error. */
\r
423 prvToggleOnBoardLED();
\r
426 /*-----------------------------------------------------------*/
\r
429 * See the documentation at the top of this file. Also see the standard FLOP
\r
430 * demo task documentation for the rationale of these tasks.
\r
432 static void vFLOPCheck1( void *pvParameters )
\r
434 volatile portFLOAT fVal1, fVal2, fResult;
\r
436 ( void ) pvParameters;
\r
440 fVal1 = ( portFLOAT ) -1234.5678;
\r
441 fVal2 = ( portFLOAT ) 2345.6789;
\r
443 fResult = fVal1 + fVal2;
\r
444 if( ( fResult > ( portFLOAT ) 1111.15 ) || ( fResult < ( portFLOAT ) 1111.05 ) )
\r
449 fResult = fVal1 / fVal2;
\r
450 if( ( fResult > ( portFLOAT ) -0.51 ) || ( fResult < ( portFLOAT ) -0.53 ) )
\r
456 /*-----------------------------------------------------------*/
\r
459 * See the documentation at the top of this file.
\r
461 static void vFLOPCheck2( void *pvParameters )
\r
463 volatile portFLOAT fVal1, fVal2, fResult;
\r
465 ( void ) pvParameters;
\r
469 fVal1 = ( portFLOAT ) -12340.5678;
\r
470 fVal2 = ( portFLOAT ) 23450.6789;
\r
472 fResult = fVal1 + fVal2;
\r
473 if( ( fResult > ( portFLOAT ) 11110.15 ) || ( fResult < ( portFLOAT ) 11110.05 ) )
\r
478 fResult = fVal1 / -fVal2;
\r
479 if( ( fResult > ( portFLOAT ) 0.53 ) || ( fResult < ( portFLOAT ) 0.51 ) )
\r
485 /*-----------------------------------------------------------*/
\r
488 * See the documentation at the top of this file.
\r
490 static void vRegisterCheck( void *pvParameters )
\r
492 ( void ) pvParameters;
\r
496 if( SP != configSTACK_START )
\r