/**
 * @file wk2114.h
 * @author Chen Jihang (embedded@eseaoptics.com)
 * @brief WK2114串口拓展芯片驱动
 * @version 1.0
 * @date 2023-01-04
 * 
 * @copyright ESEA (c) 2020
 * 
 */
#ifndef WK2114_H_
#define WK2114_H_
#include <stdint.h>
#include <stddef.h>
#include "FreeRTOS.h"
#include "task.h"
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"
#include "usart.h"
#include "stm32f4xx.h"
#include "elog.h"

extern UART_HandleTypeDef *pWK2114Uart;
extern osEventFlagsId_t wk2114EventHandle;

#define WK2114_CLOCK_HZ (12000000U)

#define WK2114_CMD_IS_WRITE(cmd) (((cmd>>6)&1)==0)
#define WK2114_CMD_IS_READ(cmd) (((cmd>>6)&1)==1)
#define WK2114_CMD_IS_REG(cmd) (((cmd>>7)&1)==0)
#define WK2114_CMD_IS_FIFO(cmd) (((cmd>>7)&1)==1)
#define WK2114_CMD_GET_ADDR(cmd) (cmd&0x3f)
#define WK2114_CMD_GET_SIZE(cmd) ((cmd&0x0f)+1)
#define WK2114_CMD_GET_CMD(cmd) (cmd>>6)
#define WK2114_CMD_GET_SUB_UART(cmd) ((cmd>>4)&0x03)
#define WK2114_REG_IS_GLOBAL(reg) ((reg&0x0f)<=0x02?1:0)
#define WK2114_BUILD_SUB_REG(sub_uart,reg) ((sub_uart<<4)|(reg&0x0f))

typedef enum 
{
    MODULE_WK2114_EVENT_READY,
    MODULE_WK2114_EVENT_EXTI_IRQ,
    MODULE_WK2114_EVENT_EXTI_IDLE,
    MODULE_WK2114_EVENT_EXTI_IT_TYPE_SENDING,
    MODULE_WK2114_EVENT_EXTI_IT_TYPE_RECEIVED,
    MODULE_WK2114_EVENT_EXTI_RCV_FIFO_COUNT_RECEIVED,
    MODULE_WK2114_EVENT_EXTI_RCV_FIFO_BUF_RECEIVED,
    // WK2114E_WK2114_EVENT_EXTI_READ_FIFO_BY_COUNT,

} MODULE_WK2114_EVENT_TypeDef;

typedef enum 
{
    WK2114_WORK_STATE_WAIT,
    WK2114_WORK_STATE_START,
    WK2114_WORK_STATE_SET_BAUD,
    WK2114_WORK_STATE_SET_BAUD_OK,
    WK2114_WORK_STATE_SEND_40_RCV_F0,
    WK2114_WORK_STATE_SEND_40_RCV_F0_OK,
    WK2114_WORK_TEST_OK,
 
    WK2114_WORK_STATE_ADD_SUB_UART,
    WK2114_WORK_STATE_ADD_SUB_UART_OK,
    WK2114_WORK_STATE_OK,
 
    WK2114_WORK_STATE_IT_SOURCE_GETTING,
    WK2114_WORK_STATE_IT_TYPE_GETTING,


    WK2114_WORK_STATE_PROCESS_IT_TYPE,
    WK2114_WORK_STATE_EXTI_IT_SOURCE_GETTING,
    WK2114_WORK_STATE_EXTI_IT_SOURCE_RECEIVE,
    WK2114_WORK_STATE_EXTI_IT_TYPE_GETTING,
    WK2114_WORK_STATE_EXTI_IT_TYPE_RECEIVE,
    WK2114_WORK_STATE_EXTI_PROCESS,
    WK2114_WORK_STATE_EXTI_PROCESS_RFTRIG_INT,
    WK2114_WORK_STATE_EXTI_PROCESS_RXOVT_INT,
    WK2114_WORK_STATE_EXTI_PROCESS_TFTRIG_INT,
    WK2114_WORK_STATE_EXTI_PROCESS_TFEMPTY_INT,
    WK2114_WORK_STATE_EXTI_PROCESS_FERR_INT,

    WK2114_WORK_STATE_EXTI_PROCESS_GET_FIFO_COUNT,
    WK2114_WORK_STATE_EXTI_READ_FIFO_BY_COUNT,
    WK2114_WORK_STATE_EXTI_DEAL_FIFO_READOUT,
    // WK2114_WORK_STATE_EXTI_READ_FIFO_DEAL,

    WK2114_WORK_STATE_EXTI_READ_SUB_FSR,
    WK2114_WORK_STATE_EXTI_READ_SUB_LSR,
    WK2114_WORK_STATE_EXTI_DEAL_LSR,
    WK2114_WORK_STATE_EXTI_READ_REG,

    WK2114_WORK_STATE_ERROR,
    WK2114_WORK_STATE_STOP
} WK2114_WORK_STATE_TypeDef;

