foxBMS - Unit Tests  1.4.1
The foxBMS Unit Tests API Documentation
algorithm.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2022, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * We kindly request you to use one or more of the following phrases to refer to
34  * foxBMS in your hardware, software, documentation or advertising materials:
35  *
36  * - ″This product uses parts of foxBMS®″
37  * - ″This product includes parts of foxBMS®″
38  * - ″This product is derived from foxBMS®″
39  *
40  */
41 
42 /**
43  * @file algorithm.c
44  * @author foxBMS Team
45  * @date 2017-12-18 (date of creation)
46  * @updated 2022-10-27 (date of last update)
47  * @version v1.4.1
48  * @ingroup ALGORITHMS
49  * @prefix ALGO
50  *
51  * @brief Main module to handle the execution of algorithms
52  *
53  *
54  */
55 
56 /*========== Includes =======================================================*/
57 #include "algorithm.h"
58 
59 #include "os.h"
60 
61 /*========== Macros and Definitions =========================================*/
62 
63 /*========== Static Constant and Variable Definitions =======================*/
64 /**
65  * This is a signal that skips initialization of #ALGO_Initialization()
66  * until it has been requested.
67  */
68 static bool algo_initializationRequested = false;
69 
70 /*========== Extern Constant and Variable Definitions =======================*/
71 
72 /*========== Static Function Prototypes =====================================*/
73 /**
74  * @brief initializes local variables and module internals needed to use the
75  * algorithm module
76  */
77 static void ALGO_Initialization(void);
78 
79 /*========== Static Function Implementations ================================*/
80 static void ALGO_Initialization(void) {
81  /* iterate over all algorithms */
82  for (uint16_t i = 0u; i < algo_length; i++) {
83  /* check if the cycle time is valid */
84  FAS_ASSERT((algo_algorithms[i].cycleTime_ms % ALGO_TICK_ms) == 0u);
85 
86  /* check only uninitialized algorithms */
87  if (algo_algorithms[i].state == ALGO_UNINITIALIZED) {
88  /* directly make ready when init function is a null pointer otherwise run init */
89  if (algo_algorithms[i].fpInitialization == NULL_PTR) {
91  } else {
93  FAS_ASSERT((STD_OK == result) || (STD_NOT_OK == result));
94  if (STD_OK == result) {
96  } else {
98  }
99  }
100  }
101  }
102 }
103 
104 /*========== Extern Function Implementations ================================*/
105 
106 extern void ALGO_UnlockInitialization(void) {
110 }
111 
112 extern void ALGO_MainFunction(void) {
114  const bool initializationRequested = algo_initializationRequested;
116  if (initializationRequested == true) {
121  }
122 
123  static uint32_t counter_ticks = 0u;
124 
125  for (uint16_t i = 0u; i < algo_length; i++) {
126  const bool runAlgorithmAsap = (algo_algorithms[i].cycleTime_ms == 0u);
127  const bool runAlgorithmCycleElapsed =
128  ((algo_algorithms[i].cycleTime_ms != 0u) && ((counter_ticks % algo_algorithms[i].cycleTime_ms) == 0u));
129  if ((runAlgorithmAsap != false) || (runAlgorithmCycleElapsed != false)) {
130  /* Cycle time elapsed -> call function */
131  if (algo_algorithms[i].state == ALGO_READY) {
132  /* Set state to running -> reset to READY before leaving algo function */
136  ALGO_MarkAsDone(i);
137  }
138  /* check if we need to reinit */
139  if (algo_algorithms[i].state == ALGO_REINIT_REQUESTED) {
140  /* set to uninitialized so that the algorithm can be reinitialized */
142 
144  }
145  }
146  }
147 
148  counter_ticks += ALGO_TICK_ms;
149 }
150 
151 extern void ALGO_MonitorExecutionTime(void) {
152  const uint32_t timestamp = OS_GetTickCount();
153 
154  for (uint16_t i = 0u; i < algo_length; i++) {
155  if ((algo_algorithms[i].startTime != 0u) && (algo_algorithms[i].state == ALGO_RUNNING) &&
156  ((algo_algorithms[i].startTime + algo_algorithms[i].maxCalculationDuration_ms) < timestamp)) {
157  /* Block task from further execution because of runtime violation, but task will finish its execution */
159 
160  /* TODO: Add diag call to notify error in algorithm module */
161  }
162  }
163 }
164 
165 /*========== Externalized Static Function Implementations (Unit Test) =======*/
166 #ifdef UNITY_UNIT_TEST
169 }
170 #endif /* UNITY_UNIT_TEST */
void ALGO_UnlockInitialization(void)
Calling this function sets a signal that lets ALGO_Initialization() know that the initialization has ...
Definition: algorithm.c:106
void TEST_ALGO_ResetInitializationRequest()
Definition: algorithm.c:167
static void ALGO_Initialization(void)
initializes local variables and module internals needed to use the algorithm module
Definition: algorithm.c:80
void ALGO_MainFunction(void)
handles the call of different algorithm functions when cycle time has expired
Definition: algorithm.c:112
void ALGO_MonitorExecutionTime(void)
monitors the calculation duration of the different algorithms
Definition: algorithm.c:151
static bool algo_initializationRequested
Definition: algorithm.c:68
Headers for the driver for the storage in the EEPROM memory.
const uint16_t algo_length
Definition: algorithm_cfg.c:71
ALGO_TASKS_s algo_algorithms[]
Definition: algorithm_cfg.c:67
void ALGO_MarkAsDone(uint32_t algorithmIndex)
Mark the current algorithm as done (will reset to ALGO_READY if possible)
Definition: algorithm_cfg.c:79
@ ALGO_UNINITIALIZED
Definition: algorithm_cfg.h:82
@ ALGO_REINIT_REQUESTED
Definition: algorithm_cfg.h:88
@ ALGO_RUNNING
Definition: algorithm_cfg.h:84
@ ALGO_FAILED_INIT
Definition: algorithm_cfg.h:87
@ ALGO_READY
Definition: algorithm_cfg.h:83
@ ALGO_BLOCKED
Definition: algorithm_cfg.h:86
#define ALGO_TICK_ms
Definition: algorithm_cfg.h:67
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:248
STD_RETURN_TYPE_e
Definition: fstd_types.h:81
@ STD_NOT_OK
Definition: fstd_types.h:83
@ STD_OK
Definition: fstd_types.h:82
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:76
Declaration of the OS wrapper interface.
void OS_ExitTaskCritical(void)
Exit Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:135
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:131
uint32_t OS_GetTickCount(void)
Returns OS based system tick value.
Definition: os_freertos.c:139
uint32_t cycleTime_ms
Definition: algorithm_cfg.h:94
uint32_t startTime
Definition: algorithm_cfg.h:96
ALGO_COMPUTATION_FUNCTION_f * fpAlgorithm
Definition: algorithm_cfg.h:99
ALGO_INITIALIZATION_FUNCTION_f * fpInitialization
Definition: algorithm_cfg.h:97
ALGO_STATE_e state
Definition: algorithm_cfg.h:93