//#############################################################################
// $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/user_pfc.h
//! \brief  Contains the user related definitions for PFC
//!         This file is shared with all of devices
//!

#ifndef USER_PFC_H
#define USER_PFC_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 USER_PFC USER_PFC
//! @{
//
//*****************************************************************************

// the includes

// modules
#include "userParams.h"

#include "user_dms.h"

#if defined(DMCPFC_REV3P2) || defined(DMCPFC_REV3P1)
//! \brief Defines the maximum voltage at the AD converter
//!
#define USER_PFC_ADC_FULL_SCALE_DC_VOLTAGE_V    (485.9432f)

//! \brief Defines the maximum voltage at the AD converter
//!
#define USER_PFC_ADC_FULL_SCALE_AC_VOLTAGE_V    (820.9119403f)


//! \brief Defines the maximum current at the AD converter
//!
#define USER_PFC_ADC_FULL_SCALE_CURRENT_A       (67.45490982f)

//! \brief ADC current offsets for switch
#define USER_PFC_IAC_OFFSET                     (0.0405768454f)

//! \brief ADC voltage offsets for AC line
#define USER_PFC_VAC_OFFSET                     (0.49724853f)

//! \brief DC bus over current threshold
#define USER_PFC_OVER_CURRENT_A                 (40.5f)     // The maximum setting value is 45.0A

//! \brief DC bus over load threshold
#define USER_PFC_OVER_LOAD_W                    (2200.0f)

//! \brief AC bus over voltage threshold
#define USER_OVER_VOLTAGE_FAULT_ACV             (270.0f)

//! \brief AC bus over voltage threshold
#define USER_OVER_VOLTAGE_NORM_ACV              (250.0f)

//! \brief AC bus under voltage threshold
#define USER_UNDER_VOLTAGE_FAULT_ACV            (12.0f)

//! \brief AC bus under voltage threshold
#define USER_UNDER_VOLTAGE_NORM_ACV             (10.0f)

//! \brief DC bus over voltage threshold
#define USER_OVER_VOLTAGE_FAULT_DCV             (410.0f)

//! \brief DC bus over voltage threshold
#define USER_OVER_VOLTAGE_NORM_DCV              (385.0f)

//! \brief DC bus under voltage threshold
#define USER_UNDER_VOLTAGE_FAULT_DCV            (15.0f)

//! \brief DC bus under voltage threshold
#define USER_UNDER_VOLTAGE_NORM_DCV             (20.0f)

//! \brief DC bus shut down voltage threshold
#define USER_SHUTDOWN_VOLTAGE_DCV               (400.0f)

//! \brief means measure voltage threshold
#define USER_PFC_MEASURE_THRESHOLD_V            (40.0f)

//! \brief compensation voltage threshold
#define USER_PFC_COMPENSATION_VACRMS_V          (100.0f)

#if defined(_F28002x)       // 100MHz CPU Clock
//! \brief Defines the Pulse Width Modulation (PWM) frequency, kHz
#define USER_PFC_PWM_FREQ_kHz                   (72.04610951f)  // 72kHz

//! \brief Defines the Pulse Width Modulation (PWM) period cycles
//#define USER_PFC_PWM_TBPRD_NUM                    (uint16_t)(USER_SYSTEM_FREQ_MHz * 1000 / USER_PFC_PWM_FREQ_kHz / 2)
#define USER_PFC_PWM_TBPRD_NUM                  (694)           //~72kHz
#elif defined(_F28003x)       // 120MHz CPU Clock
//! \brief Defines the Pulse Width Modulation (PWM) frequency, kHz
#define USER_PFC_PWM_FREQ_kHz                   (72.02881152f)  // 72kHz

//! \brief Defines the Pulse Width Modulation (PWM) period cycles
//#define USER_PFC_PWM_TBPRD_NUM                    (uint16_t)(USER_SYSTEM_FREQ_MHz * 1000 / USER_PFC_PWM_FREQ_kHz / 2)
#define USER_PFC_PWM_TBPRD_NUM                  (833)           //~72kHz
#elif defined(_F280013x)      // 120MHz CPU Clock
//! \brief Defines the Pulse Width Modulation (PWM) frequency, kHz
#define USER_PFC_PWM_FREQ_kHz                   (72.02881152f)  // 72kHz

