/**
 * @file wk2114.c
 * @author Chen Jihang (embedded@eseaoptics.com)
 * @brief WK2114串口拓展驱动
 * @version 1.0
 * @date 2023-01-04
 * 
 * @copyright ESEA (c) 2020
 * 
 */
#include <string.h>
#include <stdlib.h>
#include "wk2114.h"
// #include "critical_section.h"
#include "usart.h"
#include "elog.h"


extern osSemaphoreId_t wk2xxx_lockHandle;
extern osSemaphoreId_t wk2xxx_irq_lockHandle;
extern osSemaphoreId_t wk2xxx_sub_lockHandle;
extern osEventFlagsId_t wk2114EventHandle;
extern osThreadId_t wk2114Handle;
static uint8_t  init_buf=0x55;

// uint8_t  buf[1024]={0};        

WK2114_TypeDef *wk2114;
static uint8_t mybuf[8] ={0x01,0x00,0x00,0x00,0x01,0x00,0x01,0x01};
uint8_t wk2xxx_rcvbuf[1024] ={ 0};



/**
 * @brief 内部函数 检查发送队列中的指令并发送到串口
 * @note 请确保调用时上次发送的数据已经发送完毕
 * @param wk2114 struct wk2114结构指针
 * @return int >=0:成功 <0:失败
 * @desp 记录cmd 值，以面操作子串口和主串口混淆
 */
// int wk2114_send(WK2114_TypeDef *wk2114,uint8_t *buf, uint16_t size)
// {
//     int st;
//     osSemaphoreAcquire(wk2xxx_lockHandle, osWaitForever);
//     st = wk2114->send_fun(wk2114->send_class,buf,size+1);
//     osSemaphoreRelease(wk2xxx_lockHandle);
//     return st;
// }

int wk2114_mainport_rcv(WK2114_TypeDef *wk2114, uint8_t *buf, uint16_t size)
{
    memset(buf, 0, size);
    wk2114->idle_receive_flag = 0;
    __HAL_UART_ENABLE_IT(pWK2114Uart, UART_IT_IDLE);
    HAL_UART_Receive_DMA(pWK2114Uart, buf, size);
    return 0;
}

/**
 * @brief 内部函数 发送读寄存器指令
 * 
 * @param wk2114 struct wk2114结构指针
 * @param reg_addr 寄存器地址
 * @return int 0:成功 负数:失败
 * @note  函数自己加上 [7:6] 这两位
 */
int wk2114_read_global_reg(WK2114_TypeDef *wk2114,  uint8_t reg_addr)
{
    int ret;
    // while(  (wk2114->flag&WK2114_G_FLAG_MASK_SENDING)!=0 );
    osSemaphoreAcquire(wk2xxx_lockHandle, osWaitForever);
    wk2114->send_buf[0] =  (WK2114_CMD_READ_REG<<6)+reg_addr ;
    wk2114->send_buf[1] =  0x00;
    // wk2114->flag|=WK2114_G_FLAG_MASK_SENDING;
    wk2114->timebase-osKernelGetTickCount();
    ret = wk2114->send_fun( wk2114->send_class, wk2114->send_buf, 1 );
    // wk2114->idle_receive_flag = 0;
    ret = wk2114_mainport_rcv( wk2114,wk2xxx_rcvbuf,1024);
    // ret = HAL_UART_Receive(pWK2114Uart,  &wk2114->reg_readout,  1  , 0xFFF);
    // osSemaphoreRelease(wk2xxx_lockHandle);
    // while (wk2114->idle_receive_flag!=1)
    // {
    //     if ( wk2114->timebase-osKernelGetTickCount() >300)
    //     {
    //         return -1; 
    //     }
    // } 
    // HAL_UART_DMAStop(pWK2114Uart);
    // log_w( "wk2114_read_global_reg  ->  0x%02X  ret %d" ,  wk2114->send_buf[0]  ,ret);
    // osDelay(100);
    return ret ;

}

/**
 * @brief 内部函数 发送写寄存器指令
 * 
 * @param wk2114 struct wk2114结构指针
 * @param reg_addr 寄存器地址
 * @param reg 要写入的值
 * @return int 0:成功 负数:失败
 * @note  队列采用值传递，没有buf, 一般无需确认在buf中的位置(数据值的数据量少)
 */
int wk2114_write_global_reg(WK2114_TypeDef *wk2114,uint8_t reg_addr,uint8_t reg)
{
    int ret;
    // while(  (wk2114->flag&WK2114_G_FLAG_MASK_SENDING)!=0 );
    osSemaphoreAcquire(wk2xxx_lockHandle, osWaitForever);
    wk2114->send_buf[0] =  (WK2114_CMD_WRITE_REG<<6)|reg_addr ;
    wk2114->send_buf[1] =  reg;
 
    // wk2114->flag|=WK2114_G_FLAG_MASK_SENDING;
    ret = wk2114->send_fun( wk2114->send_class, wk2114->send_buf, 2 );
    // osSemaphoreRelease(wk2xxx_lockHandle);
    // log_w( "wk2114_write_global_reg -> 0x%02X 0x%02X    " ,  wk2114->send_buf[0],  wk2114->send_buf[1] );
    return ret ;
 
}

int wk2114_read_subuart_reg(WK2114_TypeDef *wk2114 ,enum wk2114_s_uart uart_addr,uint8_t reg_addr)
{
    int ret;
    // while(  (wk2114->flag&WK2114_G_FLAG_MASK_SENDING)!=0 );
    osSemaphoreAcquire(wk2xxx_lockHandle, osWaitForever);
    wk2114->send_buf[0] =  (WK2114_CMD_READ_REG<<6)|(uart_addr<<4)|reg_addr ;
    wk2114->send_buf[1] =  0x00;
    // wk2114->flag|=WK2114_G_FLAG_MASK_SENDING;
    // wk2114->idle_receive_flag = 0;

    wk2114->timebase-osKernelGetTickCount();
    ret = wk2114->send_fun( wk2114->send_class, wk2114->send_buf, 1 );
    ret = wk2114_mainport_rcv( wk2114,wk2xxx_rcvbuf,1024);
    // while (wk2114->idle_receive_flag!=1)
    // {
    //     if ( wk2114->timebase-osKernelGetTickCount() >300)
    //     {
    //         // log_w( "wk2114_read_subuart_reg -> 0x%02X 0x%02X error   " ,  uart_addr,  reg_addr );
    //         return -1; 
    //     }
    // } 
    // HAL_UART_DMAStop(pWK2114Uart);
    // osSemaphoreRelease( wk2xxx_lockHandle );

    // *readout = wk2xxx_rcvbuf[0];
    // log_d( "wk2114_read_subuart_reg   ->  0x%02X    ret %d   0x%02X" ,  wk2114->send_buf[0] ,  ret, wk2xxx_rcvbuf[0]);
    // osDelay(200);
    return ret ;

}


int wk2114_write_subuart_reg(WK2114_TypeDef *wk2114,enum wk2114_s_uart uart_addr,uint8_t reg_addr,uint8_t reg)
{
    int ret;

    // while(  (wk2114->flag&WK2114_G_FLAG_MASK_SENDING)!=0 );
    osSemaphoreAcquire(wk2xxx_lockHandle, osWaitForever);
    wk2114->send_buf[0]=  (WK2114_CMD_WRITE_REG<<6)+ (uart_addr<<4) +reg_addr ;
    wk2114->send_buf[1]=  reg;
    // if(  reg_addr ==WK2114_S_REG_SPAGE ){  
    //     if(reg==0){
    //         wk2114->sub_uart[uart_addr]->flag&=~WK2114_S_UART_FLAG_MASK_PAGE_D;
    //     }
    //     else{
    //         wk2114->sub_uart[uart_addr]->flag|=WK2114_S_UART_FLAG_MASK_PAGE_D;
    //     }
    // }
    // wk2114->flag|=WK2114_G_FLAG_MASK_SENDING;
    ret = wk2114->send_fun( wk2114->send_class, wk2114->send_buf, 2 );
    // osSemaphoreRelease(wk2xxx_lockHandle);
    // while(  (wk2114->flag&WK2114_G_FLAG_MASK_SENDING)!=0 );
 
    // log_w( "wk2114_write_subuart_reg -> 0x%02X 0x%02X   ret: %d " ,  wk2114->send_buf[0],  wk2114->send_buf[1] , ret);
    return ret ;
 
}

/**
 * @brief 内部函数 发送读取FIFO的命令
 * 
 * @param wk2114 struct wk2114结构指针
 * @param sub_uart 要读取FIFO的子串口地址
 * @param size 要读取的长度-1
 * @return int 0:成功 负数:失败
 */
int wk2114_read_fifo(WK2114_TypeDef *wk2114,uint8_t sub_uart,uint8_t size)
{
    int ret;
    if(wk2114->sub_uart[sub_uart]==NULL){
        return -1;
    }
    while(  (wk2114->flag&WK2114_G_FLAG_MASK_SENDING)!=0 );
    osSemaphoreAcquire(wk2xxx_lockHandle, osWaitForever);
    wk2114->send_buf[0]=((WK2114_CMD_READ_FIFO<<6)+(sub_uart<<4)) + size;
    // wk2114->flag|=WK2114_G_FLAG_MASK_SENDING;
    wk2114_mainport_rcv( wk2114,wk2xxx_rcvbuf,1024);
    ret = wk2114->send_fun( wk2114->send_class, wk2114->send_buf, 1);
    wk2114->flag|=WK2114_G_FLAG_MASK_SENDING;
    // osSemaphoreRelease(wk2xxx_lockHandle);
 
    return 0;
}
/**
 * @brief 内部函数 发送写入FIFO的命令
 * 
 * @param wk2114 struct wk2114结构指针
 * @param sub_uart 子串口地址
 * @param buf 要发送的数据缓冲区, 最大1+16， cmd_1+buf_16
 * @param size 要发送的长度-1
 * @return int 0:成功 负数:失败
 */
