Monday, January 4, 2016

Set the Logical Device type to Coordinator and router dynamically using Z-Stack on CC2538DK

Apply the following steps to SampleLight coordinator project and I can set the Logical Device type to Coordinator and router at run time.

1. Add "uint8 dev_type=0;" as global variable in ZDApp.c.

2. Add the red part in ZDAppDetermineDeviceType.

void ZDAppDetermineDeviceType( void )
{
  if (dev_type==0)
    zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR;
  else if(dev_type==1)
    zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER;

  
  if ( zgDeviceLogicalType ==ZG_DEVICETYPE_COORDINATOR )
  {
    devStartMode = MODE_HARD;     // Start as a coordinator
    ZDO_Config_Node_Descriptor.LogicalType = NODETYPE_COORDINATOR;
  }
  else
  ...

3. Revise the red part in ZDAppCheckForHoldKey.

void ZDAppCheckForHoldKey( void )
{
#if (defined HAL_KEY) && (HAL_KEY == TRUE)

  // Get Keypad directly to see if a HOLD is needed
  zdappHoldKeys = HalKeyRead();

  // Hold down the SW_BYPASS_START key (see OnBoard.h)
  // while booting to avoid starting up the device.
  if ( zdappHoldKeys == SW_BYPASS_START )
  {
    // Change the device state to HOLD on start up
    //devState = DEV_HOLD;
    dev_type=1;

  }
#endif // HAL_KEY
}
4. Revise the red part in ZGlobals.h

extern uint8 dev_type;
 
/*********************************************************************
 * MACROS
 */
#if defined( BUILD_ALL_DEVICES ) && !defined( ZSTACK_DEVICE_BUILD )
  #define ZSTACK_DEVICE_BUILD  (DEVICE_BUILD_COORDINATOR | DEVICE_BUILD_ROUTER | DEVICE_BUILD_ENDDEVICE)
#endif

// Setup to work with the existing (old) compile flags
#if 0
#if !defined ( ZSTACK_DEVICE_BUILD )
  #if defined ( ZDO_COORDINATOR )
    #define ZSTACK_DEVICE_BUILD  (DEVICE_BUILD_COORDINATOR)
  #elif defined ( RTR_NWK )
    #define ZSTACK_DEVICE_BUILD  (DEVICE_BUILD_ROUTER)
  #else
    #define ZSTACK_DEVICE_BUILD  (DEVICE_BUILD_ENDDEVICE)
  #endif
#endif
#else
#define ZSTACK_DEVICE_BUILD  (dev_type+1)
#endif

5. Compile the code and download it to CC2538DK. If I turn on CC2538DK without pressing any button, it will become ZC. If I turn on CC2538DK with UP key pressed, it will become ZR.

No comments:

Post a Comment