//! \brief Defines the Pulse Width Modulation (PWM) period cycles
//#define USER_PFC_PWM_TBPRD_NUM                (uint16_t)(USER_SYSTEM_FREQ_MHz * 1000 / USER_PFC_PWM_FREQ_kHz / 2)
#define USER_PFC_PWM_TBPRD_NUM                  (833)           //~72kHz
#elif defined(_F280015x)      // 120MHz CPU Clock
//! \brief Defines the Pulse Width Modulation (PWM) frequency, kHz
#define USER_PFC_PWM_FREQ_kHz                   (72.02881152f)  // 72kHz

//! \brief Defines the Pulse Width Modulation (PWM) period cycles
//#define USER_PFC_PWM_TBPRD_NUM                (uint16_t)(USER_SYSTEM_FREQ_MHz * 1000 / USER_PFC_PWM_FREQ_kHz / 2)
#define USER_PFC_PWM_TBPRD_NUM                  (833)           //~72kHz
#else
#error Not select the right device for this reference design
#endif  //

//! \brief Defines the number of pwm clock ticks per isr clock tick
//!        Note: Valid values are 1, 2 or 3 only
//#define USER_PFC_NUM_PWM_TICKS_PER_ISR_TICK    (3)         //~90kHz/30kHz
#define USER_PFC_NUM_PWM_TICKS_PER_ISR_TICK      (2)         //~72kHz/36kHz
//#define USER_PFC_NUM_PWM_TICKS_PER_ISR_TICK    (1)         //~36kHz/36kHz

// end of DMCPFC_REV3P2 |DMCPFC_REV3P1

//*****************************************************************************
#elif defined(HVMTRPFC_REV1P1)
#if defined(PFC_DISABLE)
//! \brief Defines the maximum voltage at the AD converter
#define USER_PFC_ADC_FULL_SCALE_DC_VOLTAGE_V    (409.90f)
#else
//! \brief Defines the maximum voltage at the AD converter
//!
#define USER_PFC_ADC_FULL_SCALE_DC_VOLTAGE_V    (510.992f)
#endif  // !PFC_DISABLE

//! \brief Defines the maximum voltage at the AD converter
//!
#define USER_PFC_ADC_FULL_SCALE_AC_VOLTAGE_V    (402.069f)

//! \brief Defines the maximum current at the AD converter
//!
#define USER_PFC_ADC_FULL_SCALE_CURRENT_A       (19.8f)

//! \brief ADC current offsets for switch
#define USER_PFC_IAC_OFFSET                     (0.000894891215f)

//! \brief ADC voltage offsets for AC line
#define USER_PFC_VAC_OFFSET                     (0.000398084143f)

//! \brief ADC voltage offsets for ACN line
#define USER_PFC_VAN_OFFSET                     (0.000403197337f)

//! \brief DC bus over current threshold
#define USER_PFC_OVER_CURRENT_A                 (5.2f)      //RMS*1.414

//! \brief DC bus over load threshold
#define USER_PFC_OVER_LOAD_W                    (850.0f)

//! \brief AC bus over voltage threshold
#define USER_OVER_VOLTAGE_FAULT_ACV             (265.0f)

//! \brief AC bus over voltage threshold
#define USER_OVER_VOLTAGE_NORM_ACV              (235.0f)

//! \brief AC bus under voltage threshold
#define USER_UNDER_VOLTAGE_FAULT_ACV            (15.0f)

//! \brief AC bus under voltage threshold
#define USER_UNDER_VOLTAGE_NORM_ACV             (20.0f)

//! \brief DC bus over voltage threshold
#define USER_OVER_VOLTAGE_FAULT_DCV             (400.0f)

//! \brief DC bus over voltage threshold
#define USER_OVER_VOLTAGE_NORM_DCV              (380.0f)

