SampleApp.c 12 KB

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