/*
 * 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 _PWMDBC_H_
#define _PWMDBC_H_

/*! \brief  Contains public interface to various functions related
 *          to the PWM Dead-band Compensation (PWMDBC) object.
 */

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

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

#ifdef __cplusplus
extern "C" {
#endif

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

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

/*! \brief  Defines all the parameters used to initialize PWMDBC object
 */
typedef struct _Pwmdbc_ParamsType_
{
    float Tdb;             /*!< The percent taking account of dead-band time and rise/fall time */
    float zeroCurrentTh_A; /*!< The threshold for zero current, in A */
    float IBase_A;         /*!< Base value of current used for P.U. */
    float gain;            /*!< Gain value for the compensation */
} Pwmdbc_ParamsType;

/*! \brief  Defines the dynamic parameters used to update PWMDBC object
 */
typedef struct _Pwmdbc_DynamicParamsType_
{
    float Tdb;             /*!< The percent taking account of dead-band time and rise/fall time */
    float zeroCurrentTh_A; /*!< The threshold for zero current, in A */
    float gain;            /*!< Gain value for the compensation */
} Pwmdbc_DynamicParamsType;

/*! \brief Defines the PWMDBC object
 */
typedef struct _PwmdbcType_
{
    _iq   TaUncomp;         /*!< Input: (duty of phase A) - 50% before compensation */
    _iq   TbUncomp;         /*!< Input: (duty of phase B) - 50% before compensation */
    _iq   TcUncomp;         /*!< Input: (duty of phase C) - 50% before compensation */
    _iq   Ia_pu;            /*!< Input: The sampled current of phase A */
    _iq   Ib_pu;            /*!< Input: The sampled current of phase B */
    _iq   Ic_pu;            /*!< Input: The sampled current of phase C */

    _iq   Tdb;              /*!< Parameter: The percent taking account of dead-band time and rise/fall time */
    _iq   scale;            /*!< Parameter: The scale of dead-band percent */
    _iq   zeroCurrentTh_pu; /*!< Parameter: The threshold for zero current, in P.U. */
    float IBase_A;          /*!< Parameter: Base value of current used for P.U. */

    _iq   TaComp;           /*!< Input: (duty of phase A) - 50% after compensation */
    _iq   TbComp;           /*!< Input: (duty of phase B) - 50% after compensation */
    _iq   TcComp;           /*!< Input: (duty of phase C) - 50% after compensation */
} PwmdbcType;

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

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

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

/*! \brief Run the PWMDBC process
 *
 * This function runs the pwm dead-band compensation function
 *
 * \param[in] obj : pointer to Pwmdbc instance
 * \return the MCUs authorized evaluation
 *         - -1 : Functions not available
 *         -  0 : Functions available
 */
extern int32_t Pwmdbc_Run(PwmdbcType *obj);

/*! \brief Update the parameters of PWMDBC
 *
 * This function updates the Pwmdbc function configuration parameters
 *
 * \param[in] obj : pointer to Pwmdbc instance
 * \param[in] pParams : pointer to control configuration paramter
 * \return the MCUs authorized evaluation
 *         - -1 : Functions not available
 *         -  0 : Functions available
 */
extern int32_t Pwmdbc_UpdateParams(PwmdbcType *obj, const Pwmdbc_DynamicParamsType *pParams);

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

#endif /* _PWMDBC_H_ */
