#ifndef __AS5047D_H_
#define __AS5047D_H_
#include "gd32f4xx.h"

///水平AS5047D端口
///CSn
///将AS5047D_HORI的CSn引脚置为高电平
#define AS5047D_HORI_CSN_DISABLE (gpio_bit_set(GPIOA,GPIO_PIN_15)) 
///将AS5047D_HORI的CSn引脚置为低电平
#define AS5047D_HORI_CSN_ENABLE  (gpio_bit_reset(GPIOA,GPIO_PIN_15))

///CLK
///将AS5047D_HORI的CLK引脚置为高电平
#define AS5047D_HORI_CLK_HIGH    (gpio_bit_set(GPIOC,GPIO_PIN_10))
///将AS5047D_HORI的CLK引脚置为低电平
#define AS5047D_HORI_CLK_LOW     (gpio_bit_reset(GPIOC,GPIO_PIN_10))

///MISO
///AS5047D_HORI的MISO引脚相连的引脚的电平值
#define AS5047D_HORI_MISO_READ   (gpio_input_bit_get(GPIOC,GPIO_PIN_11))

///MOSI
///将AS5047D_HORI的MOSI引脚置为高电平
#define AS5047D_HORI_MOSI_HIGH   (gpio_bit_set(GPIOC,GPIO_PIN_12))
///将AS5047D_HORI的MOSI引脚置为低电平
#define AS5047D_HORI_MOSI_LOW    (gpio_bit_reset(GPIOC,GPIO_PIN_12))





///垂直AS5047D端口
///垂直AS5047D_VERT芯片引脚控制
///CSn
///将AS5047D_VERT的CSn引脚置为高电平
#define AS5047D_VERT_CSN_DISABLE (gpio_bit_set(GPIOD,GPIO_PIN_0))
///将AS5047D_VERT的CSn引脚置为低电平
#define AS5047D_VERT_CSN_ENABLE  (gpio_bit_reset(GPIOD,GPIO_PIN_0))

///CLK
///将AS5047D_VERT的CLK引脚置为高电平
#define AS5047D_VERT_CLK_HIGH    (gpio_bit_set(GPIOD,GPIO_PIN_1))
///将AS5047D_VERT的CLK引脚置为低电平
#define AS5047D_VERT_CLK_LOW     (gpio_bit_reset(GPIOD,GPIO_PIN_1))

///MISO
///读取与AS5047D_VERT的MISO引脚相连的引脚的电平值
#define AS5047D_VERT_MISO_READ   (gpio_input_bit_get(GPIOD,GPIO_PIN_2))

///MOSI
///将AS5047D_VERT的MOSI引脚置为高电平
#define AS5047D_VERT_MOSI_HIGH   (gpio_bit_set(GPIOD,GPIO_PIN_3))
///将AS5047D_VERT的MOSI引脚置为低电平
#define AS5047D_VERT_MOSI_LOW    (gpio_bit_reset(GPIOD,GPIO_PIN_3))





///AS5047D寄存器地址
///NOP
#define AS5047D_NOP    0x0000 
///Clear Error Flag
#define AS5047D_CEF    0X0001 
///Programming Control
#define AS5047D_PC     0X0003 
///OTP Register Zero Position Hi
#define AS5047D_ORZPH  0X0016 
///OTP Register Zero Position Low 6 LSBs
#define AS5047D_ORZPL  0X0017 
///Diagnostics + Automatic Gain Control (AGC)
#define AS5047D_D_AGC  0X3FFD 
///Magnitude
#define AS5047D_M      0X3FFE 
///Angle
#define AS5047D_ANGLE  0X3FFF 


///清除错误标记CLEAR ERROR FLAG 0100 0000 0000 0001
#define AS5047D_CLEAR_ERROR_FLAG   0X4001 



///AS5047D寄存器读写类型
///读寄存器
#define AS5047D_READ_COMMAND       0X4000 
///写寄存器
#define AS5047D_WRITE_COMMAND      0XBFFF 
///写入数据
#define AS5047D_WRITE_DATA         0X0001 
///判断接收数据
#define AS5047D_JUDGE_RECEIVE_DATA 0X4000  // 0100 0000 0000 0000
///寄存器连续读取的次数
#define AS5047D_READ_NUM           3 
///寄存器连续写入的次数
#define AS5047D_WRITE_NUM          3


#define  AS5047D_DIR_FWD           1 //磁编码器正向转动
#define  AS5047D_DIR_REV           0 //磁编码器反向转动

///用于保存对接收数据的判断结果
///
///用于保存对接收数据的判断结果，并且详细保存出错的具体原因
///_master表示as5047d对接收到的指令判断的结果，_slave表示K60对as5047d返回的数据包判断结果
typedef struct _As5047DJudgeReceivePackage_
{
  ///接收到的数据包
  unsigned int package;
  
  ///k60对AS5047D回复的数据进行判断
  ///判断接收的数据包效验码是否正确,1错误，0正常
  unsigned char parity_error_package;

 
  
  ///AS5047D对接收到的K60指令进行判断,指令有错时的回复结果
  ///数据Error flag（EF）位，
  unsigned char error_flag_command;
  ///校验错误，1错误，0正常
  unsigned char parity_error_command;
  ///命令无效，1错误，0正常
  unsigned char command_invalid_command;
  ///帧错误，1错误，0正常
  unsigned char framing_error_command;
  
  
  ///数据操作是否成功，0不成功，1成功.即判断写入或者读取数据是否成功
  unsigned char Operation_Result;

}As5047DJudgeReceivePackage;


///函数声明

void as5047d_init();
As5047DJudgeReceivePackage as5047d_hori_judge_receive_package(unsigned int package);
As5047DJudgeReceivePackage as5047d_vert_judge_receive_package(unsigned int package);


unsigned int as5047d_command_type(unsigned int package, unsigned int type);

unsigned int as5047d_hori_receive_package();
unsigned int as5047d_hori_read_command(unsigned int add);
unsigned int as5047d_hori_send_package(unsigned int package);
unsigned int as5047d_hori_write_command(unsigned int add);
unsigned int as5047d_hori_nop_command();
As5047DJudgeReceivePackage as5047d_hori_write_data(unsigned int add,unsigned int data);
unsigned int as5047d_hori_read_data(unsigned int add);
float as5047d_hori_get_angle();
unsigned int as5047d_hori_cef_command();



unsigned int as5047d_vert_receive_package();
unsigned int as5047d_vert_read_command(unsigned int add);
unsigned int as5047d_vert_send_package(unsigned int package);
unsigned int as5047d_vert_write_command(unsigned int add);
unsigned int as5047d_vert_nop_command();
As5047DJudgeReceivePackage as5047d_vert_write_data(unsigned int add,unsigned int data);
unsigned int as5047d_vert_read_data(unsigned int add);
float as5047d_vert_get_angle();
unsigned int as5047d_vert_cef_command();
float as5047d_hori_get_angle_a();
float as5047d_vert_get_angle_a();
#endif