//! \brief DC bus under voltage threshold
#define USER_UNDER_VOLTAGE_FAULT_DCV            (20.0f)

//! \brief DC bus under voltage threshold
#define USER_UNDER_VOLTAGE_NORM_DCV             (25.0f)

//! \brief DC bus shut down voltage threshold
#define USER_SHUTDOWN_VOLTAGE_DCV               (400.0f)

//! \brief means measure voltage threshold
#define USER_PFC_MEASURE_THRESHOLD_V            (12.0f)

//! \brief compensation voltage threshold
#define USER_PFC_COMPENSATION_VACRMS_V          (100.0f)

#if defined(_F28002x)       // 100MHz CPU Clock
//! \brief Defines the Pulse Width Modulation (PWM) frequency, kHz
#define USER_PFC_PWM_FREQ_kHz                       (72.04610951f)  // 72kHz

//! \brief Defines the Pulse Width Modulation (PWM) period cycles
//#define USER_PFC_PWM_TBPRD_NUM                    (uint16_t)(USER_SYSTEM_FREQ_MHz * 1000 / USER_PFC_PWM_FREQ_kHz / 2)
#define USER_PFC_PWM_TBPRD_NUM                      (694)           //~72kHz
#elif defined(_F28003x)       // 120MHz CPU Clock
//! \brief Defines the Pulse Width Modulation (PWM) frequency, kHz
#define USER_PFC_PWM_FREQ_kHz                       (72.02881152f)  // 72kHz

//! \brief Defines the Pulse Width Modulation (PWM) period cycles
//#define USER_PFC_PWM_TBPRD_NUM                    (uint16_t)(USER_SYSTEM_FREQ_MHz * 1000 / USER_PFC_PWM_FREQ_kHz / 2)
#define USER_PFC_PWM_TBPRD_NUM                      (833)           //~72kHz
#elif defined(_F280013x)       // 120MHz CPU Clock
//! \brief Defines the Pulse Width Modulation (PWM) frequency, kHz
#define USER_PFC_PWM_FREQ_kHz                       (72.02881152f)  // 72kHz

//! \brief Defines the Pulse Width Modulation (PWM) period cycles
//#define USER_PFC_PWM_TBPRD_NUM                    (uint16_t)(USER_SYSTEM_FREQ_MHz * 1000 / USER_PFC_PWM_FREQ_kHz / 2)
#define USER_PFC_PWM_TBPRD_NUM                      (833)           //~72kHz
#elif defined(_F280015x)       // 120MHz CPU Clock
//! \brief Defines the Pulse Width Modulation (PWM) frequency, kHz
#define USER_PFC_PWM_FREQ_kHz                       (72.02881152f)  // 72kHz

//! \brief Defines the Pulse Width Modulation (PWM) period cycles
//#define USER_PFC_PWM_TBPRD_NUM                    (uint16_t)(USER_SYSTEM_FREQ_MHz * 1000 / USER_PFC_PWM_FREQ_kHz / 2)
#define USER_PFC_PWM_TBPRD_NUM                      (833)           //~72kHz
#else
#error Not select the right device for this reference design
#endif  //

//! \brief Defines the number of pwm clock ticks per isr clock tick
//!        Note: Valid values are 1, 2 or 3 only
#define USER_PFC_NUM_PWM_TICKS_PER_ISR_TICK     (2)
// end of HVMTRPFC_REV1P1
//******************************************************************************
#elif defined(BSXL8323RH_REVB)

//! \brief Defines the maximum voltage at the AD converter
#define USER_PFC_ADC_FULL_SCALE_DC_VOLTAGE_V    (57.528f)

//! \brief Defines the maximum voltage at the AD converter
#define USER_PFC_ADC_FULL_SCALE_AC_VOLTAGE_V    (57.528f)


//! \brief Defines the maximum current at the AD converter
#define USER_PFC_ADC_FULL_SCALE_CURRENT_A       (42.856f)

//! \brief ADC current offsets for switch
#define USER_PFC_IAC_OFFSET                     (0.50f)

