1. 程式人生 > >[Silicon EmberZnet] 初始化流程(二)

[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、事件處理函式 , 使用者自定義事件,及系統事件執行