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