TI中文支持网
TI专业的中文技术问题搜集分享网站

cc2530的板子,zigbee协议栈开发,节点网络断开,没法调用系统时间执行相应程序,终端代码如下

#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"
#include "common.h"
#include "DebugTrace.h"
#include "OnBoard.h"
#include "aps_groups.h"
#include "NLMEDE.h"
//#include "iic.h"
/*HAL*/
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_uart.h"
#include "OSAL_Nv.h"
#include "MT_UART.h"
#include "MT_APP.h"
#include "MT.h"
#include "OSAL_Clock.h" 
#include "hal_drivers.h"


#include "DHT11.h"
#include "SW-420.h"
#include "ADXL345.h"
#include "SD_Card.h"
#include "ff.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char headbyte[4];

FRESULT res;//FatFs函数执行结果
UINT bw,br;//写入和读出数据字节计数器
DWORD free_clust;//空簇,空扇区大小
FATFS fs;// Work area (file system object) for logical drive
FIL fsrc;// 文件结构
DIR dirs;
FILINFO fno;

static uint8 fileFlag = 0;

const cId_t TestApp_ClusterList[TestApp_MAX_CLUSTERS] = 
{TestApp_PERIODIC_CLUSTERID,TestApp_FLASH_CLUSTERID,//TestApp_P2P_CLUSTERID
};


const SimpleDescriptionFormat_t TestApp_SimpleDesc =
{TestApp_ENDPOINT,//  int Endpoint;TestApp_PROFID,//  uint16 AppProfId[2];TestApp_DEVICEID,//  uint16 AppDeviceId[2];TestApp_DEVICE_VERSION,//  intAppDevVer:4;TestApp_FLAGS,//  intAppFlags:4;TestApp_MAX_CLUSTERS,//  byte  AppNumInClusters;(cId_t *)TestApp_ClusterList,  //  byte *pAppInClusterList;TestApp_MAX_CLUSTERS,//  byte  AppNumInClusters;(cId_t *)TestApp_ClusterList  

};

endPointDesc_t TestApp_epDesc;
byte TestApp_TaskID;
byte TestApp_TransID;
devStates_t TestApp_NwkState;

afAddrType_t TestApp_Periodic_DstAddr;//广播
afAddrType_t TestApp_Flash_DstAddr; //组播
afAddrType_t TestApp_P2P_DstAddr ; //点播

aps_Group_t  TestApp_Group;

uint8 TestApp_PeriodicCounter = 0;
uint8 TestApp_FlashCounter = 0;

uint8 LedState = 0;

#define MAX_BUFFER_LENGTH 100
 char str[MAX_BUFFER_LENGTH] = {0};
 
void TestApp_Init( uint8 task_id );
uint16 TestApp_ProcessEvent( uint8 task_id,uint16 events );
void TestAPP_MessageMSGCB( afIncomingMSGPacket_t *pkt );
void TestApp_HandleKeys( uint8 shift,uint8 keys );
void TestApp_send_sensordata( uint8 sensor_type );
void TestApp_SendPeriodicMessage( void );
void TestApp_SendFlashMessage(uint16 flashTime);
void TestApp_Send_P2P_Message( uint16 shortAddr,uint8 *data,uint16 len );
void ShowInfo(void);
void TestApp_send_sensordata( uint8 sensor_type );


/***********************************************************************
*local function
*/
 
