2 FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not itcan be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
76 * Creates the demo application tasks, then starts the scheduler. The WEB
\r
77 * documentation provides more details of the demo application tasks.
\r
79 * Main. c also creates four other tasks:
\r
82 * This only executes every few seconds but has the highest priority so is
\r
83 * guaranteed to get processor time. Its main function is to check that all
\r
84 * the standard demo application tasks are still operational and have not
\r
85 * experienced any errors. vErrorChecks() will toggle the on board LED
\r
86 * every mainNO_ERROR_FLASH_PERIOD milliseconds if none of the demo application
\r
87 * tasks have reported an error. Should any task report an error at any time
\r
88 * the rate at which the on board LED is toggled is increased to
\r
89 * mainERROR_FLASH_PERIOD - providing visual feedback that something has gone
\r
92 * 2) vRegisterCheck()
\r
93 * This is a very simple task that checks that all the registers are always
\r
94 * in their expected state. The task only makes use of the A register, so
\r
95 * all the other registers should always contain their initial values.
\r
96 * An incorrect value indicates an error in the context switch mechanism.
\r
97 * The task operates at the idle priority so will be preempted regularly.
\r
98 * Any error will cause the toggle rate of the on board LED to increase to
\r
99 * mainERROR_FLASH_PERIOD milliseconds.
\r
101 * 3 and 4) vFLOPCheck1() and vFLOPCheck2()
\r
102 * These are very basic versions of the standard FLOP tasks. They are good
\r
103 * at detecting errors in the context switch mechanism, and also check that
\r
104 * the floating point libraries are correctly built to be re-enterant. The
\r
105 * stack restrictions of the 8051 prevent the use of the standard FLOP demo
\r
109 /* Standard includes. */
\r
110 #include <stdlib.h>
\r
112 /* Scheduler includes. */
\r
113 #include "FreeRTOS.h"
\r
116 /* Demo application includes. */
\r
117 #include "partest.h"
\r
119 #include "integer.h"
\r
121 #include "comtest2.h"
\r
122 #include "semtest.h"
\r
124 /* Demo task priorities. */
\r
125 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
126 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
127 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
128 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
129 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
130 #define mainINTEGER_PRIORITY tskIDLE_PRIORITY
\r
132 /* Constants required to disable the watchdog. */
\r
133 #define mainDISABLE_BYTE_1 ( ( unsigned char ) 0xde )
\r
134 #define mainDISABLE_BYTE_2 ( ( unsigned char ) 0xad )
\r
136 /* Constants to setup and use the on board LED. */
\r
137 #define ucLED_BIT ( ( unsigned char ) 0x40 )
\r
138 #define mainPORT_1_BIT_6 ( ( unsigned char ) 0x40 )
\r
139 #define mainENABLE_CROSS_BAR ( ( unsigned char ) 0x40 )
\r
141 /* Constants to set the clock frequency. */
\r
142 #define mainSELECT_INTERNAL_OSC ( ( unsigned char ) 0x80 )
\r
143 #define mainDIVIDE_CLOCK_BY_1 ( ( unsigned char ) 0x03 )
\r
144 #define mainPLL_USES_INTERNAL_OSC ( ( unsigned char ) 0x04 )
\r
145 #define mainFLASH_READ_TIMING ( ( unsigned char ) 0x30 )
\r
146 #define mainPLL_POWER_ON ( ( unsigned char ) 0x01 )
\r
147 #define mainPLL_NO_PREDIVIDE ( ( unsigned char ) 0x01 )
\r
148 #define mainPLL_FILTER ( ( unsigned char ) 0x01 )
\r
149 #define mainPLL_MULTIPLICATION ( ( unsigned char ) 0x04 )
\r
150 #define mainENABLE_PLL ( ( unsigned char ) 0x02 )
\r
151 #define mainPLL_LOCKED ( ( unsigned char ) 0x10 )
\r
152 #define mainSELECT_PLL_AS_SOURCE ( ( unsigned char ) 0x02 )
\r
154 /* Toggle rate for the on board LED - which is dependent on whether or not
\r
155 an error has been detected. */
\r
156 #define mainNO_ERROR_FLASH_PERIOD ( ( portTickType ) 5000 )
\r
157 #define mainERROR_FLASH_PERIOD ( ( portTickType ) 250 )
\r
159 /* Baud rate used by the serial port tasks. */
\r
160 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )
\r
162 /* Pass an invalid LED number to the COM test task as we don't want it to flash
\r
163 an LED. There are only 8 LEDs (excluding the on board LED) wired in and these
\r
164 are all used by the flash tasks. */
\r
165 #define mainCOM_TEST_LED ( 200 )
\r
167 /* We want the Cygnal to act as much as possible as a standard 8052. */
\r
168 #define mainAUTO_SFR_OFF ( ( unsigned char ) 0 )
\r
170 /* Constants required to setup the IO pins for serial comms. */
\r
171 #define mainENABLE_COMS ( ( unsigned char ) 0x04 )
\r
172 #define mainCOMS_LINES_TO_PUSH_PULL ( ( unsigned char ) 0x03 )
\r
174 /* Pointer passed as a parameter to vRegisterCheck() just so it has some know
\r
175 values to check for in the DPH, DPL and B registers. */
\r
176 #define mainDUMMY_POINTER ( ( xdata void * ) 0xabcd )
\r
178 /* Macro that lets vErrorChecks() know that one of the tasks defined in
\r
179 main. c has detected an error. A critical region is used around xLatchError
\r
180 as it is accessed from vErrorChecks(), which has a higher priority. */
\r
181 #define mainLATCH_ERROR() \
\r
183 portENTER_CRITICAL(); \
\r
184 xLatchedError = pdTRUE; \
\r
185 portEXIT_CRITICAL(); \
\r
189 * Setup the Cygnal microcontroller for its fastest operation.
\r
191 static void prvSetupSystemClock( void );
\r
194 * Setup the peripherals, including the on board LED.
\r
196 static void prvSetupHardware( void );
\r
199 * Toggle the state of the on board LED.
\r
201 static void prvToggleOnBoardLED( void );
\r
204 * See comments at the top of the file for details.
\r
206 static void vErrorChecks( void *pvParameters );
\r
209 * See comments at the top of the file for details.
\r
211 static void vRegisterCheck( void *pvParameters );
\r
214 * See comments at the top of the file for details.
\r
216 static void vFLOPCheck1( void *pvParameters );
\r
219 * See comments at the top of the file for details.
\r
221 static void vFLOPCheck2( void *pvParameters );
\r
223 /* File scope variable used to communicate the occurrence of an error between
\r
225 static portBASE_TYPE xLatchedError = pdFALSE;
\r
227 /*-----------------------------------------------------------*/
\r
230 * Starts all the other tasks, then starts the scheduler.
\r
234 /* Initialise the hardware including the system clock and on board
\r
236 prvSetupHardware();
\r
238 /* Initialise the port that controls the external LED's utilized by the
\r
240 vParTestInitialise();
\r
242 /* Start the used standard demo tasks. */
\r
243 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
244 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
245 vStartIntegerMathTasks( mainINTEGER_PRIORITY );
\r
246 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
247 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
249 /* Start the tasks defined in this file. The first three never block so
\r
250 must not be used with the co-operative scheduler. */
\r
251 #if configUSE_PREEMPTION == 1
\r
253 xTaskCreate( vRegisterCheck, "RegChck", configMINIMAL_STACK_SIZE, mainDUMMY_POINTER, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
254 xTaskCreate( vFLOPCheck1, "FLOP", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
255 xTaskCreate( vFLOPCheck2, "FLOP", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
259 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, ( xTaskHandle * ) NULL );
\r
261 /* Finally kick off the scheduler. This function should never return. */
\r
262 vTaskStartScheduler();
\r
264 /* Should never reach here as the tasks will now be executing under control
\r
265 of the scheduler. */
\r
267 /*-----------------------------------------------------------*/
\r
270 * Setup the hardware prior to using the scheduler. Most of the Cygnal
\r
271 * specific initialisation is performed here leaving standard 8052 setup
\r
272 * only in the driver code.
\r
274 static void prvSetupHardware( void )
\r
276 unsigned char ucOriginalSFRPage;
\r
278 /* Remember the SFR page before it is changed so it can get set back
\r
279 before the function exits. */
\r
280 ucOriginalSFRPage = SFRPAGE;
\r
282 /* Setup the SFR page to access the config SFR's. */
\r
283 SFRPAGE = CONFIG_PAGE;
\r
285 /* Don't allow the microcontroller to automatically switch SFR page, as the
\r
286 SFR page is not stored as part of the task context. */
\r
287 SFRPGCN = mainAUTO_SFR_OFF;
\r
289 /* Disable the watchdog. */
\r
290 WDTCN = mainDISABLE_BYTE_1;
\r
291 WDTCN = mainDISABLE_BYTE_2;
\r
293 /* Set the on board LED to push pull. */
\r
294 P1MDOUT |= mainPORT_1_BIT_6;
\r
296 /* Setup the cross bar to enable serial comms here as it is not part of the
\r
297 standard 8051 setup and therefore is not in the driver code. */
\r
298 XBR0 |= mainENABLE_COMS;
\r
299 P0MDOUT |= mainCOMS_LINES_TO_PUSH_PULL;
\r
301 /* Enable the cross bar so our hardware setup takes effect. */
\r
302 XBR2 = mainENABLE_CROSS_BAR;
\r
304 /* Setup a fast system clock. */
\r
305 prvSetupSystemClock();
\r
307 /* Return the SFR page. */
\r
308 SFRPAGE = ucOriginalSFRPage;
\r
310 /*-----------------------------------------------------------*/
\r
312 static void prvSetupSystemClock( void )
\r
314 volatile unsigned short usWait;
\r
315 const unsigned short usWaitTime = ( unsigned short ) 0x2ff;
\r
316 unsigned char ucOriginalSFRPage;
\r
318 /* Remember the SFR page so we can set it back at the end. */
\r
319 ucOriginalSFRPage = SFRPAGE;
\r
320 SFRPAGE = CONFIG_PAGE;
\r
322 /* Use the internal oscillator set to its fasted frequency. */
\r
323 OSCICN = mainSELECT_INTERNAL_OSC | mainDIVIDE_CLOCK_BY_1;
\r
325 /* Ensure the clock is stable. */
\r
326 for( usWait = 0; usWait < usWaitTime; usWait++ );
\r
328 /* Setup the clock source for the PLL. */
\r
329 PLL0CN &= ~mainPLL_USES_INTERNAL_OSC;
\r
331 /* Change the read timing for the flash ready for the fast clock. */
\r
332 SFRPAGE = LEGACY_PAGE;
\r
333 FLSCL |= mainFLASH_READ_TIMING;
\r
335 /* Turn on the PLL power. */
\r
336 SFRPAGE = CONFIG_PAGE;
\r
337 PLL0CN |= mainPLL_POWER_ON;
\r
339 /* Don't predivide the clock. */
\r
340 PLL0DIV = mainPLL_NO_PREDIVIDE;
\r
342 /* Set filter for fastest clock. */
\r
343 PLL0FLT = mainPLL_FILTER;
\r
344 PLL0MUL = mainPLL_MULTIPLICATION;
\r
346 /* Ensure the clock is stable. */
\r
347 for( usWait = 0; usWait < usWaitTime; usWait++ );
\r
349 /* Enable the PLL and wait for it to lock. */
\r
350 PLL0CN |= mainENABLE_PLL;
\r
351 for( usWait = 0; usWait < usWaitTime; usWait++ )
\r
353 if( PLL0CN & mainPLL_LOCKED )
\r
359 /* Select the PLL as the clock source. */
\r
360 CLKSEL |= mainSELECT_PLL_AS_SOURCE;
\r
362 /* Return the SFR back to its original value. */
\r
363 SFRPAGE = ucOriginalSFRPage;
\r
365 /*-----------------------------------------------------------*/
\r
367 static void prvToggleOnBoardLED( void )
\r
369 /* If the on board LED is on, turn it off and visa versa. */
\r
370 if( P1 & ucLED_BIT )
\r
379 /*-----------------------------------------------------------*/
\r
382 * See the documentation at the top of this file.
\r
384 static void vErrorChecks( void *pvParameters )
\r
386 portBASE_TYPE xErrorHasOccurred = pdFALSE;
\r
388 /* Just to prevent compiler warnings. */
\r
389 ( void ) pvParameters;
\r
391 /* Cycle for ever, delaying then checking all the other tasks are still
\r
392 operating without error. The delay period depends on whether an error
\r
393 has ever been detected. */
\r
396 if( xLatchedError == pdFALSE )
\r
398 /* No errors have been detected so delay for a longer period. The
\r
399 on board LED will get toggled every mainNO_ERROR_FLASH_PERIOD ms. */
\r
400 vTaskDelay( mainNO_ERROR_FLASH_PERIOD );
\r
404 /* We have at some time recognised an error in one of the demo
\r
405 application tasks, delay for a shorter period. The on board LED
\r
406 will get toggled every mainERROR_FLASH_PERIOD ms. */
\r
407 vTaskDelay( mainERROR_FLASH_PERIOD );
\r
412 /* Check the demo application tasks for errors. */
\r
414 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
416 xErrorHasOccurred = pdTRUE;
\r
419 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
421 xErrorHasOccurred = pdTRUE;
\r
424 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
426 xErrorHasOccurred = pdTRUE;
\r
429 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
431 xErrorHasOccurred = pdTRUE;
\r
434 /* If an error has occurred, latch it to cause the LED flash rate to
\r
436 if( xErrorHasOccurred == pdTRUE )
\r
438 xLatchedError = pdTRUE;
\r
441 /* Toggle the LED to indicate the completion of a check cycle. The
\r
442 frequency of check cycles is dependent on whether or not we have
\r
443 latched an error. */
\r
444 prvToggleOnBoardLED();
\r
447 /*-----------------------------------------------------------*/
\r
450 * See the documentation at the top of this file. Also see the standard FLOP
\r
451 * demo task documentation for the rationale of these tasks.
\r
453 static void vFLOPCheck1( void *pvParameters )
\r
455 volatile portFLOAT fVal1, fVal2, fResult;
\r
457 ( void ) pvParameters;
\r
461 fVal1 = ( portFLOAT ) -1234.5678;
\r
462 fVal2 = ( portFLOAT ) 2345.6789;
\r
464 fResult = fVal1 + fVal2;
\r
465 if( ( fResult > ( portFLOAT ) 1111.15 ) || ( fResult < ( portFLOAT ) 1111.05 ) )
\r
470 fResult = fVal1 / fVal2;
\r
471 if( ( fResult > ( portFLOAT ) -0.51 ) || ( fResult < ( portFLOAT ) -0.53 ) )
\r
477 /*-----------------------------------------------------------*/
\r
480 * See the documentation at the top of this file.
\r
482 static void vFLOPCheck2( void *pvParameters )
\r
484 volatile portFLOAT fVal1, fVal2, fResult;
\r
486 ( void ) pvParameters;
\r
490 fVal1 = ( portFLOAT ) -12340.5678;
\r
491 fVal2 = ( portFLOAT ) 23450.6789;
\r
493 fResult = fVal1 + fVal2;
\r
494 if( ( fResult > ( portFLOAT ) 11110.15 ) || ( fResult < ( portFLOAT ) 11110.05 ) )
\r
499 fResult = fVal1 / -fVal2;
\r
500 if( ( fResult > ( portFLOAT ) 0.53 ) || ( fResult < ( portFLOAT ) 0.51 ) )
\r
506 /*-----------------------------------------------------------*/
\r
509 * See the documentation at the top of this file.
\r
511 static void vRegisterCheck( void *pvParameters )
\r
513 ( void ) pvParameters;
\r
517 if( SP != configSTACK_START )
\r