/*
 * Copyright (c) 2022, Shenzhen CVA Innovation CO.,LTD
 * All rights reserved.
 *
 * Shenzhen CVA Innovation CO.,LTD (CVA chip) is supplying this file for use
 * exclusively with CVA's microcontroller products. This file can be freely
 * distributed within development tools that are supporting such microcontroller
 * products.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
 * CVA SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
 * OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 */

#ifndef _ESTIDC_H_
#define _ESTIDC_H_

/*! \brief  Contains public interface to various functions related
 *          to the DC current Estimation (ESTIDC) object.
 *
 */

/*******************************************************************************
 * the includes
 ******************************************************************************/

#include <stdint.h>
#include "common/iqmath/iqmath.h"

#ifdef __cplusplus
extern "C" {
#endif

/*******************************************************************************
 * the defines
 ******************************************************************************/

/*******************************************************************************
 * the typedefs
 ******************************************************************************/

/*! \brief  Defines the phase enumeration
 */
typedef enum
{
    ESTIDC_PHASE_A = 0,
    ESTIDC_PHASE_B,
    ESTIDC_PHASE_C,
    ESTIDC_PHASE_U,
    ESTIDC_PHASE_V,
    ESTIDC_PHASE_W,

    ESTIDC_PHASE_Num
} Estidc_PhaseType;

/*! \brief  Defines all the parameters used to initialize ESTIDC object
 */
typedef struct _Estidc_ParamsType_
{
    _iq     Tdb;         /*!< Percentage of dead-band */
    uint8_t phaseNumber; /*!< Number of phase */
} Estidc_ParamsType;

/*! \brief data of each phase
 */
typedef struct _Estidc_PhaseDataType
{
    _iq I;         /*!< Current of ever phase */
    _iq T;         /*!< 100%-(duty of ever phase ) */
    _iq prevI;     /*!< The last Current of ever phase */
    _iq prevT;     /*!< The last 100%-(duty of ever phase) */
    _iq prevPrevT; /*!< The previous last 100%-(duty of ever phase) */
} Estidc_PhaseDataType;

/*! \brief Defines the ESTIDC object
 */
typedef struct _EstidcType_
{
    _iq Ia; /*!< Input: current of phase A */
    _iq Ib; /*!< Input: current of phase B */
    _iq Ic; /*!< Input: current of phase C */
    _iq Iu; /*!< Input: current of phase U */
    _iq Iv; /*!< Input: current of phase V */
    _iq Iw; /*!< Input: current of phase W */
    _iq Ta; /*!< Input: 100%-(duty) of phase A */
    _iq Tb; /*!< Input: 100%-(duty) of phase B */
    _iq Tc; /*!< Input: 100%-(duty) of phase C */
    _iq Tu; /*!< Input: 100%-(duty) of phase U */
    _iq Tv; /*!< Input: 100%-(duty) of phase V */
    _iq Tw; /*!< Input: 100%-(duty) of phase W */

    _iq      Tdb;         /*!< Parameter: Percentage of dead-band */
    uint16_t phaseNumber; /*!< Parameter: Number of phase */

    Estidc_PhaseDataType data[ESTIDC_PHASE_Num]; /*!< Internal Variable: data of each phase */

    _iq estIdc; /*!< Output: The estimated DC current */
} EstidcType;

/*******************************************************************************
 * the globals
 ******************************************************************************/

/*******************************************************************************
 * the function prototypes
 ******************************************************************************/

/*! \brief Initialize the ESTIDC object
 *
 * This function initalizes Estidc by setting configuration parameters
 *
 * \param[in] obj : pointer to Estidc instance
 * \param[in] pParams : pointer to Estidc configuration parameters
 * \return the MCUs authorized evaluation
 *         - -1 : Functions not available
 *         -  0 : Functions available
 */
extern int32_t Estidc_Init(EstidcType *obj, const Estidc_ParamsType *pParams);

/*! \brief Run the estimation
 *
 * This function runs Estidc function, calculates idc estimation value
 *
 * \param[in] obj : pointer to Estidc instance
 * \return the MCUs authorized evaluation
 *         - -1 : Functions not available
 *         -  0 : Functions available
 */
extern int32_t Estidc_Run(EstidcType *obj);

/*! \brief Update the parameters of ESTIDC object
 *
 * This function updates the Estidc function configuration parameters
 *
 * \param[in] obj : pointer to Estidc instance
 * \param[in] Tdb : the percentage of dead-band
 */
extern void Estidc_UpdateParams(EstidcType *obj, _iq Tdb);

#ifdef __cplusplus
}
#endif /* extern "C" */

#endif /* _ESTIDC_H_ */
