foxBMS  1.1.1
The foxBMS Battery Management System API Documentation
database_helper.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2021, 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 2021-06-09 (date of last update)
47  * @ingroup ENGINE
48  * @prefix DATA
49  *
50  * @brief Database helper implementation
51  *
52  * @details Implementation of database helper function
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "database_helper.h"
57 
58 #include "battery_system_cfg.h"
59 
60 #include "os.h"
61 
62 /*========== Macros and Definitions =========================================*/
63 
64 /*========== Static Constant and Variable Definitions =======================*/
65 
66 /*========== Extern Constant and Variable Definitions =======================*/
67 
68 /*========== Static Function Prototypes =====================================*/
69 
70 /*========== Static Function Implementations ================================*/
71 
72 /*========== Extern Function Implementations ================================*/
74  bool retval = false;
75  if (!((dataBlockHeader.timestamp == 0u) && (dataBlockHeader.previousTimestamp == 0u))) {
76  /* Only possibility for timestamp AND previous timestamp to be 0 is, if
77  the database entry has never been updated. Thus if this is not the
78  case the database entry must have been updated */
79  retval = true;
80  }
81  return retval;
82 }
83 
84 extern bool DATA_EntryUpdatedWithinInterval(DATA_BLOCK_HEADER_s dataBlockHeader, uint32_t timeInterval) {
85  bool retval = false;
86  uint32_t currentTimestamp = OS_GetTickCount();
87 
88  /* Unsigned integer arithmetic also works correctly if currentTimestap is
89  larger than pHeader->timestamp (timer overflow), thus no need to use abs() */
90  const uint32_t timeDifferenceLastCall = currentTimestamp - dataBlockHeader.timestamp;
91  const bool updatedAtLeastOnce = DATA_DatabaseEntryUpdatedAtLeastOnce(dataBlockHeader);
92  if ((timeDifferenceLastCall <= timeInterval) && (updatedAtLeastOnce == true)) {
93  /* Difference between current timestamp and last update timestamp is
94  smaller than passed time interval */
95  retval = true;
96  }
97  return retval;
98 }
99 
100 extern bool DATA_EntryUpdatedPeriodicallyWithinInterval(DATA_BLOCK_HEADER_s dataBlockHeader, uint32_t timeInterval) {
101  bool retval = false;
102  const uint32_t currentTimestamp = OS_GetTickCount();
103 
104  /* Unsigned integer arithmetic also works correctly if currentTimestap is
105  smaller than dataBlockHeader.timestamp (timer overflow), thus no need to use abs() */
106  const uint32_t timeDifferenceLastCall = currentTimestamp - dataBlockHeader.timestamp;
107  const uint32_t timeDifferenceBetweenCalls = dataBlockHeader.timestamp - dataBlockHeader.previousTimestamp;
108 
109  if ((timeDifferenceLastCall <= timeInterval) && (timeDifferenceBetweenCalls <= timeInterval) &&
110  (DATA_DatabaseEntryUpdatedAtLeastOnce(dataBlockHeader) == true)) {
111  /* Difference between timestamps is smaller than passed time interval */
112  retval = true;
113  }
114  return retval;
115 }
116 
117 extern uint8_t DATA_GetStringNumberFromVoltageIndex(uint16_t cellIndex) {
119  return (uint8_t)(cellIndex / BS_NR_OF_BAT_CELLS);
120 }
121 
122 extern uint8_t DATA_GetModuleNumberFromVoltageIndex(uint16_t cellIndex) {
124  uint8_t stringNumber = DATA_GetStringNumberFromVoltageIndex(cellIndex);
125  uint8_t moduleNumber = (uint8_t)((cellIndex / BS_NR_OF_CELLS_PER_MODULE) - (stringNumber * BS_NR_OF_MODULES));
126  return moduleNumber;
127 }
128 
129 extern uint8_t DATA_GetCellNumberFromVoltageIndex(uint16_t cellIndex) {
131  return (uint8_t)(cellIndex % BS_NR_OF_CELLS_PER_MODULE);
132 }
133 
134 extern uint8_t DATA_GetStringNumberFromTemperatureIndex(uint16_t sensorIndex) {
135  FAS_ASSERT(sensorIndex < BS_NR_OF_TEMP_SENSORS);
136  return (uint8_t)(sensorIndex / BS_NR_OF_TEMP_SENSORS_PER_STRING);
137 }
138 
139 extern uint8_t DATA_GetModuleNumberFromTemperatureIndex(uint16_t sensorIndex) {
140  FAS_ASSERT(sensorIndex < BS_NR_OF_TEMP_SENSORS);
141  uint8_t stringNumber = DATA_GetStringNumberFromTemperatureIndex(sensorIndex);
142  uint8_t moduleNumber =
143  (uint8_t)((sensorIndex / BS_NR_OF_TEMP_SENSORS_PER_MODULE) - (stringNumber * BS_NR_OF_MODULES));
144  return moduleNumber;
145 }
146 
147 extern uint8_t DATA_GetSensorNumberFromTemperatureIndex(uint16_t sensorIndex) {
148  FAS_ASSERT(sensorIndex < BS_NR_OF_TEMP_SENSORS);
149  return (uint8_t)(sensorIndex % BS_NR_OF_TEMP_SENSORS_PER_MODULE);
150 }
151 
152 /*========== Externalized Static Function Implementations (Unit Test) =======*/
Configuration of the battery system (e.g., number of battery modules, battery cells,...
#define BS_NR_OF_STRINGS
#define BS_NR_OF_CELLS_PER_MODULE
number of battery cells per battery module (parallel cells are counted as one)
#define BS_NR_OF_MODULES
number of modules in battery pack
#define BS_NR_OF_TEMP_SENSORS_PER_MODULE
number of temperature sensors per battery module
#define BS_NR_OF_TEMP_SENSORS_PER_STRING
#define BS_NR_OF_BAT_CELLS
#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:237
uint32_t OS_GetTickCount(void)
Returns OS based system tick value.
Definition: os.c:182
Implementation of the tasks used by the system, headers.
uint32_t timestamp
Definition: database_cfg.h:111
uint32_t previousTimestamp
Definition: database_cfg.h:112