SampleApp.c 12 KB

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