typedef enum   
{
    WK2114_G_FLAG_MASK_BUSY_RCV = (1<<0),      //< wk2114标志位 正在等待接收
    WK2114_G_FLAG_MASK_INTER = (1<<1),    //< wk2114标志位 正在处理中断
    WK2114_G_FLAG_MASK_SENDING = (1<<2),   //< wk2114标志位 正在发送
} WK2114_G_FLAG_TypeDef;

#define WK2114_S_UART_FLAG_MASK_IDLE (1<<0)     //< 子串口标志位 接收到串口空闲中断
#define WK2114_S_UART_FLAG_MASK_PAGE (1<<1)     //< 子串口标志位 设置的寄存器页
#define WK2114_S_UART_FLAG_MASK_PAGE_D (1<<2)   //< 子串口标志位 当前寄存器页, 0->pahg0 ,1-page1
#define WK2114_S_UART_FLAG_MASK_ENABLE (1<<3)   //< 子串口标志位 使能

// #define WK2114_G_FLAG_MASK_BUSY_RCV (1<<0)      //< wk2114标志位 正在等待接收
// #define WK2114_G_FLAG_MASK_INTER (1<<1)     //< wk2114标志位 正在处理中断
// #define WK2114_G_FLAG_MASK_SENDING (1<<2)   //< wk2114标志位 正在发送

#define WK2114_G_REG_GIFR_MASK_UT1INT (1<<0)    //< 串口1中断
#define WK2114_G_REG_GIFR_MASK_UT2INT (1<<1)    //< 串口2中断
#define WK2114_G_REG_GIFR_MASK_UT3INT (1<<2)    //< 串口3中断
#define WK2114_G_REG_GIFR_MASK_UT4INT (1<<3)    //< 串口4中断

#define WK2114_S_REG_SIFR_MASK_RFTRIG_INT (1<<0)   //< 接收FIFO触点中断
#define WK2114_S_REG_SIFR_MASK_RXOVT_INT (1<<1)    //< 接收FIFO超时中断
#define WK2114_S_REG_SIFR_MASK_TFTRIG_INT (1<<2)   //< 发送FIFO触点中断
#define WK2114_S_REG_SIFR_MASK_TFEMPTY_INT (1<<3)  //< 发送FIFO空中断
#define WK2114_S_REG_SIFR_MASK_FERR_INT (1<<7)     //< 接收FIFO错误中断

#define WK2114_SUB_UART_MODE_MASK_RX ((uint8_t)1<<0)    //< 子串口模式 接收
#define WK2114_SUB_UART_MODE_MASK_TX ((uint8_t)1<<1)    //< 子串口模式 发送
#define WK2114_SUB_UART_MODE_MASK_SLEEP ((uint8_t)1<<2) //< 子串口模式 允许睡眠

