#include "hsuart_transfer.h"

volatile u16 RECEIVE_Len;           //Received data length

u8 RECEIVE_BUF[HSART_RX_BUFF_LEN];  //Receive buffer
u8 TRANSMIT_BUF[HSART_TX_BUFF_LEN];     //transmit buffer

u16 BtTxBuferCount;     // Send data length

/* Serial interrupt */
AT(.com_text.isr)
void hsuart_isr(void)
{
    if(HSUT0CON & BIT(12))
    {
        RECEIVE_Len = HSUT0FIFOCNT;          // Receive data size
        HSUT0CPND |= BIT(12) | BIT(15);
        HSUT0RXCNT = 0;

        hsuart_dma_start(HSUT_RECEIVE, (uint32_t)RECEIVE_BUF, HSART_RX_BUFF_LEN);   //Enable next reception
        memcpy(&TRANSMIT_BUF[BtTxBuferCount],RECEIVE_BUF,RECEIVE_Len);         //Save the data to the send buffer
        BtTxBuferCount += RECEIVE_Len;
    }
}

void my_hsuart_init(uint32_t baud)
{
    gpio_init_typedef gpio_init_structure;
    hsuart_init_typedef hsuart_init_struct;

    clock_gate0_cmd(CLOCK_GATE0_HSUART0, ENABLE);

    gpio_init_structure.gpio_pin = GPIO_PIN_6;
    gpio_init_structure.gpio_dir = GPIO_DIR_INPUT;
    gpio_init_structure.gpio_fen = GPIO_FEN_PER;
    gpio_init_structure.gpio_mode = GPIO_MODE_DIGITAL;
    gpio_init_structure.gpio_pupd = GPIO_PUPD_UP;
    gpio_init(GPIOE_REG, &gpio_init_structure);

    gpio_init_structure.gpio_pin = GPIO_PIN_7;
    gpio_init_structure.gpio_dir = GPIO_DIR_OUTPUT;
    gpio_init_structure.gpio_drv = GPIO_DRV_8MA;
    gpio_init(GPIOE_REG, &gpio_init_structure);

    gpio_func_mapping(HSUTTXMAP_PE7);
    gpio_func_mapping(HSUTRXMAP_PE6);

    hsuart_init_struct.baud =  baud;

    hsuart_init_struct.tx_mode = HSUT_TX_DMA_MODE;
    hsuart_init_struct.rx_mode = HSUT_RX_DMA_MODE;
    hsuart_init_struct.tx_stop_bit = HSUT_STOP_BIT_1BIT;
    hsuart_init_struct.tx_word_len = HSUT_TX_LENGTH_8b;
    hsuart_init_struct.rx_word_len = HSUT_RX_LENGTH_8b;
    hsuart_init(&hsuart_init_struct);

    hsuart_pic_config(hsuart_isr, 0, HSUART_IT_RX, ENABLE);

    hsuart_dma_config(HSUT_TRANSMIT, (uint32_t)TRANSMIT_BUF);
    hsuart_dma_config(HSUT_RECEIVE, (uint32_t)RECEIVE_BUF);

    hsuart_cmd(HSUT_RECEIVE, ENABLE);
    hsuart_cmd(HSUT_TRANSMIT, DISABLE);

    hsuart_dma_start(HSUT_RECEIVE, (uint32_t)RECEIVE_BUF, HSART_RX_BUFF_LEN);
}

void hsuart_transfer_example(void)
{
    if(BtTxBuferCount>0)
    {
        hsuart_cmd(HSUT_TRANSMIT, ENABLE);
        hsuart_dma_start(HSUT_TRANSMIT, (uint32_t)TRANSMIT_BUF,BtTxBuferCount);
        BtTxBuferCount=0;
    }
}

/*串口发送一个字节*/
void huart_SendByte(uint8_t Byte)
{
	hsuart_dma_config(HSUT_TRANSMIT, (uint32_t)&Byte);
    hsuart_cmd(HSUT_TRANSMIT, ENABLE);
    hsuart_dma_start(HSUT_TRANSMIT, (uint32_t)&Byte,1);
}

/*串口发送带长度的字符串*/
void huart_SendArray(uint8_t *Array, uint16_t Length)
{
	uint16_t i;
	for (i = 0; i < Length; i ++)
	{
		huart_SendByte(Array[i]);
	}
}

/*串口发送字符串*/
void huart_SendString(char *String)
{
	uint8_t i;
	for (i = 0; String[i] != '\0'; i ++)
	{
		huart_SendByte(String[i]);
	}
}

uint32_t huart_Pow(uint32_t X, uint32_t Y)
{
	uint32_t Result = 1;
	while (Y --){
		Result *= X;
	}
	return Result;
}

/*串口发送十进制数字*/
void  huart_SendNumber(uint32_t Number, uint8_t Length)
{
	uint8_t i;
	for (i = 0; i < Length; i ++){
		huart_SendByte(Number / huart_Pow(10, Length - i - 1) % 10 + '0');
	}
}
