#include "bsp_uart.h"

MYUART_TypeDef * Myuart_Init( )
{
    MYUART_TypeDef *Handle = (MYUART_TypeDef *)malloc(sizeof(MYUART_TypeDef));
    if (Handle == NULL)
    {
        return NULL;
    }
	Handle->send_flag = 0;
	Handle->send_tc_flag = 0;
	Handle->interface_type = 0;
	Handle->enable_idle_it = 0;
	Handle->enable_rcv =NULL;
	Handle->enable_snd =NULL;
 
// #if MAX485_ENABLE
// 	Handle->de485_gpio = NULL;
// 	Handle->interface_type = 1;
// #endif

// #if MAX3160_ENABLE
//     Handle->de485_gpio = NULL;
// 	Handle->sel_gpio = NULL;
// 	Handle->dplx_gpio = NULL;
// 	Handle->interface_type = 2;
// #endif

    return Handle;
}

void Myuart_Set_Huart(MYUART_TypeDef * myuart, UART_HandleTypeDef *huart)
{
    myuart->huart = huart;
}

int Myuart_Setup_Transmod( MYUART_TypeDef * myuart, uint8_t trans_mode )
{
    /* 0 :polling, 1: IT  2: DMA*/
	myuart->trans_mode = trans_mode;
	return 0;
}

int Myuart_Set_enable_snd_func( MYUART_TypeDef * myuart, enable_func enable_snd_func )
{
	if (enable_snd_func != NULL)  myuart->enable_snd = enable_snd_func;
	return 0;
}
int Myuart_Set_enable_rcv_func( MYUART_TypeDef * myuart, enable_func enable_rcv_func)
{
	if (enable_rcv_func != NULL)  myuart->enable_rcv = enable_rcv_func;
	return 0;
}

int Myuart_Set_enable_idle( MYUART_TypeDef * myuart, uint8_t enable_idle)
{
	myuart->enable_idle_it = enable_idle;
}

int Myuart_Set_Callback( MYUART_TypeDef * myuart, void *obj, callback_fun func)
{
	myuart->obj = obj;
	myuart->callback = func;
}

int Myuart_Start_Rcv(MYUART_TypeDef * myuart, uint8_t *buf, uint16_t size)
{
	int st;
    switch ( myuart->trans_mode)
    {
        case 0:
            st = HAL_UART_Transmit(myuart->huart,  buf,  size  , 0xFFFF);
            return st;
            break;
        case 1:
            st = HAL_UART_Transmit_IT(myuart->huart,  buf,  size);
            return st;
            break;
        case 2:
            /* 会出现 st ==2  huart->gState != HAL_UART_STATE_READY */
			if (myuart->enable_idle_it == 1) 
			{
				__HAL_USART_ENABLE_IT(myuart->huart, UART_IT_IDLE);
			}
            st = HAL_UART_Transmit_DMA(myuart->huart,  buf,  size);
            return st;
            break;    
        default:
            break;
    }
}

int Myuart_Trans(MYUART_TypeDef * myuart, uint8_t *buf, uint16_t size)
{
	int st;
	if (myuart->enable_snd != NULL)
	{
		myuart->enable_snd;
	}

    switch ( myuart->trans_mode)
    {
        case 0:
            st = HAL_UART_Transmit(myuart->huart,  buf,  size  , 0xFFFF);
            return st;
            break;
        case 1:
            st = HAL_UART_Transmit_IT(myuart->huart,  buf,  size);
            return st;
            break;
        case 2:
            /* 会出现 st ==2  huart->gState != HAL_UART_STATE_READY */
            st = HAL_UART_Transmit_DMA(myuart->huart,  buf,  size);
            return st;
            break;    
        default:
            break;
    }
    return 0;
}


int Myuart_Trans_TxCplt_Callback( MYUART_TypeDef * myuart )
{

	if (myuart->enable_rcv != NULL)
	{
		myuart->enable_rcv;
	}
    return 0;
}

// void Myuart_Set_Sel_GPIO_Pin(MYUART_TypeDef * myuart, GPIO_TypeDef *gpio, uint16_t pin)
// {
// #if MAX3160_ENABLE
//     myuart->sel_gpio = gpio;
//     myuart->sel_pin = pin;
// #endif
 
// }

// void Myuart_Set_Dplx_GPIO_Pin(MYUART_TypeDef * myuart, GPIO_TypeDef *gpio, uint16_t pin)
// {
// #if MAX3160_ENABLE
//     myuart->dplx_gpio = gpio;
//     myuart->dplx_pin = pin;
// #endif
// }

// void Myuart_Set_DE485_GPIO_Pin(MYUART_TypeDef * myuart, GPIO_TypeDef *gpio, uint16_t pin)
// {
// #if MAX3160_ENABLE
//     myuart->de485_gpio = gpio;
//     myuart->de485_pin = pin;
// #endif

// #if MAX485_ENABLE
//     myuart->de485_gpio = gpio;
//     myuart->de485_pin = pin;
// #endif
// }

// void Myuart_Set_232(MYUART_TypeDef * myuart)
// {
// #if MAX3160_ENABLE
//     myuart->sel = SEL_232;
//     HAL_GPIO_WritePin( myuart->sel_gpio, myuart->sel_pin, myuart->sel );
//     HAL_GPIO_WritePin( myuart->dplx_gpio, myuart->dplx_pin, DUPLEX_FULL );
// #endif
// }

// void Myuart_Set_485(MYUART_TypeDef * myuart)
// {
// #if MAX3160_ENABLE
//     myuart->sel = SEL_485;
//     HAL_GPIO_WritePin( myuart->sel_gpio, myuart->sel_pin, myuart->sel );
//     HAL_GPIO_WritePin( myuart->dplx_gpio, myuart->dplx_pin, DUPLEX_HALF );
//     HAL_GPIO_WritePin( myuart->de485_gpio, myuart->de485_pin, DISABLE_485_Trans );
// #endif
// }


// int __Myuart_Send(MYUART_TypeDef * myuart , uint8_t *buf, uint16_t size)
// {
//     uint8_t st;
//     switch ( myuart->trans_mode)
//     {
//         case 0:
//             st = HAL_UART_Transmit(myuart->huart,  buf,  size  , 0xFFFF);
//             return st;
//             break;
//         case 1:
//             st = HAL_UART_Transmit_IT(myuart->huart,  buf,  size);
//             return st;
//             break;
//         case 2:
//             /* 会出现 st ==2  huart->gState != HAL_UART_STATE_READY */
//             st = HAL_UART_Transmit_DMA(myuart->huart,  buf,  size);
//             return st;
//             break;    
//         default:
//             break;
//     }
//     return 0;
// }