#define WK2114_SUB_UART_FIFO_MODE_MASK_RX ((uint8_t)1<<2)   //< 子串口FIFO模式 接收
#define WK2114_SUB_UART_FIFO_MODE_MASK_TX ((uint8_t)1<<3)   //< 子串口FIFO模式 发送

enum wk31661_cmd{
    WK2114_CMD_WRITE_REG=0x00, //< 写寄存器
    WK2114_CMD_READ_REG=0x01,  //< 读寄存器
    WK2114_CMD_WRITE_FIFO=0x02,//< 写FIFO
    WK2114_CMD_READ_FIFO=0x03  //< 读FIFO
};
enum wk2114_g_reg{
    WK2114_G_REG_GENA=0x00,    //< 全局控制寄存器
    WK2114_G_REG_GRST=0x01,    //< 全局子串口复位寄存器
    WK2114_G_REG_GMUT=0x02,    //< 全局主串口控制寄存器
    WK2114_G_REG_GIER=0x10,    //< 全局中断寄存器
    WK2114_G_REG_GIFR=0x11     //< 全局中断标志寄存器
};
enum wk2114_s_reg{
    WK2114_S_REG_SPAGE=0x03,   //< 子串口页控制寄存器
    WK2114_S_REG_SCR=0x04,     //< PAGE0: 子串口控制寄存器
    WK2114_S_REG_LCR=0x05,     //< PAGE0: 子串口配置寄存器
    WK2114_S_REG_FCR=0x06,     //< PAGE0: 子串口FIFO控制寄存器
    WK2114_S_REG_SIER=0x07,    //< PAGE0: 子串口中断使能寄存器
    WK2114_S_REG_SIFR=0x08,    //< PAGE0: 子串口中断标志寄存器
    WK2114_S_REG_TFCNT=0x09,   //< PAGE0: 子串口发送FIFO计数寄存器
    WK2114_S_REG_RFCNT=0x0a,   //< PAGE0: 子串口接收FIFO计数寄存器
    WK2114_S_REG_FSR=0x0b,     //< PAGE0: 子串口FIFO状态寄存器
    WK2114_S_REG_LSR=0x0c,     //< PAGE0: 子串口接收状态寄存器
    WK2114_S_REG_FDAT=0x0d,    //< PAGE0: 子串口FIFO数据寄存器
    WK2114_S_REG_BAUD1=0x04,   //< PAGE1: 子串口波特率配置寄存器高字节
    WK2114_S_REG_BAUD0=0x05,   //< PAGE1: 子串口波特率配置寄存器低字节
    WK2114_S_REG_PRES=0x06,    //< PAGE1: 子串口波特率配置寄存器小数部分
    WK2114_S_REG_RFTL=0x07,    //< PAGE1: 子串口接收FIFO中断触发点配置寄存器
    WK2114_S_REG_TFTL=0x08     //< PAGE1: 子串口发送FIFO中断触发点配置寄存器
};

enum wk2114_s_uart{
    WK2114_S_UART_1=0x00,      //< 子串口1
    WK2114_S_UART_2=0x01,      //< 子串口2
    WK2114_S_UART_3=0x02,      //< 子串口3
    WK2114_S_UART_4=0x03       //< 子串口4
};

enum wk2114_uart_parity{
    WK2114_UART_PARITY_NONE=0x00,  //< 无校验
    WK2114_UART_PARITY_ZERO=0x08,  //< 强制零校验
    WK2114_UART_PARITY_ODD=0x0a,   //< 奇校验
    WK2114_UART_PARITY_EVEN=0x0c,  //< 偶校验
    WK2114_UART_PARITY_ONE=0x0e    //< 强制一校验
};

enum wk2114_uart_stop_bits{
    WK2114_UART_STOP_BITS_1=0,  //< 1位停止位
    WK2114_UART_STOP_BITS_2=1   //< 2位停止位
};

