#include "usart.h"


    /*  \arg        COM_EC800:   EC800
        \arg        COM_485: 485
	    \arg        COM_232: 232
    */
static rcu_periph_enum COM_CLK[COMn] = {COM_EC800_CLK, COM_485_CLK, COM_232_CLK};
static uint32_t COM_TX_PIN[COMn] = {COM_EC800_TX_PIN, COM_485_TX_PIN, COM_232_TX_PIN};
static uint32_t COM_RX_PIN[COMn] = {COM_EC800_RX_PIN, COM_485_RX_PIN, COM_232_RX_PIN};
static uint32_t COM_GPIO_PORT[COMn] = {COM_EC800_GPIO_PORT, COM_485_GPIO_PORT, COM_232_GPIO_PORT};
static rcu_periph_enum COM_GPIO_CLK[COMn] = {COM_EC800_GPIO_CLK, COM_485_GPIO_CLK, COM_232_GPIO_CLK};	  
static uint32_t COM_IT_HANDLER[COMn] = {COM_EC800_IT_HANDLER, COM_485_IT_HANDLER, COM_232_IT_HANDLER};
static uint32_t COM_BAUDTATE[COMn]={COM_EC800_BAUDRATE,COM_485_BAUDRATE,COM_232_BAUDRATE};

		
uint8_t UART0_RX_BUF[UART0_RX_LEN];
uint8_t UART0_RX_STAT = 0;
uint8_t UART0_RX_MQTT_SUB_STAT=0;            //mqttt订阅信息标志位
uint32_t UART0_RX_NUM = 0;	

usart_data_buf_t usart1_rx_buf;
//清除dma buffer内数据
void Clear_DMA_Buffer(void)
{
   UART0_RX_NUM=0;
   memset(UART0_RX_BUF,0,UART0_RX_LEN);
}


/*!
    \brief      configure COM port
    \param[in]  com: COM on the board
        \arg        COM_EC800:   EC800
        \arg        COM_485: 485
	    \arg        COM_232: 232
    \param[out] none
    \retval     none
*/
void gd_com_init(uint32_t com)
{
    uint32_t com_id = 0U;
    if(COM_EC800 == com)
	{
        com_id = 0U;
    }
	else if(COM_485 == com)
	{
		com_id=1U;
	}
	else if(COM_232 == com)
	{
        com_id = 2U;
		//重映射usart2端口
				rcu_periph_clock_enable(RCU_AF);
				gpio_pin_remap_config(GPIO_USART2_FULL_REMAP, ENABLE);
    }

	//nvic_irq_enable(COM_IT_HANDLER[com_id], 0, 0);
    
    /* enable GPIO clock */
				
    rcu_periph_clock_enable(COM_GPIO_CLK[com_id]);

    /* enable USART clock */
    rcu_periph_clock_enable(COM_CLK[com_id]);
		
		
    /* connect port to USARTx_Tx */
    gpio_init(COM_GPIO_PORT[com_id], GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, COM_TX_PIN[com_id]);

    /* connect port to USARTx_Rx */
    gpio_init(COM_GPIO_PORT[com_id], GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, COM_RX_PIN[com_id]);
		
		
    /* USART configure */
    usart_deinit(com);
    usart_baudrate_set(com, COM_BAUDTATE[com_id]);
    usart_word_length_set(com, USART_WL_8BIT);
    usart_stop_bit_set(com, USART_STB_1BIT);
    usart_parity_config(com, USART_PM_NONE);
    usart_hardware_flow_rts_config(com, USART_RTS_DISABLE);
    usart_hardware_flow_cts_config(com, USART_CTS_DISABLE);
    usart_receive_config(com, USART_RECEIVE_ENABLE);
    usart_transmit_config(com, USART_TRANSMIT_ENABLE);
    usart_enable(com);
	// usart_interrupt_enable(com, USART_INT_TBE);
	  usart_interrupt_enable(com, USART_INT_RBNE);
   	usart_interrupt_enable(com, USART_INT_IDLE);
    
}