int wk2114_write_fifo(WK2114_TypeDef *wk2114,uint8_t sub_uart,uint8_t *buf,uint8_t size)
{
    int ret;
    if(wk2114->sub_uart[sub_uart]==NULL){
        return -1;
    }
    if(wk2114->sub_uart[sub_uart]->send_buf!=NULL){
        return -2;
    }
    while(  (wk2114->flag&WK2114_G_FLAG_MASK_SENDING)!=0 );
    osSemaphoreAcquire(wk2xxx_lockHandle, osWaitForever);

    //  send_buf[0] = writeFifo + subuart +size
    wk2114->send_buf[0]    =  (WK2114_CMD_WRITE_FIFO<<6)+(sub_uart<<4) + size;
    memcpy( &wk2114->send_buf[1], buf, size+1 );

    wk2114->flag|=WK2114_G_FLAG_MASK_SENDING;
    ret = wk2114->send_fun( wk2114->send_class, wk2114->send_buf, size+2);

    // osSemaphoreRelease(wk2xxx_lockHandle);
    while(  (wk2114->flag&WK2114_G_FLAG_MASK_SENDING)!=0 );
    return 0;
}

int wk2114_reset(WK2114_TypeDef *wk2114)
{
    HAL_GPIO_WritePin(WK2XXX_IRQ_GPIO_PORT, WK2XXX_RST_PIN,GPIO_PIN_RESET);
    osDelay(1);
    HAL_GPIO_WritePin(WK2XXX_IRQ_GPIO_PORT, WK2XXX_RST_PIN,GPIO_PIN_SET);
    osDelay(1);
    return 0;
}


/**
 * @brief 设置主串口波特率
 * @note 此函数会被 \ref wk2114_init() 函数间接调用,一般不需要手动调用
 * @note 调用此函数前请对wk2114执行硬件复位
 * @param wk2114 struct wk2114结构指针
 * @return int >=0:成功 <0:失败
 */
int wk2114_set_baud(WK2114_TypeDef *wk2114)
{
    wk2114->flag|=WK2114_G_FLAG_MASK_SENDING;
    return wk2114->send_fun(wk2114->send_class,&init_buf,1);
}

int wk2114_test(WK2114_TypeDef *wk2114)
{
    uint8_t writein  =  0x40 ;  
    wk2114->flag|=WK2114_G_FLAG_MASK_SENDING;
    return wk2114->send_fun(wk2114->send_class,&writein,1);
}



/**
 * @brief 内部函数 设置子串口寄存器页
 * 
 * @param sub_uart 子串口结构
 * @param page 页 0/1
 * @return int 0:成功 负数:失败
 */
int wk2114_sub_uart_set_page(WK2114_Sub_Uart_TypeDef*sub_uart,uint8_t page)
{
    int ret;
    if(page>1){
        return -1;
    }
    if(((sub_uart->flag&WK2114_S_UART_FLAG_MASK_PAGE)!=0)==page){
        return 0;
    }
    ret=wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index,WK2114_S_REG_SPAGE ,page);
    if(ret<0){
        return ret;
    }
    if(page==1){
        sub_uart->flag|=WK2114_S_UART_FLAG_MASK_PAGE;
    }
    else{
        sub_uart->flag&=~WK2114_S_UART_FLAG_MASK_PAGE;
    }
    return 0;
}
/**
 * @brief 内部函数 根据已经注册的子串口刷新子串口时钟使能
 * 
 * @param wk2114 struct wk2114结构指针
 * @return int 成功:0 失败:负值
 */
int wk2114_flush_gena(WK2114_TypeDef *wk2114)
{
    uint8_t reg;
    int i;
    reg=0;
    for(i=0;i<1;i++){
        if( wk2114->sub_uart[i]!=NULL  )
        {
            if ( (wk2114->sub_uart[i]->flag&WK2114_S_UART_FLAG_MASK_ENABLE)!=0) 
            {
                reg|=(1<<i);
            }
        }
    }
    return wk2114_write_global_reg(wk2114,WK2114_G_REG_GENA,reg);
}

/**
 * @brief 内部函数 根据已经注册的子串口刷新子串口中断使能
 * 
 * @param wk2114 struct wk2114结构指针
 * @return int 成功:0 失败:负值
 */
int wk2114_flush_gier(WK2114_TypeDef *wk2114)
{
    uint8_t reg;
    int i;
    reg=0;
    for(i=0;i<4;i++){
        if(wk2114->sub_uart[i]!=NULL){
            reg|=(1<<i);
        }
    }
    return wk2114_write_global_reg(wk2114,WK2114_G_REG_GIER,reg);
}
/**
 * @brief 设置主串口参数
 * @note 此函数会被 \ref wk2114_init() 间接调用 一般不需要手动调用
 * @param wk2114 struct wk2114结构指针
 * @param party 校验
 * @param stop_bits 停止位
 * @return int 成功:0 失败:负值
 */
int wk2114_set_uart(WK2114_TypeDef *wk2114,enum wk2114_uart_parity party,enum wk2114_uart_stop_bits stop_bits)
{
    return(wk2114_write_global_reg(wk2114,WK2114_G_REG_GMUT,party|stop_bits));
}

/**
 * @brief 设置子串口参数
 * @note 此函数会被 \ref wk2114_sub_uart_init() 间接调用,一般不需要手动调用
 * @param sub_uart 子串口结构
 * @param baud 波特率
 * @param party 校验
 * @param stop_bits 停止位
 * @return int 成功:0 失败:负值
 */
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)
{
    // log_i( "..... wk2114_set_sub_uart .. fun ..    " );

    uint32_t tmp;
    uint16_t baud_div;
    tmp=sub_uart->wk2114->dev_clock_hz*100/16/baud;
    if(tmp<10){
        return -1;
    }
    baud_div=tmp/100-1;
    if(wk2114_sub_uart_set_page(sub_uart,1)<0){  // 03 01
        return -2;
    }
    if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index, WK2114_S_REG_BAUD1 ,baud_div>>8)<0){ // 04 00
        return -2;
    }
    if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index,WK2114_S_REG_BAUD0 ,baud_div&0xff)<0){ //05 05
        return -2;
    }
    if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index, WK2114_S_REG_PRES ,(tmp/10)%10)<0){  // 06 05
        return -2;
    }
    if(wk2114_sub_uart_set_page(sub_uart,0)<0){  // 03 00
        return -2;
    }
    if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index, WK2114_S_REG_LCR ,party|stop_bits)<0){ // 0500
        return -2;
    }

    return 0;
}
/**
 * @brief 内部函数 设置子串口中断使能
 * 
 * @param sub_uart 子串口结构
 * @param inter_flag 宏定义 WK2114_S_REG_SIFR_MASK_XXX 的任意结合
 * @return int 成功:0 失败:负值
 */
int wk2114_sub_uart_set_inter(WK2114_Sub_Uart_TypeDef*sub_uart,uint8_t inter_flag)
{
    if( wk2114_sub_uart_set_page(sub_uart,0)<0 ){
        return -2;
    }   
    if( wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index, WK2114_S_REG_SIER ,inter_flag)<0 ){
        // log_i("wk2114_sub_uart_set_inter  write")
        return -2;
    }
    return 0;
}
/**
 * @brief 设置子串口的模式
 * @note 此函数会被 \ref wk2114_sub_uart_init() 间接调用 一般不需要手动调用
 * @param sub_uart 子串口结构
 * @param mode 模式 WK2114_SUB_UART_MODE_MASK_XXX 的任意结合
 * @return int 成功:0 失败:负值
 */
int wk2114_sub_uart_set_mode(WK2114_Sub_Uart_TypeDef*sub_uart,uint8_t mode)
{
    if(wk2114_sub_uart_set_page(sub_uart,0)<0){
        return -2;
    }
    if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index, WK2114_S_REG_SCR, mode)<0){
        return -2;
    }
    return 0;
}
/**
 * @brief 内部函数 设置子串口FIFO触点
 * 
 * @param sub_uart 子串口结构
 * @param tx_trigger 发送FIFO触点
 * @param rx_trigger 接收FIFO触点
 * @return int 成功:0 失败:负值
 */
int wk2114_sub_uart_set_fifo_trigger(WK2114_Sub_Uart_TypeDef*sub_uart,uint8_t tx_trigger,uint8_t rx_trigger)
{
    if(wk2114_sub_uart_set_page(sub_uart,1)<0){
        return -2;
    }
    if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index, WK2114_S_REG_TFTL ,tx_trigger)<0){
        return -2;
    }
    if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index, WK2114_S_REG_RFTL,rx_trigger)<0){
        return -2;
    }
    return 0;
}
/**
 * @brief 设置子串口FIFO模式
 * @note 此函数被 \ref wk2114_sub_uart_init() 间接调用 一般不需要手动调用
 * @param sub_uart 子串口结构
 * @param mode 模式 WK2114_SUB_UART_FIFO_MODE_MASK_XXX 的任意结合
 * @return int 
 */
