#ifndef __ZEROCROSS_DETECTION_H__
#define __ZEROCROSS_DETECTION_H__

#include "include.h"

/*----------------------Macro definition----------------*/
#define PHASE_AB										5
#define PHASE_AC										1
#define PHASE_BC										3
#define PHASE_BA										2
#define PHASE_CA										6
#define PHASE_CB										4

/*-------------------Observers variable-----------------*/


typedef struct
{
    uint16_t closesendware_count;
    uint16_t zco_delaytime_count;
    uint16_t  zco_stream_time;
    uint16_t  closeSendWare_AvgCount;
    uint16_t  pastSendWare_count;
    uint8_t  delay_time;
//    uint8_t  zco_phase;
//    uint16_t szco_compensation_value;
} mc_zero_observers_t;

//typedef struct
//{
//    uint16_t adc_convertedvaluetab_a[3];
//    uint16_t adc_convertedvaluetab_b[3];
//    uint16_t adc_convertedvaluetab_c[3];
//    uint16_t adc_convertedvaluetab_vbus[3];
//    uint16_t adc_centerreference_value[3];
//} mc_adc_value_t;

typedef struct
{
    uint32_t adc_convertedvaluetab_a[3];
    uint32_t adc_convertedvaluetab_b[3];
    uint32_t adc_convertedvaluetab_c[3];
    uint32_t adc_convertedvaluetab_vbus[3];
    uint32_t adc_centerreference_value[3];
} mc_adc_value_t;

typedef enum{
	ADC_STATE_NULL = 0,
	ADC_STATE_STREAM,
	ADC_STATE_CHECK,
	ADC_STATE_CLOSE,
	ADC_STATE_SWITCHPHASE
} ADCEXECUTESTATE_T;

//AT(.motor.variable) ADCEXECUTESTATE_T gadcexecute_state;
//AT(.motor.variable)
extern mc_zero_observers_t mc_zero_observers;
//AT(.motor.variable)
//extern ADCEXECUTESTATE_T gadcexecute_state;
//AT(.motor.variable)
extern mc_adc_value_t gmc_sczco_advalue_params;

void waitting_stream(uint16_t sczeo_stream_time,uint8_t* mc_observers_state);
void bemf_to_zero_judge(uint8_t phase_mumber,uint8_t direction,uint8_t* mc_observers_state);
//uint8_t zero_crossing_detection(uint8_t phase_number,uint8_t direction,
//                                uint16_t sczeo_stream_time,uint8_t* mc_observers_state);
void comparator_zero_detection(uint8_t phase_mumber,uint8_t direction,uint8_t* mc_observers_state);
uint8_t comparator_delay_zero_time(uint8_t phase_number,uint8_t direction,uint8_t* mc_observers_state);
uint8_t delay_zero_time(uint8_t phase_number,uint8_t direction,uint8_t* mc_observers_state);
void szco_dataget(uint32_t vbus,uint32_t ua,uint32_t ub,
                  uint32_t uc,uint8_t phase,uint16_t compensation_value,
                  uint16_t duty_pwm,uint16_t timer_peroid);
#endif // __ZEROCROSS_DETECTION_H__
