Chinaunix首页 | 论坛 | 博客
  • 博客访问: 106148
  • 博文数量: 32
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 318
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-30 09:58
个人简介

经过长期对C语言的研究,目前只有两个方面不懂:这也不懂,那也不懂。 本人擅长 Ai、Fw、Fl、Br、Ps 等软件的安装卸载,精通 CSS、Java、PHP、C、C++、C#、Java、Ruby、Python、Objective-C等单词的拼写,熟悉 Windows、Linux、Mac、Android、iOS等系统的开关机 https://github.com/ianhom

文章分类

全部博文(32)

我的朋友

分类: C/C++

2015-03-19 23:38:26

ZigBee和6loWPAN都可使用802.15.4的MAC+PHY,802.15.4为了让节点更好地接入信道(channel),MAC层中使用了CSMA-CA机制。根据采用信标网络还是非信标网络,分别使用时隙CSMA-CA(适合星状网和树状网)和非时隙CSMA-CA(适合网状网)。

该实现方法尝试了非时隙CSMA-CA算法实现,时隙CSMA-CA将在日后进行补充。

使用方法:
1、初始化数据结构:
     tCsmaData.ucSlotted                  = CSMA_CA_WITHOUT_SLOTTED;  /* 非时隙 */
     tCsmaData.ucBatteryExtension = BATTERY_EXTENSION_DIS;          /* 不启用电池寿命扩展 */
     tCsmaData.pfCCA                      = CCA;                                                    /* 空闲信道评估函数(随机数模拟) */
     tCsmaData.pfRandom                = Random;                                              /*  随机数函数(ADC模拟) */
2、初始化CSMA-CA   CSMA_CA_Init(&tCsmaData);
3、使用CSMA-CA      ucReturn = CSMA_CA();
4、根据CSMA-CA 返回结果进行报文发送。

CSMA-CA.h

点击(此处)折叠或打开

  1. /******************************************************************************
  2. * file :CSMA-CA.h
  3. * Brief :Header file of 802.15.4 CSMA-CA algorithm - Ver. Beta
  4. * Version:V0.10
  5. * Author :Ian
  6. * Date :2015年3月19日
  7. * History:    Date      Editor Version         Record
  8.              2015-03-19   Ian    V0.10    Create, only CSMA-CA without
  9.                                           time slots is realized.
  10. ******************************************************************************/
  11. #ifndef _CSMA_CS_H_
  12. #define _CSMA_CS_H_

  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif

  16. #define CSMA_CA_WITH_SLOTTED (1) /* CSMA-CA with time slott mode */
  17. #define CSMA_CA_WITHOUT_SLOTTED (0) /* CSMA-CA with no time slott mode */
  18. #define MAC_BE_MIN (3) /* Min. value of Backoff Exponent */
  19. #define MAC_BE_MAX (5) /* Max. value of Backoff Exponent */
  20. #define MAC_NB_MAX (4) /* Max. number of Backoff */
  21. #define CHANNEL_BUSY (0) /* Indicate that channel is busy */
  22. #define CHANNEL_CLR (1) /* Indicate that channel is clear */
  23. #define BACKOFF_PERIOD_IN_US (20) /* Backoff time in us */
  24. #define SW_OK (0) /* Successful operation */
  25. #define SW_ERR (-1) /* Successful operation */
  26. #define BATTERY_EXTENSION_EN (1) /* Battery life extension is enable */
  27. #define BATTERY_EXTENSION_DIS (0) /* Battery life extension is disable*/


  28. typedef unsigned char CSMA_CA_MODE; /* Mode type of csma-ca */
  29. typedef int RESULT; /* Result of function */

  30. typedef struct _CSMA_CA_
  31. {
  32.     CSMA_CA_MODE ucSlotted; /* CSAM-CA mode: Time slotted or non-slotted */
  33.     uint8_t ucBatteryExtension; /* Use batteryExtension or not */
  34.     uint8_t (*pfCCA)(void); /* CCA performing function interface */
  35.     uint8_t (*pfRandom)(void); /* Random function interface */
  36. }T_CSMA_CA_DATA;

  37. /*************************************************************************
  38. * Function :RESULT CSMA_CA_Init(T_CSMA_CA_DATA* ptCsma)
  39. * Description :Init CSMA-CA, Get & check necessary data struct
  40. * Input Param :T_CSMA_CA_DATA* ptCsma Data struct for CSMA-CA
  41. * Output Param:None
  42. * Return :SW_ERR: Failed operation
  43.                 SW_OK : Successful operation
  44. **************************************************************************/
  45. RESULT CSMA_CA_Init(T_CSMA_CA_DATA* ptCsma);

  46. /*************************************************************************
  47. * Function :RESULT CSMA_CA()
  48. * Description :Perform CSMA-CA, check a channel is available or not
  49. * Input Param :None
  50. * Output Param:None
  51. * Return :SW_ERR: Failed operation
  52.                 SW_OK : Successful operation
  53. **************************************************************************/
  54. RESULT CSMA_CA();

  55. /*************************************************************************
  56. * Function :void CSMA_Backoff_Delay(uint8_t ucNum)
  57. * Description :Perform CSMA-CA Backoff
  58. * Input Param :uint8_t ucNum Number of backoff period
  59. * Output Param:None
  60. * Return :None
  61. **************************************************************************/
  62. void CSMA_Backoff_Delay(uint8_t ucNum);

  63. /*************************************************************************
  64. * Function :void Delay_ns(uint16_t ucNum)
  65. * Description :Wait for ucNum of backoff period
  66. * Input Param :uint8_t ucNum Number of backoff period
  67. * Output Param:None
  68. * Return :None
  69. **************************************************************************/
  70. void Delay_ns(uint16_t ucNum);


  71. #ifdef __cplusplus
  72. }
  73. #endif

  74. #endif /* _CSMA_CA_H_ */

