]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/integer.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / Common / Minimal / integer.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30  * Creates one or more tasks that repeatedly perform a set of integer\r
31  * calculations.  The result of each run-time calculation is compared to the\r
32  * known expected result - with a mismatch being indicative of an error in the\r
33  * context switch mechanism.\r
34  */\r
35 \r
36 #include <stdlib.h>\r
37 \r
38 /* Scheduler include files. */\r
39 #include "FreeRTOS.h"\r
40 #include "task.h"\r
41 \r
42 /* Demo program include files. */\r
43 #include "integer.h"\r
44 \r
45 /* The constants used in the calculation. */\r
46 #define intgCONST1                              ( ( long ) 123 )\r
47 #define intgCONST2                              ( ( long ) 234567 )\r
48 #define intgCONST3                              ( ( long ) -3 )\r
49 #define intgCONST4                              ( ( long ) 7 )\r
50 #define intgEXPECTED_ANSWER             ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )\r
51 \r
52 #define intgSTACK_SIZE                  configMINIMAL_STACK_SIZE\r
53 \r
54 /* As this is the minimal version, we will only create one task. */\r
55 #define intgNUMBER_OF_TASKS             ( 1 )\r
56 \r
57 /* The task function.  Repeatedly performs a 32 bit calculation, checking the\r
58 result against the expected result.  If the result is incorrect then the\r
59 context switch must have caused some corruption. */\r
60 static portTASK_FUNCTION_PROTO( vCompeteingIntMathTask, pvParameters );\r
61 \r
62 /* Variables that are set to true within the calculation task to indicate\r
63 that the task is still executing.  The check task sets the variable back to\r
64 false, flagging an error if the variable is still false the next time it\r
65 is called. */\r
66 static BaseType_t xTaskCheck[ intgNUMBER_OF_TASKS ] = { ( BaseType_t ) pdFALSE };\r
67 \r
68 /*-----------------------------------------------------------*/\r
69 \r
70 void vStartIntegerMathTasks( UBaseType_t uxPriority )\r
71 {\r
72 short sTask;\r
73 \r
74         for( sTask = 0; sTask < intgNUMBER_OF_TASKS; sTask++ )\r
75         {\r
76                 xTaskCreate( vCompeteingIntMathTask, "IntMath", intgSTACK_SIZE, ( void * ) &( xTaskCheck[ sTask ] ), uxPriority, ( TaskHandle_t * ) NULL );\r
77         }\r
78 }\r
79 /*-----------------------------------------------------------*/\r
80 \r
81 static portTASK_FUNCTION( vCompeteingIntMathTask, pvParameters )\r
82 {\r
83 /* These variables are all effectively set to constants so they are volatile to\r
84 ensure the compiler does not just get rid of them. */\r
85 volatile long lValue;\r
86 short sError = pdFALSE;\r
87 volatile BaseType_t *pxTaskHasExecuted;\r
88 \r
89         /* Set a pointer to the variable we are going to set to true each\r
90         iteration.  This is also a good test of the parameter passing mechanism\r
91         within each port. */\r
92         pxTaskHasExecuted = ( volatile BaseType_t * ) pvParameters;\r
93 \r
94         /* Keep performing a calculation and checking the result against a constant. */\r
95         for( ;; )\r
96         {\r
97                 /* Perform the calculation.  This will store partial value in\r
98                 registers, resulting in a good test of the context switch mechanism. */\r
99                 lValue = intgCONST1;\r
100                 lValue += intgCONST2;\r
101 \r
102                 /* Yield in case cooperative scheduling is being used. */\r
103                 #if configUSE_PREEMPTION == 0\r
104                 {\r
105                         taskYIELD();\r
106                 }\r
107                 #endif\r
108 \r
109                 /* Finish off the calculation. */\r
110                 lValue *= intgCONST3;\r
111                 lValue /= intgCONST4;\r
112 \r
113                 /* If the calculation is found to be incorrect we stop setting the\r
114                 TaskHasExecuted variable so the check task can see an error has\r
115                 occurred. */\r
116                 if( lValue != intgEXPECTED_ANSWER ) /*lint !e774 volatile used to prevent this being optimised out. */\r
117                 {\r
118                         sError = pdTRUE;\r
119                 }\r
120 \r
121                 if( sError == pdFALSE )\r
122                 {\r
123                         /* We have not encountered any errors, so set the flag that show\r
124                         we are still executing.  This will be periodically cleared by\r
125                         the check task. */\r
126                         portENTER_CRITICAL();\r
127                                 *pxTaskHasExecuted = pdTRUE;\r
128                         portEXIT_CRITICAL();\r
129                 }\r
130 \r
131                 /* Yield in case cooperative scheduling is being used. */\r
132                 #if configUSE_PREEMPTION == 0\r
133                 {\r
134                         taskYIELD();\r
135                 }\r
136                 #endif\r
137         }\r
138 }\r
139 /*-----------------------------------------------------------*/\r
140 \r
141 /* This is called to check that all the created tasks are still running. */\r
142 BaseType_t xAreIntegerMathsTaskStillRunning( void )\r
143 {\r
144 BaseType_t xReturn = pdTRUE;\r
145 short sTask;\r
146 \r
147         /* Check the maths tasks are still running by ensuring their check variables\r
148         are still being set to true. */\r
149         for( sTask = 0; sTask < intgNUMBER_OF_TASKS; sTask++ )\r
150         {\r
151                 if( xTaskCheck[ sTask ] == pdFALSE )\r
152                 {\r
153                         /* The check has not incremented so an error exists. */\r
154                         xReturn = pdFALSE;\r
155                 }\r
156 \r
157                 /* Reset the check variable so we can tell if it has been set by\r
158                 the next time around. */\r
159                 xTaskCheck[ sTask ] = pdFALSE;\r
160         }\r
161 \r
162         return xReturn;\r
163 }\r
164 \r