Example #1
0
/*********************************************************************
 * @fn      dataHandler
 *
 * @brief   Callback from UART indicating a data coming
 *
 * @param   port - data port.
 *
 * @param   events - type of data.
 *
 * @return  none
 */
static void dataHandler( uint8 port, uint8 events )
{  
  if((events & HAL_UART_RX_TIMEOUT) == HAL_UART_RX_TIMEOUT)
  {
    osal_stop_timerEx( biscuit_TaskID, SBP_RX_TIME_OUT_EVT);
    
    uint8 len = NPI_RxBufLen();
    uint8 buf[128];
    NPI_ReadTransport( buf, len );
    
    uint8 copy;   
    if(len > (MAX_RX_LEN-rxLen))
    {    
      copy = MAX_RX_LEN - rxLen;
      rxLen = MAX_RX_LEN;
    }
    else
    {
      rxLen += len;
      copy = len;
    }
    for(uint8 i=0; i<copy; i++)
    {
      RXBuf[rxHead] = buf[i];
      rxHead++;
      if(rxHead == MAX_RX_LEN)
      {
        rxHead = 0;
      }
    }
    
    osal_start_timerEx( biscuit_TaskID, SBP_RX_TIME_OUT_EVT, SBP_RX_TIME_OUT);
  }
  return;
}
Example #2
0
/*
1, 思路:  当串口收到数据后,就会马上调用以下回调函数,在实际测试中发现,此回调
函数调用频繁, 如果你不执行NPI_ReadTransport函数进行读取, 那么这个回调函数就会
频繁地被执行,但是,你通过串口发送一段数据, 你本意是想处理这一完整一段的数据,所以,
我们在下面引入了时间的处理方法, 也即接收的数据够多或者超时,就读取一次数据, 
然后根据当前的状态决定执行,如果没有连接上,就把所有数据当做AT命令处理, 如果连接
上了,就把数据送到对端。  ---------------amomcu   2014.08.17
*/
void NpiSerialCallback( uint8 port, uint8 events )
{
    (void)port;//加个 (void),是未了避免编译告警,明确告诉缓冲区不用理会这个变量

    if (events & (HAL_UART_RX_TIMEOUT | HAL_UART_RX_FULL))   //串口有数据
    {
        uint8 numBytes = 0;

        numBytes = NPI_RxBufLen();           //读出串口缓冲区有多少字节
        
        if(numBytes == 0)
        {
            return;
        }
        else
        {
            //申请缓冲区buffer
            uint8 *buffer = osal_mem_alloc(numBytes);
            if(buffer)
            {
                //读取读取串口缓冲区数据,释放串口数据   
                NPI_ReadTransport(buffer,numBytes);   

                //把收到的数据发送到串口-实现回环 
                NPI_WriteTransport(buffer, numBytes);  

                //释放申请的缓冲区
                osal_mem_free(buffer);
            }
        }
    }
}
// 串口回调函数
static void simpleBLE_NpiSerialCallback( uint8 port, uint8 events )
{
    (void)port;
    
    if (events & (HAL_UART_RX_TIMEOUT | HAL_UART_RX_FULL))   //串口有数据
    {
        uint8 numBytes = 0;

        numBytes = NPI_RxBufLen();           //读出串口缓冲区有多少字节
        
        if(numBytes > 0)
        {
            uint8 *buffer = osal_mem_alloc(numBytes);            
            if(buffer)
            {
                // 读出串口数据
                NPI_ReadTransport(buffer,numBytes);  
#if 1
                // 作为测试, 把读到的数据也通过串口返回, 这只是一个test功能, 你可以把去掉
                NPI_WriteTransport(buffer,numBytes);  
#endif              
                osal_mem_free(buffer);
            }
        }
    }
}
// 串口回调函数
static void simpleBLE_NpiSerialCallback( uint8 port, uint8 events )
{
    (void)port;
    
    if (events & (HAL_UART_RX_TIMEOUT | HAL_UART_RX_FULL))   //串口有数据
    {
        uint8 numBytes = 0;

        numBytes = NPI_RxBufLen();           //读出串口缓冲区有多少字节
        
        if(numBytes > 0)
        {
            uint8 *buffer = osal_mem_alloc(numBytes);            
            if(buffer)
            {
                NPI_ReadTransport(buffer,numBytes);  

                NPI_WriteTransport(buffer,numBytes);  
                    
                osal_mem_free(buffer);
            }
        }
    }
}
/***************************************************************************************************
 * @fn      uart_NpiSerialCallback
 *
 * @brief   uart callback function
 *
 * @param   port: uart port; events: events
 *
 * @return  None
 ***************************************************************************************************/
