CC2520/CC2530有一个功能可以降低90%的传输功耗,很多人没有用到

[复制链接]
 楼主| 罗菜鸟 发表于 2015-9-24 16:36 | 显示全部楼层 |阅读模式
SRC PEND,源地址判断功能。支持24个源地址判断。
应用方式如下:休眠节点唤醒后,向协调器发起一个请求接收,然后打开RX电路。协调器如果有数据发给休眠节点,事先把接收端的地址填入源地址判断缓存,收到休眠节点的请求后会产生一个中断,并在500us后决定要不要发数据给休眠节点。休眠节点唤醒发出请求后,如果500us没有收到协调器的判断,立即休眠。如果有数据,继续接收数据。
在实际应用时,休眠节点不是每一次唤醒都会接收数据,如果每次都打开RX,会白白浪费电流。以每唤醒10次后,收到一次数据来计算功耗,可以节省90%的功耗。
TI自己的协议栈,已经实现了该功能。



roger@anytrek 发表于 2015-9-28 15:37 | 显示全部楼层
什么情况楼主?求附上具体代码围观围观
 楼主| 罗菜鸟 发表于 2015-9-29 16:14 | 显示全部楼层
roger@anytrek 发表于 2015-9-28 15:37
什么情况楼主?求附上具体代码围观围观
  1. /*********************************************************************
  2. * @fn          MAC_SrcMatchEnable
  3. *
  4. * [url=home.php?mod=space&uid=247401]@brief[/url]      Enabled AUTOPEND and source address matching.
  5. *             This function shall be not be called from
  6. *             ISR. It is not thread safe.
  7. *
  8. * @param     none
  9. *
  10. * [url=home.php?mod=space&uid=266161]@return[/url]     none
  11. */
  12. void MAC_SrcMatchEnable (void)
  13. {
  14.   /* Turn on Frame Filter (TIMAC enables frame filter by default), TBD */
  15.   MAC_RADIO_TURN_ON_RX_FRAME_FILTERING();
  16.   
  17.   /* Turn on Auto ACK (TIMAC turn on Auto ACK by default), TBD */
  18.   MAC_RADIO_TURN_ON_AUTO_ACK();
  19.   
  20.   /* Turn on Autopend: set SRCMATCH.AUTOPEND and SRCMATCH.SRC_MATCH_EN */
  21.   MAC_RADIO_TURN_ON_SRC_MATCH();

  22.   /* Set SRCMATCH.AUTOPEND */
  23.   MAC_RADIO_TURN_ON_AUTOPEND();
  24.   
  25.   /* AUTOPEND function requires that the received
  26.    * frame is a DATA REQUEST MAC command frame
  27.    */
  28.   MAC_RADIO_TURN_ON_AUTOPEND_DATAREQ_ONLY();
  29.   
  30.   /* Configure all the globals */
  31.   macSrcMatchIsEnabled = TRUE;           
  32. }

  33. /*********************************************************************
  34. * @fn          MAC_SrcMatchAddEntry
  35. *
  36. * @brief       Add a short or extended address to source address table. This
  37. *              function shall be not be called from ISR. It is not thread safe.
  38. *
  39. * @param       addr  - a pointer to sAddr_t which contains addrMode
  40. *                      and a union of a short 16-bit MAC address or an extended
  41. *                      64-bit MAC address to be added to the source address
  42. *                       table.
  43. * @param       panID - the device PAN ID. It is only used when the addr is
  44. *                      using short address

  45. * @return      MAC_SUCCESS or MAC_NO_RESOURCES (source address table full)
  46. *              or MAC_DUPLICATED_ENTRY (the entry added is duplicated),
  47. *              or MAC_INVALID_PARAMETER if the input parameters are invalid.
  48. */
  49. uint8 MAC_SrcMatchAddEntry ( sAddr_t *addr, uint16 panID )
  50. {
  51.   uint8 index;
  52.   uint8 entry[MAC_SRCMATCH_SHORT_ENTRY_SIZE];
  53.   
  54.   /* Check if the input parameters are valid */
  55.   if ( addr == NULL || (addr->addrMode !=  SADDR_MODE_SHORT && addr->addrMode !=  SADDR_MODE_EXT))
  56.   {
  57.     return MAC_INVALID_PARAMETER;  
  58.   }
  59.   
  60.   /* Check if the entry already exists. Do not add duplicated entry */
  61.   if ( macSrcMatchCheckSrcAddr( addr, panID ) != MAC_SRCMATCH_INVALID_INDEX )
  62.   {
  63.     return MAC_DUPLICATED_ENTRY;
  64.   }
  65.   
  66.   /* If not duplicated, write to the radio RAM and enable the control bit */
  67.   
  68.   /* Find the first empty entry */
  69.   index = macSrcMatchFindEmptyEntry(addr->addrMode);
  70.   

  71.   if ( (index == MAC_SRCMATCH_SHORT_MAX_NUM_ENTRIES && addr->addrMode == SADDR_MODE_SHORT) ||
  72.        (index == MAC_SRCMATCH_EXT_MAX_NUM_ENTRIES && addr->addrMode == SADDR_MODE_EXT) )
  73.   {
  74.     return MAC_NO_RESOURCES;   /* Table is full */
  75.   }
  76.   
  77.   if ( addr->addrMode == SADDR_MODE_SHORT )
  78.   {
  79.     /* Write the PanID and short address */
  80.     entry[0] = LO_UINT16( panID );  /* Little Endian for the radio RAM */
  81.     entry[1] = HI_UINT16( panID );
  82.     entry[2] = LO_UINT16( addr->addr.shortAddr );
  83.     entry[3] = HI_UINT16( addr->addr.shortAddr );
  84.     MAC_RADIO_SRC_MATCH_TABLE_WRITE( ( index * MAC_SRCMATCH_SHORT_ENTRY_SIZE ),
  85.                    entry, MAC_SRCMATCH_SHORT_ENTRY_SIZE );
  86.   }
  87.   else
  88.   {
  89.     /* Write the extended address */
  90.     MAC_RADIO_SRC_MATCH_TABLE_WRITE( ( index * MAC_SRCMATCH_EXT_ENTRY_SIZE ),
  91.                    addr->addr.extAddr, MAC_SRCMATCH_EXT_ENTRY_SIZE );
  92.   }
  93.   
  94.   /* Set the Autopend enable bits */
  95.   macSrcMatchSetPendEnBit( index, addr->addrMode );
  96.   
  97.   /* Set the Src Match enable bits */
  98.   macSrcMatchSetEnableBit( index, TRUE, addr->addrMode);
  99.   
  100.   return MAC_SUCCESS;
  101. }

  102. /*********************************************************************
  103. * @fn         MAC_SrcMatchDeleteEntry
  104. *
  105. * @brief      Delete a short or extended address from source address table.
  106. *             This function shall be not be called from ISR. It is not thread
  107. *             safe.
  108. *
  109. * @param      addr  - a pointer to sAddr_t which contains addrMode
  110. *                     and a union of a short 16-bit MAC address or an extended
  111. *                     64-bit MAC address to be deleted from the source address
  112. *                     table.
  113. * @param      panID - the device PAN ID. It is only used when the addr is
  114. *                     using short address  
  115. *
  116. * @return     MAC_SUCCESS or MAC_INVALID_PARAMETER (address to be deleted
  117. *                  cannot be found in the source address table).
  118. */
  119. uint8 MAC_SrcMatchDeleteEntry ( sAddr_t *addr, uint16 panID  )
  120. {
  121.   uint8 index;
  122.   
  123.   if ( addr == NULL || (addr->addrMode !=  SADDR_MODE_SHORT && addr->addrMode !=  SADDR_MODE_EXT))
  124.   {
  125.     return MAC_INVALID_PARAMETER;  
  126.   }
  127.   
  128.   /* Look up the source address table and find the entry. */
  129.   index = macSrcMatchCheckSrcAddr( addr, panID );

  130.   if( index == MAC_SRCMATCH_INVALID_INDEX )
  131.   {
  132.     return MAC_INVALID_PARAMETER;
  133.   }
  134.   
  135.   /* Clear Src Match enable bits */
  136.   macSrcMatchSetEnableBit( index, FALSE, addr->addrMode);

  137.   return MAC_SUCCESS;
  138. }
  139.                   
  140. /*********************************************************************
  141. * @fn          MAC_SrcMatchAckAllPending
  142. *
  143. * @brief       Enabled/disable acknowledging all packets with pending bit set
  144. *              The application normally enables it when adding new entries to
  145. *              the source address table fails due to the table is full, or
  146. *              disables it when more entries are deleted and the table has
  147. *              empty slots.
  148. *
  149. * @param       option - TRUE (acknowledging all packets with pending field set)
  150. *                       FALSE (address filtering and FSM control sets the
  151. *                              pending field)
  152. *
  153. * @return      none
  154. */
  155. void MAC_SrcMatchAckAllPending ( uint8 option  )
  156. {
  157.   if( option == TRUE )
  158.   {
  159.     macSrcMatchIsAckAllPending = TRUE;
  160.    
  161.     /* Set the PENDING_OR register */
  162.     MAC_RADIO_TURN_ON_PENDING_OR();
  163.   }
  164.   else
  165.   {
  166.     macSrcMatchIsAckAllPending = FALSE;
  167.    
  168.     /* Clear the PENDING_OR register */
  169.     MAC_RADIO_TURN_OFF_PENDING_OR();
  170.   }
  171. }

  172. /*********************************************************************
  173. * @fn          MAC_SrcMatchCheckAllPending
  174. *
  175. * @brief       Check if acknowledging all packets with pending bit set
  176. *              is enabled.
  177. *
  178. * @param       none
  179. *
  180. * @return      MAC_AUTOACK_PENDING_ALL_ON or MAC_AUTOACK_PENDING_ALL_OFF
  181. */
  182. uint8 MAC_SrcMatchCheckAllPending ( void )
  183. {
  184.   if( macSrcMatchIsAckAllPending == TRUE )
  185.   {
  186.     return MAC_AUTOACK_PENDING_ALL_ON;
  187.   }
  188.   
  189.   return MAC_AUTOACK_PENDING_ALL_OFF;
  190. }

  191. /*********************************************************************
  192. * @fn          MAC_SrcMatchCheckResult
  193. *
  194. * @brief       Check the result of source matching
  195. *
  196. * @param       index - index of the entry in the source address table
  197. *
  198. * @return      TRUE or FALSE
  199. */
  200. MAC_INTERNAL_API bool MAC_SrcMatchCheckResult( void )
  201. {
  202.   uint8 resIndex;
  203.   
  204.   if ( macSrcMatchIsAckAllPending )
  205.   {
  206.     return (TRUE);
  207.   }
  208.   
  209.   MAC_RADIO_SRC_MATCH_RESINDEX( resIndex );
  210.   
  211.   return ( resIndex & AUTOPEND_RES );
  212. }

  213. /*********************************************************************
  214. * @fn          macSrcMatchFindEmptyEntry
  215. *
  216. * @brief       return index of the first empty entry found
  217. *
  218. * @param       macSrcMatchAddrMode - Address Mode for the entry. Valid values
  219. *              are SADDR_MODE_SHORT or SADDR_MODE_EXT
  220. *
  221. * @return      uint8 - return index of the first empty entry found
  222. */
  223. static uint8 macSrcMatchFindEmptyEntry( uint8 macSrcMatchAddrMode )
  224. {
  225.   uint8  index;
  226.   uint24 shortAddrEnable = MAC_RADIO_SRC_MATCH_GET_SHORTADDR_EN();
  227.   uint24 extAddrEnable = MAC_RADIO_SRC_MATCH_GET_EXTADDR_EN();
  228.   uint24 enable = shortAddrEnable | extAddrEnable;

  229.   if( macSrcMatchAddrMode == SADDR_MODE_SHORT )
  230.    {
  231.      for( index = 0; index < MAC_SRCMATCH_SHORT_MAX_NUM_ENTRIES; index ++ )
  232.      {
  233.        /* Both 2n bit of extAddrEnable and
  234.         * corresponding bit of shortAddrEnable must be clear
  235.         * in order to assume that the entry location for a short address
  236.         * is not used.
  237.         */
  238.        if( (extAddrEnable & ((uint24)0x01 << ((index/2)*2))) == 0 &&
  239.            (shortAddrEnable & ((uint24)0x01 << index)) == 0 )
  240.        {
  241.          return index;
  242.        }
  243.      }
  244.    }
  245.    else
  246.    {
  247.      for( index = 0; index < MAC_SRCMATCH_EXT_MAX_NUM_ENTRIES; index++ )
  248.      {
  249.        /* Both 2n bit of extAddrEnable and
  250.         * 2n bit and 2n+1 bit of shortAddrEnable must be clear in order
  251.         * to assume that the entry location for an extended address
  252.         * is not used.        
  253.         */
  254.        if( (enable & ((uint24)0x03 << (index*2))) == 0 )
  255.        {
  256.          return index;
  257.        }
  258.      }
  259.    }
  260.   return index;
  261. }

  262. /*********************************************************************
  263. * @fn         macSrcMatchCheckSrcAddr
  264. *
  265. * @brief      Check if a short or extended address is in the source address table.
  266. *             This function shall not be called from ISR. It is not thread safe.
  267. *
  268. * @param      addr - a pointer to sAddr_t which contains addrMode
  269. *                    and a union of a short 16-bit MAC address or an extended
  270. *                    64-bit MAC address to be checked in the source address table.
  271. * @param      panID - the device PAN ID. It is only used when the addr is
  272. *                     using short address

  273. * @return     uint8 - index of the entry in the table. Return
  274. *                     MAC_SRCMATCH_INVALID_INDEX (0xFF) if address not found.
  275. */
  276. static uint8 macSrcMatchCheckSrcAddr ( sAddr_t *addr, uint16 panID  )
  277. {
  278.   uint8 index;     
  279.   uint8 *pAddr;
  280.   uint8 entrySize;
  281.   uint8 indexUsed;
  282.   uint8 indexSize;
  283.   uint8 entry[MAC_SRCMATCH_SHORT_ENTRY_SIZE];  
  284.   uint8 ramEntry[MAC_SRCMATCH_EXT_ENTRY_SIZE];
  285.   uint24 enable;
  286.   
  287.   /*
  288.    Currently, shadow memory is not supported to optimize SPI traffic.
  289.   */
  290.   if( addr->addrMode ==  SADDR_MODE_SHORT )
  291.   {
  292.     entry[0] = LO_UINT16( panID );  /* Little Endian for the radio RAM */
  293.     entry[1] = HI_UINT16( panID );
  294.     entry[2] = LO_UINT16( addr->addr.shortAddr );
  295.     entry[3] = HI_UINT16( addr->addr.shortAddr );
  296.     pAddr = entry;
  297.     entrySize = MAC_SRCMATCH_SHORT_ENTRY_SIZE;
  298.     indexSize = 1;
  299.     enable = MAC_RADIO_SRC_MATCH_GET_SHORTADDR_EN();
  300.   }
  301.   else
  302.   {
  303.     pAddr = addr->addr.extAddr;
  304.     entrySize = MAC_SRCMATCH_EXT_ENTRY_SIZE;
  305.     indexSize = 2;
  306.     enable = MAC_RADIO_SRC_MATCH_GET_EXTADDR_EN();
  307.   }
  308.   
  309.   for( index = 0; index < MAC_SRCMATCH_SHORT_MAX_NUM_ENTRIES; index += indexSize )
  310.   {
  311.     /* Check if the entry is enabled */
  312.     if( macSrcMatchCheckEnableBit( index, enable ) == FALSE )
  313.     {
  314.       continue;
  315.     }
  316.    
  317.     indexUsed = index / indexSize;
  318.       
  319.     /* Compare the short address or extended address */
  320.     MAC_RADIO_SRC_MATCH_TABLE_READ( ( indexUsed * entrySize ), ramEntry, entrySize );
  321.      
  322.     if( osal_memcmp( pAddr, ramEntry, entrySize ) == TRUE )
  323.     {
  324.       /* Match found */
  325.       return indexUsed;
  326.     }
  327.   }
  328.   
  329.   return MAC_SRCMATCH_INVALID_INDEX;
  330. }

  331. /*********************************************************************
  332. * @fn          macSrcMatchSetPendEnBit
  333. *
  334. * @brief       Set the enable bit in the source address table
  335. *
  336. * @param       index - index of the entry in the source address table
  337. * @param       macSrcMatchAddrMode - Address Mode for the entry. Valid values
  338. *              are SADDR_MODE_SHORT or SADDR_MODE_EXT
  339. *
  340. * @return      none
  341. */
  342. static void macSrcMatchSetPendEnBit( uint8 index, uint8 macSrcMatchAddrMode )
  343. {
  344.   uint24 enable;
  345.   uint8 buf[MAC_SRCMATCH_ENABLE_BITMAP_LEN];
  346.       
  347.   if( macSrcMatchAddrMode == SADDR_MODE_SHORT )
  348.   {
  349.     enable = MAC_RADIO_SRC_MATCH_GET_SHORTADDR_PENDEN();
  350.     enable |= ( (uint24)0x01 << index );
  351.     osal_buffer_uint24( buf, enable );
  352.     MAC_RADIO_SRC_MATCH_SET_SHORTPENDEN( buf );
  353.   }
  354.   else
  355.   {
  356.     enable = MAC_RADIO_SRC_MATCH_GET_EXTADDR_PENDEN();
  357.     enable |= ( (uint24)0x01 << ( index * EXT_ADDR_INDEX_SIZE ) );
  358.     enable |= ( (uint24)0x01 << ( ( index * EXT_ADDR_INDEX_SIZE ) + 1 ) );
  359.     osal_buffer_uint24( buf, enable );
  360.     MAC_RADIO_SRC_MATCH_SET_EXTPENDEN( buf );
  361.   }
  362. }

  363. /*********************************************************************
  364. * @fn          macSrcMatchSetEnableBit
  365. *
  366. * @brief       Set or clear the enable bit in the SRCMATCH EN register
  367. *
  368. * @param       index  - index of the entry in the source address table
  369. * @param       option - true (set the enable bit), or false (clear the enable
  370. *                       bit)
  371. * @param       macSrcMatchAddrMode - Address Mode for the entry. Valid values
  372. *              are SADDR_MODE_SHORT or SADDR_MODE_EXT
  373. *
  374. * @return      none
  375. */
  376. static void macSrcMatchSetEnableBit( uint8 index,
  377.                                     bool option,
  378.                                     uint8 macSrcMatchAddrMode )
  379. {
  380.   uint24 enable;  
  381.   
  382.   if( option == TRUE )
  383.   {
  384.     if( macSrcMatchAddrMode == SADDR_MODE_SHORT )
  385.     {
  386.       enable = MAC_RADIO_SRC_MATCH_GET_SHORTADDR_EN();
  387.       enable |= ( (uint24)0x01 << index );
  388.       MAC_RADIO_SRC_MATCH_SET_SHORTEN( enable );
  389.     }
  390.     else
  391.     {
  392.       enable = MAC_RADIO_SRC_MATCH_GET_EXTADDR_EN();
  393.       enable |= ( (uint24)0x01 << ( index *  EXT_ADDR_INDEX_SIZE) );
  394.       MAC_RADIO_SRC_MATCH_SET_EXTEN( enable );
  395.     }
  396.   }
  397.   else
  398.   {
  399.     if( macSrcMatchAddrMode == SADDR_MODE_SHORT )
  400.     {
  401.       enable = MAC_RADIO_SRC_MATCH_GET_SHORTADDR_EN();
  402.       enable &= ~( (uint24)0x01 << index );
  403.       MAC_RADIO_SRC_MATCH_SET_SHORTEN( enable );
  404.     }
  405.     else
  406.     {
  407.       enable = MAC_RADIO_SRC_MATCH_GET_EXTADDR_EN();
  408.       enable &= ~( (uint24)0x01 << ( index * EXT_ADDR_INDEX_SIZE ) );
  409.       MAC_RADIO_SRC_MATCH_SET_EXTEN( enable );
  410.     }
  411.   }
  412. }

  413. /*********************************************************************
  414. * @fn          macSrcMatchCheckEnableBit
  415. *
  416. * @brief       Check the enable bit in the source address table
  417. *
  418. * @param       index - index of the entry in the source address table
  419. * @param       enable - enable register should be read before passing
  420. *              it here
  421. *
  422. * @return      TRUE or FALSE
  423. */
  424. static bool macSrcMatchCheckEnableBit( uint8 index, uint24 enable)
  425. {
  426.   if( enable & ((uint24)0x01 << index ))
  427.   {
  428.     return TRUE;
  429.   }
  430.   
  431.   return FALSE;
  432. }

  433. /*********************************************************************
  434. * @fn          macSrcMatchGetShortAddrPendEnBit
  435. *
  436. * @brief       Return the SRCMATCH ShortAddr Pend enable bitmap
  437. *
  438. * @param       none
  439. *
  440. * @return      uint24 - 24 bits bitmap
  441. */
  442. static uint24 macSrcMatchGetShortAddrPendEnBit( void )
  443. {
  444.   uint8 buf[MAC_SRCMATCH_ENABLE_BITMAP_LEN];
  445.   
  446.   MAC_RADIO_GET_SRC_SHORTPENDEN( buf );
  447.   
  448.   return osal_build_uint32( buf, MAC_SRCMATCH_ENABLE_BITMAP_LEN );
  449. }


  450. /*********************************************************************
  451. * @fn          macSrcMatchGetExtAddrPendEnBit
  452. *
  453. * @brief       Return the SRCMATCH Extended Address Pend enable bitmap
  454. *
  455. * @param       none
  456. *
  457. * @return      uint24 - 24 bits bitmap
  458. */
  459. static uint24 macSrcMatchGetExtAddrPendEnBit( void )
  460. {
  461.   uint8 buf[MAC_SRCMATCH_ENABLE_BITMAP_LEN];
  462.   
  463.   MAC_RADIO_GET_SRC_EXTENPEND( buf );
  464.   
  465.   return osal_build_uint32( buf, MAC_SRCMATCH_ENABLE_BITMAP_LEN );
  466. }

  467. /*********************************************************************
  468. * @fn          macSrcMatchGetShortAddrEnableBit
  469. *
  470. * @brief       Return the SRCMATCH ShortAddr enable bitmap
  471. *
  472. * @param       none
  473. *
  474. * @return      uint24 - 24 bits bitmap
  475. */
  476. static uint24 macSrcMatchGetShortAddrEnableBit( void )
  477. {
  478.   uint8 buf[MAC_SRCMATCH_ENABLE_BITMAP_LEN];
  479.   
  480.   MAC_RADIO_GET_SRC_SHORTEN( buf );
  481.   
  482.   return osal_build_uint32( buf, MAC_SRCMATCH_ENABLE_BITMAP_LEN );
  483. }

  484. /*********************************************************************
  485. * @fn          macSrcMatchGetExtAddrEnBit
  486. *
  487. * @brief       Return the SRCMATCH ExtAddr enable bitmap
  488. *
  489. * @param       none
  490. *
  491. * @return      uint24 - 24 bits bitmap
  492. */
  493. static uint24 macSrcMatchGetExtAddrEnableBit( void )
  494. {
  495.   uint8 buf[MAC_SRCMATCH_ENABLE_BITMAP_LEN];
  496.   
  497.   MAC_RADIO_GET_SRC_EXTEN( buf );
  498.   
  499.   return osal_build_uint32( buf, MAC_SRCMATCH_ENABLE_BITMAP_LEN );
  500. }
roger@anytrek 发表于 2015-10-14 15:15 | 显示全部楼层
嗯,好的,谢谢楼主。我在mac_autopend.c文件中看到类似的函数了,现在有个疑惑,该协议栈是否自动实现了楼主您提到的省电功能?还是要我们添加代码去调用某个函数来实现省电?PS:楼主您的协议栈是什么版本?(我的是2.3.0-1.4.0)
我无线遥控 发表于 2015-10-16 07:02 来自手机 | 显示全部楼层
楼主您好。我的2530用在遥控器上。我想**了做个接收器。但是到现在都没有成功,希望您能帮忙解决请联系我18858668001谢谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

132

主题

522

帖子

8

粉丝
快速回复 在线客服 返回列表 返回顶部