#include "wk2114.h" 
#include "usart.h"

static uint8_t reg_readout[1] = { 0 };
extern WK2114_Sub_Uart_TypeDef  *sub_uart1 ;
extern uint8_t sub_uart1_receive_buf[1024];
extern uint8_t wk2xxx_rcvbuf[1024];
// extern UART_HandleTypeDef *pWK2114Uart;

extern osSemaphoreId_t wk2xxx_lockHandle;
extern osSemaphoreId_t wk2xxx_irq_lockHandle;
extern osEventFlagsId_t wk2114EventHandle;

osSemaphoreId_t wk2xxx_lockHandle;
const osSemaphoreAttr_t wk2xxx_lock_attributes = {
    .name = "wk2xxx_lock"};

osSemaphoreId_t wk2xxx_sub_lockHandle;
const osSemaphoreAttr_t wk2xxx_sub_lock_attributes = {
    .name = "wk2xxx_sub_lock"};



    // osMutexId_t wk2xxx_irq_MutexHandle;
    // const osMutexAttr_t wk2xxx_irq_Mutex_attributes = {
    //   .name = "wk2xxx_irq_Mutex"
    // };
osSemaphoreId_t wk2xxx_irq_lockHandle;
const osSemaphoreAttr_t wk2xxx_irq_lock_attributes = {
    .name = "wk2xxx_irq_lock"};

osSemaphoreId_t wk2xxx_idle_lockHandle;
const osSemaphoreAttr_t wk2xxx_idle_lock_attributes = {
    .name = "wk2xxx_idle_lock"};

osThreadId_t wk2114Handle;
const osThreadAttr_t wk2114Handle_attributes = {
    .name = "wk2114",
    .stack_size = 1024,
    .priority = (osPriority_t)osPriorityBelowNormal,
};

osEventFlagsId_t wk2114EventHandle;
const osEventFlagsAttr_t wk2114Event_attributes = {
    .name = "wk2114Event"
};
 

#define WK2114_Uart_IRQHandler USART3_IRQHandler
UART_HandleTypeDef *pWK2114Uart = &huart3;


int Sub_rcv_callback(void * obj, uint8_t * buf, uint16_t len)
{
    return 0;
}

void WK2XXX_IRQ_HANDLER(void)
{
    if( __HAL_GPIO_EXTI_GET_IT( WK2XXX_IRQ_PIN ) )
    {
        // log_i("EXTI9_5_IRQHandler..WK2XXX IRQ HANDLER .. " );
        // osSemaphoreRelease(wk2xxx_irq_lockHandle);
        osEventFlagsSet(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_IRQ);
        // osMutexRelease(wk2xxx_irq_MutexHandle );
        log_i(" irq  EXTI9_5_  PIN7  set flag ..... " );

        __HAL_GPIO_EXTI_CLEAR_IT(WK2XXX_IRQ_PIN);
    }
    // HAL_GPIO_EXTI_IRQHandler(WK2114_IRQ_Pin);

}

// TODO 何时开启接收， 在  wk2114_read_reg 的时候
void WK2114_Uart_IRQHandler(void)
{
    uint16_t count = 0;
    uint16_t data_length = 0;
    // send cplt
    if (__HAL_UART_GET_FLAG(pWK2114Uart, UART_FLAG_TC) != RESET)
    {
 
        // log_i("WK2114 ..huart TC callback ..   "  );
        // wk2114_send_completed_callback( wk2114);
        wk2114->flag&=~WK2114_G_FLAG_MASK_SENDING;
        osSemaphoreRelease(wk2xxx_lockHandle );

        pWK2114Uart->gState = HAL_UART_STATE_READY;
        __HAL_UART_CLEAR_FLAG(pWK2114Uart, UART_FLAG_TC);
    }

    if (__HAL_UART_GET_FLAG(pWK2114Uart, UART_FLAG_IDLE) != RESET)
    {
        count = __HAL_DMA_GET_COUNTER( pWK2114Uart->hdmarx );
        // log_i("WK2114 ..huart  IDLE ... %d", 1024 - count);
        data_length = 1024 - count;
        if ( data_length > 0 )
        {
                wk2114->idle_receive_flag = 1;
                wk2114->size_received = data_length;
                osSemaphoreRelease(wk2xxx_idle_lockHandle );
        // osEventFlagsSet(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_IDLE);

            // wk2114_idle_callback(wk2114, wk2xxx_rcvbuf, data_length);
            // wk2114_reg_recv_callback(wk2114);
            // wk2114_recv_callback(wk2114, wk2xxx_rcvbuf, data_length );
            // log_i("WK2114_  IDLE.. data_length  %d   %02X",   data_length, wk2xxx_rcvbuf[0]);
        }
        __HAL_UART_CLEAR_IDLEFLAG( pWK2114Uart );
    }
}

int WK2114_Add_Sub_Uart(WK2114_TypeDef *wk2114 )
{
    sub_uart1 =  (WK2114_Sub_Uart_TypeDef *)malloc(sizeof(WK2114_Sub_Uart_TypeDef));
    if (0!=wk2114_sub_uart_register(wk2114, sub_uart1,WK2114_S_UART_1,sub_uart1_receive_buf,sizeof(sub_uart1_receive_buf), &huart2, Sub_rcv_callback))
    {
        log_e("sub-uart 1 register Failure ! ");
    };

    if (0!=wk2114_sub_uart_init( sub_uart1,115200,WK2114_SUB_UART_MODE_MASK_TX|WK2114_SUB_UART_MODE_MASK_RX,WK2114_SUB_UART_FIFO_MODE_MASK_TX|WK2114_SUB_UART_FIFO_MODE_MASK_RX,WK2114_UART_PARITY_NONE,WK2114_UART_STOP_BITS_1))
    {
        log_e("sub-uart 1 init Failure ! ");
    }
    return 0;
}


int WK2114_Init( )
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    WK2XXX_RST_GPIO_CLK_ENABLE();
    WK2XXX_IRQ_GPIO_CLK_ENABLE();
    // wk2XXX rst
    GPIO_InitStruct.Pin = WK2XXX_RST_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(WK2XXX_RST_GPIO_PORT, &GPIO_InitStruct);

    // wk2XXX irq
    GPIO_InitStruct.Pin = WK2XXX_IRQ_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
    GPIO_InitStruct.Pull = GPIO_PULLDOWN;
    HAL_GPIO_Init(WK2XXX_IRQ_GPIO_PORT, &GPIO_InitStruct);

    HAL_NVIC_SetPriority(WK2XXX_IRQ_EXTI_IRQ, 5, 0);
    HAL_NVIC_EnableIRQ(WK2XXX_IRQ_EXTI_IRQ);

    wk2114 = (WK2114_TypeDef *)malloc(sizeof(WK2114_TypeDef));
    wk2114->send_class  =  pWK2114Uart;
    wk2114->send_fun    =  HAL_UART_Transmit_DMA;

    // 命令队列  uint8_t cmd_buf[64][2] ,  消息类型 2个uint8_t  
    wk2114->cmd_queue = osMessageQueueNew( 64, sizeof(wk2114->cmd_queue_msg_put), NULL);
    if (wk2114->cmd_queue ==NULL)  return -1;

    wk2114->cmd = 0;
    wk2114->flag = 0;
    wk2114->dev_clock_hz = WK2114_CLOCK_HZ;
    
    for(size_t i=0;i<4;i++){
        wk2114->sub_uart[i] = NULL;
        wk2114->sub_uart_cfg[i] = NULL;
    }

    // osSemaphoreRelease(wk2xxx_lockHandle);
    // osSemaphoreRelease(wk2xxx_sub_lockHandle);
    // // osSemaphoreRelease(wk2xxx_irq_lockHandle);
    // osSemaphoreAcquire(wk2xxx_irq_lockHandle, osWaitForever);

    wk2114->size_received   = 0;
    // wk2114->setbaud_ok      = 0;
    // wk2114->senf40_rcvF0_ok = 0;
    wk2114->idle_receive_flag = 0;
    wk2114->state = WK2114_WORK_STATE_WAIT;


    return 0;
}


void WK2114_Port()
{
    wk2114Handle = osThreadNew(WK2114_Task, (void*)(&wk2114->state), &wk2114Handle_attributes);
    // wk2114EventHandle   = osEventFlagsNew ( &wk2114Event_attributes );
    // wk2xxx_irq_MutexHandle = osMutexNew(&wk2xxx_sub_lock_attributes);

    wk2xxx_lockHandle = osSemaphoreNew(1,1,&wk2xxx_lock_attributes);
osSemaphoreRelease(wk2xxx_lockHandle );
// osSemaphoreAcquire(wk2xxx_lockHandle, osWaitForever);


    wk2xxx_sub_lockHandle = osSemaphoreNew(1,1,&wk2xxx_sub_lock_attributes);
osSemaphoreRelease(wk2xxx_sub_lockHandle );
// osSemaphoreAcquire(wk2xxx_sub_lockHandle, osWaitForever);

    wk2xxx_irq_lockHandle = osSemaphoreNew(1,1,&wk2xxx_irq_lock_attributes);
osSemaphoreRelease(wk2xxx_irq_lockHandle );
osSemaphoreAcquire(wk2xxx_irq_lockHandle, osWaitForever);

    wk2xxx_idle_lockHandle = osSemaphoreNew(1,1,&wk2xxx_idle_lock_attributes);
osSemaphoreRelease(wk2xxx_idle_lockHandle );
osSemaphoreAcquire(wk2xxx_idle_lockHandle, osWaitForever);

    // osMutexAcquire(wk2xxx_irq_MutexHandle, osWaitForever);
    // wk2xxx_irq_MutexHandle = osMutexNew(&wk2xxx_sub_lock_attributes);

    wk2114EventHandle   = osEventFlagsNew ( &wk2114Event_attributes );
    osEventFlagsClear(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_IRQ );
    osEventFlagsClear(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_IDLE );
    // osEventFlagsClear(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_IT_SOURCE_RECEIVED );
    // osEventFlagsClear(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_IT_TYPE_SENDING );
    // osEventFlagsClear(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_IT_TYPE_RECEIVED );
    // osEventFlagsClear(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_RCV_FIFO_COUNT_RECEIVED );
    // osEventFlagsClear(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_RCV_FIFO_BUF_RECEIVED );
}