//! \brief ADC voltage offsets for AC line
//#define USER_PFC_VAC_OFFSET                   (0.303030303f)
#define USER_PFC_VAC_OFFSET                     (0.454545455f)

//! \brief ADC voltage offsets for AC line
#define USER_PFC_VAN_OFFSET                     (0.990514159f)

//! \brief DC bus over current threshold
#define USER_PFC_OVER_CURRENT_A                 (12.8f)

//! \brief DC bus over load threshold
#define USER_PFC_OVER_LOAD_W                    (50.0f)

//! \brief AC bus over voltage threshold, fault value
#define USER_OVER_VOLTAGE_FAULT_ACV             (36.0f)

//! \brief AC bus over voltage threshold, normal value
#define USER_OVER_VOLTAGE_NORM_ACV              (32.0f)

//! \brief AC bus under voltage threshold, fault value
#define USER_UNDER_VOLTAGE_FAULT_ACV            (10.0f)

//! \brief AC bus under voltage threshold, normal value
#define USER_UNDER_VOLTAGE_NORM_ACV             (12.0f)

//! \brief DC bus over voltage threshold, fault value
#define USER_OVER_VOLTAGE_FAULT_DCV             (36.0f)

//! \brief DC bus over voltage threshold, normal value
#define USER_OVER_VOLTAGE_NORM_DCV              (32.0f)

//! \brief DC bus under voltage threshold, fault value
#define USER_UNDER_VOLTAGE_FAULT_DCV            (11.0f)

//! \brief DC bus under voltage threshold, normal value
#define USER_UNDER_VOLTAGE_NORM_DCV             (12.5f)

//! \brief DC bus shut down voltage threshold
#define USER_SHUTDOWN_VOLTAGE_DCV               (50.0f)

//! \brief AC bus RMS calculation threshold
#define USER_PFC_MEASURE_THRESHOLD_V            (1.0f)

#if defined(_F28002x)       // 100MHz CPU Clock
//! \brief Defines the Pulse Width Modulation (PWM) frequency, kHz
#define USER_PFC_PWM_FREQ_kHz                       (72.04610951f)  // 72kHz

//! \brief Defines the Pulse Width Modulation (PWM) period cycles
//#define USER_PFC_PWM_TBPRD_NUM                    (uint16_t)(USER_SYSTEM_FREQ_MHz * 1000 / USER_PFC_PWM_FREQ_kHz / 2)
#define USER_PFC_PWM_TBPRD_NUM                      (694)           //~72kHz
#elif defined(_F28003x)       // 120MHz CPU Clock
//! \brief Defines the Pulse Width Modulation (PWM) frequency, kHz
#define USER_PFC_PWM_FREQ_kHz                       (72.02881152f)  // 72kHz

//! \brief Defines the Pulse Width Modulation (PWM) period cycles
//#define USER_PFC_PWM_TBPRD_NUM                    (uint16_t)(USER_SYSTEM_FREQ_MHz * 1000 / USER_PFC_PWM_FREQ_kHz / 2)
#define USER_PFC_PWM_TBPRD_NUM                      (833)           //~72kHz
#elif defined(_F280013x)       // 120MHz CPU Clock
//! \brief Defines the Pulse Width Modulation (PWM) frequency, kHz
#define USER_PFC_PWM_FREQ_kHz                       (72.02881152f)  // 72kHz

//! \brief Defines the Pulse Width Modulation (PWM) period cycles
//#define USER_PFC_PWM_TBPRD_NUM                    (uint16_t)(USER_SYSTEM_FREQ_MHz * 1000 / USER_PFC_PWM_FREQ_kHz / 2)
#define USER_PFC_PWM_TBPRD_NUM                      (833)           //~72kHz
#elif defined(_F280015x)       // 120MHz CPU Clock
//! \brief Defines the Pulse Width Modulation (PWM) frequency, kHz
#define USER_PFC_PWM_FREQ_kHz                       (72.02881152f)  // 72kHz

