]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PPC440_Xilinx_Virtex5_GCC/RTOSDemo/flop/flop-reg-test.c
5361273116f22c192250bb7bbe4acd519fab2d39
[freertos] / FreeRTOS / Demo / PPC440_Xilinx_Virtex5_GCC / RTOSDemo / flop / flop-reg-test.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 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.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29  * Tests the floating point context save and restore mechanism.\r
30  *\r
31  * Two tasks are created - each of which is allocated a buffer of \r
32  * portNO_FLOP_REGISTERS_TO_SAVE 32bit variables into which the flop context\r
33  * of the task is saved when the task is switched out, and from which the\r
34  * flop context of the task is restored when the task is switch in.  Prior to \r
35  * the tasks being created each position in the two buffers is filled with a \r
36  * unique value - this way the flop context of each task is different.\r
37  *\r
38  * The two test tasks never block so are always in either the Running or\r
39  * Ready state.  They execute at the lowest priority so will get pre-empted\r
40  * regularly, although the yield frequently so will not get much execution\r
41  * time.  The lack of execution time is not a problem as its only the \r
42  * switching in and out that is being tested.\r
43  *\r
44  * Whenever a task is moved from the Ready to the Running state its flop \r
45  * context will be loaded from the buffer, but while the task is in the\r
46  * Running state the buffer is not used and can contain any value - in this\r
47  * case and for test purposes the task itself clears the buffer to zero.  \r
48  * The next time the task is moved out of the Running state into the\r
49  * Ready state the flop context will once more get saved to the buffer - \r
50  * overwriting the zeros.\r
51  *\r
52  * Therefore whenever the task is not in the Running state its buffer contains\r
53  * the most recent values of its floating point registers - the zeroing out\r
54  * of the buffer while the task was executing being used to ensure the values \r
55  * the buffer contains are not stale.\r
56  *\r
57  * When neither test task is in the Running state the buffers should contain\r
58  * the unique values allocated before the tasks were created.  If so then\r
59  * the floating point context has been maintained.  This check is performed\r
60  * by the 'check' task (defined in main.c) by calling \r
61  * xAreFlopRegisterTestsStillRunning().\r
62  *\r
63  * The test tasks also increment a value each time they execute.\r
64  * xAreFlopRegisterTestsStillRunning() also checks that this value has changed\r
65  * since it last ran to ensure the test tasks are still getting processing time.\r
66  */\r
67 \r
68 /* Standard includes files. */\r
69 #include <string.h>\r
70 \r
71 /* Scheduler include files. */\r
72 #include "FreeRTOS.h"\r
73 #include "task.h"\r
74 \r
75 /*-----------------------------------------------------------*/\r
76 \r
77 #define flopNUMBER_OF_TASKS             2\r
78 #define flopSTART_VALUE ( 0x1 )\r
79 \r
80 /*-----------------------------------------------------------*/\r
81 \r
82 /* The two test tasks as described at the top of this file. */\r
83 static void vFlopTest1( void *pvParameters );\r
84 static void vFlopTest2( void *pvParameters );\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 /* Buffers into which the flop registers will be saved.  There is a buffer for \r
89 both tasks. */\r
90 static volatile unsigned long ulFlopRegisters[ flopNUMBER_OF_TASKS ][ portNO_FLOP_REGISTERS_TO_SAVE ];\r
91 \r
92 /* Variables that are incremented by the tasks to indicate that they are still\r
93 running. */\r
94 static volatile unsigned long ulFlop1CycleCount = 0, ulFlop2CycleCount = 0;\r
95 \r
96 /*-----------------------------------------------------------*/\r
97 \r
98 void vStartFlopRegTests( void )\r
99 {\r
100 TaskHandle_t xTaskJustCreated;\r
101 unsigned portBASE_TYPE x, y, z = flopSTART_VALUE;\r
102 \r
103         /* Fill the arrays into which the flop registers are to be saved with \r
104         known values.  These are the values that will be written to the flop\r
105         registers when the tasks start, and as the tasks do not perform any\r
106         flop operations the values should never change.  Each position in the\r
107         buffer contains a different value so the flop context of each task\r
108         will be different. */\r
109         for( x = 0; x < flopNUMBER_OF_TASKS; x++ )\r
110         {\r
111                 for( y = 0; y < ( portNO_FLOP_REGISTERS_TO_SAVE - 1); y++ )\r
112                 {\r
113                         ulFlopRegisters[ x ][ y ] = z;\r
114                         z++;\r
115                 }\r
116         }\r
117 \r
118 \r
119         /* Create the first task. */\r
120         xTaskCreate( vFlopTest1, "flop1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xTaskJustCreated );\r
121 \r
122         /* The task     tag value is a value that can be associated with a task, but \r
123         is not used by the scheduler itself.  Its use is down to the application so\r
124         it makes a convenient place in this case to store the pointer to the buffer\r
125         into which the flop context of the task will be stored.  The first created\r
126         task uses ulFlopRegisters[ 0 ], the second ulFlopRegisters[ 1 ]. */\r
127         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 0 ][ 0 ] ) );\r
128 \r
129         /* Do the same for the second task. */\r
130         xTaskCreate( vFlopTest2, "flop2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xTaskJustCreated );\r
131         vTaskSetApplicationTaskTag( xTaskJustCreated, ( void * ) &( ulFlopRegisters[ 1 ][ 0 ] ) );\r
132 }\r
133 /*-----------------------------------------------------------*/\r
134 \r
135 static void vFlopTest1( void *pvParameters )\r
136 {\r
137         /* Just to remove compiler warning. */\r
138         ( void ) pvParameters;\r
139 \r
140         for( ;; )\r
141         {\r
142                 /* The values from the buffer should have now been written to the flop\r
143                 registers.  Clear the buffer to ensure the same values then get written\r
144                 back the next time the task runs.  Being preempted during this memset\r
145                 could cause the test to fail, hence the critical section. */\r
146                 portENTER_CRITICAL();\r
147                         memset( ( void * ) ulFlopRegisters[ 0 ], 0x00, ( portNO_FLOP_REGISTERS_TO_SAVE * sizeof( unsigned portBASE_TYPE ) ) );\r
148                 portEXIT_CRITICAL();\r
149 \r
150                 /* We don't have to do anything other than indicate that we are \r
151                 still running. */\r
152                 ulFlop1CycleCount++;\r
153                 taskYIELD();\r
154         }\r
155 }\r
156 /*-----------------------------------------------------------*/\r
157 \r
158 static void vFlopTest2( void *pvParameters )\r
159 {\r
160         /* Just to remove compiler warning. */\r
161         ( void ) pvParameters;\r
162 \r
163         for( ;; )\r
164         {\r
165                 /* The values from the buffer should have now been written to the flop\r
166                 registers.  Clear the buffer to ensure the same values then get written\r
167                 back the next time the task runs. */\r
168                 portENTER_CRITICAL();\r
169                         memset( ( void * ) ulFlopRegisters[ 1 ], 0x00, ( portNO_FLOP_REGISTERS_TO_SAVE * sizeof( unsigned portBASE_TYPE ) ) );\r
170                 portEXIT_CRITICAL();\r
171 \r
172                 /* We don't have to do anything other than indicate that we are \r
173                 still running. */\r
174                 ulFlop2CycleCount++;\r
175                 taskYIELD();\r
176         }\r
177 }\r
178 /*-----------------------------------------------------------*/\r
179 \r
180 portBASE_TYPE xAreFlopRegisterTestsStillRunning( void )\r
181 {\r
182 portBASE_TYPE xReturn = pdPASS;\r
183 unsigned portBASE_TYPE x, y, z = flopSTART_VALUE;\r
184 static unsigned long ulLastFlop1CycleCount = 0, ulLastFlop2CycleCount = 0;\r
185 \r
186         /* Called from the 'check' task.\r
187         \r
188         The flop tasks cannot be currently running, check their saved registers\r
189         are as expected.  The tests tasks do not perform any flop operations so\r
190         their registers should be as per their initial setting. */\r
191         for( x = 0; x < flopNUMBER_OF_TASKS; x++ )\r
192         {\r
193                 for( y = 0; y < ( portNO_FLOP_REGISTERS_TO_SAVE - 1 ); y++ )\r
194                 {\r
195                         if( ulFlopRegisters[ x ][ y ] != z )\r
196                         {\r
197                                 xReturn = pdFAIL;\r
198                                 break;\r
199                         }\r
200 \r
201                         z++;\r
202                 }\r
203         }\r
204 \r
205         /* Check both tasks have actually been swapped in and out since this function\r
206         last executed. */\r
207         if( ulFlop1CycleCount == ulLastFlop1CycleCount )\r
208         {\r
209                 xReturn = pdFAIL;\r
210         }\r
211 \r
212         if( ulFlop2CycleCount == ulLastFlop2CycleCount )\r
213         {\r
214                 xReturn = pdFAIL;\r
215         }\r
216 \r
217         ulLastFlop1CycleCount = ulFlop1CycleCount;\r
218         ulLastFlop2CycleCount = ulFlop2CycleCount;\r
219 \r
220         return xReturn;\r
221 }\r
222 \r