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

/*! \brief  Contains public interface to various functions related
 *          to the phase current unbalance detection (PCUD) object.
 */

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

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

#ifdef __cplusplus
extern "C" {
#endif

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

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

/*! \brief  Defines all the parameters used to initialize PCUD object
 */
typedef struct _Pcud_ParamsType_
{
    float unbalanceTh;      /*!< the threshold of unbalance, normally will be 1.2 (120%) */
    float minDetectI_A;     /*!< the minimum detect current in A */
    float minDetectFreq_Hz; /*!< the minimum detect frequency in Hz */
    float IBase_A;          /*!< the current base value in A */
    float fBase_Hz;         /*!< the base value of frequency used for P.U. */
} Pcud_ParamsType;

/*! \brief Defines the PCUD object
 */
typedef struct _PcudType_
{
    _iq  Ia_rms;           /*!< Input: phase A current */
    _iq  Ib_rms;           /*!< Input: phase B current */
    _iq  Ic_rms;           /*!< Input: phase C current */
    _iq  speed_pu;         /*!< Input: speed feedback in P.U. */

    _iq  unbalanceTh;      /*!< Parameter: The threshold of unbalance */
    _iq  minDetectI_pu;    /*!< Parameter: The minimum detect current in P.U. */
    _iq  minDetectFreq_pu; /*!< Parameter: The minimum detect frequency in P.U. */

    bool isUnbalance;      /*!< Output: Whether is unbalanced */
} PcudType;

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

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

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

/*! \brief Run the PCUD process
 *
 * This function runs the phase current unbalance detection
 *
 * \param[in] obj : pointer to Pcud instance
 * \return the MCUs authorized evaluation
 *         - -1 : Functions not available
 *         -  0 : Functions available
 */
extern int32_t Pcud_Run(PcudType *obj);

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

#endif /* _PCUD_H_ */
