#ifndef _MC_HALL_H__
#define _MC_HALL_H__
#include "bsp/bsp.h"

//51ms, 77.6
//52.2ms 
//25.2
#ifdef GD32_FOC_DEMO
#define HALL_1_GROUP GPIOA
#define HALL_1_PIN GPIO_PIN_6
#define HALL_2_GROUP GPIOA
#define HALL_2_PIN GPIO_PIN_7
#define HALL_3_GROUP GPIOB
#define HALL_3_PIN GPIO_PIN_0
#else
#define HALL_1_GROUP GPIOB
#define HALL_1_PIN GPIO_PIN_8
#define HALL_2_GROUP GPIOB
#define HALL_2_PIN GPIO_PIN_7
#define HALL_3_GROUP GPIOB
#define HALL_3_PIN GPIO_PIN_6
#endif

#define DEGREES_120 0u
#define DEGREES_60 1u

#define HALL_PLACE DEGREES_120

static int __inline__ hall_bit_get(uint32_t gpio_periph,uint32_t pin)
{
    if((uint32_t)RESET != (GPIO_ISTAT(gpio_periph)&(pin))){
        return 1; 
    }else{
        return 0;
    }
}


#define READ_HALL1() (hall_bit_get(HALL_1_GROUP, HALL_1_PIN))
#define READ_HALL2() (hall_bit_get(HALL_2_GROUP, HALL_2_PIN))
#define READ_HALL3() (hall_bit_get(HALL_3_GROUP, HALL_3_PIN))

#define HALL1 READ_HALL1()
#define HALL2 READ_HALL2()
#define HALL3 READ_HALL3()

typedef struct _hall_io {
	u8 hall[3];
	u32 hall_time;
	u8 is_edged;
}hall_io_t;

void mc_hall_init(void);
int get_hall_stat(int samples);
u32 hall_get_hwcount(u8 *hall);

#endif /*_MC_HALL_H__ */

