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