//! \brief Defines the Pulse Width Modulation (PWM) period cycles
//#define USER_PFC_PWM_TBPRD_NUM                    (uint16_t)(USER_SYSTEM_FREQ_MHz * 1000 / USER_PFC_PWM_FREQ_kHz / 2)
#define USER_PFC_PWM_TBPRD_NUM                      (833)           //~72kHz
#else   // !_F28002x && !_F28003x  && !_F280013x
#error Not select the right device for this reference design
#endif  // !_F28002x && !_F28003x  && !_F280013x

//! \brief Defines the number of pwm clock ticks per isr clock tick
//!        Note: Valid values are 1, 2 or 3 only
#define USER_PFC_NUM_PWM_TICKS_PER_ISR_TICK     (2U)
// BSXL8323RH_REVB
//*****************************************************************************
#else
#error Not set the hardware board configuration for PFC
#endif


// ----------- PFC Control Parameters ------------------------------------------
//! \brief Defines the voltage for enabling voltage loop
#define USER_PFC_ENABLE_VOLTAGE_LOOP_V          (15.0f)

//! \brief Defines the voltage for disabling voltage loop
#define USER_PFC_DISABLE_VOLTAGE_LOOP_V          (10.0f)

//! \brief Defines the voltage for enabling current loop
#define USER_PFC_ENABLE_CURRENT_LOOP_V          (20.0f)

//! \brief Defines the voltage for disabling current loop
#define USER_PFC_DISABLE_CURRENT_LOOP_V         (15.0f)

//! \brief Defines the dc link voltage scale factor for the system
#define USER_PFC_VOLTAGE_DC_SF          (USER_PFC_ADC_FULL_SCALE_DC_VOLTAGE_V / 4096.0f)

//! \brief Defines the ac input voltage scale factor for the system
#define USER_PFC_VOLTAGE_AC_SF          (USER_PFC_ADC_FULL_SCALE_AC_VOLTAGE_V / 4096.0f)

//! \brief Defines the AC input current scale factor for the system
#define USER_PFC_CURRENT_SF             (USER_PFC_ADC_FULL_SCALE_CURRENT_A / 4096.0f)

//! \brief Defines the current invert factor for the system
#define USER_PFC_CURRENT_INV_SF         (4096.0f / USER_PFC_ADC_FULL_SCALE_CURRENT_A)

//! \brief Defines the dc link voltage invert factor for the system
#define USER_PFC_VOLTAGE_DCINV_SF       (1.0f / USER_PFC_ADC_FULL_SCALE_DC_VOLTAGE_V)

//! \brief Defines the number of ISR clock ticks per current controller clock tick
#define USER_PFC_NUM_ISR_TICKS_PER_CURRENT_TICK     (1)

//! \brief Defines the number of ISR clock ticks per speed controller clock tick
#define USER_PFC_NUM_ISR_TICKS_PER_VOLTAGE_TICK     (5)

//! \brief Defines the Pulse Width Modulation (PWM) period, usec
#define USER_PFC_PWM_PERIOD_usec        (1000.0f / USER_PFC_PWM_FREQ_kHz)

//! \brief Defines the Interrupt Service Routine (ISR) frequency, Hz
#define USER_PFC_ISR_FREQ_Hz            (USER_PFC_PWM_FREQ_kHz * 1000.0f / (float32_t)USER_PFC_NUM_PWM_TICKS_PER_ISR_TICK)

//! \brief Defines the SFRA sampling, Hz
#define PFC_SAMPLING_FREQ_HZ            USER_PFC_ISR_FREQ_Hz

//! \brief Defines the Interrupt Service Routine (ISR) period, usec
#define USER_PFC_ISR_PERIOD_usec        (USER_PFC_PWM_PERIOD_usec * (float32_t)USER_PFC_NUM_PWM_TICKS_PER_ISR_TICK)

//! \brief Defines the Interrupt Service Routine (ISR) period, usec
#define USER_PFC_ISR_PERIOD_sec         (1.0f / USER_PFC_ISR_FREQ_Hz)

//! \brief Defines the current controller execution period, usec
#define USER_PFC_CURRENT_PERIOD_sec     (USER_PFC_ISR_PERIOD_usec * (float32_t)USER_PFC_NUM_ISR_TICKS_PER_CURRENT_TICK)