int wk2114_sub_uart_set_fifo_mode(WK2114_Sub_Uart_TypeDef*sub_uart,uint8_t mode)
{
    if(wk2114_sub_uart_set_page(sub_uart,0)<0){
        return -2;
    }
    if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index,WK2114_S_REG_FCR ,mode)<0){
        return -2;
    }
    return 0;
}
/**
 * @brief 开启子串口时钟
 * 
 * @param sub_uart 子串口
 * @return int 成功:0 失败:负值
 */
int wk2114_sub_uart_enable(WK2114_Sub_Uart_TypeDef*sub_uart)
{   
    sub_uart->flag|=WK2114_S_UART_FLAG_MASK_ENABLE;
    return wk2114_flush_gena(sub_uart->wk2114);
}
/**
 * @brief 关闭子串口时钟
 * 
 * @param sub_uart 子串口结构
 * @return int 成功;0 失败:负值
 */
int wk2114_sub_uart_disable(WK2114_Sub_Uart_TypeDef*sub_uart)
{
    sub_uart->flag&=~WK2114_S_UART_FLAG_MASK_ENABLE;
    return wk2114_flush_gena(sub_uart->wk2114);
}
/**
 * @brief 重置子串口
 * 
 * @param sub_uart 子串口结构
 * @return int 成功:0 失败:负值
 */
int wk2114_sub_uart_reset(WK2114_Sub_Uart_TypeDef*sub_uart)
{
    return wk2114_write_global_reg(sub_uart->wk2114,WK2114_G_REG_GRST,1<<sub_uart->uart_index);
}
/**
 * @brief 通过子串口向外发送数据
 * @note buf参数指向的缓冲区在发送完成之前不能改变
 * @param sub_uart 子串口结构
 * @param buf 要发送的数据缓冲区 ， buf无需限制在FIFO的16bytes
 * @param size 要发送的长度
 * @return int 成功:0 失败:负值
 */
int wk2114_sub_uart_send( WK2114_Sub_Uart_TypeDef*sub_uart, uint8_t *buf, uint32_t size )
{
    //  TODO 发送结束后 会出现不能启动下次发送外部中断, 复位串口？？
    log_i("wk2114_sub_uart_send  >> state %d ", wk2114->state);
    int count;
    int st;
    uint8_t reg;
    reg = WK2114_S_REG_SIFR_MASK_RFTRIG_INT|WK2114_S_REG_SIFR_MASK_RXOVT_INT|
            WK2114_S_REG_SIFR_MASK_TFTRIG_INT|WK2114_S_REG_SIFR_MASK_TFEMPTY_INT|WK2114_S_REG_SIFR_MASK_FERR_INT;
 
    osSemaphoreAcquire(wk2xxx_sub_lockHandle, osWaitForever);
    count   = osMessageQueueGetCount( sub_uart->send_queue );
    sub_uart->send_record[count].p_data  =   buf;
    sub_uart->send_record[count].size    =   size;
    osMessageQueuePut( sub_uart->send_queue, (void*) &sub_uart->send_record_p, 0, portMAX_DELAY ); // 指针
    sub_uart->send_record_p   =   (sub_uart->send_record_p+1)%8;
    osSemaphoreRelease(wk2xxx_sub_lockHandle);

    // 启用子串口中断， 发送为空中断触发子串口队列发送  
    // TODO  启动中断，可能不会产生外部中断卡死， 可以直接启动发送队列
    
    if( count!=0 ) return 0;
    
    st =  wk2114_write_subuart_reg(sub_uart->wk2114,  sub_uart->uart_index,  WK2114_S_REG_SIER , reg);
    // while(  (wk2114->flag&WK2114_G_FLAG_MASK_SENDING)!=0 );
    osSemaphoreAcquire(wk2xxx_lockHandle, osWaitForever);

    osSemaphoreRelease(wk2xxx_lockHandle);
    return st;
 
}
/**
 * @brief 判断子串口是否忙，判断子串口队列子串口buff
 * 
 * @param sub_uart 子串口结构
 * @return int -1:出错 0:不忙 1:忙
 */
int wk2114_sub_uart_isbusy(WK2114_Sub_Uart_TypeDef*sub_uart)
{
    if(sub_uart==NULL){
        return -1;
    }
    if (osMessageQueueGetCount(wk2114EventHandle) >0 ){
        return 1;
    }
    if (sub_uart->send_buf!=NULL) {
        return 1;
    }
    return 0;
}

/**
 * @brief 注册子串口
 * 
 * @param wk2114 WK2114_TypeDef 结构指针
 * @param sub_uart 子串口结构
 * @param uart_addr 子串口地址
 * @param recv_buf 接收缓冲区
 * @param recv_buf_size 接收缓冲区大小 >=16
 * @param recv_class 接收回调函数的第一个参数
 * @param recv_fun 接收回调函数
 * @return int 成功:0 失败:负值
 */
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)
{
    // log_i( ".....wk2114_sub_uart_register..   addr %d    ",uart_addr  );
    if(wk2114==NULL || sub_uart==NULL || recv_class==NULL || recv_fun==NULL || uart_addr<0 || uart_addr>=4 || recv_buf==NULL || recv_buf_size<16){
        return -1;
    }
    // TODO  指针传递
    sub_uart->send_queue = osMessageQueueNew(8, sizeof(struct wk2114_send_record *) ,NULL);
    if ( sub_uart->send_queue ==NULL )  return -2;
    // if(queue_init(&sub_uart->send_queue,NULL,8)==NULL){
    //     return -2;
    // }

    if(wk2114->sub_uart[uart_addr]!=NULL){
        return -3;
    }
 
    sub_uart->recv_class=recv_class;
    sub_uart->recv_fun=recv_fun;
    sub_uart->recv_buf=recv_buf;
    sub_uart->recv_buf_size=recv_buf_size;
    sub_uart->recv_buf_p=0;
    sub_uart->wk2114=wk2114;
    sub_uart->flag=0;
    sub_uart->cmd=0;
    sub_uart->reg=0;
    sub_uart->uart_index=uart_addr;
    sub_uart->send_buf_p=0;  // ?? 0 or 0xffffffff
    sub_uart->send_buf=NULL;
    sub_uart->send_record_p=0;
    wk2114->sub_uart[uart_addr]=sub_uart;
 
    return 0;
}

/**
 * @brief 子串口初始化
 * 
 * @param sub_uart 子串口结构
 * @param baud 波特率
 * @param mode 模式 WK2114_SUB_UART_MODE_MASK_XXX 的任意结合
 * @param fifo_mode FIFO模式 WK2114_SUB_UART_FIFO_MODE_MASK_XXX 的任意结合
 * @param party 校验
 * @param stop_bits 停止位
 * @return int 成功:0 失败:负值
 */
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)
{
    uint32_t tmp;
    uint16_t baud_div;
    uint8_t reg = 0;
    tmp=sub_uart->wk2114->dev_clock_hz*100/16/baud;
    if(tmp<10){
        return -1;
    }
    baud_div=tmp/100-1;
    for(int i=0;i<4;i++){
        if(wk2114->sub_uart[i]!=NULL){
            reg|=(1<<i);
        }
    }
    //  子串口使能  00 01
    sub_uart->flag|=WK2114_S_UART_FLAG_MASK_ENABLE;
    wk2114_write_global_reg(wk2114,WK2114_G_REG_GENA,reg);
    HAL_Delay(0)  ;
 
    //  子串口复位寄存器 01 01
    wk2114_write_global_reg(sub_uart->wk2114,WK2114_G_REG_GRST,1<<sub_uart->uart_index);    
    HAL_Delay(0)  ;

    // 子串口中断使能  10 01 //10 03  / 10 07 / 10 0F
    wk2114_write_global_reg(wk2114,WK2114_G_REG_GIER,reg);
    HAL_Delay(0)  ;

    // 子串口baud 03 01 04 00 05 05 06 05 03 00 05 00 /054D0601 9600

    //  切换子串口设置页面 1
    if(wk2114_sub_uart_set_page(sub_uart,1)<0){  // 03 01
        return -2;
    }
    HAL_Delay(0)  ;
    // 
    if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index, WK2114_S_REG_BAUD1 ,baud_div>>8)<0){ // 04 00
        return -2;
    }
    HAL_Delay(0)  ;
    if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index,WK2114_S_REG_BAUD0 ,baud_div&0xff)<0){ //05 05
        return -2;
    }
    HAL_Delay(0)  ;
    if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index, WK2114_S_REG_PRES ,(tmp/10)%10)<0){  // 06 05
        return -2;
    }
    HAL_Delay(0)  ;
    if(wk2114_sub_uart_set_page(sub_uart,0)<0){  // 03 00
        return -2;
    }
    HAL_Delay(0)  ;
    if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index, WK2114_S_REG_LCR ,party|stop_bits)<0){ // 0500
        return -2;
    }
    HAL_Delay(0)  ;

    //  发送 和接收 触点中断数设置为 128  128 // 03 01 08 80 07 80
    if(wk2114_sub_uart_set_page(sub_uart,1)<0){
        return -2;
    }
    HAL_Delay(0)  ;
    if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index, WK2114_S_REG_TFTL ,16)<0){
        return -2;
    }
    HAL_Delay(0)  ;
    if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index, WK2114_S_REG_RFTL,128)<0){
        return -2;
    }
    HAL_Delay(0)  ;
    if(wk2114_sub_uart_set_page(sub_uart,0)<0){
        return -2;
    }
    HAL_Delay(0)  ;

    // 07 83  设置发送中断
    if( wk2114_write_subuart_reg(sub_uart->wk2114, 
                                    sub_uart->uart_index, 
                                    WK2114_S_REG_SIER ,
                                    WK2114_S_REG_SIFR_MASK_RFTRIG_INT|WK2114_S_REG_SIFR_MASK_RXOVT_INT|WK2114_S_REG_SIFR_MASK_FERR_INT)
                                    <0 ){
        // log_i("wk2114_sub_uart_set_inter  write")
        return -2;
    }
    HAL_Delay(0)  ;

    //06 0C   fifo Mode
    if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index,WK2114_S_REG_FCR ,fifo_mode)<0){
        return -2;
    }
    HAL_Delay(0)  ;

    // 04 03   mode  子串口rx tx
    if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index, WK2114_S_REG_SCR, mode)<0){
        return -2;
    }  
    return 0;
}