void WK2114_Task(void * argument )
{
    WK2114_WORK_STATE_TypeDef *state = argument;
    static int err_con = 0;
    static int state_ok_count = 0; 
    static uint8_t cmd;
    static uint8_t reg_readout;
    static uint8_t sub_uart_index;
    static uint8_t evt_flag = 0; 
    static uint32_t fifo_readout_count = 0; 
    static uint32_t subuart_sendqueue_count = 0; 
    static WK2114_Sub_Uart_TypeDef *p_sub_uart;
    static WK2114_Sub_Uart_CFG_TypeDef *p_sub_uart_cfg;
    for ( ;  ;  )
    {
        switch (*state) 
        {
        case WK2114_WORK_STATE_WAIT:
            log_i( "WK2114_wait  .... " );
            osDelay(100);
            break;
        case WK2114_WORK_STATE_START:
            err_con = 0;
            wk2114->timebase    = osKernelGetTickCount();
            *state = WK2114_WORK_STATE_SET_BAUD;
            continue;     
        case WK2114_WORK_STATE_SET_BAUD:
        {
            log_i( "WK2114_WORK_STATE_SET_BAUD .... " );
            wk2114->cmd = 0x55;
            reg_readout = 0xFF;
            wk2114->timebase    = osKernelGetTickCount();
            wk2114_reset( wk2114);
            wk2114_mainport_rcv( wk2114, wk2xxx_rcvbuf, 1024 );
            wk2114_set_baud( wk2114);
            osDelay(1);
            wk2114_set_baud( wk2114);
            *state = WK2114_WORK_STATE_SET_BAUD_OK;
            // osEventFlagsClear( wk2114EventHandle,  MODULE_WK2114_EVENT_EXTI_IDLE );
            osSemaphoreRelease(wk2xxx_idle_lockHandle );
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, osWaitForever);
            continue;
        }

        case WK2114_WORK_STATE_SET_BAUD_OK:
        {
            log_i( "WK2114_WORK_STATE_SET_BAUD   OK ....  " );
            // osDelay(1);
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, 0xFF);
            *state = WK2114_WORK_STATE_IT_SOURCE_GETTING;
            osSemaphoreRelease(wk2xxx_idle_lockHandle );
            if (  wk2xxx_rcvbuf[0] != 0x00 )
            {
                    err_con++;
                    log_i( " setbaud , retry  %d times ", err_con );
                    if (err_con>40)
                    {
                        *state = WK2114_WORK_STATE_ERROR;  
                        break; 
                    }
                    HAL_UART_DMAStop(pWK2114Uart);
                    *state = WK2114_WORK_STATE_SET_BAUD;  
                    break; 
            }
            // osSemaphoreWait(wk2xxx_idle_lockHandle );
            // uint32_t evt_flag = osEventFlagsWait(wk2114EventHandle,MODULE_WK2114_EVENT_EXTI_IDLE, 0, portMAX_DELAY);
            // if ( evt_flag & MODULE_WK2114_EVENT_EXTI_IDLE != 0 ) {
            //     osEventFlagsClear( wk2114EventHandle,  MODULE_WK2114_EVENT_EXTI_IDLE );
            //     *state = WK2114_WORK_STATE_IT_SOURCE_GETTING;
            //     HAL_UART_DMAStop( pWK2114Uart );
            //     continue;
            // }else{
            //      *state = WK2114_WORK_STATE_ERROR;
            //     HAL_UART_DMAStop( pWK2114Uart );
            //     continue;               
            // }
            // while (wk2114->idle_receive_flag!=1 || wk2xxx_rcvbuf[0] != 0x00)
            // {
            //     if ( wk2114->timebase-osKernelGetTickCount() >200)
            //     {
            //         err_con++;
            //         log_i( " setbaud , retry  %d times ", err_con );
            //         if (err_con>10)
            //         {
            //             *state = WK2114_WORK_STATE_ERROR;  
            //             break; 
            //         }
            //         HAL_UART_DMAStop(pWK2114Uart);
            //         *state = WK2114_WORK_STATE_SET_BAUD;  
            //         break; 
            //     }
            // }

            // log_d( "WK2114 SET_BAUD   OK ....  rcv0 0x%02X", wk2xxx_rcvbuf[0] );
            HAL_UART_DMAStop( pWK2114Uart );
            *state = WK2114_WORK_STATE_SEND_40_RCV_F0;  
            continue;
        }
  
        case WK2114_WORK_STATE_SEND_40_RCV_F0:
        {
            log_i( "WK2114_WORK_STATE  SEND_40  RCV_F0   ... " );
            wk2114->cmd = 0x40;
            wk2114->timebase    = osKernelGetTickCount();
            wk2114_mainport_rcv( wk2114,  wk2xxx_rcvbuf, 1024 );
            wk2114->send_fun(wk2114->send_class,&wk2114->cmd,1);
            *state = WK2114_WORK_STATE_SEND_40_RCV_F0_OK;
            osSemaphoreRelease(wk2xxx_idle_lockHandle );
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, osWaitForever);
            continue;
        }

        case WK2114_WORK_STATE_SEND_40_RCV_F0_OK:
        {
            log_d( "WK2114_WORK_STATE  SEND_40  RCV_F0 OK ....  " );
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, 0xFF);
            // *state = WK2114_WORK_STATE_IT_SOURCE_GETTING;
            // osSemaphoreRelease(wk2xxx_idle_lockHandle );
            if (  wk2xxx_rcvbuf[0] != 0xF0 )
            {
                    err_con++;
                    log_i( " SEND_40  RCV_F0  %d times ", err_con );
                    if (err_con>40)
                    {
                        *state = WK2114_WORK_STATE_ERROR;  
                        break; 
                    }
                    HAL_UART_DMAStop(pWK2114Uart);
                    *state = WK2114_WORK_STATE_SET_BAUD;  
                    break; 
            }
            HAL_UART_DMAStop(pWK2114Uart);
            osSemaphoreRelease(wk2xxx_idle_lockHandle );
            *state = WK2114_WORK_TEST_OK;  
    
        }

        case WK2114_WORK_TEST_OK:
        {
            log_i( "WK2114_WORK_TEST_OK    ... " );
            osDelay(20);
            break;
        }


        case WK2114_WORK_STATE_ADD_SUB_UART:
        {
            log_i( "WK2114_WORK_STATE_ADD_SUB_UART .... " );
            WK2114_Add_Sub_Uart( wk2114 );  
            *state = WK2114_WORK_STATE_ADD_SUB_UART_OK;
            continue;
        }

        case WK2114_WORK_STATE_ADD_SUB_UART_OK:
        {
            log_i( "WK2114_WORK_STATE_ADD_SUB_UART OK   ....  ??  " );
            *state = WK2114_WORK_STATE_OK;
            osDelay(2);
            continue;
        }

        case WK2114_WORK_STATE_OK:
        {
            log_i( "WK2114_WORK_STATE_OK  --> wait irq exti  " );
            // osSemaphoreAcquire(wk2xxx_irq_lockHandle, osWaitForever);

            // *state = WK2114_WORK_STATE_IT_SOURCE_GETTING;
            // osSemaphoreRelease(wk2xxx_irq_lockHandle);
            // osSemaphoreAcquire(wk2xxx_irq_lockHandle, osWaitForever);
 
            uint32_t evt_flag = osEventFlagsWait(wk2114EventHandle, 
                    MODULE_WK2114_EVENT_EXTI_IRQ,
                    0, 
                    portMAX_DELAY);
            log_i( " WK2114 IRQ  exti  happened ....." );
            if ( evt_flag & MODULE_WK2114_EVENT_EXTI_IRQ != 0 ) {
                osEventFlagsClear( wk2114EventHandle,  MODULE_WK2114_EVENT_EXTI_IRQ );
                *state = WK2114_WORK_STATE_IT_SOURCE_GETTING;
                continue;
            }
            // *state = WK2114_WORK_STATE_IT_SOURCE_GETTING;
            // osDelay(20);
            continue;
        }

        case WK2114_WORK_STATE_IT_SOURCE_GETTING:
        {
            wk2114->timebase    = osKernelGetTickCount();
            wk2114->cmd = WK2114_G_REG_GIFR;
            cmd = WK2114_G_REG_GIFR;
            osSemaphoreRelease(wk2xxx_idle_lockHandle );
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, osWaitForever);
            wk2114_read_global_reg( wk2114,  wk2114->cmd );
            // wk2114->reg_gifr = wk2xxx_rcvbuf[0];
            // log_i( " WK2114 IRQ  irq source 0x%02X", wk2114->reg_gifr );
            *state = WK2114_WORK_STATE_IT_TYPE_GETTING;

            continue;
 
        }

        case WK2114_WORK_STATE_IT_TYPE_GETTING:
        {
            log_d(" irq WK2114_WORK_STATE_IT_TYPE_GETTING .. by irq source 0x%02X", wk2114->reg_gifr);
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, 0xFF);
            // *state = WK2114_WORK_STATE_IT_SOURCE_GETTING;
            HAL_UART_DMAStop(pWK2114Uart);
            osSemaphoreRelease(wk2xxx_idle_lockHandle );
            wk2114->reg_gifr = wk2xxx_rcvbuf[0];

            wk2114->reg_readout = wk2114->reg_gifr;
            log_i(" irq source 0x%02X", wk2114->reg_gifr );
            switch (wk2114->reg_readout)
            {
            case WK2114_G_REG_GIFR_MASK_UT1INT:
                if(  wk2114->sub_uart[WK2114_S_UART_1]!=NULL){
                    wk2114->it_sub_uart_index = WK2114_S_UART_1;
                }
                break;
            case WK2114_G_REG_GIFR_MASK_UT2INT:
                if(  wk2114->sub_uart[WK2114_S_UART_2]!=NULL){
                wk2114->it_sub_uart_index = WK2114_S_UART_2;
                }
                break;
            case WK2114_G_REG_GIFR_MASK_UT3INT:
                if(  wk2114->sub_uart[WK2114_S_UART_3]!=NULL){
                wk2114->it_sub_uart_index = WK2114_S_UART_3;
                }
                break;            
            case WK2114_G_REG_GIFR_MASK_UT4INT:
                if(  wk2114->sub_uart[WK2114_S_UART_4]!=NULL){
                wk2114->it_sub_uart_index = WK2114_S_UART_4;
                }
                break;            
            default:
                log_e(" it source error ! ");
                *state = WK2114_WORK_STATE_OK;
                break;
            }
            log_i(" it_sub_uart_index source 0x%02X", wk2114->it_sub_uart_index );
            p_sub_uart = wk2114->sub_uart[wk2114->it_sub_uart_index];

            // 读子串口寄存器  it_type
            p_sub_uart->cmd = WK2114_S_REG_SIFR;
            cmd = WK2114_S_REG_SIFR;
            osSemaphoreRelease(wk2xxx_idle_lockHandle );
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, osWaitForever);
            wk2114_read_subuart_reg(wk2114,   wk2114->it_sub_uart_index, p_sub_uart->cmd );

            *state = WK2114_WORK_STATE_PROCESS_IT_TYPE;
            continue;
        }

        case WK2114_WORK_STATE_PROCESS_IT_TYPE:
        {
            log_d("WK2114_WORK_STATE_PROCESS_IT_TYPE .. subuart 0x%02X  it_type 0x%02X  ", wk2114->it_sub_uart_index,wk2xxx_rcvbuf[0]);
            // log_d("WK2114_WORK_STATE_PROCESS_IT_TYPE .. subuart 0x%02X  it_type 0x%02X  ", wk2114->it_sub_uart_index,wk2xxx_rcvbuf[0]);
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, 0xFF);
            HAL_UART_DMAStop(pWK2114Uart);
            osSemaphoreRelease(wk2xxx_idle_lockHandle );            
            wk2114->reg_readout = wk2xxx_rcvbuf[0];
            osDelay(1);
 
            // wk2114->reg_readout = wk2114->sub_uart_cfg[wk2114->it_sub_uart_index]->reg_sifr;
            p_sub_uart->reg =  wk2xxx_rcvbuf[0];
            reg_readout =  wk2xxx_rcvbuf[0];
            log_d("  subuart 0x%02X  it_type 0x%02X  0x%02X ", wk2114->it_sub_uart_index, wk2114->reg_readout, wk2xxx_rcvbuf[0]);
            // log_d("     it_type 0x%02X  ",wk2114->sub_uart_cfg[wk2114->it_sub_uart_index]->reg_sifr);
            
            // 接收FIFO触点中断 ,接收FIFO中的数据个数大于设定的 128  ,0x01     
            if( ( p_sub_uart->reg&WK2114_S_REG_SIFR_MASK_RFTRIG_INT)!=0 ){ 
                *state = WK2114_WORK_STATE_EXTI_PROCESS_RFTRIG_INT;
                continue;
            }
            //  接收字节超时中断  读取字节数未满 且空闲4字节  0x02
            if( ( p_sub_uart->reg&WK2114_S_REG_SIFR_MASK_RXOVT_INT)!=0 ){   
                *state = WK2114_WORK_STATE_EXTI_PROCESS_RXOVT_INT;
                continue;
            }

            // 发送FIFO触点中断 ,当发送FIFO中的数据个数小于设定的128时，产生该中断  /0x04
            if((p_sub_uart->reg&WK2114_S_REG_SIFR_MASK_TFTRIG_INT)!=0){  
                // log_d("_IT_ type ....  %d", p_sub_uart->reg );
                *state = WK2114_WORK_STATE_EXTI_PROCESS_TFTRIG_INT;
                continue;
            }
            // FIFO触点发送为空中断 , FIFO ,当发送FIFO中没有数据，大约设定的128 中断清除  ,0x08
            if( ( p_sub_uart->reg&WK2114_S_REG_SIFR_MASK_TFEMPTY_INT)!=0 ) { 
                *state = WK2114_WORK_STATE_EXTI_PROCESS_TFEMPTY_INT;
                continue;
            }
            // if( (p_sub_uart->reg&WK2114_S_REG_SIFR_MASK_TFEMPTY_INT)!=0 
            //         && wk2114->sub_uart[wk2114->it_sub_uart_index]->send_buf_p==0xffffffff ) { 
            //     *state = WK2114_WORK_STATE_EXTI_PROCESS_TFEMPTY_INT;
            //     continue;
            // }
            // 接收错误中断 0x80
            if( ( p_sub_uart->reg&WK2114_S_REG_SIFR_MASK_FERR_INT)!=0 ){   
                *state = WK2114_WORK_STATE_EXTI_PROCESS_FERR_INT;
                continue;
            }

            *state = WK2114_WORK_STATE_OK;
            break;
        }

        case WK2114_WORK_STATE_EXTI_PROCESS_RXOVT_INT:
        {
            log_w(" Rcv Fifo Byte Overtime Int ....  ");
            p_sub_uart->cmd = WK2114_S_REG_RFCNT;
            cmd = WK2114_S_REG_SIFR;
            osSemaphoreRelease(wk2xxx_idle_lockHandle );
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, osWaitForever);
            wk2114_read_subuart_reg(wk2114,   wk2114->it_sub_uart_index, p_sub_uart->cmd );
                *state = WK2114_WORK_STATE_EXTI_PROCESS_GET_FIFO_COUNT;
                continue;
        }


        case WK2114_WORK_STATE_EXTI_PROCESS_RFTRIG_INT:
        {
            log_i("Rcv Fifo Trigger .... read rcv fifo count ");

            // 读子串口寄存器  it_type
            p_sub_uart->cmd = WK2114_S_REG_RFCNT;
            cmd = WK2114_S_REG_SIFR;
            osSemaphoreRelease(wk2xxx_idle_lockHandle );
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, osWaitForever);
            wk2114_read_subuart_reg(wk2114,   wk2114->it_sub_uart_index, p_sub_uart->cmd );
                *state = WK2114_WORK_STATE_EXTI_PROCESS_GET_FIFO_COUNT;
                continue;
        }


        case WK2114_WORK_STATE_EXTI_PROCESS_TFEMPTY_INT:
        {
            log_w("Tx Fifo Empty Int....  ");
            if(  p_sub_uart->send_buf_p==0xffffffff){ 
                log_w(" send fifo trigger == 0");
                p_sub_uart->send_buf_p=0;
                uint8_t queue_count = osMessageQueueGetCount(p_sub_uart->send_queue);
                // 判断 子串口的 发送队列是否为空
                // p_de=queue_get_de_index(&p_sub_uart->send_queue);
                // if(p_de>=0){
                if (osMessageQueueGetCount(p_sub_uart->send_queue) >= 0){
                    if(p_sub_uart->send_record[queue_count-1].size-p_sub_uart->send_buf_p <= 256){
                        struct wk2114_send_record  record;
                        osMessageQueueGet(p_sub_uart->send_queue,(void*) &record,sizeof(record),portMAX_DELAY);
                        wk2114_write_fifo(wk2114,sub_uart_index,record.p_data[p_sub_uart->send_buf_p],record.size-p_sub_uart->send_buf_p-1);
                        // wk2114_write_fifo(wk2114,i,&p_sub_uart->send_record[p_de].p_data[p_sub_uart->send_buf_p],p_sub_uart->send_record[p_de].size-p_sub_uart->send_buf_p-1);
                        p_sub_uart->send_buf_p=0xffffffff;
                        // queue_dequeue(&p_sub_uart->send_queue,NULL);
                    }
                    else{  // buff 大于256， 发送 256, 记录发送位置  p_sub_uart->send_buf_p
                        wk2114_write_fifo(wk2114,sub_uart_index,&p_sub_uart->send_record[queue_count-1].p_data[p_sub_uart->send_buf_p],255);
                        wk2114_sub_uart_set_inter(p_sub_uart,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);
                        p_sub_uart->send_buf_p+=256;
                    }
                }
                else{
                    // 子串口队列无发送任务，取消发送出点为空中断
                    wk2114_sub_uart_set_inter(p_sub_uart,WK2114_S_REG_SIFR_MASK_RFTRIG_INT|WK2114_S_REG_SIFR_MASK_RXOVT_INT|WK2114_S_REG_SIFR_MASK_FERR_INT);
                *state = WK2114_WORK_STATE_STOP;
                }
            }
            continue;
        }

        // 发送FIFO触点中断 ,当发送FIFO中的数据个数小于设定的128时，产生该中断  /0x04
        case WK2114_WORK_STATE_EXTI_PROCESS_TFTRIG_INT:
        {
            log_d("Tx Fifo Trigger Int  TFTRIG .....  ");
            uint8_t size;
            uint8_t current_record_p =0;

            osSemaphoreAcquire(wk2xxx_sub_lockHandle, osWaitForever);
            subuart_sendqueue_count = osMessageQueueGetCount(p_sub_uart->send_queue);
            // osDelay(20);

            // log_w("Tx Fifo Trigger Int  ..... subuart_sendqueue_count %d ",subuart_sendqueue_count);
            if (subuart_sendqueue_count >= 0){
                if ( (p_sub_uart->send_record_p-subuart_sendqueue_count-1) > 0)
                {
                    current_record_p = p_sub_uart->send_record_p-subuart_sendqueue_count;
                }else{
                    current_record_p = p_sub_uart->send_record_p + 8 -subuart_sendqueue_count ;
                }
                //  log_d("  p_sub_uart->send_buf_p %d" , p_sub_uart->send_buf_p);
                // log_d("  record size %d" , p_sub_uart->send_record[current_record_p]);
                //  当前record数据发完，取出队列， 状态回到 state_ok
                if( p_sub_uart->send_buf_p >= p_sub_uart->send_record[current_record_p].size ){                   
                    log_d(" current record  send cplt " );
                    osMessageQueueGet( p_sub_uart->send_queue, (void*) &current_record_p, sizeof(current_record_p), portMAX_DELAY);  // 发送完取出当前消息
                    wk2114->reg = 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_write_subuart_reg(wk2114, wk2114->it_sub_uart_index, WK2114_S_REG_SIER ,wk2114->reg);
                    osSemaphoreAcquire(wk2xxx_lockHandle, osWaitForever);
                    osSemaphoreRelease(wk2xxx_lockHandle);
                    // wk2114_sub_uart_set_inter(p_sub_uart,wk2114->reg);
                    p_sub_uart->send_buf_p = 0xffffffff;  // 计数复原
                    osSemaphoreRelease(wk2xxx_sub_lockHandle);
                    *state = WK2114_WORK_STATE_EXTI_READ_SUB_FSR;
                    continue;
                }
                //  当前record 没有发的小于 16
                if( p_sub_uart->send_record[current_record_p].size-p_sub_uart->send_buf_p <= 16){
                    // log_w("   < < 16   ......"  );
                    size = p_sub_uart->send_record[current_record_p].size-p_sub_uart->send_buf_p;
                    //  快速写 128buf , 清除中断， 每次16 
                    wk2114_write_fifo(wk2114,
                        wk2114->it_sub_uart_index,
                        &p_sub_uart->send_record->p_data[current_record_p],
                        size-1);

                    p_sub_uart->send_buf_p=0xffffffff;

                    wk2114->reg = 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_write_subuart_reg(wk2114, wk2114->it_sub_uart_index, WK2114_S_REG_SIER ,wk2114->reg);
                    osSemaphoreAcquire(wk2xxx_lockHandle, osWaitForever);
                    osSemaphoreRelease(wk2xxx_lockHandle);
                    // wk2114_sub_uart_set_inter(p_sub_uart,wk2114->reg);
                    osSemaphoreRelease(wk2xxx_sub_lockHandle);
                    continue;
                }
                else{ // 计数大于128
                    // log_w("   > 16 ...... "  );
                    wk2114_write_fifo(wk2114,
                        wk2114->it_sub_uart_index,
                        &p_sub_uart->send_record->p_data[current_record_p],
                        15);
                    p_sub_uart->send_buf_p+=16;
                    osSemaphoreRelease(wk2xxx_sub_lockHandle);
                    continue;
                }
            }
            else{
                wk2114->reg = WK2114_S_REG_SIFR_MASK_RFTRIG_INT|WK2114_S_REG_SIFR_MASK_RXOVT_INT|WK2114_S_REG_SIFR_MASK_FERR_INT;
                wk2114_write_subuart_reg(wk2114, wk2114->it_sub_uart_index, WK2114_S_REG_SIER ,wk2114->reg);
                // wk2114_sub_uart_set_inter(p_sub_uart,wk2114->reg);
                    osSemaphoreAcquire(wk2xxx_lockHandle, osWaitForever);
                    osSemaphoreRelease(wk2xxx_lockHandle);
                    osSemaphoreRelease(wk2xxx_sub_lockHandle);
                *state = WK2114_WORK_STATE_OK;
                continue;

            }
            // osSemaphoreRelease(wk2xxx_sub_lockHandle);

            *state = WK2114_WORK_STATE_OK;
            continue;
            osDelay(20);
 
            break;
        }

        case WK2114_WORK_STATE_EXTI_PROCESS_FERR_INT:
        {
            log_e("Fifo Error ....  ");
            __HAL_UART_ENABLE_IT(pWK2114Uart, UART_IT_IDLE );
            HAL_UART_Receive_DMA(pWK2114Uart, &wk2114->reg_readout, 1 );
            // HAL_UARTEx_ReceiveToIdle_DMA( pWK2114Uart, wk2xxx_rcvbuf, 1024);
            wk2114_read_subuart_reg( wk2114,  wk2114->it_sub_uart_index, WK2114_S_REG_LSR  );
            *state = WK2114_WORK_STATE_STOP;
            break;
        }

        case WK2114_WORK_STATE_EXTI_PROCESS_GET_FIFO_COUNT:
        {
            log_e("WK2114_WORK_STATE_EXTI_PROCESS  GET_FIFO_COUNT  ...%d ",wk2114->reg_readout   );
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, 0xFF);
            // *state = WK2114_WORK_STATE_IT_SOURCE_GETTING;
            HAL_UART_DMAStop(pWK2114Uart);
            osSemaphoreRelease(wk2xxx_idle_lockHandle );   
            wk2114->reg_readout = wk2xxx_rcvbuf[0];
            // TODO  数量为0的情况处理 
            // wk2114->reg_readout = wk2xxx_rcvbuf[0];            
            log_i( " fifo  number :  0x%02X  ",  wk2114->reg_readout );
            *state = WK2114_WORK_STATE_EXTI_READ_FIFO_BY_COUNT;
            // osDelay(20);
            continue;
        }


        case WK2114_WORK_STATE_EXTI_READ_FIFO_BY_COUNT:
        {
            log_e("Read Fifo BUF by fifo ,  number -> 0x%02X ",wk2114->reg_readout );
            fifo_readout_count =  wk2114->reg_readout;

            //  接收数量为0 ， 
 

            if ( fifo_readout_count > 16 )  {
                p_sub_uart->cmd = 16-1;
                osSemaphoreRelease(wk2xxx_idle_lockHandle );
                osSemaphoreAcquire(wk2xxx_idle_lockHandle, osWaitForever);
                wk2114_read_fifo( wk2114, wk2114->it_sub_uart_index, p_sub_uart->cmd);
                fifo_readout_count -= 16;   
                log_w( "Fifo read out %d, remain  %d ", p_sub_uart->cmd+1, fifo_readout_count );           
            } else {
                p_sub_uart->cmd = fifo_readout_count-1;
                osSemaphoreRelease(wk2xxx_idle_lockHandle );
                osSemaphoreAcquire(wk2xxx_idle_lockHandle, osWaitForever);
                wk2114_read_fifo( wk2114, wk2114->it_sub_uart_index, p_sub_uart->cmd);
                fifo_readout_count = 0;               
                log_w( "Fifo read cplt .... "  );           
                *state = WK2114_WORK_STATE_EXTI_DEAL_FIFO_READOUT;
                continue;
            } 

            *state = WK2114_WORK_STATE_EXTI_DEAL_FIFO_READOUT;
            // osDelay(1000);
            continue;
        }
        case WK2114_WORK_STATE_EXTI_DEAL_FIFO_READOUT:
        {
            log_e( "   Deal FIFO BUF "   );
            osSemaphoreAcquire( wk2xxx_idle_lockHandle, 0xFF );
            // TODO  判断字节数量
            if (wk2114->size_received == ( p_sub_uart->cmd + 1) )
            {
                HAL_UART_DMAStop(pWK2114Uart);
                osSemaphoreRelease(wk2xxx_idle_lockHandle ); 
            }else{
                osSemaphoreRelease(wk2xxx_idle_lockHandle ); 

            }
            log_i(" deal buf :   %d " , wk2114->size_received);
            log_i(" deal buf :   %02X %02X %02X %02X %02X %02X %02X %02X" , 
                        wk2xxx_rcvbuf[0], wk2xxx_rcvbuf[1], wk2xxx_rcvbuf[2], wk2xxx_rcvbuf[3], wk2xxx_rcvbuf[4], wk2xxx_rcvbuf[5], wk2xxx_rcvbuf[6], wk2xxx_rcvbuf[7] );

            // TODO 接收数量为0  ，FSR判断是否有数据再处理
            if (fifo_readout_count == 0 )  {
                HAL_UART_DMAStop(pWK2114Uart);
                *state = WK2114_WORK_STATE_EXTI_READ_SUB_FSR;
            }
            else{
                *state = WK2114_WORK_STATE_EXTI_READ_FIFO_BY_COUNT;
            }
            continue;
        }

        case WK2114_WORK_STATE_EXTI_READ_SUB_FSR:
            log_e("WK2114_WORK_STATE_EXTI_READ_SUB_FSR  FIFO  Status ....  ");

            p_sub_uart->cmd = WK2114_S_REG_FSR;
            cmd = WK2114_S_REG_FSR;
            osSemaphoreRelease(wk2xxx_idle_lockHandle );
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, osWaitForever);
            wk2114_read_subuart_reg( wk2114, wk2114->it_sub_uart_index, p_sub_uart->cmd ); 
            // log_e("WK2114_S_REG_  FSR ....  0x%02X", wk2xxx_rcvbuf[0]);
            *state = WK2114_WORK_STATE_EXTI_READ_SUB_LSR;
            break;
        case WK2114_WORK_STATE_EXTI_READ_SUB_LSR:
            log_e("WK2114_WORK_STATE_EXTI_READ_SUB_FSR  FIFO  Status ....  ");
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, 0xFF);
            HAL_UART_DMAStop(pWK2114Uart);
            osSemaphoreRelease(wk2xxx_idle_lockHandle );  
            log_d("WK2114_S_REG_  FSR ....  0x%02X", wk2xxx_rcvbuf[0]);

            p_sub_uart->cmd = WK2114_S_REG_LSR ;
            cmd = WK2114_S_REG_LSR;
            osSemaphoreRelease(wk2xxx_idle_lockHandle );
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, osWaitForever);
            wk2114_read_subuart_reg( wk2114, wk2114->it_sub_uart_index, p_sub_uart->cmd ); 
            // log_e("WK2114_S_REG_  LSR ....  0x%02X", wk2xxx_rcvbuf[0]);
            *state = WK2114_WORK_STATE_EXTI_DEAL_LSR;
            break;
        case WK2114_WORK_STATE_EXTI_DEAL_LSR:
            log_e("WK2114_WORK_STATE_EXTI_READ_SUB_FSR  FIFO  Status ....  ");
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, 0xFF);
            HAL_UART_DMAStop(pWK2114Uart);
            osSemaphoreRelease(wk2xxx_idle_lockHandle );  
            log_d("WK2114_S_REG_  LSR ....  0x%02X", wk2xxx_rcvbuf[0]);
 
            *state = WK2114_WORK_STATE_EXTI_READ_REG;
            break;

        case WK2114_WORK_STATE_EXTI_READ_REG:
            log_d(" read reg for check  ....  ");

            //  写入软复位
            osSemaphoreRelease(wk2xxx_idle_lockHandle );
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, osWaitForever);
            wk2114->cmd = WK2114_G_REG_GRST ;
            wk2114_write_global_reg( wk2114, wk2114->cmd, 0x0F ); 
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, 0xFF);

            osSemaphoreRelease(wk2xxx_idle_lockHandle );
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, osWaitForever);
            wk2114->cmd = WK2114_G_REG_GENA ;
            wk2114_read_global_reg( wk2114, wk2114->cmd ); 
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, 0xFF);
            log_d("WK2114 subuart enable ....  0x%02X", wk2xxx_rcvbuf[0]);

            osSemaphoreRelease(wk2xxx_idle_lockHandle );
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, osWaitForever);
            wk2114->cmd = WK2114_G_REG_GIER ;
            wk2114_read_global_reg( wk2114,  wk2114->cmd );  
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, 0xFF);
            log_d("WK2114 subuart  it enable ....  0x%02X", wk2xxx_rcvbuf[1]);

            osSemaphoreRelease(wk2xxx_idle_lockHandle );
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, osWaitForever);
            wk2114->cmd = WK2114_G_REG_GRST ;
            wk2114_read_global_reg( wk2114,  wk2114->cmd );  
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, 0xFF);
            log_d("WK2114 subuart GRST  ....  0x%02X", wk2xxx_rcvbuf[2]);

            osSemaphoreRelease(wk2xxx_idle_lockHandle );
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, osWaitForever);
            wk2114->cmd = WK2114_S_REG_SIER ;
            wk2114_read_subuart_reg( wk2114, wk2114->it_sub_uart_index, wk2114->cmd );  
            osSemaphoreAcquire(wk2xxx_idle_lockHandle, 0xFF);
            log_d("WK2114 subuart it type ....  0x%02X", wk2xxx_rcvbuf[3]);  // 8B 发送触点中断确实被取消了
            HAL_UART_DMAStop(pWK2114Uart);
            osSemaphoreRelease(wk2xxx_idle_lockHandle );  

            // 子串口软复位


            *state = WK2114_WORK_STATE_OK;
            break;

        case WK2114_WORK_STATE_ERROR:
            log_e("WK2114_WORK STATE  ERROR ....  ");
            // TODO  重设Baud
            // HAL_UART_DMAStop(pWK2114Uart);
            osDelay(500);
            // *state = WK2114_WORK_STATE_WAIT;
            break;
        case WK2114_WORK_STATE_STOP:
            log_e("WK2114_WORK_STATE   STOP   ....  ");
            HAL_UART_DMAStop(pWK2114Uart);
            *state = WK2114_WORK_STATE_WAIT;
            break;
        default:
            break;
        }
        osDelay(1 );

    }

}

