/*
 * 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.
 */

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

#include "clff.h"

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

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

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

/*******************************************************************************
 * the functions
 ******************************************************************************/

void Clff_Init(ClffType *obj, const Clff_ParamsType *pParams)
{
    /* Based value */
    obj->RsBase_ohm = pParams->VBase_V / pParams->IBase_A;
    obj->LBase_mH   = pParams->VBase_V / pParams->IBase_A / (2 * MATH_PI * pParams->fBase_Hz) * 1000.0;
    obj->PsiBase_Wb = pParams->VBase_V / (2 * MATH_PI * pParams->fBase_Hz);

    /*init value */
    _iq Vdc_pu = _IQ(pParams->ratedVdc_V / pParams->VBase_V);
    obj->Kd    = _IQdiv(_IQ(pParams->Ld_mH / obj->LBase_mH), Vdc_pu);
    obj->Kq    = _IQdiv(_IQ(pParams->Lq_mH / obj->LBase_mH), Vdc_pu);
    obj->Kf    = _IQdiv(_IQ(pParams->flux_Wb / obj->PsiBase_Wb), Vdc_pu);
    obj->Kr    = _IQdiv(_IQ(pParams->Rs_Ohm / obj->RsBase_ohm), Vdc_pu);

    obj->minVdc_pu = _IQ(pParams->minVdc_V / pParams->VBase_V);
    obj->maxVdc_pu = _IQ(pParams->maxVdc_V / pParams->VBase_V);

    obj->Vd = 0;
    obj->Vq = 0;
}
