/
wireless.c
366 lines (306 loc) · 9.8 KB
/
wireless.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "wireless.h"
/*
Local functions
*/
static void send_packet(uint8_t *pnt);
static void read_packet(packet_t *return_pktpnt);
/*
Initialize Wireless Chip
*/
uint8_t init_wireless(void) {
// Setup up CC2500 SPI
CC2500_InitTypeDef CC2500_InitStruct;
CC2500_Init(&CC2500_InitStruct);
// Send reset command
CC2500_CommandProbe(CC2500_READBIT, CC2500_SRES);
// Wait for CHIP_RDYn
while (CC2500_CommandProbe(CC2500_READBIT, CC2500_SNOP) & 0x80);
// Configure the CC2500 registers
CC2500_SmartRF_Config();
osDelay(1000);
return CC2500_CommandProbe(CC2500_READBIT, CC2500_SNOP);
}
/*
Transmit the pitch and roll over wireless
Input:
float pitch, float roll
uint8_t ctrl2 = indicate what kind of packet we are sending
*/
void transmit_pitchroll(float pitch, float roll, uint8_t ctrl2) {
packet_t pkt;
pkt.f1 = pitch;
pkt.f2 = roll;
pkt.b1 = PACKET_CTRL1_PR;
pkt.b2 = ctrl2;
//printf(" pitch = %f ", pkt.f1);
uint8_t *pnt = (uint8_t *) &pkt;
//printf(" pitch = %f ", pkt.f1);
//printf("Data: %x %x %x %x %x %x %x %x %x %x \n",pnt[0],pnt[1],pnt[2],pnt[3],pnt[4],pnt[5],pnt[6],pnt[7],pnt[8],pnt[9]);
// Put data into the TX FIFO buffer
CC2500_Write(pnt, CC2500_FIFOADDR, WIRELESS_PKT_DATALEN); // Write to the TX buffer
//while ((CC2500_CommandProbe(CC2500_READBIT, CC2500_SNOP) & 0x70) !=);
//osDelay(1000);
//print_status();
// Move into transmit state
uint8_t status = CC2500_CommandProbe(CC2500_WRITEBIT, CC2500_STX);
//printf("Moved to TX (%x) \n",status);
// The CC2500 will move back into the idle state when the transmission has been sent
//wait_for_idle();
//osDelay(20);
}
/*
Receives the pitch and roll from the transmitter and parses the packet for the angles
Input:
float* pitch, float* roll = pointers to the angles
uint8_t* ctrl2 = pointer to the control value so that we can update it.
*/
uint16_t receive_pitchroll(float* pitch, float* roll, uint8_t* ctrl2) {
// assume already in RX
// Wait for a transmisson to be read. When this happens, the CC2500 will move to the idle state
wait_for_idle();
// Determine number of bytes to read
uint8_t bytesToRead;
CC2500_Read(&bytesToRead, CC2500_RXBYTES,1);
bytesToRead = bytesToRead & 0x7F;
uint8_t buffer[bytesToRead];
CC2500_Read(&buffer[0], CC2500_FIFOADDR, bytesToRead);
bytesToRead -= 2;
//printf("Data: %x %x %x %x %x %x %x %x %x %x \n ",buffer[0],buffer[1],buffer[2],buffer[3],
// buffer[4],buffer[5],buffer[6],buffer[7],buffer[8],buffer[9]);
packet_t *pkt_pnt = (packet_t *) &buffer[0];
*pitch = pkt_pnt->f1;
*roll = pkt_pnt->f2;
uint8_t ctrl1 = pkt_pnt->b1;
*ctrl2 = pkt_pnt->b2;
// Exract data
//*pitch = buffer[0];
//*roll = (float) buffer[4];
uint16_t ctrl = buffer[8];
//printf(" Pitch: %f Roll: %f \n",pkt_pnt->f1,pkt_pnt->f2);
// Move back to recieve state
CC2500_CommandProbe(CC2500_READBIT, CC2500_SRX);
return ctrl;
}
/*
Formats a packet for transmitting pitch from a keypad
*/
void transmit_keypad_pitch(float pitch) {
packet_t pkt;
pkt.f1 = pitch;
pkt.f2 = 0;
pkt.b1 = PACKET_CTRL1_PITCH;
pkt.b2 = 2;
uint8_t *pnt = (uint8_t *) &pkt;
send_packet(pnt);
}
/*
Formats a packet for transmitting roll from a keypad
*/
void transmit_keypad_roll(float roll) {
packet_t pkt;
pkt.f1 = roll;
pkt.f2 = 0;
pkt.b1 = PACKET_CTRL1_ROLL;
pkt.b2 = 2;
uint8_t *pnt = (uint8_t *) &pkt;
send_packet(pnt);
}
/*
Formats a packet for transmitting time from a keypad
*/
void transmit_keypad_time(float time) {
packet_t pkt;
pkt.f1 = time;
pkt.f2 = 0;
pkt.b1 = PACKET_CTRL1_TIME;
pkt.b2 = 2;
uint8_t *pnt = (uint8_t *) &pkt;
send_packet(pnt);
}
/*
Formats a packet to send indicating a start of a keypad sequence
*/
void transmit_keypad_begin() {
packet_t pkt;
pkt.f1 = 0;
pkt.f2 = 0;
pkt.b1 = PACKET_CTRL1_BEGIN;
pkt.b2 = 2;
uint8_t *pnt = (uint8_t *) &pkt;
send_packet(pnt);
}
/*
Formats a packet to send indicating a end of a keypad sequence
*/
void transmit_keypad_end() {
packet_t pkt;
pkt.f1 = 0;
pkt.f2 = 0;
pkt.b1 = PACKET_CTRL1_END;
pkt.b2 = 2;
uint8_t *pnt = (uint8_t *) &pkt;
send_packet(pnt);
}
/*
Sends a recorded sequence of angles to the receiver.
Input:
int size = dummy variable
float *pitchBuffer, float *rollBuffer = pointers to the buffers containing the data
float time_interval = time interval the sequence should complete in.
*/
void transmit_record_sequence(int size, float *pitchBuffer, float *rollBuffer, float time_interval) {
packet_t pkt;
pkt.f1 = time_interval;
pkt.f2 = 0;
pkt.b1 = PACKET_CTRL1_RECORD_BEGIN;
pkt.b2 = 0;
uint8_t *pnt = (uint8_t *) &pkt;
// Send start packet
send_packet(pnt);
osDelay(100);
pkt.b1 = PACKET_CTRL1_RECORD_PKT;
uint8_t index = 0;
while (index != 255) {
pkt.f1 = pitchBuffer[index];
pkt.f2 = rollBuffer[index];
pkt.b2 = index;
printf("Sending recorded data pkt Index = %i Pitch = %f Roll = %f \n", index, pkt.f1, pkt.f2);
send_packet(pnt);
osDelay(30); // Could be 20
index++;
}
osDelay(100);
pkt.f1 = 0;
pkt.f2 = 0;
pkt.b1 = PACKET_CTRL1_RECORD_END;
pkt.b2 = 0;
// Send end packet
send_packet(pnt);
// Wait for sequence to complete
osDelay(5000);
}
/*
Receives a sequence of angles from the transmitter
Input:
float *pitchBuffer, float *rollBuffer = pointers to the buffers that will store the sequence
float *time_interval = pointer to the variable which indicates how long the time interval the sequnce
should execute in.
*/
void receive_record_sequence(float *pitchBuffer, float *rollBuffer, float *time_interval) {
uint8_t ctrl1 = PACKET_CTRL1_RECORD_PKT;
uint8_t index;
packet_t pkt;
while (ctrl1 != PACKET_CTRL1_RECORD_END) {
wait_for_idle();
read_packet(&pkt);
ctrl1 = pkt.b1;
if (ctrl1 == PACKET_CTRL1_RECORD_PKT) {
pitchBuffer[pkt.b2] = pkt.f1;
rollBuffer[pkt.b2] = pkt.f2;
printf("Recieved recorded data pkt %i \n", pkt.b2);
}
}
}
/*
Receive Keypad variables.
uint8_t *ctrl = pointer to control value
float *value = pointer to the value you are expecting in the keypad sequence, i.e. pitch,roll,time
*/
void receive_keypad(uint8_t *ctrl, float *value) {
wait_for_idle();
// Determine number of bytes to read
uint8_t bytesToRead;
CC2500_Read(&bytesToRead, CC2500_RXBYTES,1);
bytesToRead = bytesToRead & 0x7F;
uint8_t buffer[bytesToRead];
CC2500_Read(&buffer[0], CC2500_FIFOADDR, bytesToRead);
bytesToRead -= 2;
packet_t *pkt_pnt = (packet_t *) &buffer[0];
*value = pkt_pnt->f1;
*ctrl = pkt_pnt->b1;
//uint8_t ctrl2 = pkt_pnt->b2;
// Exract data
//*pitch = buffer[0];
//*roll = (float) buffer[4];
//uint16_t ctrl = buffer[8];
//printf(" Pitch: %f Roll: %f \n",pkt_pnt->f1,pkt_pnt->f2);
// Move back to recieve state
CC2500_CommandProbe(CC2500_READBIT, CC2500_SRX);
}
/*
Sends a single packet over wireless
Input:
uint8_t *pnt = pointer to the packet being sent
*/
void send_packet(uint8_t *pnt) {
// Put data into the TX FIFO buffer
CC2500_Write(pnt, CC2500_FIFOADDR, WIRELESS_PKT_DATALEN); // Write to the TX buffer
// Move into transmit state
uint8_t status = CC2500_CommandProbe(CC2500_WRITEBIT, CC2500_STX);
// The CC2500 will move back into the idle state when the transmission has been sent
osDelay(20);
}
/*
Reads packet from wireless chip.
Input:
packet_t *return_pktpnt = pointer to where received packet will be stored
*/
void read_packet(packet_t *return_pktpnt) {
// Determine number of bytes to read
uint8_t bytesToRead;
CC2500_Read(&bytesToRead, CC2500_RXBYTES,1);
bytesToRead = bytesToRead & 0x7F;
uint8_t buffer[bytesToRead];
CC2500_Read(&buffer[0], CC2500_FIFOADDR, bytesToRead);
bytesToRead -= 2;
packet_t *pkt_pnt = (packet_t *) &buffer[0];
return_pktpnt->f1 = pkt_pnt->f1;
return_pktpnt->f2 = pkt_pnt->f2;
return_pktpnt->b1 = pkt_pnt->b1;
return_pktpnt->b2 = pkt_pnt->b2;
// Move back to recieve state
CC2500_CommandProbe(CC2500_READBIT, CC2500_SRX);
}
/*
Returns the signal strength of the wireless chip.
Reads the RSSI resgister and converts the values to dB.
Return variable is a signed Integer.
*/
int get_signal_strength(void) {
uint8_t byte;
CC2500_Read(&byte, CC2500_RSSI,1);
//printf("RSSI = %i \n",byte);
int rssi = (int) byte;
/*
1) Read the RSSI status register
2) Convert the reading from a hexadecimal number to a decimal number (RSSI_dec)
3) If RSSI_dec >= 128 then RSSI_dBm = (RSSI_dec - 256)/2 RSSI_offset
4) Else if RSSI_dec < 128 then RSSI_dBm = (RSSI_dec)/2 RSSI_offset
*/
int offset = 70;
if (rssi >= 128)
rssi = (rssi - 256)/2 - offset;
else
rssi = (rssi)/2 - offset;
return rssi;
}
/*
waits for the CC2500 to enter the Idle State.
Returns once it is in Idle State.
Every 8ms checks to see if Idle, slightly faster than 100Hz.
*/
void wait_for_idle(void) {
uint8_t status = CC2500_CommandProbe(CC2500_READBIT, CC2500_SNOP);
while ((status & 0x70) != CC2500_STATE_IDLE) {
//printf("Waiting for idle state. (%x) \n",status);
osDelay(8);
status = CC2500_CommandProbe(CC2500_READBIT, CC2500_SNOP);
}
}
/*
Return the status(state) of the CC2500 chip
*/
void print_status(void) {
uint8_t status = CC2500_CommandProbe(CC2500_READBIT, CC2500_SNOP);
printf("Status (%x) \n",status);
}