2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
97 * Creates the demo application tasks, then starts the scheduler. The WEB
\r
98 * documentation provides more details of the demo application tasks.
\r
100 * Main. c also creates four other tasks:
\r
102 * 1) vErrorChecks()
\r
103 * This only executes every few seconds but has the highest priority so is
\r
104 * guaranteed to get processor time. Its main function is to check that all
\r
105 * the standard demo application tasks are still operational and have not
\r
106 * experienced any errors. vErrorChecks() will toggle the on board LED
\r
107 * every mainNO_ERROR_FLASH_PERIOD milliseconds if none of the demo application
\r
108 * tasks have reported an error. Should any task report an error at any time
\r
109 * the rate at which the on board LED is toggled is increased to
\r
110 * mainERROR_FLASH_PERIOD - providing visual feedback that something has gone
\r
113 * 2) vRegisterCheck()
\r
114 * This is a very simple task that checks that all the registers are always
\r
115 * in their expected state. The task only makes use of the A register, so
\r
116 * all the other registers should always contain their initial values.
\r
117 * An incorrect value indicates an error in the context switch mechanism.
\r
118 * The task operates at the idle priority so will be preempted regularly.
\r
119 * Any error will cause the toggle rate of the on board LED to increase to
\r
120 * mainERROR_FLASH_PERIOD milliseconds.
\r
122 * 3 and 4) vFLOPCheck1() and vFLOPCheck2()
\r
123 * These are very basic versions of the standard FLOP tasks. They are good
\r
124 * at detecting errors in the context switch mechanism, and also check that
\r
125 * the floating point libraries are correctly built to be re-enterant. The
\r
126 * stack restrictions of the 8051 prevent the use of the standard FLOP demo
\r
130 /* Standard includes. */
\r
131 #include <stdlib.h>
\r
133 /* Scheduler includes. */
\r
134 #include "FreeRTOS.h"
\r
137 /* Demo application includes. */
\r
138 #include "partest.h"
\r
140 #include "integer.h"
\r
142 #include "comtest2.h"
\r
143 #include "semtest.h"
\r
145 /* Demo task priorities. */
\r
146 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
147 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
148 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
149 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
150 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
151 #define mainINTEGER_PRIORITY tskIDLE_PRIORITY
\r
153 /* Constants required to disable the watchdog. */
\r
154 #define mainDISABLE_BYTE_1 ( ( unsigned char ) 0xde )
\r
155 #define mainDISABLE_BYTE_2 ( ( unsigned char ) 0xad )
\r
157 /* Constants to setup and use the on board LED. */
\r
158 #define ucLED_BIT ( ( unsigned char ) 0x40 )
\r
159 #define mainPORT_1_BIT_6 ( ( unsigned char ) 0x40 )
\r
160 #define mainENABLE_CROSS_BAR ( ( unsigned char ) 0x40 )
\r
162 /* Constants to set the clock frequency. */
\r
163 #define mainSELECT_INTERNAL_OSC ( ( unsigned char ) 0x80 )
\r
164 #define mainDIVIDE_CLOCK_BY_1 ( ( unsigned char ) 0x03 )
\r
165 #define mainPLL_USES_INTERNAL_OSC ( ( unsigned char ) 0x04 )
\r
166 #define mainFLASH_READ_TIMING ( ( unsigned char ) 0x30 )
\r
167 #define mainPLL_POWER_ON ( ( unsigned char ) 0x01 )
\r
168 #define mainPLL_NO_PREDIVIDE ( ( unsigned char ) 0x01 )
\r
169 #define mainPLL_FILTER ( ( unsigned char ) 0x01 )
\r
170 #define mainPLL_MULTIPLICATION ( ( unsigned char ) 0x04 )
\r
171 #define mainENABLE_PLL ( ( unsigned char ) 0x02 )
\r
172 #define mainPLL_LOCKED ( ( unsigned char ) 0x10 )
\r
173 #define mainSELECT_PLL_AS_SOURCE ( ( unsigned char ) 0x02 )
\r
175 /* Toggle rate for the on board LED - which is dependent on whether or not
\r
176 an error has been detected. */
\r
177 #define mainNO_ERROR_FLASH_PERIOD ( ( TickType_t ) 5000 )
\r
178 #define mainERROR_FLASH_PERIOD ( ( TickType_t ) 250 )
\r
180 /* Baud rate used by the serial port tasks. */
\r
181 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )
\r
183 /* Pass an invalid LED number to the COM test task as we don't want it to flash
\r
184 an LED. There are only 8 LEDs (excluding the on board LED) wired in and these
\r
185 are all used by the flash tasks. */
\r
186 #define mainCOM_TEST_LED ( 200 )
\r
188 /* We want the Cygnal to act as much as possible as a standard 8052. */
\r
189 #define mainAUTO_SFR_OFF ( ( unsigned char ) 0 )
\r
191 /* Constants required to setup the IO pins for serial comms. */
\r
192 #define mainENABLE_COMS ( ( unsigned char ) 0x04 )
\r
193 #define mainCOMS_LINES_TO_PUSH_PULL ( ( unsigned char ) 0x03 )
\r
195 /* Pointer passed as a parameter to vRegisterCheck() just so it has some know
\r
196 values to check for in the DPH, DPL and B registers. */
\r
197 #define mainDUMMY_POINTER ( ( xdata void * ) 0xabcd )
\r
199 /* Macro that lets vErrorChecks() know that one of the tasks defined in
\r
200 main. c has detected an error. A critical region is used around xLatchError
\r
201 as it is accessed from vErrorChecks(), which has a higher priority. */
\r
202 #define mainLATCH_ERROR() \
\r
204 portENTER_CRITICAL(); \
\r
205 xLatchedError = pdTRUE; \
\r
206 portEXIT_CRITICAL(); \
\r
210 * Setup the Cygnal microcontroller for its fastest operation.
\r
212 static void prvSetupSystemClock( void );
\r
215 * Setup the peripherals, including the on board LED.
\r
217 static void prvSetupHardware( void );
\r
220 * Toggle the state of the on board LED.
\r
222 static void prvToggleOnBoardLED( void );
\r
225 * See comments at the top of the file for details.
\r
227 static void vErrorChecks( void *pvParameters );
\r
230 * See comments at the top of the file for details.
\r
232 static void vRegisterCheck( void *pvParameters );
\r
235 * See comments at the top of the file for details.
\r
237 static void vFLOPCheck1( void *pvParameters );
\r
240 * See comments at the top of the file for details.
\r
242 static void vFLOPCheck2( void *pvParameters );
\r
244 /* File scope variable used to communicate the occurrence of an error between
\r
246 static portBASE_TYPE xLatchedError = pdFALSE;
\r
248 /*-----------------------------------------------------------*/
\r
251 * Starts all the other tasks, then starts the scheduler.
\r
255 /* Initialise the hardware including the system clock and on board
\r
257 prvSetupHardware();
\r
259 /* Initialise the port that controls the external LED's utilized by the
\r
261 vParTestInitialise();
\r
263 /* Start the used standard demo tasks. */
\r
264 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
265 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
266 vStartIntegerMathTasks( mainINTEGER_PRIORITY );
\r
267 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
268 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
270 /* Start the tasks defined in this file. The first three never block so
\r
271 must not be used with the co-operative scheduler. */
\r
272 #if configUSE_PREEMPTION == 1
\r
274 xTaskCreate( vRegisterCheck, "RegChck", configMINIMAL_STACK_SIZE, mainDUMMY_POINTER, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );
\r
275 xTaskCreate( vFLOPCheck1, "FLOP", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );
\r
276 xTaskCreate( vFLOPCheck2, "FLOP", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );
\r
280 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, ( TaskHandle_t * ) NULL );
\r
282 /* Finally kick off the scheduler. This function should never return. */
\r
283 vTaskStartScheduler();
\r
285 /* Should never reach here as the tasks will now be executing under control
\r
286 of the scheduler. */
\r
288 /*-----------------------------------------------------------*/
\r
291 * Setup the hardware prior to using the scheduler. Most of the Cygnal
\r
292 * specific initialisation is performed here leaving standard 8052 setup
\r
293 * only in the driver code.
\r
295 static void prvSetupHardware( void )
\r
297 unsigned char ucOriginalSFRPage;
\r
299 /* Remember the SFR page before it is changed so it can get set back
\r
300 before the function exits. */
\r
301 ucOriginalSFRPage = SFRPAGE;
\r
303 /* Setup the SFR page to access the config SFR's. */
\r
304 SFRPAGE = CONFIG_PAGE;
\r
306 /* Don't allow the microcontroller to automatically switch SFR page, as the
\r
307 SFR page is not stored as part of the task context. */
\r
308 SFRPGCN = mainAUTO_SFR_OFF;
\r
310 /* Disable the watchdog. */
\r
311 WDTCN = mainDISABLE_BYTE_1;
\r
312 WDTCN = mainDISABLE_BYTE_2;
\r
314 /* Set the on board LED to push pull. */
\r
315 P1MDOUT |= mainPORT_1_BIT_6;
\r
317 /* Setup the cross bar to enable serial comms here as it is not part of the
\r
318 standard 8051 setup and therefore is not in the driver code. */
\r
319 XBR0 |= mainENABLE_COMS;
\r
320 P0MDOUT |= mainCOMS_LINES_TO_PUSH_PULL;
\r
322 /* Enable the cross bar so our hardware setup takes effect. */
\r
323 XBR2 = mainENABLE_CROSS_BAR;
\r
325 /* Setup a fast system clock. */
\r
326 prvSetupSystemClock();
\r
328 /* Return the SFR page. */
\r
329 SFRPAGE = ucOriginalSFRPage;
\r
331 /*-----------------------------------------------------------*/
\r
333 static void prvSetupSystemClock( void )
\r
335 volatile unsigned short usWait;
\r
336 const unsigned short usWaitTime = ( unsigned short ) 0x2ff;
\r
337 unsigned char ucOriginalSFRPage;
\r
339 /* Remember the SFR page so we can set it back at the end. */
\r
340 ucOriginalSFRPage = SFRPAGE;
\r
341 SFRPAGE = CONFIG_PAGE;
\r
343 /* Use the internal oscillator set to its fasted frequency. */
\r
344 OSCICN = mainSELECT_INTERNAL_OSC | mainDIVIDE_CLOCK_BY_1;
\r
346 /* Ensure the clock is stable. */
\r
347 for( usWait = 0; usWait < usWaitTime; usWait++ );
\r
349 /* Setup the clock source for the PLL. */
\r
350 PLL0CN &= ~mainPLL_USES_INTERNAL_OSC;
\r
352 /* Change the read timing for the flash ready for the fast clock. */
\r
353 SFRPAGE = LEGACY_PAGE;
\r
354 FLSCL |= mainFLASH_READ_TIMING;
\r
356 /* Turn on the PLL power. */
\r
357 SFRPAGE = CONFIG_PAGE;
\r
358 PLL0CN |= mainPLL_POWER_ON;
\r
360 /* Don't predivide the clock. */
\r
361 PLL0DIV = mainPLL_NO_PREDIVIDE;
\r
363 /* Set filter for fastest clock. */
\r
364 PLL0FLT = mainPLL_FILTER;
\r
365 PLL0MUL = mainPLL_MULTIPLICATION;
\r
367 /* Ensure the clock is stable. */
\r
368 for( usWait = 0; usWait < usWaitTime; usWait++ );
\r
370 /* Enable the PLL and wait for it to lock. */
\r
371 PLL0CN |= mainENABLE_PLL;
\r
372 for( usWait = 0; usWait < usWaitTime; usWait++ )
\r
374 if( PLL0CN & mainPLL_LOCKED )
\r
380 /* Select the PLL as the clock source. */
\r
381 CLKSEL |= mainSELECT_PLL_AS_SOURCE;
\r
383 /* Return the SFR back to its original value. */
\r
384 SFRPAGE = ucOriginalSFRPage;
\r
386 /*-----------------------------------------------------------*/
\r
388 static void prvToggleOnBoardLED( void )
\r
390 /* If the on board LED is on, turn it off and vice versa. */
\r
391 if( P1 & ucLED_BIT )
\r
400 /*-----------------------------------------------------------*/
\r
403 * See the documentation at the top of this file.
\r
405 static void vErrorChecks( void *pvParameters )
\r
407 portBASE_TYPE xErrorHasOccurred = pdFALSE;
\r
409 /* Just to prevent compiler warnings. */
\r
410 ( void ) pvParameters;
\r
412 /* Cycle for ever, delaying then checking all the other tasks are still
\r
413 operating without error. The delay period depends on whether an error
\r
414 has ever been detected. */
\r
417 if( xLatchedError == pdFALSE )
\r
419 /* No errors have been detected so delay for a longer period. The
\r
420 on board LED will get toggled every mainNO_ERROR_FLASH_PERIOD ms. */
\r
421 vTaskDelay( mainNO_ERROR_FLASH_PERIOD );
\r
425 /* We have at some time recognised an error in one of the demo
\r
426 application tasks, delay for a shorter period. The on board LED
\r
427 will get toggled every mainERROR_FLASH_PERIOD ms. */
\r
428 vTaskDelay( mainERROR_FLASH_PERIOD );
\r
433 /* Check the demo application tasks for errors. */
\r
435 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
437 xErrorHasOccurred = pdTRUE;
\r
440 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
442 xErrorHasOccurred = pdTRUE;
\r
445 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
447 xErrorHasOccurred = pdTRUE;
\r
450 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
452 xErrorHasOccurred = pdTRUE;
\r
455 /* If an error has occurred, latch it to cause the LED flash rate to
\r
457 if( xErrorHasOccurred == pdTRUE )
\r
459 xLatchedError = pdTRUE;
\r
462 /* Toggle the LED to indicate the completion of a check cycle. The
\r
463 frequency of check cycles is dependent on whether or not we have
\r
464 latched an error. */
\r
465 prvToggleOnBoardLED();
\r
468 /*-----------------------------------------------------------*/
\r
471 * See the documentation at the top of this file. Also see the standard FLOP
\r
472 * demo task documentation for the rationale of these tasks.
\r
474 static void vFLOPCheck1( void *pvParameters )
\r
476 volatile portFLOAT fVal1, fVal2, fResult;
\r
478 ( void ) pvParameters;
\r
482 fVal1 = ( portFLOAT ) -1234.5678;
\r
483 fVal2 = ( portFLOAT ) 2345.6789;
\r
485 fResult = fVal1 + fVal2;
\r
486 if( ( fResult > ( portFLOAT ) 1111.15 ) || ( fResult < ( portFLOAT ) 1111.05 ) )
\r
491 fResult = fVal1 / fVal2;
\r
492 if( ( fResult > ( portFLOAT ) -0.51 ) || ( fResult < ( portFLOAT ) -0.53 ) )
\r
498 /*-----------------------------------------------------------*/
\r
501 * See the documentation at the top of this file.
\r
503 static void vFLOPCheck2( void *pvParameters )
\r
505 volatile portFLOAT fVal1, fVal2, fResult;
\r
507 ( void ) pvParameters;
\r
511 fVal1 = ( portFLOAT ) -12340.5678;
\r
512 fVal2 = ( portFLOAT ) 23450.6789;
\r
514 fResult = fVal1 + fVal2;
\r
515 if( ( fResult > ( portFLOAT ) 11110.15 ) || ( fResult < ( portFLOAT ) 11110.05 ) )
\r
520 fResult = fVal1 / -fVal2;
\r
521 if( ( fResult > ( portFLOAT ) 0.53 ) || ( fResult < ( portFLOAT ) 0.51 ) )
\r
527 /*-----------------------------------------------------------*/
\r
530 * See the documentation at the top of this file.
\r
532 static void vRegisterCheck( void *pvParameters )
\r
534 ( void ) pvParameters;
\r
538 if( SP != configSTACK_START )
\r