#ifndef __BSP_UART_H
#define __BSP_UART_H

#include "stm32f4xx.h"

/**
* 封装不同使用类型串口  232  485 max3160
* 包含发送回调的GPIO操作
* 其他回调外部处理
*/
/*  485 3160 启用一个， 两个接口都有 MAX3160_ENABLE */
#define MAX485_ENABLE  0
#define MAX3160_ENABLE 1

#ifdef __cplusplus
	extern "C" {
#endif

#include <stdlib.h>
#include "main.h"

typedef enum 
{
  Uart_Interface_Default       	= 0x00U,
  Uart_Interface_485   			= 0x01U,
  Uart_Interface_Max3160_232     = 0x02U,
  Uart_Interface_Max3160_485     = 0x03U,
} Uart_Interface_Type_Typedef; 


typedef enum 
{
  Uart_Trans_Polling       	= 0x00U,
  Uart_Trans_IT   	= 0x01U,
  Uart_Trans_DMA     = 0x02U,
} Uart_Transmode_TypeDef;
 
typedef enum 
{
  Uart_IDLE_IT_DISABLE       	= 0x00U,
  Uart_IDLE_IT_ENABLE   	= 0x01U,
} Uart_IDLE_Enable_TypeDef;
 
typedef struct
{
    uint32_t size;
    uint8_t *buf;
}Record_Trans_Rcv_TypeDef;

/* -----------------------    variables     ---------------------------------*/
// extern UART_HandleTypeDef huart3;
// UART_HandleTypeDef *puarthelper = &huart3;

typedef  void ( *enable_func_gpio)  (void);
typedef  int ( *callback_fun)  (void *obj, void *buf, uint16_t size);

typedef struct 
{
	UART_HandleTypeDef *huart;
	Uart_Interface_Type_Typedef  interface_type; /* 0: default(TTL 232), 1: 485 ,2:3160-232  3: 3160-485*/
	Uart_IDLE_Enable_TypeDef enable_idle_it;

	Uart_Transmode_TypeDef trans_mode; /* 0 :polling, 1: IT  2: DMA*/
	Uart_Transmode_TypeDef rcv_mode;

	Record_Trans_Rcv_TypeDef *trans_record;
	// Record_Trans_Rcv_TypeDef *rcv_record;

	// uint8_t *trans_buf;
	// uint16_t trans_size;
	uint8_t *rcv_buf;
	uint16_t rcv_size;

	volatile uint8_t send_flag;
	volatile uint8_t send_tc_flag;  /* 0 开始发送， 1 发送完成*/
	volatile uint8_t transferring;
	// uint32_t timebase;

	enable_func_gpio enable_trans_gpio;
	enable_func_gpio enable_trans_cplt_gpio;

	void * obj;
	callback_fun callback;

	//  这个是供外部调用的； 中断是统一管理的， 中断会找到这里的接收回调，然后返回回去


// #if MAX485_ENABLE
// 	GPIO_TypeDef *de485_gpio;
// 	uint16_t de485_pin;
//     uint8_t de485;
// #endif

// #if MAX3160_ENABLE
// 	GPIO_TypeDef *de485_gpio;
// 	uint16_t de485_pin;
// 	GPIO_TypeDef *sel_gpio;
// 	GPIO_TypeDef *dplx_gpio;
// 	uint16_t sel_pin;
// 	uint16_t dplx_pin;
//     uint8_t sel;
//     uint8_t dplx;
// #endif

    // uint8_t dplx;
    // uint8_t sel;
    // uint8_t de485;
}UART_HELPER_TypeDef;

extern UART_HELPER_TypeDef *uarthelper;

UART_HELPER_TypeDef * UART_HELPER_Init( );
void UART_HELPER_Set_Huart(UART_HELPER_TypeDef * uarthelper, UART_HandleTypeDef *huart);
int UART_HELPER_Set_Interface_Type(UART_HELPER_TypeDef * uarthelper, uint8_t interface_type);

int UART_HELPER_Setup_Trans_mode( UART_HELPER_TypeDef * uarthelper, uint8_t trans_mode );
int UART_HELPER_Setup_Rcv_mode( UART_HELPER_TypeDef * uarthelper, uint8_t rcv_mode );

int UART_HELPER_Set_trans_GPIO( UART_HELPER_TypeDef * uarthelper, enable_func_gpio trans_gpio_func );
int UART_HELPER_Set_trans_cplt_GPIO( UART_HELPER_TypeDef * uarthelper, enable_func_gpio trans_cplt_gpio_func );
 
int UART_HELPER_Set_enable_idle( UART_HELPER_TypeDef * uarthelper, uint8_t enable_idle);

int UART_HELPER_Set_Rcv_Buf( UART_HELPER_TypeDef * uarthelper, uint8_t * buf, uint16_t size);
int UART_HELPER_Set_Trans_Buf( UART_HELPER_TypeDef * uarthelper, uint8_t * buf, uint16_t size);

int UART_HELPER_Set_Callback( UART_HELPER_TypeDef * uarthelper, void *obj, callback_fun func);


int UART_HELPER_Trans(UART_HELPER_TypeDef * uarthelper, uint8_t *buf, uint16_t size);
// int __UART_HELPER_Trans(UART_HELPER_TypeDef * uarthelper , uint8_t *buf, uint16_t size);
int UART_HELPER_Trans_TxCplt_Callback( UART_HELPER_TypeDef * uarthelper );
/* 接收方式 也有三种类型 */
int UART_HELPER_Start_Rcv(UART_HELPER_TypeDef * uarthelper, uint8_t *buf, uint16_t size);

int UART_HELPER_RCV_Cplt_Callback( UART_HELPER_TypeDef * uarthelper );
int UART_HELPER_RCV_IDLE_Callback( UART_HELPER_TypeDef * uarthelper, uint16_t size );

#endif

/*

发送回调函数，通过函数指针注册引入，操作GPIO
接收回调函数通过 通过函数指针注册引入 ，操作GPIO

IDLE RXCplt 都可以产生中断，



485 
enable rcv
HAL_GPIO_WritePin(uarthelper->de485_gpio, uarthelper->de485_pin, 0); 
enable send 
HAL_GPIO_WritePin(uarthelper->de485_gpio, uarthelper->de485_pin, 1); 

3160 485   --高    (232 - 低)
enable rcv
        HAL_GPIO_WritePin(uarthelper->dplx_gpio, uarthelper->dplx_gpio ,  1);
        HAL_GPIO_WritePin(uarthelper->de485_gpio, uarthelper->de485_pin, 0); 
enable send 
        HAL_GPIO_WritePin(uarthelper->dplx_gpio, uarthelper->dplx_gpio ,  0);
        HAL_GPIO_WritePin(uarthelper->de485_gpio, uarthelper->de485_pin, 1); 


uarthelper = UART_HELPER_Init();

UART_HELPER_Set_Huart(  uarthelper, phuart),
UART_HELPER_Setup_Transmod(  uarthelper, 2 );
 UART_HELPER_Set_enable_snd_func(   uarthelper, NULL );
 UART_HELPER_Set_enable_rcv_func(   uarthelper, NULL);
 UART_HELPER_Set_enable_idle(   uarthelper,  enable_idle);
 UART_HELPER_Set_enable_rcv_func(   uarthelper,  obj, (callback_fun) func);


// #if MAX3160_ENABLE
// #define     DUPLEX_HALF  	1
// #define     DUPLEX_FULL  	0
// #define    SEL_485 			1
// #define    SEL_232 			0

// #define    ENABLE_485_Trans      1
// #define    DISABLE_485_Trans     0
// # endif

// #if MAX485_ENABLE
// #define    ENABLE_485_Trans      1
// #define    DISABLE_485_Trans     0
// # endif


*/