foxBMS-UnitTests  1.0.0
The foxBMS Unit Tests API Documentation
n775.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2021, Fraunhofer-Gesellschaft zur Foerderung der
4  * angewandten Forschung e.V. All rights reserved.
5  *
6  * BSD 3-Clause License
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * We kindly request you to use one or more of the following phrases to refer
31  * to foxBMS in your hardware, software, documentation or advertising
32  * materials:
33  *
34  * ″This product uses parts of foxBMS®″
35  *
36  * ″This product includes parts of foxBMS®″
37  *
38  * ″This product is derived from foxBMS®″
39  *
40  */
41 
42 /**
43  * @file n775.c
44  * @author foxBMS Team
45  * @date 2020-05-08 (date of creation)
46  * @updated 2020-05-08 (date of last update)
47  * @ingroup DRIVERS
48  * @prefix N775
49  *
50  * @brief Driver for the MC33775A monitoring chip.
51  *
52  */
53 
54 /*========== Includes =======================================================*/
55 #include "n775.h"
56 
57 #include "HL_system.h"
58 
59 #include "MC33775A.h"
60 #include "database.h"
61 #include "diag.h"
62 #include "mic_dma.h"
63 #include "os.h"
64 
65 /*========== Macros and Definitions =========================================*/
66 
67 /** maximum number of supported cells per slave-module */
68 #define N775_MAX_SUPPORTED_CELLS (12u)
69 
70 /**
71  * Saves the last state and the last substate
72  */
73 #define N775_SAVELASTSTATES() \
74  n775_state.lastState = n775_state.state; \
75  n775_state.lastSubState = n775_state.subState
76 
77 /*========== Static Constant and Variable Definitions =======================*/
78 
79 /*========== Extern Constant and Variable Definitions =======================*/
80 #pragma SET_DATA_SECTION(".sharedRAM")
83 #pragma SET_DATA_SECTION()
84 
87 
88 uint8_t n775_enumerateAddress = 1u;
89 
90 /* static int16_t n775_voltage_min = INT16_MAX;
91 static int16_t n775_voltage_max = INT16_MIN;
92 static int32_t n775_voltage_sum = 0;
93 
94 static uint8_t n775_voltage_module_number_min = 0;
95 static uint8_t n775_voltage_module_number_max = 0;
96 static uint8_t n775_voltage_cell_number_min = 0;
97 static uint8_t n775_voltage_cell_number_max = 0;
98 static uint16_t n775_voltage_nrValidCellVoltages = 0; */
99 
100 /** local copies of database tables */
101 /**@{*/
106 /**@}*/
107 
109  .timer = 0,
110  .stateReq = N775_STATE_NO_REQUEST,
112  .subState = 0,
113  .lastState = N775_STATEMACH_UNINITIALIZED,
114  .lastSubState = 0,
115  .errRequestCounter = 0,
116  .triggerEntry = 0,
117  .firstMeasurementMade = STD_NOT_OK,
118  .checkSpiFlag = STD_NOT_OK,
119  .balanceControlDone = STD_NOT_OK,
120  .txTransmitOngoing = false,
121  .rxTransmitOngoing = false,
122  .totalMessages = 0u,
123  .remainingMessages = 0u,
124 };
125 
126 /*========== Static Function Prototypes =====================================*/
128 static void N775_CopyStructToTxBuffer(N775_MESSAGE_s *message, uint16_t *buffer);
129 static void N775_WakeUp(uint16_t daisyChainAddress, uint16_t deviceAddress, uint16_t registerAddress);
130 static void N775_Write(uint16_t daisyChainAddress, uint16_t deviceAddress, uint16_t registerAddress, uint16_t data);
131 static void N775_Read(
132  uint16_t daisyChainAddress,
133  uint16_t deviceAddress,
134  uint16_t registerAddress,
135  uint16_t totalNumberOfRequestedRegister);
136 static void N775_Initialize_Database(void);
137 /* static void N775_Get_BalancingControlValues(void); */
138 static void N775_StateTransition(N775_STATEMACH_e state, uint8_t substate, uint16_t timer_ms);
139 /* static void N775_CondBasedStateTransition(STD_RETURN_TYPE_e retVal, DIAG_ID_e diagCode,
140  N775_STATEMACH_e state_ok, uint8_t substate_ok, uint16_t timer_ms_ok,
141  N775_STATEMACH_e state_nok, uint8_t substate_nok, uint16_t timer_ms_nok); */
142 
143 uint16_t n775_CrcAddItem(uint16_t remainder, uint16_t item);
144 
146 uint16_t n775_CalcCrc(const N775_MESSAGE_s *msg);
147 
148 /*========== Static Function Implementations ================================*/
149 
150 /*========== Extern Function Implementations ================================*/
151 
152 /* TODO: use own function */
153 /**
154  * @brief Called to calculate the CRC of a message. NXP function.
155  *
156  * @param remainder
157  * @param item
158  *
159  * @return remainder
160  *
161  */
162 uint16_t n775_CrcAddItem(uint16_t remainder, uint16_t item) {
163  int i;
164  uint16_t local_remainder = remainder;
165 
166  local_remainder ^= item;
167 
168  for (i = 0; i < 16; i++) {
169  /*
170  * Try to divide the current data bit.
171  */
172  if ((local_remainder & 0x8000u) > 0u) {
173  local_remainder = (local_remainder << 1u) ^ ((0x9eb2u << 1u) + 0x1u);
174  } else {
175  local_remainder = (local_remainder << 1u);
176  }
177  }
178 
179  return (local_remainder);
180 }
181 
182 /* TODO: use own function */
183 /**
184  * @brief Calculate the CRC of a message. NXP function.
185  *
186  * @param msg
187  *
188  * @return crc
189  *
190  */
191 uint16_t n775_CalcCrc(const N775_MESSAGE_s *msg) {
192  uint16_t remainder = 0;
193  int i;
194 
195  remainder = n775_CrcAddItem(remainder, msg->head);
196  remainder = n775_CrcAddItem(remainder, msg->dataHead);
197 
198  for (i = 0; i < (msg->dataLength - 3); i++) {
199  remainder = n775_CrcAddItem(remainder, msg->data[i]);
200  }
201 
202  return (remainder);
203 }
204 
205 /**
206  * @brief in the database, initializes the fields related to the N775 driver.
207  *
208  * This function loops through all the N775-related data fields in the database
209  * and sets them to 0. It should be called in the initialization or re-initialization
210  * routine of the N775 driver.
211  */
212 static void N775_Initialize_Database(void) {
213  uint16_t i = 0;
214 
215  for (uint8_t stringNumber = 0u; stringNumber < BS_NR_OF_STRINGS; stringNumber++) {
217  n775_minMax.minimumCellVoltage_mV[stringNumber] = 0;
218  n775_minMax.maximumCellVoltage_mV[stringNumber] = 0;
219  n775_minMax.nrModuleMinimumCellVoltage[stringNumber] = 0;
220  n775_minMax.nrModuleMaximumCellVoltage[stringNumber] = 0;
221  n775_minMax.nrCellMinimumCellVoltage[stringNumber] = 0;
222  n775_minMax.nrCellMaximumCellVoltage[stringNumber] = 0;
223  for (i = 0; i < BS_NR_OF_BAT_CELLS; i++) {
224  n775_cellVoltage.cellVoltage_mV[stringNumber][i] = 0;
225  }
226 
228  n775_minMax.minimumTemperature_ddegC[stringNumber] = 0;
229  n775_minMax.maximumTemperature_ddegC[stringNumber] = 0;
230  n775_minMax.nrModuleMinimumTemperature[stringNumber] = 0;
231  n775_minMax.nrModuleMaximumTemperature[stringNumber] = 0;
232  n775_minMax.nrSensorMinimumTemperature[stringNumber] = 0;
233  n775_minMax.nrSensorMaximumTemperature[stringNumber] = 0;
234  for (i = 0; i < BS_NR_OF_TEMP_SENSORS; i++) {
235  n775_cellTemperature.cellTemperature_ddegC[stringNumber][i] = 0;
236  }
237 
238  for (i = 0; i < BS_NR_OF_BAT_CELLS; i++) {
239  n775_balancingControl.balancingState[stringNumber][i] = 0;
240  }
241  }
242 
247 }
248 
249 /**
250  * @brief function for setting N775_Trigger state transitions
251  *
252  * @param state: state to transition into
253  * @param subState: substate to transition into
254  * @param timer_ms: transition into state, substate after timer elapsed
255  */
256 static void N775_StateTransition(N775_STATEMACH_e state, uint8_t subState, uint16_t timer_ms) {
257  n775_state.state = state;
258  n775_state.subState = subState;
259  n775_state.timer = timer_ms;
260 }
261 
262 /**
263  * @brief re-entrance check of N775 state machine trigger function
264  *
265  * This function is not re-entrant and should only be called time- or event-triggered.
266  * It increments the triggerentry counter from the state variable n775_state.
267  * It should never be called by two different processes, so if it is the case, triggerentry
268  * should never be higher than 0 when this function is called.
269  *
270  *
271  * @return retval 0 if no further instance of the function is active, 0xff else
272  *
273  */
274 uint8_t N775_CheckReEntrance(void) {
275  uint8_t retval = 0;
276 
278  if (!n775_state.triggerEntry) {
280  } else {
281  retval = 0xFF; /* multiple calls of function */
282  }
284 
285  return (retval);
286 }
288  return N775_CheckReEntrance();
289 }
290 
291 /**
292  * @brief gets the current state request.
293  *
294  * This function is used in the functioning of the N775 state machine.
295  *
296  * @return retval current state request, taken from N775_STATE_REQUEST_e
297  */
300 
302  retval = n775_state.stateReq;
304 
305  return (retval);
306 }
307 
308 /**
309  * @brief gets the current state.
310  *
311  * This function is used in the functioning of the N775 state machine.
312  *
313  * @return current state, taken from N775_STATEMACH_e
314  */
316  return (n775_state.state);
317 }
318 
319 /**
320  * @brief transfers the current state request to the state machine.
321  *
322  * This function takes the current state request from n775_state and transfers it to the state machine.
323  * It resets the value from n775_state to N775_STATE_NO_REQUEST
324  *
325  * @return retVal current state request, taken from N775_STATE_REQUEST_e
326  *
327  */
330 
332  retval = n775_state.stateReq;
335 
336  return (retval);
337 }
338 
341 
343  retVal = N775_CheckStateRequest(statereq);
344 
345  if ((retVal == N775_OK) || (retVal == N775_BUSY_OK) || (retVal == N775_OK_FROM_ERROR)) {
346  n775_state.stateReq = statereq;
347  }
349 
350  return (retVal);
351 }
352 
353 void N775_Trigger(void) {
355  bool n775_goToTrigger = true;
356 
357  N775_Write(0, 0, 0, 0); /* TODO: just to compile, remove */
358  N775_Read(0, 0, 0, 0); /* TODO: just to compile, remove */
359 
360  /* Check re-entrance of function */
361  if (N775_CheckReEntrance() > 0u) {
362  n775_goToTrigger = false;
363  }
364 
366  if (n775_state.timer > 0u) {
367  if ((--n775_state.timer) > 0u) {
369  n775_goToTrigger = false;
370  ; /* handle state machine only if timer has elapsed */
371  }
372  }
373  } else {
374  if (MIC_IsTxTransmitOngoing() == true) {
375  if (n775_state.timer > 0u) {
376  if ((--n775_state.timer) > 0u) {
378  n775_goToTrigger = false;
379  ; /* handle state machine only if timer has elapsed */
380  }
381  }
382  }
383  if (MIC_IsRxTransmitOngoing() == true) {
384  if (n775_state.timer > 0u) {
385  if ((--n775_state.timer) > 0u) {
387  n775_goToTrigger = false;
388  ; /* handle state machine only if timer has elapsed */
389  }
390  }
391  }
392  }
393 
394  if (n775_goToTrigger == true) {
395  switch (n775_state.state) {
396  /****************************UNINITIALIZED***********************************/
398  /* waiting for Initialization Request */
399  n775_stateReq = N775_TransferStateRequest();
400  if (n775_stateReq == N775_STATE_INIT_REQUEST) {
404  } else if (n775_stateReq == N775_STATE_NO_REQUEST) {
405  /* no actual request pending */
406  } else {
407  n775_state.errRequestCounter++; /* illegal request pending */
408  }
409  break;
410 
411  /****************************INITIALIZATION**********************************/
415  break;
416 
417  /****************************INITIALIZED*************************************/
421  break;
422 
423  /****************************START MEASUREMENT*******************************/
425  if (n775_state.subState == N775_ENTRY) {
427  /* Send first wake-up command */
428  N775_WakeUp(0u, 0u, 0u);
430  } else if (n775_state.subState == N775_SECOND_WAKEUP) {
432  /* Send second wake-up command */
433  N775_WakeUp(0u, 0u, 0u);
435  }
436  break;
437 
438  /****************************START MEASUREMENT*******************************/
440  if (n775_state.subState == N775_ENTRY) {
442  /* Enumerate first slave; first address is 1 and not 0; 0 means unenumerated */
443  N775_Write(
444  0u,
446  MC33775_SYS_COM_CFG_OFFSET,
447  (n775_enumerateAddress << MC33775_SYS_COM_CFG_DADD_POS) |
448  (MC33775_SYS_COM_CFG_BUSFW_ENABLED_ENUM_VAL << MC33775_SYS_COM_CFG_BUSFW_POS));
454  /* Enumerate next slave */
455  N775_Write(
456  0u,
458  MC33775_SYS_COM_CFG_OFFSET,
459  (n775_enumerateAddress << MC33775_SYS_COM_CFG_DADD_POS) |
460  (MC33775_SYS_COM_CFG_BUSFW_ENABLED_ENUM_VAL << MC33775_SYS_COM_CFG_BUSFW_POS));
464  } else {
465  /* All slave enumerated */
467  }
468  }
469  break;
470 
471  /****************************START MEASUREMENT*******************************/
473 
474  break;
475 
476  /****************************READ VOLTAGE************************************/
478 
479  break;
480 
481  /****************************BALANCE CONTROL*********************************/
483 
484  break;
485 
486  /****************************DEFAULT**************************/
487  default:
488  /* invalid state */
490  break;
491  }
492  }
493 
494  n775_state.triggerEntry--; /* reentrance counter */
495 }
496 
497 /**
498  * @brief checks the state requests that are made.
499  *
500  * This function checks the validity of the state requests.
501  * The results of the checked is returned immediately.
502  *
503  * @param statereq state request to be checked
504  *
505  * @return result of the state request that was made, taken from N775_RETURN_TYPE_e
506  */
508  N775_RETURN_TYPE_e retVal = N775_OK; /* Default: request is OK */
509 
511  /* init only allowed from the uninitialized state */
512  if (statereq == N775_STATE_INIT_REQUEST) {
514  retVal = N775_OK;
515  } else {
516  retVal = N775_ALREADY_INITIALIZED;
517  }
518  }
519 
520  } else {
521  retVal = N775_REQUEST_PENDING;
522  }
523  return retVal;
524 }
525 
526 /**
527  * @brief gets the measurement initialization status.
528  *
529  * @return retval true if a first measurement cycle was made, false otherwise
530  *
531  */
533  bool retval = false;
534 
538 
539  return (retval);
540 }
541 
542 /**
543  * @brief sets the measurement initialization status.
544  */
549 }
550 
553 }
554 
555 /**
556  * @brief sends a write command to the daisy-chain.
557  * @param message message to be sent
558  * @param buffer buffer used for SPI
559  */
560 static void N775_CopyStructToTxBuffer(N775_MESSAGE_s *message, uint16_t *buffer) {
561  uint8_t i = 0;
562 
563  buffer[0] = message->head;
564  buffer[1] = message->dataHead;
565  if (((message->dataLength) >= 1u) && ((message->dataLength) <= 4u)) {
566  for (i = 0; i < (message->dataLength); i++) {
567  buffer[i + 2u] = message->data[i];
568  }
569  buffer[i + 3u] = message->crc;
570  } else {
571  /* this should not happen, stay here */
573  }
574 }
575 
576 /**
577  * @brief sends a wake-up command to the daisy-chain.
578  *
579  * @param daisyChainAddress parameter CADD in the message format
580  * @param deviceAddress parameter DADD in the message format lies
581  * between 1 and 62, 63 means all devices
582  * @param registerAddress address of register to be written to
583  */
584 static void N775_WakeUp(uint16_t daisyChainAddress, uint16_t deviceAddress, uint16_t registerAddress) {
585  uint16_t messageCounter = 0u;
586 
587  /**
588  * Set Head part or WRITE message
589  */
590  /* Set command field of WRITE message */
591  n775_sentData.head = ((N775_CMD_WAKEUP_NOP & 0x3u) << 14u) | ((n775_sentData.head) & 0x3FFFu);
592  /* Set MADD field of WRITE message */
593  n775_sentData.head = ((N775_MADD_MASTER0 & 0x1u) << 13u) | ((n775_sentData.head) & 0xDFFFu);
594  /* Set CADD field of WRITE message */
595  n775_sentData.head = ((daisyChainAddress & 0x7u) << 10u) | ((n775_sentData.head) & 0xE3FFu);
596  /* Set DADD field of WRITE message; device addresses start at 1 */
597  n775_sentData.head = ((deviceAddress & 0x3Fu) << 4u) | ((n775_sentData.head) & 0xFC0Fu);
598  /* Set message counter; not used for WRITE messages, set to 0 */
599  (n775_sentData.head) = (messageCounter & 0xFu) | ((n775_sentData.head) & 0xFFF0u);
600 
601  /**
602  * Set Data head part or WRITE message
603  */
604  /* Set DATLEN field of WRITE message */
605  /* Number of registers addressed per message: 1, which is coded by DATLEN as 0 in the frame */
606  n775_sentData.dataHead = ((0u & 0x3u) << 14u) | ((n775_sentData.dataHead) & 0x3FFFu);
607  /* Set REGADD field of WRITE message (address of register to be addressed)) */
608  n775_sentData.dataHead = (registerAddress & 0x3FFFu) | ((n775_sentData.dataHead) & 0xC000u);
609 
610  /**
611  * Set Data part or WRITE message
612  */
613  /* Set data fields; only first field is used. */
614  /* Set all data fields to 0. */
615  n775_sentData.data[0] = 0u;
616  n775_sentData.data[1] = 0u;
617  n775_sentData.data[2] = 0u;
618  n775_sentData.data[3] = 0u;
619  /* Set data length, used internally in the strcut */
621 
622  /**
623  * Set CRC part or WRITE message
624  */
626 
627  /* Copy strcut data to SPI buffer */
629 
630  /* Send WRITE command to daisy-chain */
632 }
633 
634 /**
635  * @brief sends a write command to the daisy-chain.
636  *
637  * @param daisyChainAddress parameter CADD in the message format
638  * @param deviceAddress parameter DADD in the message format lies
639  * between 1 and 62, 63 means all devices
640  * @param registerAddress address of register to be written to
641  * @param data data to be written in the device register
642  */
643 static void N775_Write(uint16_t daisyChainAddress, uint16_t deviceAddress, uint16_t registerAddress, uint16_t data) {
644  uint16_t messageCounter = 0u;
645 
646  /**
647  * Set Head part or WRITE message
648  */
649  /* Set command field of WRITE message */
650  n775_sentData.head = ((N775_CMD_WRITE & 0x3u) << 14u) | ((n775_sentData.head) & 0x3FFFu);
651  /* Set MADD field of WRITE message */
652  n775_sentData.head = ((N775_MADD_MASTER0 & 0x1u) << 13u) | ((n775_sentData.head) & 0xDFFFu);
653  /* Set CADD field of WRITE message */
654  n775_sentData.head = ((daisyChainAddress & 0x7u) << 10u) | ((n775_sentData.head) & 0xE3FFu);
655  /* Set DADD field of WRITE message; device addresses start at 1 */
656  n775_sentData.head = ((deviceAddress & 0x3Fu) << 4u) | ((n775_sentData.head) & 0xFC0Fu);
657  /* Set message counter; not used for WRITE messages, set to 0 */
658  (n775_sentData.head) = (messageCounter & 0xFu) | ((n775_sentData.head) & 0xFFF0u);
659 
660  /**
661  * Set Data head part or WRITE message
662  */
663  /* Set DATLEN field of WRITE message */
664  /* Number of registers addressed per message: 1, which is coded by DATLEN as 0 in the frame */
665  n775_sentData.dataHead = ((0u & 0x3u) << 14u) | ((n775_sentData.dataHead) & 0x3FFFu);
666  /* Set REGADD field of WRITE message (address of register to be addressed)) */
667  n775_sentData.dataHead = (registerAddress & 0x3FFFu) | ((n775_sentData.dataHead) & 0xC000u);
668 
669  /**
670  * Set Data part or WRITE message
671  */
672  /* Set data fields; only first field is used. */
673  n775_sentData.data[0] = data;
674  /* Set other data fields to 0. */
675  n775_sentData.data[1] = 0u;
676  n775_sentData.data[2] = 0u;
677  n775_sentData.data[3] = 0u;
678  /* Set data length, used internally in the strcut */
680 
681  /**
682  * Set CRC part or WRITE message
683  */
685 
686  /* Copy strcut data to SPI buffer */
688 
689  /* Send WRITE command to daisy-chain */
691 }
692 
693 /**
694  * @brief sends a read command to the daisy-chain.
695  * @param daisyChainAddress parameter CADD in the message
696  * format
697  * @param deviceAddress parameter DADD in the message
698  * format lies between 1 and 62
699  * @param registerAddress address of first consecutive
700  * register to be read from
701  * @param totalNumberOfRequestedRegister total number of registers values
702  * sent by daisy-chain lies between 1
703  * and 256
704  */
705 static void N775_Read(
706  uint16_t daisyChainAddress,
707  uint16_t deviceAddress,
708  uint16_t registerAddress,
709  uint16_t totalNumberOfRequestedRegister) {
710  uint16_t messageCounter = 0u;
711  uint16_t dataLen = 0u;
712  uint16_t readParameters = 0u;
713 
714  /**
715  * Set Head part or WRITE message
716  */
717  /* Set command field of WRITE message */
718  n775_sentData.head = ((N775_CMD_READ & 0x3u) << 14u) | ((n775_sentData.head) & 0x3FFFu);
719  /* Set MADD field of WRITE message */
720  n775_sentData.head = ((N775_MADD_MASTER0 & 0x1u) << 13u) | ((n775_sentData.head) & 0xDFFFu);
721  /* Set CADD field of WRITE message */
722  n775_sentData.head = ((daisyChainAddress & 0x7u) << 10u) | ((n775_sentData.head) & 0xE3FFu);
723  /* Set DADD field of WRITE message; device addresses start at 1 */
724  n775_sentData.head = ((deviceAddress & 0x3Fu) << 4u) | ((n775_sentData.head) & 0xFC0Fu);
725  /* Set message counter; not used for READ messages, set to 0 */
726  (n775_sentData.head) = (messageCounter & 0xFu) | ((n775_sentData.head) & 0xFFF0u);
727 
728  /**
729  * Set Data head part or WRITE message
730  */
731  /* Set DATALEN field of WRITE message; number of registers addressed per mssage: 1 */
732  n775_sentData.dataHead = ((dataLen & 0x3u) << 14u) | ((n775_sentData.dataHead) & 0x3FFFu);
733  /* Set REGADD field of WRITE message (address of register t be addressed)) */
734  n775_sentData.dataHead = (registerAddress & 0x3FFFu) | ((n775_sentData.dataHead) & 0xC000u);
735 
736  /**
737  * Set Data part or WRITE message
738  */
739  readParameters = (0u << 11u) + /* First five bits must be written with 0 */
740  (0u << 10u) + /* PAD = 0: no padding */
741  (0u << 8u) + /* RESPLEN = 0: oneregister per frame */
742  ((totalNumberOfRequestedRegister - 1u) << 0); /* NUMREG: number of consecutive registers to read */
743  /* 0 means 1 register, therefore 1u is subtracted */
744  /* Set data field; only first of the 4 fields is used */
745  n775_sentData.data[0] = readParameters;
746  /* Set other data fields to 0. */
747  n775_sentData.data[1] = 0u;
748  n775_sentData.data[2] = 0u;
749  n775_sentData.data[3] = 0u;
750  /* Set data length, used internally in the struct */
752 
753  /**
754  * Set CRC part or WRITE message
755  */
757 
758  /* Copy strcut data to SPI buffer */
760 
761  n775_state.totalMessages = totalNumberOfRequestedRegister;
762  n775_state.remainingMessages = totalNumberOfRequestedRegister;
763 
764  /* Send WRITE command to daisy-chain */
766 }
767 
768 /*========== Externalized Static Function Implementations (Unit Test) =======*/
DATA_BLOCK_MIN_MAX::nrModuleMinimumTemperature
uint16_t nrModuleMinimumTemperature[BS_NR_OF_STRINGS]
Definition: database_cfg.h:176
os.h
Implementation of the tasks used by the system, headers.
N775_TIME_AFTER_ENUMERATION_MS
#define N775_TIME_AFTER_ENUMERATION_MS
Definition: n775_cfg.h:138
N775_CMD_WAKEUP_NOP
@ N775_CMD_WAKEUP_NOP
Definition: n775_defs.h:69
N775_TransferStateRequest
N775_STATE_REQUEST_e N775_TransferStateRequest()
transfers the current state request to the state machine.
Definition: n775.c:328
BS_NR_OF_BAT_CELLS
#define BS_NR_OF_BAT_CELLS
Definition: battery_system_cfg.h:156
n775_balancingControl
static DATA_BLOCK_BALANCING_CONTROL_s n775_balancingControl
Definition: n775.c:105
N775_STATE::stateReq
N775_STATE_REQUEST_e stateReq
Definition: n775_defs.h:229
N775_STATEMACH_INITIALIZED
@ N775_STATEMACH_INITIALIZED
Definition: n775_defs.h:87
N775_CMD_WRITE
@ N775_CMD_WRITE
Definition: n775_defs.h:71
TEST_N775_SetFirstMeasurementCycleFinished
void TEST_N775_SetFirstMeasurementCycleFinished(N775_STATE_s *n775_state)
Definition: n775.c:551
N775_Initialize_Database
static void N775_Initialize_Database(void)
in the database, initializes the fields related to the N775 driver.
Definition: n775.c:212
N775_StateTransition
static void N775_StateTransition(N775_STATEMACH_e state, uint8_t substate, uint16_t timer_ms)
function for setting N775_Trigger state transitions
Definition: n775.c:256
DATA_BLOCK_MIN_MAX::nrSensorMinimumTemperature
uint16_t nrSensorMinimumTemperature[BS_NR_OF_STRINGS]
Definition: database_cfg.h:177
N775_STATE::timer
uint16_t timer
Definition: n775_defs.h:228
BS_NR_OF_TEMP_SENSORS
#define BS_NR_OF_TEMP_SENSORS
Definition: battery_system_cfg.h:158
DATA_BLOCK_MIN_MAX::maximumTemperature_ddegC
int16_t maximumTemperature_ddegC[BS_NR_OF_STRINGS]
Definition: database_cfg.h:178
DATA_BLOCK_MIN_MAX::nrModuleMaximumTemperature
uint16_t nrModuleMaximumTemperature[BS_NR_OF_STRINGS]
Definition: database_cfg.h:179
N775_Read
static void N775_Read(uint16_t daisyChainAddress, uint16_t deviceAddress, uint16_t registerAddress, uint16_t totalNumberOfRequestedRegister)
sends a read command to the daisy-chain.
Definition: n775.c:705
DATA_BLOCK_ID_CELL_TEMPERATURE_BASE
@ DATA_BLOCK_ID_CELL_TEMPERATURE_BASE
Definition: database_cfg.h:95
N775_MESSAGE::data
uint16_t data[4]
Definition: n775_defs.h:249
DATA_BLOCK_MIN_MAX::header
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:162
DATA_BLOCK_MIN_MAX::nrModuleMinimumCellVoltage
uint16_t nrModuleMinimumCellVoltage[BS_NR_OF_STRINGS]
Definition: database_cfg.h:170
N775_MESSAGE::crc
uint16_t crc
Definition: n775_defs.h:250
DATA_BLOCK_MIN_MAX::nrSensorMaximumTemperature
uint16_t nrSensorMaximumTemperature[BS_NR_OF_STRINGS]
Definition: database_cfg.h:180
N775_STATE_REQUEST_e
N775_STATE_REQUEST_e
Definition: n775_defs.h:120
N775_ENTRY
@ N775_ENTRY
Definition: n775_defs.h:96
n775_RXbuffer
uint16_t n775_RXbuffer[N775_MAX_N_BYTES_FOR_DATA_RECEPTION]
Definition: n775.c:81
DATA_BLOCK_MIN_MAX::nrCellMinimumCellVoltage
uint16_t nrCellMinimumCellVoltage[BS_NR_OF_STRINGS]
Definition: database_cfg.h:171
diag.h
Diagnosis driver header.
N775_STATE_INIT_REQUEST
@ N775_STATE_INIT_REQUEST
Definition: n775_defs.h:121
MIC_IsRxTransmitOngoing
bool MIC_IsRxTransmitOngoing(void)
gets the SPI transmit status.
Definition: nxp_mic_dma.c:79
N775_STATE::subState
uint8_t subState
Definition: n775_defs.h:231
N775_SetStateRequest
N775_RETURN_TYPE_e N775_SetStateRequest(N775_STATE_REQUEST_e statereq)
sets the current state request of the state variable n775_state.
Definition: n775.c:339
DATA_WRITE_DATA
#define DATA_WRITE_DATA(...)
Definition: database.h:82
DATA_BLOCK_CELL_TEMPERATURE::state
uint8_t state
Definition: database_cfg.h:136
N775_MESSAGE::dataLength
uint16_t dataLength
Definition: n775_defs.h:251
mic_dma.h
Headers for the driver for the general DMA module of monitoring ICs.
n775_cellTemperature
static DATA_BLOCK_CELL_TEMPERATURE_s n775_cellTemperature
Definition: n775.c:103
N775_STATEMACH_WAKEUP
@ N775_STATEMACH_WAKEUP
Definition: n775_defs.h:85
n775_enumerateAddress
uint8_t n775_enumerateAddress
Definition: n775.c:88
N775_STATE::firstMeasurementMade
STD_RETURN_TYPE_e firstMeasurementMade
Definition: n775_defs.h:236
N775_CheckReEntrance
uint8_t N775_CheckReEntrance(void)
re-entrance check of N775 state machine trigger function
Definition: n775.c:274
MIC_IsTxTransmitOngoing
bool MIC_IsTxTransmitOngoing(void)
gets the SPI transmit status.
Definition: nxp_mic_dma.c:73
N775_CHECK_ENUMERATION
@ N775_CHECK_ENUMERATION
Definition: n775_defs.h:98
N775_TIME_DAISYCHAIN_WAKEUP_MS
#define N775_TIME_DAISYCHAIN_WAKEUP_MS
Definition: n775_cfg.h:128
N775_STATE::remainingMessages
uint16_t remainingMessages
Definition: n775_defs.h:242
N775_TWAKECOM_MS
#define N775_TWAKECOM_MS
Definition: n775_cfg.h:117
DATA_BLOCK_BALANCING_CONTROL::header
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:249
FAS_ASSERT
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:233
N775_STATEMACH_UNINITIALIZED
@ N775_STATEMACH_UNINITIALIZED
Definition: n775_defs.h:83
OS_ExitTaskCritical
void OS_ExitTaskCritical(void)
Exit Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os.c:178
DATA_BLOCK_CELL_TEMPERATURE::header
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:135
DATA_BLOCK_ID_MIN_MAX
@ DATA_BLOCK_ID_MIN_MAX
Definition: database_cfg.h:75
DATA_BLOCK_ID_CELL_VOLTAGE_BASE
@ DATA_BLOCK_ID_CELL_VOLTAGE_BASE
Definition: database_cfg.h:94
STD_OK
@ STD_OK
Definition: fstd_types.h:72
N775_SetFirstMeasurementCycleFinished
static void N775_SetFirstMeasurementCycleFinished(N775_STATE_s *n775_state)
sets the measurement initialization status.
Definition: n775.c:545
N775_REQUEST_PENDING
@ N775_REQUEST_PENDING
Definition: n775_defs.h:131
N775_ENTRY_UNINITIALIZED
@ N775_ENTRY_UNINITIALIZED
Definition: n775_defs.h:105
N775_CheckStateRequest
static N775_RETURN_TYPE_e N775_CheckStateRequest(N775_STATE_REQUEST_e statereq)
checks the state requests that are made.
Definition: n775.c:507
N775_OK
@ N775_OK
Definition: n775_defs.h:129
n775_minMax
static DATA_BLOCK_MIN_MAX_s n775_minMax
Definition: n775.c:104
OS_EnterTaskCritical
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os.c:174
N775_GetState
N775_STATEMACH_e N775_GetState(void)
gets the current state.
Definition: n775.c:315
STD_NOT_OK
@ STD_NOT_OK
Definition: fstd_types.h:73
N775_STATE::errRequestCounter
uint32_t errRequestCounter
Definition: n775_defs.h:234
N775_MESSAGE::dataHead
uint16_t dataHead
Definition: n775_defs.h:248
DATA_BLOCK_CELL_VOLTAGE::cellVoltage_mV
int16_t cellVoltage_mV[BS_NR_OF_STRINGS][BS_NR_OF_BAT_CELLS]
Definition: database_cfg.h:122
N775_N_N775
#define N775_N_N775
Definition: n775_cfg.h:69
DATA_BLOCK_CELL_TEMPERATURE
Definition: database_cfg.h:131
DATA_BLOCK_MIN_MAX::nrModuleMaximumCellVoltage
uint16_t nrModuleMaximumCellVoltage[BS_NR_OF_STRINGS]
Definition: database_cfg.h:172
DATA_BLOCK_ID_BALANCING_CONTROL
@ DATA_BLOCK_ID_BALANCING_CONTROL
Definition: database_cfg.h:77
DATA_BLOCK_BALANCING_CONTROL::balancingState
uint8_t balancingState[BS_NR_OF_STRINGS][BS_NR_OF_BAT_CELLS]
Definition: database_cfg.h:253
N775_STATE
Definition: n775_defs.h:227
DATA_BLOCK_MIN_MAX::maximumCellVoltage_mV
int16_t maximumCellVoltage_mV[BS_NR_OF_STRINGS]
Definition: database_cfg.h:167
N775_STATE::state
N775_STATEMACH_e state
Definition: n775_defs.h:230
n775_receivedData
N775_MESSAGE_s n775_receivedData
Definition: n775.c:86
N775_IsFirstMeasurementCycleFinished
bool N775_IsFirstMeasurementCycleFinished(void)
gets the measurement initialization status.
Definition: n775.c:532
DATA_BLOCK_CELL_VOLTAGE
Definition: database_cfg.h:115
N775_ERROR
@ N775_ERROR
Definition: n775_defs.h:138
DATA_BLOCK_MIN_MAX
Definition: database_cfg.h:158
N775_SAVELASTSTATES
#define N775_SAVELASTSTATES()
Definition: n775.c:73
N775_CopyStructToTxBuffer
static void N775_CopyStructToTxBuffer(N775_MESSAGE_s *message, uint16_t *buffer)
sends a write command to the daisy-chain.
Definition: n775.c:560
N775_WakeUp
static void N775_WakeUp(uint16_t daisyChainAddress, uint16_t deviceAddress, uint16_t registerAddress)
sends a wake-up command to the daisy-chain.
Definition: n775.c:584
N775_STATEMACH_BALANCECONTROL
@ N775_STATEMACH_BALANCECONTROL
Definition: n775_defs.h:91
database.h
Database module header.
N775_STATEMACH_STARTMEAS
@ N775_STATEMACH_STARTMEAS
Definition: n775_defs.h:89
DATA_BLOCK_MIN_MAX::minimumTemperature_ddegC
int16_t minimumTemperature_ddegC[BS_NR_OF_STRINGS]
Definition: database_cfg.h:175
N775_STATEMACH_ENUMERATE
@ N775_STATEMACH_ENUMERATE
Definition: n775_defs.h:86
N775_SECOND_WAKEUP
@ N775_SECOND_WAKEUP
Definition: n775_defs.h:97
N775_STATEMACH_READVOLTAGE
@ N775_STATEMACH_READVOLTAGE
Definition: n775_defs.h:90
N775_STATE::checkSpiFlag
STD_RETURN_TYPE_e checkSpiFlag
Definition: n775_defs.h:237
N775_STATEMACH_e
N775_STATEMACH_e
Definition: n775_defs.h:82
n775_TXbuffer
uint16_t n775_TXbuffer[N775_TX_MESSAGE_LENGTH]
Definition: n775.c:82
N775_MESSAGE::head
uint16_t head
Definition: n775_defs.h:247
n775_CrcAddItem
uint16_t n775_CrcAddItem(uint16_t remainder, uint16_t item)
Called to calculate the CRC of a message. NXP function.
Definition: n775.c:162
n775_CalcCrc
uint16_t n775_CalcCrc(const N775_MESSAGE_s *msg)
Calculate the CRC of a message. NXP function.
Definition: n775.c:191
N775_MAX_N_BYTES_FOR_DATA_RECEPTION
#define N775_MAX_N_BYTES_FOR_DATA_RECEPTION
Definition: n775_cfg.h:151
N775_GetStateRequest
N775_STATE_REQUEST_e N775_GetStateRequest(void)
gets the current state request.
Definition: n775.c:298
n775.h
Headers for the driver for the MC33775A monitoring chip.
n775_state
N775_STATE_s n775_state
Definition: n775.c:108
DATA_BLOCK_MIN_MAX::nrCellMaximumCellVoltage
uint16_t nrCellMaximumCellVoltage[BS_NR_OF_STRINGS]
Definition: database_cfg.h:173
DATA_BLOCK_MIN_MAX::minimumCellVoltage_mV
int16_t minimumCellVoltage_mV[BS_NR_OF_STRINGS]
Definition: database_cfg.h:165
N775_BUSY_OK
@ N775_BUSY_OK
Definition: n775_defs.h:130
n775_cellVoltage
static DATA_BLOCK_CELL_VOLTAGE_s n775_cellVoltage
Definition: n775.c:102
N775_SendData
#define N775_SendData(txbuf, rxbuf, length)
Definition: n775_cfg.h:156
FAS_TRAP
#define FAS_TRAP
Define that evaluates to essential boolean false thus tripping an assert.
Definition: fassert.h:108
N775_STATE::totalMessages
uint16_t totalMessages
Definition: n775_defs.h:241
N775_STATE_NO_REQUEST
@ N775_STATE_NO_REQUEST
Definition: n775_defs.h:122
N775_TX_MESSAGE_LENGTH
#define N775_TX_MESSAGE_LENGTH
Definition: n775_cfg.h:79
N775_CMD_READ
@ N775_CMD_READ
Definition: n775_defs.h:70
N775_MESSAGE
Definition: n775_defs.h:246
n775_sentData
N775_MESSAGE_s n775_sentData
Definition: n775.c:85
N775_MADD_MASTER0
@ N775_MADD_MASTER0
Definition: n775_defs.h:77
N775_Write
static void N775_Write(uint16_t daisyChainAddress, uint16_t deviceAddress, uint16_t registerAddress, uint16_t data)
sends a write command to the daisy-chain.
Definition: n775.c:643
DATA_BLOCK_BALANCING_CONTROL
Definition: database_cfg.h:245
DATA_BLOCK_CELL_VOLTAGE::state
uint8_t state
Definition: database_cfg.h:120
DATA_BLOCK_CELL_VOLTAGE::header
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:119
N775_STATE::triggerEntry
uint8_t triggerEntry
Definition: n775_defs.h:235
N775_STATEMACH_SHORTTIME
#define N775_STATEMACH_SHORTTIME
Definition: n775_cfg.h:111
N775_OK_FROM_ERROR
@ N775_OK_FROM_ERROR
Definition: n775_defs.h:137
N775_STATEMACH_INITIALIZATION
@ N775_STATEMACH_INITIALIZATION
Definition: n775_defs.h:84
TEST_N775_CheckReEntrance
uint8_t TEST_N775_CheckReEntrance(void)
Definition: n775.c:287
BS_NR_OF_STRINGS
#define BS_NR_OF_STRINGS
Definition: battery_system_cfg.h:89
N775_RETURN_TYPE_e
N775_RETURN_TYPE_e
Definition: n775_defs.h:128
N775_ALREADY_INITIALIZED
@ N775_ALREADY_INITIALIZED
Definition: n775_defs.h:139
DATA_BLOCK_CELL_TEMPERATURE::cellTemperature_ddegC
int16_t cellTemperature_ddegC[BS_NR_OF_STRINGS][BS_NR_OF_TEMP_SENSORS]
Definition: database_cfg.h:137
N775_Trigger
void N775_Trigger(void)
trigger function for the N775 driver state machine.
Definition: n775.c:353
DATA_BLOCKHEADER::uniqueId
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:109