void wk2114_FERR_INT_Handler(WK2114_TypeDef *wk2114,  uint8_t it_type)
{

}
void wk2114_RXOVT_INT_Handler(WK2114_TypeDef *wk2114,  uint8_t it_type)
{

}
void wk2114_RFTRIG_INT_Handler(WK2114_TypeDef *wk2114,  uint8_t it_type)
{

}
void wk2114_TFEMPTY_INT_Handler(WK2114_TypeDef *wk2114,  uint8_t it_type)
{

}
void wk2114_TFTRIG_INT_Handler(WK2114_TypeDef *wk2114,  uint8_t it_type)
{

}
// void WK2114_Task2(void * argument )
// {
//     WK2114_WORK_STATE_TypeDef *state = argument;
//     static int err_con = 0;
//     static int state_ok_count = 0; 
//     static uint8_t cmd;
//     static uint8_t sub_uart_index;
//     static uint8_t evt_flag = 0; 
//     static uint32_t fifo_readout_count = 0; 
//     static uint32_t subuart_sendqueue_count = 0; 
//     static WK2114_Sub_Uart_TypeDef *p_sub_uart;
//     for ( ;  ;  )
//     {
//         // log_i( "WK2114 WORK  ....  %d", *state );
//         switch (*state)
//         {
//         case WK2114_WORK_STATE_WAIT:
 
