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