void TestApp_Init( uint8 task_id )
{TestApp_TaskID = task_id;TestApp_NwkState = DEV_INIT;TestApp_TransID = 0;// Broadcast to everyone 发送模式:广播发送TestApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;TestApp_Periodic_DstAddr.endPoint = TestApp_ENDPOINT;TestApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF;// Broadcast to everyone 发送模式:组播发送TestApp_Flash_DstAddr.addrMode = (afAddrMode_t)AddrGroup;TestApp_Flash_DstAddr.endPoint = TestApp_ENDPOINT;TestApp_Flash_DstAddr.addr.shortAddr = TestApp_FLASH_GROUP;// Broadcast to everyone 发送模式:点播发送TestApp_P2P_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;TestApp_P2P_DstAddr.endPoint = TestApp_ENDPOINT;TestApp_P2P_DstAddr.addr.shortAddr = TestApp_COORD_SHORTADDR;TestApp_epDesc.endPoint = TestApp_ENDPOINT; //定义本设备用来通信的APS层端点描述符TestApp_epDesc.task_id = &TestApp_TaskID; //任务描述符IDTestApp_epDesc.simpleDesc = (SimpleDescriptionFormat_t *)&TestApp_SimpleDesc; //简单描述符TestApp_epDesc.latencyReq = noLatencyReqs;  //延时策略afRegister(&TestApp_epDesc); //向AF层登记描述符RegisterForKeys( TestApp_TaskID ); //登记所有按键事件TestApp_Group.ID = TestApp_FLASH_GROUP;osal_memcpy(TestApp_Group.name,"Group 1",7);aps_AddGroup(TestApp_ENDPOINT,&TestApp_Group);

#if defined ( LCD_SUPPORTED )HalLcdWriteString( "TestApp", HAL_LCD_LINE_1 ); //如果支持LCD,显示提示信息
#endif//------------------------配置串口---------------------------------MT_UartInit();//串口初始化MT_UartRegisterTaskID(task_id);//注册串口任务osal_memset( str ,0,MAX_BUFFER_LENGTH );printf( "Zigbee endDevice OK \n" );
 
//  osalTimeUpdate();HalUARTPoll();  // This replaces MT_SerialPoll() and osal_check_timer().//-----------------------------------------------------------------//ASXL345初始化//IIC_INIT();ADXL345_Init();printf( "ADXL345 ID: 0x%02x\n" , (char)ADXL345_GetDeviceID() );HalUARTPoll();//温湿度DHT11 初始化DHT111_Init();//震动传感器初始化SW_240_Init();register_SW_420ID( TestApp_TaskID );if(SD_Init()){printf("mmc init fail\n");HalUARTPoll();}else{printf("mmc size:%lu\n",SD_GetCapacity());if( f_mount(0,&fs)==0 ){printf( "fs_mount ok\n" );HalUARTPoll();}else{printf( "fs_mount error\n" );HalUARTPoll();}}
}


/******************************************************************************
* @function TestApp_ProcessEvent
* @brief  Application task process. this function is called to process all events for the task.
*events include timers messages and any other user defined event
* @param  task_id - the OSAL assigned task ID.
*events  - events to process .this is a bit map and can contain more than one event.
* @return none
*/

