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

/*! \brief  Contains public interface to various functions related
 *          to the Initial Position Detection (IPD) using
 *          High Frequency Injection method (HFI) object.
 */

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

#include <stdint.h>
#include "common/iqmath/iqmath.h"
#include "common/filter/bp_so.h"
#include "common/filter/iir_fo.h"
#include "common/pid/pid.h"
#include "md_lib/fag/fag.h"
#include "md_lib/park/park.h"
#include "md_lib/clarke/clarke.h"

#ifdef __cplusplus
extern "C" {
#endif

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

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

/*! \brief  Defines all the parameters used to initialize HFI object
 */
typedef struct _Hfi_ParamsType_
{
    float runFreq_kHz;    /*!< Frequency to run the process */
    float injectFreq_kHz; /*!< The injected frequency in kHz */
    float injectVd_pVdc;  /*!< The injected frequency per Vdc */
    float lpfPole_rps;    /*!< Pole value of low pass filter in rad/s */
    float pllPole_rps;    /*!< Pole value of PLL in rad/s */
    float Ld_mH;          /*!< d-axis inductance in mH */
    float Lq_mH;          /*!< q-axis inductance in mH */
    float ratedVdc_V;     /*!< Rated DC bus voltage in Volts */
    float fBase_Hz;       /*!< Base value of frequency used for P.U. */
    float compFactor;     /*!< The factor to compensate the high-frequency voltage and current */
} Hfi_ParamsType;

/*! \brief Defines the HFI object
 */
typedef struct _HfiType_
{
    _iq        Ia_pu;        /*!< Input: The phase-A current */
    _iq        Ib_pu;        /*!< Input: The phase-B current */
    _iq        Ic_pu;        /*!< Input: The phase-C current */

    _iq        VdAmplitude;  /*!< Internal Variable: The amplitude of high-frequency Vd */
    _iq        compAngle_pu; /*!< Internal Variable: The compensation angle */
    _iq        angleFactor;  /*!< Internal Variable: Factor to calculate angle from speed */
    _iq        nextAngle_pu; /*!< Internal variable: The angle for next control loop */
    ClarkeType clarkeI;      /*!< Internal Variable: The Clarke transform for current */
    IirFoType  lpf;          /*!< Internal Variable: The low pass filter for the Iq */
    BpSoType   IalphaBpf;    /*!< Internal Variable: The band-pass filter for the alpha-axis current */
    BpSoType   IbetaBpf;     /*!< Internal Variable: The band-pass filter for the beta-axis current */
    FagType    fag;          /*!< Internal Variable: The high-frequency angle generator */
    ParkType   park;         /*!< Internal Variable: The PARK transform of the injected high-frequency current */
    PidType    pidPLL;       /*!< Internal Variable: The PLL PID */

    _iq        Vd_hf;        /*!< Output: The high-frequency Vd */
    _iq        estAngle;     /*!< Output: The delta angle between the true angle and the one passed in from inputs */
} HfiType;

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

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

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

/*! \brief  Run the HFI process
 *
 * This function runs Hfi function to control motor
 *
 * \param[in] obj : pointer to Hfi instance
 * \return the MCUs authorized evaluation
 *         - -1 : Functions not available
 *         -  0 : Functions available
 */
extern int32_t Hfi_Run(HfiType *obj);

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

#endif /* _HFI_H_ */
