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

/*! \brief Contains public interface to various functions related
 *         to the fag modules
 */

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

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

#ifdef __cplusplus
extern "C" {
#endif

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

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

/*! \brief Fag prototype definition
 */
typedef struct _FagType_
{
    _iq frequency_pu; /*!< Input: The electric frequency */

    _iq factor; /*!< Internal Variable: The factor used to calculate the angle */

    _iq angle_pu; /*!< Output: The angle generated */
} FagType;

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

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

/*! \brief Initialize the FAG object
 *
 * This function initalizes Fag by setting configuration parameters
 *
 * \param[in] obj : pointer to Fag instance
 * \param[in] fBase_Hz : the default maximum current base frequency
 * \param[in] runFreq_kHz : the frequency of running current loop
 */
extern void Fag_Init(FagType *obj, float fBase_Hz, float runFreq_kHz);

/*! \brief Gets FAG calculation value
 *
 * This function  gets Fag function calculation value
 *
 * \param[in] obj : pointer to Fag instance
 */
static inline _iq Fag_GetAngle(FagType *obj)
{
    return (obj->angle_pu);
}

/*! \brief Sets the frequency of false angle
 *
 * This function  Sets the frequency of false angle
 *
 * \param[in] obj : pointer to Fag instance
 * \param[in] frequency : the desired frequency of false angle
 */
static inline void Fag_SetFrequency(FagType *obj, _iq frequency)
{
    obj->frequency_pu = frequency;
}

/*! \brief Runs the FAG
 *
 * This function runs Fag function, calculates dynamic changing false angle value
 *
 * \param[in] obj : pointer to Fag instance
 */
static inline void Fag_Run(FagType *obj)
{
    obj->angle_pu += _IQmpy(obj->frequency_pu, obj->factor);

    obj->angle_pu = _IQfrac(obj->angle_pu); /* Get the fraction part */
    if(obj->angle_pu < 0)
    {
        obj->angle_pu += _IQ(1.0);
    }
}

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

#endif /* _FAG_H_ */
