foxBMS - Unit Tests  1.4.0
The foxBMS Unit Tests API Documentation
mxm_17852.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 mxm_17852.c
44  * @author foxBMS Team
45  * @date 2021-11-24 (date of creation)
46  * @updated 2022-07-28 (date of last update)
47  * @version v1.4.0
48  * @ingroup DRIVERS
49  * @prefix MXM
50  *
51  * @brief Operation state machine implementation for the MAX17852
52  *
53  * @details This file contains that part of the state machine that is executed
54  * during the operation state. It is adapted for MAX17852.
55  *
56  */
57 
58 /*========== Includes =======================================================*/
59 /* clang-format off */
60 #include "mxm_1785x.h"
61 /* clang-format on */
62 
63 #include "database.h"
64 #include "mxm_1785x_tools.h"
65 #include "mxm_battery_management.h"
66 #include "mxm_registry.h"
67 #include "os.h"
68 
69 /*========== Macros and Definitions =========================================*/
70 /** @defgroup MXM_I2C_IMPLEMENTATION symbols and settings pertaining to the I2C implementation in MXM
71  * @{
72  */
73 /** @brief address of MUX0 */
74 #define MXM_I2C_MUX0_ADDRESS (0x4Cu)
75 /** @brief address of MUX1 */
76 #define MXM_I2C_MUX1_ADDRESS (0x4Du)
77 /**@}*/
78 
79 /** @brief Delay in milliseconds before the balancing status is updated */
80 #define MXM_DELAY_BALANCING 10000u
81 
82 /*========== Static Constant and Variable Definitions =======================*/
83 
84 /*========== Extern Constant and Variable Definitions =======================*/
85 
86 /*========== Static Function Prototypes =====================================*/
87 
88 /*========== Static Function Implementations ================================*/
89 
90 /*========== Extern Function Implementations ================================*/
92  return MXM_MODEL_ID_MAX17852;
93 }
94 
96  FAS_ASSERT(pState != NULL_PTR);
97 
98  /**
99  * @brief Mapping of voltage registers
100  *
101  * This array maps registers of the monitoring IC onto cell-numbers. The
102  * register values are defined in the #MXM_REG_NAME_e enum.
103  * For now the length of this array is #MXM_VOLTAGE_READ_ARRAY_LENGTH
104  * as it is enabled for the measurement of all cells, two AUX-voltages and
105  * one block voltage. This has to be adapted once this driver is enabled for
106  * general operation.
107  */
108  const MXM_REG_NAME_e mxm_voltageCellAddresses[MXM_VOLTAGE_READ_ARRAY_LENGTH] = {
123  MXM_REG_AUX2,
124  MXM_REG_AUX3,
126  };
127 
128  pState->operationRequested = false;
129  /* TODO handle transition to measurement states properly with dedicated state-machine */
130  /* TODO parse DATACHECKBYTE where available */
131 
133 
134  switch (pState->operationSubstate) {
135  case MXM_OP_ENTRY_STATE:
137  break;
140  pState->diagnosticCounter = 0u;
142  } else {
143  pState->diagnosticCounter++;
145  }
146  break;
149  const uint8_t temp_len =
150  (uint8_t)((BATTERY_MANAGEMENT_TX_LENGTH_READALL + (2uL * MXM_5XGetNumberOfSatellites(pState->pInstance5X))) & (uint8_t)UINT8_MAX);
152  const bool someDeviceHasBeenReset = MXM_CheckIfADeviceHasBeenReset(pState);
153  if (someDeviceHasBeenReset == true) {
154  /* a device has been reset, we should immediately reset the daisy chain by restarting the driver */
155  MXM_ErrorHandlerReset(pState, true);
156  }
157  }
158  break;
161  const uint8_t temp_len =
162  (uint8_t)((BATTERY_MANAGEMENT_TX_LENGTH_READALL + (2uL * MXM_5XGetNumberOfSatellites(pState->pInstance5X))) & (uint8_t)UINT8_MAX);
164  }
165  break;
168  const uint8_t temp_len =
169  (uint8_t)((BATTERY_MANAGEMENT_TX_LENGTH_READALL + (2uL * MXM_5XGetNumberOfSatellites(pState->pInstance5X))) & (uint8_t)UINT8_MAX);
171  }
172  break;
175  const uint8_t temp_len =
176  (uint8_t)((BATTERY_MANAGEMENT_TX_LENGTH_READALL + (2uL * MXM_5XGetNumberOfSatellites(pState->pInstance5X))) & (uint8_t)UINT8_MAX);
178  }
179  break;
182  const uint8_t temp_len =
183  (uint8_t)((BATTERY_MANAGEMENT_TX_LENGTH_READALL + (2uL * MXM_5XGetNumberOfSatellites(pState->pInstance5X))) & (uint8_t)UINT8_MAX);
185  }
186  break;
188  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
190  pState->batteryCmdBuffer.lsb = 0x00u;
191  pState->batteryCmdBuffer.msb = 0x00u;
192  }
194  break;
196  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
198  pState->batteryCmdBuffer.lsb = 0x00u;
199  pState->batteryCmdBuffer.msb = 0x00u;
200  }
202  break;
204  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
206  pState->batteryCmdBuffer.lsb = 0x00u;
207  pState->batteryCmdBuffer.msb = 0x00u;
208  }
210  break;
213  break;
215  /* configure I2CPNTR to channel number corresponding to mux counter*/
217  pState->batteryCmdBuffer.lsb = (0x01u << pState->muxCounter);
218  pState->batteryCmdBuffer.msb = 0x00u;
220  break;
221  case MXM_OP_WRITE_MUX0:
222  /* send configuration to MUX0 for channel 0 (PNTR configured to 1u) */
224  pState->batteryCmdBuffer.lsb = (MXM_I2C_MUX0_ADDRESS << 1u);
225  pState->batteryCmdBuffer.msb = 0x40u;
227  break;
228  case MXM_OP_WRITE_MUX1:
229  /* send configuration to MUX1 for channel 0 (PNTR configured to 1u) */
231  pState->batteryCmdBuffer.lsb = (MXM_I2C_MUX1_ADDRESS << 1u);
232  pState->batteryCmdBuffer.msb = 0x40u;
234  break;
237  /* set SCANSTROBE, enable 4x OVERSAMPL */
238  pState->batteryCmdBuffer.lsb = 0x09u;
239  /* enable AUTOBALSWDIS */
240  pState->batteryCmdBuffer.msb = 0x10u;
242  break;
245  /* no additional handling needed */
246  }
247  break;
248  case MXM_OP_GET_VOLTAGES:
250  temp_mon_state = MXM_MonGetVoltages(pState, mxm_voltageCellAddresses[pState->mxmVoltageCellCounter]);
251 
252  if (temp_mon_state == MXM_MONITORING_STATE_PASS) {
253  if (pState->mxmVoltageCellCounter < (uint8_t)UINT8_MAX) {
254  pState->mxmVoltageCellCounter++;
255  }
257  MXM_VOLTAGE_READ_ARRAY_LENGTH <= (uint8_t)UINT8_MAX,
258  "invalid define MXM_VOLTAGE_READ_ARRAY_LENGTH");
259  /* modified: read one additional aux entry */
261  pState->mxmVoltageCellCounter = 0;
263  }
264  } else {
265  /* MXM_MONITORING_STATE_PENDING, do nothing */
266  }
267  break;
268  case MXM_OP_GET_ALRTSUM:
270  /* no additional handling needed */
271  }
272  break;
274  if (MXM_ParseVoltagesIntoDB(pState) == STD_OK) {
276  pState->firstMeasurementDone = true;
277  } else {
278  MXM_ErrorHandlerReset(pState, false);
279  }
280  if (pState->stopRequested == true) {
282  } else {
283  /* do nothing */
284  }
285  break;
287  if (pState->openwireRequested == true) {
289  pState->openwireRequested = false;
290  } else {
292  }
293  break;
294  case MXM_OP_PINOPEN_EXIT:
296  break;
298  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
300  pState->batteryCmdBuffer.lsb = 0xFFu;
301  pState->batteryCmdBuffer.msb = 0xFFu; /* execute diagnostic on every cell */
302  }
304  break;
306  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
308  pState->batteryCmdBuffer.lsb = 0x81u;
309  pState->batteryCmdBuffer.msb = 0x00u; /* request comp scan */
310  }
312  break;
315  /* no additional handling needed */
316  }
317  break;
320  /* no additional handling needed */
321  }
322  break;
324  if (MXM_ProcessOpenWire(pState) == STD_OK) {
326  } else {
327  MXM_ErrorHandlerReset(pState, false);
328  }
329  break;
331  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
333  pState->batteryCmdBuffer.lsb = 0x00u;
334  pState->batteryCmdBuffer.msb = 0x00u;
335  }
337  break;
338  case MXM_OP_BAL_ENTRY:
339  /* Get the current time */
341 
342  /* Wait 'MXM_DELAY_BALANCING' milliseconds before processing the balancing */
346  MXM_DELAY_BALANCING) == false) {
347  /* nothing to do, exit balancing state chain */
349  } else {
350  /* Balancing needs to be processed */
352  /* First reset the balancing switches, and THEN
353  set the balancing switches according to the database */
355 
356  /* Change the parity of cells to balance */
357  if (pState->pBalancingState->evenCellsBalancingProcessed == true) {
358  pState->pBalancingState->evenCellsNeedBalancing = false;
359  pState->pBalancingState->oddCellsNeedBalancing = true;
360  }
361  /* Same for odd cells */
362  if (pState->pBalancingState->oddCellsBalancingProcessed == true) {
363  pState->pBalancingState->evenCellsNeedBalancing = true;
364  pState->pBalancingState->oddCellsNeedBalancing = false;
365  }
366  }
367  break;
369  /* Send a WRITEALL command to stop all balancing action */
370  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
372  pState->batteryCmdBuffer.lsb = 0x00U;
373  pState->batteryCmdBuffer.msb = 0x00U;
374  }
376  break;
378  /* Send a WRITEALL command to open all balancing switches */
379  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
381  /* CBRESTART is not reset to 0, not needed. */
382  pState->batteryCmdBuffer.lsb = 0x00U;
383  pState->batteryCmdBuffer.msb = 0x00U;
384  }
386  break;
388  /* Send a WRITEDEVICE command to each module in a daisy-chain
389  to close appropriate balancing switches */
390  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
393 
395  if (database_retval == STD_OK) {
396  /* Construct the balancing buffer */
398 
399  if (retval == STD_OK) {
400  if (pState->pBalancingState->cellsToBalance > 0u) {
401  /* Some cells need to be balanced */
404  pState->batteryCmdBuffer.lsb =
405  (uint8_t)(pState->pBalancingState->cellsToBalance & MXM_BM_LSB);
406  pState->batteryCmdBuffer.msb =
407  (uint8_t)(pState->pBalancingState->cellsToBalance >> MXM_CELLS_IN_LSB);
408  pState->batteryCmdBuffer.msb &= ((uint8_t)1u << 7u); /* set CBRESTART bit */
409 
410  const STD_RETURN_TYPE_e setStateRequestReturn = MXM_5XSetStateRequest(
411  pState->pInstance5X,
413  pState->batteryCmdBuffer,
414  &pState->requestStatus5x);
415  FAS_ASSERT(setStateRequestReturn == STD_OK);
416  } else {
417  /* It is not necessary to re-send 0 to the device, because it has been done previously
418  in the BALANCING_CONTROL_RESET_ALL sub-state */
420  }
421  } else {
422  /* this should not happen if the software works as expected */
425  }
426  } else {
427  /* database read not successful, retry */
429  }
430  } else if (pState->requestStatus5x == MXM_5X_STATE_UNPROCESSED) {
431  /* wait for processing */
432  } else if (pState->requestStatus5x == MXM_5X_STATE_PROCESSED) {
433  if (pState->pBalancingState->moduleBalancingIndex < pState->highest5xDevice) {
434  /* Not all modules have been treated. Repeat this state with the next module */
437  /* Re-set the status to UNSENT to repeat the WRITE operation */
439  } else {
440  /* Finished the iteration of all modules in a daisy chain, continue */
442 
443  /* Update flags for the end of balancing */
444  if (pState->pBalancingState->evenCellsNeedBalancing == true) {
446  } else if (pState->pBalancingState->oddCellsNeedBalancing == true) {
448  } else {
449  /* nothing to do here */
450  }
453  }
454  } else if (pState->requestStatus5x == MXM_5X_STATE_ERROR) {
455  /* default-behavior: retry */
457  MXM_ErrorHandlerReset(pState, false);
458  } else {
460  }
461  break;
462  case MXM_OP_BAL_START:
463  /* Initiate balancing for all devices in a daisy chain */
464  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
466  /* Manual ON MODE + Balancing halt in case of High temperature
467  + Alert when cell balancing is finished */
468  pState->batteryCmdBuffer.lsb = 0x0EU;
469  pState->batteryCmdBuffer.msb = 0x18U;
470  }
472  break;
475  const uint8_t temp_len =
476  (uint8_t)((BATTERY_MANAGEMENT_TX_LENGTH_READALL + (2uL * MXM_5XGetNumberOfSatellites(pState->pInstance5X))) & (uint8_t)UINT8_MAX);
478  }
479  break;
480  case MXM_OP_BAL_EXIT:
482  break;
484  /* actions that should be taken at the end of a measurement cycle */
486  break;
488  if (pState->muxCounter < (8u - 1u)) {
489  pState->muxCounter++;
490  } else {
491  pState->muxCounter = 0u;
492  }
494  break;
497  break;
498  /* "initialization" from here on */
499  case MXM_INIT_ENTRY:
501  break;
502  case MXM_INIT_DEVCFG1:
503  /* switch to single UART with external loopback */
504  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
506  pState->batteryCmdBuffer.lsb = 0x00u; /* alert interface disabled */
507  pState->batteryCmdBuffer.msb = 0x01u; /* single uart with external loopback*/
508  }
510  break;
511  case MXM_INIT_DEVCFG2:
512  /* default value for DEVCFG2 */
513  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
515  pState->batteryCmdBuffer.lsb = 0x00u; /* activate CB watchdog */
516  pState->batteryCmdBuffer.msb = 0x40u; /* default*/
517  }
519  break;
521  /* clear ALRTDUALUART (as requested for startup routine in data-sheet) */
522  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
524  pState->batteryCmdBuffer.lsb = 0x00u; /* clear lsb */
525  pState->batteryCmdBuffer.msb = 0x00u; /* clear msb, containing ALRTDUALUART */
526  }
528  break;
529  case MXM_INIT_STATUS1:
530  /* clear ALRTRST */
531  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
533  pState->batteryCmdBuffer.lsb = 0x00u;
534  pState->batteryCmdBuffer.msb = 0x00u;
535  }
537  break;
539  /* add version information to registry */
541  uint8_t temp_len =
542  (uint8_t)((BATTERY_MANAGEMENT_TX_LENGTH_READALL + (2uL * MXM_5XGetNumberOfSatellites(pState->pInstance5X))) & (uint8_t)UINT8_MAX);
543  MXM_MonRegistryParseVersionIntoDevices(pState, temp_len);
544  }
545  break;
546  case MXM_INIT_GET_ID1:
547  /* add ID1 to registry */
548  if (MXM_HandleStateReadall(pState, MXM_REG_ID1, MXM_INIT_GET_ID2) == true) {
549  uint8_t temp_len =
550  (uint8_t)((BATTERY_MANAGEMENT_TX_LENGTH_READALL + (2uL * MXM_5XGetNumberOfSatellites(pState->pInstance5X))) & (uint8_t)UINT8_MAX);
552  }
553  break;
554  case MXM_INIT_GET_ID2:
555  /* add ID2 to registry */
557  uint8_t temp_len =
558  (uint8_t)((BATTERY_MANAGEMENT_TX_LENGTH_READALL + (2uL * MXM_5XGetNumberOfSatellites(pState->pInstance5X))) & (uint8_t)UINT8_MAX);
560  }
561  break;
562  case MXM_INIT_MEASUREEN1:
563  /* enable all 14 cells */
564  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
566  pState->batteryCmdBuffer.lsb = 0xFFu;
567  pState->batteryCmdBuffer.msb = 0x7Fu; /* TODO this also enables block measurement */
568  }
570  break;
571  case MXM_INIT_MEASUREEN2:
572  /* enable AUX2 and AUX3 */
573  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
575  pState->batteryCmdBuffer.lsb = 0x0Cu; /* AUX2 and AUX3 */
576  pState->batteryCmdBuffer.msb = 0x00u;
577  }
579  break;
580  case MXM_INIT_AUXGPIOCFG:
581  /* switch GPIO2 and GPIO3 to AUX, enable I2C */
582  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
584  pState->batteryCmdBuffer.lsb = 0x00u;
585  /* conf for MAX17853: 0x3Eu */
586  /* conf for MAX17852, I2C enable: 0x8Eu */
587  /* I2C enable, AUX2 and AUX3 */
588  pState->batteryCmdBuffer.msb = 0x80u;
589  }
591  break;
592  case MXM_INIT_AUXTIMEREG:
593  /* configure settling time that NTC network takes for measurement to 500us */
594  /* WARNING: reevaluate this value if thermistor supply is switched
595  during sampling */
596  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
598  pState->batteryCmdBuffer.lsb = 0x53u;
599  pState->batteryCmdBuffer.msb = 0x00u;
600  }
602  break;
603  case MXM_INIT_ACQCFG:
604  /* set ACQCFG */
605  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
607  /* default values */
608  pState->batteryCmdBuffer.lsb = 0x00u;
609  /* we have to turn thermistor switch manually on, as charging
610  the network takes to long */
611  pState->batteryCmdBuffer.msb = 0x06u;
612  }
614  break;
615  case MXM_INIT_UVTHSETREG:
616  /* configure UVTHSETREG */
617  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
621  &pState->batteryCmdBuffer.lsb,
622  &pState->batteryCmdBuffer.msb);
623  }
625  break;
626  case MXM_INIT_OVTHSETREG:
627  /* configure OVTHSETREG */
628  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
632  &pState->batteryCmdBuffer.lsb,
633  &pState->batteryCmdBuffer.msb);
634  }
636  break;
637  case MXM_INIT_BALEXP1:
638  /* set BALEXP1 to have 5 minute timeout in manual balancing */
639  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
641  pState->batteryCmdBuffer.lsb = 0x05u;
642  pState->batteryCmdBuffer.msb = 0x00u;
643  }
645  break;
646  case MXM_INIT_BALEXP2:
647  /* set BALEXP2 to have 0 minute timeout in manual balancing */
648  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
650  pState->batteryCmdBuffer.lsb = 0x00u;
651  pState->batteryCmdBuffer.msb = 0x00u;
652  }
654  break;
655  case MXM_INIT_BALEXP3:
656  /* set BALEXP3 to have 0 minute timeout in manual balancing */
657  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
659  pState->batteryCmdBuffer.lsb = 0x00u;
660  pState->batteryCmdBuffer.msb = 0x00u;
661  }
663  break;
664  case MXM_INIT_BALEXP4:
665  /* set BALEXP4 to have 0 minute timeout in manual balancing */
666  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
668  pState->batteryCmdBuffer.lsb = 0x00u;
669  pState->batteryCmdBuffer.msb = 0x00u;
670  }
672  break;
673  case MXM_INIT_BALEXP5:
674  /* set BALEXP5 to have 0 minute timeout in manual balancing */
675  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
677  pState->batteryCmdBuffer.lsb = 0x00u;
678  pState->batteryCmdBuffer.msb = 0x00u;
679  }
681  break;
682  case MXM_INIT_BALEXP6:
683  /* set BALEXP6 to have 0 minute timeout in manual balancing */
684  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
686  pState->batteryCmdBuffer.lsb = 0x00u;
687  pState->batteryCmdBuffer.msb = 0x00u;
688  }
690  break;
691  case MXM_INIT_BALEXP7:
692  /* set BALEXP7 to have 0 minute timeout in manual balancing */
693  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
695  pState->batteryCmdBuffer.lsb = 0x00u;
696  pState->batteryCmdBuffer.msb = 0x00u;
697  }
699  break;
700  case MXM_INIT_BALEXP8:
701  /* set BALEXP8 to have 0 minute timeout in manual balancing */
702  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
704  pState->batteryCmdBuffer.lsb = 0x00u;
705  pState->batteryCmdBuffer.msb = 0x00u;
706  }
708  break;
709  case MXM_INIT_BALEXP9:
710  /* set BALEXP9 to have 0 minute timeout in manual balancing */
711  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
713  pState->batteryCmdBuffer.lsb = 0x00u;
714  pState->batteryCmdBuffer.msb = 0x00u;
715  }
717  break;
718  case MXM_INIT_BALEXP10:
719  /* set BALEXP10 to have 0 minute timeout in manual balancing */
720  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
722  pState->batteryCmdBuffer.lsb = 0x00u;
723  pState->batteryCmdBuffer.msb = 0x00u;
724  }
726  break;
727  case MXM_INIT_BALEXP11:
728  /* set BALEXP11 to have 0 minute timeout in manual balancing */
729  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
731  pState->batteryCmdBuffer.lsb = 0x00u;
732  pState->batteryCmdBuffer.msb = 0x00u;
733  }
735  break;
736  case MXM_INIT_BALEXP12:
737  /* set BALEXP12 to have 0 minute timeout in manual balancing */
738  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
740  pState->batteryCmdBuffer.lsb = 0x00u;
741  pState->batteryCmdBuffer.msb = 0x00u;
742  }
744  break;
745  case MXM_INIT_BALEXP13:
746  /* set BALEXP13 to have 0 minute timeout in manual balancing */
747  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
749  pState->batteryCmdBuffer.lsb = 0x00u;
750  pState->batteryCmdBuffer.msb = 0x00u;
751  }
753  break;
754  case MXM_INIT_BALEXP14:
755  /* set BALEXP14 to have 0 minute timeout in manual balancing */
756  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
758  pState->batteryCmdBuffer.lsb = 0x00u;
759  pState->batteryCmdBuffer.msb = 0x00u;
760  }
762  break;
763  case MXM_INIT_BALSWDLY:
764  /* set BALSWDLY to 2 ms settling time after balancing */
765  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
767  /* CELLDLY = 1920us (~2ms) --> 20*96us
768  CELLDLY corresponds to the time to relax the cell before voltage measurement */
769  pState->batteryCmdBuffer.lsb = 0x00u;
770  pState->batteryCmdBuffer.msb = 0x14u;
771  }
773  break;
774  case MXM_INIT_ALRTOVEN:
775  /* enable ALRTOVEN */
776  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
778  pState->batteryCmdBuffer.lsb = 0xFFu;
779  pState->batteryCmdBuffer.msb = 0x3Fu;
780  }
782  break;
783  case MXM_INIT_ALRTUVEN:
784  /* enable ALRTUVEN */
785  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
787  pState->batteryCmdBuffer.lsb = 0xFFu;
788  pState->batteryCmdBuffer.msb = 0x3Fu;
789  }
791  break;
793  /* configure COMPOPNTHREG */
794  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
796  /* TODO 0.5V */
799  &pState->batteryCmdBuffer.lsb,
800  &pState->batteryCmdBuffer.msb);
801  }
803  break;
806  /* no additional handling needed */
807  }
808  break;
809  case MXM_INIT_I2C_CFG:
810  /* configure I2CCFG to
811  * 400kHz
812  * Alternate write Mode (just a pointer without data)
813  * Combined Format
814  * 7 Bit addressing
815  * one byte pointer length
816  * default
817  */
818  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
820  pState->batteryCmdBuffer.lsb = 0x00u;
821  pState->batteryCmdBuffer.msb = 0xE0u;
822  }
824  break;
825  case MXM_INIT_I2C_PNTR:
826  /* configure I2CPNTR */
827  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
829  pState->batteryCmdBuffer.lsb = 0x01u;
830  pState->batteryCmdBuffer.msb = 0x00u;
831  }
833  break;
835  /* send configuration to MUX0 for channel 0 (PNTR configured to 1u) */
836  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
838  pState->batteryCmdBuffer.lsb = (MXM_I2C_MUX0_ADDRESS << 1u);
839  pState->batteryCmdBuffer.msb = 0x40u;
840  }
842  break;
844  /* send configuration to MUX1 for channel 0 (PNTR configured to 1u) */
845  if (pState->requestStatus5x == MXM_5X_STATE_UNSENT) {
847  pState->batteryCmdBuffer.lsb = (MXM_I2C_MUX1_ADDRESS << 1u);
848  pState->batteryCmdBuffer.msb = 0x40u;
849  }
851  break;
854  /* no additional handling needed */
855  }
856  break;
857  default:
858  /* invalid state */
860  break;
861  }
862 }
863 
864 /*========== Externalized Static Function Implementations (Unit Test) =======*/
865 #ifdef UNITY_UNIT_TEST
866 
867 #endif
Database module header.
#define DATA_READ_DATA(...)
Definition: database.h:83
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:248
#define FAS_TRAP
Define that evaluates to essential boolean false thus tripping an assert.
Definition: fassert.h:126
#define FAS_STATIC_ASSERT(cond, msg)
Definition: fassert.h:282
STD_RETURN_TYPE_e
Definition: fstd_types.h:81
@ STD_OK
Definition: fstd_types.h:82
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:76
#define MXM_I2C_MUX0_ADDRESS
address of MUX0
Definition: mxm_17852.c:74
#define MXM_I2C_MUX1_ADDRESS
address of MUX1
Definition: mxm_17852.c:76
#define MXM_BM_LSB
Monitoring Register LSB.
#define MXM_DELAY_BALANCING
Delay in milliseconds before the balancing status is updated.
Definition: mxm_17852.c:80
MXM_MODEL_ID_e MXM_GetModelIdOfDaisyChain(void)
returns the model ID of the daisy chain
Definition: mxm_17852.c:91
void MXM_StateMachineOperation(MXM_MONITORING_INSTANCE_s *pState)
State-Machine implementation for operation state.
Definition: mxm_17852.c:95
void MXM_ErrorHandlerReset(MXM_MONITORING_INSTANCE_s *pInstance, bool immediateReset)
This error handler is used as a last resort and tries a reset of the complete driver.
Definition: mxm_1785x.c:901
bool GEN_MUST_CHECK_RETURN MXM_HandleStateReadall(MXM_MONITORING_INSTANCE_s *pInstance, MXM_REG_NAME_e registerName, MXM_STATEMACHINE_OPERATION_STATES_e nextState)
Handle the statemachine-transactions for a READALL.
Definition: mxm_1785x.c:940
STD_RETURN_TYPE_e MXM_ProcessOpenWire(const MXM_MONITORING_INSTANCE_s *const kpkInstance)
Processes the retrieved information on openwire.
Definition: mxm_1785x.c:971
STD_RETURN_TYPE_e MXM_ParseVoltagesIntoDB(const MXM_MONITORING_INSTANCE_s *const kpkInstance)
Copies measured voltage data into the database.
Definition: mxm_1785x.c:1075
STD_RETURN_TYPE_e MXM_ConstructBalancingBuffer(MXM_BALANCING_STATE_s *pBalancingInstance)
Fill the balancing datastructure.
Definition: mxm_1785x.c:1016
MXM_MONITORING_STATE_e GEN_MUST_CHECK_RETURN MXM_MonGetVoltages(MXM_MONITORING_INSTANCE_s *pState, MXM_REG_NAME_e regAddress)
Encapsulation for reading voltages from a register.
Definition: mxm_1785x.c:1275
void MXM_HandleStateWriteall(MXM_MONITORING_INSTANCE_s *pInstance, MXM_STATEMACHINE_OPERATION_STATES_e nextState)
Handle the statemachine-transactions for a WRITEALL.
Definition: mxm_1785x.c:912
Headers for the driver for the MAX17841B ASCI and MAX1785x monitoring chip.
#define MXM_REF_UNIPOLAR_CELL_mV
Definition: mxm_1785x.h:104
#define MXM_CELLS_IN_LSB
Definition: mxm_1785x.h:101
#define MXM_THRESHOLD_DIAGNOSTIC_AFTER_CYCLES
Battery monitoring driver for MAX1785x battery monitoring ICs.
Definition: mxm_1785x.h:95
#define MXM_VOLTAGE_READ_ARRAY_LENGTH
Definition: mxm_1785x.h:98
void MXM_Unipolar14BitInto16Bit(uint16_t inputValue, uint8_t *lsb, uint8_t *msb)
convert a unipolar 14bit-value and shifts it into the 16bit-format
uint16_t MXM_VoltageIntoUnipolar14Bit(uint16_t voltage_mV, uint16_t fullscaleReference_mV)
convert a voltage value into a unipolar 14bit value
This is a collection of helper functions for the MAX1785x ICs.
MXM_MONITORING_STATE_e
@ MXM_MONITORING_STATE_PASS
@ MXM_MONITORING_STATE_PENDING
@ MXM_INIT_I2C_GET_STAT1
@ MXM_OP_DIAGNOSTIC_CLEAR_FMEA1
@ MXM_INIT_BALEXP5
@ MXM_OP_WRITE_MUX0
@ MXM_INIT_BALEXP6
@ MXM_OP_DIAGNOSTIC_FMEA2
@ MXM_OP_BAL_CONTROL_STOP_BALANCING
@ MXM_INIT_STATUS1
@ MXM_INIT_BALEXP13
@ MXM_INIT_OVTHSETREG
@ MXM_INIT_BALEXP1
@ MXM_INIT_BALEXP9
@ MXM_OP_PINOPEN_RESTORE_CURRENT_SOURCE_CONF
@ MXM_INIT_SET_STATUS2
@ MXM_OP_DIAGNOSTIC_FMEA1
@ MXM_INIT_I2C_SEND_MUX1
@ MXM_INIT_MEASUREEN1
@ MXM_OP_PINOPEN_SET_CURRENT_SOURCES
@ MXM_INIT_GET_ID2
@ MXM_INIT_MEASUREEN2
@ MXM_OP_PINOPEN_GET_SCAN_STROBE
@ MXM_OP_DIAGNOSTIC_CLEAR_STATUS2
@ MXM_INIT_I2C_PNTR
@ MXM_OP_BAL_START
@ MXM_INIT_BALEXP8
@ MXM_INIT_UVTHSETREG
@ MXM_OP_PINOPEN_COMPSCAN
@ MXM_INIT_I2C_SEND_MUX0
@ MXM_INIT_BALSWDLY
@ MXM_INIT_GET_VERSION
@ MXM_OP_DIAGNOSTIC_CLEAR_FMEA2
@ MXM_OP_BAL_CONTROL_SET_ALL
@ MXM_INIT_ENTRY
@ MXM_OP_BAL_READ_BALSTAT
@ MXM_INIT_GET_ID1
@ MXM_OP_DIAGNOSTIC_EXIT
@ MXM_OP_DIAGNOSTIC_STATUS1
@ MXM_INIT_GET_I2C_STAT2
@ MXM_OP_PINOPEN_EXIT
@ MXM_OP_PINOPEN_ENTRY
@ MXM_OP_PINOPEN_PROCESS_OPENWIRE
@ MXM_INIT_BALEXP10
@ MXM_INIT_BALEXP14
@ MXM_INIT_BALEXP12
@ MXM_OP_INCREMENT_MUX_COUNTER
@ MXM_OP_BAL_EXIT
@ MXM_OP_PARSE_VOLTAGES_INTO_DB
@ MXM_INIT_I2C_CFG
@ MXM_INIT_ALRTUVEN
@ MXM_INIT_BALEXP3
@ MXM_OP_SELECT_MUX_CHANNEL
@ MXM_INIT_BALEXP2
@ MXM_OP_WRITE_MUX1
@ MXM_INIT_ALRTOVEN
@ MXM_OP_ENTRY_STATE
@ MXM_INIT_BALEXP7
@ MXM_INIT_DEVCFG2
@ MXM_OP_CYCLE_END_EXIT
@ MXM_OP_GET_VOLTAGES
@ MXM_OP_GET_ALRTSUM
@ MXM_OP_CYCLE_END_ENTRY
@ MXM_OP_BAL_CONTROL_RESET_ALL
@ MXM_OP_BAL_ENTRY
@ MXM_INIT_ACQCFG
@ MXM_INIT_COMPOPNTHREG
@ MXM_OP_PINOPEN_GET_ALRT
@ MXM_OP_DIAGNOSTIC_STATUS2
@ MXM_INIT_BALEXP4
@ MXM_OP_DIAGNOSTIC_STATUS3
@ MXM_OP_SET_SCAN_STROBE
@ MXM_OP_GET_SCAN_STROBE
@ MXM_INIT_DEVCFG1
@ MXM_INIT_BALEXP11
@ MXM_INIT_AUXTIMEREG
@ MXM_INIT_AUXGPIOCFG
@ MXM_OP_DIAGNOSTIC_ENTRY
@ MXM_STATEMACHINE_STATES_IDLE
MXM_MODEL_ID_e
Type of monitoring device.
@ MXM_MODEL_ID_MAX17852
STD_RETURN_TYPE_e MXM_5XSetStateRequest(MXM_5X_INSTANCE_s *pInstance5x, MXM_STATEMACHINE_5X_e state, MXM_5X_COMMAND_PAYLOAD_s commandPayload, MXM_5X_STATE_REQUEST_STATUS_e *processed)
Set state request for the Battery Management Statemachine.
uint8_t MXM_5XGetNumberOfSatellites(const MXM_5X_INSTANCE_s *const kpkInstance)
Get number of satellites.
Headers for the driver for the MAX17841B ASCI and MAX1785x monitoring chip.
@ MXM_STATEMACH_5X_WRITE_DEVICE
@ MXM_5X_STATE_UNSENT
@ MXM_5X_STATE_PROCESSED
@ MXM_5X_STATE_UNPROCESSED
@ MXM_5X_STATE_ERROR
#define BATTERY_MANAGEMENT_TX_LENGTH_READALL
Battery Management Protocol lengths of TX buffer.
MXM_REG_NAME_e
MAX1785x register names.
@ MXM_REG_CTSTCFG
@ MXM_REG_CELL1
@ MXM_REG_CELL4
@ MXM_REG_BALEXP11
@ MXM_REG_STATUS1
@ MXM_REG_BALEXP6
@ MXM_REG_STATUS3
@ MXM_REG_STATUS2
@ MXM_REG_BALEXP2
@ MXM_REG_ALRTSUM
@ MXM_REG_CELL10
@ MXM_REG_BALEXP13
@ MXM_REG_I2CCFG
@ MXM_REG_BALEXP5
@ MXM_REG_UVTHSET
@ MXM_REG_BALEXP10
@ MXM_REG_AUX3
@ MXM_REG_BALEXP1
@ MXM_REG_SCANCTRL
@ MXM_REG_BALSTAT
@ MXM_REG_VERSION
@ MXM_REG_MEASUREEN2
@ MXM_REG_I2CSTAT
@ MXM_REG_ID1
@ MXM_REG_BALEXP14
@ MXM_REG_I2CPNTR
@ MXM_REG_CELL9
@ MXM_REG_AUXTIME
@ MXM_REG_BLOCK
@ MXM_REG_DEVCFG2
@ MXM_REG_COMPOPNTH
@ MXM_REG_AUXGPIOCFG
@ MXM_REG_CELL3
@ MXM_REG_BALEXP8
@ MXM_REG_FMEA1
@ MXM_REG_ID2
@ MXM_REG_BALEXP9
@ MXM_REG_CELL14
@ MXM_REG_CELL6
@ MXM_REG_I2CSEND
@ MXM_REG_ACQCFG
@ MXM_REG_BALCTRL
@ MXM_REG_CELL2
@ MXM_REG_BALEXP7
@ MXM_REG_ALRTUVEN
@ MXM_REG_BALEXP3
@ MXM_REG_CELL11
@ MXM_REG_MEASUREEN1
@ MXM_REG_BALSWDLY
@ MXM_REG_CELL5
@ MXM_REG_CELL13
@ MXM_REG_OVTHSET
@ MXM_REG_AUX2
@ MXM_REG_BALSWCTRL
@ MXM_REG_FMEA2
@ MXM_REG_CELL7
@ MXM_REG_BALEXP12
@ MXM_REG_ALRTCOMPUV
@ MXM_REG_DEVCFG1
@ MXM_REG_ALRTOVEN
@ MXM_REG_CELL8
@ MXM_REG_CELL12
@ MXM_REG_BALEXP4
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:121
void MXM_MonRegistryParseStatusFmeaIntoDevices(MXM_MONITORING_INSTANCE_s *pState, uint8_t rxBufferLength)
Parse STATUS or FMEA into the registry.
Definition: mxm_registry.c:192
void MXM_MonRegistryParseVersionIntoDevices(MXM_MONITORING_INSTANCE_s *pState, uint8_t rxBufferLength)
Parse Version into the registry.
Definition: mxm_registry.c:152
bool MXM_CheckIfADeviceHasBeenReset(const MXM_MONITORING_INSTANCE_s *const kpkState)
check if one of the devices in the registry has the ALRTRST bit set
Definition: mxm_registry.c:239
Functions in order to have a registry of monitoring ICs.
bool OS_CheckTimeHasPassedWithTimestamp(uint32_t oldTimeStamp_ms, uint32_t currentTimeStamp_ms, uint32_t timeToPass_ms)
This function checks if timeToPass has passed since the last timestamp to now.
Definition: os.c:118
Declaration of the OS wrapper interface.
uint32_t OS_GetTickCount(void)
Returns OS based system tick value.
Definition: os_freertos.c:139
DATA_BLOCK_BALANCING_CONTROL_s *const pBalancingControl_table
MXM_5X_COMMAND_PAYLOAD_s batteryCmdBuffer
MXM_STATEMACHINE_OPERATION_STATES_e operationSubstate
MXM_BALANCING_STATE_s *const pBalancingState
MXM_STATEMACHINE_STATES_e state
MXM_5X_INSTANCE_s *const pInstance5X
MXM_5X_STATE_REQUEST_STATUS_e requestStatus5x