2 FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 * Creates the demo application tasks, then starts the scheduler. The WEB
\r
67 * documentation provides more details of the demo application tasks.
\r
69 * Main. c also creates four other tasks:
\r
72 * This only executes every few seconds but has the highest priority so is
\r
73 * guaranteed to get processor time. Its main function is to check that all
\r
74 * the standard demo application tasks are still operational and have not
\r
75 * experienced any errors. vErrorChecks() will toggle the on board LED
\r
76 * every mainNO_ERROR_FLASH_PERIOD milliseconds if none of the demo application
\r
77 * tasks have reported an error. Should any task report an error at any time
\r
78 * the rate at which the on board LED is toggled is increased to
\r
79 * mainERROR_FLASH_PERIOD - providing visual feedback that something has gone
\r
82 * 2) vRegisterCheck()
\r
83 * This is a very simple task that checks that all the registers are always
\r
84 * in their expected state. The task only makes use of the A register, so
\r
85 * all the other registers should always contain their initial values.
\r
86 * An incorrect value indicates an error in the context switch mechanism.
\r
87 * The task operates at the idle priority so will be preempted regularly.
\r
88 * Any error will cause the toggle rate of the on board LED to increase to
\r
89 * mainERROR_FLASH_PERIOD milliseconds.
\r
91 * 3 and 4) vFLOPCheck1() and vFLOPCheck2()
\r
92 * These are very basic versions of the standard FLOP tasks. They are good
\r
93 * at detecting errors in the context switch mechanism, and also check that
\r
94 * the floating point libraries are correctly built to be re-enterant. The
\r
95 * stack restrictions of the 8051 prevent the use of the standard FLOP demo
\r
99 /* Standard includes. */
\r
100 #include <stdlib.h>
\r
102 /* Scheduler includes. */
\r
103 #include "FreeRTOS.h"
\r
106 /* Demo application includes. */
\r
107 #include "partest.h"
\r
109 #include "integer.h"
\r
111 #include "comtest2.h"
\r
112 #include "semtest.h"
\r
114 /* Demo task priorities. */
\r
115 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
116 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
117 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
118 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
119 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
120 #define mainINTEGER_PRIORITY tskIDLE_PRIORITY
\r
122 /* Constants required to disable the watchdog. */
\r
123 #define mainDISABLE_BYTE_1 ( ( unsigned char ) 0xde )
\r
124 #define mainDISABLE_BYTE_2 ( ( unsigned char ) 0xad )
\r
126 /* Constants to setup and use the on board LED. */
\r
127 #define ucLED_BIT ( ( unsigned char ) 0x40 )
\r
128 #define mainPORT_1_BIT_6 ( ( unsigned char ) 0x40 )
\r
129 #define mainENABLE_CROSS_BAR ( ( unsigned char ) 0x40 )
\r
131 /* Constants to set the clock frequency. */
\r
132 #define mainSELECT_INTERNAL_OSC ( ( unsigned char ) 0x80 )
\r
133 #define mainDIVIDE_CLOCK_BY_1 ( ( unsigned char ) 0x03 )
\r
134 #define mainPLL_USES_INTERNAL_OSC ( ( unsigned char ) 0x04 )
\r
135 #define mainFLASH_READ_TIMING ( ( unsigned char ) 0x30 )
\r
136 #define mainPLL_POWER_ON ( ( unsigned char ) 0x01 )
\r
137 #define mainPLL_NO_PREDIVIDE ( ( unsigned char ) 0x01 )
\r
138 #define mainPLL_FILTER ( ( unsigned char ) 0x01 )
\r
139 #define mainPLL_MULTIPLICATION ( ( unsigned char ) 0x04 )
\r
140 #define mainENABLE_PLL ( ( unsigned char ) 0x02 )
\r
141 #define mainPLL_LOCKED ( ( unsigned char ) 0x10 )
\r
142 #define mainSELECT_PLL_AS_SOURCE ( ( unsigned char ) 0x02 )
\r
144 /* Toggle rate for the on board LED - which is dependent on whether or not
\r
145 an error has been detected. */
\r
146 #define mainNO_ERROR_FLASH_PERIOD ( ( portTickType ) 5000 )
\r
147 #define mainERROR_FLASH_PERIOD ( ( portTickType ) 250 )
\r
149 /* Baud rate used by the serial port tasks. */
\r
150 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )
\r
152 /* Pass an invalid LED number to the COM test task as we don't want it to flash
\r
153 an LED. There are only 8 LEDs (excluding the on board LED) wired in and these
\r
154 are all used by the flash tasks. */
\r
155 #define mainCOM_TEST_LED ( 200 )
\r
157 /* We want the Cygnal to act as much as possible as a standard 8052. */
\r
158 #define mainAUTO_SFR_OFF ( ( unsigned char ) 0 )
\r
160 /* Constants required to setup the IO pins for serial comms. */
\r
161 #define mainENABLE_COMS ( ( unsigned char ) 0x04 )
\r
162 #define mainCOMS_LINES_TO_PUSH_PULL ( ( unsigned char ) 0x03 )
\r
164 /* Pointer passed as a parameter to vRegisterCheck() just so it has some know
\r
165 values to check for in the DPH, DPL and B registers. */
\r
166 #define mainDUMMY_POINTER ( ( xdata void * ) 0xabcd )
\r
168 /* Macro that lets vErrorChecks() know that one of the tasks defined in
\r
169 main. c has detected an error. A critical region is used around xLatchError
\r
170 as it is accessed from vErrorChecks(), which has a higher priority. */
\r
171 #define mainLATCH_ERROR() \
\r
173 portENTER_CRITICAL(); \
\r
174 xLatchedError = pdTRUE; \
\r
175 portEXIT_CRITICAL(); \
\r
179 * Setup the Cygnal microcontroller for its fastest operation.
\r
181 static void prvSetupSystemClock( void );
\r
184 * Setup the peripherals, including the on board LED.
\r
186 static void prvSetupHardware( void );
\r
189 * Toggle the state of the on board LED.
\r
191 static void prvToggleOnBoardLED( void );
\r
194 * See comments at the top of the file for details.
\r
196 static void vErrorChecks( void *pvParameters );
\r
199 * See comments at the top of the file for details.
\r
201 static void vRegisterCheck( void *pvParameters );
\r
204 * See comments at the top of the file for details.
\r
206 static void vFLOPCheck1( void *pvParameters );
\r
209 * See comments at the top of the file for details.
\r
211 static void vFLOPCheck2( void *pvParameters );
\r
213 /* File scope variable used to communicate the occurrence of an error between
\r
215 static portBASE_TYPE xLatchedError = pdFALSE;
\r
217 /*-----------------------------------------------------------*/
\r
220 * Starts all the other tasks, then starts the scheduler.
\r
224 /* Initialise the hardware including the system clock and on board
\r
226 prvSetupHardware();
\r
228 /* Initialise the port that controls the external LED's utilized by the
\r
230 vParTestInitialise();
\r
232 /* Start the used standard demo tasks. */
\r
233 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
234 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
235 vStartIntegerMathTasks( mainINTEGER_PRIORITY );
\r
236 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
237 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
239 /* Start the tasks defined in this file. The first three never block so
\r
240 must not be used with the co-operative scheduler. */
\r
241 #if configUSE_PREEMPTION == 1
\r
243 xTaskCreate( vRegisterCheck, "RegChck", configMINIMAL_STACK_SIZE, mainDUMMY_POINTER, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
244 xTaskCreate( vFLOPCheck1, "FLOP", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
245 xTaskCreate( vFLOPCheck2, "FLOP", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
249 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, ( xTaskHandle * ) NULL );
\r
251 /* Finally kick off the scheduler. This function should never return. */
\r
252 vTaskStartScheduler();
\r
254 /* Should never reach here as the tasks will now be executing under control
\r
255 of the scheduler. */
\r
257 /*-----------------------------------------------------------*/
\r
260 * Setup the hardware prior to using the scheduler. Most of the Cygnal
\r
261 * specific initialisation is performed here leaving standard 8052 setup
\r
262 * only in the driver code.
\r
264 static void prvSetupHardware( void )
\r
266 unsigned char ucOriginalSFRPage;
\r
268 /* Remember the SFR page before it is changed so it can get set back
\r
269 before the function exits. */
\r
270 ucOriginalSFRPage = SFRPAGE;
\r
272 /* Setup the SFR page to access the config SFR's. */
\r
273 SFRPAGE = CONFIG_PAGE;
\r
275 /* Don't allow the microcontroller to automatically switch SFR page, as the
\r
276 SFR page is not stored as part of the task context. */
\r
277 SFRPGCN = mainAUTO_SFR_OFF;
\r
279 /* Disable the watchdog. */
\r
280 WDTCN = mainDISABLE_BYTE_1;
\r
281 WDTCN = mainDISABLE_BYTE_2;
\r
283 /* Set the on board LED to push pull. */
\r
284 P1MDOUT |= mainPORT_1_BIT_6;
\r
286 /* Setup the cross bar to enable serial comms here as it is not part of the
\r
287 standard 8051 setup and therefore is not in the driver code. */
\r
288 XBR0 |= mainENABLE_COMS;
\r
289 P0MDOUT |= mainCOMS_LINES_TO_PUSH_PULL;
\r
291 /* Enable the cross bar so our hardware setup takes effect. */
\r
292 XBR2 = mainENABLE_CROSS_BAR;
\r
294 /* Setup a fast system clock. */
\r
295 prvSetupSystemClock();
\r
297 /* Return the SFR page. */
\r
298 SFRPAGE = ucOriginalSFRPage;
\r
300 /*-----------------------------------------------------------*/
\r
302 static void prvSetupSystemClock( void )
\r
304 volatile unsigned short usWait;
\r
305 const unsigned short usWaitTime = ( unsigned short ) 0x2ff;
\r
306 unsigned char ucOriginalSFRPage;
\r
308 /* Remember the SFR page so we can set it back at the end. */
\r
309 ucOriginalSFRPage = SFRPAGE;
\r
310 SFRPAGE = CONFIG_PAGE;
\r
312 /* Use the internal oscillator set to its fasted frequency. */
\r
313 OSCICN = mainSELECT_INTERNAL_OSC | mainDIVIDE_CLOCK_BY_1;
\r
315 /* Ensure the clock is stable. */
\r
316 for( usWait = 0; usWait < usWaitTime; usWait++ );
\r
318 /* Setup the clock source for the PLL. */
\r
319 PLL0CN &= ~mainPLL_USES_INTERNAL_OSC;
\r
321 /* Change the read timing for the flash ready for the fast clock. */
\r
322 SFRPAGE = LEGACY_PAGE;
\r
323 FLSCL |= mainFLASH_READ_TIMING;
\r
325 /* Turn on the PLL power. */
\r
326 SFRPAGE = CONFIG_PAGE;
\r
327 PLL0CN |= mainPLL_POWER_ON;
\r
329 /* Don't predivide the clock. */
\r
330 PLL0DIV = mainPLL_NO_PREDIVIDE;
\r
332 /* Set filter for fastest clock. */
\r
333 PLL0FLT = mainPLL_FILTER;
\r
334 PLL0MUL = mainPLL_MULTIPLICATION;
\r
336 /* Ensure the clock is stable. */
\r
337 for( usWait = 0; usWait < usWaitTime; usWait++ );
\r
339 /* Enable the PLL and wait for it to lock. */
\r
340 PLL0CN |= mainENABLE_PLL;
\r
341 for( usWait = 0; usWait < usWaitTime; usWait++ )
\r
343 if( PLL0CN & mainPLL_LOCKED )
\r
349 /* Select the PLL as the clock source. */
\r
350 CLKSEL |= mainSELECT_PLL_AS_SOURCE;
\r
352 /* Return the SFR back to its original value. */
\r
353 SFRPAGE = ucOriginalSFRPage;
\r
355 /*-----------------------------------------------------------*/
\r
357 static void prvToggleOnBoardLED( void )
\r
359 /* If the on board LED is on, turn it off and visa versa. */
\r
360 if( P1 & ucLED_BIT )
\r
369 /*-----------------------------------------------------------*/
\r
372 * See the documentation at the top of this file.
\r
374 static void vErrorChecks( void *pvParameters )
\r
376 portBASE_TYPE xErrorHasOccurred = pdFALSE;
\r
378 /* Just to prevent compiler warnings. */
\r
379 ( void ) pvParameters;
\r
381 /* Cycle for ever, delaying then checking all the other tasks are still
\r
382 operating without error. The delay period depends on whether an error
\r
383 has ever been detected. */
\r
386 if( xLatchedError == pdFALSE )
\r
388 /* No errors have been detected so delay for a longer period. The
\r
389 on board LED will get toggled every mainNO_ERROR_FLASH_PERIOD ms. */
\r
390 vTaskDelay( mainNO_ERROR_FLASH_PERIOD );
\r
394 /* We have at some time recognised an error in one of the demo
\r
395 application tasks, delay for a shorter period. The on board LED
\r
396 will get toggled every mainERROR_FLASH_PERIOD ms. */
\r
397 vTaskDelay( mainERROR_FLASH_PERIOD );
\r
402 /* Check the demo application tasks for errors. */
\r
404 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
406 xErrorHasOccurred = pdTRUE;
\r
409 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
411 xErrorHasOccurred = pdTRUE;
\r
414 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
416 xErrorHasOccurred = pdTRUE;
\r
419 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
421 xErrorHasOccurred = pdTRUE;
\r
424 /* If an error has occurred, latch it to cause the LED flash rate to
\r
426 if( xErrorHasOccurred == pdTRUE )
\r
428 xLatchedError = pdTRUE;
\r
431 /* Toggle the LED to indicate the completion of a check cycle. The
\r
432 frequency of check cycles is dependent on whether or not we have
\r
433 latched an error. */
\r
434 prvToggleOnBoardLED();
\r
437 /*-----------------------------------------------------------*/
\r
440 * See the documentation at the top of this file. Also see the standard FLOP
\r
441 * demo task documentation for the rationale of these tasks.
\r
443 static void vFLOPCheck1( void *pvParameters )
\r
445 volatile portFLOAT fVal1, fVal2, fResult;
\r
447 ( void ) pvParameters;
\r
451 fVal1 = ( portFLOAT ) -1234.5678;
\r
452 fVal2 = ( portFLOAT ) 2345.6789;
\r
454 fResult = fVal1 + fVal2;
\r
455 if( ( fResult > ( portFLOAT ) 1111.15 ) || ( fResult < ( portFLOAT ) 1111.05 ) )
\r
460 fResult = fVal1 / fVal2;
\r
461 if( ( fResult > ( portFLOAT ) -0.51 ) || ( fResult < ( portFLOAT ) -0.53 ) )
\r
467 /*-----------------------------------------------------------*/
\r
470 * See the documentation at the top of this file.
\r
472 static void vFLOPCheck2( void *pvParameters )
\r
474 volatile portFLOAT fVal1, fVal2, fResult;
\r
476 ( void ) pvParameters;
\r
480 fVal1 = ( portFLOAT ) -12340.5678;
\r
481 fVal2 = ( portFLOAT ) 23450.6789;
\r
483 fResult = fVal1 + fVal2;
\r
484 if( ( fResult > ( portFLOAT ) 11110.15 ) || ( fResult < ( portFLOAT ) 11110.05 ) )
\r
489 fResult = fVal1 / -fVal2;
\r
490 if( ( fResult > ( portFLOAT ) 0.53 ) || ( fResult < ( portFLOAT ) 0.51 ) )
\r
496 /*-----------------------------------------------------------*/
\r
499 * See the documentation at the top of this file.
\r
501 static void vRegisterCheck( void *pvParameters )
\r
503 ( void ) pvParameters;
\r
507 if( SP != configSTACK_START )
\r