[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、事件处理函数 , 用户自定义事件,及系统事件运行

 

 

 

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值