foxBMS  1.1.0
The foxBMS Battery Management System API Documentation
mxm_registry.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 mxm_registry.c
44  * @author foxBMS Team
45  * @date 2020-07-16 (date of creation)
46  * @updated 2021-06-16 (date of last update)
47  * @ingroup DRIVERS
48  * @prefix MXM
49  *
50  * @brief Functions in order to have a registry of monitoring ICs
51  *
52  * @details Monitoring registry stores information about the connected ICs.
53  *
54  */
55 
56 /*========== Includes =======================================================*/
57 #include "mxm_registry.h"
58 
59 /*========== Macros and Definitions =========================================*/
60 
61 /*========== Static Constant and Variable Definitions =======================*/
62 
63 /*========== Extern Constant and Variable Definitions =======================*/
64 
65 /*========== Static Function Prototypes =====================================*/
66 
67 /*========== Static Function Implementations ================================*/
68 
69 /*========== Extern Function Implementations ================================*/
71  FAS_ASSERT(pState != NULL_PTR);
72  for (uint8_t i = 0; i < MXM_MAXIMUM_NR_OF_MODULES; i++) {
73  MXM_REGISTRY_ENTRY_s *entry = &pState->registry[i];
74  entry->connected = false;
75  entry->deviceAddress = 0u;
76  entry->deviceID = 0u;
77  entry->model = MXM_MODEL_ID_NONE;
79  }
80 }
81 
83  FAS_ASSERT(pState != NULL_PTR);
84  STD_RETURN_TYPE_e retval = STD_OK;
85  if (numberOfDevices > MXM_MAXIMUM_NR_OF_MODULES) {
86  retval = STD_NOT_OK;
87  } else {
88  for (uint8_t i = 0; i < numberOfDevices; i++) {
89  pState->registry[i].connected = true;
92  }
93  }
94 
95  return retval;
96 }
97 
99  FAS_ASSERT(kpkState != NULL_PTR);
100  /* return highest connected device */
101  return kpkState->highest5xDevice;
102 }
103 
106  uint8_t rxBufferLength,
107  MXM_REG_NAME_e type) {
108  FAS_ASSERT(pState != NULL_PTR);
109  /* only ID1 or ID2 are valid */
110  FAS_ASSERT((type == MXM_REG_ID1) || (type == MXM_REG_ID2));
111 
112  /* find highest connected device */
113  uint8_t highestConnectedDevice = MXM_MonRegistryGetHighestConnected5XDevice(pState);
114 
115  const uint8_t startBytes = 2u;
116 
117  for (uint8_t i = 0; i <= highestConnectedDevice; i++) {
118  uint8_t bufferPosition = startBytes + (i * 2u);
119  MXM_REGISTRY_ENTRY_s *currentDevice = &pState->registry[highestConnectedDevice - i];
120  FAS_ASSERT((bufferPosition + 1u) <= rxBufferLength);
121  uint16_t id = 0;
123  pState->rxBuffer[bufferPosition], pState->rxBuffer[bufferPosition + 1u], MXM_BM_WHOLE_REG, &id);
124  if (type == MXM_REG_ID1) {
125  currentDevice->deviceID = id;
126  } else {
127  /* intended condition: (type == MXM_REG_ID2) */
128  currentDevice->deviceID = ((uint32_t)id << 16u) | currentDevice->deviceID;
129  }
130  }
131 }
132 
133 extern void MXM_MonRegistryParseVersionIntoDevices(MXM_MONITORING_INSTANCE_s *pState, uint8_t rxBufferLength) {
134  FAS_ASSERT(pState != NULL_PTR);
135 
136  /* find highest connected device */
137  uint8_t highestConnectedDevice = MXM_MonRegistryGetHighestConnected5XDevice(pState);
138 
139  for (uint8_t i = 0; i <= highestConnectedDevice; i++) {
140  const uint8_t startBytes = 2u;
141  uint8_t bufferPosition = startBytes + (i * 2u);
142  MXM_REGISTRY_ENTRY_s *currentDevice = &pState->registry[highestConnectedDevice - i];
143  FAS_ASSERT((bufferPosition + 1u) <= rxBufferLength);
144  uint16_t model = 0;
146  pState->rxBuffer[bufferPosition], pState->rxBuffer[bufferPosition + 1u], MXM_REG_VERSION_MOD, &model);
147  currentDevice->model = (MXM_MODEL_ID_e)model;
148 
149  uint16_t version = 0;
151  pState->rxBuffer[bufferPosition], pState->rxBuffer[bufferPosition + 1u], MXM_REG_VERSION_VER, &version);
152  currentDevice->siliconVersion = (MXM_siliconVersion_e)version;
153  }
154 }
155 
156 /*========== Externalized Static Function Implementations (Unit Test) =======*/
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:237
@ STD_NOT_OK
Definition: fstd_types.h:73
@ STD_OK
Definition: fstd_types.h:72
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:66
enum STD_RETURN_TYPE STD_RETURN_TYPE_e
#define MXM_BM_WHOLE_REG
All bits of monitoring register.
#define MXM_REG_VERSION_VER
Monitoring Register Version/Silicon Version.
#define MXM_REG_VERSION_MOD
Monitoring Register Version/Model.
void MXM_ExtractValueFromRegister(uint8_t lsb, uint8_t msb, MXM_REG_BM bitmask, uint16_t *pValue)
Extract a value from a single register.
enum MXM_MODEL_ID MXM_MODEL_ID_e
Type of monitoring device.
@ MXM_siliconVersion_0
enum MXM_siliconVersion MXM_siliconVersion_e
@ MXM_MODEL_ID_NONE
#define MXM_MAXIMUM_NR_OF_MODULES
Maximum number of modules.
#define HELLOALL_START_SEED
enum MXM_REG_NAME MXM_REG_NAME_e
MAX1785x register names.
@ MXM_REG_ID1
@ MXM_REG_ID2
void MXM_MonRegistryParseIdIntoDevices(MXM_MONITORING_INSTANCE_s *pState, uint8_t rxBufferLength, MXM_REG_NAME_e type)
Parse ID (1 or 2) into the registry.
Definition: mxm_registry.c:104
void MXM_MonRegistryInit(MXM_MONITORING_INSTANCE_s *pState)
Initialize monitoring registry.
Definition: mxm_registry.c:70
STD_RETURN_TYPE_e MXM_MonRegistryConnectDevices(MXM_MONITORING_INSTANCE_s *pState, uint8_t numberOfDevices)
Mark devices as connected in the registry and set the address.
Definition: mxm_registry.c:82
uint8_t MXM_MonRegistryGetHighestConnected5XDevice(const MXM_MONITORING_INSTANCE_s *const kpkState)
Parse number of highest connected device from monitoring- register.
Definition: mxm_registry.c:98
void MXM_MonRegistryParseVersionIntoDevices(MXM_MONITORING_INSTANCE_s *pState, uint8_t rxBufferLength)
Parse Version into the registry.
Definition: mxm_registry.c:133
Functions in order to have a registry of monitoring ICs.
uint8_t rxBuffer[MXM_RX_BUFFER_LENGTH]
MXM_REGISTRY_ENTRY_s registry[MXM_MAXIMUM_NR_OF_MODULES]
uint8_t deviceAddress
MXM_siliconVersion_e siliconVersion
MXM_MODEL_ID_e model
bool connected
uint32_t deviceID