[Silicon EmberZnet] 初始化流程(二)

正戏开始,这部分初始化,主要是初始化各种回调函数, 协议栈 , 中断 , 等。  我是边阅读代码边写的,所以建议阅读的同学们对着代码。

 

int emberAfMain(MAIN_FUNCTION_PARAMETERS)
{
  EmberStatus status;
  //第一步 , 调用了一个回调函数emberAfMainStartCallback , 函数啥都没实现, 估计是让用户自己实现一些初始化之前的动作
  {
    int returnCode;
    if (emberAfMainStartCallback(&returnCode, APP_FRAMEWORK_MAIN_ARGUMENTS)) {
      return returnCode;
    }
  }
  //第二步, 初始化Ember Stack ,这是库函数,PASS
  // Initialize the Ember Stack.
  status = emberInit();

  if (status != EMBER_SUCCESS) {
    emberAfCorePrintln("%pemberInit 0x%x", "ERROR: ", status);

    // The app can choose what to do here.  If the app is running
    // another device then it could stay running and report the
    // error visually for example. This app asserts.
    assert(false);
  } else {
    emberAfDebugPrintln("init pass");
  }

#if defined(EMBER_ENABLE_EM4)
#if defined(EMBER_TEST)
  uint8_t reset;
  reset = halGetResetInfo();
  if (reset == RESET_2xx_SOFTWARE_EM4) {
    // This can only be called if idle-sleep plugin is enabled, and em4 is OK.
    emberAfCameBackFromEM4Callback();
  }
#else
  uint16_t extReset;
  extReset = halGetExtendedResetInfo();
  emberAfCorePrintln("extReset :%d , RESET_SOFTWARE_EM4 : %d, RESET_EXTERNAL_EM4PIN= %d",
                     extReset,
                     RESET_SOFTWARE_EM4,
                     RESET_EXTERNAL_EM4PIN);
  if (extReset == RESET_SOFTWARE_EM4 || extReset == RESET_EXTERNAL_EM4PIN) {
    emberAfCameBackFromEM4Callback();
  }
#endif // EMBER_TEST
#endif

  //第三步,初始化网络架构, 基本都是库函数,PASS
  // This will initialize the stack of networks maintained by the framework,
  // including setting the default network.
  emAfInitializeNetworkIndexStack();
  //第四步,初始化消息回调数组,全部清空。 最大的是  EMBER_APS_UNICAST_MESSAGE_COUNT   10
  // Initialize messageSentCallbacks table
  emAfInitializeMessageSentCallbackArray();
  //第五步 ,初始化配置信息,有一些固定的参数
  emberAfEndpointConfigure();
  //第六步,用户应用初始化(这部分是使用回调实现的),Clusters 初始化。这个没看懂。
  emAfInit();

  //第七步,初始化地址缓存数据, ,然后并不知道是做什么的
  // The address cache needs to be initialized and used with the source routing
  // code for the trust center to operate properly.
  securityAddressCacheInit(EMBER_AF_PLUGIN_ADDRESS_TABLE_SIZE,                     // offset
                           EMBER_AF_PLUGIN_ADDRESS_TABLE_TRUST_CENTER_CACHE_SIZE); // size
  //第八步, 网络初始化
  EM_AF_NETWORK_INIT();
  //第九步,命令初始化,感觉是串口命令的那个, 并没有验证是否猜测对
  COMMAND_READER_INIT();

  //第十步,设置厂家编号,有一个默认的0x1002
  // Set the manufacturing code. This is defined by ZigBee document 053874r10
  // Ember's ID is 0x1002 and is the default, but this can be overridden in App Builder.
  emberSetManufacturerCode(EMBER_AF_MANUFACTURER_CODE);

  emberSetMaximumIncomingTransferSize(EMBER_AF_INCOMING_BUFFER_LENGTH);
  emberSetMaximumOutgoingTransferSize(EMBER_AF_MAXIMUM_SEND_PAYLOAD_LENGTH);
  //第十一步,设置电源模式
  emberSetTxPowerMode(EMBER_AF_TX_POWER_MODE);

  while (true) {
    halResetWatchdog();   // Periodically reset the watchdog.
    emberTick();          // Allow the stack to run.
    // Allow the ZCL clusters and plugin ticks to run. This should go
    // immediately after emberTick
    // Skip these ticks if a crypto operation is ongoing
    if (0 == emAfIsCryptoOperationInProgress()) {
      emAfTick();
    }

    emberSerialBufferTick();

    emberAfRunEvents();

#if defined(ZA_CLI_FULL)
    if (emberProcessCommandInput(APP_SERIAL)) {
      emberAfGuaranteedPrint("%p>", ZA_PROMPT);
    }
#endif

#if defined(EMBER_TEST)
    if (true) {
      // Simulation only
      uint32_t timeToNextEventMax = emberMsToNextStackEvent();
      timeToNextEventMax = emberAfMsToNextEvent(timeToNextEventMax);
      simulatedTimePassesMs(timeToNextEventMax);
    }
#endif

    // After each interation through the main loop, our network index stack
    // should be empty and we should be on the default network index again.
    emAfAssertNetworkIndexStackIsEmpty();

    if (false) {
      break;
    }
  }
  return 0;
}

while中主要就是

1、看门狗喂狗

2、调用tick函数,  猜测这个是基于tick, 去判断event时间, 再去做不同的event 的call back。

3、串口buffer的tick  是个空函数, 暂时不知道干嘛用的

4、事件处理函数 , 用户自定义事件,及系统事件运行

 

 

 

你可能感兴趣的:(Silicon,EmberZnet)