/*
 * File: BSPR_MdulVoltCoord.c
 *
 * Code generated for Simulink model 'BSPR_MdulVoltCoord'.
 *
 * Model version                  : 1.967F
 * Simulink Coder version         : 8.11F (R2016b) 25-Aug-2016
 * C/C++ source code generated on : Mon Mar 02 11:53:09 2020
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: Infineon->TriCore
 * Code generation objectives:
 *    1. MISRA C:2012 guidelines
 *    2. Execution efficiency
 * Validation result: Not run
 */

#include "BSPR_MdulVoltCoord.h"
#include "BSPR_MdulVoltCoord_private.h"

/* Exported data definition */

/* Definition for custom storage class: OutputSignal */
volatile uint8_T BSPR_cntModuSerNum;   /* modul number in current battery package */
volatile real32_T BSPR_uMdulVoltAvrg;  /* Calculated average module voltage */
volatile real32_T BSPR_uMdulVoltMax;   /* Calculated max module voltage */
volatile real32_T BSPR_uMdulVoltMin;   /* Calculated min module voltage */
volatile real32_T BSPR_uMdulVolt_A[16];/* Calculated modul voltage after coordinating */

/* Block states (auto storage) */
DW_BSPR_MdulVoltCoord_T BSPR_MdulVoltCoord_DW;

/* Forward declaration for local functions */
static void BSPR_enter_internal_SglProc_Stt(real32_T *BSPR_uSumVoltMdulAvl,
  uint8_T *BSPR_cntVoltMdulAvl);

/* Function for Chart: '<S4>/MdulVoltSglProc' */
static void BSPR_enter_internal_SglProc_Stt(real32_T *BSPR_uSumVoltMdulAvl,
  uint8_T *BSPR_cntVoltMdulAvl)
{
  uint8_T i;
  int32_T tmp;

  /* SglProc */
  i = 0U;
  *BSPR_uSumVoltMdulAvl = 0.0F;
  *BSPR_cntVoltMdulAvl = 0U;
  BSPR_uMdulVoltMin = 409.4F;
  BSPR_uMdulVoltMax = 0.0F;
  while (i < BSPR_cntModuSerNum) {
    if (COMR_uMdulVolt_A[i] != 409.5F) {
      *BSPR_uSumVoltMdulAvl += COMR_uMdulVolt_A[i];
      tmp = *BSPR_cntVoltMdulAvl + 1;
      if (tmp > 255) {
        tmp = 255;
      }

      *BSPR_cntVoltMdulAvl = (uint8_T)tmp;
      if (COMR_uMdulVolt_A[i] < BSPR_uMdulVoltMin) {
        BSPR_uMdulVoltMin = COMR_uMdulVolt_A[i];
      }

      if (COMR_uMdulVolt_A[i] > BSPR_uMdulVoltMax) {
        BSPR_uMdulVoltMax = COMR_uMdulVolt_A[i];
      }
    }

    i++;
  }
}

/* Model step function */
void BSPR_MdulVoltCoord_step(void)
{
  real32_T BSPR_uSumVoltMdulAvl;
  uint8_T BSPR_cntVoltMdulAvl;
  BSPR_cntModuSerNum = BSPR_cntModuSerNum_C;
  if (BSPR_MdulVoltCoord_DW.is_active_c10_BSPR_MdulVoltCoor == 0U) {
    BSPR_MdulVoltCoord_DW.is_active_c10_BSPR_MdulVoltCoor = 1U;
    BSPR_enter_internal_SglProc_Stt(&BSPR_uSumVoltMdulAvl, &BSPR_cntVoltMdulAvl);
  } else {
    BSPR_enter_internal_SglProc_Stt(&BSPR_uSumVoltMdulAvl, &BSPR_cntVoltMdulAvl);
  }

  if (BSPR_cntVoltMdulAvl == 0) {
    BSPR_cntVoltMdulAvl = BSPR_numModuDivPrtNum_C;
  }

  BSPR_uMdulVoltAvrg = BSPR_uSumVoltMdulAvl / (real32_T)BSPR_cntVoltMdulAvl;
  if (BSPR_MdulVoltCoord_DW.is_active_c1_BSPR_MdulVoltCoord == 0U) {
    BSPR_MdulVoltCoord_DW.is_active_c1_BSPR_MdulVoltCoord = 1U;

    /*  SglCoord */
    for (BSPR_cntVoltMdulAvl = 0U; BSPR_cntVoltMdulAvl < BSPR_cntModuSerNum_C;
         BSPR_cntVoltMdulAvl++) {
      if (COMR_uMdulVolt_A[BSPR_cntVoltMdulAvl] != 409.5F) {
        BSPR_uMdulVolt_A[BSPR_cntVoltMdulAvl] =
          COMR_uMdulVolt_A[BSPR_cntVoltMdulAvl];
      } else {
        BSPR_uMdulVolt_A[BSPR_cntVoltMdulAvl] = BSPR_uMdulVoltAvrg;
      }
    }
  } else {
    /*  SglCoord */
    for (BSPR_cntVoltMdulAvl = 0U; BSPR_cntVoltMdulAvl < BSPR_cntModuSerNum_C;
         BSPR_cntVoltMdulAvl++) {
      if (COMR_uMdulVolt_A[BSPR_cntVoltMdulAvl] != 409.5F) {
        BSPR_uMdulVolt_A[BSPR_cntVoltMdulAvl] =
          COMR_uMdulVolt_A[BSPR_cntVoltMdulAvl];
      } else {
        BSPR_uMdulVolt_A[BSPR_cntVoltMdulAvl] = BSPR_uMdulVoltAvrg;
      }
    }
  }
}

/* Model initialize function */
void BSPR_MdulVoltCoord_initialize(void)
{
  /* Registration code */

  /* block I/O */

  /* custom signals */
  BSPR_uMdulVoltAvrg = 0.0F;
  BSPR_uMdulVoltMax = 0.0F;
  BSPR_uMdulVoltMin = 0.0F;

  {
    int32_T i;
    for (i = 0; i < 16; i++) {
      BSPR_uMdulVolt_A[i] = 0.0F;
    }
  }

  BSPR_cntModuSerNum = ((uint8_T)8U);
  BSPR_cntModuSerNum = BSPR_cntModuSerNum_C;
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
