forked from fulr/rfm69
-
Notifications
You must be signed in to change notification settings - Fork 1
/
device.go
406 lines (374 loc) · 10.1 KB
/
device.go
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Package rfm69 RFM69 Implementation in Go
package rfm69
import (
"errors"
"log"
"time"
"github.com/davecheney/gpio"
)
const (
spiPath = "/dev/spidev0.0"
)
// OnReceiveHandler is the receive callback
type OnReceiveHandler func(*Data)
// Device RFM69 Device
type Device struct {
spiDevice *spiDevice
gpio gpio.Pin
mode byte
address byte
network byte
isRFM69HW bool
powerLevel byte
tx chan *Data
quit chan bool
OnReceive OnReceiveHandler
}
// Global settings
const (
CsmaLimit = -80
MaxDataLen = 66
)
// NewDevice creates a new device
func NewDevice(nodeID, networkID byte, isRfm69HW bool) (*Device, error) {
pin, err := getPin()
if err != nil {
return nil, err
}
spi, err := newSPIDevice(spiPath)
if err != nil {
return nil, err
}
ret := &Device{
spiDevice: spi,
gpio: pin,
network: networkID,
address: nodeID,
isRFM69HW: isRfm69HW,
powerLevel: 31,
tx: make(chan *Data, 5),
quit: make(chan bool),
}
err = ret.setup()
if err != nil {
return nil, err
}
go ret.loop()
return ret, nil
}
// Close cleans up
func (r *Device) Close() error {
r.quit <- true
<-r.quit
err := r.gpio.Close()
if err != nil {
return err
}
r.spiDevice.spiClose()
return err
}
func (r *Device) writeReg(addr, data byte) error {
tx := make([]byte, 2)
tx[0] = addr | 0x80
tx[1] = data
//log.Printf("write %x: %x", addr, data)
_, err := r.spiDevice.spiXfer(tx)
if err != nil {
log.Println(err)
}
return err
}
func (r *Device) readReg(addr byte) (byte, error) {
tx := make([]uint8, 2)
tx[0] = addr & 0x7f
tx[1] = 0
rx, err := r.spiDevice.spiXfer(tx)
if err != nil {
log.Println(err)
}
return rx[1], err
}
func (r *Device) setup() error {
config := [][]byte{
/* 0x01 */ {REG_OPMODE, RF_OPMODE_SEQUENCER_ON | RF_OPMODE_LISTEN_OFF | RF_OPMODE_STANDBY},
/* 0x02 */ {REG_DATAMODUL, RF_DATAMODUL_DATAMODE_PACKET | RF_DATAMODUL_MODULATIONTYPE_FSK | RF_DATAMODUL_MODULATIONSHAPING_00}, // no shaping
/* 0x03 */ {REG_BITRATEMSB, RF_BITRATEMSB_19200}, // default: 4.8 KBPS
/* 0x04 */ {REG_BITRATELSB, RF_BITRATELSB_19200},
/* 0x05 */ {REG_FDEVMSB, RF_FDEVMSB_25000}, // default: 5KHz, (FDEV + BitRate / 2 <= 500KHz)
/* 0x06 */ {REG_FDEVLSB, RF_FDEVMSB_25000},
/* 0x07 */ {REG_FRFMSB, RF_FRFMSB_868},
/* 0x08 */ {REG_FRFMID, RF_FRFMID_868},
/* 0x09 */ {REG_FRFLSB, RF_FRFLSB_868},
// looks like PA1 and PA2 are not implemented on RFM69W, hence the max output power is 13dBm
// +17dBm and +20dBm are possible on RFM69HW
// +13dBm formula: Pout = -18 + OutputPower (with PA0 or PA1**)
// +17dBm formula: Pout = -14 + OutputPower (with PA1 and PA2)**
// +20dBm formula: Pout = -11 + OutputPower (with PA1 and PA2)** and high power PA settings (section 3.3.7 in datasheet)
///* 0x11 */ { REG_PALEVEL, RF_PALEVEL_PA0_ON | RF_PALEVEL_PA1_OFF | RF_PALEVEL_PA2_OFF | RF_PALEVEL_OUTPUTPOWER_11111},
///* 0x13 */ { REG_OCP, RF_OCP_ON | RF_OCP_TRIM_95 }, // over current protection (default is 95mA)
// RXBW defaults are { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_24 | RF_RXBW_EXP_5} (RxBw: 10.4KHz)
/* 0x19 */ {REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_24 | RF_RXBW_EXP_3},
/* 0x25 */ {REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01}, // DIO0 is the only IRQ we're using
/* 0x26 */ {REG_DIOMAPPING2, RF_DIOMAPPING2_CLKOUT_OFF}, // DIO5 ClkOut disable for power saving
/* 0x28 */ {REG_IRQFLAGS2, RF_IRQFLAGS2_FIFOOVERRUN}, // writing to this bit ensures that the FIFO & status flags are reset
/* 0x29 */ //{REG_RSSITHRESH, 220}, // must be set to dBm = (-Sensitivity / 2), default is 0xE4 = 228 so -114dBm
///* 0x2D */ { REG_PREAMBLELSB, RF_PREAMBLESIZE_LSB_VALUE } // default 3 preamble bytes 0xAAAAAA
/* 0x2E */ {REG_SYNCCONFIG, RF_SYNC_ON | RF_SYNC_FIFOFILL_AUTO | RF_SYNC_SIZE_2 | RF_SYNC_TOL_0},
/* 0x2F */ {REG_SYNCVALUE1, 0x2D}, // attempt to make this compatible with sync1 byte of RFM12B lib
/* 0x30 */ {REG_SYNCVALUE2, r.network}, // NETWORK ID
/* 0x37 */ {REG_PACKETCONFIG1, RF_PACKET1_FORMAT_VARIABLE | RF_PACKET1_DCFREE_OFF | RF_PACKET1_CRC_ON | RF_PACKET1_CRCAUTOCLEAR_ON | RF_PACKET1_ADRSFILTERING_OFF},
/* 0x38 */ {REG_PAYLOADLENGTH, 66}, // in variable length mode: the max frame size, not used in TX
///* 0x39 */ { REG_NODEADRS, nodeID }, // turned off because we're not using address filtering
/* 0x3C */ {REG_FIFOTHRESH, RF_FIFOTHRESH_TXSTART_FIFONOTEMPTY | RF_FIFOTHRESH_VALUE}, // TX on FIFO not empty
/* 0x3D */ {REG_PACKETCONFIG2, RF_PACKET2_RXRESTARTDELAY_NONE | RF_PACKET2_AUTORXRESTART_ON | RF_PACKET2_AES_OFF}, // RXRESTARTDELAY must match transmitter PA ramp-down time (bitrate dependent)
/* 0x6F */ {REG_TESTDAGC, RF_DAGC_IMPROVED_LOWBETA0}, // run DAGC continuously in RX mode for Fading Margin Improvement, recommended default for AfcLowBetaOn=0
}
for data, err := r.readReg(REG_SYNCVALUE1); err == nil && data != 0xAA; data, err = r.readReg(REG_SYNCVALUE1) {
err := r.writeReg(REG_SYNCVALUE1, 0xAA)
if err != nil {
return err
}
}
for data, err := r.readReg(REG_SYNCVALUE1); err == nil && data != 0x55; data, err = r.readReg(REG_SYNCVALUE1) {
r.writeReg(REG_SYNCVALUE1, 0x55)
if err != nil {
return err
}
}
for _, c := range config {
err := r.writeReg(c[0], c[1])
if err != nil {
return err
}
}
err := r.Encrypt([]byte{})
if err != nil {
return err
}
err = r.setHighPower(r.isRFM69HW)
if err != nil {
return err
}
err = r.SetMode(RF_OPMODE_STANDBY)
if err != nil {
return err
}
err = r.waitForMode()
return err
}
func (r *Device) waitForMode() error {
errChan := make(chan error)
go func() {
for {
reg, err := r.readReg(REG_IRQFLAGS1)
if err != nil {
errChan <- err
break
}
if reg&RF_IRQFLAGS1_MODEREADY != 0 {
errChan <- nil
break
}
}
}()
time.AfterFunc(5*time.Second, func() {
errChan <- errors.New("timeout")
})
return <-errChan
}
// Encrypt sets the encryption key and enables AES encryption
func (r *Device) Encrypt(key []byte) error {
var turnOn byte
if len(key) == 16 {
turnOn = 1
tx := make([]byte, 17)
tx[0] = REG_AESKEY1 | 0x80
copy(tx[1:], key)
if _, err := r.spiDevice.spiXfer(tx); err != nil {
return err
}
}
return r.readWriteReg(REG_PACKETCONFIG2, 0xFE, turnOn)
}
// SetMode sets operation mode
func (r *Device) SetMode(newMode byte) error {
if newMode == r.mode {
return nil
}
err := r.readWriteReg(REG_OPMODE, 0xE3, newMode)
if err != nil {
return err
}
if r.isRFM69HW && (newMode == RF_OPMODE_RECEIVER || newMode == RF_OPMODE_TRANSMITTER) {
err := r.setHighPowerRegs(newMode == RF_OPMODE_TRANSMITTER)
if err != nil {
return err
}
}
if r.mode == RF_OPMODE_SLEEP {
err = r.waitForMode()
if err != nil {
return err
}
}
r.mode = newMode
return nil
}
// SetModeAndWait sets the mode and waits for it
func (r *Device) SetModeAndWait(newMode byte) error {
err := r.SetMode(RF_OPMODE_STANDBY)
if err != nil {
return err
}
err = r.waitForMode()
if err != nil {
return err
}
return nil
}
func (r *Device) setHighPower(turnOn bool) error {
r.isRFM69HW = turnOn
ocp := byte(RF_OCP_ON)
if r.isRFM69HW {
ocp = RF_OCP_OFF
}
err := r.writeReg(REG_OCP, ocp)
if err != nil {
return err
}
if r.isRFM69HW {
err = r.readWriteReg(REG_PALEVEL, 0x1F, RF_PALEVEL_PA1_ON|RF_PALEVEL_PA2_ON)
} else {
err = r.readWriteReg(REG_PALEVEL, 0, RF_PALEVEL_PA0_ON|RF_PALEVEL_PA1_OFF|RF_PALEVEL_PA2_OFF|r.powerLevel)
}
return err
}
func (r *Device) setHighPowerRegs(turnOn bool) (err error) {
var (
testPa1 byte = 0x55
testPa2 byte = 0x70
)
if turnOn {
testPa1 = 0x5D
testPa2 = 0x7C
}
err = r.writeReg(REG_TESTPA1, testPa1)
if err != nil {
return
}
err = r.writeReg(REG_TESTPA2, testPa2)
return
}
// SetNetwork sets the network ID
func (r *Device) SetNetwork(networkID byte) error {
r.network = networkID
return r.writeReg(REG_SYNCVALUE2, networkID)
}
// SetAddress sets the node address
func (r *Device) SetAddress(address byte) error {
r.address = address
return r.writeReg(REG_NODEADRS, address)
}
// SetPowerLevel sets the TX power
func (r *Device) SetPowerLevel(powerLevel byte) error {
r.powerLevel = powerLevel
if r.powerLevel > 31 {
r.powerLevel = 31
}
return r.readWriteReg(REG_PALEVEL, 0xE0, r.powerLevel)
}
func (r *Device) canSend() (bool, error) {
// if signal stronger than -100dBm is detected assume channel activity
if r.mode == RF_OPMODE_RECEIVER {
rssi, err := r.readRSSI(false)
if err != nil {
return false, err
}
if rssi < CsmaLimit {
err = r.SetMode(RF_OPMODE_STANDBY)
return true, err
}
}
return false, nil
}
func (r *Device) readRSSI(forceTrigger bool) (rssi int, err error) {
if forceTrigger {
// RSSI trigger not needed if DAGC is in continuous mode
err = r.writeReg(REG_RSSICONFIG, RF_RSSI_START)
if err != nil {
return
}
for {
data, err := r.readReg(REG_RSSICONFIG)
if err != nil {
return 0, err
}
if data&RF_RSSI_DONE != 0 {
break
}
}
}
data, err := r.readReg(REG_RSSIVALUE)
if err != nil {
return
}
rssi = -int(data) / 2
return
}
func (r *Device) readWriteReg(reg, andMask, orMask byte) error {
regValue, err := r.readReg(reg)
if err != nil {
return err
}
regValue = (regValue & andMask) | orMask
return r.writeReg(reg, regValue)
}
func (r *Device) writeFifo(data *Data) error {
buffersize := len(data.Data)
if buffersize > MaxDataLen {
buffersize = MaxDataLen
}
tx := make([]byte, buffersize+5)
// write to FIFO
tx[0] = REG_FIFO | 0x80
tx[1] = byte(buffersize + 3)
tx[2] = data.ToAddress
tx[3] = r.address
if data.RequestAck {
tx[4] = 0x40
}
if data.SendAck {
tx[4] = 0x80
}
copy(tx[5:], data.Data[:buffersize])
_, err := r.spiDevice.spiXfer(tx)
return err
}
func (r *Device) readFifo() (Data, error) {
var err error
data := Data{}
data.Rssi, err = r.readRSSI(false)
if err != nil {
return data, err
}
tx := new([70]byte)
tx[0] = REG_FIFO & 0x7f
rx, err := r.spiDevice.spiXfer(tx[:3])
if err != nil {
return data, err
}
data.ToAddress = rx[2]
length := rx[1] - 3
if length > 66 {
length = 66
}
rx, err = r.spiDevice.spiXfer(tx[:length+3])
if err != nil {
return data, err
}
data.FromAddress = rx[1]
data.SendAck = bool(rx[2]&0x80 > 0)
data.RequestAck = bool(rx[2]&0x40 > 0)
data.Data = rx[3:]
return data, nil
}