eternal-echo / zigbee-module

zigbee网关中使用的zigbee模块程序
0 stars 0 forks source link

例程测试 #1

Open eternal-echo opened 1 year ago

eternal-echo commented 1 year ago

Generic APP

宏定义配置在下面,这里的修改都保存在.ewp文件中。 image

工程介绍

Zmain.c介绍

初始化了板上外设、驱动、NV存储、Mac、osal系统

/*********************************************************************
 * @fn      main
 * @brief   First function called after startup.
 * @return  don't care
 */
int main( void )
{
  // Turn off interrupts
  osal_int_disable( INTS_ALL );

  // Initialization for board related stuff such as LEDs
  HAL_BOARD_INIT();

  // Make sure supply voltage is high enough to run
  zmain_vdd_check();

  // Initialize board I/O
  InitBoard( OB_COLD );

  // Initialze HAL drivers
  HalDriverInit();

  // Initialize NV System
  osal_nv_init( NULL );

  // Initialize the MAC
  ZMacInit();

  // Determine the extended address
  zmain_ext_addr();

#if defined ZCL_KEY_ESTABLISH
  // Initialize the Certicom certificate information.
  zmain_cert_init();
#endif

  // Initialize basic NV items
  zgInit();

#ifndef NONWK
  // Since the AF isn't a task, call it's initialization routine
  afInit();
#endif

  // Initialize the operating system
  osal_init_system();

  // Allow interrupts
  osal_int_enable( INTS_ALL );

  // Final board initialization
  InitBoard( OB_READY );

  // Display information about this device
  zmain_dev_info();

  /* Display the device info on the LCD */
#ifdef LCD_SUPPORTED
  zmain_lcd_init();
#endif

#ifdef WDT_IN_PM1
  /* If WDT is used, this is a good place to enable it. */
  WatchDogEnable( WDTIMX );
#endif

  osal_start_system(); // No Return from here

  return 0;  // Shouldn't get here.
} // main()

串口debug

工程的宏定义中有个xZTOOL_P1,在ewp文件中,说明系统选择串口1作为默认的debug串口 zstack的lcd包含了串口的配置,具体的函数实现如下。首先通过下面创建一个msg,设置为CMD_DEBUG_STR事件,

/*********************************************************************
 * @fn      debug_str
 *
 * @brief
 *
 *   This feature allows modules to display a debug text string as
 *   applications execute in real-time. This feature will output to
 *   the serial port for display in the Z-Test tool.
 *
 *   This feature will most likely be compiled out in the production
 *   code in order to save code space.
 *
 * @param   byte *str_ptr - pointer to null-terminated string
 *
 * @return  void
 */
void debug_str( byte *str_ptr )
{
  mtDebugStr_t *msg;
  byte mln;
  byte strLen;

  // Text string length
  strLen = (byte)osal_strlen( (void*)str_ptr );

  // Debug string message length
  mln = sizeof ( mtDebugStr_t ) + strLen;

  // Get a message buffer to build the debug message
  msg = (mtDebugStr_t *)osal_msg_allocate( mln );
  if ( msg )
  {
    // Message type, length
    msg->hdr.event = CMD_DEBUG_STR;
    msg->strLen = strLen;

    // Append message, no terminator
    msg->pString = (uint8 *)(msg+1);
    osal_memcpy ( msg->pString, str_ptr, strLen );

    osal_msg_send( MT_TaskID, (uint8 *)msg );
  }
} // debug_str()

MT_ProcessEvent函数会添加到应用程序的任务列表中,比如Projects\zstack\HomeAutomation\GenericApp\Source\OSAL_GenericApp.c中的tasksArrMT_ProcessIncomingCommand函数中的CMD_DEBUG_STR的分支对应着前面debug_str消息的配置,调用了MT_ProcessDebugStr函数,但是这个调试函数对应的是ztool的响应。


/***************************************************************************************************
 * @fn      MT_ProcessDebugStr
 *
 * @brief   Build and send a debug string.
 *
 * @param   byte *dstr - pointer to the data portion of the debug message
 *
 * @return  void
 ***************************************************************************************************/
void MT_ProcessDebugStr(mtDebugStr_t *dstr)
{
  byte *msg_ptr;

  /* Get a message buffer to build the debug message */
  msg_ptr = osal_mem_alloc( (byte)(SPI_0DATA_MSG_LEN + dstr->strLen) );
  if ( msg_ptr )
  {
#ifdef MT_UART_DEFAULT_PORT
    /* Debug message is set to AREQ CMD 0x80 for now */
    /* Build and send back the response */
    MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_AREQ | (uint8)MT_RPC_SYS_DBG), MT_DEBUG_MSG, dstr->strLen, dstr->pString);
#endif
    osal_mem_free( msg_ptr );
  }
}

/**************************************************************************************************
 */

/***************************************************************************************************
 * @fn      MT_ProcessIncomingCommand
 *
 * @brief
 *
 *   Process Event Messages.
 *
 * @param   *msg - pointer to event message
 *
 * @return
 ***************************************************************************************************/
