123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- #include "OSAL.h"
- #include "ZGlobals.h"
- #include "AF.h"
- #include "aps_groups.h"
- #include "ZDApp.h"
- #include "SampleApp.h"
- #include "SampleAppHw.h"
- #include "OnBoard.h"
- #include <math.h>
- #include "MT_UART.h"
- #include "MT_APP.h"
- #include "MT.h"
- #include "hal_uart.h"
- #include "hal_adc.h"
- #include <stdio.h>
- #include "string.h"
- #include <stdlib.h>
- #include "public.h"
- void App_GatherTask(void);
- const cId_t SD_App_ClusterList[SD_APP_MAX_CLUSTERS] =
- {
- SD_APP_BROADCAST_CLUSTERID,
- SD_APP_POINT_TO_POINT_CLUSTERID
- };
- const SimpleDescriptionFormat_t SD_App_SimpleDesc =
- {
- SD_APP_ENDPOINT,
- SD_APP_PROFID,
- SD_APP_DEVICEID,
- SD_APP_DEVICE_VERSION,
- SD_APP_FLAGS,
- SD_APP_MAX_CLUSTERS,
- (cId_t *)SD_App_ClusterList,
- SD_APP_MAX_CLUSTERS,
- (cId_t *)SD_App_ClusterList
- };
- endPointDesc_t SD_App_epDesc;
- uint8 SD_App_TaskID;
-
-
- devStates_t SD_App_NwkState;
- uint8 SD_App_TransID;
- afAddrType_t SD_App_Broadcast_DstAddr;
- afAddrType_t SD_App_Point_To_Point_DstAddr;
- void SD_App_MessageMSGCB( afIncomingMSGPacket_t *pckt );
- #define MY_DEFINE_UART_PORT 0
- #define RX_MAX_LENGTH 20
- uint8 RX_BUFFER[RX_MAX_LENGTH];
- void UartCallBackFunction(uint8 port , uint8 event);
- halUARTCfg_t uartConfig;
- void Uart_Config(void);
- void Uart_Config(void)
- {
- uartConfig.configured = TRUE;
- uartConfig.baudRate = HAL_UART_BR_115200;
- uartConfig.flowControl = FALSE;
- uartConfig.flowControlThreshold = 64;
- uartConfig.rx.maxBufSize = 128;
- uartConfig.tx.maxBufSize = 128;
- uartConfig.idleTimeout = 6;
- uartConfig.intEnable = TRUE;
- uartConfig.callBackFunc = UartCallBackFunction;
- }
- void SD_App_Init( uint8 task_id )
- {
- SD_App_TaskID = task_id;
- SD_App_NwkState = DEV_INIT;
- SD_App_TransID = 0;
-
- #if defined ( BUILD_ALL_DEVICES )
-
-
-
-
- if ( readCoordinatorJumper() )
- zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR;
- else
- zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER;
- #endif
- #if defined ( HOLD_AUTO_START )
-
-
-
- ZDOInitDevice(0);
- #endif
-
- Uart_Config();
- HalUARTOpen(MY_DEFINE_UART_PORT , &uartConfig);
- HalUARTWrite(MY_DEFINE_UART_PORT ,"HAL_UART_OK\r\n" , 15);
-
-
-
- SD_App_Broadcast_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;
- SD_App_Broadcast_DstAddr.endPoint = SD_APP_ENDPOINT;
- SD_App_Broadcast_DstAddr.addr.shortAddr = 0xFFFF;
- SD_App_Point_To_Point_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
- SD_App_Point_To_Point_DstAddr.endPoint = SD_APP_ENDPOINT;
- SD_App_Point_To_Point_DstAddr.addr.shortAddr = 0x0000;
- SD_App_epDesc.endPoint = SD_APP_ENDPOINT;
- SD_App_epDesc.task_id = &SD_App_TaskID;
- SD_App_epDesc.simpleDesc
- = (SimpleDescriptionFormat_t *)&SD_App_SimpleDesc;
- SD_App_epDesc.latencyReq = noLatencyReqs;
- afRegister( &SD_App_epDesc );
-
-
-
-
-
- osal_start_timerEx(SD_App_TaskID,
- SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
- SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT);
- P0DIR&=~0x20;
-
- P0DIR&=~0x80;
-
- InitTimer3();
- InitTimer4();
-
- }
- #define RT_Input P0_7
- uint8 SendData[10]={0};
- int IfgTimer=0;
- char RT_InFlag=0;
- uint16 ifg_Rt =0;
- char Send_Flag=0;
- static uint16 GetTime=0;
- void App_GatherTask(void)
- {
- SendData[0]=0xAA;
- if(++GetTime>80)
- {
- GetTime=80;
- if(RT_Input==1)
- {
- SendData[1]=1;
- ifg_Rt=0;
-
- }
- else
- {
- ifg_Rt++;
- if(ifg_Rt>36)
- {
- SendData[1]=0;
- ifg_Rt=0;
- }
- }
- }
- AF_DataRequest(&SD_App_Broadcast_DstAddr,
- &SD_App_epDesc,
- SD_APP_BROADCAST_CLUSTERID,
- 2,
- SendData,
- &SD_App_TransID,
- AF_DISCV_ROUTE,
- AF_DEFAULT_RADIUS );
-
- }
- uint16 SD_App_ProcessEvent( uint8 task_id, uint16 events )
- {
- afIncomingMSGPacket_t *MSGpkt;
- (void)task_id;
- if ( events & SYS_EVENT_MSG )
- {
-
- MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SD_App_TaskID );
-
- while ( MSGpkt )
- {
- switch ( MSGpkt->hdr.event )
- {
-
- case CMD_SERIAL_MSG:
- break;
-
-
- case AF_INCOMING_MSG_CMD:
-
- SD_App_MessageMSGCB( MSGpkt );
- break;
- case ZDO_STATE_CHANGE:
-
- SD_App_NwkState = (devStates_t)(MSGpkt->hdr.status);
- if ((SD_App_NwkState == DEV_ZB_COORD)
- || (SD_App_NwkState == DEV_ROUTER)
- || (SD_App_NwkState == DEV_END_DEVICE))
- {
- if(SD_App_NwkState == DEV_ZB_COORD)
- {
-
- HalUARTWrite(MY_DEFINE_UART_PORT ,"Coordinator_OK!!\r\n" , 18);
- }
- if(SD_App_NwkState == DEV_ROUTER)
- {
-
- HalUARTWrite(MY_DEFINE_UART_PORT ,"Router_OK!!\r\n" , 13);
- }
- if(SD_App_NwkState == DEV_END_DEVICE)
- {
-
- HalUARTWrite(MY_DEFINE_UART_PORT ,"EndDevice_OK!!\r\n" ,16);
- }
- }
- else
- {
-
- }
- break;
- default:
- break;
- }
-
- osal_msg_deallocate( (uint8 *)MSGpkt );
-
-
- MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SD_App_TaskID );
- }
-
- return (events ^ SYS_EVENT_MSG);
- }
-
-
-
- if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
- {
-
-
-
-
- App_GatherTask();
-
- osal_start_timerEx( SD_App_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
- (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) );
-
-
- return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
- }
- return 0;
- }
- void SD_App_MessageMSGCB( afIncomingMSGPacket_t *pkt )
- {
-
- switch ( pkt->clusterId )
- {
- case SD_APP_POINT_TO_POINT_CLUSTERID:
- break;
- case SD_APP_BROADCAST_CLUSTERID:
- break;
- }
- }
- static void UartCallBackFunction(uint8 port , uint8 event)
- {
- uint8 RX_Length = 0;
-
- RX_Length = Hal_UART_RxBufLen(MY_DEFINE_UART_PORT);
-
- if(RX_Length != 0)
- {
-
- HalUARTRead(MY_DEFINE_UART_PORT , RX_BUFFER , RX_Length);
-
- HalUARTWrite(MY_DEFINE_UART_PORT , RX_BUFFER , RX_Length);
-
- }
- }
|