-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCrazyradio.cpp
224 lines (196 loc) · 5.04 KB
/
Crazyradio.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
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
#include "Crazyradio.h"
#include <sstream>
#include <stdexcept>
#include <libusb.h>
namespace bitcraze {
namespace crazyflieLinkCpp {
enum
{
SET_RADIO_CHANNEL = 0x01,
SET_RADIO_ADDRESS = 0x02,
SET_DATA_RATE = 0x03,
SET_RADIO_POWER = 0x04,
SET_RADIO_ARD = 0x05,
SET_RADIO_ARC = 0x06,
ACK_ENABLE = 0x10,
SET_CONT_CARRIER = 0x20,
// SCANN_CHANNELS = 0x21,
LAUNCH_BOOTLOADER = 0xFF,
};
Crazyradio::Crazyradio(
libusb_device* dev)
: USBDevice(dev)
, channel_(0)
, address_(0)
, datarate_(Datarate_250KPS)
, ackEnabled_(true)
{
setDatarate(Datarate_2MPS);
setChannel(2);
setContCarrier(false);
setAddress(0xE7E7E7E7E7);
setPower(Power_0DBM);
setArc(3);
setArdBytes(32);
setAckEnabled(true);
}
Crazyradio::~Crazyradio()
{
}
void Crazyradio::setChannel(uint8_t channel)
{
sendVendorSetup(SET_RADIO_CHANNEL, channel, 0, NULL, 0);
channel_ = channel;
}
void Crazyradio::setAddress(uint64_t address)
{
unsigned char a[5];
a[4] = (address >> 0) & 0xFF;
a[3] = (address >> 8) & 0xFF;
a[2] = (address >> 16) & 0xFF;
a[1] = (address >> 24) & 0xFF;
a[0] = (address >> 32) & 0xFF;
sendVendorSetup(SET_RADIO_ADDRESS, 0, 0, a, 5);
address_ = address;
}
void Crazyradio::setDatarate(Datarate datarate)
{
sendVendorSetup(SET_DATA_RATE, datarate, 0, NULL, 0);
datarate_ = datarate;
}
void Crazyradio::setPower(Power power)
{
sendVendorSetup(SET_RADIO_POWER, power, 0, NULL, 0);
}
void Crazyradio::setArc(uint8_t arc)
{
sendVendorSetup(SET_RADIO_ARC, arc, 0, NULL, 0);
}
void Crazyradio::setArdTime(uint8_t us)
{
// Auto Retransmit Delay:
// 0000 - Wait 250uS
// 0001 - Wait 500uS
// 0010 - Wait 750uS
// ........
// 1111 - Wait 4000uS
// Round down, to value representing a multiple of 250uS
int t = (us / 250) - 1;
if (t < 0) {
t = 0;
}
if (t > 0xF) {
t = 0xF;
}
sendVendorSetup(SET_RADIO_ARD, t, 0, NULL, 0);
}
void Crazyradio::setArdBytes(uint8_t nbytes)
{
sendVendorSetup(SET_RADIO_ARD, 0x80 | nbytes, 0, NULL, 0);
}
void Crazyradio::setAckEnabled(bool enable)
{
sendVendorSetup(ACK_ENABLE, enable, 0, NULL, 0);
ackEnabled_ = enable;
}
void Crazyradio::setContCarrier(bool active)
{
sendVendorSetup(SET_CONT_CARRIER, active, 0, NULL, 0);
}
Crazyradio::Ack Crazyradio::sendPacket(
const uint8_t* data,
uint32_t length)
{
Crazyradio::Ack ack;
int status;
int transferred;
// Send data
status = libusb_bulk_transfer(
dev_handle_,
/* endpoint*/ (0x01 | LIBUSB_ENDPOINT_OUT),
(uint8_t*)data,
length,
&transferred,
/*timeout*/ 100);
// if (status == LIBUSB_ERROR_TIMEOUT) {
// return;
// }
if (status != LIBUSB_SUCCESS) {
throw std::runtime_error(libusb_error_name(status));
}
if (length != (uint32_t)transferred) {
std::stringstream sstr;
sstr << "Did transfer " << transferred << " but " << length << " was requested!";
throw std::runtime_error(sstr.str());
}
// Read result
status = libusb_bulk_transfer(
dev_handle_,
/* endpoint*/ (0x81 | LIBUSB_ENDPOINT_IN),
ack.data_.data(),
ack.data_.size(),
&transferred,
/*timeout*/ 10);
if (status == LIBUSB_ERROR_TIMEOUT) {
return ack;
}
if (status != LIBUSB_SUCCESS) {
throw std::runtime_error(libusb_error_name(status));
}
ack.size_ = transferred - 1;
return ack;
}
void Crazyradio::sendPacketNoAck(
const uint8_t* data,
uint32_t length)
{
int status;
int transferred;
// Send data
status = libusb_bulk_transfer(
dev_handle_,
/* endpoint*/ (0x01 | LIBUSB_ENDPOINT_OUT),
(uint8_t*)data,
length,
&transferred,
/*timeout*/ 100);
// if (status == LIBUSB_ERROR_TIMEOUT) {
// return;
// }
if (status != LIBUSB_SUCCESS) {
throw std::runtime_error(libusb_error_name(status));
}
if (length != (uint32_t)transferred) {
std::stringstream sstr;
sstr << "Did transfer " << transferred << " but " << length << " was requested!";
throw std::runtime_error(sstr.str());
}
}
void Crazyradio::send2PacketsNoAck(
const uint8_t* data,
uint32_t totalLength)
{
int status;
int transferred;
// Send data
status = libusb_bulk_transfer(
dev_handle_,
/* endpoint*/ (0x01 | LIBUSB_ENDPOINT_OUT),
(uint8_t*)data,
totalLength,
&transferred,
/*timeout*/ 100);
// if (status == LIBUSB_ERROR_TIMEOUT) {
// return;
// }
if (status != LIBUSB_SUCCESS) {
throw std::runtime_error(libusb_error_name(status));
}
if (totalLength != (uint32_t)transferred) {
std::stringstream sstr;
sstr << "Did transfer " << transferred << " but " << totalLength << " was requested!";
throw std::runtime_error(sstr.str());
}
}
} // namespace crazyflieLinkCpp
} // namespace bitcraze