-
Notifications
You must be signed in to change notification settings - Fork 0
/
RF.c
278 lines (240 loc) · 7.92 KB
/
RF.c
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "RF.h"
#include "defines.h"
unsigned char TO_BE_CHANGED[PACKET_LEN]; //DIT IS TIJDELIJK, MOET VERVANGEN WORDEN DOOR DE DATA DIE VERZONDEN MOET WORDEN DOOR DE SLAVES
void append(unsigned char* array_in, unsigned char address, unsigned char* array_out) {
//unsigned int len = sizeof(s);
array_out[0] = address;
unsigned int i2;
for (i2=1; i2< PACKET_LEN+1; i2++) {
array_out[i2] = array_in[i2-1];
}
}
void Send_Data(unsigned char ADDRESS, unsigned char* Data)
{
unsigned char TxBuffer[PACKET_LEN];
append(Data, ADDRESS, TxBuffer);
ReceiveOff();
receiving = 0;
Transmit( (unsigned char*)TxBuffer, sizeof TxBuffer);
transmitting = 1;
}
# if RF_VERSIE == 1
void init_RF(void){
// Increase PMMCOREV level to 2 for proper radio operation
SetVCore(2);
ResetRadioCore();
InitRadio();
ReceiveOn();
receiving = 1;
transmitting = 0;
ADDRESS = 0x01;
}
#elif RF_VERSIE == 2
void init_RF(void){
// Increase PMMCOREV level to 2 for proper radio operation
SetVCore(2);
ResetRadioCore();
InitRadio();
ReceiveOn();
receiving = 1;
transmitting = 0;
ADDRESS = 0x04;
}
#elif RF_VERSIE == 3
void init_RF(void){
// Increase PMMCOREV level to 2 for proper radio operation
SetVCore(2);
ResetRadioCore();
InitRadio();
ReceiveOn();
receiving = 1;
transmitting = 0;
ADDRESS = 0x02;
}
#elif RF_VERSIE == 4
void init_RF(void){
// Increase PMMCOREV level to 2 for proper radio operation
SetVCore(2);
ResetRadioCore();
InitRadio();
ReceiveOn();
receiving = 1;
transmitting = 0;
ADDRESS = 0x03;
}
#endif
/* weggecommentarieerd voor veiligheid. momenteel nutteloos...
unsigned char* Receive_data(unsigned char* RxBuffer, unsigned char ADDRESS, unsigned char* Received_data){
// Contact Cedric for errors
// Check for ADDRESS:
// If ADDRESS = Correct:
// Return contents of RxBuffer
// If ADDRESS = False:
// Break
volatile unsigned i;
// Read the length byte from the FIFO
RxBufferLength = ReadSingleReg( RXBYTES );
ReadBurstReg(RF_RXFIFORD, RxBuffer, RxBufferLength);
// Stop here to see contents of RxBuffer
__no_operation();
// Check the CRC results
if(RxBuffer[CRC_LQI_IDX] && CRC_OK){
// Check to see if the ADDRESS is correct)
if(RxBuffer[0] && ADDRESS){
for(i = PACKET_LEN+1; i > 0; i--){
Received_data[i] = RxBuffer[i];
}
}
}
return 0;
}
*/
void InitRadio(void)
{
// Set the High-Power Mode Request Enable bit so LPM3 can be entered
// with active radio enabled
PMMCTL0_H = 0xA5;
PMMCTL0_L |= PMMHPMRE_L;
PMMCTL0_H = 0x00;
WriteRfSettings(&rfSettings);
WriteSinglePATable(PATABLE_VAL);
}
void Transmit(unsigned char *buffer, unsigned char length)
{
RF1AIES |= BIT9;
RF1AIFG &= ~BIT9; // Clear pending interrupts
RF1AIE |= BIT9; // Enable TX end-of-packet interrupt
WriteBurstReg(RF_TXFIFOWR, buffer, length);
Strobe( RF_STX ); // Strobe STX
}
void ReceiveOn(void)
{
RF1AIES |= BIT9; // Falling edge of RFIFG9
RF1AIFG &= ~BIT9; // Clear a pending interrupt
RF1AIE |= BIT9; // Enable the interrupt
// Radio is in IDLE following a TX, so strobe SRX to enter Receive Mode
Strobe( RF_SRX );
}
void ReceiveOff(void)
{
RF1AIE &= ~BIT9; // Disable RX interrupts
RF1AIFG &= ~BIT9; // Clear pending IFG
// It is possible that ReceiveOff is called while radio is receiving a packet.
// Therefore, it is necessary to flush the RX FIFO after issuing IDLE strobe
// such that the RXFIFO is empty prior to receiving a packet.
Strobe( RF_SIDLE );
Strobe( RF_SFRX );
}
#if RF_VERSIE == 1 || RF_VERSIE == 3 || RF_VERSIE == 4
#pragma vector=CC1101_VECTOR
__interrupt void CC1101_ISR(void)
{
switch(__even_in_range(RF1AIV,32)) // Prioritizing Radio Core Interrupt
{
case 0: break; // No RF core interrupt pending
case 2: break; // RFIFG0
case 4: break; // RFIFG1
case 6: break; // RFIFG2
case 8: break; // RFIFG3
case 10: break; // RFIFG4
case 12: break; // RFIFG5
case 14: break; // RFIFG6
case 16: break; // RFIFG7
case 18: break; // RFIFG8
case 20: // RFIFG9
if(receiving) // RX end of packet
{
// Check for ADDRESS:
// If ADDRESS = Correct:
// Set transmitting = 1
// If ADDRESS = False:
// Break
// Read the length byte from the FIFO
RxBufferLength = ReadSingleReg( RXBYTES );
ReadBurstReg(RF_RXFIFORD, RxBuffer, RxBufferLength);
// Stop here to see contents of RxBuffer
__no_operation();
// Check the CRC results
if(RxBuffer[CRC_LQI_IDX] & CRC_OK){
// Check to see if the ADDRESS is correct)
if(RxBuffer[0] && ADDRESS){
Send_Data(ADDRESS_MASTER,TO_BE_CHANGED);
}
else {
break;
}
}
}
else if(transmitting) // TX end of packet
{
RF1AIE &= ~BIT9; // Disable TX end-of-packet interrupt
transmitting = 0;
}
else while(1); // trap
break;
case 22: break; // RFIFG10
case 24: break; // RFIFG11
case 26: break; // RFIFG12
case 28: break; // RFIFG13
case 30: break; // RFIFG14
case 32: break; // RFIFG15
}
__bic_SR_register_on_exit(LPM3_bits);
}
#elif RF_VERSIE==2
#pragma vector=CC1101_VECTOR
__interrupt void CC1101_ISR(void)
{
switch(__even_in_range(RF1AIV,32)) // Prioritizing Radio Core Interrupt
{
case 0: break; // No RF core interrupt pending
case 2: break; // RFIFG0
case 4: break; // RFIFG1
case 6: break; // RFIFG2
case 8: break; // RFIFG3
case 10: break; // RFIFG4
case 12: break; // RFIFG5
case 14: break; // RFIFG6
case 16: break; // RFIFG7
case 18: break; // RFIFG8
case 20: // RFIFG9
if(receiving) // RX end of packet
{
// Check for ADDRESS:
// If ADDRESS = Correct:
// Return contents of RxBuffer
// If ADDRESS = False:
// Break
volatile unsigned i;
// Read the length byte from the FIFO
RxBufferLength = ReadSingleReg( RXBYTES );
ReadBurstReg(RF_RXFIFORD, RxBuffer, RxBufferLength);
// Stop here to see contents of RxBuffer
__no_operation();
// Check the CRC results
if(RxBuffer[CRC_LQI_IDX] & CRC_OK){
// Check to see if the ADDRESS is correct)
if(RxBuffer[0] && ADDRESS){
for(i = PACKET_LEN+1; i > 0; i--){
Received_data[i] = RxBuffer[i];
}
}
}
}
else if(transmitting) // TX end of packet
{
RF1AIE &= ~BIT9; // Disable TX end-of-packet interrupt
transmitting = 0;
}
else while(1); // trap
break;
case 22: break; // RFIFG10
case 24: break; // RFIFG11
case 26: break; // RFIFG12
case 28: break; // RFIFG13
case 30: break; // RFIFG14
case 32: break; // RFIFG15
}
__bic_SR_register_on_exit(LPM3_bits);
}
#endif