-
Notifications
You must be signed in to change notification settings - Fork 10
/
si4432.cpp
493 lines (361 loc) · 13.5 KB
/
si4432.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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#include "si4432.h"
#include <SPI.h>
#define MAX_TRANSMIT_TIMEOUT 200
// #define DEBUG
//values here are kept in khz x 10 format (for not to deal with decimals) - look at AN440 page 26 for whole table
const uint16_t IFFilterTable[][2] = { { 322, 0x26 }, { 3355, 0x88 }, { 3618, 0x89 }, { 4202, 0x8A }, { 4684, 0x8B }, {
5188, 0x8C }, { 5770, 0x8D }, { 6207, 0x8E } };
Si4432::Si4432(uint8_t sdnPin, uint8_t InterruptPin) :
_sdnPin(sdnPin), _intPin(InterruptPin), _freqCarrier(433000000), _freqChannel(0), _kbps(100), _packageSign(
0xDEAD) { // default is 450 mhz
}
void Si4432::setFrequency(unsigned long baseFrequencyMhz) {
if ((baseFrequencyMhz < 240) || (baseFrequencyMhz > 930))
return; // invalid frequency
_freqCarrier = baseFrequencyMhz;
byte highBand = 0;
if (baseFrequencyMhz >= 480) {
highBand = 1;
}
double fPart = (baseFrequencyMhz / (10 * (highBand + 1))) - 24;
uint8_t freqband = (uint8_t) fPart; // truncate the int
uint16_t freqcarrier = (fPart - freqband) * 64000;
// sideband is always on (0x40) :
byte vals[3] = { 0x40 | (highBand << 5) | (freqband & 0x3F), freqcarrier >> 8, freqcarrier & 0xFF };
BurstWrite(REG_FREQBAND, vals, 3);
}
void Si4432::setCommsSignature(uint16_t signature) {
_packageSign = signature;
ChangeRegister(REG_TRANSMIT_HEADER3, _packageSign >> 8); // header (signature) byte 3 val
ChangeRegister(REG_TRANSMIT_HEADER2, (_packageSign & 0xFF)); // header (signature) byte 2 val
ChangeRegister(REG_CHECK_HEADER3, _packageSign >> 8); // header (signature) byte 3 val for receive checks
ChangeRegister(REG_CHECK_HEADER2, (_packageSign & 0xFF)); // header (signature) byte 2 val for receive checks
#ifdef DEBUG
Serial.println("Package signature is set!");
#endif
}
void Si4432::init() {
if (_intPin != 0)
pinMode(_intPin, INPUT);
pinMode(_sdnPin, OUTPUT);
turnOff();
pinMode(SS, OUTPUT);
digitalWrite(SS, HIGH); // set pin high, so chip would know we don't use it. - well, it's turned off anyway but...
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV16); // 16/ 2 = 8 MHZ. Max. is 10 MHZ, so we're cool.
SPI.setDataMode(SPI_MODE0);
#ifdef DEBUG
Serial.println("SPI is initialized now.");
#endif
hardReset();
}
void Si4432::boot() {
/*
byte currentFix[] = { 0x80, 0x40, 0x7F };
BurstWrite(REG_CHARGEPUMP_OVERRIDE, currentFix, 3); // refer to AN440 for reasons
ChangeRegister(REG_GPIO0_CONF, 0x0F); // tx/rx data clk pin
ChangeRegister(REG_GPIO1_CONF, 0x00); // POR inverted pin
ChangeRegister(REG_GPIO2_CONF, 0x1C); // clear channel pin
*/
ChangeRegister(REG_AFC_TIMING_CONTROL, 0x02); // refer to AN440 for reasons
ChangeRegister(REG_AFC_LIMITER, 0xFF); // write max value - excel file did that.
ChangeRegister(REG_AGC_OVERRIDE, 0x60); // max gain control
ChangeRegister(REG_AFC_LOOP_GEARSHIFT_OVERRIDE, 0x3C); // turn off AFC
ChangeRegister(REG_DATAACCESS_CONTROL, 0xAD); // enable rx packet handling, enable tx packet handling, enable CRC, use CRC-IBM
ChangeRegister(REG_HEADER_CONTROL1, 0x0C); // no broadcast address control, enable check headers for bytes 3 & 2
ChangeRegister(REG_HEADER_CONTROL2, 0x22); // enable headers byte 3 & 2, no fixed package length, sync word 3 & 2
ChangeRegister(REG_PREAMBLE_LENGTH, 0x08); // 8 * 4 bits = 32 bits (4 bytes) preamble length
ChangeRegister(REG_PREAMBLE_DETECTION, 0x3A); // validate 7 * 4 bits of preamble in a package
ChangeRegister(REG_SYNC_WORD3, 0x2D); // sync byte 3 val
ChangeRegister(REG_SYNC_WORD2, 0xD4); // sync byte 2 val
ChangeRegister(REG_TX_POWER, 0x1F); // max power
ChangeRegister(REG_CHANNEL_STEPSIZE, 0x64); // each channel is of 1 Mhz interval
setFrequency(_freqCarrier); // default freq
setBaudRate(_kbps); // default baud rate is 100kpbs
setChannel(_freqChannel); // default channel is 0
setCommsSignature(_packageSign); // default signature
switchMode(Ready);
}
bool Si4432::sendPacket(uint8_t length, const byte* data, bool waitResponse, uint32_t ackTimeout,
uint8_t* responseLength, byte* responseBuffer) {
clearTxFIFO();
ChangeRegister(REG_PKG_LEN, length);
BurstWrite(REG_FIFO, data, length);
ChangeRegister(REG_INT_ENABLE1, 0x04); // set interrupts on for package sent
ChangeRegister(REG_INT_ENABLE2, 0x00); // set interrupts off for anything else
//read interrupt registers to clean them
ReadRegister(REG_INT_STATUS1);
ReadRegister(REG_INT_STATUS2);
switchMode(TXMode | Ready);
uint64_t enterMillis = millis();
while (millis() - enterMillis < MAX_TRANSMIT_TIMEOUT) {
if ((_intPin != 0) && (digitalRead(_intPin) != 0)) {
continue;
}
byte intStatus = ReadRegister(REG_INT_STATUS1);
ReadRegister(REG_INT_STATUS2);
if (intStatus & 0x04) {
switchMode(Ready | TuneMode);
#ifdef DEBUG
Serial.print("Package sent! -- ");
Serial.println(intStatus, HEX);
#endif
// package sent. now, return true if not to wait ack, or wait ack (wait for packet only for 'remaining' amount of time)
if (waitResponse) {
if (waitForPacket(ackTimeout)) {
getPacketReceived(responseLength, responseBuffer);
return true;
} else {
return false;
}
} else {
return true;
}
}
}
//timeout occurred.
//#ifdef DEBUG
Serial.println("Timeout in Transit -- ");
//#endif
switchMode(Ready);
if (ReadRegister(REG_DEV_STATUS) & 0x80) {
clearFIFO();
}
return false;
}
bool Si4432::waitForPacket(uint64_t waitMs) {
startListening();
uint64_t enterMillis = millis();
while (millis() - enterMillis < waitMs) {
if (!isPacketReceived()) {
continue;
} else {
return true;
}
}
//timeout occurred.
Serial.println("Timeout in receive-- ");
switchMode(Ready);
clearRxFIFO();
return false;
}
void Si4432::getPacketReceived(uint8_t* length, byte* readData) {
*length = ReadRegister(REG_RECEIVED_LENGTH);
BurstRead(REG_FIFO, readData, *length);
clearRxFIFO(); // which will also clear the interrupts
}
void Si4432::setChannel(byte channel) {
ChangeRegister(REG_FREQCHANNEL, channel);
}
void Si4432::switchMode(byte mode) {
ChangeRegister(REG_STATE, mode); // receive mode
//delay(20);
#ifdef DEBUG
byte val = ReadRegister(REG_DEV_STATUS);
if (val == 0 || val == 0xFF) {
Serial.print(val, HEX);
Serial.println(" -- WHAT THE HELL!!");
}
#endif
}
void Si4432::ChangeRegister(Registers reg, byte value) {
BurstWrite(reg, &value, 1);
}
void Si4432::setBaudRate(uint16_t kbps) {
// chip normally supports very low bps values, but they are cumbersome to implement - so I just didn't implement lower bps values
if ((kbps > 256) || (kbps < 1))
return;
_kbps = kbps;
byte freqDev = kbps <= 10 ? 15 : 150; // 15khz / 150 khz
byte modulationValue = _kbps < 30 ? 0x4c : 0x0c; // use FIFO Mode, GFSK, low baud mode on / off
byte modulationVals[] = { modulationValue, 0x23, round((freqDev * 1000.0) / 625.0) }; // msb of the kpbs to 3rd bit of register
BurstWrite(REG_MODULATION_MODE1, modulationVals, 3);
// set data rate
uint16_t bpsRegVal = round((kbps * (kbps < 30 ? 2097152 : 65536.0)) / 1000.0);
byte datarateVals[] = { bpsRegVal >> 8, bpsRegVal & 0xFF };
BurstWrite(REG_TX_DATARATE1, datarateVals, 2);
//now set the timings
uint16_t minBandwidth = (2 * (uint32_t) freqDev) + kbps;
#ifdef DEBUG
Serial.print("min Bandwidth value: ");
Serial.println(minBandwidth, HEX);
#endif
byte IFValue = 0xff;
//since the table is ordered (from low to high), just find the 'minimum bandwidth which is greater than required'
for (byte i = 0; i < 8; ++i) {
if (IFFilterTable[i][0] >= (minBandwidth * 10)) {
IFValue = IFFilterTable[i][1];
break;
}
}
#ifdef DEBUG
Serial.print("Selected IF value: ");
Serial.println(IFValue, HEX);
#endif
ChangeRegister(REG_IF_FILTER_BW, IFValue);
byte dwn3_bypass = (IFValue & 0x80) ? 1 : 0; // if msb is set
byte ndec_exp = (IFValue >> 4) & 0x07; // only 3 bits
uint16_t rxOversampling = round((500.0 * (1 + 2 * dwn3_bypass)) / ((pow(2, ndec_exp - 3)) * (double ) kbps));
uint32_t ncOffset = ceil(((double) kbps * (pow(2, ndec_exp + 20))) / (500.0 * (1 + 2 * dwn3_bypass)));
uint16_t crGain = 2 + ((65535 * (int64_t) kbps) / ((int64_t) rxOversampling * freqDev));
byte crMultiplier = 0x00;
if (crGain > 0x7FF) {
crGain = 0x7FF;
}
#ifdef DEBUG
Serial.print("dwn3_bypass value: ");
Serial.println(dwn3_bypass, HEX);
Serial.print("ndec_exp value: ");
Serial.println(ndec_exp, HEX);
Serial.print("rxOversampling value: ");
Serial.println(rxOversampling, HEX);
Serial.print("ncOffset value: ");
Serial.println(ncOffset, HEX);
Serial.print("crGain value: ");
Serial.println(crGain, HEX);
Serial.print("crMultiplier value: ");
Serial.println(crMultiplier, HEX);
#endif
byte timingVals[] = { rxOversampling & 0x00FF, ((rxOversampling & 0x0700) >> 3) | ((ncOffset >> 16) & 0x0F),
(ncOffset >> 8) & 0xFF, ncOffset & 0xFF, ((crGain & 0x0700) >> 8) | crMultiplier, crGain & 0xFF };
BurstWrite(REG_CLOCK_RECOVERY_OVERSAMPLING, timingVals, 6);
}
byte Si4432::ReadRegister(Registers reg) {
byte val = 0xFF;
BurstRead(reg, &val, 1);
return val;
}
void Si4432::BurstWrite(Registers startReg, const byte value[], uint8_t length) {
byte regVal = (byte) startReg | 0x80; // set MSB
digitalWrite(SS, LOW);
SPI.transfer(regVal);
for (byte i = 0; i < length; ++i) {
#ifdef DEBUG
Serial.print("Writing: ");
Serial.print((regVal != 0xFF ? (regVal + i) & 0x7F : 0x7F), HEX);
Serial.print(" | ");
Serial.println(value[i], HEX);
#endif
SPI.transfer(value[i]);
}
digitalWrite(SS, HIGH);
}
void Si4432::BurstRead(Registers startReg, byte value[], uint8_t length) {
byte regVal = (byte) startReg & 0x7F; // set MSB
digitalWrite(SS, LOW);
SPI.transfer(regVal);
for (byte i = 0; i < length; ++i) {
value[i] = SPI.transfer(0xFF);
#ifdef DEBUG
Serial.print("Reading: ");
Serial.print((regVal != 0x7F ? (regVal + i) & 0x7F : 0x7F), HEX);
Serial.print(" | ");
Serial.println(value[i], HEX);
#endif
}
digitalWrite(SS, HIGH);
}
void Si4432::readAll() {
byte allValues[0x7F];
BurstRead(REG_DEV_TYPE, allValues, 0x7F);
for (byte i = 0; i < 0x7f; ++i) {
Serial.print("REG(");
Serial.print((int) REG_DEV_TYPE + i, HEX);
Serial.print(") : ");
Serial.println((int) allValues[i], HEX);
}
}
void Si4432::clearTxFIFO() {
ChangeRegister(REG_OPERATION_CONTROL, 0x01);
ChangeRegister(REG_OPERATION_CONTROL, 0x00);
}
void Si4432::clearRxFIFO() {
ChangeRegister(REG_OPERATION_CONTROL, 0x02);
ChangeRegister(REG_OPERATION_CONTROL, 0x00);
}
void Si4432::clearFIFO() {
ChangeRegister(REG_OPERATION_CONTROL, 0x03);
ChangeRegister(REG_OPERATION_CONTROL, 0x00);
}
void Si4432::softReset() {
ChangeRegister(REG_STATE, 0x80);
byte reg = ReadRegister(REG_INT_STATUS2);
while ((reg & 0x02) != 0x02) {
delay(1);
reg = ReadRegister(REG_INT_STATUS2);
}
boot();
}
void Si4432::hardReset() {
turnOff();
turnOn();
byte reg = ReadRegister(REG_INT_STATUS2);
while ((reg & 0x02) != 0x02) {
Serial.print("POR: ");
Serial.println(reg, HEX);
delay(1);
reg = ReadRegister(REG_INT_STATUS2);
}
boot();
}
void Si4432::startListening() {
clearRxFIFO(); // clear first, so it doesn't overflow if packet is big
ChangeRegister(REG_INT_ENABLE1, 0x03); // set interrupts on for package received and CRC error
#ifdef DEBUG
ChangeRegister(REG_INT_ENABLE2, 0xC0);
#else
ChangeRegister(REG_INT_ENABLE2, 0x00); // set other interrupts off
#endif
//read interrupt registers to clean them
ReadRegister(REG_INT_STATUS1);
ReadRegister(REG_INT_STATUS2);
switchMode(RXMode | Ready);
}
bool Si4432::isPacketReceived() {
if ((_intPin != 0) && (digitalRead(_intPin) != 0)) {
return false; // if no interrupt occurred, no packet received is assumed (since startListening will be called prior, this assumption is enough)
}
// check for package received status interrupt register
byte intStat = ReadRegister(REG_INT_STATUS1);
#ifdef DEBUG
byte intStat2 = ReadRegister(REG_INT_STATUS2);
if (intStat2 & 0x40) { //interrupt occurred, check it && read the Interrupt Status1 register for 'preamble '
Serial.print("HEY!! HEY!! Valid Preamble detected -- ");
Serial.println(intStat2, HEX);
}
if (intStat2 & 0x80) { //interrupt occurred, check it && read the Interrupt Status1 register for 'preamble '
Serial.print("HEY!! HEY!! SYNC WORD detected -- ");
Serial.println(intStat2, HEX);
}
#else
ReadRegister(REG_INT_STATUS2);
#endif
if (intStat & 0x02) { //interrupt occurred, check it && read the Interrupt Status1 register for 'valid packet'
switchMode(Ready | TuneMode); // if packet came, get out of Rx mode till the packet is read out. Keep PLL on for fast reaction
#ifdef DEBUG
Serial.print("Packet detected -- ");
Serial.println(intStat, HEX);
#endif
return true;
} else if (intStat & 0x01) { // packet crc error
switchMode(Ready); // get out of Rx mode till buffers are cleared
//#ifdef DEBUG
Serial.print("CRC Error in Packet detected!-- ");
Serial.println(intStat, HEX);
//#endif
clearRxFIFO();
switchMode(RXMode | Ready); // get back to work
return false;
}
//no relevant interrupt? no packet!
return false;
}
void Si4432::turnOn() {
digitalWrite(_sdnPin, LOW); // turn on the chip now
delay(20);
}
void Si4432::turnOff() {
digitalWrite(_sdnPin, HIGH); // turn off the chip now
delay(1);
}