如需要绘制PCB,设计电路可以和我联系。sunhenu@163.com.
分类: 嵌入式
2013-04-20 12:31:01
原文地址:SimpleApp程序中的部分代码说明 作者:frankzfz
uint8 startOptions;
sapi_TaskID = task_id;
sapi_bindInProgress = 0xffff;
sapi_epDesc.endPoint = zb_SimpleDesc.EndPoint;
sapi_epDesc.task_id = &sapi_TaskID;
sapi_epDesc.simpleDesc = (SimpleDescriptionFormat_t *)&zb_SimpleDesc;
sapi_epDesc.latencyReq = noLatencyReqs;
// Register the endpoint/interface description with the AF
afRegister( &sapi_epDesc );
afSetMatch(sapi_epDesc.simpleDesc->EndPoint, FALSE);
ZDO_RegisterForZDOMsg( sapi_TaskID, NWK_addr_rsp );
ZDO_RegisterForZDOMsg( sapi_TaskID, Match_Desc_rsp );
#if (defined HAL_KEY) && (HAL_KEY == TRUE)
RegisterForKeys( sapi_TaskID );
if ( HalKeyRead () == HAL_KEY_SW_5)
// If SW5 is pressed and held while powerup, force auto-start and nv-restore off and reset
startOptions = ZCD_STARTOPT_CLEAR_STATE | ZCD_STARTOPT_CLEAR_CONFIG;
zb_WriteConfiguration( ZCD_NV_STARTUP_OPTION, sizeof(uint8), &startOptions );
zb_SystemReset();
osal_set_event(task_id, ZB_ENTRY_EVENT);
UINT16 SAPI_ProcessEvent( byte task_id, UINT16 events )
osal_event_hdr_t *pMsg;
afIncomingMSGPacket_t *pMSGpkt;
afDataConfirm_t *pDataConfirm;
if ( events & SYS_EVENT_MSG ) //强制事件,也是系统定义好的事件,下面有很多子事件
{
pMsg = (osal_event_hdr_t *) osal_msg_receive( task_id );
SAPI_ProcessZDOMsgs( (zdoIncomingMsg_t *)pMsg );
// This message is received as a confirmation of a data packet sent. The status is of ZStatus_t type [defined in ZComDef.h]
// The message fields are defined in AF.h
//数据包发送确认信息,这些状态值定义在ZcomDef.h文件中,该信息定义在AF.h中
pDataConfirm = (afDataConfirm_t *) pMsg;
SAPI_SendDataConfirm( pDataConfirm->transID, pDataConfirm->hdr.status );
SAPI_ReceiveDataIndication( pMSGpkt->srcAddr.addr.shortAddr,pMSGpkt->clusterId, pMSGpkt->cmd.DataLength, pMSGpkt->cmd.Data);
// If the device has started up, notify the application 向应用通报设备启动
if (pMsg->status == DEV_END_DEVICE ||
else if (pMsg->status == DEV_HOLD ||
SAPI_AllowBindConfirm( ((ZDO_MatchDescRspSent_t *)pMsg)->nwkAddr );
zb_HandleKeys( ((keyChange_t *)pMsg)->state, ((keyChange_t *)pMsg)->keys );
break;
SAPI_SendDataConfirm(
(uint8)((sapi_CbackEvent_t *)pMsg)->data,
((sapi_CbackEvent_t *)pMsg)->hdr.status );
SAPI_BindConfirm(
((sapi_CbackEvent_t *)pMsg)->data,
((sapi_CbackEvent_t *)pMsg)->hdr.status );
SAPI_StartConfirm( ((sapi_CbackEvent_t *)pMsg)->hdr.status );
//用户信息处理
// User messages should be handled by user or passed to the application
if ( pMsg->event >= ZB_USER_MSG )
pMsg = (osal_event_hdr_t *) osal_msg_receive( task_id );
// Return unprocessed events 返回没有处理的事件
}
if ( events & ZB_ALLOW_BIND_TIMER ) //允许绑定时间事件,也就是绑定时间到,没有绑定成功时执行
{
afSetMatch(sapi_epDesc.simpleDesc->EndPoint, FALSE);
}
if ( events & ZB_BIND_TIMER ) //绑定时间
{
// Send bind confirm callback to application
SAPI_BindConfirm( sapi_bindInProgress, ZB_TIMEOUT );
}
if ( events & ZB_ENTRY_EVENT )
{
// Give indication to application of device startup 提示用户设备已经启动
// LED off cancels HOLD_AUTO_START blink set in the stack 点高蓝色指示灯
zb_ReadConfiguration( ZCD_NV_STARTUP_OPTION, sizeof(uint8), &startOptions );
if ( startOptions & ZCD_STARTOPT_AUTO_START )
// blink leds and wait for external input to config and restart 再次启动设备 加入网络
}
// This must be the last event to be processed
if ( events & ( ZB_USER_EVENTS ) )
{
// User events are passed to the application 处理用户自己定义的事件
// Do not return here, return 0 later
}
// Discard unknown events 丢弃未知的事件
return 0;
事件 |
处理函数 | ||
系统消息事件
SYS_ENTRY_MSG |
ZDO反馈
ZDO_CB_MSG |
SAPI_ProcessZDOMsgs() | |
AF 数据确认
AF_DATA_CONFIRM_CMD |
SAPI_SendDataConfirm() | ||
AF 信息输入
AF_INCOMING_MSG_CMD |
SAPI_ReceiveDataIndication() | ||
ZDO 状态改变
ZDO_STATE_CHANGE |
SAPI_StartConfirm() | ||
ZDO 匹配描述符响应
ZDO_MATCH_DESC_RSP_SENT |
SAPI_AllowBindConfirm() | ||
按键
KEY_CHANGE |
zb_HandleKeys() | ||
发送数据确认
SAPICB_DATA_CNF |
SAPI_SendDataConfirm() | ||
绑定确认
SAPICB_BIND_CNF |
SAPI_BindConfirm() | ||
设备启动确认
SAPICB_START_CNF |
SAPI_StartConfirm() | ||
允许绑定时间事件
ZB_ALLOW_BIND_TIMER |
afSetMatch() | ||
绑定时间事件
ZB_BIND_TIMER |
SAPI_BindConfirm() | ||
进入事件
ZB_ENTRY_EVENT |
zb_StartRequest() |
uint8 capabilities;
// Save the task ID 初始化任务ID,其中task_id由操作系统初始化任务函数osalInitTasks()决定
ZDAppTaskID = task_id;
ZDAppNwkAddr.addrMode = Addr16Bit;
ZDAppNwkAddr.addr.shortAddr = INVALID_NODE_ADDR;
(void)NLME_GetExtAddr(); // 保存64位IEEE地址。
// Check for manual "Hold Auto Start" 检测是否阻止自动启动
ZDAppCheckForHoldKey();
// Initialize ZDO items and setup the device - type of device to create.
ZDO_Init();
// Register the endpoint description with the AF
// This task doesn't have a Simple description, but we still need
// to register the endpoint. 注册端点0
afRegister( (endPointDesc_t *)&ZDApp_epDesc );
ZDApp_InitUserDesc();
// set broadcast address mask to support broadcast filtering
NLME_GetRequest(nwkCapabilityInfo, 0, &capabilities);
NLME_SetBroadcastFilter( capabilities );
// Start the device? 启动设备
if ( devState != DEV_HOLD )
ZDOInitDevice( 0 );
else
// Blink LED to indicate HOLD_START
HalLedBlink ( HAL_LED_4, 0, 50, 500 );
ZDApp_RegisterCBs(); //注册响应事件
事件 |
处理函数 | ||
系统消息事件
SYS_ENTRY_MSG |
AF 信息输入
AF_INCOMING_MSG_CMD |
ZDP_IncomingData() | |
ZDO 反馈
AF_DATA_CONFIRM_CMD |
ZDApp_ProcessMsgCBs() | ||
AF 数据确认
AF_DATA_CONFIRM_CMD |
无 | ||
网络发现确认
ZDO_NWK_DISC_CNF |
NLME_JoinRequest()或
NLME_ReJoinRequest() | ||
网络加入指示
ZDO_NWK_JOIN_IND |
ZDApp_ProcessNetworkJoin() | ||
网络加入请求
ZDO_NWK_JOIN_REQ |
ZDApp_NetworkInit( ) | ||
网络初始化
ZDO_NETWORK_INIT |
ZDO_StartDevice() | ||
网络启动
ZDO_NETWORK_START |
ZDApp_NetworkStartEvt() | ||
路由启动
ZDO_ROUTER_START |
osal_pwrmgr_device() | ||
状态改变
ZDO_STATE_CHANGE_EVT |
ZDO_UpdateNwkStatus() | ||
网络NV 更新
ZDO_NWK_UPDATE_NV |
ZDApp_SaveNetworkStateEvt() | ||
设备重新启动
ZDO_DEVICE_RESET |
SystemResetSoft() |
在f8wCoord.cfg文件中,我们可以看到,有下面的编译选项,协调器的设置,应该是表示
#define ZDO_COORDINATOR
// Values for ZCD_NV_LOGICAL_TYPE (zgDeviceLogicalType)
#define DEVICE_LOGICAL_TYPE ZG_DEVICETYPE_SOFT
#define DEVICE_LOGICAL_TYPE ZG_DEVICETYPE_COORDINATOR
#define DEVICE_LOGICAL_TYPE ZG_DEVICETYPE_ROUTER
#define DEVICE_LOGICAL_TYPE ZG_DEVICETYPE_ENDDEVICE
#if defined( ZDO_COORDINATOR ) && !defined( SOFT_START )
// Set the default to coodinator
devStartModes_t devStartMode = MODE_HARD;