//             log_i( "WK2114_wait  .... %d" );
//             osDelay(100);
//             break;
//         case WK2114_WORK_STATE_START:
//             err_con = 0;
//             wk2114->timebase    = osKernelGetTickCount();
//             // wk2114_set_baud(wk2114);
//             *state = WK2114_WORK_STATE_SET_BAUD;
//             continue;
//         case WK2114_WORK_STATE_SET_BAUD:
//         {
//             log_i( "WK2114_WORK_STATE_SET_BAUD .... %d", wk2114->flag & WK2114_G_FLAG_MASK_SENDING);
//             wk2114->timebase    = osKernelGetTickCount();
//             HAL_GPIO_WritePin(WK2XXX_IRQ_GPIO_PORT, WK2XXX_RST_PIN,GPIO_PIN_RESET);
//             osDelay(10);
//             HAL_GPIO_WritePin(WK2XXX_IRQ_GPIO_PORT, WK2XXX_RST_PIN,GPIO_PIN_SET);
//             wk2114_set_baud(wk2114);
//             osDelay(2);
//             wk2114_set_baud(wk2114);
//             osDelay(2);
//             wk2114_set_baud(wk2114);
//             osDelay(2);
//             *state = WK2114_WORK_STATE_ADD_SUB_UART;
//             continue;
//         }

//         case WK2114_WORK_STATE_ADD_SUB_UART:
//         {
//             // log_i( "WK2114_WORK_STATE_SUB_UART_REGISTER  Init .... ");
//             if ( (wk2114->flag & WK2114_G_FLAG_MASK_SENDING) == 0 )
//             {
//                 err_con = 0;
//                 wk2114->timebase    = osKernelGetTickCount();
//                 WK2114_Add_Sub_Uart( wk2114  );
//                 *state = WK2114_WORK_STATE_ADD_SUB_UART_OK;  
//                 osDelay(2); 
//                 continue;; 
//             } 
//             break;
//         }

//         case WK2114_WORK_STATE_ADD_SUB_UART_OK:
//         {
//             // log_i( "WK2114_WORK_STATE_SUB_UART_REGISTER  Init .... ");
//             if ( osMessageQueueGetCount(wk2114->cmd_queue) > 0 && (wk2114->flag & WK2114_G_FLAG_MASK_SENDING) == 0)
//             {
//                 wk2114_send(wk2114);
//                 // osDelay(1);
//             }
//             if ( osMessageQueueGetCount(wk2114->cmd_queue) == 0 && (wk2114->flag & WK2114_G_FLAG_MASK_SENDING) == 0)
//             {
//                 *state=WK2114_WORK_STATE_OK;
//                 continue;
//             }
//             break;
//         }

//         case WK2114_WORK_STATE_OK:
//         {
//             log_i( "WK2114_WORK_STATE_OK  -->> wait exti ... " );
//             // 等待外部中断
//             uint32_t evt_flag = osEventFlagsWait(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_IT_SOURCE_SENDING,0, portMAX_DELAY);
//             if (evt_flag &MODULE_WK2114_EVENT_EXTI_IT_SOURCE_SENDING != 0) {
//                 log_d( " WK2114 IRQ  exti  happened .....   "  );
//                 *state= WK2114_WORK_STATE_EXTI_IT_SOURCE_GETTING;
//             }
//             // 等待子串口的size>0
//             continue;
//         }


