第一次写博客,觉得应该把学习的心得体会记下来!长期更新。希望能帮助到新手少走弯路,也希望能提出问题,大家一起讨论,一起进步。转载请标明出处。谢 谢 http://blog.csdn.net/libin55/article/details/51203727
void main (void)
{
bspIState_t intState;
#ifdef FREQUENCY_AGILITY
memset(sSample, 0x0, sizeof(sSample));
#endif
BSP_Init();
/* If an on-the-fly device address is generated it must be done before the
* call to SMPL_Init(). If the address is set here the ROM value will not
* be used. If SMPL_Init() runs before this IOCTL is used the IOCTL call
* will not take effect. One shot only. The IOCTL call below is conformal.
*/
#ifdef I_WANT_TO_CHANGE_DEFAULT_ROM_DEVICE_ADDRESS_PSEUDO_CODE
{
addr_t lAddr;
createRandomAddress(&lAddr);
SMPL_Ioctl(IOCTL_OBJ_ADDR, IOCTL_ACT_SET, &lAddr);
}
#endif /* I_WANT_TO_CHANGE_DEFAULT_ROM_DEVICE_ADDRESS_PSEUDO_CODE */
SMPL_Init(sCB); //初始化通信系统和simpliciti的协议栈
/* green and red LEDs on solid to indicate waiting for a Join. */
if (!BSP_LED2_IS_ON())
{
toggleLED(2);
}
if (!BSP_LED1_IS_ON())
{
toggleLED(1);
}
/* main work loop */
while (1)
{
/* manage FHSS schedule if FHSS is active */
FHSS_ACTIVE( nwk_pllBackgrounder( false ) );
/* Wait for the Join semaphore to be set by the receipt of a Join frame from a
* device that supports an End Device.
*
* An external method could be used as well. A button press could be connected
* to an ISR and the ISR could set a semaphore that is checked by a function
* call here, or a command shell running in support of a serial connection
* could set a semaphore that is checked by a function call.
*/
if (sJoinSem && (sNumCurrentPeers < NUM_CONNECTIONS))
{
/* listen for a new connection */
while (1)
{
if (SMPL_SUCCESS == SMPL_LinkListen(&sLID[sNumCurrentPeers]))
{
break;
}
/* Implement fail-to-link policy here. otherwise, listen again. */
}
sNumCurrentPeers++;
BSP_ENTER_CRITICAL_SECTION(intState);
sJoinSem--;
BSP_EXIT_CRITICAL_SECTION(intState);
}
/* Have we received a frame on one of the ED connections?
* No critical section -- it doesn't really matter much if we miss a poll
*/
if (sPeerFrameSem)
{
uint8_t msg[MAX_APP_PAYLOAD], len, i;
/* process all frames waiting */
for (i=0; i= 0xF)
{
sBlinky = 1;
toggleLED(1);
toggleLED(2);
}
}
BSP_EXIT_CRITICAL_SECTION(intState);
}
}
main函数最开始是初始化板级支持包Bsp_Init();
接着初始化协议栈SMPL_Init(sCB);
这里sCB对应的是回调函数,针对AP来说这个是必须加的,因为AP(协调器)是肯定要接收ED(节点)的数据的。而对于只上报的节点设备,sCB就可以忽略。
整个程序的核心在接收中断里,很庞大,简单解说就是通过数据帧剥离出端口,后面调用回调函数sCB,向应用层提示本帧数据是Join帧还是通用的数据帧。在main.c里面定义了两个全局变量。
static volatile uint8_t sPeerFrameSem = 0; /* 数据帧标识,每接收到一桢新数据就会通过回调加一,然后主循环查询进行数据的读取
static volatile uint8_t sJoinSem = 0; /* Join帧标识变量,ED请求加入时会通过回调加一,主循环查询后进行LINK的监听*/
static uint8_t sCB(linkID_t lid)
{
if (lid)
{
sPeerFrameSem++;
sBlinky = 0;
}
else
{
sJoinSem++;
}
/* leave frame to be read by application. */
return 0;
}