]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/integer.c
Update ready for V5.1.0 release.
[freertos] / Demo / Common / Minimal / integer.c
1 /*\r
2         FreeRTOS.org V5.1.0 - Copyright (C) 2003-2008 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org 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.org 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.org; 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.org, 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     ***************************************************************************\r
28     *                                                                         *\r
29     * SAVE TIME AND MONEY!  We can port FreeRTOS.org to your own hardware,    *\r
30     * and even write all or part of your application on your behalf.          *\r
31     * See http://www.OpenRTOS.com for details of the services we provide to   *\r
32     * expedite your project.                                                  *\r
33     *                                                                         *\r
34     ***************************************************************************\r
35     ***************************************************************************\r
36 \r
37         Please ensure to read the configuration and relevant port sections of the\r
38         online documentation.\r
39 \r
40         http://www.FreeRTOS.org - Documentation, latest information, license and \r
41         contact details.\r
42 \r
43         http://www.SafeRTOS.com - A version that is certified for use in safety \r
44         critical systems.\r
45 \r
46         http://www.OpenRTOS.com - Commercial support, development, porting, \r
47         licensing and training services.\r
48 */\r
49 \r
50 /*\r
51  * This version of integer. c is for use on systems that have limited stack\r
52  * space and no display facilities.  The complete version can be found in\r
53  * the Demo/Common/Full directory.\r
54  *\r
55  * As with the full version, the tasks created in this file are a good test \r
56  * of the scheduler context switch mechanism.  The processor has to access \r
57  * 32bit variables in two or four chunks (depending on the processor).  The low \r
58  * priority of these tasks means there is a high probability that a context \r
59  * switch will occur mid calculation.  See flop. c documentation for \r
60  * more information.\r
61  *\r
62  */\r
63 \r
64 /*\r
65 Changes from V1.2.1\r
66 \r
67         + The constants used in the calculations are larger to ensure the\r
68           optimiser does not truncate them to 16 bits.\r
69 \r
70 Changes from V1.2.3\r
71 \r
72         + uxTaskCheck is now just used as a boolean.  Instead of incrementing\r
73           the variable each cycle of the task, the variable is simply set to\r
74           true.  sAreIntegerMathsTaskStillRunning() sets it back to false and\r
75           expects it to have been set back to true by the time it is called\r
76           again.\r
77         + A division has been included in the calculation.\r
78 */\r
79 \r
80 #include <stdlib.h>\r
81 \r
82 /* Scheduler include files. */\r
83 #include "FreeRTOS.h"\r
84 #include "task.h"\r
85 \r
86 /* Demo program include files. */\r
87 #include "integer.h"\r
88 \r
89 /* The constants used in the calculation. */\r
90 #define intgCONST1                              ( ( portLONG ) 123 )\r
91 #define intgCONST2                              ( ( portLONG ) 234567 )\r
92 #define intgCONST3                              ( ( portLONG ) -3 )\r
93 #define intgCONST4                              ( ( portLONG ) 7 )\r
94 #define intgEXPECTED_ANSWER             ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )\r
95 \r
96 #define intgSTACK_SIZE                  configMINIMAL_STACK_SIZE\r
97 \r
98 /* As this is the minimal version, we will only create one task. */\r
99 #define intgNUMBER_OF_TASKS             ( 1 )\r
100 \r
101 /* The task function.  Repeatedly performs a 32 bit calculation, checking the\r
102 result against the expected result.  If the result is incorrect then the\r
103 context switch must have caused some corruption. */\r
104 static portTASK_FUNCTION_PROTO( vCompeteingIntMathTask, pvParameters );\r
105 \r
106 /* Variables that are set to true within the calculation task to indicate\r
107 that the task is still executing.  The check task sets the variable back to\r
108 false, flagging an error if the variable is still false the next time it\r
109 is called. */\r
110 static volatile signed portBASE_TYPE xTaskCheck[ intgNUMBER_OF_TASKS ] = { ( signed portBASE_TYPE ) pdFALSE };\r
111 \r
112 /*-----------------------------------------------------------*/\r
113 \r
114 void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority )\r
115 {\r
116 portSHORT sTask;\r
117 \r
118         for( sTask = 0; sTask < intgNUMBER_OF_TASKS; sTask++ )\r
119         {\r
120                 xTaskCreate( vCompeteingIntMathTask, ( signed portCHAR * ) "IntMath", intgSTACK_SIZE, ( void * ) &( xTaskCheck[ sTask ] ), uxPriority, ( xTaskHandle * ) NULL );\r
121         }\r
122 }\r
123 /*-----------------------------------------------------------*/\r
124 \r
125 static portTASK_FUNCTION( vCompeteingIntMathTask, pvParameters )\r
126 {\r
127 /* These variables are all effectively set to constants so they are volatile to\r
128 ensure the compiler does not just get rid of them. */\r
129 volatile portLONG lValue;\r
130 portSHORT sError = pdFALSE;\r
131 volatile signed portBASE_TYPE *pxTaskHasExecuted;\r
132 \r
133         /* Set a pointer to the variable we are going to set to true each\r
134         iteration.  This is also a good test of the parameter passing mechanism\r
135         within each port. */\r
136         pxTaskHasExecuted = ( volatile signed portBASE_TYPE * ) pvParameters;\r
137 \r
138         /* Keep performing a calculation and checking the result against a constant. */\r
139         for( ;; )\r
140         {\r
141                 /* Perform the calculation.  This will store partial value in\r
142                 registers, resulting in a good test of the context switch mechanism. */\r
143                 lValue = intgCONST1;\r
144                 lValue += intgCONST2;\r
145 \r
146                 /* Yield in case cooperative scheduling is being used. */\r
147                 #if configUSE_PREEMPTION == 0\r
148                 {\r
149                         taskYIELD();\r
150                 }\r
151                 #endif\r
152 \r
153                 /* Finish off the calculation. */\r
154                 lValue *= intgCONST3;\r
155                 lValue /= intgCONST4;\r
156 \r
157                 /* If the calculation is found to be incorrect we stop setting the \r
158                 TaskHasExecuted variable so the check task can see an error has \r
159                 occurred. */\r
160                 if( lValue != intgEXPECTED_ANSWER ) /*lint !e774 volatile used to prevent this being optimised out. */\r
161                 {\r
162                         sError = pdTRUE;\r
163                 }\r
164 \r
165                 if( sError == pdFALSE )\r
166                 {\r
167                         /* We have not encountered any errors, so set the flag that show\r
168                         we are still executing.  This will be periodically cleared by\r
169                         the check task. */\r
170                         portENTER_CRITICAL();\r
171                                 *pxTaskHasExecuted = pdTRUE;\r
172                         portEXIT_CRITICAL();\r
173                 }\r
174 \r
175                 /* Yield in case cooperative scheduling is being used. */\r
176                 #if configUSE_PREEMPTION == 0\r
177                 {\r
178                         taskYIELD();\r
179                 }\r
180                 #endif\r
181         }\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 /* This is called to check that all the created tasks are still running. */\r
186 portBASE_TYPE xAreIntegerMathsTaskStillRunning( void )\r
187 {\r
188 portBASE_TYPE xReturn = pdTRUE;\r
189 portSHORT sTask;\r
190 \r
191         /* Check the maths tasks are still running by ensuring their check variables \r
192         are still being set to true. */\r
193         for( sTask = 0; sTask < intgNUMBER_OF_TASKS; sTask++ )\r
194         {\r
195                 if( xTaskCheck[ sTask ] == pdFALSE )\r
196                 {\r
197                         /* The check has not incremented so an error exists. */\r
198                         xReturn = pdFALSE;\r
199                 }\r
200 \r
201                 /* Reset the check variable so we can tell if it has been set by\r
202                 the next time around. */\r
203                 xTaskCheck[ sTask ] = pdFALSE;\r
204         }\r
205 \r
206         return xReturn;\r
207 }\r
208 \r