#ifndef __SPI_HELPER_H
#define __SPI_HELPER_H
/**
* 封装 uart 发送接收操作
* ??  是否用到buf? 
*/

#if 0 

#ifdef __cplusplus
	extern "C" {
#endif



#include "spi.h"
#include <stdlib.h>
/*  回调函数，将数据返回上层 */
typedef void (*it_callback)(void *obj,uint8_t *buf, uint32_t buf_size );

/* 由于可能存在多个外设，考虑将 事件类型封装在helper, 事件定义初始化不必放这里*/
typedef enum
{
   SPI_HELPER_Event_READY,                   /*!< Startup finished. */
   SPI_HELPER_Event_INIT,                  
   SPI_HELPER_Event_INIT_SUCCESS,                  
   SPI_HELPER_Event_TRANS_ONLY,                  
   SPI_HELPER_Event_TRANS_FOR_DATA,                 
   SPI_HELPER_Event_DATA_RCV,                 
} SPI_HELPER_Event_TypeDef;

typedef enum 
{
	SPI_HELPER_TRANS_POLLING,
	SPI_HELPER_TRANS_IT,
	SPI_HELPER_TRANS_DMA
}SPI_HELPER_TRANS_TypeDef;

typedef enum 
{
	SPI_HELPER_RCV_POLLING,
	SPI_HELPER_RCV_IT,
	SPI_HELPER_RCV_DMA
}SPI_HELPER_RCV_TypeDef;

typedef enum 
{
	SPI_HELPER_MODE_MASTER,
	SPI_HELPER_MODE_SLAVE
}SPI_HELPER_MODE_TypeDef;

typedef struct 
{
    SPI_HandleTypeDef *hspi;
	uint8_t i2c_write_address;
	uint8_t i2c_read_address;
	SPI_HELPER_TRANS_TypeDef trans_type;  /* 0 :polling, 1: IT  2: DMA*/
    SPI_HELPER_RCV_TypeDef rcv_type;  /* 0 :polling, 1: IT  2: DMA*/
	SPI_HELPER_MODE_TypeDef mode;   

    uint8_t *trans_buf;
    volatile uint32_t trans_size;
    volatile uint32_t trans_data_con;

    uint8_t *receive_buf;
    volatile uint32_t receive_size;
    volatile uint8_t receive_data_con;
 
    // volatile uint8_t receive_buf_con;
    // uint32_t receive_buf_half_len;
    // uint8_t half_flag;  /*0 : 未半满， 1：半满， 2： 半满已经处理*/

    // volatile Uart_Status_TypeDef status;  /* 发送状态 0: ready , 1：正在发送 busy，2：发送ok，*/

    void * obj;
    it_callback callback;


}SPIHelper_TypeDef;

SPIHelper_TypeDef * SPIHelper_Init( );
void SPIHelper_Set_hspi( SPIHelper_TypeDef *spihelper,  SPI_HandleTypeDef * hspi  );
void SPIHelper_Set_Mode( SPIHelper_TypeDef *spihelper,  SPI_HELPER_MODE_TypeDef mode  );
void SPIHelper_Set_Trans_Type( SPIHelper_TypeDef *spihelper,  SPI_HELPER_TRANS_TypeDef trans_type  );
void SPIHelper_Set_Rcv_Type( SPIHelper_TypeDef *spihelper,  SPI_HELPER_RCV_TypeDef rcv_type  );
 
// void SPIHelper_Set_timeout( SPIHelper_TypeDef *spihelper,  uint32_t timeout_ms  );

int SPIHelper_Set_callback_func_obj( SPIHelper_TypeDef *spihelper,   void * obj,  it_callback callback );

void SPIHelper_Set_rcv_buf( SPIHelper_TypeDef *spihelper, uint8_t *buf, uint32_t buf_size );
void SPIHelper_Set_Trans_Buf( SPIHelper_TypeDef *spihelper, uint8_t *buf, uint32_t buf_size );

int SPIHelper_Transmit( SPIHelper_TypeDef *spihelper,  uint8_t * buf, uint16_t size);
int SPIHelper_Begin_Rcv(SPIHelper_TypeDef *spihelper,  uint8_t * buf, uint16_t size);

// int SPIHelper_Flags_Set(SPIHelper_TypeDef *spihelper,  SPI_HELPER_Event_TypeDef evt_type );
// int SPIHelper_Flags_Clear(SPIHelper_TypeDef *spihelper,  SPI_HELPER_Event_TypeDef evt_type );
// int SPIHelper_Flags_Wait(SPIHelper_TypeDef *spihelper,  SPI_HELPER_Event_TypeDef evt_type );

 

void SPIHelper_Set_GPIO_For_Trans_Cplt(  SPIHelper_TypeDef *spihelper );
void SPIHelper_Rcv_Cplt_Callback( SPIHelper_TypeDef *spihelper  );
void SPIHelper_Rcv_Idle_Callback( SPIHelper_TypeDef *spihelper  );
void SPIHelper_Rcv_DMA_Half_Callback( SPIHelper_TypeDef *spihelper  );

int SPIHelper_copy(SPIHelper_TypeDef *spihelper,uint8_t *buf,uint32_t buf_size);
int SPIHelper_isbusy(SPIHelper_TypeDef *spihelper );
void SPIHelper_error(SPIHelper_TypeDef *spihelper );
 



// uint16_t Get_Crc16(const char *buf, uint16_t len);
// uint8_t Check_Crc16(uint8_t *buf, uint16_t length); 
#ifdef __cplusplus
	}
#endif

#endif

#endif

 