//! \brief Defines the voltage controller execution frequency, Hz
#define USER_PFC_VOLTAGE_FREQ_Hz        (USER_PFC_ISR_FREQ_Hz / (float32_t)USER_PFC_NUM_ISR_TICKS_PER_VOLTAGE_TICK)

//! \brief Defines the power measure frequency, Hz
#define USER_PFC_MEASURE_FREQ_Hz        (USER_PFC_VOLTAGE_FREQ_Hz * 1.0)

//! \brief Defines the voltage controller execution period, usec
#define USER_PFC_VOLTAGE_PERIOD_sec     (USER_PFC_ISR_PERIOD_usec / 1000.0f / 1000.0f * (float32_t)USER_PFC_NUM_ISR_TICKS_PER_VOLTAGE_TICK)

//! \brief Defines the AC source frequency, Hz
#define USER_GRID_AC_FREQ_Hz            (60.0f)     // 60Hz

//! defines ADC oversampling buffer size, 2
#define IAC_OVERSAMPLE_TIMES            (2)

//! defines ADC oversampling buffer size, 2
#define VAC_OVERSAMPLE_TIMES            (2)

//! defines ADC oversampling buffer size, 2
#define VDC_OVERSAMPLE_TIMES            (2)

//! defines DMA buffer size for ADC, 2*2
#define DMA_PFC_ADC_BUF_SIZE            (8)     // 4 is ok

//! defines DMA buffer size for ADC, 2*2
#define DMA_PFC_ADC_FREQ_TIMES          (4)     // DMA_PFC_ADC_FREQ_TIMES = DMA_PFC_ADC_BUF_SIZE

//! defines DMA burst size for ADC, 1
#define DMA_PFC_ADC_BURST_SIZE          (1)

//! defines DMA transfer size for ADC, 2*2
#define DMA_PFC_ADC_TRANSFER_SIZE       (8)

//! defines ADC wrap size for ADC, 2*2
#define DMA_PFC_SRC_WRAP_SIZE           (1)

//! defines ADC wrap size for ADC, 3*2
#define DMA_PFC_DST_WRAP_SIZE           (8)

//! \brief Defines the ADC timer frequency, Hz
#define USER_PFC_ADC_TIMER_FREQ_HZ      (USER_PFC_ISR_FREQ_Hz * (float32_t)DMA_PFC_ADC_FREQ_TIMES)

//! \brief Defines the power measure threshold (V)
#define USER_PFC_VOLTAGE_TRAGET_V       (40.0f)

//! \brief Defines the interleaved mode switching on threshold (V)
#define USER_PFC_IL_MODE_OFF_VREF       (0.050f)

//! \brief Defines the interleaved mode switching off threshold (V)
#define USER_PFC_IL_MODE_ON_VREF        (0.090f)

//! \brief Defines the power measure threshold (V)
#define USER_PFC_VOLTAGE_TRAGET_V       (40.0f)

//! \brief Defines the voltage trajectory rate (PU)
#define USER_PFC_VOLTAGE_TRAJ_RATE_PU   (0.00001f / USER_PFC_ADC_FULL_SCALE_DC_VOLTAGE_V)

//! \brief Defines the PU factor
#define USER_ADC_PU_SCALE_FACTOR        (0.000244140625f)   // 12bit, =1.0/4096.0

//! \brief Defines the ADC invert scale factor for the system
#define USER_PFC_ADC_INV_SF             (4096.0f)

//! \brief Defines the ADC scale factor for the system
#define USER_PFC_ADC_PU_SF              (1.0f / 4096.0f)

//! \brief Defines the AC current scale factor with oversample for the system
#define USER_PFC_ADC_IAC_PU_SF          (1.0f / 4096.0f / (float32_t)IAC_OVERSAMPLE_TIMES)

//! \brief Defines the AC voltage scale factor with oversample for the system
#define USER_PFC_ADC_VAC_PU_SF          (1.0f / 4096.0f / (float32_t)VAC_OVERSAMPLE_TIMES)

