foxBMS  1.6.0
The foxBMS Battery Management System API Documentation
adi_ades183x_helpers.h
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 adi_ades183x_helpers.h
44  * @author foxBMS Team
45  * @date 2022-12-06 (date of creation)
46  * @updated 2023-10-12 (date of last update)
47  * @version v1.6.0
48  * @ingroup DRIVERS
49  * @prefix ADI
50  *
51  * @brief Headers for the diagnostic driver for the ADI analog front-end.
52  *
53  */
54 
55 #ifndef FOXBMS__ADI_ADES183X_HELPERS_H_
56 #define FOXBMS__ADI_ADES183X_HELPERS_H_
57 
58 /*========== Includes =======================================================*/
59 /* clang-format off */
60 #include "adi_ades183x_cfg.h"
61 /* clang-format on */
62 
63 #include "adi_ades183x_defs.h"
64 
65 #include <stdint.h>
66 
67 /*========== Macros and Definitions =========================================*/
68 
69 /*========== Extern Constant and Variable Declarations ======================*/
70 
71 /*========== Extern Function Prototypes =====================================*/
72 /**
73  * @brief send command to the ades183x daisy-chain (e.g., start voltage
74  * measurement).
75  * @param command command to be sent
76  * @param adiState state of the driver
77  */
78 extern void ADI_TransmitCommand(uint16_t *command, ADI_STATE_s *adiState);
79 
80 /**
81  * @brief copy command bits in variables.
82  * @details The commands are defined as const. As some of them have
83  * configuration bits, they are first copied in a non-const variable.
84  * They can then be configured with
85  * ADI_WriteCommandConfigurationBits().
86  * @param sourceCommand command definition from command list
87  * @param destinationCommand copied command bits from command definition
88  */
89 extern void ADI_CopyCommandBits(const uint16_t *sourceCommand, uint16_t *destinationCommand);
90 
91 /**
92  * @brief write configuration bits in commands (e.g., channel in ADAX command).
93  * @param configuredCommand command in which bits are configured
94  * @param position start bit of configuration field to write
95  * @param length length of configuration field to write
96  * @param configuration configuration to write to the configuration field
97  */
99  uint16_t *configuredCommand,
100  uint8_t position,
101  uint8_t length,
102  uint16_t configuration);
103 
104 /**
105  * @brief Helper function to read specific bits from received data.
106  * @param receivedData data received from the AFEs
107  * @param pDataToRead specific bits read from received data
108  * @param position position where to read the bits in received data
109  * @param mask mask corresponding to bits to read
110  */
111 extern void ADI_ReadDataBits(uint8_t receivedData, uint8_t *pDataToRead, uint8_t position, uint8_t mask);
112 
113 /**
114  * @brief send command to the ades183x daisy-chain to read a register.
115  * @details This is the core function to receive data from the ades183x
116  * daisy-chain.
117  * A 2 byte command is sent with the corresponding PEC.
118  * Example: Read configuration register (RDCFG).
119  * The data send is:
120  * 2 bytes (COMMAND) 2 bytes (PEC)
121  * The data received is:
122  * 6 bytes (IC1) 2 bytes (PEC) + 6 bytes (IC2) 2 bytes (PEC)
123  * + 6 bytes (IC3) 2 bytes (PEC)
124  * + ...
125  * + 6 bytes (IC{ADI_N_ADI}) 2 bytes (PEC)
126  * The function does not check the PECs. This has to be done
127  * elsewhere.
128  * @param registerToRead register to be read
129  * @param data read data
130  * @param adiState state of the driver
131  */
132 extern void ADI_ReadRegister(uint16_t *registerToRead, uint8_t *data, ADI_STATE_s *adiState);
133 
134 /**
135  * @brief Sends data in the configuration stored in driver to all AFEs in the
136  * daisy chain, for all configuration registers.
137  * @param adiState state of the driver
138  */
140 
141 /**
142  * @brief Compares the configuration stored in the AFE driver with
143  * the configuration read from the AFEs.
144  * @details If the stored and read configuration match, the corresponding flag
145  * is set to true in the error table, otherwise it is set to false.
146  * @param registerSet configuration register to check
147  * @param adiState state of the driver
148  * @return STD_OK if check was OK, STD_NOT_OK otherwise
149  */
151 
152 /**
153  * @brief Sends data in the configuration stored in driver to all AFEs in the
154  * daisy chain, for a specific configuration register.
155  * @param registerSet configuration register to write
156  * @param adiState state of the driver
157  * @return STD_OK if check made after writing was OK, STD_NOT_OK otherwise
158  */
160 
161 /**
162  * @brief Write the same 6 bytes to all devices in the daisy-chain.
163  * @param registerToWrite register to write
164  * @param data data to write in register
165  * @param pecFaultInjection used for diagnostics to inject faults in PEC
166  * @param adiState state of the driver
167  */
168 extern void ADI_WriteRegisterGlobal(
169  const uint16_t *registerToWrite,
170  uint8_t *data,
171  ADI_PEC_FAULT_INJECTION_e pecFaultInjection,
172  ADI_STATE_s *adiState);
173 
174 /**
175  * @brief Send RSTCC to AFEs in daisy-chain and clear command counter of AFE driver.
176  * @param adiState state of the driver
177  */
178 extern void ADI_ClearCommandCounter(ADI_STATE_s *adiState);
179 
180 /**
181  * @brief Transmits and receives data on SPI.
182  * @param adiState state of the ADI driver
183  * @param pTxBuff pointer to data that is transmitted by the SPI interface
184  * @param pRxBuff pointer to data that is received by the SPI interface
185  * @param frameLength number of bytes to be transmitted by the SPI interface
186  */
187 extern void ADI_SpiTransmitReceiveData(
188  ADI_STATE_s *adiState,
189  uint16_t *pTxBuff,
190  uint16_t *pRxBuff,
191  uint32_t frameLength);
192 
193 /**
194  * @brief Writes data in the configuration stored in AFE driver,
195  * for all AFEs in the daisy-chain.
196  * @details Does the same as ADI_StoredConfigurationFillRegisterData(), but
197  * for all AFEs in the daisy chain.
198  * @param registerSet configuration register to address
199  * @param registerOffset position of byte to address in configuration register
200  * @param data data to write to byte in register
201  * @param position position where to store the data in byte
202  * @param mask mask corresponding to data to store
203  * @param adiState state of the driver
204  */
206  ADI_CFG_REGISTER_SET_e registerSet,
207  uint8_t registerOffset,
208  uint8_t data,
209  uint8_t position,
210  uint8_t mask,
211  ADI_STATE_s *adiState);
212 
213 /**
214  * @brief wait for a certain number of milliseconds
215  * @details This function is used to wait for a definite amount of time.
216  * In foxBMS, block task running ades183x.
217  * The task cannot be woken up before the time has elapsed.
218  * @param waitTime time in ms during which task will be blocked
219  */
220 extern void ADI_Wait(uint32_t waitTime);
221 
222 /**
223  * @brief Helper function to write specific bits in data to be sent.
224  * @param pSentData data to send to the AFEs
225  * @param dataToWrite specific bits to write in data to be sent
226  * @param position position where to write the bits in data to send
227  * @param mask mask corresponding to bits to write
228  */
229 extern void ADI_WriteDataBits(uint8_t *pSentData, uint8_t dataToWrite, uint8_t position, uint8_t mask);
230 
231 /**
232  * @brief sends command to the ades183x daisy-chain to write data.
233  * @details This is the core function to transmit data to the ades183x
234  * daisy-chain.
235  * The data sent is:
236  * COMMAND + 6 bytes (IC1) + 6 bytes (IC2)
237  * + 6 bytes (IC3)
238  * + ...
239  * + 6 bytes (IC{ADI_N_ADI})
240  * A 2 byte command is sent with the corresponding PEC.
241  * Example: write configuration register (WRCFG).
242  * The command has to be set and then the function calculates the PEC
243  * automatically.
244  * The function calculates the needed PEC to send the data to the
245  * daisy-chain. The sent data has the format:
246  * 2 byte-COMMAND (2 bytes PEC) + 6 bytes (IC1) (2 bytes PEC)
247  * + 6 bytes (IC2) (2 bytes PEC)
248  * + 6 bytes (IC3) (2 bytes PEC)
249  * + ...
250  * + 6 bytes (IC{ADI_N_ADI}) (2 bytes PEC)
251  * The pecFaultInjection parameters is used to inject faults in the
252  * command PEC or in the data PEC. It is used for diagnostics.
253  * The default value is ADI_PEC_NO_FAULT_INJECTION, in this case the
254  * correct PECs are transmitted.
255  * @param registerToWrite register to write
256  * @param data data to write in register
257  * @param adiState state of the driver
258  * @param pecFaultInjection used for diagnostics to inject faults in PEC
259  */
260 extern void ADI_WriteRegister(
261  const uint16_t *registerToWrite,
262  uint8_t *data,
263  ADI_PEC_FAULT_INJECTION_e pecFaultInjection,
264  ADI_STATE_s *adiState);
265 
266 /*========== Externalized Static Functions Prototypes (Unit Test) ===========*/
267 #ifdef UNITY_UNIT_TEST
268 extern void TEST_ADI_StoredConfigurationFillRegisterData(
269  uint8_t module,
270  ADI_CFG_REGISTER_SET_e registerSet,
271  uint8_t registerOffset,
272  uint8_t data,
273  uint8_t position,
274  uint8_t mask,
275  ADI_STATE_s *adiState);
276 extern void TEST_ADI_IncrementCommandCounter(ADI_STATE_s *adiState);
277 #endif
278 
279 #endif /* FOXBMS__ADI_ADES183X_HELPERS_H_ */
Header for the configuration for the ADI analog front-end.
Headers for the driver for the ades183x analog front-end.
ADI_CFG_REGISTER_SET_e
ADI_PEC_FAULT_INJECTION_e
void ADI_WriteCommandConfigurationBits(uint16_t *configuredCommand, uint8_t position, uint8_t length, uint16_t configuration)
write configuration bits in commands (e.g., channel in ADAX command).
void ADI_WriteDataBits(uint8_t *pSentData, uint8_t dataToWrite, uint8_t position, uint8_t mask)
Helper function to write specific bits in data to be sent.
void ADI_ReadDataBits(uint8_t receivedData, uint8_t *pDataToRead, uint8_t position, uint8_t mask)
Helper function to read specific bits from received data.
void ADI_ReadRegister(uint16_t *registerToRead, uint8_t *data, ADI_STATE_s *adiState)
send command to the ades183x daisy-chain to read a register.
void ADI_ClearCommandCounter(ADI_STATE_s *adiState)
Send RSTCC to AFEs in daisy-chain and clear command counter of AFE driver.
void ADI_WriteRegister(const uint16_t *registerToWrite, uint8_t *data, ADI_PEC_FAULT_INJECTION_e pecFaultInjection, ADI_STATE_s *adiState)
sends command to the ades183x daisy-chain to write data.
STD_RETURN_TYPE_e ADI_CheckConfigurationRegister(ADI_CFG_REGISTER_SET_e registerSet, ADI_STATE_s *adiState)
Compares the configuration stored in the AFE driver with the configuration read from the AFEs.
void ADI_StoredConfigurationWriteToAfeGlobal(ADI_STATE_s *adiState)
Sends data in the configuration stored in driver to all AFEs in the daisy chain, for all configuratio...
void ADI_StoredConfigurationFillRegisterDataGlobal(ADI_CFG_REGISTER_SET_e registerSet, uint8_t registerOffset, uint8_t data, uint8_t position, uint8_t mask, ADI_STATE_s *adiState)
Writes data in the configuration stored in AFE driver, for all AFEs in the daisy-chain.
void ADI_SpiTransmitReceiveData(ADI_STATE_s *adiState, uint16_t *pTxBuff, uint16_t *pRxBuff, uint32_t frameLength)
Transmits and receives data on SPI.
void ADI_WriteRegisterGlobal(const uint16_t *registerToWrite, uint8_t *data, ADI_PEC_FAULT_INJECTION_e pecFaultInjection, ADI_STATE_s *adiState)
Write the same 6 bytes to all devices in the daisy-chain.
STD_RETURN_TYPE_e ADI_StoredConfigurationWriteToAfe(ADI_CFG_REGISTER_SET_e registerSet, ADI_STATE_s *adiState)
Sends data in the configuration stored in driver to all AFEs in the daisy chain, for a specific confi...
void ADI_TransmitCommand(uint16_t *command, ADI_STATE_s *adiState)
send command to the ades183x daisy-chain (e.g., start voltage measurement).
void ADI_Wait(uint32_t waitTime)
wait for a certain number of milliseconds
void ADI_CopyCommandBits(const uint16_t *sourceCommand, uint16_t *destinationCommand)
copy command bits in variables.
STD_RETURN_TYPE_e
Definition: fstd_types.h:82