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

/*! \brief  Contains public interface to various functions related
 *          to the Interior Permanent Magnet Motor (IPM)
 */

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

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

#ifdef __cplusplus
extern "C" {
#endif

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

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

/*! \brief Defines the motor object
 */
typedef enum
{
    IPM_OUTPUTTYPE_FIXED,
    IPM_OUTPUTTYPE_DYNAMIC,
} Ipm_ParamsModeType;

/*! \brief Defines the initial parameters for IPM object
 */
typedef struct _Ipm_ParamsType_
{
    Ipm_ParamsModeType type;          /*!< How the use motor parameters */
    void              *pContainer;    /*!< Pointer to the table container */
    uint16_t           maxRows;       /*!< Max row number of motor parameter 2-D table */
    uint16_t           maxColumns;    /*!< Max column number of motor parameter 2-D table */
    _iq                defaultRs_Ohm; /*!< The default value of Rs */
    _iq                defaultLd_mH;  /*!< The default value of Ld */
    _iq                defaultLq_mH;  /*!< The default value of Lq */
    _iq                defaultPsi_Wb; /*!< The default value of Psi */
} Ipm_ParamsType;

/*! \brief Defines the motor object
 */
typedef struct _IpmType_
{
    _iq                temp_pu;            /*!< Input: Motor temperature */
    _iq                Id_pu;              /*!< Input: D-axis current */
    _iq                Iq_pu;              /*!< Input: Q-axis current */

    Ipm_ParamsModeType type;               /*!< Parameter: How to use motor parameters */
    uint16_t           maxRows;            /*!< Parameter: Max row number of motor parameter matrix */
    uint16_t           maxColumns;         /*!< Parameter: Max column number of motor parameter matrix */
    _iq                defaultRs_Ohm;      /*!< Parameter: The default value of Rs */
    _iq                defaultLd_mH;       /*!< Parameter: The default value of Ld */
    _iq                defaultLq_mH;       /*!< Parameter: The default value of Lq */
    _iq                defaultPsi_Wb;      /*!< Parameter: The default value of Psi */

    TableType          paramTable;         /*!< Internal variable: The motor parameter table */
    _iq                rowInterval;        /*!< Internal variable: The interval between rows of motor parameter matrix */
    _iq                columnInterval;     /*!< Internal variable: The interval between columns of motor parameter matrix */
    _iq                oneOverRowIntvl;    /*!< Internal variable: Reciprocal of interval between rows of motor parameter matrix */
    _iq                oneOverColumnIntvl; /*!< Internal variable: Reciprocal of interval between columns of motor parameter matrix */
    _iq                absId_pu;           /*!< Internal variable: ABS value of D-axis current */
    _iq                absIq_pu;           /*!< Internal variable: ABS value of Q-axis current */

    _iq                Rs_Ohm;             /*!< Output: The stator resistance in P.U. */
    _iq                Ld_mH;              /*!< Output: The direct stator inductance in P.U. */
    _iq                Lq_mH;              /*!< Output: The quadrature stator inductance in P.U. */
    _iq                Psi_Wb;             /*!< Output: The permanent magnet flux in P.U. */
} IpmType;

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

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

/*! \brief Initialize the IPM object
 *
 * This function initalizes Ipm by setting configuration parameters
 *
 * \param[in] obj : pointer to Ipm instance
 * \param[in] pParams : pointer to Ipm configuration parameters
 */
extern void Ipm_Init(IpmType *obj, const Ipm_ParamsType *pParams);

/*! \brief Calculates and Gets the Ipm parameters
 *
 * This function calculates and gets the Ipm parameters from table data
 *
 * \param[in] obj : pointer to Ipm instance
 */
extern void Ipm_Calc(IpmType *obj);

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

#endif /* _IPM_H_ */
