/**
 *******************************************************************************
 * @file  hrpwm/hrpwm_capture_ext_event/source/main.h
 * @brief This file contains the including files of main routine.
 @verbatim
   Change Logs:
   Date             Author          Notes
   2024-01-15       CDT             First version
 @endverbatim
 *******************************************************************************
 * Copyright (C) 2022-2024, Xiaohua Semiconductor Co., Ltd. All rights reserved.
 *
 * This software component is licensed by XHSC under BSD 3-Clause license
 * (the "License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                    opensource.org/licenses/BSD-3-Clause
 *
 *******************************************************************************
 */
#ifndef __MAIN_H__
#define __MAIN_H__

#include "hc32_ll.h"
#include "hc32_ll_def.h"
#include "ev_hc32f334_lqfp64_bsp.h"
#include "math.h"


#define EXAMPLE_PERIPH_WE               (LL_PERIPH_GPIO | LL_PERIPH_EFM | LL_PERIPH_FCG | LL_PERIPH_PWC_CLK_RMU)
#define EXAMPLE_PERIPH_WP               (LL_PERIPH_EFM | LL_PERIPH_FCG)

#define HRPWM_FB_1                      (CM_HRPWM1)
#define HRPWM_FB_2                      (CM_HRPWM4)
#define HRPWM_FB1_FCG                   (FCG2_PERIPH_HRPWM_1)
#define HRPWM_FB2_FCG                   (FCG2_PERIPH_HRPWM_4)

#define UNFOLDING_SCR_L_PORT            (GPIO_PORT_C)
#define UNFOLDING_SCR_L_PIN             (GPIO_PIN_08)
#define UNFOLDING_SCR_R_PORT            (GPIO_PORT_C)
#define UNFOLDING_SCR_R_PIN             (GPIO_PIN_09)

#define UNFOLDING_MOS_L_PORT            (GPIO_PORT_A)
#define UNFOLDING_MOS_L_PIN             (GPIO_PIN_08)
#define UNFOLDING_MOS_R_PORT            (GPIO_PORT_A)
#define UNFOLDING_MOS_R_PIN             (GPIO_PIN_09)

#define HRPWM_EMB1_PORT                 (GPIO_PORT_B)
#define HRPWM_EMB1_PIN                  (GPIO_PIN_07)

#define HRPWM_EMB2_PORT                 (GPIO_PORT_A)
#define HRPWM_EMB2_PIN                  (GPIO_PIN_11)

#define HRPWM_EMB3_PORT                 (GPIO_PORT_B)
#define HRPWM_EMB3_PIN                  (GPIO_PIN_08)

#define HRPWM_EMB4_PORT                 (GPIO_PORT_B)
#define HRPWM_EMB4_PIN                  (GPIO_PIN_09)

#define HRPWM_EMB5_PORT                 (GPIO_PORT_A)
#define HRPWM_EMB5_PIN                  (GPIO_PIN_10)

#define HRPWM_EMB_FUNC                  (GPIO_FUNC_5)

#define CMP_INP1_PORT                   (GPIO_PORT_A)
#define CMP_INP1_PIN                    (GPIO_PIN_07)
#define CMP_INP2_PORT                   (GPIO_PORT_A)
#define CMP_INP2_PIN                    (GPIO_PIN_03)

#define ADC_ICT1_DEST_CH                 (ADC_CH0)
#define ADC_ICT1_PIN                     (ADC1_PIN_PC2)
#define GPIO_ICT1_PORT                   (GPIO_PORT_C)
#define GPIO_ICT1_PIN                    (GPIO_PIN_02)

#define ADC_IACL_DEST_CH                 (ADC_CH1)
#define ADC_IACL_PIN                     (ADC1_PIN_PC4)
#define GPIO_IACL_PORT                   (GPIO_PORT_C)
#define GPIO_IACL_PIN                    (GPIO_PIN_04)

#define ADC_VAC_DEST_CH                  (ADC_CH2)
#define ADC_VAC_PIN                      (ADC1_PIN_PA2)
#define GPIO_VAC_PORT                    (GPIO_PORT_A)
#define GPIO_VAC_PIN                     (GPIO_PIN_02)

