本章节主要细讲协议栈组网部分及其实现方法,至此自己也回顾一下。
目前博主已经根据官方的协议栈改写了个简单的协议栈,后续会拿出来讲解分享。
文章是博主自己写的,如发现问题,请指正。
转摘请注明出处,谢谢 http://blog.csdn.net/libin55/article/details/52795997
一、简要概述
该网络有点类似TCP/IP里的服务器与客户端的概念,主节点AP为长供电设备。他时刻监听着整个网络(可以理解为服务器Server),而每台节点设备ED(可以理解为客户端Client)。要加入到这个网络,即与AP建立连接,必须要先进行三次握手。
握手校验通过后AP会为ED分配一个唯一的ID号,用来作为该通信链路的标识。
二、代码实现
一、AP主节点:
打开CC2530工程,上一章节我已经提到过了。查看mian函数
BSP_Init();//板级初始化
SMPL_Init(sCB); //初始化通信系统和simpliciti的协议栈
我重点讲下SMPL_Init这个函数,先贴下源码把(自己加了下备注),在nwk_api.c文件下
/***********************************************************************************
* @fn SMPL_Init
*
* @brief Initialize the SimpliciTI stack.
*
* input parameters
* @param f - Pointer to call back function. Function called by NWK when
* user application frame received. The callback is done in the
* ISR thread. Argument is Link ID associated with frame. Function
* returns 0 if frame is to be kept by NWK, otherwise 1. Frame
* should be kept if application will do a SMPL_Receive() in the
* user thread (recommended). Pointer may be NULL.
*
* output parameters
*
* @return Status of operation:
* SMPL_SUCCESS
* SMPL_NO_JOIN No Join reply. AP possibly not yet up.
* SMPL_NO_CHANNEL Only if Frequency Agility enabled. Channel scan
* failed. AP possibly not yet up.
*/
smplStatus_t SMPL_Init(uint8_t (*f)(linkID_t))
{
smplStatus_t rc;
if (!sInit_done)//防止重复初始化
{
/* set up radio. */
MRFI_Init();//射频初始化
/* initialize network */
if ((rc=nwk_nwkInit(f)) != SMPL_SUCCESS)//初始化网络参数
{
return rc;
}
MRFI_WakeUp();//唤醒RF设备
#if defined( FREQUENCY_AGILITY )//通信信道
{
freqEntry_t chan;
chan.logicalChan = 0;
/* ok to set default channel explicitly now that MRFI initialized. */
nwk_setChannel(&chan);
}
#endif
/* don't turn Rx on if we're an end device that isn't always on. */
/* but do turn the radio on for PLL operations */
#if !defined( END_DEVICE ) || defined( NWK_PLL )//开启接收
MRFI_RxOn();
#endif
#if defined( END_DEVICE )//主节点不开启地址滤波
/* All except End Devices are in promiscuous mode */
MRFI_SetRxAddrFilter((uint8_t *)nwk_getMyAddress());
MRFI_EnableRxAddrFilter();
#endif
}
sInit_done = 1;
#ifdef NWK_PLL//跳频使用,这里没用到
/* If the PLL is enabled then it must get running before the join
* request or the system may lock up in the join request becuase
* PLL is not locked in.
*/
// turn on the PLL
SMPL_Ioctl(IOCTL_OBJ_PLL, IOCTL_ACT_ON, NULL);
// reference clocks are by definition always locked.
#ifndef NWK_PLL_REFERENCE_CLOCK
// wait for a 5ms failure rate to be achieved
while( nwk_pllIsLocked( 0 ) == false )
nwk_pllBackgrounder( false );
#endif
#endif
/* Join. if no AP or Join fails that status is returned. */
rc = nwk_join();
return rc;
}
MRFI_Init();为射频初始化接口,TI在接口下面把自己的射频驱动已经写好了,如果需要移植到其他的片子的话,可以在这个接口下做更改。
MRFI_RxOn();开启接收,AP主节点因为要时刻监听网络,所以除了通信以外其他时间都是处于接收模式。
注意:因为TI射频片子通信属于半双工,即只能处于接收或者发送状态(区别于Zigbee)。
nwk_join();因为这里配置的是主节点AP,所有这个接口不做处理