]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/integer.c
287581defdcc15c7cee8c45632c31185905e8c64
[freertos] / FreeRTOS / Demo / Common / Minimal / integer.c
1 /*\r
2     FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /*\r
66  * Creates one or more tasks that repeatedly perform a set of integer\r
67  * calculations.  The result of each run-time calculation is compared to the \r
68  * known expected result - with a mismatch being indicative of an error in the\r
69  * context switch mechanism.\r
70  */\r
71 \r
72 #include <stdlib.h>\r
73 \r
74 /* Scheduler include files. */\r
75 #include "FreeRTOS.h"\r
76 #include "task.h"\r
77 \r
78 /* Demo program include files. */\r
79 #include "integer.h"\r
80 \r
81 /* The constants used in the calculation. */\r
82 #define intgCONST1                              ( ( long ) 123 )\r
83 #define intgCONST2                              ( ( long ) 234567 )\r
84 #define intgCONST3                              ( ( long ) -3 )\r
85 #define intgCONST4                              ( ( long ) 7 )\r
86 #define intgEXPECTED_ANSWER             ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )\r
87 \r
88 #define intgSTACK_SIZE                  configMINIMAL_STACK_SIZE\r
89 \r
90 /* As this is the minimal version, we will only create one task. */\r
91 #define intgNUMBER_OF_TASKS             ( 1 )\r
92 \r
93 /* The task function.  Repeatedly performs a 32 bit calculation, checking the\r
94 result against the expected result.  If the result is incorrect then the\r
95 context switch must have caused some corruption. */\r
96 static portTASK_FUNCTION_PROTO( vCompeteingIntMathTask, pvParameters );\r
97 \r
98 /* Variables that are set to true within the calculation task to indicate\r
99 that the task is still executing.  The check task sets the variable back to\r
100 false, flagging an error if the variable is still false the next time it\r
101 is called. */\r
102 static volatile signed portBASE_TYPE xTaskCheck[ intgNUMBER_OF_TASKS ] = { ( signed portBASE_TYPE ) pdFALSE };\r
103 \r
104 /*-----------------------------------------------------------*/\r
105 \r
106 void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority )\r
107 {\r
108 short sTask;\r
109 \r
110         for( sTask = 0; sTask < intgNUMBER_OF_TASKS; sTask++ )\r
111         {\r
112                 xTaskCreate( vCompeteingIntMathTask, ( signed char * ) "IntMath", intgSTACK_SIZE, ( void * ) &( xTaskCheck[ sTask ] ), uxPriority, ( xTaskHandle * ) NULL );\r
113         }\r
114 }\r
115 /*-----------------------------------------------------------*/\r
116 \r
117 static portTASK_FUNCTION( vCompeteingIntMathTask, pvParameters )\r
118 {\r
119 /* These variables are all effectively set to constants so they are volatile to\r
120 ensure the compiler does not just get rid of them. */\r
121 volatile long lValue;\r
122 short sError = pdFALSE;\r
123 volatile signed portBASE_TYPE *pxTaskHasExecuted;\r
124 \r
125         /* Set a pointer to the variable we are going to set to true each\r
126         iteration.  This is also a good test of the parameter passing mechanism\r
127         within each port. */\r
128         pxTaskHasExecuted = ( volatile signed portBASE_TYPE * ) pvParameters;\r
129 \r
130         /* Keep performing a calculation and checking the result against a constant. */\r
131         for( ;; )\r
132         {\r
133                 /* Perform the calculation.  This will store partial value in\r
134                 registers, resulting in a good test of the context switch mechanism. */\r
135                 lValue = intgCONST1;\r
136                 lValue += intgCONST2;\r
137 \r
138                 /* Yield in case cooperative scheduling is being used. */\r
139                 #if configUSE_PREEMPTION == 0\r
140                 {\r
141                         taskYIELD();\r
142                 }\r
143                 #endif\r
144 \r
145                 /* Finish off the calculation. */\r
146                 lValue *= intgCONST3;\r
147                 lValue /= intgCONST4;\r
148 \r
149                 /* If the calculation is found to be incorrect we stop setting the \r
150                 TaskHasExecuted variable so the check task can see an error has \r
151                 occurred. */\r
152                 if( lValue != intgEXPECTED_ANSWER ) /*lint !e774 volatile used to prevent this being optimised out. */\r
153                 {\r
154                         sError = pdTRUE;\r
155                 }\r
156 \r
157                 if( sError == pdFALSE )\r
158                 {\r
159                         /* We have not encountered any errors, so set the flag that show\r
160                         we are still executing.  This will be periodically cleared by\r
161                         the check task. */\r
162                         portENTER_CRITICAL();\r
163                                 *pxTaskHasExecuted = pdTRUE;\r
164                         portEXIT_CRITICAL();\r
165                 }\r
166 \r
167                 /* Yield in case cooperative scheduling is being used. */\r
168                 #if configUSE_PREEMPTION == 0\r
169                 {\r
170                         taskYIELD();\r
171                 }\r
172                 #endif\r
173         }\r
174 }\r
175 /*-----------------------------------------------------------*/\r
176 \r
177 /* This is called to check that all the created tasks are still running. */\r
178 portBASE_TYPE xAreIntegerMathsTaskStillRunning( void )\r
179 {\r
180 portBASE_TYPE xReturn = pdTRUE;\r
181 short sTask;\r
182 \r
183         /* Check the maths tasks are still running by ensuring their check variables \r
184         are still being set to true. */\r
185         for( sTask = 0; sTask < intgNUMBER_OF_TASKS; sTask++ )\r
186         {\r
187                 if( xTaskCheck[ sTask ] == pdFALSE )\r
188                 {\r
189                         /* The check has not incremented so an error exists. */\r
190                         xReturn = pdFALSE;\r
191                 }\r
192 \r
193                 /* Reset the check variable so we can tell if it has been set by\r
194                 the next time around. */\r
195                 xTaskCheck[ sTask ] = pdFALSE;\r
196         }\r
197 \r
198         return xReturn;\r
199 }\r
200 \r