foxBMS - Unit Tests  1.4.0
The foxBMS Unit Tests API Documentation
database_helper.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 database_helper.c
44  * @author foxBMS Team
45  * @date 2021-05-05 (date of creation)
46  * @updated 2022-07-28 (date of last update)
47  * @version v1.4.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 /*========== Macros and Definitions =========================================*/
64 
65 /*========== Static Constant and Variable Definitions =======================*/
66 
67 /*========== Extern Constant and Variable Definitions =======================*/
68 
69 /*========== Static Function Prototypes =====================================*/
70 
71 /*========== Static Function Implementations ================================*/
72 
73 /*========== Extern Function Implementations ================================*/
75  bool retval = false;
76  if (!((dataBlockHeader.timestamp == 0u) && (dataBlockHeader.previousTimestamp == 0u))) {
77  /* Only possibility for timestamp AND previous timestamp to be 0 is, if
78  the database entry has never been updated. Thus if this is not the
79  case the database entry must have been updated */
80  retval = true;
81  }
82  return retval;
83 }
84 
85 extern bool DATA_EntryUpdatedWithinInterval(DATA_BLOCK_HEADER_s dataBlockHeader, uint32_t timeInterval) {
86  bool retval = false;
87  uint32_t currentTimestamp = OS_GetTickCount();
88 
89  /* Unsigned integer arithmetic also works correctly if currentTimestamp is
90  larger than pHeader->timestamp (timer overflow), thus no need to use abs() */
91  const uint32_t timeDifferenceLastCall = currentTimestamp - dataBlockHeader.timestamp;
92  const bool updatedAtLeastOnce = DATA_DatabaseEntryUpdatedAtLeastOnce(dataBlockHeader);
93  if ((timeDifferenceLastCall <= timeInterval) && (updatedAtLeastOnce == true)) {
94  /* Difference between current timestamp and last update timestamp is
95  smaller than passed time interval */
96  retval = true;
97  }
98  return retval;
99 }
100 
101 extern bool DATA_EntryUpdatedPeriodicallyWithinInterval(DATA_BLOCK_HEADER_s dataBlockHeader, uint32_t timeInterval) {
102  bool retval = false;
103  const uint32_t currentTimestamp = OS_GetTickCount();
104 
105  /* Unsigned integer arithmetic also works correctly if currentTimestamp is
106  smaller than dataBlockHeader.timestamp (timer overflow), thus no need to use abs() */
107  const uint32_t timeDifferenceLastCall = currentTimestamp - dataBlockHeader.timestamp;
108  const uint32_t timeDifferenceBetweenCalls = dataBlockHeader.timestamp - dataBlockHeader.previousTimestamp;
109 
110  if ((timeDifferenceLastCall <= timeInterval) && (timeDifferenceBetweenCalls <= timeInterval) &&
111  (DATA_DatabaseEntryUpdatedAtLeastOnce(dataBlockHeader) == true)) {
112  /* Difference between timestamps is smaller than passed time interval */
113  retval = true;
114  }
115  return retval;
116 }
117 
118 extern uint8_t DATA_GetStringNumberFromVoltageIndex(uint16_t cellIndex) {
120  return (uint8_t)(cellIndex / BS_NR_OF_CELL_BLOCKS_PER_STRING);
121 }
122 
123 extern uint8_t DATA_GetModuleNumberFromVoltageIndex(uint16_t cellIndex) {
125  uint8_t stringNumber = DATA_GetStringNumberFromVoltageIndex(cellIndex);
126  uint8_t moduleNumber =
127  (uint8_t)((cellIndex / BS_NR_OF_CELL_BLOCKS_PER_MODULE) - (stringNumber * BS_NR_OF_MODULES_PER_STRING));
128  return moduleNumber;
129 }
130 
131 extern uint8_t DATA_GetCellNumberFromVoltageIndex(uint16_t cellIndex) {
133  return (uint8_t)(cellIndex % BS_NR_OF_CELL_BLOCKS_PER_MODULE);
134 }
135 
136 extern uint8_t DATA_GetStringNumberFromTemperatureIndex(uint16_t sensorIndex) {
137  FAS_ASSERT(sensorIndex < BS_NR_OF_TEMP_SENSORS);
138  return (uint8_t)(sensorIndex / BS_NR_OF_TEMP_SENSORS_PER_STRING);
139 }
140 
141 extern uint8_t DATA_GetModuleNumberFromTemperatureIndex(uint16_t sensorIndex) {
142  FAS_ASSERT(sensorIndex < BS_NR_OF_TEMP_SENSORS);
143  uint8_t stringNumber = DATA_GetStringNumberFromTemperatureIndex(sensorIndex);
144  uint8_t moduleNumber =
145  (uint8_t)((sensorIndex / BS_NR_OF_TEMP_SENSORS_PER_MODULE) - (stringNumber * BS_NR_OF_MODULES_PER_STRING));
146  return moduleNumber;
147 }
148 
149 extern uint8_t DATA_GetSensorNumberFromTemperatureIndex(uint16_t sensorIndex) {
150  FAS_ASSERT(sensorIndex < BS_NR_OF_TEMP_SENSORS);
151  return (uint8_t)(sensorIndex % BS_NR_OF_TEMP_SENSORS_PER_MODULE);
152 }
153 
154 /*========== Externalized Static Function Implementations (Unit Test) =======*/
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:248
Declaration of the OS wrapper interface.
uint32_t OS_GetTickCount(void)
Returns OS based system tick value.
Definition: os_freertos.c:139
uint32_t previousTimestamp
Definition: database_cfg.h:121