2 FreeRTOS V8.2.2 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
71 * Creates the demo application tasks, then starts the scheduler. The WEB
\r
72 * documentation provides more details of the demo application tasks.
\r
74 * Main. c also creates four other tasks:
\r
77 * This only executes every few seconds but has the highest priority so is
\r
78 * guaranteed to get processor time. Its main function is to check that all
\r
79 * the standard demo application tasks are still operational and have not
\r
80 * experienced any errors. vErrorChecks() will toggle the on board LED
\r
81 * every mainNO_ERROR_FLASH_PERIOD milliseconds if none of the demo application
\r
82 * tasks have reported an error. Should any task report an error at any time
\r
83 * the rate at which the on board LED is toggled is increased to
\r
84 * mainERROR_FLASH_PERIOD - providing visual feedback that something has gone
\r
87 * 2) vRegisterCheck()
\r
88 * This is a very simple task that checks that all the registers are always
\r
89 * in their expected state. The task only makes use of the A register, so
\r
90 * all the other registers should always contain their initial values.
\r
91 * An incorrect value indicates an error in the context switch mechanism.
\r
92 * The task operates at the idle priority so will be preempted regularly.
\r
93 * Any error will cause the toggle rate of the on board LED to increase to
\r
94 * mainERROR_FLASH_PERIOD milliseconds.
\r
96 * 3 and 4) vFLOPCheck1() and vFLOPCheck2()
\r
97 * These are very basic versions of the standard FLOP tasks. They are good
\r
98 * at detecting errors in the context switch mechanism, and also check that
\r
99 * the floating point libraries are correctly built to be re-enterant. The
\r
100 * stack restrictions of the 8051 prevent the use of the standard FLOP demo
\r
104 /* Standard includes. */
\r
105 #include <stdlib.h>
\r
107 /* Scheduler includes. */
\r
108 #include "FreeRTOS.h"
\r
111 /* Demo application includes. */
\r
112 #include "partest.h"
\r
114 #include "integer.h"
\r
116 #include "comtest2.h"
\r
117 #include "semtest.h"
\r
119 /* Demo task priorities. */
\r
120 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
121 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
122 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
123 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
124 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
125 #define mainINTEGER_PRIORITY tskIDLE_PRIORITY
\r
127 /* Constants required to disable the watchdog. */
\r
128 #define mainDISABLE_BYTE_1 ( ( unsigned char ) 0xde )
\r
129 #define mainDISABLE_BYTE_2 ( ( unsigned char ) 0xad )
\r
131 /* Constants to setup and use the on board LED. */
\r
132 #define ucLED_BIT ( ( unsigned char ) 0x40 )
\r
133 #define mainPORT_1_BIT_6 ( ( unsigned char ) 0x40 )
\r
134 #define mainENABLE_CROSS_BAR ( ( unsigned char ) 0x40 )
\r
136 /* Constants to set the clock frequency. */
\r
137 #define mainSELECT_INTERNAL_OSC ( ( unsigned char ) 0x80 )
\r
138 #define mainDIVIDE_CLOCK_BY_1 ( ( unsigned char ) 0x03 )
\r
139 #define mainPLL_USES_INTERNAL_OSC ( ( unsigned char ) 0x04 )
\r
140 #define mainFLASH_READ_TIMING ( ( unsigned char ) 0x30 )
\r
141 #define mainPLL_POWER_ON ( ( unsigned char ) 0x01 )
\r
142 #define mainPLL_NO_PREDIVIDE ( ( unsigned char ) 0x01 )
\r
143 #define mainPLL_FILTER ( ( unsigned char ) 0x01 )
\r
144 #define mainPLL_MULTIPLICATION ( ( unsigned char ) 0x04 )
\r
145 #define mainENABLE_PLL ( ( unsigned char ) 0x02 )
\r
146 #define mainPLL_LOCKED ( ( unsigned char ) 0x10 )
\r
147 #define mainSELECT_PLL_AS_SOURCE ( ( unsigned char ) 0x02 )
\r
149 /* Toggle rate for the on board LED - which is dependent on whether or not
\r
150 an error has been detected. */
\r
151 #define mainNO_ERROR_FLASH_PERIOD ( ( TickType_t ) 5000 )
\r
152 #define mainERROR_FLASH_PERIOD ( ( TickType_t ) 250 )
\r
154 /* Baud rate used by the serial port tasks. */
\r
155 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )
\r
157 /* Pass an invalid LED number to the COM test task as we don't want it to flash
\r
158 an LED. There are only 8 LEDs (excluding the on board LED) wired in and these
\r
159 are all used by the flash tasks. */
\r
160 #define mainCOM_TEST_LED ( 200 )
\r
162 /* We want the Cygnal to act as much as possible as a standard 8052. */
\r
163 #define mainAUTO_SFR_OFF ( ( unsigned char ) 0 )
\r
165 /* Constants required to setup the IO pins for serial comms. */
\r
166 #define mainENABLE_COMS ( ( unsigned char ) 0x04 )
\r
167 #define mainCOMS_LINES_TO_PUSH_PULL ( ( unsigned char ) 0x03 )
\r
169 /* Pointer passed as a parameter to vRegisterCheck() just so it has some know
\r
170 values to check for in the DPH, DPL and B registers. */
\r
171 #define mainDUMMY_POINTER ( ( xdata void * ) 0xabcd )
\r
173 /* Macro that lets vErrorChecks() know that one of the tasks defined in
\r
174 main. c has detected an error. A critical region is used around xLatchError
\r
175 as it is accessed from vErrorChecks(), which has a higher priority. */
\r
176 #define mainLATCH_ERROR() \
\r
178 portENTER_CRITICAL(); \
\r
179 xLatchedError = pdTRUE; \
\r
180 portEXIT_CRITICAL(); \
\r
184 * Setup the Cygnal microcontroller for its fastest operation.
\r
186 static void prvSetupSystemClock( void );
\r
189 * Setup the peripherals, including the on board LED.
\r
191 static void prvSetupHardware( void );
\r
194 * Toggle the state of the on board LED.
\r
196 static void prvToggleOnBoardLED( void );
\r
199 * See comments at the top of the file for details.
\r
201 static void vErrorChecks( void *pvParameters );
\r
204 * See comments at the top of the file for details.
\r
206 static void vRegisterCheck( void *pvParameters );
\r
209 * See comments at the top of the file for details.
\r
211 static void vFLOPCheck1( void *pvParameters );
\r
214 * See comments at the top of the file for details.
\r
216 static void vFLOPCheck2( void *pvParameters );
\r
218 /* File scope variable used to communicate the occurrence of an error between
\r
220 static portBASE_TYPE xLatchedError = pdFALSE;
\r
222 /*-----------------------------------------------------------*/
\r
225 * Starts all the other tasks, then starts the scheduler.
\r
229 /* Initialise the hardware including the system clock and on board
\r
231 prvSetupHardware();
\r
233 /* Initialise the port that controls the external LED's utilized by the
\r
235 vParTestInitialise();
\r
237 /* Start the used standard demo tasks. */
\r
238 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
239 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
240 vStartIntegerMathTasks( mainINTEGER_PRIORITY );
\r
241 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
242 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
244 /* Start the tasks defined in this file. The first three never block so
\r
245 must not be used with the co-operative scheduler. */
\r
246 #if configUSE_PREEMPTION == 1
\r
248 xTaskCreate( vRegisterCheck, "RegChck", configMINIMAL_STACK_SIZE, mainDUMMY_POINTER, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );
\r
249 xTaskCreate( vFLOPCheck1, "FLOP", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );
\r
250 xTaskCreate( vFLOPCheck2, "FLOP", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );
\r
254 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, ( TaskHandle_t * ) NULL );
\r
256 /* Finally kick off the scheduler. This function should never return. */
\r
257 vTaskStartScheduler();
\r
259 /* Should never reach here as the tasks will now be executing under control
\r
260 of the scheduler. */
\r
262 /*-----------------------------------------------------------*/
\r
265 * Setup the hardware prior to using the scheduler. Most of the Cygnal
\r
266 * specific initialisation is performed here leaving standard 8052 setup
\r
267 * only in the driver code.
\r
269 static void prvSetupHardware( void )
\r
271 unsigned char ucOriginalSFRPage;
\r
273 /* Remember the SFR page before it is changed so it can get set back
\r
274 before the function exits. */
\r
275 ucOriginalSFRPage = SFRPAGE;
\r
277 /* Setup the SFR page to access the config SFR's. */
\r
278 SFRPAGE = CONFIG_PAGE;
\r
280 /* Don't allow the microcontroller to automatically switch SFR page, as the
\r
281 SFR page is not stored as part of the task context. */
\r
282 SFRPGCN = mainAUTO_SFR_OFF;
\r
284 /* Disable the watchdog. */
\r
285 WDTCN = mainDISABLE_BYTE_1;
\r
286 WDTCN = mainDISABLE_BYTE_2;
\r
288 /* Set the on board LED to push pull. */
\r
289 P1MDOUT |= mainPORT_1_BIT_6;
\r
291 /* Setup the cross bar to enable serial comms here as it is not part of the
\r
292 standard 8051 setup and therefore is not in the driver code. */
\r
293 XBR0 |= mainENABLE_COMS;
\r
294 P0MDOUT |= mainCOMS_LINES_TO_PUSH_PULL;
\r
296 /* Enable the cross bar so our hardware setup takes effect. */
\r
297 XBR2 = mainENABLE_CROSS_BAR;
\r
299 /* Setup a fast system clock. */
\r
300 prvSetupSystemClock();
\r
302 /* Return the SFR page. */
\r
303 SFRPAGE = ucOriginalSFRPage;
\r
305 /*-----------------------------------------------------------*/
\r
307 static void prvSetupSystemClock( void )
\r
309 volatile unsigned short usWait;
\r
310 const unsigned short usWaitTime = ( unsigned short ) 0x2ff;
\r
311 unsigned char ucOriginalSFRPage;
\r
313 /* Remember the SFR page so we can set it back at the end. */
\r
314 ucOriginalSFRPage = SFRPAGE;
\r
315 SFRPAGE = CONFIG_PAGE;
\r
317 /* Use the internal oscillator set to its fasted frequency. */
\r
318 OSCICN = mainSELECT_INTERNAL_OSC | mainDIVIDE_CLOCK_BY_1;
\r
320 /* Ensure the clock is stable. */
\r
321 for( usWait = 0; usWait < usWaitTime; usWait++ );
\r
323 /* Setup the clock source for the PLL. */
\r
324 PLL0CN &= ~mainPLL_USES_INTERNAL_OSC;
\r
326 /* Change the read timing for the flash ready for the fast clock. */
\r
327 SFRPAGE = LEGACY_PAGE;
\r
328 FLSCL |= mainFLASH_READ_TIMING;
\r
330 /* Turn on the PLL power. */
\r
331 SFRPAGE = CONFIG_PAGE;
\r
332 PLL0CN |= mainPLL_POWER_ON;
\r
334 /* Don't predivide the clock. */
\r
335 PLL0DIV = mainPLL_NO_PREDIVIDE;
\r
337 /* Set filter for fastest clock. */
\r
338 PLL0FLT = mainPLL_FILTER;
\r
339 PLL0MUL = mainPLL_MULTIPLICATION;
\r
341 /* Ensure the clock is stable. */
\r
342 for( usWait = 0; usWait < usWaitTime; usWait++ );
\r
344 /* Enable the PLL and wait for it to lock. */
\r
345 PLL0CN |= mainENABLE_PLL;
\r
346 for( usWait = 0; usWait < usWaitTime; usWait++ )
\r
348 if( PLL0CN & mainPLL_LOCKED )
\r
354 /* Select the PLL as the clock source. */
\r
355 CLKSEL |= mainSELECT_PLL_AS_SOURCE;
\r
357 /* Return the SFR back to its original value. */
\r
358 SFRPAGE = ucOriginalSFRPage;
\r
360 /*-----------------------------------------------------------*/
\r
362 static void prvToggleOnBoardLED( void )
\r
364 /* If the on board LED is on, turn it off and vice versa. */
\r
365 if( P1 & ucLED_BIT )
\r
374 /*-----------------------------------------------------------*/
\r
377 * See the documentation at the top of this file.
\r
379 static void vErrorChecks( void *pvParameters )
\r
381 portBASE_TYPE xErrorHasOccurred = pdFALSE;
\r
383 /* Just to prevent compiler warnings. */
\r
384 ( void ) pvParameters;
\r
386 /* Cycle for ever, delaying then checking all the other tasks are still
\r
387 operating without error. The delay period depends on whether an error
\r
388 has ever been detected. */
\r
391 if( xLatchedError == pdFALSE )
\r
393 /* No errors have been detected so delay for a longer period. The
\r
394 on board LED will get toggled every mainNO_ERROR_FLASH_PERIOD ms. */
\r
395 vTaskDelay( mainNO_ERROR_FLASH_PERIOD );
\r
399 /* We have at some time recognised an error in one of the demo
\r
400 application tasks, delay for a shorter period. The on board LED
\r
401 will get toggled every mainERROR_FLASH_PERIOD ms. */
\r
402 vTaskDelay( mainERROR_FLASH_PERIOD );
\r
407 /* Check the demo application tasks for errors. */
\r
409 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
411 xErrorHasOccurred = pdTRUE;
\r
414 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
416 xErrorHasOccurred = pdTRUE;
\r
419 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
421 xErrorHasOccurred = pdTRUE;
\r
424 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
426 xErrorHasOccurred = pdTRUE;
\r
429 /* If an error has occurred, latch it to cause the LED flash rate to
\r
431 if( xErrorHasOccurred == pdTRUE )
\r
433 xLatchedError = pdTRUE;
\r
436 /* Toggle the LED to indicate the completion of a check cycle. The
\r
437 frequency of check cycles is dependent on whether or not we have
\r
438 latched an error. */
\r
439 prvToggleOnBoardLED();
\r
442 /*-----------------------------------------------------------*/
\r
445 * See the documentation at the top of this file. Also see the standard FLOP
\r
446 * demo task documentation for the rationale of these tasks.
\r
448 static void vFLOPCheck1( void *pvParameters )
\r
450 volatile portFLOAT fVal1, fVal2, fResult;
\r
452 ( void ) pvParameters;
\r
456 fVal1 = ( portFLOAT ) -1234.5678;
\r
457 fVal2 = ( portFLOAT ) 2345.6789;
\r
459 fResult = fVal1 + fVal2;
\r
460 if( ( fResult > ( portFLOAT ) 1111.15 ) || ( fResult < ( portFLOAT ) 1111.05 ) )
\r
465 fResult = fVal1 / fVal2;
\r
466 if( ( fResult > ( portFLOAT ) -0.51 ) || ( fResult < ( portFLOAT ) -0.53 ) )
\r
472 /*-----------------------------------------------------------*/
\r
475 * See the documentation at the top of this file.
\r
477 static void vFLOPCheck2( void *pvParameters )
\r
479 volatile portFLOAT fVal1, fVal2, fResult;
\r
481 ( void ) pvParameters;
\r
485 fVal1 = ( portFLOAT ) -12340.5678;
\r
486 fVal2 = ( portFLOAT ) 23450.6789;
\r
488 fResult = fVal1 + fVal2;
\r
489 if( ( fResult > ( portFLOAT ) 11110.15 ) || ( fResult < ( portFLOAT ) 11110.05 ) )
\r
494 fResult = fVal1 / -fVal2;
\r
495 if( ( fResult > ( portFLOAT ) 0.53 ) || ( fResult < ( portFLOAT ) 0.51 ) )
\r
501 /*-----------------------------------------------------------*/
\r
504 * See the documentation at the top of this file.
\r
506 static void vRegisterCheck( void *pvParameters )
\r
508 ( void ) pvParameters;
\r
512 if( SP != configSTACK_START )
\r