Zigbee Firmware

From Neuromeka Wiki
Jump to: navigation, search
BB-Air


PRE-DEFINE

ZTOOL_P1
MT_TASK
MT_SYS_FUNC
MT_ZDO_FUNC
HAL_UART=TRUE
HAL_UART_USB
HAL_UART_DMA=0
MYHAL_UART_ISR=2
MYHAL_UART_DMA=0
_IGOT_COORD
_IGOT_BBAIR
  • Explain the Pre-Define

- HAL_UART=TRUE : Enable UART
- HAL_UART_USB : UART Mode is USB
- MYHAL_UART_ISR=2 : MYUART Mode is ISR(UART1)
- _IGOT_COORD : Zigbee Mode is Coordinator
- _IGOT_BBAIR : Zigbee used for BBAIR

  • How to set the Pre-Define

1) Click the 'Project-Option'
2) Click the 'C/C++ Compiler' on Category
3) Click the 'Preprocessor' on tab
00 1 ZB PreDefine.png

SOURCE

  • Init
  halUARTCfg_t uartConfig;
  uartConfig.configured           = TRUE;
  uartConfig.baudRate             = BuadRate;
/*  uartConfig.flowControl          = TRUE;
  uartConfig.flowControlThreshold = 48;
  uartConfig.rx.maxBufSize        = 128;
  uartConfig.tx.maxBufSize        = 128;
  uartConfig.idleTimeout          = 6;
  uartConfig.intEnable            = TRUE;*/
  uartConfig.callBackFunc         = NULL;
  
#ifdef _IGOT_BBAIR
  HalUARTOpen(ZB_SETUP_UART_PORT, &uartConfig);
  UART1_ALt2_config(&uartConfig); 
  UART1_print("Neuromeka-ZIGBEE(1)\r\n");
#endif
  res = osal_nv_item_init(ZCD_NV_BAUDRATE, sizeof(sel_Baudrate), NULL);
  if (res !=NV_OPER_FAILED)
  {
    res=osal_nv_write(ZCD_NV_BAUDRATE, 0, sizeof(sel_Baudrate), &sel_Baudrate);
  }
  • performPeriodicTask
