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

/*! \brief  Contains public interface to various functions related
 *          Phase Voltage and Current Reconstruction (PVCR) 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 PVCR object
 */
typedef struct _Pvcr_ParamsType_
{
    float Tdb; /*!< The percent taking account of dead-band time and rise/fall time */
} Pvcr_ParamsType;

/*! \brief Defines the PVCR object
 */
typedef struct _PvcrType_
{
    _iq sensedIa;   /*!< Input: The sampled current of phase A */
    _iq sensedIb;   /*!< Input: The sampled current of phase B */
    _iq sensedIc;   /*!< Input: The sampled current of phase C */
    _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 Vdc;        /*!< Input: Voltage of DC */

    _iq Tdb;        /*!< Parameter: The percent taking account of dead-band time and rise/fall time */

    _iq offsetV;    /*!< Internal variable: The offset voltage of 3 phases, note that all 3 phases's offset are the same */
    _iq prevTa;     /*!< Internal variable: The previous Ta */
    _iq prevTb;     /*!< Internal variable: The previous Tb */
    _iq prevTc;     /*!< Internal variable: The previous Tc */
    _iq prevPrevTa; /*!< Internal variable: The previous of previous Ta */
    _iq prevPrevTb; /*!< Internal variable: The previous of previous Tb */
    _iq prevPrevTc; /*!< Internal variable: The previous of previous Tc; */
    _iq prevIa;     /*!< Internal variable: The previous Ia */
    _iq prevIb;     /*!< Internal variable: The previous Ib */
    _iq prevIc;     /*!< Internal variable: The previous Ic */

    _iq Va;         /*!< Output: The estimated phase A voltage at the middle of last PWM period */
    _iq Vb;         /*!< Output: The estimated phase B voltage at the middle of last PWM period */
    _iq Vc;         /*!< Output: The estimated phase C voltage at the middle of last PWM period */
    _iq Ia;         /*!< Output: The estimated phase A current at the middle of last PWM period */
    _iq Ib;         /*!< Output: The estimated phase A current at the middle of last PWM period */
    _iq Ic;         /*!< Output: The estimated phase A current at the middle of last PWM period */
} PvcrType;

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

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

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

/*! \brief  Run the PVCR process
 *  \note   The input must use the FOC control result from this loop
 *
 * This function runs the phase Voltage and current reconstruction function
 * 
 * \param[in] obj : pointer to Pvcr instance
 * \return the MCUs authorized evaluation
 *         - -1 : Functions not available
 *         -  0 : Functions available
 */
extern int32_t Pvcr_Run(PvcrType *obj);

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

#endif /* _PVCR_H_ */