//         // 外部中断
//         case WK2114_WORK_STATE_EXTI_IT_SOURCE_GETTING:
//         {
//             log_i( "    IT_SOURCE getting ....   "   );
//             osEventFlagsClear(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_IT_SOURCE_SENDING);
//             err_con = 0;
//             wk2114->size_received=0;

//             // wk2114->flag|=WK2114_G_FLAG_MASK_INTER;
//             wk2114->send_buf[0] = (WK2114_CMD_READ_REG<<6)+WK2114_G_REG_GIFR;
//             wk2114->send_buf[1] = 0x00;
//             // log_i( "    IT_SOURCE SENDING.... sendbuf  %02X " , wk2114->send_buf[0]  );

//             // // 开启接收中断
//             memset(wk2xxx_rcvbuf, 0, 1024);
//             __HAL_UART_ENABLE_IT(pWK2114Uart, UART_IT_IDLE);
//             HAL_UART_Receive_DMA(pWK2114Uart, wk2xxx_rcvbuf, 1024);
//             // HAL_UARTEx_ReceiveToIdle_DMA( pWK2114Uart, wk2xxx_rcvbuf, 1024);
//             wk2114->send_fun(wk2114->send_class, wk2114->send_buf , 2);
//             // wk2114_read_reg( wk2114, WK2114_G_REG_GIFR );
//             p_sub_uart =NULL;
//             *state = WK2114_WORK_STATE_EXTI_IT_SOURCE_RECEIVE ;
//             continue;           
//         }

//         case WK2114_WORK_STATE_EXTI_IT_SOURCE_RECEIVE:
//         {
//             evt_flag = osEventFlagsWait(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_IT_SOURCE_RECEIVED,0, 0x200);
//             if (evt_flag & MODULE_WK2114_EVENT_EXTI_IT_SOURCE_RECEIVED == 0) {
//                     log_i( "  Get It SOurce ..... TimeOut  "  );
//                     *state = WK2114_WORK_STATE_ERROR;
//                     continue;
//             }

//             // log_i(" it source reg >>>>>>>>>>>>>>>  %02X ", wk2xxx_rcvbuf[0] );

//             // 处理接收到的中断源数据 
//             cmd = wk2114->send_buf[0];  // 上次发送buf第一位 send_buf[0]
//             wk2114->send_buf[0] = 0;

//             if( wk2114->size_received == 1 ){
//                 HAL_UART_DMAStop(pWK2114Uart);
//             }

//             if( !WK2114_CMD_IS_READ(cmd) ){  
//                 *state=WK2114_WORK_STATE_ERROR;
//              }

//             if ( !WK2114_CMD_IS_REG(cmd)) {
//                 *state=WK2114_WORK_STATE_ERROR;
//             }

//             if(!WK2114_REG_IS_GLOBAL(cmd)){ 
//                 *state=WK2114_WORK_STATE_ERROR;
//             }  

//             if(wk2114->cmd!=0xff){
//                 *state=WK2114_WORK_STATE_ERROR;
//             }

//             wk2114->cmd=cmd;
//             wk2114->reg= wk2xxx_rcvbuf[0];
//             wk2114->it_sub_uart_index = wk2114->reg&0x03 -1;
//             p_sub_uart = wk2114->sub_uart[wk2114->it_sub_uart_index];
//             // wk2114->it_sub_uart  = wk2114->sub_uart[wk2114->it_sub_uart_index];

//             if( (wk2114->reg&0x0F)==0 ) {
//                 *state = WK2114_WORK_STATE_OK;
//             }else{
//                 wk2114->it_sub_uart_index = wk2114->reg&0x03 -1;
//                 p_sub_uart = wk2114->sub_uart[wk2114->it_sub_uart_index];
//                 // wk2114->it_sub_uart  = wk2114->sub_uart[wk2114->it_sub_uart_index];              
//                 *state = WK2114_WORK_STATE_EXTI_IT_TYPE_GETTING;
//             }
//             continue;
//         }

//         case WK2114_WORK_STATE_EXTI_IT_TYPE_GETTING:
//         {
//             log_i( "_IT_TYPE getting  ....  last rcv it_source --> subuart 0x%02X" ,  wk2114->reg);
//             osEventFlagsClear(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_IT_SOURCE_RECEIVED );

//             wk2114->send_buf[0] = (WK2114_CMD_READ_REG<<6) 
//                                     + (wk2114->it_sub_uart_index<<4) 
//                                     + WK2114_S_REG_SIFR ;
//             wk2114->send_buf[1] = 0x00;
//             // // 开启接收中断
//             memset(wk2xxx_rcvbuf, 0, 1024);
//             __HAL_UART_ENABLE_IT(pWK2114Uart, UART_IT_IDLE );
//             HAL_UART_Receive_DMA(pWK2114Uart, wk2xxx_rcvbuf, 1024);
//             // HAL_UARTEx_ReceiveToIdle_DMA( pWK2114Uart, wk2xxx_rcvbuf, 1024);

//             // wk2114_read_reg(wk2114, WK2114_BUILD_SUB_REG( wk2114->it_sub_uart_index, WK2114_S_REG_SIFR) );
//             wk2114->send_fun( wk2114->send_class, wk2114->send_buf , 2 );

//             *state = WK2114_WORK_STATE_EXTI_IT_TYPE_RECEIVE;
//             continue;
//         }

//         case WK2114_WORK_STATE_EXTI_IT_TYPE_RECEIVE:
//         {
//             log_d( "_IT_TYPE  _RECEIVE ....  "  );
            
//             evt_flag = osEventFlagsWait( wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_IT_TYPE_RECEIVED, 0, 0x800 );
//             if ( (evt_flag & MODULE_WK2114_EVENT_EXTI_IT_TYPE_RECEIVED) == 0 ) {
//                 log_w( "  GET IT TYPE ..... TimeOut ..... "  );
//                 *state = WK2114_WORK_STATE_ERROR;
//                 continue;
//             }
//             // if (p_sub_uart==NULL ) {
//             //     log_w( "  GET IT TYPE ..... p_sub_uart==NULL  "  );
//             //     *state = WK2114_WORK_STATE_ERROR;
//             // } 

//             // log_d("_IT_TYPE reg >>>>>>>>>>>>>>>  cmd  0x%02X  ittype 0x%02X ", wk2114->send_buf[0], wk2xxx_rcvbuf[0] );

//             // cmd = wk2114->send_buf[0];      // 上次发送内容第一位 send_buf[0]
//             // wk2114->send_buf[0]     = 0;
//             // p_sub_uart->cmd         = wk2114->send_buf[0];
//             // p_sub_uart->reg         = wk2xxx_rcvbuf[0]; 

//             // log_d("_IT_TYPE reg >>>>>>>>>>>>>>>  cmd  0x%02X  ittype 0x%02X  cmd 0x%02X", wk2114->send_buf[0], wk2xxx_rcvbuf[0] ,cmd);

//             // if( wk2114->size_received == 1 ){
//             //     HAL_UART_DMAStop(pWK2114Uart);
//             // }

//             // if( !WK2114_CMD_IS_READ(cmd) ){  
//             //     *state=WK2114_WORK_STATE_ERROR;
//             // }

//             // if ( !WK2114_CMD_IS_REG(cmd) ) {
//             //     *state=WK2114_WORK_STATE_ERROR;
//             // }

//             // if( WK2114_REG_IS_GLOBAL(cmd) ){ 
//             //     *state=WK2114_WORK_STATE_ERROR;
//             // }  

//             // if( wk2114->cmd!=0xff ){
//             //     *state=WK2114_WORK_STATE_ERROR;
//             // }

//             *state = WK2114_WORK_STATE_EXTI_PROCESS;
//             continue;
//         }

//         case WK2114_WORK_STATE_EXTI_PROCESS:
//         {
//             log_d("_IT_ PROCESS .... p_sub_uart->reg (it type) 0x%02X p_sub_uart->cmd 0x%02X",
//                          p_sub_uart->reg, p_sub_uart->cmd);

//             if (p_sub_uart==NULL ) {
//                 *state = WK2114_WORK_STATE_ERROR;
//             }
//             // if ( (p_sub_uart->flag&WK2114_S_UART_FLAG_MASK_PAGE_D)==0 ) {
//             //     *state = WK2114_WORK_STATE_ERROR;
//             // }

//             // WK2114_S_REG_SIFR 子串口中断标志寄存器
//             // if(WK2114_CMD_GET_ADDR(p_sub_uart->cmd)!=WK2114_BUILD_SUB_REG(wk2114->it_sub_uart_index,WK2114_S_REG_SIFR)){ 
//             //     *state=WK2114_WORK_STATE_ERROR;
//             // }

//             // log_d("_IT_ type ....  %d  state  %d", p_sub_uart->reg,*state );

//             // // 接收错误中断
//             // if( (p_sub_uart->reg&WK2114_S_REG_SIFR_MASK_FERR_INT)!=0 ){   
//             //     *state = WK2114_WORK_STATE_EXTI_PROCESS_FERR_INT;
//             //     continue;
//             // }
//             // //  接收字节超时中断  读取字节数
//             // if( (p_sub_uart->reg&WK2114_S_REG_SIFR_MASK_RXOVT_INT)!=0 ){   
//             //     *state = WK2114_WORK_STATE_EXTI_PROCESS_RXOVT_INT;
//             //     continue;
//             // }
//             // // 接收FIFO触点中断        
//             // if( (p_sub_uart->reg&WK2114_S_REG_SIFR_MASK_RFTRIG_INT)!=0 ){ 
//             //     *state = WK2114_WORK_STATE_EXTI_PROCESS_RFTRIG_INT;
//             //     continue;
//             // }
//             // // FIFO触点发送为空中断 发送子串口队列
//             // if( (p_sub_uart->reg&WK2114_S_REG_SIFR_MASK_TFEMPTY_INT)!=0 
//             //         && wk2114->sub_uart[wk2114->it_sub_uart_index]->send_buf_p==0xffffffff ) { 
//             //     *state = WK2114_WORK_STATE_EXTI_PROCESS_TFEMPTY_INT;
//             //     continue;
//             // }
//             // // 发送FIFO触点中断 发送同一个字符串
//             // // if((p_sub_uart->reg&WK2114_S_REG_SIFR_MASK_TFTRIG_INT)!=0){  
//             // if( (p_sub_uart->reg&WK2114_S_REG_SIFR_MASK_TFTRIG_INT)!=0 ){  
//             //     log_d("_IT_ type ....  %d", p_sub_uart->reg );
//             //     // *state = WK2114_WORK_STATE_EXTI_PROCESS_TFTRIG_INT;
//             //     continue;
//             // }

//             continue;
//         }

//         case WK2114_WORK_STATE_EXTI_PROCESS_RXOVT_INT:
//         {
//             log_w(" Rcv Fifo Byte Overtime Int ....  ");
//             wk2114->send_buf[0] = (WK2114_CMD_READ_REG<<6) 
//                                     + (wk2114->it_sub_uart_index<<4) 
//                                     + WK2114_S_REG_RFCNT ;
//             wk2114->send_buf[1] = 0x00;
//             // // 开启接收中断
//             __HAL_UART_ENABLE_IT(pWK2114Uart, UART_IT_IDLE );
//             HAL_UART_Receive_DMA(pWK2114Uart, wk2xxx_rcvbuf, 1024);
//             // HAL_UARTEx_ReceiveToIdle_DMA( pWK2114Uart, wk2xxx_rcvbuf, 1024);

//             // wk2114_read_reg( wk2114, WK2114_BUILD_SUB_REG( wk2114->it_sub_uart_index, WK2114_S_REG_RFCNT) );
//             wk2114->send_fun( wk2114->send_class, wk2114->send_buf , 2 );
//             *state = WK2114_WORK_STATE_EXTI_PROCESS_GET_FIFO_COUNT;
//             continue;
//         }