CSMA-CA.c

点击(此处)折叠或打开

  1. /******************************************************************************
  2. * file :CSMA-CA.c
  3. * Brief :802.15.4 CSMA-CA algorithm - Ver. Beta
  4. * Version:V0.10
  5. * Author :Ian
  6. * Date :2015年3月19日
  7. * History:     Date     Editor Version    Record
  8.             2015-03-19    Ian    V0.10     Create, only CSMA-CA without
  9.                                            time slots is realized.
  10. ******************************************************************************/

  11. #include "common.h"
  12. #include "csma_ca.h"

  13. static T_CSMA_CA_DATA *sg_ptCsma = {NULL}; /* Pointer for CSMA-CA data struct */

  14. /*************************************************************************
  15. * Function :RESULT CSMA_CA_Init(T_CSMA_CA_DATA* ptCsma)
  16. * Description :Init CSMA-CA, Get & check necessary data struct
  17. * Input Param :T_CSMA_CA_DATA* ptCsma Data struct for CSMA-CA
  18. * Output Param:None
  19. * Return :SW_ERR: Failed operation
  20.                 SW_OK : Successful operation
  21. **************************************************************************/
  22. RESULT CSMA_CA_Init(T_CSMA_CA_DATA* ptCsma)
  23. {
  24.     if ((NULL == ptCsma)\
  25.       ||(NULL == ptCsma->pfCCA)\
  26.       ||(NULL == ptCsma->pfRandom)) /* If the pointer is invalid, or if CCA/Random is not available */
  27.     {
  28.         return SW_ERR; /* return error */
  29.     }
  30.         
  31.     sg_ptCsma = ptCsma; /* Get the data struct for the module */
  32.     return SW_OK;
  33. }

  34. /*************************************************************************
  35. * Function :RESULT CSMA_CA()
  36. * Description :Perform CSMA-CA, check a channel is available or not
  37. * Input Param :None
  38. * Output Param:None
  39. * Return :SW_ERR: Failed operation
  40.                 SW_OK : Successful operation
  41. **************************************************************************/
  42. RESULT CSMA_CA()
  43. {
  44.     uint8_t ucBE,ucNB,ucRand,ucChClr;
  45.     switch(sg_ptCsma->ucSlotted) /* Check if it is slotted mode of csma */
  46.     {
  47.         case CSMA_CA_WITH_SLOTTED: /* If it is slotted mode */
  48.         {
  49.             /* To be continued.. */
  50.             break;
  51.             
  52.         }
  53.         case CSMA_CA_WITHOUT_SLOTTED: /* If it is not slotted mode */
  54.         {
  55.             ucNB = 0; /* Set init value of NB */
  56.             ucBE = MAC_BE_MIN; /* Set init value of BE */
  57.             
  58.             for (;ucNB <= MAC_NB_MAX;)/* If Number of backoff does not reach MAX.*/
  59.             {
  60.                 ucRand = sg_ptCsma->pfRandom()%((1<<ucBE)-1); /* Get Random uint backoff delay */
  61.                 printf("Random value is %d , Max value is %d\n",ucRand, ((1<<ucBE) -1));
  62.                 CSMA_Backoff_Delay(ucRand); /* Delay random uint backoff period */
  63.                 ucChClr = sg_ptCsma->pfCCA(); /* Perform a CCA */
  64.     
  65.                 if (CHANNEL_CLR == ucChClr) /* If the channel is clear */
  66.                 {
  67.                     printf("CSMA-CA is ok!\n");
  68.                     return SW_OK; /* Get the channel */
  69.                 }
  70.                 else /* If the channel is busy */
  71.                 {
  72.                     ucNB++; /* Update NB value */
  73.                     ucBE++; /* Update BE value */
  74.                     ucBE = ((ucBE<MAC_BE_MAX)?ucBE : MAC_BE_MAX); /* Get the Min. value*/
  75.                     printf("CSMA-CA is trying!, NB=%d, BE=%d\n",ucNB,ucBE);
  76.                 }
  77.             }
  78.             printf("CSMA-CA is failed!\n");
  79.             return SW_ERR; /* Max number of backoff is over, fail to get clear channel */
  80.             break;
  81.         }
  82.         default: /* Invalid mode */
  83.         {
  84.             break;
  85.         }
  86.     }
  87.     return SW_ERR; /* failed operation */
  88. }


  89. /*************************************************************************
  90. * Function :void CSMA_Backoff_Delay(uint8_t ucNum)
  91. * Description :Perform CSMA-CA Backoff
  92. * Input Param :uint8_t ucNum Number of backoff period
  93. * Output Param:None
  94. * Return :None
  95. **************************************************************************/
  96. void CSMA_Backoff_Delay(uint8_t ucNum)
  97. {
  98.     if(0 == ucNum)
  99.     {
  100.         return; /* if the number of backoff unit is 0, no delay is needed */
  101.     }
  102.     else
  103.     {
  104.         Delay_ns(ucNum*BACKOFF_PERIOD_IN_US);
  105.     }
  106.     return;
  107. }

  108. /*************************************************************************
  109. * Function :void Delay_ns(uint16_t ucNum)
  110. * Description :Wait for ucNum of backoff period
  111. * Input Param :uint8_t ucNum Number of backoff period
  112. * Output Param:None
  113. * Return :None
  114. **************************************************************************/
  115. void Delay_ns(uint16_t ucNum)
  116. {
  117.     uint16_t wTemp;
  118.     
  119.     for (wTemp = 0; wTemp < ucNum; wTemp++)
  120.     {
  121.         ; /* Just wait */
  122.     }
  123.     return;
  124. }

csma.rar

阅读(3484) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

偶非新新人2017-04-03 21:37:13

#include \"common.h\"的内容呢?。。。