/**
 * @brief 更改子串口接收回调函数
 * 
 * @param sub_uart 子串口结构
 * @param recv_class 接受回调函数的第一个参数
 * @param recv_fun 接收回调函数
 * @return int 成功:0 失败:负值
 */
int wk2114_sub_uart_chenge_recv_fun(WK2114_Sub_Uart_TypeDef*sub_uart,void *recv_class,wk2114_sub_uart_recv_fun recv_fun)
{
    if(sub_uart==NULL || recv_class==NULL || recv_fun==NULL){
        return -1;
    }
    sub_uart->recv_class=recv_class;
    sub_uart->recv_fun=recv_fun;
    return 0;
}


// void wk2114_idle_callback(WK2114_TypeDef *wk2114, uint8_t *buf, uint32_t size)
// {
//     // log_w( " wk2114_idle_callback   ....  %d ", wk2114->state );
//     wk2114->idle_receive_flag = 1;
//     wk2114->size_received = size;
//     // if (wk2114->state == WK2114_WORK_STATE_SET_BAUD)
//     // {
//     //     if (wk2114->size_received == 1 && wk2xxx_rcvbuf[0] == 0) 
//     //     {
//     //         log_d("0x55  ok rcv  , byte_0 0x%02X", wk2xxx_rcvbuf[0] );
//     //         wk2114->setbaud_ok = 1;
//     //     }
//     // }
//     // if (wk2114->state == WK2114_WORK_STATE_SEND_40_RCV_F0_OK)
//     // {
//     //     if (wk2114->size_received == 1 && wk2xxx_rcvbuf[0] == 0xF0) 
//     //     {
//     //         log_d("0x40  ok rcv  , byte_0 0x%02X", wk2xxx_rcvbuf[0] );
//     //         wk2114->senf40_rcvF0_ok = 1;
//     //     }
//     // }
//     // if (wk2114->state == WK2114_WORK_STATE_OK)
//     // {
//     //     if (wk2114->size_received == 1 ) wk2114->idle_receive_flag = 1;
//     // }
//     // if (wk2114->state == WK2114_WORK_STATE_EXTI_READ_FIFO_BY_COUNT)
//     // {
//     //         log_d("size   %d  byte_0 0x%02X",size,wk2xxx_rcvbuf[0]);
//     //     // if (wk2114->size_received == 1 ) wk2114->idle_receive_flag = 1;
//     // }
 
//     return;
// }

// /**
//  * @brief 内部函数 接收寄存器回调
//  * 
//  * @param wk2114 WK2114_TypeDef 结构指针
//  * @note WK2114_G_REG_GIFR 判断哪个子串口中断，然后 子串口 WK2114_S_REG_SIFR 中断类型
//  */
// void wk2114_reg_recv_callback(WK2114_TypeDef *wk2114)
// {
//     int i;
//     int p_de;
//     if(WK2114_CMD_GET_ADDR(wk2114->cmd)==WK2114_G_REG_GIFR){  // 去读子串口中断类型
//         wk2114->cmd=0;
//         if((wk2114->reg&WK2114_G_REG_GIFR_MASK_UT1INT)!=0 && wk2114->sub_uart[WK2114_S_UART_1]!=NULL){
//             wk2114_read_subuart_reg(wk2114, WK2114_S_UART_1, WK2114_S_REG_SIFR );
//         }
//         if((wk2114->reg&WK2114_G_REG_GIFR_MASK_UT2INT)!=0 && wk2114->sub_uart[WK2114_S_UART_2]!=NULL){
//             wk2114_read_subuart_reg(wk2114, WK2114_S_UART_2,WK2114_S_REG_SIFR );
//         }
//         if((wk2114->reg&WK2114_G_REG_GIFR_MASK_UT3INT)!=0 && wk2114->sub_uart[WK2114_S_UART_3]!=NULL){
//             wk2114_read_subuart_reg(wk2114, WK2114_S_UART_3, WK2114_S_REG_SIFR );
//         }
//         if((wk2114->reg&WK2114_G_REG_GIFR_MASK_UT4INT)!=0 && wk2114->sub_uart[WK2114_S_UART_4]!=NULL){
//             wk2114_read_subuart_reg(wk2114, WK2114_S_UART_4, WK2114_S_REG_SIFR );
//         }
//         return;
//     }

//     // 判断中断类型
//     for(i=0;i<4;i++){
//         if(wk2114->sub_uart[i]!=NULL && (wk2114->sub_uart[i]->flag&WK2114_S_UART_FLAG_MASK_PAGE_D)==0){
//             if(WK2114_CMD_GET_ADDR(wk2114->sub_uart[i]->cmd)==WK2114_BUILD_SUB_REG(i,WK2114_S_REG_SIFR)){  // WK2114_S_REG_SIFR 子串口中断标志寄存器
//                 wk2114->sub_uart[i]->cmd=0;
//                 if((wk2114->sub_uart[i]->reg&WK2114_S_REG_SIFR_MASK_RXOVT_INT)!=0){
//                     wk2114->sub_uart[i]->flag|=WK2114_S_UART_FLAG_MASK_IDLE;
//                     wk2114_read_subuart_reg(wk2114, i,WK2114_S_REG_RFCNT );
//                 }
//                 else if((wk2114->sub_uart[i]->reg&WK2114_S_REG_SIFR_MASK_RFTRIG_INT)!=0){
//                     wk2114_read_fifo(wk2114,i,126);
//                 }
//                 if((wk2114->sub_uart[i]->reg&WK2114_S_REG_SIFR_MASK_TFEMPTY_INT)!=0 && wk2114->sub_uart[i]->send_buf_p==0xffffffff){ // 发送FIFO触点空中断 发送下一个字符串
//                     wk2114->sub_uart[i]->send_buf_p=0;
//                     // 判断 子串口的 发送队列是否为空
//                     // p_de=queue_get_de_index(&wk2114->sub_uart[i]->send_queue);
//                     // if(p_de>=0){
//                     if (osMessageQueueGetCount(wk2114->sub_uart[i]->send_queue) >= 0){
//                         if(wk2114->sub_uart[i]->send_record[p_de].size-wk2114->sub_uart[i]->send_buf_p <= 256){
//                             struct wk2114_send_record  record;
//                             osMessageQueueGet(wk2114->sub_uart[i]->send_queue,(void*) &record,sizeof(record),portMAX_DELAY);
//                             wk2114_write_fifo(wk2114,i,record.p_data[wk2114->sub_uart[i]->send_buf_p],record.size-wk2114->sub_uart[i]->send_buf_p-1);
//                             // wk2114_write_fifo(wk2114,i,&wk2114->sub_uart[i]->send_record[p_de].p_data[wk2114->sub_uart[i]->send_buf_p],wk2114->sub_uart[i]->send_record[p_de].size-wk2114->sub_uart[i]->send_buf_p-1);
//                             wk2114->sub_uart[i]->send_buf_p=0xffffffff;
//                             // queue_dequeue(&wk2114->sub_uart[i]->send_queue,NULL);
//                         }
//                         else{
//                             wk2114_write_fifo(wk2114,i,&wk2114->sub_uart[i]->send_record[p_de].p_data[wk2114->sub_uart[i]->send_buf_p],255);
//                             wk2114_sub_uart_set_inter(wk2114->sub_uart[i],WK2114_S_REG_SIFR_MASK_RFTRIG_INT|WK2114_S_REG_SIFR_MASK_RXOVT_INT|WK2114_S_REG_SIFR_MASK_TFTRIG_INT|WK2114_S_REG_SIFR_MASK_TFEMPTY_INT|WK2114_S_REG_SIFR_MASK_FERR_INT);
//                             wk2114->sub_uart[i]->send_buf_p+=256;
//                         }
//                     }
//                     else{
//                         wk2114_sub_uart_set_inter(wk2114->sub_uart[i],WK2114_S_REG_SIFR_MASK_RFTRIG_INT|WK2114_S_REG_SIFR_MASK_RXOVT_INT|WK2114_S_REG_SIFR_MASK_FERR_INT);
//                     }
//                 }
//                 else if((wk2114->sub_uart[i]->reg&WK2114_S_REG_SIFR_MASK_TFTRIG_INT)!=0){  // 发送FIFO触点中断 发送同一个字符串
//                     // p_de=queue_get_de_index(&wk2114->sub_uart[i]->send_queue);
//                     // if(p_de>=0){
//                     if (osMessageQueueGetCount(wk2114->sub_uart[i]->send_queue) >= 0){
//                         if(wk2114->sub_uart[i]->send_buf_p >= wk2114->sub_uart[i]->send_record[p_de].size){
//                             wk2114_sub_uart_set_inter(wk2114->sub_uart[i],WK2114_S_REG_SIFR_MASK_RFTRIG_INT|WK2114_S_REG_SIFR_MASK_RXOVT_INT|WK2114_S_REG_SIFR_MASK_TFEMPTY_INT|WK2114_S_REG_SIFR_MASK_FERR_INT);
//                             wk2114->sub_uart[i]->send_buf_p=0xffffffff;
//                             return;
//                         }
//                         if(wk2114->sub_uart[i]->send_record[p_de].size-wk2114->sub_uart[i]->send_buf_p <= 128){
//                             struct wk2114_send_record record;
//                             osMessageQueueGet(wk2114->sub_uart[i]->send_queue,(void*) &record,sizeof(record),portMAX_DELAY);
//                             wk2114_write_fifo(wk2114,i,record.p_data[wk2114->sub_uart[i]->send_buf_p],record.size-wk2114->sub_uart[i]->send_buf_p-1);
                           
