//#############################################################################
// $Copyright:
// Copyright (C) 2017-2023 Texas Instruments Incorporated - http://www.ti.com/
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
//   Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
//
//   Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the
//   distribution.
//
//   Neither the name of Texas Instruments Incorporated nor the names of
//   its contributors may be used to endorse or promote products derived
//   from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//#############################################################################


//! \file   /solutions/tidm_02010_dmpfc/common/include/motor2_drive.h
//!
//! \brief  header file to be included in all labs
//!         support for dualmtrpfc board with F28002x/F28003x/F280013x
//!

#ifndef MOTOR2_DRIVE_H
#define MOTOR2_DRIVE_H


//*****************************************************************************
//
// If building with a C++ compiler, make all of the definitions in this header
// have a C binding.
//
//*****************************************************************************
#ifdef __cplusplus
extern "C"
{
#endif

//*****************************************************************************
//
//! \defgroup MOTOR2_DRIVE MOTOR2_DRIVE
//! @{
//
//*****************************************************************************

// includes
#ifdef __TMS320C28XX_CLA__
#include "libraries/math/include/CLAmath.h"
#else
#include "libraries/math/include/math.h"
#include <math.h>
#endif

#include "userParams.h"
#include "motor_common.h"

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


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


// *****************************************************************************
// the globals
extern float32_t angleFOCM2_rad;     //!< the rotor angle from FOC modules

#ifdef MOTOR2_ESMO
extern ESMO_Handle esmoM2Handle;
extern ESMO_Obj    esmoM2;

extern SPDFR_Handle spdfrM2Handle;
extern SPDFR_Obj spdfrM2;

extern float32_t angleCompM2_rad;
extern float32_t anglePLLM2_rad;
extern float32_t angleSMOM2_rad;
extern float32_t speedPLLM2_Hz;
extern float32_t frswPosM2_sf;
#endif  // MOTOR2_ESMO

#if defined(MOTOR2_FAST)
extern EST_Handle    estM2Handle;     //!< the handle for the estimator
extern EST_InputData_t estInputDataM2;
extern EST_OutputData_t estOutputDataM2;

extern float32_t angleDeltaM2_rad;   //!< the rotor angle compensation value
extern float32_t angleESTM2_rad;     //!< the rotor angle from EST modules
extern float32_t speedESTM2_Hz;     //!< the speed from EST modules
#endif  // MOTOR2_FAST

#if((DMC_BUILDLEVEL == DMC_LEVEL_3) || (DMC_BUILDLEVEL == DMC_LEVEL_3) || \
        defined(MOTOR2_ESMO))
//!< the handles for Angle Generate for open loop control
extern ANGLE_GEN_Handle angleGenM2Handle;

//!< the Angle Generate onject for open loop control
extern ANGLE_GEN_Obj    angleGenM2;
extern float32_t angleGenM2_rad;     //!< the rotor angle from Generator modules
extern float32_t speedRefM2_Hz;
#endif  // (DMC_BUILDLEVEL <= DMC_LEVEL_3) || defined(MOTOR2_ESMO)

#if defined(MOTOR2_DCLINKSS)
extern DCLINK_SS_Handle dclinkM2Handle;
extern DCLINK_SS_Obj    dclinkM2;
#endif  // MOTOR2_DCLINKSS
// Flying start is only for fan
extern float32_t flyingStartSpeed_Hz;
extern uint16_t flyingStartTimeDelay;


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

//! \brief The main interrupt service (ISR) routine
extern __interrupt void motor2CtrlISR(void);

//! \brief initialize motor control parameters
extern void initMotor2CtrlParameters(void);

//! \brief run motor control in main loop
extern void runMotor2Control(void);

//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************

//*****************************************************************************
//
// Mark the end of the C bindings section for C++ compilers.
//
//*****************************************************************************
#ifdef __cplusplus
}
#endif // extern "C"

#endif // end of MOTOR2_DRIVE_H definition
