第七次实验:CC2530平台上多跳通信的TinyOS编程

时间:2023-03-08 17:04:01
第七次实验:CC2530平台上多跳通信的TinyOS编程

module  P2MM

{

uses interface Boot;

uses interface Timer<TMilli> as Timer0;

uses interface SplitControl as AMControl;

uses interface AMPacket;

uses interface AMSend;

uses interface Receive;

uses interface Packet;

}

implementation

{

typedef nx_struct P2MMsg {nx_uint16_t nodeid[3];nx_uint16_t num;nx_uint16_t counter;}P2MMsg;

uint8_t counter=0;

bool busy =FALSE;

message_t pkt;

task void test() { }

/**********************************************

* 函数名:booted()

* 功  能:系统启动完毕后自动触发

* 参  数:无

***********************************************/

event void Boot.booted()

{

call AMControl.start();

}

/**********************************************

* 函数名:fired()

* 功  能:定时器触发事件

* 参  数:无

***********************************************/

event void Timer0.fired()

{

counter++;

if (!busy)

{

P2MMsg* btrpkt = (P2MMsg*)(call Packet.getPayload(&pkt, sizeof(P2MMsg)));

btrpkt->num=0;

btrpkt->nodeid[btrpkt->num] = TOS_NODE_ID;

btrpkt->counter = counter;

call AMPacket.setGroup(&pkt,TOS_IEEE_GROUP);

if (call AMSend.send(0xFFFF, &pkt, sizeof(P2MMsg)) == SUCCESS)

busy = TRUE;

}

}

/**********************************************

* 函数名:startDone(error_t err)

* 功  能:SplitControl控制启动完成事件

* 参  数:error_t err

***********************************************/

event void AMControl.startDone(error_t err)

{

if(err==SUCCESS)

{

if(TOS_NODE_ID!=0x1)

call Timer0.startPeriodic(1000);

}

else

call AMControl.start();

}

/**********************************************

* 函数名:sendDone(message_t* msg, error_t erro)

* 功  能:AM消息发送完毕事件

* 参  数:message_t* msg, error_t erro

***********************************************/

event void AMSend.sendDone(message_t* msg, error_t erro)

{

if (&pkt == msg)

busy = FALSE;

}

/**********************************************

* 函数名:receive(message_t* msg, void* payload, uint8_t len)

* 功  能:Receive接收事件

* 参  数:message_t* msg, void* payload, uint8_t len

***********************************************/

event message_t* Receive.receive(message_t* msg, void* payload, uint8_t len)

{

if (len == sizeof(P2MMsg))

{

P2MMsg* btrpkt = (P2MMsg*)payload;

DbgOut(9,"Data is %d,Start is %d,Other is %d,%d\r\n",(uint16_t)btrpkt->counter,

(uint16_t)btrpkt->nodeid[0],(uint16_t)btrpkt->nodeid[1],(uint16_t)btrpkt->nodeid[2]);

btrpkt->num++;

if(TOS_NODE_ID!=0x1){

if(btrpkt->num<3){

bool flag=TRUE;

uint16_t i;

for(i=0;i<btrpkt->num;i++){

if(btrpkt->nodeid[i] == TOS_NODE_ID)

flag=FALSE;

}

if(flag){

btrpkt->nodeid[btrpkt->num] = TOS_NODE_ID;

call AMSend.send(0xFFFF, msg, sizeof(P2MMsg));

}

}

}

}

}

event void AMControl.stopDone(error_t err)   { }

}

configuration P2MC {}

#define AM_DATA_TYPE  123

implementation

{

components P2MM as App;

components MainC;

App.Boot ->MainC;

components ActiveMessageC as AM; //消息组件

App.Packet -> AM.Packet;

App.AMPacket -> AM.AMPacket;

App.AMSend -> AM.AMSend[AM_DATA_TYPE];

App.Receive -> AM.Receive[AM_DATA_TYPE];

App.AMControl -> AM.SplitControl;

components new TimerMilliC () as Timer0;

App.Timer0 ->Timer0;

}

COMPONENT= P2MC

PFLAGS += -DUART_DEBUG

PFLAGS += -DUART_BAUDRATE=9600

include $(MAKERULES)

好课程内容。