FreeRTOS Real Time Operating System, Modified from Kenji Arai's initial port. See freertos.org for full documentation.

Fork of FreeRTOS_on_mbed_v1 by Kenji Arai

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers integer.c Source File

integer.c

00001 /*
00002     FreeRTOS V6.0.3 - Copyright (C) 2010 Real Time Engineers Ltd.
00003 
00004     ***************************************************************************
00005     *                                                                         *
00006     * If you are:                                                             *
00007     *                                                                         *
00008     *    + New to FreeRTOS,                                                   *
00009     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *
00010     *    + Looking for basic training,                                        *
00011     *    + Wanting to improve your FreeRTOS skills and productivity           *
00012     *                                                                         *
00013     * then take a look at the FreeRTOS eBook                                  *
00014     *                                                                         *
00015     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *
00016     *                  http://www.FreeRTOS.org/Documentation                  *
00017     *                                                                         *
00018     * A pdf reference manual is also available.  Both are usually delivered   *
00019     * to your inbox within 20 minutes to two hours when purchased between 8am *
00020     * and 8pm GMT (although please allow up to 24 hours in case of            *
00021     * exceptional circumstances).  Thank you for your support!                *
00022     *                                                                         *
00023     ***************************************************************************
00024 
00025     This file is part of the FreeRTOS distribution.
00026 
00027     FreeRTOS is free software; you can redistribute it and/or modify it under
00028     the terms of the GNU General Public License (version 2) as published by the
00029     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
00030     ***NOTE*** The exception to the GPL is included to allow you to distribute
00031     a combined work that includes FreeRTOS without being obliged to provide the
00032     source code for proprietary components outside of the FreeRTOS kernel.
00033     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
00034     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00035     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00036     more details. You should have received a copy of the GNU General Public 
00037     License and the FreeRTOS license exception along with FreeRTOS; if not it 
00038     can be viewed here: http://www.freertos.org/a00114.html and also obtained 
00039     by writing to Richard Barry, contact details for whom are available on the
00040     FreeRTOS WEB site.
00041 
00042     1 tab == 4 spaces!
00043 
00044     http://www.FreeRTOS.org - Documentation, latest information, license and
00045     contact details.
00046 
00047     http://www.SafeRTOS.com - A version that is certified for use in safety
00048     critical systems.
00049 
00050     http://www.OpenRTOS.com - Commercial support, development, porting,
00051     licensing and training services.
00052 */
00053 
00054 /*
00055  * This version of integer. c is for use on systems that have limited stack
00056  * space and no display facilities.  The complete version can be found in
00057  * the Demo/Common/Full directory.
00058  *
00059  * As with the full version, the tasks created in this file are a good test 
00060  * of the scheduler context switch mechanism.  The processor has to access 
00061  * 32bit variables in two or four chunks (depending on the processor).  The low 
00062  * priority of these tasks means there is a high probability that a context 
00063  * switch will occur mid calculation.  See flop. c documentation for 
00064  * more information.
00065  *
00066  */
00067 
00068 /*
00069 Changes from V1.2.1
00070 
00071     + The constants used in the calculations are larger to ensure the
00072       optimiser does not truncate them to 16 bits.
00073 
00074 Changes from V1.2.3
00075 
00076     + uxTaskCheck is now just used as a boolean.  Instead of incrementing
00077       the variable each cycle of the task, the variable is simply set to
00078       true.  sAreIntegerMathsTaskStillRunning() sets it back to false and
00079       expects it to have been set back to true by the time it is called
00080       again.
00081     + A division has been included in the calculation.
00082 */
00083 
00084 #include <stdlib.h>
00085 
00086 /* Scheduler include files. */
00087 #include "FreeRTOS.h"
00088 #include "task.h"
00089 
00090 /* Demo program include files. */
00091 #include "integer.h"
00092 
00093 /* The constants used in the calculation. */
00094 #define intgCONST1                ( ( long ) 123 )
00095 #define intgCONST2                ( ( long ) 234567 )
00096 #define intgCONST3                ( ( long ) -3 )
00097 #define intgCONST4                ( ( long ) 7 )
00098 #define intgEXPECTED_ANSWER        ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
00099 
00100 #define intgSTACK_SIZE            configMINIMAL_STACK_SIZE
00101 
00102 /* As this is the minimal version, we will only create one task. */
00103 #define intgNUMBER_OF_TASKS        ( 1 )
00104 
00105 /* The task function.  Repeatedly performs a 32 bit calculation, checking the
00106 result against the expected result.  If the result is incorrect then the
00107 context switch must have caused some corruption. */
00108 static portTASK_FUNCTION_PROTO( vCompeteingIntMathTask, pvParameters );
00109 
00110 /* Variables that are set to true within the calculation task to indicate
00111 that the task is still executing.  The check task sets the variable back to
00112 false, flagging an error if the variable is still false the next time it
00113 is called. */
00114 static volatile signed portBASE_TYPE xTaskCheck[ intgNUMBER_OF_TASKS ] = { ( signed portBASE_TYPE ) pdFALSE };
00115 
00116 /*-----------------------------------------------------------*/
00117 
00118 void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority )
00119 {
00120 short sTask;
00121 
00122     for( sTask = 0; sTask < intgNUMBER_OF_TASKS; sTask++ )
00123     {
00124         xTaskCreate( vCompeteingIntMathTask, ( signed char * ) "IntMath", intgSTACK_SIZE, ( void * ) &( xTaskCheck[ sTask ] ), uxPriority, ( xTaskHandle * ) NULL );
00125     }
00126 }
00127 /*-----------------------------------------------------------*/
00128 
00129 static portTASK_FUNCTION( vCompeteingIntMathTask, pvParameters )
00130 {
00131 /* These variables are all effectively set to constants so they are volatile to
00132 ensure the compiler does not just get rid of them. */
00133 volatile long lValue;
00134 short sError = pdFALSE;
00135 volatile signed portBASE_TYPE *pxTaskHasExecuted;
00136 
00137     /* Set a pointer to the variable we are going to set to true each
00138     iteration.  This is also a good test of the parameter passing mechanism
00139     within each port. */
00140     pxTaskHasExecuted = ( volatile signed portBASE_TYPE * ) pvParameters;
00141 
00142     /* Keep performing a calculation and checking the result against a constant. */
00143     for( ;; )
00144     {
00145         /* Perform the calculation.  This will store partial value in
00146         registers, resulting in a good test of the context switch mechanism. */
00147         lValue = intgCONST1;
00148         lValue += intgCONST2;
00149 
00150         /* Yield in case cooperative scheduling is being used. */
00151         #if configUSE_PREEMPTION == 0
00152         {
00153             taskYIELD();
00154         }
00155         #endif
00156 
00157         /* Finish off the calculation. */
00158         lValue *= intgCONST3;
00159         lValue /= intgCONST4;
00160 
00161         /* If the calculation is found to be incorrect we stop setting the 
00162         TaskHasExecuted variable so the check task can see an error has 
00163         occurred. */
00164         if( lValue != intgEXPECTED_ANSWER ) /*lint !e774 volatile used to prevent this being optimised out. */
00165         {
00166             sError = pdTRUE;
00167         }
00168 
00169         if( sError == pdFALSE )
00170         {
00171             /* We have not encountered any errors, so set the flag that show
00172             we are still executing.  This will be periodically cleared by
00173             the check task. */
00174             portENTER_CRITICAL();
00175                 *pxTaskHasExecuted = pdTRUE;
00176             portEXIT_CRITICAL();
00177         }
00178 
00179         /* Yield in case cooperative scheduling is being used. */
00180         #if configUSE_PREEMPTION == 0
00181         {
00182             taskYIELD();
00183         }
00184         #endif
00185     }
00186 }
00187 /*-----------------------------------------------------------*/
00188 
00189 /* This is called to check that all the created tasks are still running. */
00190 portBASE_TYPE xAreIntegerMathsTaskStillRunning( void )
00191 {
00192 portBASE_TYPE xReturn = pdTRUE;
00193 short sTask;
00194 
00195     /* Check the maths tasks are still running by ensuring their check variables 
00196     are still being set to true. */
00197     for( sTask = 0; sTask < intgNUMBER_OF_TASKS; sTask++ )
00198     {
00199         if( xTaskCheck[ sTask ] == pdFALSE )
00200         {
00201             /* The check has not incremented so an error exists. */
00202             xReturn = pdFALSE;
00203         }
00204 
00205         /* Reset the check variable so we can tell if it has been set by
00206         the next time around. */
00207         xTaskCheck[ sTask ] = pdFALSE;
00208     }
00209 
00210     return xReturn;
00211 }
00212