-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnrf24l01.js
386 lines (359 loc) · 10.1 KB
/
nrf24l01.js
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var Logger = require('./util/logger');
var Nrf_register = require('./util/nrf-register');
var Nrf_executor = require('./util/nrf-command');
var Nrf_io = require('./util/nrf-io');
var Nrf_scheduler = require('./util/nrf-task-scheduler');
function Nrf()
{
this.logger = new Logger('NRF');
EventEmitter.call(this);
this.transmitting = false;
}
util.inherits(Nrf, EventEmitter);
Nrf.prototype.init = function(callback, spidev, gpio_ce, gpio_csn, gpio_irq)
{
if(typeof spidev == 'undefined')
spidev = '/dev/spidev0.1';
if(typeof gpio_ce == 'undefined')
gpio_ce = 'GPIO25';
if(typeof gpio_csn == 'undefined')
gpio_csn = 'GPIO22';
if(typeof gpio_irq == 'undefined')
gpio_irq = 'GPIO27';
this.nrf_io = new Nrf_io(gpio_ce, gpio_csn, gpio_irq);
this.nrf_io.get_logger().set_devel(this.logger.get_devel());
this.executor = new Nrf_executor(this.nrf_io);
this.executor.get_logger().set_devel(this.logger.get_devel());
this.nrf_regset = new Nrf_register();
this.nrf_io.init(spidev, callback, function(nrf) {
return function() {
nrf.irq_callback.call(nrf);
}
}(this));
}
Nrf.prototype.get_logger = function()
{
return this.logger;
}
Nrf.prototype.is_transmitting = function()
{
return this.transmitting;
}
Nrf.prototype.get_details = function(obj)
{
return util.inspect(obj);
}
Nrf.prototype.print_details = function(callback)
{
this.get_all_registers(function(regset) {
this.logger.log(this.get_details(regset));
if(typeof callback == 'function')
callback(regset);
});
}
Nrf.prototype.get_all_registers = function(callback, regset)
{
if(typeof regset == 'undefined')
regset = this.nrf_regset;
var scheduler = new Nrf_scheduler(this);
scheduler.get_logger().set_devel(this.logger.get_devel());
for(var key in regset)
if(regset.hasOwnProperty(key))
{
var register = regset[key];
scheduler.add_task(function(register, nrf) {
return function(callback) {
var buff = new Buffer(register.length);
buff.fill(0);
this.executor.exec.call(this.executor, Nrf_executor.r_register, register.addr, buff, function(err, data) {
if(err)
nrf.logger.log('Failed to update data');
else
{
for(var key_ in register.fields)
if(register.fields.hasOwnProperty(key_))
{
var bitfield = register.fields[key_];
bitfield.set_from_register(data);
}
}
callback();
});
};
}(register, this));
}
scheduler.run(function(regset, nrf) {
return function() {
callback.call(nrf, regset);
}
}(regset, this));
}
Nrf.prototype.set_all_registers = function(callback, regset)
{
if(typeof regset == 'undefined')
regset = this.nrf_regset;
var scheduler = new Nrf_scheduler(this);
scheduler.get_logger().set_devel(this.logger.get_devel());
for(var key in regset)
if(regset.hasOwnProperty(key))
{
var register = regset[key];
scheduler.add_task(function(register, nrf) {
return function(callback) {
this.executor.exec.call(this.executor, Nrf_executor.w_register, register.addr, register.get_value(), function(err, data) {
if(err)
nrf.logger.log('Failed to update module registers');
callback();
});
}
}(register, this));
}
scheduler.run(function(regset, nrf) {
return function() {
callback.call(nrf, regset);
}
}(regset, this));
}
Nrf.prototype.set_register = function(register, callback)
{
this.executor.exec.call(this.executor, Nrf_executor.w_register, register.addr, register.get_value(), function(nrf) {
return function(err, data) {
if(err)
nrf.logger.log('Failed to update register');
callback(err, data);
}
}(this));
}
Nrf.prototype.get_register = function(register, callback)
{
var buff = new Buffer(register.length);
buff.fill(0);
this.executor.exec.call(this.executor, Nrf_executor.r_register, register.addr, buff, function(nrf) {
return function(err, data) {
if(err)
nrf.logger.log('Failed to update register');
else
register.set_value(data);
callback(err, data);
}
}(this));
}
Nrf.prototype.irq_callback = function()
{
this.logger.log('Received IRQ', Logger.level.debug);
var buff = new Buffer(1);
buff.fill(0);
this.get_register(this.nrf_regset.status, function(nrf) {
return function(err, data) {
if(err)
nrf.logger.log('Failed to get status');
else
{
if(!nrf.emit('irq', nrf.nrf_regset.status))
{
nrf.clear_irq_flags();
}
nrf.transmitting = false;
}
}
}(this));
}
Nrf.prototype.clear_irq_flags = function(callback)
{
var regmap = new Nrf_register();
regmap.status.rx_dr.set_value(1);
regmap.status.tx_ds.set_value(1);
regmap.status.max_rt.set_value(1);
this.set_register(regmap.status, function(nrf) {
return function() {
nrf.get_register.call(nrf, nrf.nrf_regset.status, function(err, data) {
if(callback)
callback(err, data);
});
}
}(this));
}
Nrf.prototype.init_module = function(callback)
{
this.nrf_regset = new Nrf_register();
this.nrf_regset.config.crco.set_value(1);
this.nrf_regset.config.en_crc.set_value(1);
this.nrf_regset.rf_setup.rf_dr_high.set_value(0);
this.nrf_regset.rx_pw_p0.rx_pw_p0.set_value(32);
this.nrf_regset.rx_pw_p1.rx_pw_p1.set_value(32);
this.nrf_regset.setup_retr.ard.set_value(15);
var scheduler = new Nrf_scheduler(this);
scheduler.get_logger().set_devel(this.logger.get_devel());
scheduler.add_task(function(callback) {
this.set_all_registers(callback);
});
scheduler.add_task(function(callback) {
this.flush_rx(callback);
});
scheduler.add_task(function(callback) {
this.flush_tx(callback);
});
scheduler.add_task(function(callback) {
this.clear_irq_flags(callback);
});
scheduler.run(function() {
callback.call(this);
});
}
Nrf.prototype.init_rx = function(callback)
{
this.nrf_regset.config.prim_rx.set_value(1);
this.nrf_regset.config.pwr_up.set_value(1);
var scheduler = new Nrf_scheduler(this);
scheduler.get_logger().set_devel(this.logger.get_devel());
scheduler.add_task(function(callback) {
this.set_register(this.nrf_regset.config, callback);
});
scheduler.add_task(function(callback) {
this.nrf_io.ce_hi(callback);
});
scheduler.run(callback);
}
Nrf.prototype.init_tx = function(callback)
{
this.nrf_regset.config.prim_rx.set_value(0);
this.nrf_regset.config.pwr_up.set_value(1);
var scheduler = new Nrf_scheduler(this);
scheduler.get_logger().set_devel(this.logger.get_devel());
scheduler.add_task(function(callback) {
this.set_register(this.nrf_regset.config, callback);
});
scheduler.add_task(function(callback) {
this.nrf_io.ce_hi(callback);
});
scheduler.run(callback);
}
Nrf.prototype.flush_rx = function(callback)
{
this.executor.exec.call(this.executor, Nrf_executor.flush_rx, 0, undefined, function(err, data) {
callback(err, data);
});
}
Nrf.prototype.flush_tx = function(callback)
{
this.executor.exec.call(this.executor, Nrf_executor.flush_tx, 0, undefined, function(err, data) {
callback(err, data);
});
}
Nrf.prototype.set_channel = function(channel, callback)
{
this.nrf_regset.rf_ch.rf_ch.set_value(channel);
this.set_register(this.nrf_regset.rf_ch, callback);
}
Nrf.prototype.set_tx_address = function(address, callback)
{
this.nrf_regset.tx_addr.tx_addr.set_value(address);
this.set_register(this.nrf_regset.tx_addr, callback);
}
Nrf.prototype.set_rx_address = function(address, callback, pipe)
{
if(typeof pipe == 'undefined')
pipe = 0;
if(pipe == 0)
{
this.nrf_regset.rx_addr_p0.rx_addr_p0.set_value(address);
this.set_register(this.nrf_regset.rx_addr_p0, callback);
}
else if(pipe == 1)
{
this.nrf_regset.rx_addr_p1.rx_addr_p1.set_value(address);
this.set_register(this.nrf_regset.rx_addr_p1, callback);
}
else if(pipe == 2)
{
this.nrf_regset.rx_addr_p2.rx_addr_p2.set_value(address);
this.set_register(this.nrf_regset.rx_addr_p2, callback);
}
else if(pipe == 3)
{
this.nrf_regset.rx_addr_p3.rx_addr_p3.set_value(address);
this.set_register(this.nrf_regset.rx_addr_p3, callback);
}
else if(pipe == 4)
{
this.nrf_regset.rx_addr_p4.rx_addr_p4.set_value(address);
this.set_register(this.nrf_regset.rx_addr_p4, callback);
}
else if(pipe == 5)
{
this.nrf_regset.rx_addr_p5.rx_addr_p5.set_value(address);
this.set_register(this.nrf_regset.rx_addr_p5, callback);
}
}
Nrf.prototype.set_payload_width = function(callback, pipe, width)
{
if(typeof width == 'undefined')
width = 32;
if(typeof pipe == 'undefined')
pipe = 0;
if(pipe == 0)
{
this.nrf_regset.rx_pw_p0.rx_pw_p0.set_value(width);
this.set_register(this.nrf_regset.rx_pw_p0, callback);
}
else if(pipe == 1)
{
this.nrf_regset.rx_pw_p1.rx_pw_p1.set_value(width);
this.set_register(this.nrf_regset.rx_pw_p1, callback);
}
else if(pipe == 2)
{
this.nrf_regset.rx_pw_p2.rx_pw_p2.set_value(width);
this.set_register(this.nrf_regset.rx_pw_p2, callback);
}
else if(pipe == 3)
{
this.nrf_regset.rx_pw_p3.rx_pw_p3.set_value(width);
this.set_register(this.nrf_regset.rx_pw_p3, callback);
}
else if(pipe == 4)
{
this.nrf_regset.rx_pw_p4.rx_pw_p4.set_value(width);
this.set_register(this.nrf_regset.rx_pw_p4, callback);
}
else if(pipe == 5)
{
this.nrf_regset.rx_pw_p5.rx_pw_p5.set_value(width);
this.set_register(this.nrf_regset.rx_pw_p5, callback);
}
}
Nrf.prototype.send_data = function(data, callback)
{
this.transmitting = true;
if(typeof callback != 'function')
callback = function() { };
var scheduler = new Nrf_scheduler(this);
scheduler.get_logger().set_devel(this.logger.get_devel());
scheduler.add_task(function(callback) {
this.nrf_io.ce_lo(callback);
});
scheduler.add_task(function(callback) {
this.init_tx(callback);
});
scheduler.add_task(function(nrf) {
return function(callback) {
this.executor.exec.call(this.executor, Nrf_executor.w_tx_payload, 0, data, function(err, data) {
if(err)
nrf.logger.log('Failed to write tx payload');
else
nrf.logger.log('Wrote tx payload ' + data.length + ' bytes', Logger.level.debug);
callback();
});
};
}(this));
scheduler.add_task(function(callback) {
this.nrf_io.ce_hi(callback);
});
scheduler.add_task(function(callback) {
this.nrf_io.ce_lo(callback);
});
scheduler.run(callback);
}
module.exports = Nrf;