static void MT_ProcessIncomingCommand( mtOSALSerialData_t *msg )
{
  uint8 len, *msg_ptr = msg->msg;

  /* Use the first byte of the message as the command ID */
  switch ( msg->hdr.event )
  {
    case CMD_SERIAL_MSG:
      MT_ProcessIncoming(msg_ptr);
      break;

    case CMD_DEBUG_MSG:
      MT_ProcessDebugMsg( (mtDebugMsg_t *)msg );
      break;

    case CB_FUNC:
      /*
        Build SPI message here instead of redundantly calling MT_BuildSPIMsg
        because we have copied data already in the allocated message
      */

      /* msg_ptr is the beginning of the intended SPI message */
      len = SPI_0DATA_MSG_LEN + msg_ptr[DATALEN_FIELD];

      /*
        FCS goes to the last byte in the message and is calculated over all
        the bytes except FCS and SOP
      */
#if !defined(NPI)
      msg_ptr[len-1] = MT_UartCalcFCS(msg_ptr + 1, (uint8)(len-2));
#else
      msg_ptr[len-1] = npiframe_calcMTFCS(msg_ptr + 1, (uint8)(len-2));
#endif

#ifdef MT_UART_DEFAULT_PORT
      HalUARTWrite ( MT_UART_DEFAULT_PORT, msg_ptr, len );
#endif
      break;

    case CMD_DEBUG_STR:
      MT_ProcessDebugStr( (mtDebugStr_t *)msg );
      break;

#if !defined ( NONWK )
    case MT_SYS_APP_RSP_MSG:
      len = SPI_0DATA_MSG_LEN + msg_ptr[DATALEN_FIELD];
      MTProcessAppRspMsg( msg_ptr, len );
      break;
#endif  // NONWK

#if defined (MT_UTIL_FUNC)
#if defined ZCL_KEY_ESTABLISH
    case ZCL_KEY_ESTABLISH_IND:
      MT_UtilKeyEstablishInd((zclKE_StatusInd_t *)msg);
      break;
#endif
#endif
#ifdef MT_ZDO_CB_FUNC
    case ZDO_STATE_CHANGE:
      MT_ZdoStateChangeCB((osal_event_hdr_t *)msg);
      break;
#endif

    default:
      break;
  }
}
/**************************************************************************************************
 * @fn      MT_ProcessEvent
 *
 * @brief   MonitorTest Task Event Processor.  This task is put into the task table.
 *
 * @param   task_id - task ID of the MT Task
 * @param   events - event(s) for the MT Task
 *
 * @return  Bit mask of the unprocessed MT Task events.
 **************************************************************************************************/
UINT16 MT_ProcessEvent(uint8 task_id, uint16 events)
{
  /* Could be multiple events, so switch won't work */
  if ( events & SYS_EVENT_MSG )
  {
    uint8 *msg_ptr = osal_msg_receive(task_id);

    if (msg_ptr != NULL)
    {
      MT_ProcessIncomingCommand((mtOSALSerialData_t *)msg_ptr);
      osal_msg_deallocate(msg_ptr);
    }

串口波特率设置是在这里:


/***************************************************************************************************
 * @fn      MT_UartInit
 *
 * @brief   Initialize MT with UART support
 *
 * @param   None
 *
 * @return  None
***************************************************************************************************/
void MT_UartInit ()
{
  halUARTCfg_t uartConfig;

  /* Initialize APP ID */
  App_TaskID = 0;

  /* UART Configuration */
  uartConfig.configured           = TRUE;
  uartConfig.baudRate             = MT_UART_DEFAULT_BAUDRATE;
  uartConfig.flowControl          = MT_UART_DEFAULT_OVERFLOW;
  uartConfig.flowControlThreshold = MT_UART_DEFAULT_THRESHOLD;
  uartConfig.rx.maxBufSize        = MT_UART_DEFAULT_MAX_RX_BUFF;
  uartConfig.tx.maxBufSize        = MT_UART_DEFAULT_MAX_TX_BUFF;
  uartConfig.idleTimeout          = MT_UART_DEFAULT_IDLE_TIMEOUT;
  uartConfig.intEnable            = TRUE;
#if defined (ZTOOL_P1) || defined (ZTOOL_P2)
  uartConfig.callBackFunc         = MT_UartProcessZToolData;
#elif defined (ZAPP_P1) || defined (ZAPP_P2)
  uartConfig.callBackFunc         = MT_UartProcessZAppData;
#else
  uartConfig.callBackFunc         = NULL;
#endif

  /* Start UART */
#if defined (MT_UART_DEFAULT_PORT)
  HalUARTOpen (MT_UART_DEFAULT_PORT, &uartConfig);
#else
  /* Silence IAR compiler warning */
  (void)uartConfig;
#endif

  /* Initialize for ZApp */
#if defined (ZAPP_P1) || defined (ZAPP_P2)
  /* Default max bytes that ZAPP can take */
  MT_UartMaxZAppBufLen  = 1;
  MT_UartZAppRxStatus   = MT_UART_ZAPP_RX_READY;
#endif

}
eternal-echo commented 1 year ago

ZNP

资料

zigbee znp的教程
HA的python组件,使用了cc2530的znp zigbee2mqtt

znp让cc2530作为zigbee模块,处理zigbee协议相关的底层通信任务。主机控制器通过串口来控制cc2530,主从之间通过特定的协议来控制。符合本项目的要求。

介绍

ZNP,即Zigbee网络处理器,是TI的Zigbee解决方案的一种应用形式。在这种形式下,Zigbee芯片只负责处理底层网络的功能,而将上层应用交给主机处理,ZNP与主机之间通过USB或UART连接。Zigbee芯片实现ZNP功能所需的固件及其源码,以及接口API的说明都包含在Z-Stack中。所谓接口API,指主机通过USB或UART与ZNP通信的帧格式,以及命令和参数的定义。在主机侧,TI提供了对接口API的包装,即znp-host-framework,C语言实现,开源,代码托管在TI的Git网站上,另外在GitHub上还有适用于Node.js的版本。 image