//         case WK2114_WORK_STATE_EXTI_PROCESS_RFTRIG_INT:
//             log_i("Rcv Fifo Trigger .... read rcv fifo count ");
//             // // 开启接收中断
//             __HAL_UART_ENABLE_IT(pWK2114Uart, UART_IT_IDLE );
//             HAL_UART_Receive_DMA(pWK2114Uart, wk2xxx_rcvbuf, 1024);
//             // HAL_UARTEx_ReceiveToIdle_DMA( pWK2114Uart, wk2xxx_rcvbuf, 1024);
//             wk2114_read_fifo( wk2114,   wk2114->it_sub_uart_index, 128-1  );
//             *state = WK2114_WORK_STATE_EXTI_READ_FIFO_BY_COUNT;
//             continue;

//         case WK2114_WORK_STATE_EXTI_PROCESS_TFEMPTY_INT:
//             log_w("TF Fifo Empty Int....  ");
//             if((p_sub_uart->reg&WK2114_S_REG_SIFR_MASK_TFEMPTY_INT)!=0 && p_sub_uart->send_buf_p==0xffffffff){ 
//                 log_w(" send fifo trigger == 0");
//                 p_sub_uart->send_buf_p=0;
//                 uint8_t queue_count = osMessageQueueGetCount(p_sub_uart->send_queue);
//                 // 判断 子串口的 发送队列是否为空
//                 // p_de=queue_get_de_index(&p_sub_uart->send_queue);
//                 // if(p_de>=0){
//                 if (osMessageQueueGetCount(p_sub_uart->send_queue) >= 0){
//                     if(p_sub_uart->send_record[queue_count-1].size-p_sub_uart->send_buf_p <= 256){
//                         struct wk2114_send_record  record;
//                         osMessageQueueGet(p_sub_uart->send_queue,(void*) &record,sizeof(record),portMAX_DELAY);
//                         wk2114_write_fifo(wk2114,sub_uart_index,record.p_data[p_sub_uart->send_buf_p],record.size-p_sub_uart->send_buf_p-1);
//                         // wk2114_write_fifo(wk2114,i,&p_sub_uart->send_record[p_de].p_data[p_sub_uart->send_buf_p],p_sub_uart->send_record[p_de].size-p_sub_uart->send_buf_p-1);
//                         p_sub_uart->send_buf_p=0xffffffff;
//                         // queue_dequeue(&p_sub_uart->send_queue,NULL);
//                     }
//                     else{  // buff 大于256， 发送 256, 记录发送位置  p_sub_uart->send_buf_p
//                         wk2114_write_fifo(wk2114,sub_uart_index,&p_sub_uart->send_record[queue_count-1].p_data[p_sub_uart->send_buf_p],255);
//                         wk2114_sub_uart_set_inter(p_sub_uart,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);
//                         p_sub_uart->send_buf_p+=256;
//                     }
//                 }
//                 else{
//                     // 子串口队列无发送任务，取消发送出点为空中断
//                     wk2114_sub_uart_set_inter(p_sub_uart,WK2114_S_REG_SIFR_MASK_RFTRIG_INT|WK2114_S_REG_SIFR_MASK_RXOVT_INT|WK2114_S_REG_SIFR_MASK_FERR_INT);
//                 *state = WK2114_WORK_STATE_STOP;
//                 }
//             }
//             continue;
//         case WK2114_WORK_STATE_EXTI_PROCESS_TFTRIG_INT:
//             log_w("Trans Fifo Trigger Int....  ");
//             subuart_sendqueue_count = osMessageQueueGetCount(p_sub_uart->send_queue);
//             if (subuart_sendqueue_count >= 0){
//                 if(p_sub_uart->send_buf_p >= p_sub_uart->send_record[subuart_sendqueue_count-1].size){
//                     wk2114_sub_uart_set_inter(p_sub_uart,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);
//                     p_sub_uart->send_buf_p=0xffffffff;
//                     return;
//                 }
//                 if(p_sub_uart->send_record[subuart_sendqueue_count-1].size-p_sub_uart->send_buf_p <= 128){
//                     struct wk2114_send_record record;
//                     osMessageQueueGet(p_sub_uart->send_queue,(void*) &record,sizeof(record),portMAX_DELAY);
//                     wk2114_write_fifo(wk2114,sub_uart_index,record.p_data[p_sub_uart->send_buf_p],record.size-p_sub_uart->send_buf_p-1);
                    
//                     // wk2114_write_fifo(wk2114,i,&p_sub_uart->send_record[p_de].p_data[p_sub_uart->send_buf_p],p_sub_uart->send_record[p_de].size-p_sub_uart->send_buf_p-1);
//                     p_sub_uart->send_buf_p=0xffffffff;
//                     wk2114_sub_uart_set_inter(p_sub_uart,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(&p_sub_uart->send_queue,NULL);
//                 }
//                 else{
//                     wk2114_write_fifo(wk2114,sub_uart_index,&p_sub_uart->send_record[subuart_sendqueue_count-1].p_data[p_sub_uart->send_buf_p],127);
//                     p_sub_uart->send_buf_p+=128;
//                 }
//             }
//             else{
//                 wk2114_sub_uart_set_inter(p_sub_uart,WK2114_S_REG_SIFR_MASK_RFTRIG_INT|WK2114_S_REG_SIFR_MASK_RXOVT_INT|WK2114_S_REG_SIFR_MASK_FERR_INT);
//                 *state = WK2114_WORK_STATE_STOP;
//             }
 
//             break;
//         case WK2114_WORK_STATE_EXTI_PROCESS_FERR_INT:
//             log_e("Fifo Error ....  ");
//             __HAL_UART_ENABLE_IT(pWK2114Uart, UART_IT_IDLE );
//             HAL_UART_Receive_DMA(pWK2114Uart, &wk2114->reg_readout, 1 );
//             // HAL_UARTEx_ReceiveToIdle_DMA( pWK2114Uart, wk2xxx_rcvbuf, 1024);
//             wk2114_read_subuart_reg( wk2114,  wk2114->it_sub_uart_index, WK2114_S_REG_LSR  );
//             *state = WK2114_WORK_STATE_STOP;
//             break;

//         case WK2114_WORK_STATE_EXTI_PROCESS_GET_FIFO_COUNT:
//         {
//             log_e("Read Fifo BUF ....  ");
//             evt_flag = osEventFlagsWait(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_RCV_FIFO_COUNT_RECEIVED,0, 0x200);
//             if ( (evt_flag & MODULE_WK2114_EVENT_EXTI_IT_TYPE_RECEIVED) == 0) {
//                 log_i( "  GET _FIFO_COUNT ..... TimeOut  "  );
//                 *state = WK2114_WORK_STATE_ERROR;
//                 continue;
//             }

//             log_e(" _FIFO_COUNT ....  %d ", wk2xxx_rcvbuf[0] );

//             cmd = wk2114->send_buf[0];  // 上次发送内容第一位 send_buf[0]
//             wk2114->send_buf[0] = 0;
//             if( wk2114->size_received == 1 ){
//                 HAL_UART_DMAStop(pWK2114Uart);
//             }

//             if( !WK2114_CMD_IS_READ(cmd) ){  
//                 *state=WK2114_WORK_STATE_ERROR;
//             }

//             if ( !WK2114_CMD_IS_REG(cmd)) {
//                 *state=WK2114_WORK_STATE_ERROR;
//             }

//             if(WK2114_REG_IS_GLOBAL(cmd)){ 
//                 *state=WK2114_WORK_STATE_ERROR;
//             }  

//             if(wk2114->cmd!=0xff){
//                 *state=WK2114_WORK_STATE_ERROR;
//             }
//             wk2114->it_sub_uart->cmd = cmd;
//             wk2114->it_sub_uart->reg = wk2xxx_rcvbuf[0];   
//             fifo_readout_count =   wk2114->it_sub_uart->reg;  
//             memset( wk2xxx_rcvbuf, 0, 1024 );
//             osEventFlagsClear(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_RCV_FIFO_COUNT_RECEIVED);

//             *state = WK2114_WORK_STATE_EXTI_READ_FIFO_BY_COUNT;
//             continue;
//         }
//         case WK2114_WORK_STATE_EXTI_READ_FIFO_BY_COUNT:
//             log_e( "Read Fifo BUF  _BY_COUNT .... "   );
 
//             wk2114->send_buf[1] = 0x00;
//             // memset( wk2xxx_rcvbuf, 0, 1024 );
//             __HAL_UART_ENABLE_IT(pWK2114Uart, UART_IT_IDLE );
//             HAL_UART_Receive_DMA(pWK2114Uart, wk2xxx_rcvbuf, 1024);

//             if (fifo_readout_count > 16 )  {
//                 wk2114->send_buf[0] = (WK2114_CMD_READ_FIFO<<6) 
//                                         + (wk2114->it_sub_uart_index<<4) 
//                                         + (16-1) ;  
//                 fifo_readout_count -= 16;   
//                 log_w( "Fifo read out %d", fifo_readout_count );           
//             }  else{
//                 wk2114->send_buf[0] = (WK2114_CMD_READ_FIFO<<6) 
//                                         + (wk2114->it_sub_uart_index<<4) 
//                                         + (fifo_readout_count-1) ; 
//                 fifo_readout_count = 0;               
//                 log_w( "Fifo read out send last byte"  );           
//             } 

//             // wk2114_read_fifo( wk2114,   wk2114->it_sub_uart_index, wk2114->reg_readout  );
//             wk2114->send_fun( wk2114->send_class, wk2114->send_buf , 2 );

//         case WK2114_WORK_STATE_EXTI_READ_FIFO_DEAL:
//         {
//             log_e( "Read Fifo BUF ...  Deal FIFO BUF "   );
//             evt_flag = osEventFlagsWait(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_RCV_FIFO_BUF_RECEIVED,0, 0x200);
//             if ( (evt_flag & MODULE_WK2114_EVENT_EXTI_RCV_FIFO_BUF_RECEIVED) == 0) {
//                 log_i( "  GET _FIFO _BUF _RECEIVED ... TimeOut ... "  );
//                 *state = WK2114_WORK_STATE_ERROR;
//                 continue;
//             }

//             // TODO  deal buf,  数量大于约定， 没有数据了，回调
//             log_i(" deal buf :   %d " , wk2114->size_received);
//             log_i(" deal buf :   %02X %02X %02X %02X %02X %02X %02X %02X" , 
//                         wk2xxx_rcvbuf[0], wk2xxx_rcvbuf[1], wk2xxx_rcvbuf[2], wk2xxx_rcvbuf[3], wk2xxx_rcvbuf[4], wk2xxx_rcvbuf[5], wk2xxx_rcvbuf[6], wk2xxx_rcvbuf[7] );

//             if (fifo_readout_count == 0 )  {
//                 *state = WK2114_WORK_STATE_STOP;
//             }
//             else{
//                 *state = WK2114_WORK_STATE_EXTI_READ_FIFO_BY_COUNT;
//             }
//             osEventFlagsClear(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_RCV_FIFO_BUF_RECEIVED );

//             continue;
//         }

//         case WK2114_WORK_STATE_ERROR:
//             log_e("WK2114_WORK STATE  ERROR ....  ");
//             HAL_UART_DMAStop(pWK2114Uart);
//             *state = WK2114_WORK_STATE_WAIT;
//             break;

//         case WK2114_WORK_STATE_STOP:
//             log_e("WK2114_WORK   STATE  STOP ... ");
//             HAL_UART_DMAStop(pWK2114Uart);
//             *state = WK2114_WORK_STATE_OK;
//             continue;
                
//         default:
//             break;
//         }
//         osDelay(1);
//     }
    
// }


// /**
//  * @brief wk2114中断回调
//  * @note 不要将此函数放入中断中或比其他回调函数优先级更高的任务中
//  * @param wk2114 WK2114_TypeDef 结构指针
//  * 外部中断, 
//  */
// void wk2114_inter_callback(WK2114_TypeDef *wk2114)
// {
//     // if(  (osEventFlagsGet(wk2114EventHandle) &  MODULE_WK2114_EVENT_EXTI_IT_SOURCE_SENDING) !=0){
//     //     return;
//     // }
//     osEventFlagsSet(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_IT_SOURCE_SENDING); 
//     // log_i( "   exti  ..... %d  ",osKernelGetTickCount() );