//                             // wk2114_write_fifo(wk2114,i,&wk2114->sub_uart[i]->send_record[p_de].p_data[wk2114->sub_uart[i]->send_buf_p],wk2114->sub_uart[i]->send_record[p_de].size-wk2114->sub_uart[i]->send_buf_p-1);
//                             wk2114->sub_uart[i]->send_buf_p=0xffffffff;
//                             wk2114_sub_uart_set_inter(wk2114->sub_uart[i],WK2114_S_REG_SIFR_MASK_RFTRIG_INT|WK2114_S_REG_SIFR_MASK_RXOVT_INT|WK2114_S_REG_SIFR_MASK_TFEMPTY_INT|WK2114_S_REG_SIFR_MASK_FERR_INT);
//                             // queue_dequeue(&wk2114->sub_uart[i]->send_queue,NULL);
//                         }
//                         else{
//                             wk2114_write_fifo(wk2114,i,&wk2114->sub_uart[i]->send_record[p_de].p_data[wk2114->sub_uart[i]->send_buf_p],127);
//                             wk2114->sub_uart[i]->send_buf_p+=128;
//                         }
//                     }
//                     else{
//                         wk2114_sub_uart_set_inter(wk2114->sub_uart[i],WK2114_S_REG_SIFR_MASK_RFTRIG_INT|WK2114_S_REG_SIFR_MASK_RXOVT_INT|WK2114_S_REG_SIFR_MASK_FERR_INT);
//                     }
//                 }
//                 if((wk2114->sub_uart[i]->reg&WK2114_S_REG_SIFR_MASK_FERR_INT)!=0){
//                     wk2114_read_subuart_reg(wk2114, i, WK2114_S_REG_LSR );
//                 }
//             }
//             else if(WK2114_CMD_GET_ADDR(wk2114->sub_uart[i]->cmd)==WK2114_BUILD_SUB_REG(i,WK2114_S_REG_RFCNT)){
//                 wk2114->sub_uart[i]->cmd=0;
//                 wk2114_read_fifo(wk2114,i,wk2114->sub_uart[i]->reg-1);
//             }
//             else if(WK2114_CMD_GET_ADDR(wk2114->sub_uart[i]->cmd)==WK2114_BUILD_SUB_REG(i,WK2114_S_REG_LSR)){
//                 wk2114->sub_uart[i]->cmd=0;
//                 wk2114_read_subuart_reg(wk2114, i, WK2114_S_REG_FSR );
//             }
//             else if(WK2114_CMD_GET_ADDR(wk2114->sub_uart[i]->cmd)==WK2114_BUILD_SUB_REG(i,WK2114_S_REG_FSR)){
//                 wk2114->sub_uart[i]->cmd=0;
//             }
//         }
//     }
//     return;
// }

// void wk2114_idle_callback(WK2114_TypeDef *wk2114,uint8_t *buf,uint32_t size)
// {
//             log_w( " wk2114_idle_callback   ...., ");
//     wk2114->size_received = size;
//     switch (wk2114->state)
//     {
//         case WK2114_WORK_STATE_EXTI_READ_IT_SOURCE:
//             // osThreadSuspend(wk2114Handle    );
//             wk2114->state = WK2114_WORK_STATE_EXTI_READ_IT_SOURCE_OK;
//             // osThreadResume(wk2114Handle    );
//             return;
//         case WK2114_WORK_STATE_EXTI_READ_IT_TYPE:
//             log_w( " wk2114_idle_callback   READ_IT_TYPE ...., ");
//             // osThreadSuspend(wk2114Handle    );
//             wk2114->state = WK2114_WORK_STATE_EXTI_READ_IT_TYPE_OK;
//             // osThreadResume(wk2114Handle    );
//             return;          
//         case WK2114_WORK_STATE_EXTI_PROCESS_DATA:
//             log_w( " wk2114_idle_callback   PROCESS_DATA ...., ");
//             // osThreadSuspend(wk2114Handle    );
//             wk2114->state = WK2114_WORK_STATE_EXTI_PROCESS_DATA_OK;
//             // osThreadResume(wk2114Handle    );
//             return;    
//         default:
//             break;
//     }
// }


// /**
//  * @brief wk2114接收回调  读FIFO
//  * 
//  * @param wk2114 WK2114_TypeDef 结构指针
//  * @param buf 接收到的数据缓冲区
//  * @param size 接收到的数据长度
//  */
// void wk2114_recv_callback(WK2114_TypeDef *wk2114,uint8_t *buf,uint32_t size)
// {
//     int p_de;
//     uint8_t cmd;
//     uint8_t cmd_size;
//     uint8_t sub_uart;
//     WK2114_Sub_Uart_TypeDef*p_sub_uart;
//     cmd=wk2114->send_buf[0];
//     wk2114->send_buf[0]=0;
//     cmd_size=WK2114_CMD_GET_SIZE(cmd);  // 命令要读取的数量
//     sub_uart=WK2114_CMD_GET_SUB_UART(cmd);
//     p_sub_uart=wk2114->sub_uart[sub_uart];
//     if(WK2114_CMD_IS_WRITE(cmd)){
//         return;
//     }
//     if(WK2114_CMD_IS_REG(cmd)){    // 读寄存器
//         if(size!=1){
//             return;
//         }
//         if(WK2114_REG_IS_GLOBAL(cmd_size)){    // 全局寄存器， 重写wk2114的 cmd  reg
//             if(wk2114->cmd!=0xff){
//                 return;
//             }
//             wk2114->cmd=cmd;
//             wk2114->reg=buf[0];
//         }
//         else{                                   // 子串口寄存器, 重写子串口的 cmd  reg
//             if(p_sub_uart==NULL || wk2114->sub_uart[sub_uart]->cmd!=0xff){
//                 return;
//             }
//             p_sub_uart->cmd=cmd;
//             p_sub_uart->reg=buf[0];
//         }
//         wk2114->flag&=~WK2114_G_FLAG_MASK_BUSY_RCV;
//         wk2114_reg_recv_callback(wk2114);
//         return;
//     }

//     //  读数据回调
//     if(size!=cmd_size){  // 命令读取数量和实际读取数量不一致
//         return;
//     }
//     if(p_sub_uart->recv_buf_size-p_sub_uart->recv_buf_p < size){ // subuart buf 空间不足
//         cmd_size=p_sub_uart->recv_buf_size-p_sub_uart->recv_buf_p;
//     }
//     memcpy(&p_sub_uart->recv_buf[p_sub_uart->recv_buf_p],buf,cmd_size);
//     p_sub_uart->recv_buf_p+=cmd_size;

//     // 判断 发送队列是否为空，或缓冲区满，数据进一步回调
//     if (  osMessageQueueGetCount(wk2114->cmd_queue) == 0  && (p_sub_uart->flag&WK2114_S_UART_FLAG_MASK_IDLE)==1 )
//     {
//         p_sub_uart->flag&=~WK2114_S_UART_FLAG_MASK_IDLE;
//         p_sub_uart->recv_fun(p_sub_uart->recv_class,p_sub_uart->recv_buf,p_sub_uart->recv_buf_p);
//         p_sub_uart->recv_buf_p=0;
//     }
//     if(p_sub_uart->recv_buf_p==p_sub_uart->recv_buf_size)  // 缓冲区满
//     {
//         p_sub_uart->recv_fun(p_sub_uart->recv_class,p_sub_uart->recv_buf,p_sub_uart->recv_buf_p);
//         p_sub_uart->recv_buf_p=size-cmd_size;
//         memcpy(p_sub_uart->recv_buf,&buf[cmd_size],p_sub_uart->recv_buf_p);
//     }
//     wk2114->flag&=~WK2114_G_FLAG_MASK_BUSY_RCV;