#define ADC_IDC_DEST_CH                  (ADC_CH3)
#define ADC_IDC_PIN                      (ADC1_PIN_PA1)
#define GPIO_IDC_PORT                    (GPIO_PORT_A)
#define GPIO_IDC_PIN                     (GPIO_PIN_01)

#define ADC_ICT2_DEST_CH                 (ADC_CH4)
#define ADC_ICT2_PIN                     (ADC2_PIN_PC0)
#define GPIO_ICT2_PORT                   (GPIO_PORT_C)
#define GPIO_ICT2_PIN                    (GPIO_PIN_00)

#define ADC_IACR_DEST_CH                 (ADC_CH5)
#define ADC_IACR_PIN                     (ADC2_PIN_PB0)
#define GPIO_IACR_PORT                   (GPIO_PORT_B)
#define GPIO_IACR_PIN                    (GPIO_PIN_00)

#define ADC_VDC_DEST_CH                  (ADC_CH6)
#define ADC_VDC_PIN                      (ADC2_PIN_PC3)
#define GPIO_VDC_PORT                    (GPIO_PORT_C)
#define GPIO_VDC_PIN                     (GPIO_PIN_03)


#define DAC1_OUTPUT2_PORT                (GPIO_PORT_A)
#define DAC1_OUTPUT2_PIN                 (GPIO_PIN_05)

#define DAC2_OUTPUT1_PORT                (GPIO_PORT_A)
#define DAC2_OUTPUT1_PIN                 (GPIO_PIN_06)

#define LED_GREEN_PORT                   (GPIO_PORT_C)
#define LED_GREEN_PIN                    (GPIO_PIN_11)

#define LED_RED_PORT                     (GPIO_PORT_A)
#define LED_RED_PIN                      (GPIO_PIN_12)

#define CMP1_OUT_PORT                    (GPIO_PORT_B)
#define CMP1_OUT_PIN                     (GPIO_PIN_15)
#define CMP1_OUT_FUNC                    (GPIO_FUNC_2)

#define USART_DEVICE                     (CM_USART2)
#define USART_FCG                        (FCG3_PERIPH_USART2)

#define USART_BAUDRATE                   (115200UL)
#define USART_BAUDRATE_ERR_MAX           (0.025f)

#define USART_TX_PORT                    (GPIO_PORT_C)
#define USART_TX_PIN                     (GPIO_PIN_10)
#define USART_TX_PORT_FUNC               (GPIO_FUNC_36)

#define USART_RX_PORT                    (GPIO_PORT_A)
#define USART_RX_PIN                     (GPIO_PIN_15)
#define USART_RX_PORT_FUNC               (GPIO_FUNC_37)

#define PERIOD_MAX                       268800
#define CTRL_PERIOD_VAL                  6000

////
extern uint16_t deltavalue;
extern float_t  Vpv_N_Div;
extern float_t  Ton_pu_feedforward;
extern uint8_t  Vally_Cal_All_Finish_Flag;
extern float_t  Iac_Rms;
extern float_t  Reactive_Comp_Ratio;
extern uint16_t Value1,Value2,deltavalue;
extern float_t  IFB2_Gain_par;
extern float_t	IFB2_Offset_par;
extern float_t	IFB1_Gain_par;
extern float_t	IFB1_Offset_par;
extern float_t	Vpv_Gain_par;
extern float_t	Vpv_Offset_par;
	
extern float_t	IgL_Gain_par;
extern float_t	IgL_Offset_par;
extern float_t	IgR_Gain_par;
extern float_t	IgR_Offset_par;
extern float_t	Ipv_Gain_par;
extern float_t	Ipv_Offset_par;
extern float_t	Vgrid_Gain_par;
extern float_t	Vgrid_Offset_par;
extern float_t  ZCD_gain_par;

extern uint8_t CalibPara_Done_Flag;

extern uint8_t Update_CalPar_cmd;
////
extern void HRPWM_Config(void);
extern void TmrA_Config(void);
extern void Adc_Config(void);
extern void Cmp_Config(void);
extern void Dac_Config(void);
extern void GPIO_Config(void);
extern void Emb_Config(void);
extern void WDT_Config(void);
extern int32_t Usart_init(void *USART, uint32_t u32Baudrate);
extern void Read_CalPar(void);
extern void Write_CalPar(void);
extern inline void LED_Control(void);
extern inline void comm_uart(void);
#endif /* __MAIN_H__ */

/*******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/