// WK2xxx 复位IO  高电平，低电平重启
#define WK2XXX_RST_GPIO_PORT  GPIOF
#define WK2XXX_RST_PIN  GPIO_PIN_6
#define WK2XXX_RST_GPIO_CLK_ENABLE() __HAL_RCC_GPIOF_CLK_ENABLE()
// WK2xxx IRQ IO 默认Pulldown   下降沿触发
#define WK2XXX_IRQ_GPIO_PORT  GPIOF
#define WK2XXX_IRQ_PIN  GPIO_PIN_7
#define WK2XXX_IRQ_GPIO_CLK_ENABLE() __HAL_RCC_GPIOF_CLK_ENABLE()
#define WK2XXX_IRQ_EXTI_IRQ  EXTI9_5_IRQn
#define WK2XXX_IRQ_HANDLER  EXTI9_5_IRQHandler

typedef void (*wk2114_sub_uart_recv_fun)(void *recv_class,uint8_t *buf,uint32_t size);  //< 子串口接收数据回调函数
typedef int (*wk2114_send_fun)(void *send_class,uint8_t *buf,uint32_t size);   
         //< wk2114发送函数

/**
 * @brief 发送记录
 * 
 */
struct wk2114_send_record
{
    uint32_t size;
    uint8_t *p_data;
};

typedef struct __wk2114     WK2114_TypeDef;
typedef struct __wk2114_sub_uart     WK2114_Sub_Uart_TypeDef;
typedef struct __wk2114_sub_uart_cfg  WK2114_Sub_Uart_CFG_TypeDef;

struct __wk2114
{
    volatile uint8_t flag;                           //< 标志位
    volatile uint8_t cmd;                            //< 上一次的全局寄存器读命令
    volatile uint8_t reg;                                    //< 上一次接收到的全局寄存器
    volatile uint8_t reg_readout; 

	osMessageQueueId_t cmd_queue;				//< 命令队列 
    // struct queue cmd_queue;                 //< 命令队列
    WK2114_Sub_Uart_TypeDef *sub_uart[4];    //< 子串口
    WK2114_Sub_Uart_CFG_TypeDef *sub_uart_cfg[4];    //< 子串口
    WK2114_Sub_Uart_TypeDef *it_sub_uart ; // 当前中断子串口
    uint8_t it_sub_uart_index;
 
    // uint8_t cmd_queue_msg[2] ;
    uint8_t cmd_queue_msg_put[2] ;
    uint8_t cmd_queue_msg_get[2] ;

    // uint8_t cmd_buf[64][2];                 //< 命令队列缓冲区
    uint8_t send_buf[18];                   //< 发送缓冲区   2+16
    void *send_class;                       //< 发送函数的第一个参数
    uint32_t dev_clock_hz;                  //< wk2114时钟源频率
    wk2114_send_fun send_fun;               //< 发送函数

    uint16_t size_received;             //< 当前接收到的字节数

    // uint8_t it_type;
    uint64_t timebase;
    uint32_t timeout_ms;
    volatile WK2114_WORK_STATE_TypeDef state;
    // uint8_t setbaud_ok;
    // uint8_t senf40_rcvF0_ok;
    volatile uint8_t idle_receive_flag;
    uint8_t reg_gifr;  // 全局中断寄存器
}  ;

struct   __wk2114_sub_uart
{
    volatile uint8_t flag;                               //< 标志位
    volatile uint8_t cmd;                                //< 上一次的子串口寄存器读命令
    uint8_t reg;                                //< 上一次接收到的子串口寄存器
    uint8_t uart_index;                         //< 串口地址

    void *recv_class;                           //< 接收回调函数的第一个参数
    wk2114_sub_uart_recv_fun recv_fun;          //< 接收回调函数
	osMessageQueueId_t send_queue;				//< 发送队列 ?? 子串口待发送出去的？还是通过主串口发回来的
    // struct queue send_queue;                    //< 发送队列
    uint8_t *recv_buf;                          //< 接收缓冲区
    uint8_t *send_buf;                          //< 当前发送数据指针
    uint32_t recv_buf_p;                        //< 接收缓冲区计数
    uint32_t recv_buf_size;                     //< 接收缓冲区大小
    uint32_t send_buf_p;                        //< 发送缓冲区计数
    WK2114_TypeDef *wk2114;                      //< wk2114结构指针
    struct wk2114_send_record send_record[8];   //< 发送记录 8*16 = 256
    uint8_t send_record_p;

