SampleApp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #include "OSAL.h"
  2. #include "ZGlobals.h"
  3. #include "AF.h"
  4. #include "aps_groups.h"
  5. #include "ZDApp.h"
  6. #include "SampleApp.h"
  7. #include "SampleAppHw.h"
  8. #include "OnBoard.h"
  9. #include <math.h>
  10. #include "MT_UART.h"
  11. #include "MT_APP.h"
  12. #include "MT.h"
  13. #include "hal_uart.h"
  14. #include "hal_adc.h"
  15. #include <stdio.h>
  16. #include "string.h"
  17. #include <stdlib.h>
  18. void App_GatherTask(void);
  19. // This list should be filled with Application specific Cluster IDs.
  20. const cId_t SD_App_ClusterList[SD_APP_MAX_CLUSTERS] =
  21. {
  22. SD_APP_BROADCAST_CLUSTERID,
  23. SD_APP_POINT_TO_POINT_CLUSTERID
  24. };//这就是每一个端点里面的簇有2个并且簇的ID分别是1,2
  25. const SimpleDescriptionFormat_t SD_App_SimpleDesc =
  26. {
  27. SD_APP_ENDPOINT, // int Endpoint;
  28. SD_APP_PROFID, // uint16 AppProfId[2];
  29. SD_APP_DEVICEID, // uint16 AppDeviceId[2];
  30. SD_APP_DEVICE_VERSION, // int AppDevVer:4;
  31. SD_APP_FLAGS, // int AppFlags:4;
  32. SD_APP_MAX_CLUSTERS, // uint8 AppNumInClusters;
  33. (cId_t *)SD_App_ClusterList, // uint8 *pAppInClusterList;
  34. SD_APP_MAX_CLUSTERS, // uint8 AppNumInClusters;
  35. (cId_t *)SD_App_ClusterList // uint8 *pAppInClusterList;
  36. };
  37. // This is the Endpoint/Interface description. It is defined here, but
  38. // filled-in in SD_App_Init(). Another way to go would be to fill
  39. // in the structure here and make it a "const" (in code space). The
  40. // way it's defined in this sample app it is define in RAM.
  41. endPointDesc_t SD_App_epDesc;//定义的端点描述符
  42. /*********************************************************************
  43. * LOCAL VARIABLES
  44. */
  45. uint8 SD_App_TaskID; // Task ID for internal task/event processing
  46. // This variable will be received when
  47. // SD_App_Init() is called.
  48. devStates_t SD_App_NwkState;
  49. uint8 SD_App_TransID; // This is the unique message ID (counter)
  50. afAddrType_t SD_App_Broadcast_DstAddr;
  51. afAddrType_t SD_App_Point_To_Point_DstAddr;
  52. /*********************************************************************
  53. * LOCAL FUNCTIONS
  54. */
  55. void SD_App_MessageMSGCB( afIncomingMSGPacket_t *pckt );
  56. /* 串口基本定义 */
  57. #define MY_DEFINE_UART_PORT 0 //自定义串口号(0,1);
  58. #define RX_MAX_LENGTH 20 //接收缓冲区最大值: 20个字节;
  59. uint8 RX_BUFFER[RX_MAX_LENGTH]; //接收缓冲区;
  60. void UartCallBackFunction(uint8 port , uint8 event); //回调函数声明,定义在最后面;
  61. /* 配置串口 */
  62. halUARTCfg_t uartConfig; //定义串口配置结构体变量;
  63. void Uart_Config(void); //函数声明;
  64. void Uart_Config(void) //函数定义;
  65. {
  66. uartConfig.configured = TRUE; //允许配置;
  67. uartConfig.baudRate = HAL_UART_BR_115200;//波特率;
  68. uartConfig.flowControl = FALSE;
  69. uartConfig.flowControlThreshold = 64; //don't care - see uart driver.
  70. uartConfig.rx.maxBufSize = 128; //串口接收缓冲区大小
  71. uartConfig.tx.maxBufSize = 128; //串口发送缓冲区大小
  72. uartConfig.idleTimeout = 6; //don't care - see uart driver.
  73. uartConfig.intEnable = TRUE; //使能中断
  74. uartConfig.callBackFunc = UartCallBackFunction; //指定回调函数名;
  75. }
  76. /*********************************************************************
  77. * @fn SD_App_Init
  78. *
  79. * @brief Initialization function for the Generic App Task.
  80. * This is called during initialization and should contain
  81. * any application specific initialization (ie. hardware
  82. * initialization/setup, table initialization, power up
  83. * notificaiton ... ).
  84. *
  85. * @param task_id - the ID assigned by OSAL. This ID should be
  86. * used to send messages and set timers.
  87. *
  88. * @return none
  89. */
  90. void SD_App_Init( uint8 task_id )
  91. {
  92. SD_App_TaskID = task_id;
  93. SD_App_NwkState = DEV_INIT;
  94. SD_App_TransID = 0;
  95. #if defined ( BUILD_ALL_DEVICES )
  96. // The "Demo" target is setup to have BUILD_ALL_DEVICES and HOLD_AUTO_START
  97. // We are looking at a jumper (defined in SD_AppHw.c) to be jumpered
  98. // together - if they are - we will start up a coordinator. Otherwise,
  99. // the device will start as a router.
  100. if ( readCoordinatorJumper() )
  101. zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR;
  102. else
  103. zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER;
  104. #endif // BUILD_ALL_DEVICES
  105. #if defined ( HOLD_AUTO_START )
  106. // HOLD_AUTO_START is a compile option that will surpress ZDApp
  107. // from starting the device and wait for the application to
  108. // start the device.
  109. ZDOInitDevice(0);
  110. #endif
  111. /* 串口操作 */
  112. Uart_Config(); //配置串口
  113. HalUARTOpen(MY_DEFINE_UART_PORT , &uartConfig); //打开串口
  114. HalUARTWrite(MY_DEFINE_UART_PORT ,"HAL_UART_OK\r\n" , 15);
  115. // Setup for the periodic message's destination address
  116. // Broadcast to everyone
  117. SD_App_Broadcast_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;//广播模式
  118. SD_App_Broadcast_DstAddr.endPoint = SD_APP_ENDPOINT;
  119. SD_App_Broadcast_DstAddr.addr.shortAddr = 0xFFFF;
  120. SD_App_Point_To_Point_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
  121. SD_App_Point_To_Point_DstAddr.endPoint = SD_APP_ENDPOINT;
  122. SD_App_Point_To_Point_DstAddr.addr.shortAddr = 0x0000;
  123. // // Fill out the endpoint description.
  124. SD_App_epDesc.endPoint = SD_APP_ENDPOINT;
  125. SD_App_epDesc.task_id = &SD_App_TaskID;
  126. SD_App_epDesc.simpleDesc
  127. = (SimpleDescriptionFormat_t *)&SD_App_SimpleDesc;
  128. SD_App_epDesc.latencyReq = noLatencyReqs;
  129. //
  130. // // Register the endpoint description with the AF
  131. afRegister( &SD_App_epDesc );
  132. // Register for all key events - This app will handle all key events
  133. // RegisterForKeys( SD_App_TaskID );//注册所有按键事件-此应用程序将处理所有关键事件。
  134. // P1DIR|=0x02;//p11输出
  135. // Start sending the periodic message in a regular interval.
  136. //这个定时器只是为发送周期信息开启的,设备启动初始化后从这里开始
  137. //触发第一个周期信息的发送,然后周而复始下去
  138. osal_start_timerEx(SD_App_TaskID,
  139. SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
  140. SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT);
  141. P0DIR&=~0x20;//p05设置为输入
  142. P0DIR&=~0x80;//p06设置为输入
  143. // P0DIR|=0x20;
  144. }
  145. #define RT_Input P0_7
  146. uint8 SendData[10]={0};//发送数据缓存区
  147. int IfgTimer=0;
  148. char RT_InFlag=0;//感应标志
  149. uint16 ifg_Rt =0;//计时
  150. char Send_Flag=0;
  151. static uint16 GetTime=0;
  152. void App_GatherTask(void)
  153. {
  154. SendData[0]=0xAA;
  155. if(++GetTime>80)//上电40S不检测 等到人体感应传感器稳定后开始检测
  156. {
  157. GetTime=80;
  158. if(RT_Input==1)
  159. {
  160. SendData[1]=1; //有人
  161. ifg_Rt=0;
  162. }
  163. else
  164. {
  165. ifg_Rt++;
  166. if(ifg_Rt>36)//计时 20s
  167. {
  168. SendData[1]=0;//没人
  169. ifg_Rt=0;
  170. }
  171. }
  172. }
  173. AF_DataRequest(&SD_App_Broadcast_DstAddr, //发送目的地址+端点地址和传送模式
  174. &SD_App_epDesc,//源(答复或确认)终端的描述(比如操作系统中任务ID等)源EP
  175. SD_APP_BROADCAST_CLUSTERID, //被Profile指定的有效的集群号
  176. 2, // 发送数据长度
  177. SendData,// 发送数据缓冲区
  178. &SD_App_TransID, // 任务ID号
  179. AF_DISCV_ROUTE, // 有效位掩码的发送选项
  180. AF_DEFAULT_RADIUS );//传送跳数,通常设置为AF_DEFAULT_RADIUS
  181. }
  182. //用户应用任务的事件处理函数
  183. //task_id 任务id号,events 事件id号
  184. uint16 SD_App_ProcessEvent( uint8 task_id, uint16 events )
  185. {
  186. afIncomingMSGPacket_t *MSGpkt;
  187. (void)task_id; // Intentionally unreferenced parameter
  188. if ( events & SYS_EVENT_MSG )//接收系统消息事件 再进行判断
  189. {
  190. //接收属于本应用任务SampleApp的消息,以SampleApp_TaskID标记
  191. MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SD_App_TaskID );
  192. //根据不同任务id号接收消息 在强制转换
  193. while ( MSGpkt )
  194. {
  195. switch ( MSGpkt->hdr.event )
  196. {
  197. case CMD_SERIAL_MSG:
  198. break;
  199. // Received when a messages is received (OTA) for this endpoint
  200. case AF_INCOMING_MSG_CMD: //接收数据事件,调用函数AF_DataRequest()接收数据
  201. //注册完了之后我们就能收到外面从这个端点进来的数据信息了
  202. SD_App_MessageMSGCB( MSGpkt );//调用回调函数对收到的数据进行处理
  203. break;
  204. case ZDO_STATE_CHANGE: //只要网络状态发生改变,就通过ZDO_STATE_CHANGE事件通知所有的任务。
  205. //同时完成对协调器,路由器,终端的设置
  206. SD_App_NwkState = (devStates_t)(MSGpkt->hdr.status);
  207. if ((SD_App_NwkState == DEV_ZB_COORD)//判断是否为协调器
  208. || (SD_App_NwkState == DEV_ROUTER)
  209. || (SD_App_NwkState == DEV_END_DEVICE))
  210. {
  211. if(SD_App_NwkState == DEV_ZB_COORD)
  212. {
  213. // LS164_BYTE(11);//'C'协调器
  214. HalUARTWrite(MY_DEFINE_UART_PORT ,"Coordinator_OK!!\r\n" , 18);
  215. }
  216. if(SD_App_NwkState == DEV_ROUTER)
  217. {
  218. // LS164_BYTE(12);//‘R’路由器
  219. HalUARTWrite(MY_DEFINE_UART_PORT ,"Router_OK!!\r\n" , 13);
  220. }
  221. if(SD_App_NwkState == DEV_END_DEVICE)
  222. {
  223. // LS164_BYTE(13);//‘E’终端
  224. HalUARTWrite(MY_DEFINE_UART_PORT ,"EndDevice_OK!!\r\n" ,16);
  225. }
  226. }
  227. else
  228. {
  229. // Device is no longer in the network
  230. }
  231. break;
  232. default:
  233. break;
  234. }
  235. // Release the memory 释放内存
  236. osal_msg_deallocate( (uint8 *)MSGpkt );
  237. // Next - if one is available 指针指向下一个放在缓冲区的待处理的事件,
  238. //返回while ( MSGpkt )判断重新处理事件,直到缓冲区没有等待处理事件为止
  239. MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SD_App_TaskID );
  240. }
  241. // return unprocessed events返回未处理的事件
  242. return (events ^ SYS_EVENT_MSG);
  243. }
  244. // Send a message out - This event is generated by a timer
  245. // (setup in SampleApp_Init()).
  246. if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )//发送消息-此事件由计时器生成
  247. {
  248. // Send the periodic message 处理周期性事件,
  249. //利用SampleApp_SendPeriodicMessage()处理完当前的周期性事件,然后启动定时器
  250. //开启下一个周期性事情,这样一种循环下去,也即是上面说的周期性事件了,
  251. //可以做为传感器定时采集、上传任务
  252. App_GatherTask();
  253. // Setup to send message again in normal period (+ a little jitter)
  254. osal_start_timerEx( SD_App_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
  255. (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) );//再次启动
  256. // 返回未处理的事件
  257. return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
  258. }
  259. return 0;
  260. }
  261. //接收数据,参数为接收到的数据
  262. void SD_App_MessageMSGCB( afIncomingMSGPacket_t *pkt )
  263. {
  264. //收到广播数据
  265. switch ( pkt->clusterId )//判断簇ID
  266. {
  267. case SD_APP_POINT_TO_POINT_CLUSTERID: //簇1
  268. break;
  269. case SD_APP_BROADCAST_CLUSTERID: //簇2
  270. break;
  271. }
  272. }
  273. /*********************************************************************
  274. *
  275. * 函数名: UartCallBackFunction
  276. * 函数功能:串口回调函数,接收到数据时会调用到该函数;
  277. * 传入参数:port:串口号 event:事件
  278. * 返回参数:无
  279. *
  280. *********************************************************************/
  281. static void UartCallBackFunction(uint8 port , uint8 event)
  282. {
  283. uint8 RX_Length = 0; //接收到字符串大小;
  284. RX_Length = Hal_UART_RxBufLen(MY_DEFINE_UART_PORT); //读取接收字符串大小;
  285. if(RX_Length != 0) //有数据存在;
  286. {
  287. //读取串口数据;
  288. HalUARTRead(MY_DEFINE_UART_PORT , RX_BUFFER , RX_Length);
  289. //发送回给电脑,使用 hal_uart.h 的接口函数:
  290. HalUARTWrite(MY_DEFINE_UART_PORT , RX_BUFFER , RX_Length);
  291. }
  292. }