/*!
    \brief      configure EC800M pin
    \param[out] none 
    \retval     none
*/
void gd_EC800M_pin_init(void)
{
    /* enable the EC800M power pin clock */
    rcu_periph_clock_enable(EC800M_PER_GPIO_CLK);
    /* configure EC800M GPIO port */ 
    gpio_init(EC800M_PER_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, EC800M_PER_PIN);

    GPIO_BC(EC800M_PER_GPIO_PORT) = EC800M_PER_PIN;

    /* enable the EC800M reset pin clock */
    rcu_periph_clock_enable(EC800M_RST_GPIO_CLK);
    /* configure EC800M GPIO port */ 
    gpio_init(EC800M_RST_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, EC800M_RST_PIN);

    GPIO_BC(EC800M_RST_GPIO_PORT) = EC800M_RST_PIN;
    
}

void gd_pull_EC800M_pwr_up(void)
{
	GPIO_BOP(EC800M_PER_GPIO_PORT) = EC800M_PER_PIN;
}

void gd_pull_EC800M_pwr_down(void)
{
	GPIO_BC(EC800M_PER_GPIO_PORT) = EC800M_PER_PIN;
}

void gd_pull_EC800M_rst_up(void)
{
	GPIO_BOP(EC800M_RST_GPIO_PORT) = EC800M_RST_PIN;
}

void gd_pull_EC800M_rst_down(void)
{
	GPIO_BC(EC800M_RST_GPIO_PORT) = EC800M_RST_PIN;
}

////////////////////////////////////////////////////////////////////
//默认的dma配置
void dma_config(void)
{
    dma_parameter_struct dma_init_struct;

    rcu_periph_clock_enable(RCU_DMA0);

    dma_deinit(DMA0, DMA_CH4);
    dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;		
    dma_init_struct.memory_addr = (uint32_t)UART0_RX_BUF;		
    dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;	
    dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;		
    dma_init_struct.number = UART0_RX_LEN;						
    dma_init_struct.periph_addr = (uint32_t)(&USART_DATA(USART0));// ((uint32_t)0x40013804);		
    dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;	
    dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;	
    dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;			
    dma_init(DMA0, DMA_CH4, &dma_init_struct);
    
    dma_circulation_disable(DMA0, DMA_CH4);
    dma_memory_to_memory_disable(DMA0, DMA_CH4);

    usart_dma_transmit_config(USART0, USART_DENT_ENABLE|USART_DENR_ENABLE);
		//nvic_irq_enable(DMA0_Channel4_IRQn, 2, 2);
	
    dma_interrupt_enable(DMA0, DMA_CH4, DMA_INT_FTF|DMA_INT_HTF|DMA_INT_ERR);
    dma_channel_enable(DMA0, DMA_CH4);
	 

}
//手动控制dma搬运的数据地址和大小，在使用完成后需要恢复到默认的dma配置
void dma_config_change(char *dmaBuffer,uint32_t bufferSize)
{
	  dma_parameter_struct dma_init_struct;

    rcu_periph_clock_enable(RCU_DMA0);

    dma_deinit(DMA0, DMA_CH4);
    dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;		
    dma_init_struct.memory_addr = (uint32_t)dmaBuffer;		
    dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;	
    dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;		
    dma_init_struct.number = bufferSize;						
    dma_init_struct.periph_addr = (uint32_t)(&USART_DATA(USART0));// ((uint32_t)0x40013804);		
    dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;	
    dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;	
    dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;			
    dma_init(DMA0, DMA_CH4, &dma_init_struct);
    
    dma_circulation_disable(DMA0, DMA_CH4);
    dma_memory_to_memory_disable(DMA0, DMA_CH4);

    usart_dma_transmit_config(USART0, USART_DENT_ENABLE|USART_DENR_ENABLE);
		//nvic_irq_enable(DMA0_Channel4_IRQn, 2, 2);
	
    dma_interrupt_enable(DMA0, DMA_CH4, DMA_INT_FTF|DMA_INT_ERR);
    dma_channel_enable(DMA0, DMA_CH4);
}


void nvic_config(void)
{
    nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);  // 设置抢占优先级和子优先级的位数为 2
		nvic_irq_enable(USART0_IRQn, 0, 0);
		// nvic_irq_enable(USART1_IRQn, 1, 0);
		// nvic_irq_enable(USART2_IRQn, 2, 0);
		nvic_irq_enable(DMA0_Channel4_IRQn, 2, 1);
	
}