    // uint8_t s_reg_sifr_read;  // 子串口中断源
    // uint8_t s_reg_lsr;  // 子串口中断源
    // uint8_t s_reg_lcr;  // 子串口中断源
} ;

struct   __wk2114_sub_uart_cfg
{
    //  baud reg ,  baud_div =(dev_clock_hz*100/16/baud )/100  - 1
    uint8_t baud1_reg;   // baud >>8
    uint8_t baud0_reg;   // baud &0xFF
    uint8_t pres_reg;    // baud 小数部分   ((dev_clock_hz*100/16/baud)/10)%10
    
    //  lcr reg , parity  stopbit , party|stop_bits
    uint8_t  lcr_reg;    // party|stop_bits
	uint8_t  fwcr_reg;  // WK2114_SUB_UART_FIFO_MODE_MASK_TX|WK2114_SUB_UART_FIFO_MODE_MASK_RX
    uint8_t  scr_reg;    // 04 03   mode  WK2114_SUB_UART_MODE_MASK_TX|WK2114_SUB_UART_MODE_MASK_RX

    uint8_t tftl_reg;   // Tx fifo Trigger
    uint8_t rftl_reg;   // Rx fifo Trigger
    uint8_t iser_reg;   // 中断类型设置

    uint8_t reg_sifr;  // 子串口中断源
    uint8_t reg_fsr;  // 子串口FIFO状态
    uint8_t reg_lsr;  // 子串口接收状态寄存器
    uint8_t reg_fdat;  // 子串口 FIFO 数据寄存器 , 需要读写状态
    // uint8_t line;
    // uint8_t new_lcr_reg;
	// uint8_t new_fwcr_reg;
    // uint8_t new_scr_reg; 
    // /*baud register*/
    // uint8_t new_baud1_reg;
    // uint8_t new_baud0_reg;
    // uint8_t new_pres_reg;
} ;

extern WK2114_TypeDef *wk2114;
extern WK2114_Sub_Uart_TypeDef  *sub_uart1 ;
extern uint8_t sub_uart1_receive_buf[1024];


int WK2114_Init( );
void WK2114_Port();
void WK2114_Task(void * argument );

int Sub_rcv_callback(void * obj, uint8_t * buf, uint16_t len);
int WK2114_Add_Sub_Uart(WK2114_TypeDef *wk2114  );


int wk2114_send(WK2114_TypeDef *wk2114);

int wk2114_read_global_reg(WK2114_TypeDef *wk2114 , uint8_t reg_addr);
int wk2114_write_global_reg(WK2114_TypeDef *wk2114,uint8_t reg_addr,uint8_t reg);
int wk2114_read_subuart_reg(WK2114_TypeDef *wk2114 ,enum wk2114_s_uart uart_addr, uint8_t reg_addr);
int wk2114_write_subuart_reg(WK2114_TypeDef *wk2114,enum wk2114_s_uart uart_addr, uint8_t reg_addr,uint8_t reg);
int wk2114_read_fifo(WK2114_TypeDef *wk2114,uint8_t sub_uart,uint8_t size);
int wk2114_write_fifo(WK2114_TypeDef *wk2114,uint8_t sub_uart,uint8_t *buf,uint8_t size);

int wk2114_sub_uart_set_page(WK2114_Sub_Uart_TypeDef*sub_uart,uint8_t page);
int wk2114_flush_gena(WK2114_TypeDef *wk2114);
int wk2114_flush_gier(WK2114_TypeDef *wk2114);
int wk2114_sub_uart_set_inter(WK2114_Sub_Uart_TypeDef*sub_uart,uint8_t inter_flag);
int wk2114_sub_uart_set_fifo_trigger(WK2114_Sub_Uart_TypeDef*sub_uart,uint8_t tx_trigger,uint8_t rx_trigger);
void wk2114_reg_recv_callback(WK2114_TypeDef *wk2114);