// }

// void wk2114_idle_callback(WK2114_TypeDef *wk2114, uint8_t *buf, uint32_t size)
// {
//     // log_w( " wk2114_idle_callback   ....  %d ", wk2114->state );
//     wk2114->size_received = size;
//     switch (wk2114->state)
//     {
//         // case WK2114_WORK_STATE_EXTI_IT_SOURCE_GETTING:
//         case WK2114_WORK_STATE_EXTI_IT_SOURCE_RECEIVE:
//             osEventFlagsSet( wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_IT_SOURCE_RECEIVED );
//             return; 
//         // case WK2114_WORK_STATE_EXTI_IT_TYPE_GETTING:
//         case WK2114_WORK_STATE_EXTI_IT_TYPE_RECEIVE:
//             osEventFlagsSet( wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_IT_TYPE_RECEIVED );
//             log_i( " it type idle ... " );
//             return;          
//         case WK2114_WORK_STATE_EXTI_PROCESS_GET_FIFO_COUNT:
//             osEventFlagsSet( wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_RCV_FIFO_COUNT_RECEIVED );
//             return;    
//         case WK2114_WORK_STATE_EXTI_READ_FIFO_DEAL:
//             osEventFlagsSet( wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_RCV_FIFO_BUF_RECEIVED );
//             return;    
//         default:
//             break;
//     }
//     return;
// }

// void wk2114_get_it_type_by_it_source(WK2114_TypeDef *wk2114 )
// {
//     //  TODO 中断源同时为多个 ？？ 先依序发送出去
//     // log_i("get_it_type_by_it_source....it_source ->  %d",   wk2114->it_source );
 
//     // if(WK2114_CMD_GET_ADDR(wk2114->cmd)==WK2114_G_REG_GIFR){  // 去读子串口中断类型
//     //     wk2114->cmd=0;
//     //     if((wk2114->it_source&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->it_source&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->it_source&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->it_source&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));
//     //     }
//     // }

//     // while (osMessageQueueGetCount(wk2114->cmd_queue) > 0)
//     // {
//     //     wk2114_send(wk2114);
//     // }

//     // return;

// }


// void wk2114_process_by_it_type(WK2114_TypeDef *wk2114,  uint8_t it_type)
// {
//     int sub_uart_index ;
//     int p_de;    
//     log_i( " wk2114_process_by_it_type ....  ");

//     WK2114_Sub_Uart_TypeDef *p_sub_uart;
//     sub_uart_index = WK2114_CMD_GET_SUB_UART(wk2114->cmd);
//     p_sub_uart = wk2114->sub_uart[WK2114_CMD_GET_SUB_UART(wk2114->cmd)];
//     // 判断中断类型

//     if (p_sub_uart!=NULL ) return ;
//     if ((p_sub_uart->flag&WK2114_S_UART_FLAG_MASK_PAGE_D)==0) return ;

//     // WK2114_S_REG_SIFR 子串口中断标志寄存器
//     if(WK2114_CMD_GET_ADDR(p_sub_uart->cmd)==WK2114_BUILD_SUB_REG(sub_uart_index,WK2114_S_REG_SIFR)){  
//         p_sub_uart->cmd=0;

//         //  接收超时 
//         if( (p_sub_uart->reg&WK2114_S_REG_SIFR_MASK_RXOVT_INT)!=0 ){   
//             log_i(" 接收FIFO overtime it ... ");
//             p_sub_uart->flag|=WK2114_S_UART_FLAG_MASK_IDLE;
//             wk2114_read_reg(wk2114,WK2114_BUILD_SUB_REG(sub_uart_index,WK2114_S_REG_RFCNT));
//         }

//         // 接收FIFO触点中断        
//         if((p_sub_uart->reg&WK2114_S_REG_SIFR_MASK_RFTRIG_INT)!=0){ 
//             log_i(" 接收FIFO触点中断 ... ");
//             wk2114_read_fifo(wk2114, wk2114->cmd, 126 );
//         }

//         // 发送FIFO空中断 , 判断子串口发送队列，将子串口send_buf 发送出去
//         if((p_sub_uart->reg&WK2114_S_REG_SIFR_MASK_TFEMPTY_INT)!=0 && p_sub_uart->send_buf_p==0xffffffff){ 
//             log_w(" send fifo trigger == 0");
//             p_sub_uart->send_buf_p=0;
//             // 判断 子串口的 发送队列是否为空
//             // p_de=queue_get_de_index(&p_sub_uart->send_queue);
//             // if(p_de>=0){
//             if (osMessageQueueGetCount(p_sub_uart->send_queue) >= 0){
//                 if(p_sub_uart->send_record[p_de].size-p_sub_uart->send_buf_p <= 256){
//                     struct wk2114_send_record  record;
//                     osMessageQueueGet(p_sub_uart->send_queue,(void*) &record,sizeof(record),portMAX_DELAY);
//                     wk2114_write_fifo(wk2114,sub_uart_index,record.p_data[p_sub_uart->send_buf_p],record.size-p_sub_uart->send_buf_p-1);
//                     // wk2114_write_fifo(wk2114,i,&p_sub_uart->send_record[p_de].p_data[p_sub_uart->send_buf_p],p_sub_uart->send_record[p_de].size-p_sub_uart->send_buf_p-1);
//                     p_sub_uart->send_buf_p=0xffffffff;
//                     // queue_dequeue(&p_sub_uart->send_queue,NULL);
//                 }
//                 else{  // buff 大于256， 发送 256, 记录发送位置  p_sub_uart->send_buf_p
//                     wk2114_write_fifo(wk2114,sub_uart_index,&p_sub_uart->send_record[p_de].p_data[p_sub_uart->send_buf_p],255);
//                     wk2114_sub_uart_set_inter(p_sub_uart,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);

//                     p_sub_uart->send_buf_p+=256;
//                 }
//             }
//             else{
//                 // 子串口队列无发送任务，取消发送出点为空中断
//                 wk2114_sub_uart_set_inter(p_sub_uart,WK2114_S_REG_SIFR_MASK_RFTRIG_INT|WK2114_S_REG_SIFR_MASK_RXOVT_INT|WK2114_S_REG_SIFR_MASK_FERR_INT);
//             }
//         }

//         // 发送FIFO触点中断, 装载子串口发送队列 send_buf
//         if((p_sub_uart->reg&WK2114_S_REG_SIFR_MASK_TFTRIG_INT)!=0){  
//             log_w(" send fifo trigger == 1");
//             // p_de=queue_get_de_index(&p_sub_uart->send_queue);
//             // if(p_de>=0){
//             if (osMessageQueueGetCount(p_sub_uart->send_queue) >= 0){
//                 if(p_sub_uart->send_buf_p >= p_sub_uart->send_record[p_de].size){
//                     wk2114_sub_uart_set_inter(p_sub_uart,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);
//                     p_sub_uart->send_buf_p=0xffffffff;
//                     return;
//                 }
//                 if(p_sub_uart->send_record[p_de].size-p_sub_uart->send_buf_p <= 128){
//                     struct wk2114_send_record record;
//                     osMessageQueueGet(p_sub_uart->send_queue,(void*) &record,sizeof(record),portMAX_DELAY);
//                     wk2114_write_fifo(wk2114,sub_uart_index,record.p_data[p_sub_uart->send_buf_p],record.size-p_sub_uart->send_buf_p-1);
                    
//                     // wk2114_write_fifo(wk2114,i,&p_sub_uart->send_record[p_de].p_data[p_sub_uart->send_buf_p],p_sub_uart->send_record[p_de].size-p_sub_uart->send_buf_p-1);
//                     p_sub_uart->send_buf_p=0xffffffff;
//                     wk2114_sub_uart_set_inter(p_sub_uart,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(&p_sub_uart->send_queue,NULL);
//                 }
//                 else{
//                     wk2114_write_fifo(wk2114,sub_uart_index,&p_sub_uart->send_record[p_de].p_data[p_sub_uart->send_buf_p],127);
//                     p_sub_uart->send_buf_p+=128;
//                 }
//             }
//             else{
//                 wk2114_sub_uart_set_inter(p_sub_uart,WK2114_S_REG_SIFR_MASK_RFTRIG_INT|WK2114_S_REG_SIFR_MASK_RXOVT_INT|WK2114_S_REG_SIFR_MASK_FERR_INT);
//             }
//         }
//         // 接收错误中断
//         if( (p_sub_uart->reg&WK2114_S_REG_SIFR_MASK_FERR_INT)!=0 ){   
//             wk2114_read_reg( wk2114,WK2114_BUILD_SUB_REG(sub_uart_index,WK2114_S_REG_LSR) );
//         }
//     }

//     //子串口接收FIFO计数寄存器
//     if(WK2114_CMD_GET_ADDR(p_sub_uart->cmd)==WK2114_BUILD_SUB_REG(sub_uart_index,WK2114_S_REG_RFCNT)){  
//         p_sub_uart->cmd=0;
//         wk2114_read_fifo(wk2114,sub_uart_index,p_sub_uart->reg-1);
//     }
//     //子串口接收状态寄存器
//     if(WK2114_CMD_GET_ADDR(p_sub_uart->cmd)==WK2114_BUILD_SUB_REG(sub_uart_index,WK2114_S_REG_LSR)){   
//         p_sub_uart->cmd=0;
//         wk2114_read_reg(wk2114,WK2114_BUILD_SUB_REG(sub_uart_index,WK2114_S_REG_FSR));
//     }
//     //子串口FIFO状态寄存器 
//     if(WK2114_CMD_GET_ADDR(p_sub_uart->cmd)==WK2114_BUILD_SUB_REG(sub_uart_index,WK2114_S_REG_FSR)){  
//         p_sub_uart->cmd=0;
//     }