void uart_NpiSerialCallback( uint8 port, uint8 events )
{
    (void)port;//加个 (void),是未了避免编译告警,明确告诉缓冲区不用理会这个变量

    static uint32 old_time;     //老时间
    static uint32 old_time_data_len = 0;     //老时间是的数据长度    
    uint32 new_time;            //新时间
    uint8 readMaxBytes = 32;
        
    if (events & (HAL_UART_RX_TIMEOUT | HAL_UART_RX_FULL))   //串口有数据
    {

        uint8 numBytes = 0;
        numBytes = NPI_RxBufLen();           //读出串口缓冲区有多少字节
        
        if(numBytes == 0)
        {
            old_time_data_len = 0;
            return;
        }
        if(old_time_data_len == 0)
        {
            old_time = osal_GetSystemClock(); //有数据来时, 记录一下
            old_time_data_len = numBytes;
        }
        else
        {
            new_time = osal_GetSystemClock(); //当前时间
            
            if( (numBytes >= readMaxBytes) 
                || ( (new_time - old_time) > 50/*ms*/))
            {
                uint8 sendBytes = 0;
                //申请缓冲区buffer
                uint8 *buffer = osal_mem_alloc(numBytes);
                    
                if(numBytes > readMaxBytes)
                {
                    sendBytes = readMaxBytes;
                }
                else
                {
                    sendBytes = numBytes;
                }

                if(buffer)
                {
                    //读取读取串口缓冲区数据,释放串口数据   
                    NPI_ReadTransport(buffer,numBytes);
                    
                    //NPI_WriteTransport(buffer,numBytes);
                    //NPI_WriteTransport("\r\n",2);
                    
                    //sprintf(buffer,"numBytes=%d\r\n",numBytes);
                    //NPI_WriteTransport(buffer,strlen(buffer));
                    //NPI_WriteTransport(tmpbuffer,strlen(tmpbuffer));

                    // 命令处理函数
                    Uart_Msg_Handle(buffer,numBytes);
                    //Uart_Msg_Handle(tmpbuffer,strlen(tmpbuffer));
                    
                    //释放申请的缓冲区
                    osal_mem_free(buffer);
    
                }

                old_time = new_time;
                old_time_data_len = numBytes - sendBytes;
            }                
        }    
    }
}
/*********************************************************************
 * @fn      SimpleBLEPeripheral_ProcessEvent
 *
 * @brief   Simple BLE Peripheral Application Task event processor.  This function
 *          is called to process all events for the task.  Events
 *          include timers, messages and any other user defined events.
 *
 * @param   task_id  - The OSAL assigned task ID.
 * @param   events - events to process.  This is a bit map and can
 *                   contain more than one event.
 *
 * @return  events not processed
 */
