foxBMS - Unit Tests  1.6.0
The foxBMS Unit Tests API Documentation
database_helper.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2023, 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 database_helper.c
44  * @author foxBMS Team
45  * @date 2021-05-05 (date of creation)
46  * @updated 2023-10-12 (date of last update)
47  * @version v1.6.0
48  * @ingroup ENGINE
49  * @prefix DATA
50  *
51  * @brief Database helper implementation
52  *
53  * @details Implementation of database helper function
54  */
55 
56 /*========== Includes =======================================================*/
57 #include "database_helper.h"
58 
59 #include "battery_system_cfg.h"
60 
61 #include "os.h"
62 
63 #include <stdint.h>
64 
65 /*========== Macros and Definitions =========================================*/
66 
67 /*========== Static Constant and Variable Definitions =======================*/
68 
69 /*========== Extern Constant and Variable Definitions =======================*/
70 
71 /*========== Static Function Prototypes =====================================*/
72 
73 /*========== Static Function Implementations ================================*/
74 
75 /*========== Extern Function Implementations ================================*/
77  bool retval = false;
78  if (!((dataBlockHeader.timestamp == 0u) && (dataBlockHeader.previousTimestamp == 0u))) {
79  /* Only possibility for timestamp AND previous timestamp to be 0 is, if
80  the database entry has never been updated. Thus if this is not the
81  case the database entry must have been updated */
82  retval = true;
83  }
84  return retval;
85 }
86 
87 extern bool DATA_EntryUpdatedWithinInterval(DATA_BLOCK_HEADER_s dataBlockHeader, uint32_t timeInterval) {
88  bool retval = false;
89  uint32_t currentTimestamp = OS_GetTickCount();
90 
91  /* Unsigned integer arithmetic also works correctly if currentTimestamp is
92  larger than pHeader->timestamp (timer overflow), thus no need to use abs() */
93  const uint32_t timeDifferenceLastCall = currentTimestamp - dataBlockHeader.timestamp;
94  const bool updatedAtLeastOnce = DATA_DatabaseEntryUpdatedAtLeastOnce(dataBlockHeader);
95  if ((timeDifferenceLastCall <= timeInterval) && (updatedAtLeastOnce == true)) {
96  /* Difference between current timestamp and last update timestamp is
97  smaller than passed time interval */
98  retval = true;
99  }
100  return retval;
101 }
102 
103 extern bool DATA_EntryUpdatedPeriodicallyWithinInterval(DATA_BLOCK_HEADER_s dataBlockHeader, uint32_t timeInterval) {
104  bool retval = false;
105  const uint32_t currentTimestamp = OS_GetTickCount();
106 
107  /* Unsigned integer arithmetic also works correctly if currentTimestamp is
108  smaller than dataBlockHeader.timestamp (timer overflow), thus no need to use abs() */
109  const uint32_t timeDifferenceLastCall = currentTimestamp - dataBlockHeader.timestamp;
110  const uint32_t timeDifferenceBetweenCalls = dataBlockHeader.timestamp - dataBlockHeader.previousTimestamp;
111 
112  if ((timeDifferenceLastCall <= timeInterval) && (timeDifferenceBetweenCalls <= timeInterval) &&
113  (DATA_DatabaseEntryUpdatedAtLeastOnce(dataBlockHeader) == true)) {
114  /* Difference between timestamps is smaller than passed time interval */
115  retval = true;
116  }
117  return retval;
118 }
119 
120 extern uint8_t DATA_GetStringNumberFromVoltageIndex(uint16_t cellIndex) {
122  return (uint8_t)(cellIndex / BS_NR_OF_CELL_BLOCKS_PER_STRING);
123 }
124 
125 extern uint8_t DATA_GetModuleNumberFromVoltageIndex(uint16_t cellIndex) {
127  uint8_t stringNumber = DATA_GetStringNumberFromVoltageIndex(cellIndex);
128  uint8_t moduleNumber =
129  (uint8_t)((cellIndex / BS_NR_OF_CELL_BLOCKS_PER_MODULE) - (stringNumber * BS_NR_OF_MODULES_PER_STRING));
130  return moduleNumber;
131 }
132 
133 extern uint8_t DATA_GetCellNumberFromVoltageIndex(uint16_t cellIndex) {
135  return (uint8_t)(cellIndex % BS_NR_OF_CELL_BLOCKS_PER_MODULE);
136 }
137 
138 extern uint8_t DATA_GetStringNumberFromTemperatureIndex(uint16_t sensorIndex) {
139  FAS_ASSERT(sensorIndex < BS_NR_OF_TEMP_SENSORS);
140  return (uint8_t)(sensorIndex / BS_NR_OF_TEMP_SENSORS_PER_STRING);
141 }
142 
143 extern uint8_t DATA_GetModuleNumberFromTemperatureIndex(uint16_t sensorIndex) {
144  FAS_ASSERT(sensorIndex < BS_NR_OF_TEMP_SENSORS);
145  uint8_t stringNumber = DATA_GetStringNumberFromTemperatureIndex(sensorIndex);
146  uint8_t moduleNumber =
147  (uint8_t)((sensorIndex / BS_NR_OF_TEMP_SENSORS_PER_MODULE) - (stringNumber * BS_NR_OF_MODULES_PER_STRING));
148  return moduleNumber;
149 }
150 
151 extern uint8_t DATA_GetSensorNumberFromTemperatureIndex(uint16_t sensorIndex) {
152  FAS_ASSERT(sensorIndex < BS_NR_OF_TEMP_SENSORS);
153  return (uint8_t)(sensorIndex % BS_NR_OF_TEMP_SENSORS_PER_MODULE);
154 }
155 
156 /*========== Externalized Static Function Implementations (Unit Test) =======*/
157 #ifdef UNITY_UNIT_TEST
158 #endif
Configuration of the battery system (e.g., number of battery modules, battery cells,...
#define BS_NR_OF_CELL_BLOCKS_PER_MODULE
number of cells per module
#define BS_NR_OF_STRINGS
Number of parallel strings in the battery pack.
#define BS_NR_OF_TEMP_SENSORS_PER_MODULE
number of temperature sensors per battery module
#define BS_NR_OF_CELL_BLOCKS_PER_STRING
#define BS_NR_OF_MODULES_PER_STRING
number of modules in a string
#define BS_NR_OF_TEMP_SENSORS_PER_STRING
#define BS_NR_OF_TEMP_SENSORS
uint8_t DATA_GetSensorNumberFromTemperatureIndex(uint16_t sensorIndex)
Returns sensor number of passed temperature sensor index.
uint8_t DATA_GetModuleNumberFromVoltageIndex(uint16_t cellIndex)
Returns module number of passed cell index.
uint8_t DATA_GetModuleNumberFromTemperatureIndex(uint16_t sensorIndex)
Returns module number of passed temperature sensor index.
uint8_t DATA_GetStringNumberFromVoltageIndex(uint16_t cellIndex)
Returns string number of passed cell index.
uint8_t DATA_GetStringNumberFromTemperatureIndex(uint16_t sensorIndex)
Returns string number of passed temperature sensor index.
bool DATA_DatabaseEntryUpdatedAtLeastOnce(DATA_BLOCK_HEADER_s dataBlockHeader)
Checks if passed database entry has been updated at least once.
bool DATA_EntryUpdatedPeriodicallyWithinInterval(DATA_BLOCK_HEADER_s dataBlockHeader, uint32_t timeInterval)
Checks if passed database entry has been periodically updated within the time interval.
uint8_t DATA_GetCellNumberFromVoltageIndex(uint16_t cellIndex)
Returns cell number of passed cell index.
bool DATA_EntryUpdatedWithinInterval(DATA_BLOCK_HEADER_s dataBlockHeader, uint32_t timeInterval)
Checks if passed database entry has been updated within the last time interval.
Database module header.
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:251
Declaration of the OS wrapper interface.
uint32_t OS_GetTickCount(void)
Returns OS based system tick value.
Definition: os_freertos.c:158
uint32_t previousTimestamp
Definition: database_cfg.h:127