//     return;
// }


















        // case WK2114_WORK_STATE_EXTI_READ_IT_TYPE:
        //     // Timeout
        //     HAL_UARTEx_ReceiveToIdle_DMA( pWK2114Uart, wk2xxx_rcvbuf, 1024);

        //     wk2114->send_buf[0] = (WK2114_CMD_READ_REG<<6) + WK2114_BUILD_SUB_REG(sub_uart, WK2114_S_REG_SIFR);
        //     wk2114->send_buf[1] = 0x00;
        //     wk2114->send_fun(wk2114->send_class, wk2114->send_buf , 2);

        //     osEventFlagsClear(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_GET_IT_SOURCE);
        //     *state = WK2114_WORK_STATE_EXTI_READ_IT_TYPE_OK;
        //     continue;

        // case WK2114_WORK_STATE_EXTI_READ_IT_TYPE_OK:
        //     log_i( "WK2114_WORK_STATE_EXTI_READ_IT_   TYPE_OK ....  "  );
        //     // uint8_t cmd;
        //     cmd = wk2114->send_buf[0];  // 上次发送内容第一位 send_buf[0]
        //     wk2114->send_buf[0] = 0;
        //     sub_uart = WK2114_CMD_GET_SUB_UART(cmd);
        //     p_sub_uart=wk2114->sub_uart[sub_uart];

        //     if(wk2114->size_received ==1){
        //         wk2114->flag&=~WK2114_G_FLAG_MASK_BUSY_RCV;
        //     }

        //     if( !WK2114_CMD_IS_READ(cmd) ){  
        //         *state=WK2114_WORK_STATE_ERROR;
        //     }

        //     if ( !WK2114_CMD_IS_REG(cmd)) {
        //         *state=WK2114_WORK_STATE_ERROR;
        //     }

        //     if(WK2114_REG_IS_GLOBAL(cmd)){ 
        //         *state=WK2114_WORK_STATE_ERROR;
        //     } 

        //     if(p_sub_uart==NULL || wk2114->sub_uart[sub_uart]->cmd!=0xff){
        //         *state=WK2114_WORK_STATE_ERROR;
        //     }

        //     p_sub_uart->cmd=cmd;
        //     p_sub_uart->reg=wk2xxx_rcvbuf[0];

        //     // 开启接收中断
        //     wk2114->size_received=0;
        //     if (HAL_DMA_GetState(pWK2114Uart->hdmarx) == HAL_DMA_STATE_BUSY)
        //     {
        //         HAL_UART_DMAStop(pWK2114Uart);
        //     }
        //     HAL_UARTEx_ReceiveToIdle_DMA( pWK2114Uart, wk2xxx_rcvbuf, 1024);
        //     // 依据中断 中断的类型， 进行数据处理
        //     wk2114_process_by_it_type(wk2114);
        //     // *state = WK2114_WORK_STATE_EXTI_PROCESS_DATA;
        //     continue;

        // case WK2114_WORK_STATE_EXTI_PROCESS_DATA:
        //     // Timeout
        //     if  ( (osKernelGetTickCount() - wk2114->timebase) > wk2114->timeout_ms )
        //     { 
        //         log_w( "wk2114 EXTI PROCESS_DATA Failed %d, Retrying...", err_con );
        //         err_con++;
        //         wk2114->timebase    = osKernelGetTickCount();
        //         if(err_con>10){
        //             log_e("EXTI PROCESS_DATA  Error");
        //             *state=WK2114_WORK_STATE_ERROR;
        //             break;
        //         } 
        //     }
        //     // *state = WK2114_WORK_STATE_EXTI_READ_IT_SOURCE_OK;
        //     osDelay(2);
        //     *state = WK2114_WORK_STATE_EXTI_PROCESS_DATA_OK;
        //     continue;
        // case WK2114_WORK_STATE_EXTI_PROCESS_DATA_OK:
        //     log_e("WK2114_WORK_STATE_EXTI PROCESS_DATA_OK ..  ");
        //     // uint8_t cmd;
        //     uint8_t cmd_size;
        //     cmd = wk2114->send_buf[0];  // 上次发送内容第一位 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->size_received >=1){
        //         wk2114->flag&=~WK2114_G_FLAG_MASK_BUSY_RCV;
        //     }
        //     //  读数据回调
        //     if( wk2114->size_received!=cmd_size ){  // 命令读取数量和实际读取数量不一致
        //         *state=WK2114_WORK_STATE_ERROR;
        //     }

        //     // 拷贝数据
        //     if(p_sub_uart->recv_buf_size-p_sub_uart->recv_buf_p < wk2114->size_received){ // 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 = wk2114->size_received -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;
        //     break;


        // case WK2114_WORK_STATE_SET_BAUD_OK:
        // {
        //     log_i( "WK2114_WORK_STATE_SET_BAUD ....  " );
        //     if (wk2114->setbaud_ok) 
        //     {
        //         HAL_UART_DMAStop(pWK2114Uart);
        //         *state = WK2114_WORK_STATE_OK;  
        //         break; 
        //     }
        //     if ( wk2114->timebase-osKernelGetTickCount() >200)
        //     {
        //         err_con++;
        //         log_i( " set  Baud, retry  %d times ", err_con );
        //         if (err_con>10)
        //         {
        //             *state = WK2114_WORK_STATE_ERROR;  
        //             break; 
        //         }
        //         HAL_UART_DMAStop(pWK2114Uart);
        //         *state = WK2114_WORK_STATE_SET_BAUD;  
        //         break; 
        //     }
        //     osDelay(20);
        //     continue;
        // } 

        // case WK2114_WORK_STATE_READ_GREG_0:
        // {
        //     log_i( "WK2114_WORK_STATE_READ_GREG_0 .... " );
        //     wk2114->timebase    = osKernelGetTickCount();
        //     wk2114_mainport_rcv( wk2114);
        //     // wk2114_read_global_reg(wk2114,WK2114_G_REG_GENA );
        //     uint8_t reg_0 = 0x40;
        //     wk2114->send_fun(wk2114->send_class,&reg_0,1);
        //     *state = WK2114_WORK_STATE_READ_GREG_0_OK;
        //     continue;
        // }
        // case WK2114_WORK_STATE_READ_GREG_0_OK:
        // {
        //     log_i( "WK2114_WORK_STATE_READ_GREG_0  OK ....  " );
        //     if (wk2114->read_greg0_ok) 
        //     {
        //         HAL_UART_DMAStop(pWK2114Uart);
        //         *state = WK2114_WORK_STATE_OK;  
        //         break; 
        //     }
        //     if ( wk2114->timebase-osKernelGetTickCount() >2000)
        //     {
        //         err_con++;
        //         log_i( " READ_GREG_0, retry  %d times ", err_con );
        //         if (err_con>10)
        //         {
        //             *state = WK2114_WORK_STATE_ERROR;  
        //             break; 
        //         }
        //         HAL_UART_DMAStop(pWK2114Uart);
        //         *state = WK2114_WORK_STATE_SET_BAUD;  
        //         break; 
        //     }
        //     osDelay(20);
        //     continue;
        // }

// #define WK2114_Uart_IRQHandler USART3_IRQHandler
// UART_HandleTypeDef *pWK2114Uart = &huart3;

// // WK2114_Sub_Uart_TypeDef  *sub_uart1 = NULL;
// // uint8_t sub_uart1_receive_buf[1024];

// int Sub_rcv_callback(void * obj, uint8_t * buf, uint16_t len)
// {
//     return 0;
// }

// void WK2XXX_IRQ_HANDLER(void)
// {
//     if( __HAL_GPIO_EXTI_GET_IT( GPIO_PIN_7 ) )
//     {
//     log_i("EXTI9_5_IRQHandler....");

//         wk2114_inter_callback( wk2114);
//         __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_7);
//     }
//     // HAL_GPIO_EXTI_IRQHandler(WK2114_IRQ_Pin);

// }

// // TODO 何时开启接收， 在  wk2114_read_reg 的时候
// void WK2114_Uart_IRQHandler(void)
// {
//     uint16_t count = 0;
//     uint16_t data_length = 0;
//     // uint32_t sending_flag = 0;
//     // uint32_t ret = 0;

//     if (__HAL_UART_GET_FLAG(pWK2114Uart, UART_FLAG_TC) != RESET)
//     {
 
//         log_i("WK2114 ..huart TC callback ..   "  );
//         // wk2114_send_completed_callback( wk2114);
//         wk2114->flag&=~WK2114_G_FLAG_MASK_SENDING;

//         pWK2114Uart->gState = HAL_UART_STATE_READY;
//         __HAL_UART_CLEAR_FLAG(pWK2114Uart, UART_FLAG_TC);
//     }

//     if (__HAL_UART_GET_FLAG(pWK2114Uart, UART_FLAG_IDLE) != RESET)
//     {
//         count = __HAL_DMA_GET_COUNTER( pWK2114Uart->hdmarx );
//         log_i("WK2114 ..huart  IDLE ... %d", 1024 - count);
//         data_length = 1024 - count;
//         if ( data_length > 0 )
//         {
//             // wk2114_idle_callback(wk2114, wk2xxx_rcvbuf, data_length);
//             // wk2114_reg_recv_callback(wk2114);
//             // wk2114_recv_callback(wk2114, wk2xxx_rcvbuf, data_length );
//             // log_i("WK2114_  IDLE.. data_length  %d   %02X",   data_length, wk2xxx_rcvbuf[0]);
//         }
//         __HAL_UART_CLEAR_IDLEFLAG( pWK2114Uart );
//     }
// }

// int WK2114_Add_Sub_Uart(WK2114_TypeDef *wk2114 )
// {
//     sub_uart1 =  (WK2114_Sub_Uart_TypeDef *)malloc(sizeof(WK2114_Sub_Uart_TypeDef));
//     if (0!=wk2114_sub_uart_register(wk2114, sub_uart1,WK2114_S_UART_1,sub_uart1_receive_buf,sizeof(sub_uart1_receive_buf), &huart2, Sub_rcv_callback))
//     {
//         log_e("sub-uart 1 register Failure ! ");
//     };

//     if (0!=wk2114_sub_uart_init( sub_uart1,115200,WK2114_SUB_UART_MODE_MASK_TX|WK2114_SUB_UART_MODE_MASK_RX,WK2114_SUB_UART_FIFO_MODE_MASK_TX|WK2114_SUB_UART_FIFO_MODE_MASK_RX,WK2114_UART_PARITY_NONE,WK2114_UART_STOP_BITS_1))
//     {
//         log_e("sub-uart 1 init Failure ! ");
//     }
//     return 0;
// }

// int WK2114_Init( )
// {
//       GPIO_InitTypeDef GPIO_InitStruct = {0};
//       WK2XXX_RST_GPIO_CLK_ENABLE();
//       WK2XXX_IRQ_GPIO_CLK_ENABLE();
//     // wk2XXX rst
//     GPIO_InitStruct.Pin = WK2XXX_RST_PIN;
//     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
//     GPIO_InitStruct.Pull = GPIO_PULLUP;
//     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
//     HAL_GPIO_Init(WK2XXX_RST_GPIO_PORT, &GPIO_InitStruct);

//     // wk2XXX irq
//     GPIO_InitStruct.Pin = WK2XXX_IRQ_PIN;
//     GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
//     GPIO_InitStruct.Pull = GPIO_PULLDOWN;
//     HAL_GPIO_Init(WK2XXX_IRQ_GPIO_PORT, &GPIO_InitStruct);

//     HAL_NVIC_SetPriority(WK2XXX_IRQ_EXTI_IRQ, 5, 0);
//     HAL_NVIC_EnableIRQ(WK2XXX_IRQ_EXTI_IRQ);

//     wk2114 = (WK2114_TypeDef *)malloc(sizeof(WK2114_TypeDef));
//     wk2114->send_class =  pWK2114Uart;
//     wk2114->send_fun =  HAL_UART_Transmit_DMA;

//     // 命令队列  uint8_t cmd_buf[64][2] ,  消息类型 2个uint8_t  
//     wk2114->cmd_queue = osMessageQueueNew( 64, sizeof(wk2114->cmd_queue_msg_put), NULL);
//     if (wk2114->cmd_queue ==NULL)  return -1;

//     wk2114->cmd = 0;
//     wk2114->flag = 0;
//     wk2114->dev_clock_hz = WK2114_CLOCK_HZ;
    
//     for(size_t i=0;i<4;i++){
//         wk2114->sub_uart[i] = NULL;
//     }

//     // HAL_GPIO_WritePin(WK2XXX_IRQ_GPIO_PORT, WK2XXX_RST_PIN,GPIO_PIN_RESET);
//     // HAL_Delay(2);

//     // HAL_GPIO_WritePin(WK2XXX_IRQ_GPIO_PORT, WK2XXX_RST_PIN,GPIO_PIN_SET);
//     wk2114_set_baud(wk2114);
//     HAL_Delay(2);
//     wk2114_set_baud(wk2114);
//     HAL_Delay(2);
//     wk2114_set_baud(wk2114);
//     HAL_Delay(2);

//     osSemaphoreRelease(wk2xxx_lockHandle);

//     wk2114->size_received = 0;
//     // wk2114->timebase    = osKernelGetTickCount();
//     // wk2114->timeout_ms  = 500;
//     // wk2114->state       = WK2114_WORK_STATE_WAIT;

//     // wk2114EventHandle   = osEventFlagsNew ( &wk2114Event_attributes );
//     // osEventFlagsClear(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_IT_SOURCE_SENDING );
//     // osEventFlagsClear(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_IT_SOURCE_RECEIVED );
//     // osEventFlagsClear(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_IT_TYPE_SENDING );
//     // osEventFlagsClear(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_IT_TYPE_RECEIVED );
//     // osEventFlagsClear(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_RCV_FIFO_COUNT_RECEIVED );
//     // osEventFlagsClear(wk2114EventHandle, MODULE_WK2114_EVENT_EXTI_RCV_FIFO_BUF_RECEIVED );
//     return 0;
// }