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