//     // p_de=queue_get_de_index(&wk2114->cmd_queue);
//     // if((p_de<0 || (wk2114->cmd_buf[p_de][0]&0xf0)!=(cmd&0xf0)) && (p_sub_uart->flag&WK2114_S_UART_FLAG_MASK_IDLE)==1){   // 接收超时
//     //     p_sub_uart->flag&=~WK2114_S_UART_FLAG_MASK_IDLE;
//     //     p_sub_uart->recv_fun(p_sub_uart->recv_class,p_sub_uart->recv_buf,p_sub_uart->recv_buf_p);
//     //     p_sub_uart->recv_buf_p=0;
//     // }
//     // else if(p_sub_uart->recv_buf_p==p_sub_uart->recv_buf_size)  // 缓冲区满
//     // {
//     //     p_sub_uart->recv_fun(p_sub_uart->recv_class,p_sub_uart->recv_buf,p_sub_uart->recv_buf_p);
//     //     p_sub_uart->recv_buf_p=size-cmd_size;
//     //     memcpy(p_sub_uart->recv_buf,&buf[cmd_size],p_sub_uart->recv_buf_p);
//     // }
//     // wk2114->flag&=~WK2114_G_FLAG_MASK_BUSY_RCV;
// }

// /**
//  * @brief wk2114中断回调
//  * @note 不要将此函数放入中断中或比其他回调函数优先级更高的任务中
//  * @param wk2114 WK2114_TypeDef 结构指针
//  * 外部中断, 
//  */
// void wk2114_inter_callback(WK2114_TypeDef *wk2114)
// {
//     if((wk2114->flag&WK2114_G_FLAG_MASK_INTER)!=0){
//         return;
//     }

//     // wk2114->flag|=WK2114_G_FLAG_MASK_INTER;
//     // wk2114_read_reg( wk2114, WK2114_G_REG_GIFR );
//     // log_i("EXTI cmd QUeue  count   %d", osMessageQueueGetCount(wk2114->cmd_queue) );
//     // osThreadSuspend(wk2114Handle);  
//     wk2114->state = WK2114_WORK_STATE_EXTI;
//     // osThreadResume(wk2114Handle);  
// }

// /**
//  * @brief wk2114发送完成回调
//  * 
//  * @param wk2114 WK2114_TypeDef 结构指针
//  */
// void wk2114_send_completed_callback(WK2114_TypeDef *wk2114)
// {
//     wk2114->flag&=~WK2114_G_FLAG_MASK_SENDING;
//     // wk2114_send(wk2114);
// }


// void wk2114_get_it_type_by_it_source(WK2114_TypeDef *wk2114)
// {
//     // log_i("get_it_type_by_it_source....reg  %d",   wk2114->reg );
//     int i;
//     int p_de;
//     if(WK2114_CMD_GET_ADDR(wk2114->cmd)==WK2114_G_REG_GIFR){  // 去读子串口中断类型
//         wk2114->cmd=0;
//         if((wk2114->reg&WK2114_G_REG_GIFR_MASK_UT1INT)!=0 && wk2114->sub_uart[WK2114_S_UART_1]!=NULL){
//             wk2114_read_reg(wk2114,WK2114_BUILD_SUB_REG(WK2114_S_UART_1,WK2114_S_REG_SIFR));
//         }
//         if((wk2114->reg&WK2114_G_REG_GIFR_MASK_UT2INT)!=0 && wk2114->sub_uart[WK2114_S_UART_2]!=NULL){
//             wk2114_read_reg(wk2114,WK2114_BUILD_SUB_REG(WK2114_S_UART_2,WK2114_S_REG_SIFR));
//         }
//         if((wk2114->reg&WK2114_G_REG_GIFR_MASK_UT3INT)!=0 && wk2114->sub_uart[WK2114_S_UART_3]!=NULL){
//             wk2114_read_reg(wk2114,WK2114_BUILD_SUB_REG(WK2114_S_UART_3,WK2114_S_REG_SIFR));
//         }
//         if((wk2114->reg&WK2114_G_REG_GIFR_MASK_UT4INT)!=0 && wk2114->sub_uart[WK2114_S_UART_4]!=NULL){
//             wk2114_read_reg(wk2114,WK2114_BUILD_SUB_REG(WK2114_S_UART_4,WK2114_S_REG_SIFR));
//         }
//         return;
//     }

// }

// void wk2114_process_by_it_type(WK2114_TypeDef *wk2114)
// {
//     int i;
//     int p_de;    
//     log_i( " wk2114_process_by_it_type ...., ");
//     // 判断中断类型
//     for(i=0;i<4;i++){
//         // 判断子串口是否注册， 子串口flag的 标志位当前寄存器页 page0
//         if(wk2114->sub_uart[i]!=NULL && (wk2114->sub_uart[i]->flag&WK2114_S_UART_FLAG_MASK_PAGE_D)==0){ 
//             if(WK2114_CMD_GET_ADDR(wk2114->sub_uart[i]->cmd)==WK2114_BUILD_SUB_REG(i,WK2114_S_REG_SIFR)){  // WK2114_S_REG_SIFR 子串口中断标志寄存器
//                 wk2114->sub_uart[i]->cmd=0;
//                 if((wk2114->sub_uart[i]->reg&WK2114_S_REG_SIFR_MASK_RXOVT_INT)!=0){   // 接收超时
//                     wk2114->sub_uart[i]->flag|=WK2114_S_UART_FLAG_MASK_IDLE;
//                     wk2114_read_reg(wk2114,WK2114_BUILD_SUB_REG(i,WK2114_S_REG_RFCNT));
//                 }
//                 else if((wk2114->sub_uart[i]->reg&WK2114_S_REG_SIFR_MASK_RFTRIG_INT)!=0){ // 接收FIFO触点中断
//                     wk2114_read_fifo(wk2114,i,126);
//                 }
//                 if((wk2114->sub_uart[i]->reg&WK2114_S_REG_SIFR_MASK_TFEMPTY_INT)!=0 && wk2114->sub_uart[i]->send_buf_p==0xffffffff){ // FIFO触点发送为空中断 发送下一个字符串
//                     wk2114->sub_uart[i]->send_buf_p=0;
//                     // 判断 子串口的 发送队列是否为空
//                     // p_de=queue_get_de_index(&wk2114->sub_uart[i]->send_queue);
//                     // if(p_de>=0){
//                     if (osMessageQueueGetCount(wk2114->sub_uart[i]->send_queue) >= 0){
//                         if(wk2114->sub_uart[i]->send_record[p_de].size-wk2114->sub_uart[i]->send_buf_p <= 256){
//                             struct wk2114_send_record  record;
//                             osMessageQueueGet(wk2114->sub_uart[i]->send_queue,(void*) &record,sizeof(record),portMAX_DELAY);
//                             wk2114_write_fifo(wk2114,i,record.p_data[wk2114->sub_uart[i]->send_buf_p],record.size-wk2114->sub_uart[i]->send_buf_p-1);
//                             // wk2114_write_fifo(wk2114,i,&wk2114->sub_uart[i]->send_record[p_de].p_data[wk2114->sub_uart[i]->send_buf_p],wk2114->sub_uart[i]->send_record[p_de].size-wk2114->sub_uart[i]->send_buf_p-1);
//                             wk2114->sub_uart[i]->send_buf_p=0xffffffff;
//                             // queue_dequeue(&wk2114->sub_uart[i]->send_queue,NULL);
//                         }
//                         else{
//                             wk2114_write_fifo(wk2114,i,&wk2114->sub_uart[i]->send_record[p_de].p_data[wk2114->sub_uart[i]->send_buf_p],255);
//                             wk2114_sub_uart_set_inter(wk2114->sub_uart[i],WK2114_S_REG_SIFR_MASK_RFTRIG_INT|WK2114_S_REG_SIFR_MASK_RXOVT_INT|WK2114_S_REG_SIFR_MASK_TFTRIG_INT|WK2114_S_REG_SIFR_MASK_TFEMPTY_INT|WK2114_S_REG_SIFR_MASK_FERR_INT);
//                             wk2114->sub_uart[i]->send_buf_p+=256;
//                         }
//                     }
//                     else{
//                         wk2114_sub_uart_set_inter(wk2114->sub_uart[i],WK2114_S_REG_SIFR_MASK_RFTRIG_INT|WK2114_S_REG_SIFR_MASK_RXOVT_INT|WK2114_S_REG_SIFR_MASK_FERR_INT);
//                     }
//                 }
//                 else if((wk2114->sub_uart[i]->reg&WK2114_S_REG_SIFR_MASK_TFTRIG_INT)!=0){  // 发送FIFO触点中断 发送同一个字符串
//                     // p_de=queue_get_de_index(&wk2114->sub_uart[i]->send_queue);
//                     // if(p_de>=0){
//                     if (osMessageQueueGetCount(wk2114->sub_uart[i]->send_queue) >= 0){
//                         if(wk2114->sub_uart[i]->send_buf_p >= wk2114->sub_uart[i]->send_record[p_de].size){
//                             wk2114_sub_uart_set_inter(wk2114->sub_uart[i],WK2114_S_REG_SIFR_MASK_RFTRIG_INT|WK2114_S_REG_SIFR_MASK_RXOVT_INT|WK2114_S_REG_SIFR_MASK_TFEMPTY_INT|WK2114_S_REG_SIFR_MASK_FERR_INT);
//                             wk2114->sub_uart[i]->send_buf_p=0xffffffff;
//                             return;
//                         }
//                         if(wk2114->sub_uart[i]->send_record[p_de].size-wk2114->sub_uart[i]->send_buf_p <= 128){
//                             struct wk2114_send_record record;
//                             osMessageQueueGet(wk2114->sub_uart[i]->send_queue,(void*) &record,sizeof(record),portMAX_DELAY);
//                             wk2114_write_fifo(wk2114,i,record.p_data[wk2114->sub_uart[i]->send_buf_p],record.size-wk2114->sub_uart[i]->send_buf_p-1);
                           
