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

#ifdef __cplusplus
	extern "C" {
#endif

#include "uart_interface.h"

/*  回调函数，将数据返回上层 */
typedef void (*it_callback)(void *obj,uint8_t *buf, uint32_t buf_size );

// typedef struct uart_tx_record
// {
//     uint32_t size;
//     uint8_t *buf;
// } Uart_Trans_Record_TypeDef;

typedef struct 
{
    UART_HandleTypeDef *huart;
	UartInterface_TypeDef *interface;
    uint8_t interface_type; /* 0: common, 1: 485 ,2:3160*/
	uint8_t mode_232_485; /* 0: common, 1: 485  */
    uint8_t trans_type;  /* 0 :polling, 1: IT  2: DMA*/
    uint8_t rcv_type;  /* 0 :polling, 1: IT  2: DMA*/
    uint8_t idle_enable_disable;    /* 0 不开启空闲中断 ， 1 开启空闲中断 */
	uint8_t trans_mode;         /* 0 :polling, 1: IT  2: DMA*/

    volatile uint32_t trans_size;
    uint8_t *trans_buf;

    volatile uint8_t receive_buf_con;
    // volatile uint8_t transferring_flag;
    volatile uint32_t receive_data_size;
    uint8_t *receive_buf;
    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;

}UartHelper_TypeDef;

UartHelper_TypeDef * UartHelper_Init( );
void UartHelper_Set_Interface( UartHelper_TypeDef *uarthelper,  UartInterface_TypeDef * interface );
void UartHelper_Set_Huart( UartHelper_TypeDef *uarthelper,  UART_HandleTypeDef * huart  );
void UartHelper_Set_Interface_Type( UartHelper_TypeDef *uarthelper,   Uart_Interface_Type_Typedef interface_type  );
void UartHelper_Set_Trans_Type( UartHelper_TypeDef *uarthelper,  Uart_Transmode_TypeDef trans_type  );
void UartHelper_Set_Rcv_Type( UartHelper_TypeDef *uarthelper,  Uart_Transmode_TypeDef rcv_type  );
void UartHelper_Set_Idle_Enable( UartHelper_TypeDef *uarthelper,  Uart_IDLE_Enable_TypeDef idle_enable_disable  );
void UartHelper_Set_TransMode( UartHelper_TypeDef *uarthelper,  Uart_Transmode_TypeDef trans_mode  );
void UartHelper_Set_RsMode_232_485( UartHelper_TypeDef *uarthelper,  Uart_RS_Mode_TypeDef mode_232_485  );
// void UartHelper_Set_timeout( UartHelper_TypeDef *uarthelper,  uint32_t timeout_ms  );

int UartHelper_Set_callback_func_obj( UartHelper_TypeDef *uarthelper,   void * obj,  it_callback callback );


void UartHelper_Set_rcv_buf( UartHelper_TypeDef *uarthelper, uint8_t *buf, uint32_t buf_size );
void UartHelper_Set_Trans_Buf( UartHelper_TypeDef *uarthelper, uint8_t *buf, uint32_t buf_size );

int UartHelper_Transmit( UartHelper_TypeDef *uarthelper,  uint8_t * buf, uint16_t size);
int UartHelper_Begin_Rcv(UartHelper_TypeDef *uarthelper,  uint8_t * buf, uint16_t size);

void UartHelper_Set_GPIO_For_Trans_Cplt(  UartHelper_TypeDef *uarthelper );
void UartHelper_Rcv_Cplt_Callback( UartHelper_TypeDef *uarthelper  );
void UartHelper_Rcv_Idle_Callback( UartHelper_TypeDef *uarthelper  );
void UartHelper_Rcv_DMA_Half_Callback( UartHelper_TypeDef *uarthelper  );

int UartHelper_copy(UartHelper_TypeDef *uarthelper,uint8_t *buf,uint32_t buf_size);
int UartHelper_isbusy(UartHelper_TypeDef *uarthelper );
void UartHelper_error(UartHelper_TypeDef *uarthelper );
 

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

#endif

 