-
Notifications
You must be signed in to change notification settings - Fork 1
/
nRF24l01plus.cpp
184 lines (165 loc) · 4.8 KB
/
nRF24l01plus.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "nRF24l01plus.h"
#include "ether.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//constructor
nRF24l01plus::nRF24l01plus(QObject *parent,Ether * someEthar):nRF24interface(parent),coalision(false),theEther(someEthar)
{
//ctor
theTimer = new QTimer(this);
connect(this,SIGNAL(CEsetHIGH()),this,SLOT(CEsetHIGH()));
connect(this,SIGNAL(TXmodeSet()),this,SLOT(TXmodeSet()));
connect(this,SIGNAL(PWRUPset()),this,SLOT(PWRUPset()));
connect(this,SIGNAL(TXpacketAdded()),this,SLOT(TXpacketAdded()));
connect(this->theTimer,SIGNAL(timeout()),this,SLOT(noACKalarm()));
connect(theEther,SIGNAL(dispachMsg(tMsgFrame*)),this,SLOT(reciveMsgFromET(tMsgFrame*)));
connect(this,SIGNAL(sendMsgToET(tMsgFrame*)),theEther,SLOT(sendMSG(tMsgFrame*)));
connect(theEther,SIGNAL(coalisionSig()),this,SLOT(setCoalision()));
}
nRF24l01plus::~nRF24l01plus()
{
//dtor
}
/*
* Start transmision...
*
*/
void nRF24l01plus::startPTX()
{ //
if(getCE() == false || isPWRUP() == false)return;
if(isRX_MODE())return;
tMsgFrame * packetToSend = getTXpacket();
if(packetToSend == NULL)return;
//packet found in TX that is not ACK packet
//load with TX address
packetToSend->Address = getTXaddress();
emit sendMsgToET((tMsgFrame*)packetToSend);
//check if ack expected
if((packetToSend->Packet_Control_Field.NP_ACK == 0) && (getARC()!=0))
{
//set for ACK recipt..
ACK_address = getTXaddress();
waitingForACK = true;
clearARC_CNT();
theTimer->start(getARD()*10);
TXpacket = packetToSend;
}
else
{//no ack last transmited is the one that is
if(lastTransmited != NULL)
{
delete lastTransmited;
lastTransmited = NULL;
}
lastTransmited = packetToSend;
setTX_DS_IRQ();
}
}
/*
* in PTX if CE is set HIGH and there is a packet waiting transmit packet
*
*/
void nRF24l01plus::CEsetHIGH()
{
//if nRF is in RX mode nothing shoud happen;
if(isRX_MODE() == true || isPWRUP() == false)return;
startPTX();
}
/*
* If TX mode is set while CE is high and there is a packet waiting transmit packet
*
*/
void nRF24l01plus::TXmodeSet()
{
if(getCE() == false || isPWRUP() == false) return;
startPTX();
}
void nRF24l01plus::TXpacketAdded()
{
if(getCE() == false || isRX_MODE() == true || isPWRUP() == false) return;
startPTX();
}
void nRF24l01plus::PWRUPset()
{
if(getCE() == false || isRX_MODE() == true)return;
startPTX();
}
void nRF24l01plus::ackReceved(tMsgFrame *theMSG,byte pipe)
{
if(isDynamicACKEnabled())
{//coud be ACK with payload
receve_frame(theMSG,pipe);
waitingForACK = false;
}
else
{//could be regular ack
if(theMSG->Packet_Control_Field.Payload_length == 0)
{//regular ACK receved :D
waitingForACK = false;
}
}
if(waitingForACK == false)
{
theTimer->stop();
if(lastTransmited != NULL)
{
delete lastTransmited;
lastTransmited = NULL;
}
lastTransmited = TXpacket;
setTX_DS_IRQ();
}
}
void nRF24l01plus::noACKalarm()
{
if(getARC_CNT() == getARC())
{//number of max retransmits acchived
//failed to reach targe set aproprite flags
PLOS_CNT_INC();
setMAX_RT_IRQ();
if(lastTransmited != NULL)
delete lastTransmited;
lastTransmited = TXpacket;
theTimer->stop();
waitingForACK = false;
}
else
{//retransmit message and increasse reTransCounter and start timer again
emit sendMsgToET(TXpacket);
//setup wait for ack...
ARC_CNT_INC();
theTimer->start(getARD() * 10);//10ms real life 250us
}
}
void nRF24l01plus::reciveMsgFromET(tMsgFrame *theMSG)
{
if(!coalision)
{//coalision did not happen
if(isPWRUP() && getCE())
{//in Standby mode (PWRUP = 1 && CE = 1)
byte pipe = addressToPype(theMSG->Address);
if(isRX_MODE())
{//receving
//check if address is one off th
if(pipe != 0xFF)
{//pipe is open ready to receve
//fill RX buffer
receve_frame(theMSG,pipe);
}
}
else if(waitingForACK)
{//waiting for ack
if(pipe == addressToPype(getTXaddress()))
{//addess is the P0 address, this is ACK packet
ackReceved(theMSG,pipe);
}
}
}
}
coalision = false;
}
void nRF24l01plus::setCoalision()
{
coalision = true;
}