int wk2114_reset(WK2114_TypeDef *wk2114);
int wk2114_set_baud( WK2114_TypeDef *wk2114 );
int wk2114_test(WK2114_TypeDef *wk2114);
int wk2114_mainport_rcv(WK2114_TypeDef *wk2114, uint8_t *buf, uint16_t size);

int wk2114_set_uart(WK2114_TypeDef *wk2114,enum wk2114_uart_parity party,enum wk2114_uart_stop_bits stop_bits);
int wk2114_set_sub_uart(WK2114_Sub_Uart_TypeDef*sub_uart,uint32_t baud,enum wk2114_uart_parity party,enum wk2114_uart_stop_bits stop_bits);
int wk2114_sub_uart_set_mode(WK2114_Sub_Uart_TypeDef*sub_uart,uint8_t mode);
int wk2114_sub_uart_set_fifo_mode(WK2114_Sub_Uart_TypeDef*sub_uart,uint8_t mode);
int wk2114_sub_uart_enable(WK2114_Sub_Uart_TypeDef*sub_uart);
int wk2114_sub_uart_disable(WK2114_Sub_Uart_TypeDef*sub_uart);
int wk2114_sub_uart_reset(WK2114_Sub_Uart_TypeDef*sub_uart);

// WK2114_TypeDef * wk2114_init(WK2114_TypeDef *wk2114,uint32_t dev_clock_hz,void *send_class,wk2114_send_fun send_fun);
int wk2114_sub_uart_register(WK2114_TypeDef *wk2114,WK2114_Sub_Uart_TypeDef*sub_uart,enum wk2114_s_uart uart_addr,uint8_t *recv_buf,uint32_t recv_buf_size,void *recv_class,wk2114_sub_uart_recv_fun recv_fun);
int wk2114_sub_uart_init(WK2114_Sub_Uart_TypeDef*sub_uart,uint32_t baud,uint8_t mode,uint8_t fifo_mode,enum wk2114_uart_parity party,enum wk2114_uart_stop_bits stop_bits);
int wk2114_sub_uart_chenge_recv_fun(WK2114_Sub_Uart_TypeDef*sub_uart,void *recv_class,wk2114_sub_uart_recv_fun recv_fun);
int wk2114_sub_uart_send(WK2114_Sub_Uart_TypeDef*sub_uart,uint8_t *buf,uint32_t size);
int wk2114_sub_uart_isbusy(WK2114_Sub_Uart_TypeDef*sub_uart);
void wk2114_recv_callback(WK2114_TypeDef *wk2114,uint8_t *buf,uint32_t size);

void wk2114_inter_callback(WK2114_TypeDef *wk2114);
// void wk2114_exti_callback(WK2114_TypeDef *wk2114);
void wk2114_send_completed_callback(WK2114_TypeDef *wk2114);


void wk2114_idle_callback(WK2114_TypeDef *wk2114,uint8_t *buf,uint32_t size);
void wk2114_get_it_type_by_it_source(WK2114_TypeDef *wk2114);
void wk2114_process_by_it_type(WK2114_TypeDef *wk2114,  uint8_t it_type);
#endif


/*

#define WK2114_RST_Pin GPIO_PIN_6
#define WK2114_RST_GPIO_Port GPIOF
#define WK2114_IRQ_Pin GPIO_PIN_7
#define WK2114_IRQ_GPIO_Port GPIOF

__HAL_RCC_GPIOF_CLK_ENABLE();
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  GPIO_InitStruct.Pin = WK2114_RST_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);

// 以下可以启动子串口的产生的 接收外部中断
  GPIO_InitStruct.Pin = WK2114_IRQ_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  HAL_GPIO_Init(WK2114_IRQ_GPIO_Port, &GPIO_InitStruct);

  // EXTI interrupt init
  HAL_NVIC_SetPriority(EXTI9_5_IRQn, 5, 0);
  HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);

void EXTI9_5_IRQHandler(void)
{
 
  HAL_GPIO_EXTI_IRQHandler(WK2114_IRQ_Pin);
 
}

上次是否发送完成启用信号量控制，在 rtos中
*/


