]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/integer.c
Next revision of queue set implementation.
[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  * Creates one or more tasks that repeatedly perform a set of integer\r
71  * calculations.  The result of each run-time calculation is compared to the \r
72  * known expected result - with a mismatch being indicative of an error in the\r
73  * context switch mechanism.\r
74  */\r
75 \r
76 #include <stdlib.h>\r
77 \r
78 /* Scheduler include files. */\r
79 #include "FreeRTOS.h"\r
80 #include "task.h"\r
81 \r
82 /* Demo program include files. */\r
83 #include "integer.h"\r
84 \r
85 /* The constants used in the calculation. */\r
86 #define intgCONST1                              ( ( long ) 123 )\r
87 #define intgCONST2                              ( ( long ) 234567 )\r
88 #define intgCONST3                              ( ( long ) -3 )\r
89 #define intgCONST4                              ( ( long ) 7 )\r
90 #define intgEXPECTED_ANSWER             ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )\r
91 \r
92 #define intgSTACK_SIZE                  configMINIMAL_STACK_SIZE\r
93 \r
94 /* As this is the minimal version, we will only create one task. */\r
95 #define intgNUMBER_OF_TASKS             ( 1 )\r
96 \r
97 /* The task function.  Repeatedly performs a 32 bit calculation, checking the\r
98 result against the expected result.  If the result is incorrect then the\r
99 context switch must have caused some corruption. */\r
100 static portTASK_FUNCTION_PROTO( vCompeteingIntMathTask, pvParameters );\r
101 \r
102 /* Variables that are set to true within the calculation task to indicate\r
103 that the task is still executing.  The check task sets the variable back to\r
104 false, flagging an error if the variable is still false the next time it\r
105 is called. */\r
106 static volatile signed portBASE_TYPE xTaskCheck[ intgNUMBER_OF_TASKS ] = { ( signed portBASE_TYPE ) pdFALSE };\r
107 \r
108 /*-----------------------------------------------------------*/\r
109 \r
110 void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority )\r
111 {\r
112 short sTask;\r
113 \r
114         for( sTask = 0; sTask < intgNUMBER_OF_TASKS; sTask++ )\r
115         {\r
116                 xTaskCreate( vCompeteingIntMathTask, ( signed char * ) "IntMath", intgSTACK_SIZE, ( void * ) &( xTaskCheck[ sTask ] ), uxPriority, ( xTaskHandle * ) NULL );\r
117         }\r
118 }\r
119 /*-----------------------------------------------------------*/\r
120 \r
121 static portTASK_FUNCTION( vCompeteingIntMathTask, pvParameters )\r
122 {\r
123 /* These variables are all effectively set to constants so they are volatile to\r
124 ensure the compiler does not just get rid of them. */\r
125 volatile long lValue;\r
126 short sError = pdFALSE;\r
127 volatile signed portBASE_TYPE *pxTaskHasExecuted;\r
128 \r
129         /* Set a pointer to the variable we are going to set to true each\r
130         iteration.  This is also a good test of the parameter passing mechanism\r
131         within each port. */\r
132         pxTaskHasExecuted = ( volatile signed portBASE_TYPE * ) pvParameters;\r
133 \r
134         /* Keep performing a calculation and checking the result against a constant. */\r
135         for( ;; )\r
136         {\r
137                 /* Perform the calculation.  This will store partial value in\r
138                 registers, resulting in a good test of the context switch mechanism. */\r
139                 lValue = intgCONST1;\r
140                 lValue += intgCONST2;\r
141 \r
142                 /* Yield in case cooperative scheduling is being used. */\r
143                 #if configUSE_PREEMPTION == 0\r
144                 {\r
145                         taskYIELD();\r
146                 }\r
147                 #endif\r
148 \r
149                 /* Finish off the calculation. */\r
150                 lValue *= intgCONST3;\r
151                 lValue /= intgCONST4;\r
152 \r
153                 /* If the calculation is found to be incorrect we stop setting the \r
154                 TaskHasExecuted variable so the check task can see an error has \r
155                 occurred. */\r
156                 if( lValue != intgEXPECTED_ANSWER ) /*lint !e774 volatile used to prevent this being optimised out. */\r
157                 {\r
158                         sError = pdTRUE;\r
159                 }\r
160 \r
161                 if( sError == pdFALSE )\r
162                 {\r
163                         /* We have not encountered any errors, so set the flag that show\r
164                         we are still executing.  This will be periodically cleared by\r
165                         the check task. */\r
166                         portENTER_CRITICAL();\r
167                                 *pxTaskHasExecuted = pdTRUE;\r
168                         portEXIT_CRITICAL();\r
169                 }\r
170 \r
171                 /* Yield in case cooperative scheduling is being used. */\r
172                 #if configUSE_PREEMPTION == 0\r
173                 {\r
174                         taskYIELD();\r
175                 }\r
176                 #endif\r
177         }\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 /* This is called to check that all the created tasks are still running. */\r
182 portBASE_TYPE xAreIntegerMathsTaskStillRunning( void )\r
183 {\r
184 portBASE_TYPE xReturn = pdTRUE;\r
185 short sTask;\r
186 \r
187         /* Check the maths tasks are still running by ensuring their check variables \r
188         are still being set to true. */\r
189         for( sTask = 0; sTask < intgNUMBER_OF_TASKS; sTask++ )\r
190         {\r
191                 if( xTaskCheck[ sTask ] == pdFALSE )\r
192                 {\r
193                         /* The check has not incremented so an error exists. */\r
194                         xReturn = pdFALSE;\r
195                 }\r
196 \r
197                 /* Reset the check variable so we can tell if it has been set by\r
198                 the next time around. */\r
199                 xTaskCheck[ sTask ] = pdFALSE;\r
200         }\r
201 \r
202         return xReturn;\r
203 }\r
204 \r