#include "uart1.h"


struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;  /* 初始化配置参数 */
/* 用于接收消息的信号量 */
static struct rt_semaphore rx_sem;
 rt_device_t serial;
char rx_buf[11];
char tx_buf0[1];
int sum;
int num=0;
int rx_flag=0;
float Roll;
int output;
/**

  * @brief  uart_input //接收数据回调函数

  * @param  dev

  *         size

  * @retval RT_EOK

  */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
    /* 串口接收到数据后产生中断，调用此回调函数，然后发送接收信号量 */
    rt_sem_release(&rx_sem);
    return RT_EOK;
}
/*
  * @brief  uart1_irq_entry
  * @param  parameter
  * @retval None
  */

static void uart1_irq_entry(void *parameter)
{
    uint8_t ch;
    static int num=0;
    int i;
    while (1)
    {
        /* 从串口读取一个字节的数据，没有读取到则等待接收信号量 */
        while (rt_device_read(serial, -1, &rx_buf[num], 1) != 1)
        {
            /* 阻塞等待接收信号量，等到信号量后再次读取数据 */
            rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
        }
        //rt_kprintf("%x ",rx_buf[num]);
        num++;
        if(num>10){
            rx_flag=1;
            num=0;
        }
    }
}

/*
  * @brief  SCI_Send_Datas
  * @param  None
  * @retval None
  */

void SCI_Send_Datas()
{
    static unsigned short int send_data[3][4] = { { 0 }, { 0 }, { 0 } };
    short int checksum=0;
    unsigned char xorsum=0,high,low;

    send_data[0][0] = (unsigned short int)(Roll);
    send_data[0][1] = (unsigned short int)(90);
    send_data[0][2] = (unsigned short int)(0);
    send_data[0][3] = (unsigned short int)(0);

    send_data[1][0] = (unsigned short int)(output);
    send_data[1][1] = (unsigned short int)(0);
    send_data[1][2] = (unsigned short int)(0);
    send_data[1][3] = (unsigned short int)(0);

    send_data[2][0] = (unsigned short int)(0);
    send_data[2][1] = (unsigned short int)(0);
    send_data[2][2] = (unsigned short int)(0);
    send_data[2][3] = (unsigned short int)(0);

    rt_sprintf(tx_buf0, "%c",0x53);
    rt_device_write(serial, 0, tx_buf0, (sizeof(tx_buf0)) );
    rt_sprintf(tx_buf0, "%c",0x54);
    rt_device_write(serial, 0, tx_buf0, (sizeof(tx_buf0)) );

    for (int i = 0; i < 3; i++)
    for (int j = 0; j < 4; j++)
    {
      low=(unsigned char)(send_data[i][j] & 0x00ff);
      high=(unsigned char)(send_data[i][j] >> 8u);

      rt_sprintf(tx_buf0, "%c",low);
      rt_device_write(serial, 0, tx_buf0, (sizeof(tx_buf0)) );
      rt_sprintf(tx_buf0, "%c",high);
      rt_device_write(serial, 0, tx_buf0, (sizeof(tx_buf0)) );

      checksum+=low; checksum+=high;
      xorsum^=low; xorsum^= high;
    }

    rt_sprintf(tx_buf0, "%c",(unsigned char)(checksum & 0x00ff));
    rt_device_write(serial, 0, tx_buf0, (sizeof(tx_buf0)) );
    rt_sprintf(tx_buf0, "%c",xorsum);
    rt_device_write(serial, 0, tx_buf0, (sizeof(tx_buf0)) );
}

/*
  * @brief  uart1_init
  * @param  None
  * @retval ret
  */
int uart1_init(void)
{
    rt_err_t ret = RT_EOK;
    char uart_name[RT_NAME_MAX];
    char str[] = "hello RT-Thread!\r\n";
    rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
    /* 查找系统中的串口设备 */
    serial = rt_device_find(uart_name);
    if (!serial)
    {
        rt_kprintf("find %s failed!\n", uart_name);
        return RT_ERROR;
    }
    /* 修改串口配置参数 */
    config.baud_rate = BAUD_RATE_115200;        //修改波特率为 115200
    config.data_bits = DATA_BITS_8;           //数据位 8
    config.stop_bits = STOP_BITS_1;           //停止位 1
    config.bufsz     = 512;                   //修改缓冲区 buff size 为 128
    config.parity    = PARITY_NONE;           //无奇偶校验位
    /* 控制串口设备。通过控制接口传入命令控制字，与控制参数 */
    rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
    /* 初始化信号量 */
    rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
    /* 以中断接收及轮询发送模式打开串口设备 */
    rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
    /* 设置接收回调函数 */
    rt_device_set_rx_indicate(serial, uart_input);
    /* 发送字符串 */
    rt_device_write(serial, 0, str, (sizeof(str) - 1));
    /* 创建 serial 线程 */
    rt_thread_t thread = rt_thread_create("serial", uart1_irq_entry, RT_NULL, 1024,1, 1);
    /* 创建成功则启动线程 */
    if (thread != RT_NULL)
    {
        rt_thread_startup(thread);
    }
    else
    {
        ret = RT_ERROR;
    }
    return ret;
}

/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(uart1_init, uart device sample);