//                             // wk2114_write_fifo(wk2114,i,&wk2114->sub_uart[i]->send_record[p_de].p_data[wk2114->sub_uart[i]->send_buf_p],wk2114->sub_uart[i]->send_record[p_de].size-wk2114->sub_uart[i]->send_buf_p-1);
//                             wk2114->sub_uart[i]->send_buf_p=0xffffffff;
//                             wk2114_sub_uart_set_inter(wk2114->sub_uart[i],WK2114_S_REG_SIFR_MASK_RFTRIG_INT|WK2114_S_REG_SIFR_MASK_RXOVT_INT|WK2114_S_REG_SIFR_MASK_TFEMPTY_INT|WK2114_S_REG_SIFR_MASK_FERR_INT);
//                             // queue_dequeue(&wk2114->sub_uart[i]->send_queue,NULL);
//                         }
//                         else{
//                             wk2114_write_fifo(wk2114,i,&wk2114->sub_uart[i]->send_record[p_de].p_data[wk2114->sub_uart[i]->send_buf_p],127);
//                             wk2114->sub_uart[i]->send_buf_p+=128;
//                         }
//                     }
//                     else{
//                         wk2114_sub_uart_set_inter(wk2114->sub_uart[i],WK2114_S_REG_SIFR_MASK_RFTRIG_INT|WK2114_S_REG_SIFR_MASK_RXOVT_INT|WK2114_S_REG_SIFR_MASK_FERR_INT);
//                     }
//                 }
//                 if((wk2114->sub_uart[i]->reg&WK2114_S_REG_SIFR_MASK_FERR_INT)!=0){   // 接收错误中断
//                     wk2114_read_reg(wk2114,WK2114_BUILD_SUB_REG(i,WK2114_S_REG_LSR));
//                 }
//             }
//             else if(WK2114_CMD_GET_ADDR(wk2114->sub_uart[i]->cmd)==WK2114_BUILD_SUB_REG(i,WK2114_S_REG_RFCNT)){  //子串口接收FIFO计数寄存器
//                 wk2114->sub_uart[i]->cmd=0;
//                 wk2114_read_fifo(wk2114,i,wk2114->sub_uart[i]->reg-1);
//             }
//             else if(WK2114_CMD_GET_ADDR(wk2114->sub_uart[i]->cmd)==WK2114_BUILD_SUB_REG(i,WK2114_S_REG_LSR)){   //子串口接收状态寄存器
//                 wk2114->sub_uart[i]->cmd=0;
//                 wk2114_read_reg(wk2114,WK2114_BUILD_SUB_REG(i,WK2114_S_REG_FSR));
//             }
//             else if(WK2114_CMD_GET_ADDR(wk2114->sub_uart[i]->cmd)==WK2114_BUILD_SUB_REG(i,WK2114_S_REG_FSR)){  //子串口FIFO状态寄存器 
//                 wk2114->sub_uart[i]->cmd=0;
//             }
//         }
//     }
//     return;
// }

// void wk2114_process_data_by_it_type(WK2114_TypeDef *wk2114,uint8_t *buf,uint32_t size)
// {
//     int p_de;
//     uint8_t cmd;
//     uint8_t cmd_size;
//     uint8_t sub_uart;
//     WK2114_Sub_Uart_TypeDef*p_sub_uart;
//     cmd=wk2114->send_buf[0];
//     wk2114->send_buf[0]=0;
//     cmd_size=WK2114_CMD_GET_SIZE(cmd);  // 命令要读取的数量
//     sub_uart=WK2114_CMD_GET_SUB_UART(cmd);
//     p_sub_uart=wk2114->sub_uart[sub_uart];
//     if(WK2114_CMD_IS_WRITE(cmd)){
//         return;
//     }
//     if(WK2114_CMD_IS_REG(cmd)){    // 读寄存器
//         if(size!=1){
//             return;
//         }
//         if(WK2114_REG_IS_GLOBAL(cmd_size)){    // 全局寄存器
//             if(wk2114->cmd!=0xff){
//                 return;
//             }
//             wk2114->cmd=cmd;
//             wk2114->reg=buf[0];
//         }
//         else{                                   // 子串口寄存器
//             if(p_sub_uart==NULL || wk2114->sub_uart[sub_uart]->cmd!=0xff){
//                 return;
//             }
//             p_sub_uart->cmd=cmd;
//             p_sub_uart->reg=buf[0];
//         }
//         wk2114->flag&=~WK2114_G_FLAG_MASK_BUSY_RCV;
//         wk2114_reg_recv_callback(wk2114);
//         return;
//     }

//     //  读数据回调
//     if(size!=cmd_size){  // 命令读取数量和实际读取数量不一致
//         return;
//     }
//     if(p_sub_uart->recv_buf_size-p_sub_uart->recv_buf_p < size){ // subuart buf甚于空间不足
//         cmd_size=p_sub_uart->recv_buf_size-p_sub_uart->recv_buf_p;
//     }
//     memcpy(&p_sub_uart->recv_buf[p_sub_uart->recv_buf_p],buf,cmd_size);
//     p_sub_uart->recv_buf_p+=cmd_size;

//     // 判断 发送队列是否为空，或缓冲区满，数据进一步回调
//     if (  osMessageQueueGetCount(wk2114->cmd_queue) == 0  && (p_sub_uart->flag&WK2114_S_UART_FLAG_MASK_IDLE)==1 )
//     {
//         p_sub_uart->flag&=~WK2114_S_UART_FLAG_MASK_IDLE;
//         p_sub_uart->recv_fun(p_sub_uart->recv_class,p_sub_uart->recv_buf,p_sub_uart->recv_buf_p);
//         p_sub_uart->recv_buf_p=0;
//     }
//     if(p_sub_uart->recv_buf_p==p_sub_uart->recv_buf_size)  // 缓冲区满
//     {
//         p_sub_uart->recv_fun(p_sub_uart->recv_class,p_sub_uart->recv_buf,p_sub_uart->recv_buf_p);
//         p_sub_uart->recv_buf_p=size-cmd_size;
//         memcpy(p_sub_uart->recv_buf,&buf[cmd_size],p_sub_uart->recv_buf_p);
//     }
//     wk2114->flag&=~WK2114_G_FLAG_MASK_BUSY_RCV;

//     // p_de=queue_get_de_index(&wk2114->cmd_queue);
//     // if((p_de<0 || (wk2114->cmd_buf[p_de][0]&0xf0)!=(cmd&0xf0)) && (p_sub_uart->flag&WK2114_S_UART_FLAG_MASK_IDLE)==1){   // 接收超时
//     //     p_sub_uart->flag&=~WK2114_S_UART_FLAG_MASK_IDLE;
//     //     p_sub_uart->recv_fun(p_sub_uart->recv_class,p_sub_uart->recv_buf,p_sub_uart->recv_buf_p);
//     //     p_sub_uart->recv_buf_p=0;
//     // }
//     // else if(p_sub_uart->recv_buf_p==p_sub_uart->recv_buf_size)  // 缓冲区满
//     // {
//     //     p_sub_uart->recv_fun(p_sub_uart->recv_class,p_sub_uart->recv_buf,p_sub_uart->recv_buf_p);
//     //     p_sub_uart->recv_buf_p=size-cmd_size;
//     //     memcpy(p_sub_uart->recv_buf,&buf[cmd_size],p_sub_uart->recv_buf_p);
//     // }
//     // wk2114->flag&=~WK2114_G_FLAG_MASK_BUSY_RCV;
// }


// WK2114_TypeDef * wk2114_init(WK2114_TypeDef *wk2114,uint32_t dev_clock_hz,void *send_class,wk2114_send_fun send_fun)
// {
//     int i;
//     if(wk2114==NULL || send_class==NULL || send_fun==NULL || dev_clock_hz>32000000 || dev_clock_hz<1000000){
//         return NULL;
//     }
//     if(queue_init(&wk2114->cmd_queue,NULL,64)==NULL){
//         return NULL;
//     }
//     wk2114->send_class=send_class;
//     wk2114->send_fun=send_fun;
//     wk2114->flag=0;
//     wk2114->dev_clock_hz=dev_clock_hz;
//     for(i=0;i<4;i++){
//         wk2114->sub_uart[0]=NULL;
//     }
//     if(wk2114_set_baud(wk2114)<0){
//         return NULL;
//     };
//     return wk2114;
// }




