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.

3 comments:

  1. Hello,
    my cc2538 coordinator is to receive the data from the enddevice.The data needs to sent without pressing the keys from enddevice(it is standalone)
    EX:Helloworld Example message is sent from the enddevice to coordiantor without pressing the keys

    ReplyDelete
    Replies
    1. endPointDesc_t *epDesc;
      afAddrType_t dstAddr;
      cId_t cId;
      uint8 transId, txOpts, radius;
      uint8 retValue = ZFailure;
      char *pBuf = "Hello World";
      epDesc->endPoint = 8; // SAMPLESW_ENDPOINT or SAMPLELIGHT_ENDPOINT
      epDesc->latencyReq = 0;

      dstAddr.addr.shortAddr = 0x0000;
      epDesc->epType = ZG_BUILD_ENDDEVICE_TYPE;

      // ClusterId
      cId = osal_build_uint16( pBuf );

      // TransId
      transId = 1;

      // TxOption
      txOpts = 1;

      // Radius
      radius = AF_DEFAULT_RADIUS;

      // Length
      dataLen = strlen(pBuf);

      // Destination address
      dstAddr.addrMode = Addr16Bit;
      dstAddr.panId = _NIB.nwkPanId;

      retValue = AF_DataRequest(&dstAddr, epDesc, cId, dataLen, pBuf, &transId, txOpts, radius);

      Try this, retValue=0, confirm you the success of send to co-ordinator

      Delete