]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/integer.c
First version under SVN is V4.0.1
[freertos] / Demo / Common / Minimal / integer.c
1 /*\r
2         FreeRTOS V4.0.1 - Copyright (C) 2003-2006 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS distribution.\r
5 \r
6         FreeRTOS is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30         ***************************************************************************\r
31 */\r
32 \r
33 /*\r
34  * This version of integer. c is for use on systems that have limited stack\r
35  * space and no display facilities.  The complete version can be found in\r
36  * the Demo/Common/Full directory.\r
37  *\r
38  * As with the full version, the tasks created in this file are a good test \r
39  * of the scheduler context switch mechanism.  The processor has to access \r
40  * 32bit variables in two or four chunks (depending on the processor).  The low \r
41  * priority of these tasks means there is a high probability that a context \r
42  * switch will occur mid calculation.  See flop. c documentation for \r
43  * more information.\r
44  *\r
45  */\r
46 \r
47 /*\r
48 Changes from V1.2.1\r
49 \r
50         + The constants used in the calculations are larger to ensure the\r
51           optimiser does not truncate them to 16 bits.\r
52 \r
53 Changes from V1.2.3\r
54 \r
55         + uxTaskCheck is now just used as a boolean.  Instead of incrementing\r
56           the variable each cycle of the task, the variable is simply set to\r
57           true.  sAreIntegerMathsTaskStillRunning() sets it back to false and\r
58           expects it to have been set back to true by the time it is called\r
59           again.\r
60         + A division has been included in the calculation.\r
61 */\r
62 \r
63 #include <stdlib.h>\r
64 \r
65 /* Scheduler include files. */\r
66 #include "FreeRTOS.h"\r
67 #include "task.h"\r
68 \r
69 /* Demo program include files. */\r
70 #include "integer.h"\r
71 \r
72 /* The constants used in the calculation. */\r
73 #define intgCONST1                              ( ( portLONG ) 123 )\r
74 #define intgCONST2                              ( ( portLONG ) 234567 )\r
75 #define intgCONST3                              ( ( portLONG ) -3 )\r
76 #define intgCONST4                              ( ( portLONG ) 7 )\r
77 #define intgEXPECTED_ANSWER             ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )\r
78 \r
79 #define intgSTACK_SIZE                  configMINIMAL_STACK_SIZE\r
80 \r
81 /* As this is the minimal version, we will only create one task. */\r
82 #define intgNUMBER_OF_TASKS             ( 1 )\r
83 \r
84 /* The task function.  Repeatedly performs a 32 bit calculation, checking the\r
85 result against the expected result.  If the result is incorrect then the\r
86 context switch must have caused some corruption. */\r
87 static portTASK_FUNCTION_PROTO( vCompeteingIntMathTask, pvParameters );\r
88 \r
89 /* Variables that are set to true within the calculation task to indicate\r
90 that the task is still executing.  The check task sets the variable back to\r
91 false, flagging an error if the variable is still false the next time it\r
92 is called. */\r
93 static volatile signed portBASE_TYPE xTaskCheck[ intgNUMBER_OF_TASKS ] = { ( signed portBASE_TYPE ) pdFALSE };\r
94 \r
95 /*-----------------------------------------------------------*/\r
96 \r
97 void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority )\r
98 {\r
99 portSHORT sTask;\r
100 \r
101         for( sTask = 0; sTask < intgNUMBER_OF_TASKS; sTask++ )\r
102         {\r
103                 xTaskCreate( vCompeteingIntMathTask, ( const signed portCHAR * const ) "IntMath", intgSTACK_SIZE, ( void * ) &( xTaskCheck[ sTask ] ), uxPriority, ( xTaskHandle * ) NULL );\r
104         }\r
105 }\r
106 /*-----------------------------------------------------------*/\r
107 \r
108 static portTASK_FUNCTION( vCompeteingIntMathTask, pvParameters )\r
109 {\r
110 /* These variables are all effectively set to constants so they are volatile to\r
111 ensure the compiler does not just get rid of them. */\r
112 volatile portLONG lValue;\r
113 portSHORT sError = pdFALSE;\r
114 volatile signed portBASE_TYPE *pxTaskHasExecuted;\r
115 \r
116         /* Set a pointer to the variable we are going to set to true each\r
117         iteration.  This is also a good test of the parameter passing mechanism\r
118         within each port. */\r
119         pxTaskHasExecuted = ( volatile signed portBASE_TYPE * ) pvParameters;\r
120 \r
121         /* Keep performing a calculation and checking the result against a constant. */\r
122         for( ;; )\r
123         {\r
124                 /* Perform the calculation.  This will store partial value in\r
125                 registers, resulting in a good test of the context switch mechanism. */\r
126                 lValue = intgCONST1;\r
127                 lValue += intgCONST2;\r
128 \r
129                 /* Yield in case cooperative scheduling is being used. */\r
130                 #if configUSE_PREEMPTION == 0\r
131                 {\r
132                         taskYIELD();\r
133                 }\r
134                 #endif\r
135 \r
136                 /* Finish off the calculation. */\r
137                 lValue *= intgCONST3;\r
138                 lValue /= intgCONST4;\r
139 \r
140                 /* If the calculation is found to be incorrect we stop setting the \r
141                 TaskHasExecuted variable so the check task can see an error has \r
142                 occurred. */\r
143                 if( lValue != intgEXPECTED_ANSWER ) /*lint !e774 volatile used to prevent this being optimised out. */\r
144                 {\r
145                         sError = pdTRUE;\r
146                 }\r
147 \r
148                 if( sError == pdFALSE )\r
149                 {\r
150                         /* We have not encountered any errors, so set the flag that show\r
151                         we are still executing.  This will be periodically cleared by\r
152                         the check task. */\r
153                         portENTER_CRITICAL();\r
154                                 *pxTaskHasExecuted = pdTRUE;\r
155                         portEXIT_CRITICAL();\r
156                 }\r
157 \r
158                 /* Yield in case cooperative scheduling is being used. */\r
159                 #if configUSE_PREEMPTION == 0\r
160                 {\r
161                         taskYIELD();\r
162                 }\r
163                 #endif\r
164         }\r
165 }\r
166 /*-----------------------------------------------------------*/\r
167 \r
168 /* This is called to check that all the created tasks are still running. */\r
169 portBASE_TYPE xAreIntegerMathsTaskStillRunning( void )\r
170 {\r
171 portBASE_TYPE xReturn = pdTRUE;\r
172 portSHORT sTask;\r
173 \r
174         /* Check the maths tasks are still running by ensuring their check variables \r
175         are still being set to true. */\r
176         for( sTask = 0; sTask < intgNUMBER_OF_TASKS; sTask++ )\r
177         {\r
178                 if( xTaskCheck[ sTask ] == pdFALSE )\r
179                 {\r
180                         /* The check has not incremented so an error exists. */\r
181                         xReturn = pdFALSE;\r
182                 }\r
183 \r
184                 /* Reset the check variable so we can tell if it has been set by\r
185                 the next time around. */\r
186                 xTaskCheck[ sTask ] = pdFALSE;\r
187         }\r
188 \r
189         return xReturn;\r
190 }\r
191 \r