uint16 SimpleBLEPeripheral_ProcessEvent( uint8 task_id, uint16 events )
{

  VOID task_id; // OSAL required parameter that isn't used in this function

  if ( events & SYS_EVENT_MSG )
  {
    uint8 *pMsg;

    if ( (pMsg = osal_msg_receive( simpleBLEPeripheral_TaskID )) != NULL )
    {
      simpleBLEPeripheral_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );

      // Release the OSAL message
      VOID osal_msg_deallocate( pMsg );
    }

    // return unprocessed events
    return (events ^ SYS_EVENT_MSG);
  }
  
  //user add
  if ( events & ZIGBEE_READ_ZM516X_INFO_EVT )
  {
    static uint8 state = 0;
    switch(state)
    {
    case 0:     
      init_zigbee_zm516x( task_id, ZIGBEE_READ_ZM516X_INFO_EVT );
      state = 1;
      break;
    case 1:
      HalGpioSet(HAL_GPIO_ZM516X_ALL,1); 
      HalGpioSet( HAL_GPIO_ZM516X_RESET,1 );
      osal_start_timerEx( task_id, ZIGBEE_READ_ZM516X_INFO_EVT, 100 );
      state = 2;
      break;
    case 2:
      HalGpioSet(HAL_GPIO_ZM516X_ALL,1); 
      read_local_cfg();
      state = 3;
      break;
    case 3:
      UartReceiveData( task_id, ZIGBEE_READ_ZM516X_INFO_EVT );
      state = 2;
      osal_start_timerEx( task_id, ZIGBEE_READ_ZM516X_INFO_EVT, 100 );
      break;
    default:
      break;
    }
    return (events ^ ZIGBEE_READ_ZM516X_INFO_EVT);
  }
  
  if ( events & UART_RECEIVE_EVT )
  {
    static unsigned char lenold = 0;
    unsigned char len = 0;
    //; NPI_ReadTransport(buf,len);
    if((len = NPI_RxBufLen()) > 0)
    {
      if(lenold == len)
      {
        osal_set_event( task_id, ZIGBEE_READ_ZM516X_INFO_EVT );
        lenold = 0;
      }
      else
      {
        lenold = len;
        osal_stop_timerEx( task_id, UART_RECEIVE_EVT );
        osal_start_timerEx( task_id, UART_RECEIVE_EVT, 3 );
      }
      return ( events ^ UART_RECEIVE_EVT );
    }
    else
    {
      //if(lenold > 0)
      //osal_set_event( task_id, SBP_READ_ZM516X_INFO_EVT );
      //lenold = len;
      //osal_start_timerEx( task_id, UART_RECEIVE_EVT, 10 );
      //osal_set_event( task_id, SBP_READ_ZM516X_INFO_EVT );
      return ( events ^ UART_RECEIVE_EVT );
    }
  }
  //end user add
  
  
  if ( events & SBP_START_DEVICE_EVT )
  {
    // Start the Device
    VOID GAPRole_StartDevice( &simpleBLEPeripheral_PeripheralCBs );

    // Start Bond Manager
    VOID GAPBondMgr_Register( &simpleBLEPeripheral_BondMgrCBs );

    // Set timer for first periodic event
    osal_start_timerEx( simpleBLEPeripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD );
    
    // user add : start user event
    osal_set_event( simpleBLEPeripheral_TaskID, ZIGBEE_READ_ZM516X_INFO_EVT );

    return ( events ^ SBP_START_DEVICE_EVT );
  }

  if ( events & SBP_PERIODIC_EVT )
  {
    // Restart timer
    if ( SBP_PERIODIC_EVT_PERIOD )
    {
      osal_start_timerEx( simpleBLEPeripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD );
    }

    // Perform periodic application task
    performPeriodicTask();

    return (events ^ SBP_PERIODIC_EVT);
  }

  // Discard unknown events
  return 0;
}
Example #7
0
void cSerialPacketParser( uint8 port, uint8 events )
{
    //unused input parameters
    (void)port;
    (void)events;

    uint8 done = FALSE;
    uint8 numBytes;

    // get the number of available bytes to process
    numBytes = NPI_RxBufLen();
    // check if there's any serial port data to process
    while ( (numBytes > 0) && (!done) )
    {
        switch (serialRxState)
        {
        case SERIAL_STATE_LEN:
            //read the length
            (void)NPI_ReadTransport((uint8 *)&packet_length, 1);
            // decrement the number of available bytes
            numBytes -= 1;

            // next state
            serialRxState = SERIAL_STATE_DATA;

        //DROP THROUGH

        case SERIAL_STATE_DATA:
            //check if we've dma'd the entire packet
            if (numBytes >= packet_length)
            {
                // alloc temporary buffer
                uint8 *temp_buf;
                temp_buf = osal_mem_alloc( packet_length );

                //store dma buffer into temp buffer
                (void)NPI_ReadTransport(temp_buf, packet_length);

                // copy data from temp buffer into rx circular buffer
                for (uint8 i = 0; i < packet_length; i++)
                {
                    //copy one byte to data buffer
                    serialBuffer[serialBufferOffset] = temp_buf[i];
                    //update offset
                    serialBufferOffset = circular_add(serialBufferOffset,1);
                }
                //free temp buffer
                osal_mem_free(temp_buf);

                //decrement number of available bytes
                numBytes -= packet_length;

                //reset state machine
                serialRxState = SERIAL_STATE_LEN;
            }
            else
            {
                //not enough data to progress, so leave it in driver buffer
                done = TRUE;
            }
            break;
        }
    }
}