static void performPeriodicTask( void )
{
  uint8 rx_val=255,myrx_val=255;
  int cindex=-1, k, i;
  int res=0, myres=0;;
  ZStatus_t ret, scan_ret;
  uint8 nv_res;
  char dis[10];
  static int32 InitCnt=40;
  uint8 RX_PORT=ZB_SETUP_UART_PORT;
  char macstr[MID_BUFFER_LEN];
  
  if (zigbee_mode==ZIGBEE_MODE_SETP)
  {    
#ifdef _IGOT_BBAIR
    myres=MyHalUARTRead(ZB_SETUP_UART_PORT, &myrx_val, 1); //uart1
    res=HalUARTRead(ZB_SETUP_UART_PORT, &rx_val, 1);//hal uart usb
#endif  
    if ((res<=0)&&(myres>0))
    {
      res= myres;
      rx_val= myrx_val;
    }
    
    if ( (res >0) && (rx_val<128))
    {
      rx_buf[rx_cnt++]=rx_val;
      rx_buf[rx_cnt]=0;
    }
    
    if (rx_cnt>=MID_BUFFER_LEN)
    {
      rx_cnt=0;
      //rx_buf[rx_cnt]=0;
      memset(rx_buf, 0, MID_BUFFER_LEN);
    }
    
    if ((rx_val=='\r') || (rx_val=='\n'))
    {
      AT_command(rx_buf);      
    }      
  } //if (zigbee_mode==ZIGBEE_MODE_SETP)
  
  else //communication mode
  {
    memset(rx_buf, 0, MID_BUFFER_LEN);
    res=0;

#ifdef _IGOT_BBAIR
    myres=MyHalUARTRead(ZB_SETUP_UART_PORT, rx_buf, MID_BUFFER_LEN);//uart1
    res=HalUARTRead(ZB_SETUP_UART_PORT, rx_buf, MID_BUFFER_LEN); //hal uart usb
#endif
    
    if ((res<=0)&&(myres<=0)) return;
    else if ((res<=0)&&(myres>0))
    {
      res= myres;
    }
    
    if (check_plusplusplus(res)) //found "+++\n"
    {
      rx_cnt=0;
      memset(rx_buf, 0, MID_BUFFER_LEN); //ready to setting mode
      return;
    }
    if ((zigbee_mode==ZIGBEE_MODE_COMM) && (zigbee_connected==ZIGBEE_CON) )  
    {
#ifdef _IGOT_COORD   
        GenericApp_DstAddr.addrMode=Addr16Bit;
        for(i=0; i<joinDevCnt; ++i)
        {
          GenericApp_DstAddr.addr.shortAddr=joinPanID[i];
          AF_DataRequest( &GenericApp_DstAddr, &GenericApp_epDesc,
                       GENERICAPP_CLUSTERID,
                       (byte)osal_strlen( (char*)rx_buf ) + 1,
                       (byte *)&rx_buf,
                       &GenericApp_TransID,
                       AF_DISCV_ROUTE, AF_DEFAULT_RADIUS );      
        }         
#else  
        res = AF_DataRequest( &GenericApp_DstAddr, &GenericApp_epDesc,
                     GENERICAPP_CLUSTERID,
                     (byte)osal_strlen( (char*)rx_buf ) + 1,
                     (byte *)&rx_buf,
                     &GenericApp_TransID,
                     AF_DISCV_ROUTE, AF_DEFAULT_RADIUS );
        char dis[10];
        my_utoe(res, dis);
        UART1_print(dis);        
#endif           
    }
    else if ((zigbee_mode==ZIGBEE_MODE_COMM)
             &&(zigbee_connected==ZIGBEE_NOCON)) //send to BLE for setting
    {
      if (RX_PORT==ZB_SETUP_UART_PORT)
        UART1_print(rx_buf);
      else
        UART0_print(rx_buf);
    }          
  } //else //communication mode
  
#ifdef _IGOT_COORD  
  if (match_req==1)
  {
    Connect_activate();
    match_req=0;
    zigbee_connected=ZIGBEE_CON;
  }  
  else if (match_req>1)
    match_req=0;
#endif
}
  • AT Command
void AT_command(char* rx_buf)
{
  int cindex=-1;
  
  //Exit the Setup Mode
  if ( (strncmp((char*)rx_buf, "atex", 4)==0) 
      || (strncmp((char*)rx_buf, "ATEX", 4)==0) )
  {
    zigbee_mode=ZIGBEE_MODE_COMM;
    ZB_Setup_Response("Comm. mode\r\n");;
      SaveModeWrite(zigbee_mode);
  }
  
  //Information
  else if ( (strncmp((char*)rx_buf, "atif", 4)==0) 
           || (strncmp((char*)rx_buf, "ATIF", 4)==0) ) 
  {
    PeriodicTaskBusy=1; //temporary stop the task
    printDevInfo();
    
    PeriodicTaskBusy=0; //resume periodic task
    osal_start_timerEx( GenericApp_TaskID, GENERICAPP_PERIODIC_EVT, GENERICAPP_PERIODIC_EVT_PERIOD );
  }
  
  //Set the Baudrate
  else if ( (strncmp((char*)rx_buf, "atbr", 4)==0) 
           || (strncmp((char*)rx_buf, "ATBR", 4)==0) )
  {
    printUart();
    cindex=rx_buf[4]-'0';
    if( (rx_buf[4] >= 0) || (rx_buf[4] <= 4) )
    {
      // 0: 9600 / 1: 19200 / 2: 38400 / 3: 57600 / 4: 115200 
      if( (cindex>=0) && (cindex<=4) )
      {
        sel_Baudrate = cindex;
        CommUartInit(sel_Baudrate); //cindex = sel_Baudrate     
        printUart();       
      }
    }
  }
  
  //LQI
  else if ( (strncmp((char*)rx_buf, "atla", 4)==0) 
           || (strncmp((char*)rx_buf, "ATLA", 4)==0) ) 
  {
  }
  rx_cnt=0;
  memset(rx_buf, 0, MID_BUFFER_LEN);
}