uint16 TestApp_ProcessEvent( uint8 task_id,uint16 events )
{afIncomingMSGPacket_t *MSGpkt;if( events & SYS_EVENT_MSG ){MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( TestApp_TaskID );while( MSGpkt ){switch( MSGpkt->hdr.event ){case KEY_CHANGE:printf("key\n");TestApp_HandleKeys( ((keyChange_t *)MSGpkt)->state,((keyChange_t *)MSGpkt)->keys);break;case AF_INCOMING_MSG_CMD:TestAPP_MessageMSGCB(MSGpkt);break;case ZDO_STATE_CHANGE:TestApp_NwkState = (devStates_t)(MSGpkt->hdr.status);if ( TestApp_NwkState== DEV_END_DEVICE ){printf("connection\n\r");HalUARTPoll();res=f_opendir(&dirs, "0:/");osal_set_event( TestApp_TaskID ,SHOW_INFO_EVENT );osal_set_event( TestApp_TaskID ,SD_EVENT );}if ( TestApp_NwkState == DEV_INIT ){printf("disconnection\n\r");HalUARTPoll();res = f_open(&fsrc,"0:/mao.txt",FA_OPEN_ALWAYS | FA_WRITE );printf("open file1 /mao.txt %u\n",res);printf("network disconnection\r\n");osal_start_timerEx( TestApp_TaskID,SENSOR_LOOP_EVENT,TestApp_SENSOR_LOOP_TIMEOUT );}break;default:break;}osal_msg_deallocate((uint8 *)MSGpkt);MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive(TestApp_TaskID);}return ( events ^ SYS_EVENT_MSG );}if( events & SHOW_INFO_EVENT){ShowInfo();return ( events ^ SHOW_INFO_EVENT );}if( events & SD_EVENT ){if( fileFlag == 0 ){if( (f_readdir(&dirs,&fno) == FR_OK) && fno.fname[0] ){printf("sd success\n\r");res = f_open(&fsrc,fno.fname,FA_OPEN_ALWAYS | FA_READ );fileFlag = 1;}else{printf("sd fail\n\r");osal_start_timerEx(TestApp_TaskID,SENSOR_LOOP_EVENT,TestApp_SENSOR_LOOP_TIMEOUT);}}if( fileFlag == 1 ){if( fsrc.fptr < fsrc.fsize ){osal_memset( str,0,MAX_BUFFER_LENGTH );res = f_read( &fsrc,headbyte,sizeof(headbyte),&br );res = f_read( &fsrc,str,strtol((const char *)headbyte,NULL,10),&br );TestApp_Send_P2P_Message( TestApp_COORD_SHORTADDR,(uint8 *)str,strlen( (const char *)str ));HalUARTWrite(0,(uint8 *)str,strlen((const char *)str));}else{fileFlag = 0;res = f_unlink(fno.fname);}osal_set_event(TestApp_TaskID,SD_EVENT);}return ( events ^ SD_EVENT );}if( events & SENSOR_LOOP_EVENT){TestApp_send_sensordata( TEMPERATURE_DHT11 );TestApp_send_sensordata( ADXL345 );osal_start_timerEx( TestApp_TaskID,SENSOR_LOOP_EVENT,TestApp_SENSOR_LOOP_TIMEOUT );return ( events ^ SENSOR_LOOP_EVENT );}if( events & TestApp_SW_420_INIT_EVENT ){SW_420_enable_int();return ( events ^ TestApp_SW_420_INIT_EVENT );}if( events & TestApp_SW_420_EVENT ){TestApp_send_sensordata( SHOCK );return ( events ^ TestApp_SW_420_EVENT );}return 0;
}
void TestAPP_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{byte buf;uint8 sensor_type;switch(pkt->clusterId){case TestApp_PERIODIC_CLUSTERID:HalUARTWrite(0, "PERIODIC Rx:", 12);HalUARTWrite(0, pkt->cmd.Data, pkt->cmd.DataLength);break;case TestApp_FLASH_CLUSTERID:osal_memcpy(&buf,pkt->cmd.Data,1);if( buf == 1){HalLedSet(HAL_LED_1,HAL_LED_MODE_ON);}else{HalLedSet(HAL_LED_1,HAL_LED_MODE_OFF);}break;case TestApp_P2P_CLUSTERID:if( strstr((char *)pkt->cmd.Data,(char *)sensor_name[TEMPERATURE_DHT11])){sensor_type = TEMPERATURE_DHT11;goto result;}else if( strstr((char *)pkt->cmd.Data,(char *)sensor_name[HUMIDITY_DHT11])){sensor_type = HUMIDITY_DHT11;goto result;}else if( strstr((char *)pkt->cmd.Data,(char *)sensor_name[ADXL345])){sensor_type = ADXL345;goto result;}else if( strstr((char *)pkt->cmd.Data,(char *) sensor_name[SHOCK])){sensor_type = SHOCK;goto result;}elsereturn;result:printf("P2P RX: %s\n",pkt->cmd.Data);// HalUARTWrite(0, pkt->cmd.Data, pkt->cmd.DataLength);TestApp_send_sensordata( sensor_type );break;break;}

}
void TestApp_HandleKeys( uint8 shift,uint8 keys )
{(void)shift;if( keys & HAL_KEY_SW_1 ){HalUARTWrite(0,"send key1",9);HalUARTPoll();TestApp_SendFlashMessage(0);}if( keys & HAL_KEY_SW_6 ){aps_Group_t *grp;grp = aps_FindGroup( TestApp_ENDPOINT,TestApp_FLASH_GROUP );if(grp){aps_RemoveGroup(TestApp_ENDPOINT,TestApp_FLASH_GROUP);}else{aps_AddGroup(TestApp_ENDPOINT,&TestApp_Group);}}

}


void TestApp_send_sensordata( uint8 sensor_type )
{ADXL345_Data_T *ADXL345_Data;printf("data\n\r");osal_memset( str,0,MAX_BUFFER_LENGTH);if( sensor_type == TEMPERATURE_DHT11 || sensor_type == HUMIDITY_DHT11 ){DHT11_start();sprintf( str,"wendu: %d%d , shidu: %d%d\n\r",wendu_shi,wendu_ge,shidu_shi,shidu_ge );goto result;}else if( sensor_type == ADXL345 ){//ADXL345初始化ADXL345_Data = ADXL345_GetRawAccelGyro();sprintf( str, "%s: ax:%.2f ay:%.2f az:%.2f mg#\r\n",sensor_name[sensor_type], ADXL345_Data->accel_x , ADXL345_Data->accel_y , ADXL345_Data->accel_z );goto result;}else if( sensor_type == SHOCK ){printf("SHOCK\n");sprintf( str,"%s: %c\r\n",sensor_name[SHOCK],get_sw_420_state() );goto result;}elsereturn;result:if( TestApp_NwkState == DEV_INIT ){printf("DEV_INIT\n");HalUARTPoll();res = f_lseek( &fsrc,fsrc.fsize );sprintf( headbyte,"%-3u ",strlen((const char *)str) );res = f_write( &fsrc,headbyte,sizeof(headbyte),&bw );res = f_write( &fsrc,str,strlen((const char *)str),&bw );res = f_sync( &fsrc );}else if( TestApp_NwkState == DEV_END_DEVICE ){TestApp_Send_P2P_Message( TestApp_COORD_SHORTADDR,(uint8 *)str,strlen( (const char *)str ));}HalUARTWrite(0,(uint8 *)str,strlen( (const char *)str ));HalUARTPoll();if( sensor_type == SHOCK ){osal_start_timerEx( TestApp_TaskID,TestApp_SW_420_INIT_EVENT,TestApp_SW_420_IT_TIMEOUT );}
}


void TestApp_SendFlashMessage(uint16 flashTime)
{LedState = ~LedState;if( AF_DataRequest( &TestApp_Flash_DstAddr ,&TestApp_epDesc,TestApp_FLASH_CLUSTERID,1,&LedState,&TestApp_TransID,AF_DISCV_ROUTE,AF_DEFAULT_RADIUS ) == afStatus_SUCCESS ){if( LedState == 0 ){HalLedSet( HAL_LED_1,HAL_LED_MODE_ON );}else{HalLedSet( HAL_LED_1,HAL_LED_MODE_OFF );}}else{}

}


void TestApp_SendPeriodicMessage( void )
{AF_DataRequest( &TestApp_Periodic_DstAddr,&TestApp_epDesc,TestApp_PERIODIC_CLUSTERID,1,(uint8*)&TestApp_PeriodicCounter,&TestApp_TransID,AF_DISCV_ROUTE,AF_DEFAULT_RADIUS );
}


void TestApp_Send_P2P_Message( uint16 shortAddr,uint8 *data,uint16 len )
{printf("end send\n\r");//TestApp_P2P_DstAddr.addr.shortAddr = shortAddr;TestApp_P2P_DstAddr.addr.shortAddr = TestApp_COORD_SHORTADDR;if(AF_DataRequest( &TestApp_P2P_DstAddr,&TestApp_epDesc,TestApp_P2P_CLUSTERID,len,data,&TestApp_TransID,AF_DISCV_ROUTE,AF_DEFAULT_RADIUS) == afStatus_SUCCESS){}else{// Error occurred in request to send.}

}
void ShowInfo(void)
{uint16 myShortAddr;uint8 myMAC[8];myShortAddr = NLME_GetShortAddr();//得到自己的网络地址osal_memcpy(myMAC,NLME_GetExtAddr(),8);sprintf( (char *)str,"NWK:%x MAC:%x%x%x%x\n",myShortAddr,(uint16)myMAC[6],(uint16)myMAC[4], (uint16)myMAC[2],(uint16)myMAC[0]);HalUARTWrite(0,(uint8 *)str,strlen((const char *)str));TestApp_Send_P2P_Message( TestApp_COORD_SHORTADDR,(uint8 *)str,strlen((const char *)str) );HalUARTPoll();
}

//、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、终端节点和协调器断开时不执行下面这段代码,这是为什么啊

  if ( TestApp_NwkState == DEV_INIT ){printf("disconnection\n\r");HalUARTPoll();res = f_open(&fsrc,"0:/mao.txt",FA_OPEN_ALWAYS | FA_WRITE );printf("open file1 /mao.txt %u\n",res);printf("network disconnection\r\n");osal_start_timerEx( TestApp_TaskID,SENSOR_LOOP_EVENT,TestApp_SENSOR_LOOP_TIMEOUT );}

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

然而当连接上协调器时,下面这段代码却能正常执行

if ( TestApp_NwkState== DEV_END_DEVICE ){printf("connection\n\r");HalUARTPoll();res=f_opendir(&dirs, "0:/");osal_set_event( TestApp_TaskID ,SHOW_INFO_EVENT );osal_set_event( TestApp_TaskID ,SD_EVENT );}

、、、、、、、、、、、、、、、、、、、、、、、、、、、

求各位牛人解答下

gaoyang9992006:

那个状态变量应该跟协调器是有关系的,可以查看一下那个变量寄存器的定义与说明。

赞(0)
未经允许不得转载:TI中文支持网 » cc2530的板子,zigbee协议栈开发,节点网络断开,没法调用系统时间执行相应程序,终端代码如下
分享到: 更多 (0)