/*
命令说明
 7 6 5 4 3 2 1 0 

 [7]   0  reg读写  ; 1 FIFO读写 
 [6]   0  写    1 读
 [5:4]   字串口号  全局寄存器为 00 
 [3:0]   寄存器模式 寄存器地址， FIFO模式 为读写字节数

 0x55 return 0x00
 0x40 return 0xF0


初始化，   0x55设置子窗口波特率， 注册子串口， 子串口初始化(初始要有时间延迟)
如何确认初始成功？

注册初始化串口 1 , 命令参考函数的注释
write  reg  0x00  0x01  
write  reg  0x01  0x01  
write  reg  0x10  0x01  
write  reg  0x03  0x01  
write  reg  0x04  0x00  
write  reg  0x05  0x4d(9600)  0505(115200)
write  reg  0x06  0x01   0605
write  reg  0x03  0x00  
write  reg  0x05  0x00  
write  reg  0x03  0x01  
write  reg  0x08  0x80  
write  reg  0x07  0x80  
write  reg  0x03  0x00  
write  reg  0x07  0x83  
write  reg  0x06  0x0c  
write  reg  0x04  0x03 

//WK2xxx 初始化
if(0==WK2114_Init())
{
    log_i("wk2114 Init Ok...");
}else{
    log_i("wk2114 Init Failure...");
}
// 启动任务
WK2114_Port();
// 状态机初始化
wk2114->state= WK2114_WORK_STATE_START;


// 发送 ， 调用wk2114_sub_uart_send函数， 装载子串口队列及buf
// wk2114_sub_uart_send启用子串口中断， 发送触点为空中断，调用wk2114_write_fifo 进行发送
// 注意：  发送fifo 256, 发送触点中断设置为128超过128 自动启动发送触点中断
wk2114_sub_uart_send(wk2114->sub_uart[0],send_buf,8);

8.3.1 FIFO数据错误中断 
FIFO数据错误中断表明当前接收FIFO中有一个或以上的数据错误，产生错误的条件包括OE
（数据溢出错误），FE（数据帧错误），和PE（奇偶校验错），BE（Line-Break错误）。
一旦有接收FIFO中有出错数据,当读取FSR寄存器后，清除相应的中断。
8.3.2 发送FIFO空中断 -- 
当发送FIFO中没有数据，产生该中断。
当发送FIFO中的数据个数大于设定的发送FIFO触发点时，该中断被清除。
8.3.3 发送FIFO触发点中断 
当发送FIFO中的数据个数小于设定的发送FIFO触发点时，产生该中断。
当发送FIFO中的数据个数大于设定的发送FIFO触发点时，该中断被清除。
8.3.4接收FIFO超时中断 
当接收FIFO中数据个数小于设定的接收FIFO触发点并且RX引脚4个字节之内没有数据，产生该中断。
当接收FIFO中的数据被读走或者RX继续接收数据时，该中断消失。
8.3.5 接收FIFO触发点中断
当接收FIFO中的数据个数大于设定的发送FIFO触发点时，产生该中断。
当接收FIFO中的数据个数小于设定的发送FIFO触发点时，该中断被清除。


wk2114_read_reg( wk2114, 0x00 ); 自动加上 [7:6] 0 1

    HAL_UART_DMAStop(pWK2114Uart);
    HAL_UARTEx_ReceiveToIdle_DMA( pWK2114Uart, rcvbuf, 1024);
    wk2114->cmd =0;
    wk2114_read_reg( wk2114, 0x00 );


*/