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