Zigbee 修改物理地址IEEE address

参考C:\Texas Instruments\ZStack-CC2530-2.5.1a\Documents   Z-Stack User's Guide - CC2530DB 可知

The Secondary IEEE address location is found on the last page of the CC2530 flash memory, at an offset of 0x0018 bytes from the last memory address. For a 256-Kbyte device, the IEEE address can be commissioned at 0x3FFE8-0x3FFEF.

您可以通过FLASH写入。关于协议里面存放IEEE的地址,和使用的逻辑请再搞清楚下。

1)首先CC2530里面有两个IEEE地址,一个Primary IEEE地址,还有一个Secondary IEEE地址。这个你应该知道的。

2)Primary IEEE是放在Info Page里面,用户只能读,没办法写。Secondary IEEE是可以读也可以写的

3)协议栈第一次启动的时候,会去Secondary IEEE和Primary IEEE地址里面挑选一个,作为该设备的MAC 地址,然后写到NV里面。

所以整个CC2530程序跑起来以后,有三个地方存放了IEEE地址,分别是Primary IEEE 在info page里面。 Secondary IEEE在Flash里面,MAC Address 在NV Flash里面。其中MAC Address是选择Primary和Secondary的某一个。至于怎么选的逻辑上面的程序很清楚了。

4)协议栈在第一次启动的时候,首先会判断NV里面有没有,如果没有的话就去Primary和Secondary里面挑了,所以你改的代码是没有用的,因为前天通过挑选已经挑选好了,并且放到NV去了,你的代码根本不会执行到。

所以建议做下面的修改,

在刚进入的zmain_ext_addr(void)的时候,利用Flash的 Write函数,也就是HalFlashWrite,往HAL_FLASH_IEEE_PAGE, HAL_FLASH_IEEE_OSET地方,写进入8个字节的数据就可以了。

当然如不你不用代码操作,你也可以用SmartRF Flash Programmer来写也可以的。

/**************************************************************************************************
 * @fn          zmain_ext_addr
 *
 * @brief       Execute a prioritized search for a valid extended address and write the results
 *              into the OSAL NV system for use by the system. Temporary address not saved to NV.
 *
 * input parameters
 *
 * None.
 *
 * output parameters
 *
 * None.
 *
 * @return      None.
 **************************************************************************************************
 */
static void zmain_ext_addr(void)
{
  uint8 nullAddr[Z_EXTADDR_LEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  uint8 aExtendedAddress[Z_EXTADDR_LEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  uint8 writeNV = TRUE;
  
  aExtendedAddress[0] = 0x01;
  aExtendedAddress[1] = 0x02;
  aExtendedAddress[2] = 0x01;
  aExtendedAddress[3] = 0x01;
  aExtendedAddress[4] = 0x01;
  aExtendedAddress[5] = 0xFF;
  aExtendedAddress[6] = 0xFF;
  aExtendedAddress[7] = 0xFF;
  HalFlashWrite(0xfffa, aExtendedAddress, 2);

  // First check whether a non-erased extended address exists in the OSAL NV.
  if ((SUCCESS != osal_nv_item_init(ZCD_NV_EXTADDR, Z_EXTADDR_LEN, NULL))  ||
      (SUCCESS != osal_nv_read(ZCD_NV_EXTADDR, 0, Z_EXTADDR_LEN, aExtendedAddress)) ||
      (osal_memcmp(aExtendedAddress, nullAddr, Z_EXTADDR_LEN)))
  {
    // Attempt to read the extended address from the location on the lock bits page
    // where the programming tools know to reserve it.
    HalFlashRead(HAL_FLASH_IEEE_PAGE, HAL_FLASH_IEEE_OSET, aExtendedAddress, Z_EXTADDR_LEN);

    if (osal_memcmp(aExtendedAddress, nullAddr, Z_EXTADDR_LEN))
    {
      // Attempt to read the extended address from the designated location in the Info Page.
      if (!osal_memcmp((uint8 *)(P_INFOPAGE+HAL_INFOP_IEEE_OSET), nullAddr, Z_EXTADDR_LEN))
      {
        osal_memcpy(aExtendedAddress, (uint8 *)(P_INFOPAGE+HAL_INFOP_IEEE_OSET), Z_EXTADDR_LEN);
      }
      else  // No valid extended address was found.
      {
        uint8 idx;
        
#if !defined ( NV_RESTORE )
        writeNV = FALSE;  // Make this a temporary IEEE address
#endif

        /* Attempt to create a sufficiently random extended address for expediency.
         * Note: this is only valid/legal in a test environment and
         *       must never be used for a commercial product.
         */
        for (idx = 0; idx < (Z_EXTADDR_LEN - 2);)
        {
          uint16 randy = osal_rand();
          aExtendedAddress[idx++] = LO_UINT16(randy);
          aExtendedAddress[idx++] = HI_UINT16(randy);
        }
        // Next-to-MSB identifies ZigBee devicetype.
#if ZG_BUILD_COORDINATOR_TYPE && !ZG_BUILD_JOINING_TYPE
        aExtendedAddress[idx++] = 0x10;
#elif ZG_BUILD_RTRONLY_TYPE
        aExtendedAddress[idx++] = 0x20;
#else
        aExtendedAddress[idx++] = 0x30;
#endif
        // MSB has historical signficance.
        aExtendedAddress[idx] = 0xF8;
      }
    }

    if (writeNV)
    {
      (void)osal_nv_write(ZCD_NV_EXTADDR, 0, Z_EXTADDR_LEN, aExtendedAddress);
    }
  }

  // Set the MAC PIB extended address according to results from above.
  (void)ZMacSetReq(MAC_EXTENDED_ADDRESS, aExtendedAddress);
}

猜你喜欢

转载自blog.csdn.net/qq_27747359/article/details/95969779
今日推荐