// 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)
// {
//     uint32_t tmp;
//     uint16_t baud_div;
//     uint8_t reg = 0;
//     tmp=sub_uart->wk2114->dev_clock_hz*100/16/baud;
//     if(tmp<10){
//         return -1;
//     }
//     baud_div=tmp/100-1;
//     for(int i=0;i<4;i++){
//         if(wk2114->sub_uart[i]!=NULL){
//             reg|=(1<<i);
//         }
//     }
//     //  子串口使能  00 01
//     sub_uart->flag|=WK2114_S_UART_FLAG_MASK_ENABLE;
//     wk2114_write_global_reg(wk2114,WK2114_G_REG_GENA,reg);
//     HAL_Delay(0);
 
//     wk2114_write_global_reg(sub_uart->wk2114,WK2114_G_REG_GRST,1<<sub_uart->uart_index);    
//     HAL_Delay(0);

//     // 子串口中断使能  10 01 //10 03  / 10 07 / 10 0F
//     wk2114_write_global_reg(wk2114,WK2114_G_REG_GIER,reg);
//     HAL_Delay(0);

//     // 子串口baud 03 01 04 00 05 05 06 05 03 00 05 00 /054D0601 9600

//     //  切换子串口设置页面 1
//     if(wk2114_sub_uart_set_page(sub_uart,1)<0){  // 03 01
//         return -2;
//     }
//     HAL_Delay(0);
//     // 
//     if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index, WK2114_S_REG_BAUD1 ,baud_div>>8)<0){ // 04 00
//         return -2;
//     }
//     HAL_Delay(0);
//     if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index,WK2114_S_REG_BAUD0 ,baud_div&0xff)<0){ //05 05
//         return -2;
//     }
//     HAL_Delay(0);
//     if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index, WK2114_S_REG_PRES ,(tmp/10)%10)<0){  // 06 05
//         return -2;
//     }
//     HAL_Delay(0);
//     if(wk2114_sub_uart_set_page(sub_uart,0)<0){  // 03 00
//         return -2;
//     }
//     HAL_Delay(0);
//     if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index, WK2114_S_REG_LCR ,party|stop_bits)<0){ // 0500
//         return -2;
//     }
//     HAL_Delay(0);


//     //  发送 和接收 触点中断数设置为 128  128 // 03 01 08 80 07 80
//     if(wk2114_sub_uart_set_page(sub_uart,1)<0){
//         return -2;
//     }
//     HAL_Delay(0);
//     if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index, WK2114_S_REG_TFTL ,128)<0){
//         return -2;
//     }
//     HAL_Delay(0);
//     if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index, WK2114_S_REG_RFTL,128)<0){
//         return -2;
//     }
//     HAL_Delay(0);
//     if(wk2114_sub_uart_set_page(sub_uart,0)<0){
//         return -2;
//     }
//     HAL_Delay(0);

//     // 07 83  设置发送中断
//     if( wk2114_write_subuart_reg(sub_uart->wk2114, 
//                                     sub_uart->uart_index, 
//                                     WK2114_S_REG_SIER ,
//                                     WK2114_S_REG_SIFR_MASK_RFTRIG_INT|WK2114_S_REG_SIFR_MASK_RXOVT_INT|WK2114_S_REG_SIFR_MASK_FERR_INT)
//                                     <0 ){
//         // log_i("wk2114_sub_uart_set_inter  write")
//         return -2;
//     }
//     HAL_Delay(0);

//     //06 0C   fifo Mode
//     if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index,WK2114_S_REG_FCR ,fifo_mode)<0){
//         return -2;
//     }
//     HAL_Delay(0);

//     // 04 03   mode  子串口rx tx
//     if(wk2114_write_subuart_reg(sub_uart->wk2114, sub_uart->uart_index, WK2114_S_REG_SCR, mode)<0){
//         return -2;
//     }  
//     return 0;
// }


// int wk2114_send(WK2114_TypeDef *wk2114)
// {
//     // log_i( "..... wk2114_send .---->>> flag  %d    " , wk2114->flag  );
//     // int p_de;
//     uint8_t cmd;
//     volatile uint8_t size;
//     WK2114_Sub_Uart_TypeDef *p_sub_uart = NULL;
//     // if( osEventFlagsGet(wk2114EventHandle)&WK2114_G_FLAG_MASK_BUSY_RCV != 0){  // 如果接收状态退出,exit
//     if( (wk2114->flag&WK2114_G_FLAG_MASK_BUSY_RCV)!=0 ){  // 如果接收状态退出,exit
//         return 0;
//     }
//     // if( osEventFlagsGet(wk2114EventHandle)&WK2114_G_FLAG_MASK_SENDING != 0){  // 如果接收状态退出,exit
//     if( (wk2114->flag&WK2114_G_FLAG_MASK_SENDING)!=0 ){ //  如果 sending ,exit
//         return 0;
//     }

//     //  TODO: 如何消掉 EXTI 中断标记, 消息数量是否为 0 
//     if (osMessageQueueGetCount( wk2114->cmd_queue) > 0) 
//     {
//         osMessageQueueGet( wk2114->cmd_queue, (void*)wk2114->cmd_queue_msg_get, 0 ,portMAX_DELAY);
//     }
//     if (osMessageQueueGetCount( wk2114->cmd_queue) == 0) 
//     {
//         wk2114->flag&=~WK2114_G_FLAG_MASK_INTER;  
//     }

//     wk2114->flag|=WK2114_G_FLAG_MASK_SENDING;
//     // osEventFlagsSet( wk2114EventHandle, WK2114_G_FLAG_MASK_SENDING );
//     cmd=wk2114->cmd_queue_msg_get[0];   // first byte
//     if( WK2114_CMD_IS_REG(cmd) ){   // 若为命令
//         // printf(" send -> is cmd    %02X\n " , cmd  );
//         // log_i( "..... send.......... cmd ...  %02X", cmd );
//         if(WK2114_CMD_IS_READ(cmd)){  // 读命令  1byte 0+1
//             // printf(" send read -> is cmd    %02X \r\n " , cmd  );
//             // log_i( "..... send.......... read"  );
//             size=0; 
//             wk2114->flag|=WK2114_G_FLAG_MASK_BUSY_RCV;
//             // osEventFlagsSet( wk2114EventHandle, WK2114_G_FLAG_MASK_BUSY_RCV );
//             wk2114->send_buf[0]=cmd;


//         }
//         else{    // 写命令  2byte 1+1
//             size=1;
//             wk2114->send_buf[0]=cmd;
//             wk2114->send_buf[1]=wk2114->cmd_queue_msg_get[1];
//             if( WK2114_CMD_GET_ADDR(cmd)==WK2114_S_REG_SPAGE ){
//                 if(wk2114->cmd_queue_msg_get[1]==0){
//                     wk2114->sub_uart[WK2114_CMD_GET_SUB_UART(cmd)]->flag&=~WK2114_S_UART_FLAG_MASK_PAGE_D;
//                 }
//                 else{
//                     wk2114->sub_uart[WK2114_CMD_GET_SUB_UART(cmd)]->flag|=WK2114_S_UART_FLAG_MASK_PAGE_D;
//                 }
//             }
//         }
//         // queue_dequeue(&wk2114->cmd_queue,NULL);  // 出队
//         // osMessageQueueGet(wk2114->cmd_queue );
//     }
//     else{   // 若为数据, ?? 需要开启接收？？
//         // printf(" send -> is data  \n "   );
//         // log_i( "..... send.......... data ... %02X", cmd );
//         p_sub_uart=wk2114->sub_uart[WK2114_CMD_GET_SUB_UART(cmd)];
//         if(p_sub_uart==NULL){
//             return -3;
//         }
//         // printf(" sub uart ok  \n "   );
//         if(WK2114_CMD_IS_READ(cmd)){   //  read  1byte 0+1
//             wk2114->flag|=WK2114_G_FLAG_MASK_BUSY_RCV;
//             // osEventFlagsSet( wk2114EventHandle, WK2114_G_FLAG_MASK_BUSY_RCV );
//             size=0;
//             if(wk2114->cmd_queue_msg_get[1] < 16){   // 判断数据量
//                 wk2114->send_buf[0]=(cmd&0xf0)+wk2114->cmd_queue_msg_get[1];
//                 // queue_dequeue(&wk2114->cmd_queue,NULL);
//             }
//             else{
//                 wk2114->send_buf[0]=cmd|0x0f;
//                 wk2114->cmd_queue_msg_get[1]-=16;
//             }
//         }
//         else{   // write  
//             if(wk2114->cmd_queue_msg_get[1] < 16){   // 判断数据量
//                 size=wk2114->cmd_queue_msg_get[1]+1;
//                 wk2114->send_buf[0]=(cmd&0xf0)+size-1;
//                 memcpy(&wk2114->send_buf[1],p_sub_uart->send_buf,size-1); // 发送子串口的send_buf ?? size size-1
//                 // queue_dequeue(&wk2114->cmd_queue,NULL);
//                 p_sub_uart->send_buf=NULL;
//             }
//             else{
//                 size=16;
//                 wk2114->send_buf[0]=cmd|0x0f;
//                 memcpy(&wk2114->send_buf[1],p_sub_uart->send_buf,size);
//                 p_sub_uart->send_buf+=16;
//                 wk2114->cmd_queue_msg_get[1]-=16;
//             }
//         }
//     }
//     log_i( "..... send. buf %02X   %02X  size %d" , wk2114->send_buf[0] , wk2114->send_buf[1],size+1);

//     return wk2114->send_fun(wk2114->send_class,wk2114->send_buf,size+1);
// }
