-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrfspy.go
117 lines (100 loc) · 2.97 KB
/
rfspy.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
// rfspy.go contains the CC-layer code for making RF okay again
package gorileylink
import (
log "github.com/sirupsen/logrus"
)
type RxFilter byte
type SwEncoding byte
type CxRegister byte
const (
RxFilterWide RxFilter = 0x50 // 300KHz
RxFilterNarrow RxFilter = 0x90 // 150KHz
EncodingNone SwEncoding = 0x00
EncodingManchester SwEncoding = 0x01
Encoding4b6b SwEncoding = 0x02
RegisterSync1 CxRegister = 0x00
RegisterSync0 CxRegister = 0x01
RegisterPktlen CxRegister = 0x02
RegisterPktctrl1 CxRegister = 0x03
RegisterPktctrl0 CxRegister = 0x04
RegisterFsctrl1 CxRegister = 0x07
RegisterFreq2 CxRegister = 0x09
RegisterFreq1 CxRegister = 0x0a
RegisterFreq0 CxRegister = 0x0b
RegisterMdmcfg4 CxRegister = 0x0c
RegisterMdmcfg3 CxRegister = 0x0d
RegisterMdmcfg2 CxRegister = 0x0e
RegisterMdmcfg1 CxRegister = 0x0f
RegisterMdmcfg0 CxRegister = 0x10
RegisterDeviatn CxRegister = 0x11
RegisterMcsm0 CxRegister = 0x14
RegisterFoccfg CxRegister = 0x15
RegisterAgcctrl2 CxRegister = 0x17
RegisterAgcctrl1 CxRegister = 0x18
RegisterAgcctrl0 CxRegister = 0x19
RegisterFrend1 CxRegister = 0x1a
RegisterFrend0 CxRegister = 0x1b
RegisterFscal3 CxRegister = 0x1c
RegisterFscal2 CxRegister = 0x1d
RegisterFscal1 CxRegister = 0x1e
RegisterFscal0 CxRegister = 0x1f
RegisterTest1 CxRegister = 0x24
RegisterTest0 CxRegister = 0x25
RegisterPaTable0 CxRegister = 0x2e
// 24MHz crystal
OscillatorHz = 24000000
)
// GetFrequency returns the radio's current tuning in Hz (from Kenneth)
func (crl *ConnectedRileyLink) GetFrequency() (uint32, error) {
var (
frequency uint32 = 0
value byte
err error
)
log.Debug("reading FREQ2")
value, err = crl.ReadRegister(RegisterFreq2)
if err != nil {
return 0, err
}
frequency += uint32(value) << 16
log.Debug("reading FREQ1")
value, err = crl.ReadRegister(RegisterFreq1)
if err != nil {
return 0, err
}
frequency += uint32(value) << 8
log.Debug("reading FREQ0")
value, err = crl.ReadRegister(RegisterFreq0)
if err != nil {
return 0, err
}
frequency += uint32(value)
// oscillator multiplier
frequency = uint32(uint64(frequency) * OscillatorHz >> 16)
return frequency, nil
}
// SetFrequency tells the CC to tune to a specific frequency
func (crl *ConnectedRileyLink) SetFrequency(freq uint32) error {
var err error
// oscilator multiplier
freqcal := (uint64(freq)<<16 + OscillatorHz/2) / OscillatorHz
freq2 := byte(freqcal >> 16)
log.WithField("freq2", freq2).Debug("writing FREQ2")
err = crl.WriteRegister(RegisterFreq2, freq2)
if err != nil {
return err
}
freq1 := byte(freqcal >> 8)
log.WithField("freq1", freq1).Debug("writing FREQ1")
err = crl.WriteRegister(RegisterFreq1, freq1)
if err != nil {
return err
}
freq0 := byte(freqcal)
log.WithField("freq0", freq0).Debug("writing FREQ0")
err = crl.WriteRegister(RegisterFreq0, freq0)
if err != nil {
return err
}
return nil
}