-
Notifications
You must be signed in to change notification settings - Fork 16
/
SX1276.cpp
256 lines (203 loc) · 5.74 KB
/
SX1276.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
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
#include "SX1276.h"
// registers
#define REG_FIFO 0x00
#define REG_OP_MODE 0x01
#define REG_BITRATE_H 0x02
#define REG_BITRATE_L 0x03
#define REG_FDEV_H 0x04
#define REG_FDEV_L 0x05
#define REG_FRF_H 0x06
#define REG_FRF_M 0x07
#define REG_FRF_L 0x08
#define REG_RXCFG 0x0D
#define REG_RSSI 0x11
#define REG_RXBW 0x12
#define REG_AFCBW 0x13
#define REG_AFC_H 0x1B
#define REG_AFC_L 0x1C
#define REG_FEI_H 0x1D
#define REG_FEI_L 0x1E
#define REG_PAYLOAD_LEN 0x32
#define REG_VERSION 0x42
// modes
#define MODE_LONG_RANGE_MODE 0x80
#define MODE_SLEEP 0x00
#define MODE_STDBY 0x01
#define MODE_TX 0x03
#define MODE_RX_CONTINUOUS 0x05
#define MODE_RX_SINGLE 0x06
SX1276Class::SX1276Class() :
_spiSettings(10E6, MSBFIRST, SPI_MODE0),
_ss(SX1276_DEFAULT_SS_PIN),
_reset(SX1276_DEFAULT_RESET_PIN),
_dio0(SX1276_DEFAULT_DIO0_PIN)
{
}
int SX1276Class::begin()
{
pinMode(_ss, OUTPUT);
pinMode(_reset, OUTPUT);
pinMode(_dio0, INPUT);
digitalWrite(_reset, LOW);
delay(20);
digitalWrite(_reset, HIGH);
delay(50);
digitalWrite(_ss, HIGH);
SPI.begin();
uint8_t version = readRegister(REG_VERSION);
if (version != 0x12)
{
return 0;
}
init();
return 1;
}
void SX1276Class::init()
{
uint8_t initRegs[] = { 0x01, 0x78, 0x02, 0xB8, 0xD8, 0xFC, 0x8B, 0xFC, 0x49, 0x2B, 0x23, 0x08, 0x02, 0x0A, 0xFF, 0xB9, 0x12, 0x01, 0x28, 0x0C, 0x12, 0x47, 0x32, 0x3E, 0x00, 0x00, 0x00, 0xFD, 0x38, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x12, 0x3E, 0x6B, 0x3E, 0x55, 0x55, 0x55, 0x55, 0x55, 0x48, 0x40, 0x17, 0x00, 0x00, 0x0F, 0x00, 0x0C, 0x00, 0xF5, 0x20, 0x82, 0xE2, 0x02, 0xDA, 0x24, 0x00, 0x00, 0x12, 0x24, 0x2D, 0x00, 0x03, 0x00, 0x04, 0x23, 0x00, 0x09, 0x05, 0x84, 0x32, 0x2B, 0x14, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x0F, 0xE0, 0x00, 0x0C, 0xD9, 0x07, 0x00, 0x5C, 0x78, 0x00, 0x1C, 0x0E, 0x5B, 0xCC, 0x0E, 0x7F, 0x50, 0x00, 0x00, 0x00, 0x00, 0x09, 0x3F, 0xB1, 0x0B };
setSleep();
for(int reg = 0; reg < sizeof(initRegs); reg++)
{
writeRegister(2 + reg, initRegs[reg]);
}
}
void SX1276Class::writeAfc(uint32_t freq)
{
uint32_t regFreq = (uint64_t)freq * (1<<19) / 32000000ULL;
writeRegister(REG_AFC_H, regFreq >> 8);
writeRegister(REG_AFC_L, regFreq >> 0);
}
int32_t SX1276Class::readAfc()
{
int16_t reg = 0;
reg |= (readRegister(REG_AFC_H) << 8);
reg |= (readRegister(REG_AFC_L) << 0);
int32_t ret = (int64_t)reg * 32000000ULL / (1<<19);
return ret;
}
int32_t SX1276Class::readFei()
{
int16_t reg = 0;
reg |= (readRegister(REG_FEI_H) << 8);
reg |= (readRegister(REG_FEI_L) << 0);
int32_t ret = (int64_t)reg * 32000000ULL / (1<<19);
return ret;
}
uint8_t SX1276Class::readRssi()
{
return readRegister(REG_RSSI);
}
uint8_t SX1276Class::readFifo()
{
return readRegister(REG_FIFO);
}
uint32_t SX1276Class::delta(uint32_t ref, uint32_t value)
{
if(ref > value)
{
return ref - value;
}
return value - ref;
}
void SX1276Class::writeRxBw(uint32_t bw)
{
uint32_t regBw = 0;
uint32_t bestBw = 0;
for(int exp = 1; exp < 8; exp++)
{
for(int mant = 0; mant < 3; mant++)
{
uint32_t matchBw = (8000000 / (16+mant*4)) >> exp;
if(delta(bw, matchBw) < delta(bw, bestBw))
{
bestBw = matchBw;
regBw = (exp << 0) | (mant << 3);
}
}
}
writeRegister(REG_RXBW, regBw);
}
void SX1276Class::writeAfcBw(uint32_t bw)
{
uint32_t regBw = 0;
uint32_t bestBw = 0;
for(int exp = 1; exp < 8; exp++)
{
for(int mant = 0; mant < 3; mant++)
{
uint32_t matchBw = (8000000 / (16+mant*4)) >> exp;
if(delta(bw, matchBw) < delta(bw, bestBw))
{
bestBw = matchBw;
regBw = (exp << 0) | (mant << 3);
}
}
}
writeRegister(REG_AFCBW, regBw);
}
void SX1276Class::writeBitrate(uint32_t rate)
{
uint32_t regBitRate = 32000000ULL / rate;
writeRegister(REG_BITRATE_H, regBitRate >> 8);
writeRegister(REG_BITRATE_L, regBitRate >> 0);
}
void SX1276Class::writeFreqDev(uint32_t dev)
{
uint32_t regFreqDev = (uint64_t)dev * (1<<19) / 32000000ULL;
writeRegister(REG_FDEV_H, regFreqDev >> 8);
writeRegister(REG_FDEV_L, regFreqDev >> 0);
}
void SX1276Class::writeFreq(uint64_t freq)
{
uint32_t regFrf = freq * (1<<19) / 32000000;
writeRegister(REG_FRF_H, regFrf >> 16);
writeRegister(REG_FRF_M, regFrf >> 8);
writeRegister(REG_FRF_L, regFrf >> 0);
}
void SX1276Class::writePayloadLength(uint8_t length)
{
writeRegister(REG_PAYLOAD_LEN, length);
}
void SX1276Class::writeRxConfig(uint8_t cfg)
{
writeRegister(REG_RXCFG, cfg);
}
void SX1276Class::setSleep()
{
delayMicroseconds(100);
writeRegister(REG_OP_MODE, MODE_SLEEP);
delayMicroseconds(100);
writeRegister(REG_OP_MODE, MODE_SLEEP);
}
void SX1276Class::setStandby()
{
writeRegister(REG_OP_MODE, MODE_STDBY);
}
void SX1276Class::setRx()
{
writeRegister(REG_OP_MODE, MODE_STDBY);
writeRegister(REG_OP_MODE, MODE_RX_CONTINUOUS);
}
void SX1276Class::setSPIFrequency(uint32_t frequency)
{
_spiSettings = SPISettings(frequency, MSBFIRST, SPI_MODE0);
}
uint8_t SX1276Class::readRegister(uint8_t address)
{
return singleTransfer(address & 0x7f, 0x00);
}
void SX1276Class::writeRegister(uint8_t address, uint8_t value)
{
singleTransfer(address | 0x80, value);
}
uint8_t SX1276Class::singleTransfer(uint8_t address, uint8_t value)
{
uint8_t response;
digitalWrite(_ss, LOW);
SPI.beginTransaction(_spiSettings);
SPI.transfer(address);
response = SPI.transfer(value);
SPI.endTransaction();
digitalWrite(_ss, HIGH);
return response;
}