#include "include.h"
#include "driver_gpio.h"
#include "driver_uart.h"
#include "uart_transfer.h"

#if TRACE_EN
#define TRACE(...)                          my_printf("[WIRELESS] ");\
                                            my_printf(__VA_ARGS__)
#define TRACE_R(...)                        my_print_r(__VA_ARGS__);
#else
#define TRACE(...)
#define TRACE_R(...)
#endif // TRACE_EN

#define UART_TX_PORT              GPIOA_REG
#define UART_TX_PIN               GPIO_PIN_5
#define UART_RX_PORT              GPIOA_REG
#define UART_RX_PIN               GPIO_PIN_6

#define USB_EP1_COOR_SIZE     (PACKAGE_POINT_SIZE *(MAX_POINT/PACKAGE_POINT_NUM))

uart_transfer_cb_typedef uart_rx = {
    .done = 0,
    .len = 0,
    .backlen = 0,
    .timeout = 50, //ms
};

u8 u8ReceiveDataStep = 0;


AT(.com_text.isr)
void uart_isr(void)
{
    if (uart_get_flag(UART, UART_IT_RX) != RESET) {
        uart_rx.tick = tick_get();
        if (uart_rx.len < sizeof(uart_rx.buf))
        {
            switch (u8ReceiveDataStep)
            {
            case 0:
                if (uart_receive_data(UART) == 0x55)
                {
                    uart_rx.buf[uart_rx.len++] = uart_receive_data(UART);
                    u8ReceiveDataStep = 1;
                }
                else
                {
                    uart_rx.len = 0;
                    u8ReceiveDataStep = 0;
                }
                break;

            case 1:
                if (uart_receive_data(UART) == 0x54)
                {
                    uart_rx.buf[uart_rx.len++] = uart_receive_data(UART);
                    u8ReceiveDataStep = 2;
                }
                else
                {
                    uart_rx.len = 0;
                    u8ReceiveDataStep = 0;
                }
                break;
            /*
            case 2:
                if ((uart_receive_data(UART) & 0x80) == 0x80)
                {
                    uart_rx.buf[uart_rx.len++] = uart_receive_data(UART);
                    u8ReceiveDataStep = 3;
                }
                else
                {
                    uart_rx.len = 0;
                    u8ReceiveDataStep = 0;
                }
                break;
            */
            default:
                uart_rx.buf[uart_rx.len++] = uart_receive_data(UART);
                break;
            }
        }

        uart_clear_flag(UART, UART_IT_RX);
    }
}

void uart_transfer_init(u32 baud)
{
    if (bTransmitEn == TRUE)  //transmit
    {
        return;
    }

    uart_init_typedef uart_init_struct;
    gpio_init_typedef gpio_init_structure;

    clk_gate0_cmd(CLK_GATE0_UART, CLK_EN);

    //clk_udet_clk_set(XOSC24M_CLK);  //udet 必须调用

    // RX
    gpio_init_structure.gpio_pin = UART_RX_PIN;
    gpio_init_structure.gpio_dir = GPIO_DIR_INPUT;
    gpio_init_structure.gpio_fen = GPIO_FEN_PER;
    gpio_init_structure.gpio_fdir = GPIO_FDIR_SELF;
    gpio_init_structure.gpio_mode = GPIO_MODE_DIGITAL;
    gpio_init_structure.gpio_pupd = GPIO_PUPD_PU10K;
    gpio_init(UART_RX_PORT, &gpio_init_structure);

    // TX
    gpio_init_structure.gpio_pin = UART_TX_PIN;
    gpio_init_structure.gpio_dir = GPIO_DIR_OUTPUT;
    gpio_init_structure.gpio_drv = GPIO_DRV_6MA;
    gpio_init(UART_TX_PORT, &gpio_init_structure);

    gpio_func_mapping_config(UART_RX_PORT, UART_RX_PIN, GPIO_CROSSBAR_IN_UARTRX);
    gpio_func_mapping_config(UART_TX_PORT, UART_TX_PIN, GPIO_CROSSBAR_OUT_UARTTX);

    uart_init_struct.baud = baud;
    uart_init_struct.mode = UART_SEND_RECV_MODE;
    uart_init_struct.word_len = UART_WORD_LENGTH_8b;
    uart_init_struct.stop_bits = UART_STOP_BIT_1BIT;
    uart_init_struct.one_line_enable = UART_SEPARATE;
    /* Only UART Valid */
    uart_init_struct.baud_fix = UART_BAUD_FIX;
    /* Only UART Valid */
    uart_init_struct.clock_source = UART_INC_CLOCK;
    uart_init(UART, &uart_init_struct);

    uart_pic_config(UART, uart_isr, 0, UART_IT_RX, ENABLE);

    uart_cmd(UART, ENABLE);

    //PRINT("uart_transfer_init\n");
}

void uart_transfer_data()
{
    //if (((tick_get() - uart_rx.tick) >= uart_rx.timeout) && uart_rx.len)
    if ((USB_EP1_COOR_SIZE + 10) == uart_rx.len)
    {
		//uart_rx.done = 1;
        while (uart_rx.backlen < 10) {
            uart_rx.bufback[uart_rx.backlen] = uart_rx.buf[USB_EP1_COOR_SIZE + uart_rx.backlen];
            uart_rx.backlen++;
        }

        uart_rx.len = 0;
        u8ReceiveDataStep = 0;

        u8 *p = uart_rx.bufback;

        while (uart_rx.backlen) {

            uart_send_data(UART, *p++);
            while (uart_get_flag(UART, UART_IT_TX) != SET);
            uart_rx.backlen --;
        }
    }
}

void uart_to_wireless_transfer_data()
{
    if (10 == uart_rx.len)
    {

		while (uart_rx.backlen < 10) {
            uart_rx.bufback[uart_rx.backlen] = uart_rx.buf[uart_rx.backlen];
            uart_rx.backlen++;
        }

        uart_rx.len = 0;
        u8ReceiveDataStep = 0;
        uart_rx.backlen = 0;

        wireless_send_test(10, (u8 *)uart_rx.bufback);
    }
}
