2 FreeRTOS V8.0.0 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 * Creates the demo application tasks, then starts the scheduler. The WEB
\r
68 * documentation provides more details of the demo application tasks.
\r
70 * Main. c also creates four other tasks:
\r
73 * This only executes every few seconds but has the highest priority so is
\r
74 * guaranteed to get processor time. Its main function is to check that all
\r
75 * the standard demo application tasks are still operational and have not
\r
76 * experienced any errors. vErrorChecks() will toggle the on board LED
\r
77 * every mainNO_ERROR_FLASH_PERIOD milliseconds if none of the demo application
\r
78 * tasks have reported an error. Should any task report an error at any time
\r
79 * the rate at which the on board LED is toggled is increased to
\r
80 * mainERROR_FLASH_PERIOD - providing visual feedback that something has gone
\r
83 * 2) vRegisterCheck()
\r
84 * This is a very simple task that checks that all the registers are always
\r
85 * in their expected state. The task only makes use of the A register, so
\r
86 * all the other registers should always contain their initial values.
\r
87 * An incorrect value indicates an error in the context switch mechanism.
\r
88 * The task operates at the idle priority so will be preempted regularly.
\r
89 * Any error will cause the toggle rate of the on board LED to increase to
\r
90 * mainERROR_FLASH_PERIOD milliseconds.
\r
92 * 3 and 4) vFLOPCheck1() and vFLOPCheck2()
\r
93 * These are very basic versions of the standard FLOP tasks. They are good
\r
94 * at detecting errors in the context switch mechanism, and also check that
\r
95 * the floating point libraries are correctly built to be re-enterant. The
\r
96 * stack restrictions of the 8051 prevent the use of the standard FLOP demo
\r
100 /* Standard includes. */
\r
101 #include <stdlib.h>
\r
103 /* Scheduler includes. */
\r
104 #include "FreeRTOS.h"
\r
107 /* Demo application includes. */
\r
108 #include "partest.h"
\r
110 #include "integer.h"
\r
112 #include "comtest2.h"
\r
113 #include "semtest.h"
\r
115 /* Demo task priorities. */
\r
116 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
117 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
118 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
119 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
120 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
121 #define mainINTEGER_PRIORITY tskIDLE_PRIORITY
\r
123 /* Constants required to disable the watchdog. */
\r
124 #define mainDISABLE_BYTE_1 ( ( unsigned char ) 0xde )
\r
125 #define mainDISABLE_BYTE_2 ( ( unsigned char ) 0xad )
\r
127 /* Constants to setup and use the on board LED. */
\r
128 #define ucLED_BIT ( ( unsigned char ) 0x40 )
\r
129 #define mainPORT_1_BIT_6 ( ( unsigned char ) 0x40 )
\r
130 #define mainENABLE_CROSS_BAR ( ( unsigned char ) 0x40 )
\r
132 /* Constants to set the clock frequency. */
\r
133 #define mainSELECT_INTERNAL_OSC ( ( unsigned char ) 0x80 )
\r
134 #define mainDIVIDE_CLOCK_BY_1 ( ( unsigned char ) 0x03 )
\r
135 #define mainPLL_USES_INTERNAL_OSC ( ( unsigned char ) 0x04 )
\r
136 #define mainFLASH_READ_TIMING ( ( unsigned char ) 0x30 )
\r
137 #define mainPLL_POWER_ON ( ( unsigned char ) 0x01 )
\r
138 #define mainPLL_NO_PREDIVIDE ( ( unsigned char ) 0x01 )
\r
139 #define mainPLL_FILTER ( ( unsigned char ) 0x01 )
\r
140 #define mainPLL_MULTIPLICATION ( ( unsigned char ) 0x04 )
\r
141 #define mainENABLE_PLL ( ( unsigned char ) 0x02 )
\r
142 #define mainPLL_LOCKED ( ( unsigned char ) 0x10 )
\r
143 #define mainSELECT_PLL_AS_SOURCE ( ( unsigned char ) 0x02 )
\r
145 /* Toggle rate for the on board LED - which is dependent on whether or not
\r
146 an error has been detected. */
\r
147 #define mainNO_ERROR_FLASH_PERIOD ( ( TickType_t ) 5000 )
\r
148 #define mainERROR_FLASH_PERIOD ( ( TickType_t ) 250 )
\r
150 /* Baud rate used by the serial port tasks. */
\r
151 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )
\r
153 /* Pass an invalid LED number to the COM test task as we don't want it to flash
\r
154 an LED. There are only 8 LEDs (excluding the on board LED) wired in and these
\r
155 are all used by the flash tasks. */
\r
156 #define mainCOM_TEST_LED ( 200 )
\r
158 /* We want the Cygnal to act as much as possible as a standard 8052. */
\r
159 #define mainAUTO_SFR_OFF ( ( unsigned char ) 0 )
\r
161 /* Constants required to setup the IO pins for serial comms. */
\r
162 #define mainENABLE_COMS ( ( unsigned char ) 0x04 )
\r
163 #define mainCOMS_LINES_TO_PUSH_PULL ( ( unsigned char ) 0x03 )
\r
165 /* Pointer passed as a parameter to vRegisterCheck() just so it has some know
\r
166 values to check for in the DPH, DPL and B registers. */
\r
167 #define mainDUMMY_POINTER ( ( xdata void * ) 0xabcd )
\r
169 /* Macro that lets vErrorChecks() know that one of the tasks defined in
\r
170 main. c has detected an error. A critical region is used around xLatchError
\r
171 as it is accessed from vErrorChecks(), which has a higher priority. */
\r
172 #define mainLATCH_ERROR() \
\r
174 portENTER_CRITICAL(); \
\r
175 xLatchedError = pdTRUE; \
\r
176 portEXIT_CRITICAL(); \
\r
180 * Setup the Cygnal microcontroller for its fastest operation.
\r
182 static void prvSetupSystemClock( void );
\r
185 * Setup the peripherals, including the on board LED.
\r
187 static void prvSetupHardware( void );
\r
190 * Toggle the state of the on board LED.
\r
192 static void prvToggleOnBoardLED( void );
\r
195 * See comments at the top of the file for details.
\r
197 static void vErrorChecks( void *pvParameters );
\r
200 * See comments at the top of the file for details.
\r
202 static void vRegisterCheck( void *pvParameters );
\r
205 * See comments at the top of the file for details.
\r
207 static void vFLOPCheck1( void *pvParameters );
\r
210 * See comments at the top of the file for details.
\r
212 static void vFLOPCheck2( void *pvParameters );
\r
214 /* File scope variable used to communicate the occurrence of an error between
\r
216 static portBASE_TYPE xLatchedError = pdFALSE;
\r
218 /*-----------------------------------------------------------*/
\r
221 * Starts all the other tasks, then starts the scheduler.
\r
225 /* Initialise the hardware including the system clock and on board
\r
227 prvSetupHardware();
\r
229 /* Initialise the port that controls the external LED's utilized by the
\r
231 vParTestInitialise();
\r
233 /* Start the used standard demo tasks. */
\r
234 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
235 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
236 vStartIntegerMathTasks( mainINTEGER_PRIORITY );
\r
237 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
238 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
240 /* Start the tasks defined in this file. The first three never block so
\r
241 must not be used with the co-operative scheduler. */
\r
242 #if configUSE_PREEMPTION == 1
\r
244 xTaskCreate( vRegisterCheck, "RegChck", configMINIMAL_STACK_SIZE, mainDUMMY_POINTER, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );
\r
245 xTaskCreate( vFLOPCheck1, "FLOP", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );
\r
246 xTaskCreate( vFLOPCheck2, "FLOP", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );
\r
250 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, ( TaskHandle_t * ) NULL );
\r
252 /* Finally kick off the scheduler. This function should never return. */
\r
253 vTaskStartScheduler();
\r
255 /* Should never reach here as the tasks will now be executing under control
\r
256 of the scheduler. */
\r
258 /*-----------------------------------------------------------*/
\r
261 * Setup the hardware prior to using the scheduler. Most of the Cygnal
\r
262 * specific initialisation is performed here leaving standard 8052 setup
\r
263 * only in the driver code.
\r
265 static void prvSetupHardware( void )
\r
267 unsigned char ucOriginalSFRPage;
\r
269 /* Remember the SFR page before it is changed so it can get set back
\r
270 before the function exits. */
\r
271 ucOriginalSFRPage = SFRPAGE;
\r
273 /* Setup the SFR page to access the config SFR's. */
\r
274 SFRPAGE = CONFIG_PAGE;
\r
276 /* Don't allow the microcontroller to automatically switch SFR page, as the
\r
277 SFR page is not stored as part of the task context. */
\r
278 SFRPGCN = mainAUTO_SFR_OFF;
\r
280 /* Disable the watchdog. */
\r
281 WDTCN = mainDISABLE_BYTE_1;
\r
282 WDTCN = mainDISABLE_BYTE_2;
\r
284 /* Set the on board LED to push pull. */
\r
285 P1MDOUT |= mainPORT_1_BIT_6;
\r
287 /* Setup the cross bar to enable serial comms here as it is not part of the
\r
288 standard 8051 setup and therefore is not in the driver code. */
\r
289 XBR0 |= mainENABLE_COMS;
\r
290 P0MDOUT |= mainCOMS_LINES_TO_PUSH_PULL;
\r
292 /* Enable the cross bar so our hardware setup takes effect. */
\r
293 XBR2 = mainENABLE_CROSS_BAR;
\r
295 /* Setup a fast system clock. */
\r
296 prvSetupSystemClock();
\r
298 /* Return the SFR page. */
\r
299 SFRPAGE = ucOriginalSFRPage;
\r
301 /*-----------------------------------------------------------*/
\r
303 static void prvSetupSystemClock( void )
\r
305 volatile unsigned short usWait;
\r
306 const unsigned short usWaitTime = ( unsigned short ) 0x2ff;
\r
307 unsigned char ucOriginalSFRPage;
\r
309 /* Remember the SFR page so we can set it back at the end. */
\r
310 ucOriginalSFRPage = SFRPAGE;
\r
311 SFRPAGE = CONFIG_PAGE;
\r
313 /* Use the internal oscillator set to its fasted frequency. */
\r
314 OSCICN = mainSELECT_INTERNAL_OSC | mainDIVIDE_CLOCK_BY_1;
\r
316 /* Ensure the clock is stable. */
\r
317 for( usWait = 0; usWait < usWaitTime; usWait++ );
\r
319 /* Setup the clock source for the PLL. */
\r
320 PLL0CN &= ~mainPLL_USES_INTERNAL_OSC;
\r
322 /* Change the read timing for the flash ready for the fast clock. */
\r
323 SFRPAGE = LEGACY_PAGE;
\r
324 FLSCL |= mainFLASH_READ_TIMING;
\r
326 /* Turn on the PLL power. */
\r
327 SFRPAGE = CONFIG_PAGE;
\r
328 PLL0CN |= mainPLL_POWER_ON;
\r
330 /* Don't predivide the clock. */
\r
331 PLL0DIV = mainPLL_NO_PREDIVIDE;
\r
333 /* Set filter for fastest clock. */
\r
334 PLL0FLT = mainPLL_FILTER;
\r
335 PLL0MUL = mainPLL_MULTIPLICATION;
\r
337 /* Ensure the clock is stable. */
\r
338 for( usWait = 0; usWait < usWaitTime; usWait++ );
\r
340 /* Enable the PLL and wait for it to lock. */
\r
341 PLL0CN |= mainENABLE_PLL;
\r
342 for( usWait = 0; usWait < usWaitTime; usWait++ )
\r
344 if( PLL0CN & mainPLL_LOCKED )
\r
350 /* Select the PLL as the clock source. */
\r
351 CLKSEL |= mainSELECT_PLL_AS_SOURCE;
\r
353 /* Return the SFR back to its original value. */
\r
354 SFRPAGE = ucOriginalSFRPage;
\r
356 /*-----------------------------------------------------------*/
\r
358 static void prvToggleOnBoardLED( void )
\r
360 /* If the on board LED is on, turn it off and vice versa. */
\r
361 if( P1 & ucLED_BIT )
\r
370 /*-----------------------------------------------------------*/
\r
373 * See the documentation at the top of this file.
\r
375 static void vErrorChecks( void *pvParameters )
\r
377 portBASE_TYPE xErrorHasOccurred = pdFALSE;
\r
379 /* Just to prevent compiler warnings. */
\r
380 ( void ) pvParameters;
\r
382 /* Cycle for ever, delaying then checking all the other tasks are still
\r
383 operating without error. The delay period depends on whether an error
\r
384 has ever been detected. */
\r
387 if( xLatchedError == pdFALSE )
\r
389 /* No errors have been detected so delay for a longer period. The
\r
390 on board LED will get toggled every mainNO_ERROR_FLASH_PERIOD ms. */
\r
391 vTaskDelay( mainNO_ERROR_FLASH_PERIOD );
\r
395 /* We have at some time recognised an error in one of the demo
\r
396 application tasks, delay for a shorter period. The on board LED
\r
397 will get toggled every mainERROR_FLASH_PERIOD ms. */
\r
398 vTaskDelay( mainERROR_FLASH_PERIOD );
\r
403 /* Check the demo application tasks for errors. */
\r
405 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
407 xErrorHasOccurred = pdTRUE;
\r
410 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
412 xErrorHasOccurred = pdTRUE;
\r
415 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
417 xErrorHasOccurred = pdTRUE;
\r
420 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
422 xErrorHasOccurred = pdTRUE;
\r
425 /* If an error has occurred, latch it to cause the LED flash rate to
\r
427 if( xErrorHasOccurred == pdTRUE )
\r
429 xLatchedError = pdTRUE;
\r
432 /* Toggle the LED to indicate the completion of a check cycle. The
\r
433 frequency of check cycles is dependent on whether or not we have
\r
434 latched an error. */
\r
435 prvToggleOnBoardLED();
\r
438 /*-----------------------------------------------------------*/
\r
441 * See the documentation at the top of this file. Also see the standard FLOP
\r
442 * demo task documentation for the rationale of these tasks.
\r
444 static void vFLOPCheck1( void *pvParameters )
\r
446 volatile portFLOAT fVal1, fVal2, fResult;
\r
448 ( void ) pvParameters;
\r
452 fVal1 = ( portFLOAT ) -1234.5678;
\r
453 fVal2 = ( portFLOAT ) 2345.6789;
\r
455 fResult = fVal1 + fVal2;
\r
456 if( ( fResult > ( portFLOAT ) 1111.15 ) || ( fResult < ( portFLOAT ) 1111.05 ) )
\r
461 fResult = fVal1 / fVal2;
\r
462 if( ( fResult > ( portFLOAT ) -0.51 ) || ( fResult < ( portFLOAT ) -0.53 ) )
\r
468 /*-----------------------------------------------------------*/
\r
471 * See the documentation at the top of this file.
\r
473 static void vFLOPCheck2( void *pvParameters )
\r
475 volatile portFLOAT fVal1, fVal2, fResult;
\r
477 ( void ) pvParameters;
\r
481 fVal1 = ( portFLOAT ) -12340.5678;
\r
482 fVal2 = ( portFLOAT ) 23450.6789;
\r
484 fResult = fVal1 + fVal2;
\r
485 if( ( fResult > ( portFLOAT ) 11110.15 ) || ( fResult < ( portFLOAT ) 11110.05 ) )
\r
490 fResult = fVal1 / -fVal2;
\r
491 if( ( fResult > ( portFLOAT ) 0.53 ) || ( fResult < ( portFLOAT ) 0.51 ) )
\r
497 /*-----------------------------------------------------------*/
\r
500 * See the documentation at the top of this file.
\r
502 static void vRegisterCheck( void *pvParameters )
\r
504 ( void ) pvParameters;
\r
508 if( SP != configSTACK_START )
\r