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