#if defined(PFC_DISABLE) && defined(HVMTRPFC_REV1P1)
//! \brief Defines the dc link voltage scale factor with oversample for the system
#define USER_PFC_ADC_VDC_PU_SF          (1.0f / 4096.0f )
#else
//! \brief Defines the dc link voltage scale factor with oversample for the system
#define USER_PFC_ADC_VDC_PU_SF          (1.0f / 4096.0f / (float32_t)VDC_OVERSAMPLE_TIMES)
#endif

//! \brief Defines the power measure threshold (PU)
#define USER_PFC_MEASURE_THRESHOLD_PU   (USER_PFC_MEASURE_THRESHOLD_V / USER_PFC_ADC_FULL_SCALE_AC_VOLTAGE_V)

//! \brief DC bus over current threshold (A)
#define USER_PFC_PEAK_CURRENT_A         (USER_PFC_OVER_CURRENT_A * MATH_SQRT_TWO)

//! \brief DC bus over current threshold (PU)
#define USER_PFC_PEAK_CURRENT_PU        (USER_PFC_PEAK_CURRENT_A / USER_PFC_ADC_FULL_SCALE_CURRENT_A)

//! \brief Defines the maximum output voltage
#define USER_PFC_VOLTAGE_OUT_MAX_V      (400.0f)

//! \brief Defines the minimum output voltage
#define USER_PFC_VOLTAGE_OUT_MIN_V      (80.0f)

//! \brief Defines the maximum AC input voltage
#define USER_PFC_VOLTAGE_AC_IN_MAX_V    (265.0f)

//! \brief Defines the minimum AC input voltage
#define USER_PFC_VOLTAGE_AC_IN_MIN_V    (85.0f)

//! \brief Defines the number of AC bus over/under voltage setting time
//!  timer base = 5ms
#define USER_PFC_VOLTAGE_AC_FAULT_TIME_SET      (40)         // 5ms * 40 = 200ms

//! \brief Defines the number of DC bus over/under voltage setting time
//!  timer base = 5ms
#define USER_PFC_VOLTAGE_DC_FAULT_TIME_SET      (40)         // 5ms * 40 = 200ms

//! \brief Defines the number of over load setting time
//!  timer base = 5ms
#define USER_PFC_OVER_LOAD_FAULT_TIME_SET       (10)

//! \brief Defines the number of over load setting times
#define USER_PFC_OVER_CURRENT_FAULT_TIMES_SET   (10)

//! \brief Defines the power scale factor
#define USER_PFC_SCALE_POWER_W                  (USER_PFC_ADC_FULL_SCALE_AC_VOLTAGE_V * USER_PFC_ADC_FULL_SCALE_CURRENT_A)

//! \brief DC bus shut down voltage threshold
#define USER_SHUTDOWN_VOLTAGE_PU                (USER_SHUTDOWN_VOLTAGE_DCV / USER_PFC_ADC_FULL_SCALE_DC_VOLTAGE_V)

//! \brief DC bus PFC tunr off voltage threshold
#define USER_OVER_OFF_VOLTAGE_V                 (405.0f)


//! \brief AC Grid input minimum frequency, 40Hz*2, the signal is rectified
#define USER_UNIVERSAL_GRID_MIN_FREQ            (80.0f)

//! \brief AC Grid input maximum frequency, 70Hz*2, the signal is rectified
#define USER_UNIVERSAL_GRID_MAX_FREQ            (140.0f)

//! \brief  Vdc filter corner frequency (in Hz), 100/120Hz
#define USER_LPF_VDC_FREQ_Hz                    (60.0f / 0.5f)      // 120.0Hz

//! \brief  Vdc filter coefficient
#define USER_LPF_VDC_COEFF                      (USER_LPF_VDC_FREQ_Hz / USER_PFC_MEASURE_FREQ_Hz)

//! \brief  PFC CMPSS offset value
#define PFC_CMPSS_OFFSET_VALUE                  (uint16_t)(USER_PFC_IAC_OFFSET * USER_PFC_ADC_INV_SF)

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

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

#endif // end of USER_PFC_H definition

