|
@@ -1,460 +1,470 @@
|
1
|
|
-/**
|
2
|
|
- * Author Wojciech Domski <Wojciech.Domski@gmail.com>
|
3
|
|
- * www: www.Domski.pl
|
4
|
|
- *
|
5
|
|
- * work based on DORJI.COM sample code and
|
6
|
|
- * https://github.com/realspinner/SX1276_LoRa
|
7
|
|
- */
|
8
|
|
-
|
9
|
|
-#include "SX1276.h"
|
10
|
|
-#include <string.h>
|
11
|
|
-
|
12
|
|
-
|
13
|
|
-//////////////////////////////////
|
14
|
|
-// logic
|
15
|
|
-//////////////////////////////////
|
16
|
|
-
|
17
|
|
-__weak void SX1276_hw_init(SX1276_hw_t * hw) {
|
18
|
|
- SX1276_hw_SetNSS(hw, 1);
|
19
|
|
- HAL_GPIO_WritePin(hw->reset.port, hw->reset.pin, GPIO_PIN_SET);
|
20
|
|
-}
|
21
|
|
-
|
22
|
|
-__weak void SX1276_hw_SetNSS(SX1276_hw_t * hw, int value) {
|
23
|
|
- HAL_GPIO_WritePin(hw->nss.port, hw->nss.pin,
|
24
|
|
- (value == 1) ? GPIO_PIN_SET : GPIO_PIN_RESET);
|
25
|
|
-}
|
26
|
|
-
|
27
|
|
-__weak void SX1276_hw_Reset(SX1276_hw_t * hw) {
|
28
|
|
- SX1276_hw_SetNSS(hw, 1);
|
29
|
|
- HAL_GPIO_WritePin(hw->reset.port, hw->reset.pin, GPIO_PIN_RESET);
|
30
|
|
-
|
31
|
|
- SX1276_hw_DelayMs(1);
|
32
|
|
-
|
33
|
|
- HAL_GPIO_WritePin(hw->reset.port, hw->reset.pin, GPIO_PIN_SET);
|
34
|
|
-
|
35
|
|
- SX1276_hw_DelayMs(100);
|
36
|
|
-}
|
37
|
|
-
|
38
|
|
-#if 0 // PYJ.2019.04.01_BEGIN --
|
39
|
|
-__weak void SX1276_hw_SPICommand(SX1276_hw_t * hw, uint8_t cmd) {
|
40
|
|
- SX1276_hw_SetNSS(hw, 0);
|
41
|
|
- HAL_SPI_Transmit(hw->spi, &cmd, 1, 1000);
|
42
|
|
- while (HAL_SPI_GetState(hw->spi) != HAL_SPI_STATE_READY)
|
43
|
|
- ;
|
44
|
|
-}
|
45
|
|
-#endif // PYJ.2019.04.01_END --
|
46
|
|
-void SX1276_hw_SPICommand(SX1276_hw_t * hw, uint8_t cmd) {
|
47
|
|
- SX1276_hw_SetNSS(hw, 0);
|
48
|
|
- BLUECELL_SPI_Transmit(cmd);
|
49
|
|
-}
|
50
|
|
-
|
51
|
|
-#if 0 // PYJ.2019.04.01_BEGIN --
|
52
|
|
-__weak uint8_t SX1276_hw_SPIReadByte(SX1276_hw_t * hw) {
|
53
|
|
- uint8_t txByte = 0x00;
|
54
|
|
- uint8_t rxByte = 0x00;
|
55
|
|
-
|
56
|
|
- SX1276_hw_SetNSS(hw, 0);
|
57
|
|
- HAL_SPI_TransmitReceive(hw->spi, &txByte, &rxByte, 1, 1000);
|
58
|
|
- return rxByte;
|
59
|
|
-}
|
60
|
|
-#endif // PYJ.2019.04.01_END --
|
61
|
|
-uint8_t SX1276_hw_SPIReadByte(SX1276_hw_t * hw) {
|
62
|
|
- //uint8_t txByte = 0x00;
|
63
|
|
- uint8_t rxByte = 0x00;
|
64
|
|
-
|
65
|
|
- SX1276_hw_SetNSS(hw, 0);
|
66
|
|
-// HAL_SPI_TransmitReceive(hw->spi, &txByte, &rxByte, 1, 1000);
|
67
|
|
- rxByte = SpiRead();
|
68
|
|
- return rxByte;
|
69
|
|
-}
|
70
|
|
-
|
71
|
|
-__weak void SX1276_hw_DelayMs(uint32_t msec) {
|
72
|
|
- HAL_Delay(msec);
|
73
|
|
-}
|
74
|
|
-
|
75
|
|
-__weak int SX1276_hw_GetDIO0(SX1276_hw_t * hw) {
|
76
|
|
- return (HAL_GPIO_ReadPin(hw->dio0.port, hw->dio0.pin) == GPIO_PIN_SET);
|
77
|
|
-}
|
78
|
|
-
|
79
|
|
-//////////////////////////////////
|
80
|
|
-// logic
|
81
|
|
-//////////////////////////////////
|
82
|
|
-
|
83
|
|
-uint8_t SX1276_SPIRead(SX1276_t * module, uint8_t addr) {
|
84
|
|
- uint8_t tmp;
|
85
|
|
- SX1276_hw_SPICommand(module->hw, addr);
|
86
|
|
- tmp = SX1276_hw_SPIReadByte(module->hw);
|
87
|
|
- SX1276_hw_SetNSS(module->hw, 1);
|
88
|
|
- return tmp;
|
89
|
|
-}
|
90
|
|
-uint8_t SX1276_SPIIDRead(SX1276_t * module, uint8_t addr) {
|
91
|
|
- uint8_t tmp;
|
92
|
|
- SX1276_hw_SPICommand(module->hw, addr);
|
93
|
|
- tmp = SX1276_hw_SPIReadByte(module->hw);
|
94
|
|
- SX1276_hw_SetNSS(module->hw, 1);
|
95
|
|
- return tmp;
|
96
|
|
-}
|
97
|
|
-
|
98
|
|
-void SX1276_SPIWrite(SX1276_t * module, uint8_t addr, uint8_t cmd) {
|
99
|
|
- SX1276_hw_SetNSS(module->hw, 0);
|
100
|
|
- SX1276_hw_SPICommand(module->hw, addr | 0x80);
|
101
|
|
- SX1276_hw_SPICommand(module->hw, cmd);
|
102
|
|
-// SPIGPIOTxRx(addr | 0x80,cmd);
|
103
|
|
-
|
104
|
|
- SX1276_hw_SetNSS(module->hw, 1);
|
105
|
|
-}
|
106
|
|
-
|
107
|
|
-void SX1276_SPIBurstRead(SX1276_t * module, uint8_t addr, uint8_t* rxBuf,
|
108
|
|
- uint8_t length) {
|
109
|
|
- uint8_t i;
|
110
|
|
- if (length <= 1) {
|
111
|
|
- return;
|
112
|
|
- } else {
|
113
|
|
- SX1276_hw_SetNSS(module->hw, 0);
|
114
|
|
- SX1276_hw_SPICommand(module->hw, addr);
|
115
|
|
- //printf("Test Data:");
|
116
|
|
- for (i = 0; i < length; i++) {
|
117
|
|
- rxBuf[i] = SX1276_hw_SPIReadByte(module->hw);
|
118
|
|
- //printf("%02x ",rxBuf[i]);
|
119
|
|
- }
|
120
|
|
- //printf("\n");
|
121
|
|
- SX1276_hw_SetNSS(module->hw, 1);
|
122
|
|
- }
|
123
|
|
-}
|
124
|
|
-
|
125
|
|
-void SX1276_SPIBurstWrite(SX1276_t * module, uint8_t addr, uint8_t* txBuf,
|
126
|
|
- uint8_t length) {
|
127
|
|
- uint8_t i;
|
128
|
|
- if (length <= 1) {
|
129
|
|
- return;
|
130
|
|
- } else {
|
131
|
|
- SX1276_hw_SetNSS(module->hw, 0);
|
132
|
|
- SX1276_hw_SPICommand(module->hw, addr | 0x80);
|
133
|
|
-// printf("Test Data:");
|
134
|
|
- for (i = 0; i < length; i++) {
|
135
|
|
- SX1276_hw_SPICommand(module->hw, txBuf[i]);
|
136
|
|
-// printf("%02x ",txBuf[i]);
|
137
|
|
- }
|
138
|
|
-// printf("\n");
|
139
|
|
- SX1276_hw_SetNSS(module->hw, 1);
|
140
|
|
- }
|
141
|
|
-}
|
142
|
|
- /*
|
143
|
|
- RegPaConfig - 0x09
|
144
|
|
- PaSelect Mode Power Range Pout Formula
|
145
|
|
- 0 PA_HF or PA_LF on RFO_HF or RFO_LF -4 to +15dBm Pout=Pmax-(15-OutputPower), Pmax=10.8+0.6*MaxPower [dBm]
|
146
|
|
- 1 PA_HP on PA_BOOST, any frequency +2 to +17dBm Pout=17-(15-OutputPower) [dBm]
|
147
|
|
- PA_LF option doesn't seem to transmit anything. Not connected on these boards, perhaps?
|
148
|
|
- Use only PA_BOOST mode.
|
149
|
|
-
|
150
|
|
- PA_LF Pmax
|
151
|
|
- Pout 0 1 2 3 4 5 6 7
|
152
|
|
- 0 -4.2 -3.6 -3 -2.4 -1.8 -1.2 -0.6 0
|
153
|
|
- 1 -3.2 -2.6 -2 -1.4 -0.8 -0.2 0.4 1
|
154
|
|
- 2 -2.2 -1.6 -1 -0.4 0.2 0.8 1.4 2
|
155
|
|
- 3 -1.2 -0.6 0 0.6 1.2 1.8 2.4 3
|
156
|
|
- 4 -0.2 0.4 1 1.6 2.2 2.8 3.4 4
|
157
|
|
- 5 0.8 1.4 2 2.6 3.2 3.8 4.4 5
|
158
|
|
- 6 1.8 2.4 3 3.6 4.2 4.8 5.4 6
|
159
|
|
- 7 2.8 3.4 4 4.6 5.2 5.8 6.4 7
|
160
|
|
- 8 3.8 4.4 5 5.6 6.2 6.8 7.4 8
|
161
|
|
- 9 4.8 5.4 6 6.6 7.2 7.8 8.4 9
|
162
|
|
- 10 5.8 6.4 7 7.6 8.2 8.8 9.4 10
|
163
|
|
- 11 6.8 7.4 8 8.6 9.2 9.8 10.4 11
|
164
|
|
- 12 7.8 8.4 9 9.6 10.2 10.8 11.4 12
|
165
|
|
- 13 8.8 9.4 10 10.6 11.2 11.8 12.4 13
|
166
|
|
- 14 9.8 10.4 11 11.6 12.2 12.8 13.4 14
|
167
|
|
- 15 10.8 11.4 12 12.6 13.2 13.8 14.4 15 [dBm]
|
168
|
|
-
|
169
|
|
- PA_BOOST
|
170
|
|
- Pout OutputPower
|
171
|
|
- 0 2 dBm
|
172
|
|
- 1 3 dBm
|
173
|
|
- 2 4 dBm
|
174
|
|
- 3 5 dBm
|
175
|
|
- 4 6 dBm
|
176
|
|
- 5 7 dBm
|
177
|
|
- 6 8 dBm
|
178
|
|
- 7 9 dBm
|
179
|
|
- 8 10 dBm
|
180
|
|
- 9 11 dBm
|
181
|
|
- 10 12 dBm
|
182
|
|
- 11 13 dBm
|
183
|
|
- 12 14 dBm
|
184
|
|
- 13 15 dBm
|
185
|
|
- 14 16 dBm
|
186
|
|
- 15 17 dBm
|
187
|
|
-
|
188
|
|
- PaSelect
|
189
|
|
- 0 RFO pin. Maximum power of +14 dBm
|
190
|
|
- 1 PA_BOOST pin. Maximum power of +20 dBm
|
191
|
|
- MaxPower
|
192
|
|
- Select max output power: Pmax=10.8+0.6*MaxPower [dBm]
|
193
|
|
- OutputPower
|
194
|
|
- Pout=Pmax-(15-OutputPower) if PaSelect = 0 (RFO pins)
|
195
|
|
- Pout=17-(15-OutputPower) if PaSelect = 1 (PA_BOOST pin)
|
196
|
|
- PA_HF and PA_LF are high efficiency amplifiers capable of yielding RF power programmable in 1 dB steps
|
197
|
|
- from -4 to +14dBm directly into a 50 ohm load with low current consumption. PA_LF covers the lower bands
|
198
|
|
- (up to 525 MHz), whilst PA_HF will cover the upper bands (from 860 MHz).
|
199
|
|
- PA_HP (High Power), connected to the PA_BOOST pin, covers all frequency bands that the chip addresses.
|
200
|
|
- It permits continuous operation at up to +17 dBm and duty cycled operation at up to +20dBm.
|
201
|
|
- RegOcp - 0x0B
|
202
|
|
- OcpTrim IMAX Imax Formula
|
203
|
|
- 0 to 15 45 to 120 mA 45 + 5*OcpTrim [mA]
|
204
|
|
- 16 to 27 130 to 240 mA -30 + 10*OcpTrim [mA]
|
205
|
|
- 27+ 240 mA 240 mA
|
206
|
|
-
|
207
|
|
- The power amplifiers of RFM95/96/97/98(W) are protected against current over supply in adverse RF load
|
208
|
|
- conditions by the over current protection block. The current limiter value is controlled by the OcpTrim
|
209
|
|
- bits in RegOcp.
|
210
|
|
- Imax sets a limit on the current drain of the Power Amplifier only, hence the maximum current drain of the
|
211
|
|
- RFM96/77/78 is equal to Imax + IFS.
|
212
|
|
- Default 100mA changed to 240mA.
|
213
|
|
-*/
|
214
|
|
-
|
215
|
|
-void SX1276_set_power(SX1276_t * module)
|
216
|
|
-{
|
217
|
|
-// SX1276_SPIWrite(LR_RegPaConfig, (PaSelect << 7) | ((MaxPower & 0x07) << 4) | (OutputPower & 0x0F));
|
218
|
|
- SX1276_SPIWrite(module,LR_RegPaConfig, (module->LoRa_Pa_boost << 7) | ((0x07) << 4) | (module->power & 0x0F));
|
219
|
|
- //RFO -> 0 ~ 15 / PA_BOOST -> 2 ~ 17
|
220
|
|
-}
|
221
|
|
-void SX1276_defaultConfig(SX1276_t * module) {
|
222
|
|
- SX1276_config(module, module->frequency, module->power, module->LoRa_Rate,
|
223
|
|
- module->LoRa_BW,module->LoRa_Lna,module->LoRa_Pa_boost);
|
224
|
|
-}
|
225
|
|
-
|
226
|
|
-void SX1276_config(SX1276_t * module, uint8_t frequency, uint8_t power,
|
227
|
|
- uint8_t LoRa_Rate, uint8_t LoRa_BW,uint8_t LoRa_Lna,uint8_t LoRa_PaBoost) {
|
228
|
|
- SX1276_sleep(module); //Change modem mode Must in Sleep mode
|
229
|
|
- SX1276_hw_DelayMs(15);
|
230
|
|
-
|
231
|
|
- SX1276_entryLoRa(module);
|
232
|
|
- //SX1276_SPIWrite(module, 0x5904); //?? Change digital regulator form 1.6V to 1.47V: see errata note
|
233
|
|
-
|
234
|
|
- SX1276_SPIBurstWrite(module, LR_RegFrMsb,
|
235
|
|
- (uint8_t*) SX1276_Frequency[frequency], 3); //setting frequency parameter
|
236
|
|
-
|
237
|
|
- //setting base parameter
|
238
|
|
- SX1276_SPIWrite(module, LR_RegPaConfig, SX1276_Power[power]); //Setting output power parameter
|
239
|
|
-
|
240
|
|
- SX1276_SPIWrite(module, LR_RegOcp, 0x0B); //RegOcp,Close Ocp
|
241
|
|
-// SX1276_SPIWrite(module, LR_RegLna, 0x20); //RegLNA,High & LNA Enable
|
242
|
|
- SX1276_set_power(module);
|
243
|
|
-
|
244
|
|
- SX1276_SPIWrite(module, LR_RegLna, LoRa_Lna); //RegLNA,High & LNA Enable
|
245
|
|
-
|
246
|
|
- if (SX1276_SpreadFactor[LoRa_Rate] == 6) { //SFactor=6
|
247
|
|
- uint8_t tmp;
|
248
|
|
- SX1276_SPIWrite(module,
|
249
|
|
- LR_RegModemConfig1,
|
250
|
|
- ((SX1276_LoRaBandwidth[LoRa_BW] << 4) + (SX1276_CR << 1) + 0x01)); //Implicit Enable CRC Enable(0x02) & Error Coding rate 4/5(0x01), 4/6(0x02), 4/7(0x03), 4/8(0x04)
|
251
|
|
-
|
252
|
|
- SX1276_SPIWrite(module,
|
253
|
|
- LR_RegModemConfig2,
|
254
|
|
- ((SX1276_SpreadFactor[LoRa_Rate] << 4) + (SX1276_CRC << 2)
|
255
|
|
- + 0x03));
|
256
|
|
-
|
257
|
|
- tmp = SX1276_SPIRead(module, 0x31);
|
258
|
|
- tmp &= 0xF8;
|
259
|
|
- tmp |= 0x05;
|
260
|
|
- SX1276_SPIWrite(module, 0x31, tmp);
|
261
|
|
- SX1276_SPIWrite(module, 0x37, 0x0C);
|
262
|
|
- } else {
|
263
|
|
- SX1276_SPIWrite(module,
|
264
|
|
- LR_RegModemConfig1,
|
265
|
|
- ((SX1276_LoRaBandwidth[LoRa_BW] << 4) + (SX1276_CR << 1) + 0x00)); //Explicit Enable CRC Enable(0x02) & Error Coding rate 4/5(0x01), 4/6(0x02), 4/7(0x03), 4/8(0x04)
|
266
|
|
-
|
267
|
|
- SX1276_SPIWrite(module,
|
268
|
|
- LR_RegModemConfig2,
|
269
|
|
- ((SX1276_SpreadFactor[LoRa_Rate] << 4) + (SX1276_CRC << 2)
|
270
|
|
- + 0x03)); //SFactor & LNA gain set by the internal AGC loop
|
271
|
|
- }
|
272
|
|
-
|
273
|
|
- SX1276_SPIWrite(module, LR_RegSymbTimeoutLsb, 0xFF); //RegSymbTimeoutLsb Timeout = 0x3FF(Max)
|
274
|
|
- SX1276_SPIWrite(module, LR_RegPreambleMsb, 0x00); //RegPreambleMsb
|
275
|
|
- SX1276_SPIWrite(module, LR_RegPreambleLsb, 12); //RegPreambleLsb 8+4=12byte Preamble
|
276
|
|
- SX1276_SPIWrite(module, REG_LR_DIOMAPPING2, 0x01); //RegDioMapping2 DIO5=00, DIO4=01
|
277
|
|
- module->readBytes = 0;
|
278
|
|
- SX1276_standby(module); //Entry standby mode
|
279
|
|
-}
|
280
|
|
-
|
281
|
|
-void SX1276_standby(SX1276_t * module) {
|
282
|
|
- SX1276_SPIWrite(module, LR_RegOpMode, 0x09);
|
283
|
|
- module->status = STANDBY;
|
284
|
|
-}
|
285
|
|
-
|
286
|
|
-void SX1276_sleep(SX1276_t * module) {
|
287
|
|
- SX1276_SPIWrite(module, LR_RegOpMode, 0x08);
|
288
|
|
- module->status = SLEEP;
|
289
|
|
-}
|
290
|
|
-
|
291
|
|
-void SX1276_entryLoRa(SX1276_t * module) {
|
292
|
|
- SX1276_SPIWrite(module, LR_RegOpMode, 0x88);
|
293
|
|
-}
|
294
|
|
-
|
295
|
|
-void SX1276_clearLoRaIrq(SX1276_t * module) {
|
296
|
|
- SX1276_SPIWrite(module, LR_RegIrqFlags, 0xFF);
|
297
|
|
-}
|
298
|
|
-
|
299
|
|
-int SX1276_LoRaEntryRx(SX1276_t * module, uint8_t length, uint32_t timeout) {
|
300
|
|
- uint8_t addr;
|
301
|
|
-
|
302
|
|
- module->packetLength = length;
|
303
|
|
-
|
304
|
|
- SX1276_defaultConfig(module); //Setting base parameter
|
305
|
|
- SX1276_SPIWrite(module, REG_LR_PADAC, 0x84); //Normal and RX
|
306
|
|
- SX1276_SPIWrite(module, LR_RegHopPeriod, 0xFF); //No FHSS
|
307
|
|
- SX1276_SPIWrite(module, REG_LR_DIOMAPPING1, 0x01);//DIO=00,DIO1=00,DIO2=00, DIO3=01
|
308
|
|
- SX1276_SPIWrite(module, LR_RegIrqFlagsMask, 0x3F);//Open RxDone interrupt & Timeout
|
309
|
|
- SX1276_clearLoRaIrq(module);
|
310
|
|
- SX1276_SPIWrite(module, LR_RegPayloadLength, length);//Payload Length 21byte(this register must difine when the data long of one byte in SF is 6)
|
311
|
|
- addr = SX1276_SPIRead(module, LR_RegFifoRxBaseAddr); //Read RxBaseAddr
|
312
|
|
- SX1276_SPIWrite(module, LR_RegFifoAddrPtr, addr); //RxBaseAddr->FiFoAddrPtr
|
313
|
|
- SX1276_SPIWrite(module, LR_RegOpMode, 0x85); //Mode//Low Frequency Mode
|
314
|
|
- //SX1276_SPIWrite(module, LR_RegOpMode,0x05); //Continuous Rx Mode //High Frequency Mode
|
315
|
|
- module->readBytes = 0;
|
316
|
|
-
|
317
|
|
- while (1) {
|
318
|
|
- if ((SX1276_SPIRead(module, LR_RegModemStat) & 0x04) == 0x04) { //Rx-on going RegModemStat
|
319
|
|
- module->status = RX;
|
320
|
|
- return 1;
|
321
|
|
- }
|
322
|
|
- if (--timeout == 0) {
|
323
|
|
- SX1276_hw_Reset(module->hw);
|
324
|
|
- SX1276_defaultConfig(module);
|
325
|
|
- return 0;
|
326
|
|
- }
|
327
|
|
- SX1276_hw_DelayMs(1);
|
328
|
|
- }
|
329
|
|
-}
|
330
|
|
-
|
331
|
|
-uint8_t SX1276_LoRaRxPacket(SX1276_t * module) {
|
332
|
|
- unsigned char addr;
|
333
|
|
- unsigned char packet_size;
|
334
|
|
-
|
335
|
|
- if (SX1276_hw_GetDIO0(module->hw)) {
|
336
|
|
- memset(module->rxBuffer, 0x00, SX1276_MAX_PACKET);
|
337
|
|
-
|
338
|
|
- addr = SX1276_SPIRead(module, LR_RegFifoRxCurrentaddr); //last packet addr
|
339
|
|
- SX1276_SPIWrite(module, LR_RegFifoAddrPtr, addr); //RxBaseAddr -> FiFoAddrPtr
|
340
|
|
-
|
341
|
|
- if (module->LoRa_Rate == SX1276_LORA_SF_6) { //When SpreadFactor is six,will used Implicit Header mode(Excluding internal packet length)
|
342
|
|
- packet_size = module->packetLength;
|
343
|
|
- } else {
|
344
|
|
- packet_size = SX1276_SPIRead(module, LR_RegRxNbBytes); //Number for received bytes
|
345
|
|
- }
|
346
|
|
-
|
347
|
|
- SX1276_SPIBurstRead(module, 0x00, module->rxBuffer, packet_size);
|
348
|
|
- module->readBytes = packet_size;
|
349
|
|
- SX1276_clearLoRaIrq(module);
|
350
|
|
- }
|
351
|
|
- return module->readBytes;
|
352
|
|
-}
|
353
|
|
-
|
354
|
|
-
|
355
|
|
-
|
356
|
|
-
|
357
|
|
-int SX1276_LoRaEntryTx(SX1276_t * module, uint8_t length, uint32_t timeout) {
|
358
|
|
- uint8_t addr;
|
359
|
|
- uint8_t temp;
|
360
|
|
-
|
361
|
|
- module->packetLength = length;
|
362
|
|
-
|
363
|
|
- SX1276_defaultConfig(module); //setting base parameter
|
364
|
|
- SX1276_SPIWrite(module, REG_LR_PADAC, 0x87); //Tx for 20dBm
|
365
|
|
- SX1276_SPIWrite(module, LR_RegHopPeriod, 0x00); //RegHopPeriod NO FHSS
|
366
|
|
- SX1276_SPIWrite(module, REG_LR_DIOMAPPING1, 0x41); //DIO0=01, DIO1=00,DIO2=00, DIO3=01
|
367
|
|
- SX1276_clearLoRaIrq(module);
|
368
|
|
- SX1276_SPIWrite(module, LR_RegIrqFlagsMask, 0xF7); //Open TxDone interrupt
|
369
|
|
- SX1276_SPIWrite(module, LR_RegPayloadLength, length); //RegPayloadLength 21byte
|
370
|
|
- addr = SX1276_SPIRead(module, LR_RegFifoTxBaseAddr); //RegFiFoTxBaseAddr
|
371
|
|
- SX1276_SPIWrite(module, LR_RegFifoAddrPtr, addr); //RegFifoAddrPtr
|
372
|
|
-
|
373
|
|
- while (1) {
|
374
|
|
- temp = SX1276_SPIRead(module, LR_RegPayloadLength);
|
375
|
|
- if (temp == length) {
|
376
|
|
- module->status = TX;
|
377
|
|
- return 1;
|
378
|
|
- }
|
379
|
|
-
|
380
|
|
- if (--timeout == 0) {
|
381
|
|
- SX1276_hw_Reset(module->hw);
|
382
|
|
- SX1276_defaultConfig(module);
|
383
|
|
- return 0;
|
384
|
|
- }
|
385
|
|
- }
|
386
|
|
-}
|
387
|
|
-
|
388
|
|
-int SX1276_LoRaTxPacket(SX1276_t * module, uint8_t* txBuffer, uint8_t length,
|
389
|
|
- uint32_t timeout) {
|
390
|
|
- SX1276_SPIBurstWrite(module, 0x00, txBuffer, length);
|
391
|
|
- SX1276_SPIWrite(module, LR_RegOpMode, 0x8b); //Tx Mode
|
392
|
|
- while (1) {
|
393
|
|
- if (SX1276_hw_GetDIO0(module->hw)) { //if(Get_NIRQ()) //Packet send over
|
394
|
|
- SX1276_SPIRead(module, LR_RegIrqFlags);
|
395
|
|
- SX1276_clearLoRaIrq(module); //Clear irq
|
396
|
|
- SX1276_standby(module); //Entry Standby mode
|
397
|
|
- return 1;
|
398
|
|
- }
|
399
|
|
-
|
400
|
|
- if (--timeout == 0) {
|
401
|
|
- SX1276_hw_Reset(module->hw);
|
402
|
|
- SX1276_defaultConfig(module);
|
403
|
|
- return 0;
|
404
|
|
- }
|
405
|
|
- SX1276_hw_DelayMs(1);
|
406
|
|
- }
|
407
|
|
-}
|
408
|
|
-
|
409
|
|
-void SX1276_begin(SX1276_t * module, uint8_t frequency, uint8_t power,
|
410
|
|
- uint8_t LoRa_Rate, uint8_t LoRa_BW, uint8_t packetLength,uint8_t LoRa_Lna,uint8_t LoRa_PaBoost) {
|
411
|
|
- SX1276_hw_init(module->hw);
|
412
|
|
- module->frequency = frequency;
|
413
|
|
- module->power = power;
|
414
|
|
- module->LoRa_Rate = LoRa_Rate;
|
415
|
|
- module->LoRa_BW = LoRa_BW;
|
416
|
|
- module->packetLength = packetLength;
|
417
|
|
- module->LoRa_Lna = LoRa_Lna;
|
418
|
|
- module->LoRa_Pa_boost = LoRa_PaBoost;
|
419
|
|
- SX1276_defaultConfig(module);
|
420
|
|
-}
|
421
|
|
-
|
422
|
|
-int SX1276_transmit(SX1276_t * module, uint8_t* txBuf, uint8_t length,
|
423
|
|
- uint32_t timeout) {
|
424
|
|
- if (SX1276_LoRaEntryTx(module, length, timeout)) {
|
425
|
|
- return SX1276_LoRaTxPacket(module, txBuf, length, timeout);
|
426
|
|
- }
|
427
|
|
- return 0;
|
428
|
|
-}
|
429
|
|
-
|
430
|
|
-int SX1276_receive(SX1276_t * module, uint8_t length, uint32_t timeout) {
|
431
|
|
- return SX1276_LoRaEntryRx(module, length, timeout);
|
432
|
|
-}
|
433
|
|
-
|
434
|
|
-uint8_t SX1276_available(SX1276_t * module) {
|
435
|
|
- return SX1276_LoRaRxPacket(module);
|
436
|
|
-}
|
437
|
|
-
|
438
|
|
-uint8_t SX1276_read(SX1276_t * module, uint8_t* rxBuf, uint8_t length) {
|
439
|
|
- if (length != module->readBytes)
|
440
|
|
- length = module->readBytes;
|
441
|
|
- memcpy(rxBuf, module->rxBuffer, length);
|
442
|
|
- rxBuf[length] = '\0';
|
443
|
|
- module->readBytes = 0;
|
444
|
|
- return length;
|
445
|
|
-}
|
446
|
|
-
|
447
|
|
-uint8_t SX1276_RSSI_LoRa(SX1276_t * module) {
|
448
|
|
- uint32_t temp = 10;
|
449
|
|
- temp = SX1276_SPIRead(module, LR_RegRssiValue); //Read RegRssiValue, Rssi value
|
450
|
|
- temp = temp + 127 - 137; //127:Max RSSI, 137:RSSI offset
|
451
|
|
- return (uint8_t) temp;
|
452
|
|
-}
|
453
|
|
-
|
454
|
|
-uint8_t SX1276_RSSI(SX1276_t * module) {
|
455
|
|
- uint8_t temp = 0xff;
|
456
|
|
- temp = SX1276_SPIRead(module, 0x11);
|
457
|
|
- temp = 127 - (temp >> 1); //127:Max RSSI
|
458
|
|
- return temp;
|
459
|
|
-}
|
460
|
|
-
|
|
1
|
+/**
|
|
2
|
+ * Author Wojciech Domski <Wojciech.Domski@gmail.com>
|
|
3
|
+ * www: www.Domski.pl
|
|
4
|
+ *
|
|
5
|
+ * work based on DORJI.COM sample code and
|
|
6
|
+ * https://github.com/realspinner/SX1276_LoRa
|
|
7
|
+ */
|
|
8
|
+
|
|
9
|
+#include "SX1276.h"
|
|
10
|
+#include <string.h>
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+//////////////////////////////////
|
|
14
|
+// logic
|
|
15
|
+//////////////////////////////////
|
|
16
|
+
|
|
17
|
+__weak void SX1276_hw_init(SX1276_hw_t * hw) {
|
|
18
|
+ SX1276_hw_SetNSS(hw, 1);
|
|
19
|
+ HAL_GPIO_WritePin(hw->reset.port, hw->reset.pin, GPIO_PIN_SET);
|
|
20
|
+}
|
|
21
|
+
|
|
22
|
+__weak void SX1276_hw_SetNSS(SX1276_hw_t * hw, int value) {
|
|
23
|
+ HAL_GPIO_WritePin(hw->nss.port, hw->nss.pin,
|
|
24
|
+ (value == 1) ? GPIO_PIN_SET : GPIO_PIN_RESET);
|
|
25
|
+}
|
|
26
|
+
|
|
27
|
+__weak void SX1276_hw_Reset(SX1276_hw_t * hw) {
|
|
28
|
+ SX1276_hw_SetNSS(hw, 1);
|
|
29
|
+ HAL_GPIO_WritePin(hw->reset.port, hw->reset.pin, GPIO_PIN_RESET);
|
|
30
|
+
|
|
31
|
+ SX1276_hw_DelayMs(1);
|
|
32
|
+
|
|
33
|
+ HAL_GPIO_WritePin(hw->reset.port, hw->reset.pin, GPIO_PIN_SET);
|
|
34
|
+
|
|
35
|
+ SX1276_hw_DelayMs(100);
|
|
36
|
+}
|
|
37
|
+
|
|
38
|
+#if 0 // PYJ.2019.04.01_BEGIN --
|
|
39
|
+__weak void SX1276_hw_SPICommand(SX1276_hw_t * hw, uint8_t cmd) {
|
|
40
|
+ SX1276_hw_SetNSS(hw, 0);
|
|
41
|
+ HAL_SPI_Transmit(hw->spi, &cmd, 1, 1000);
|
|
42
|
+ while (HAL_SPI_GetState(hw->spi) != HAL_SPI_STATE_READY)
|
|
43
|
+ ;
|
|
44
|
+}
|
|
45
|
+#endif // PYJ.2019.04.01_END --
|
|
46
|
+void SX1276_hw_SPICommand(SX1276_hw_t * hw, uint8_t cmd) {
|
|
47
|
+ SX1276_hw_SetNSS(hw, 0);
|
|
48
|
+ BLUECELL_SPI_Transmit(cmd);
|
|
49
|
+}
|
|
50
|
+
|
|
51
|
+#if 0 // PYJ.2019.04.01_BEGIN --
|
|
52
|
+__weak uint8_t SX1276_hw_SPIReadByte(SX1276_hw_t * hw) {
|
|
53
|
+ uint8_t txByte = 0x00;
|
|
54
|
+ uint8_t rxByte = 0x00;
|
|
55
|
+
|
|
56
|
+ SX1276_hw_SetNSS(hw, 0);
|
|
57
|
+ HAL_SPI_TransmitReceive(hw->spi, &txByte, &rxByte, 1, 1000);
|
|
58
|
+ return rxByte;
|
|
59
|
+}
|
|
60
|
+#endif // PYJ.2019.04.01_END --
|
|
61
|
+uint8_t SX1276_hw_SPIReadByte(SX1276_hw_t * hw) {
|
|
62
|
+ //uint8_t txByte = 0x00;
|
|
63
|
+ uint8_t rxByte = 0x00;
|
|
64
|
+
|
|
65
|
+ SX1276_hw_SetNSS(hw, 0);
|
|
66
|
+// HAL_SPI_TransmitReceive(hw->spi, &txByte, &rxByte, 1, 1000);
|
|
67
|
+ rxByte = SpiRead();
|
|
68
|
+ return rxByte;
|
|
69
|
+}
|
|
70
|
+
|
|
71
|
+__weak void SX1276_hw_DelayMs(uint32_t msec) {
|
|
72
|
+ HAL_Delay(msec);
|
|
73
|
+}
|
|
74
|
+
|
|
75
|
+__weak int SX1276_hw_GetDIO0(SX1276_hw_t * hw) {
|
|
76
|
+ return (HAL_GPIO_ReadPin(hw->dio0.port, hw->dio0.pin) == GPIO_PIN_SET);
|
|
77
|
+}
|
|
78
|
+
|
|
79
|
+//////////////////////////////////
|
|
80
|
+// logic
|
|
81
|
+//////////////////////////////////
|
|
82
|
+
|
|
83
|
+uint8_t SX1276_SPIRead(SX1276_t * module, uint8_t addr) {
|
|
84
|
+ uint8_t tmp;
|
|
85
|
+ SX1276_hw_SPICommand(module->hw, addr);
|
|
86
|
+ tmp = SX1276_hw_SPIReadByte(module->hw);
|
|
87
|
+ SX1276_hw_SetNSS(module->hw, 1);
|
|
88
|
+ return tmp;
|
|
89
|
+}
|
|
90
|
+uint8_t SX1276_SPIIDRead(SX1276_t * module, uint8_t addr) {
|
|
91
|
+ uint8_t tmp;
|
|
92
|
+ SX1276_hw_SPICommand(module->hw, addr);
|
|
93
|
+ tmp = SX1276_hw_SPIReadByte(module->hw);
|
|
94
|
+ SX1276_hw_SetNSS(module->hw, 1);
|
|
95
|
+ return tmp;
|
|
96
|
+}
|
|
97
|
+
|
|
98
|
+void SX1276_SPIWrite(SX1276_t * module, uint8_t addr, uint8_t cmd) {
|
|
99
|
+ SX1276_hw_SetNSS(module->hw, 0);
|
|
100
|
+ SX1276_hw_SPICommand(module->hw, addr | 0x80);
|
|
101
|
+ SX1276_hw_SPICommand(module->hw, cmd);
|
|
102
|
+// SPIGPIOTxRx(addr | 0x80,cmd);
|
|
103
|
+
|
|
104
|
+ SX1276_hw_SetNSS(module->hw, 1);
|
|
105
|
+}
|
|
106
|
+
|
|
107
|
+void SX1276_SPIBurstRead(SX1276_t * module, uint8_t addr, uint8_t* rxBuf,
|
|
108
|
+ uint8_t length) {
|
|
109
|
+ uint8_t i;
|
|
110
|
+ if (length <= 1) {
|
|
111
|
+ return;
|
|
112
|
+ } else {
|
|
113
|
+ SX1276_hw_SetNSS(module->hw, 0);
|
|
114
|
+ SX1276_hw_SPICommand(module->hw, addr);
|
|
115
|
+ //printf("Test Data:");
|
|
116
|
+ for (i = 0; i < length; i++) {
|
|
117
|
+ rxBuf[i] = SX1276_hw_SPIReadByte(module->hw);
|
|
118
|
+ //printf("%02x ",rxBuf[i]);
|
|
119
|
+ }
|
|
120
|
+ //printf("\n");
|
|
121
|
+ SX1276_hw_SetNSS(module->hw, 1);
|
|
122
|
+ }
|
|
123
|
+}
|
|
124
|
+
|
|
125
|
+void SX1276_SPIBurstWrite(SX1276_t * module, uint8_t addr, uint8_t* txBuf,
|
|
126
|
+ uint8_t length) {
|
|
127
|
+ uint8_t i;
|
|
128
|
+ if (length <= 1) {
|
|
129
|
+ return;
|
|
130
|
+ } else {
|
|
131
|
+ SX1276_hw_SetNSS(module->hw, 0);
|
|
132
|
+ SX1276_hw_SPICommand(module->hw, addr | 0x80);
|
|
133
|
+// printf("Test Data:");
|
|
134
|
+ for (i = 0; i < length; i++) {
|
|
135
|
+ SX1276_hw_SPICommand(module->hw, txBuf[i]);
|
|
136
|
+// printf("%02x ",txBuf[i]);
|
|
137
|
+ }
|
|
138
|
+// printf("\n");
|
|
139
|
+ SX1276_hw_SetNSS(module->hw, 1);
|
|
140
|
+ }
|
|
141
|
+}
|
|
142
|
+ /*
|
|
143
|
+ RegPaConfig - 0x09
|
|
144
|
+ PaSelect Mode Power Range Pout Formula
|
|
145
|
+ 0 PA_HF or PA_LF on RFO_HF or RFO_LF -4 to +15dBm Pout=Pmax-(15-OutputPower), Pmax=10.8+0.6*MaxPower [dBm]
|
|
146
|
+ 1 PA_HP on PA_BOOST, any frequency +2 to +17dBm Pout=17-(15-OutputPower) [dBm]
|
|
147
|
+ PA_LF option doesn't seem to transmit anything. Not connected on these boards, perhaps?
|
|
148
|
+ Use only PA_BOOST mode.
|
|
149
|
+
|
|
150
|
+ PA_LF Pmax
|
|
151
|
+ Pout 0 1 2 3 4 5 6 7
|
|
152
|
+ 0 -4.2 -3.6 -3 -2.4 -1.8 -1.2 -0.6 0
|
|
153
|
+ 1 -3.2 -2.6 -2 -1.4 -0.8 -0.2 0.4 1
|
|
154
|
+ 2 -2.2 -1.6 -1 -0.4 0.2 0.8 1.4 2
|
|
155
|
+ 3 -1.2 -0.6 0 0.6 1.2 1.8 2.4 3
|
|
156
|
+ 4 -0.2 0.4 1 1.6 2.2 2.8 3.4 4
|
|
157
|
+ 5 0.8 1.4 2 2.6 3.2 3.8 4.4 5
|
|
158
|
+ 6 1.8 2.4 3 3.6 4.2 4.8 5.4 6
|
|
159
|
+ 7 2.8 3.4 4 4.6 5.2 5.8 6.4 7
|
|
160
|
+ 8 3.8 4.4 5 5.6 6.2 6.8 7.4 8
|
|
161
|
+ 9 4.8 5.4 6 6.6 7.2 7.8 8.4 9
|
|
162
|
+ 10 5.8 6.4 7 7.6 8.2 8.8 9.4 10
|
|
163
|
+ 11 6.8 7.4 8 8.6 9.2 9.8 10.4 11
|
|
164
|
+ 12 7.8 8.4 9 9.6 10.2 10.8 11.4 12
|
|
165
|
+ 13 8.8 9.4 10 10.6 11.2 11.8 12.4 13
|
|
166
|
+ 14 9.8 10.4 11 11.6 12.2 12.8 13.4 14
|
|
167
|
+ 15 10.8 11.4 12 12.6 13.2 13.8 14.4 15 [dBm]
|
|
168
|
+
|
|
169
|
+ PA_BOOST
|
|
170
|
+ Pout OutputPower
|
|
171
|
+ 0 2 dBm
|
|
172
|
+ 1 3 dBm
|
|
173
|
+ 2 4 dBm
|
|
174
|
+ 3 5 dBm
|
|
175
|
+ 4 6 dBm
|
|
176
|
+ 5 7 dBm
|
|
177
|
+ 6 8 dBm
|
|
178
|
+ 7 9 dBm
|
|
179
|
+ 8 10 dBm
|
|
180
|
+ 9 11 dBm
|
|
181
|
+ 10 12 dBm
|
|
182
|
+ 11 13 dBm
|
|
183
|
+ 12 14 dBm
|
|
184
|
+ 13 15 dBm
|
|
185
|
+ 14 16 dBm
|
|
186
|
+ 15 17 dBm
|
|
187
|
+
|
|
188
|
+ PaSelect
|
|
189
|
+ 0 RFO pin. Maximum power of +14 dBm
|
|
190
|
+ 1 PA_BOOST pin. Maximum power of +20 dBm
|
|
191
|
+ MaxPower
|
|
192
|
+ Select max output power: Pmax=10.8+0.6*MaxPower [dBm]
|
|
193
|
+ OutputPower
|
|
194
|
+ Pout=Pmax-(15-OutputPower) if PaSelect = 0 (RFO pins)
|
|
195
|
+ Pout=17-(15-OutputPower) if PaSelect = 1 (PA_BOOST pin)
|
|
196
|
+ PA_HF and PA_LF are high efficiency amplifiers capable of yielding RF power programmable in 1 dB steps
|
|
197
|
+ from -4 to +14dBm directly into a 50 ohm load with low current consumption. PA_LF covers the lower bands
|
|
198
|
+ (up to 525 MHz), whilst PA_HF will cover the upper bands (from 860 MHz).
|
|
199
|
+ PA_HP (High Power), connected to the PA_BOOST pin, covers all frequency bands that the chip addresses.
|
|
200
|
+ It permits continuous operation at up to +17 dBm and duty cycled operation at up to +20dBm.
|
|
201
|
+ RegOcp - 0x0B
|
|
202
|
+ OcpTrim IMAX Imax Formula
|
|
203
|
+ 0 to 15 45 to 120 mA 45 + 5*OcpTrim [mA]
|
|
204
|
+ 16 to 27 130 to 240 mA -30 + 10*OcpTrim [mA]
|
|
205
|
+ 27+ 240 mA 240 mA
|
|
206
|
+
|
|
207
|
+ The power amplifiers of RFM95/96/97/98(W) are protected against current over supply in adverse RF load
|
|
208
|
+ conditions by the over current protection block. The current limiter value is controlled by the OcpTrim
|
|
209
|
+ bits in RegOcp.
|
|
210
|
+ Imax sets a limit on the current drain of the Power Amplifier only, hence the maximum current drain of the
|
|
211
|
+ RFM96/77/78 is equal to Imax + IFS.
|
|
212
|
+ Default 100mA changed to 240mA.
|
|
213
|
+*/
|
|
214
|
+
|
|
215
|
+void SX1276_set_power(SX1276_t * module)
|
|
216
|
+{
|
|
217
|
+// SX1276_SPIWrite(LR_RegPaConfig, (PaSelect << 7) | ((MaxPower & 0x07) << 4) | (OutputPower & 0x0F));
|
|
218
|
+ SX1276_SPIWrite(module,LR_RegPaConfig, (module->LoRa_Pa_boost << 7) | ((0x07) << 4) | (module->power & 0x0F));
|
|
219
|
+ //RFO -> 0 ~ 15 / PA_BOOST -> 2 ~ 17
|
|
220
|
+}
|
|
221
|
+void SX1276_defaultConfig(SX1276_t * module) {
|
|
222
|
+ SX1276_config(module, module->frequency, module->power, module->LoRa_Rate,
|
|
223
|
+ module->LoRa_BW,module->LoRa_Lna,module->LoRa_Pa_boost);
|
|
224
|
+}
|
|
225
|
+
|
|
226
|
+void SX1276_config(SX1276_t * module, uint8_t frequency, uint8_t power,
|
|
227
|
+ uint8_t LoRa_Rate, uint8_t LoRa_BW,uint8_t LoRa_Lna,uint8_t LoRa_PaBoost) {
|
|
228
|
+ SX1276_sleep(module); //Change modem mode Must in Sleep mode
|
|
229
|
+ SX1276_hw_DelayMs(15);
|
|
230
|
+
|
|
231
|
+ SX1276_entryLoRa(module);
|
|
232
|
+ //SX1276_SPIWrite(module, 0x5904); //?? Change digital regulator form 1.6V to 1.47V: see errata note
|
|
233
|
+
|
|
234
|
+ SX1276_SPIBurstWrite(module, LR_RegFrMsb,
|
|
235
|
+ (uint8_t*) SX1276_Frequency[frequency], 3); //setting frequency parameter
|
|
236
|
+
|
|
237
|
+ //setting base parameter
|
|
238
|
+ SX1276_SPIWrite(module, LR_RegPaConfig, SX1276_Power[power]); //Setting output power parameter
|
|
239
|
+
|
|
240
|
+ SX1276_SPIWrite(module, LR_RegOcp, 0x0B); //RegOcp,Close Ocp
|
|
241
|
+// SX1276_SPIWrite(module, LR_RegLna, 0x20); //RegLNA,High & LNA Enable
|
|
242
|
+ SX1276_set_power(module);
|
|
243
|
+
|
|
244
|
+ SX1276_SPIWrite(module, LR_RegLna, LoRa_Lna); //RegLNA,High & LNA Enable
|
|
245
|
+
|
|
246
|
+ if (SX1276_SpreadFactor[LoRa_Rate] == 6) { //SFactor=6
|
|
247
|
+ uint8_t tmp;
|
|
248
|
+ SX1276_SPIWrite(module,
|
|
249
|
+ LR_RegModemConfig1,
|
|
250
|
+ ((SX1276_LoRaBandwidth[LoRa_BW] << 4) + (SX1276_CR << 1) + 0x01)); //Implicit Enable CRC Enable(0x02) & Error Coding rate 4/5(0x01), 4/6(0x02), 4/7(0x03), 4/8(0x04)
|
|
251
|
+
|
|
252
|
+ SX1276_SPIWrite(module,LR_RegModemConfig2,((SX1276_SpreadFactor[LoRa_Rate] << 4) + (SX1276_CRC << 2) + 0x03));
|
|
253
|
+
|
|
254
|
+ tmp = SX1276_SPIRead(module, 0x31);
|
|
255
|
+ tmp &= 0xF8;
|
|
256
|
+ tmp |= 0x05;
|
|
257
|
+ SX1276_SPIWrite(module, 0x31, tmp);
|
|
258
|
+ SX1276_SPIWrite(module, 0x37, 0x0C);
|
|
259
|
+ } else {
|
|
260
|
+ SX1276_SPIWrite(module,
|
|
261
|
+ LR_RegModemConfig1,
|
|
262
|
+ ((SX1276_LoRaBandwidth[LoRa_BW] << 4) + (SX1276_CR << 1) + 0x00)); //Explicit Enable CRC Enable(0x02) & Error Coding rate 4/5(0x01), 4/6(0x02), 4/7(0x03), 4/8(0x04)
|
|
263
|
+
|
|
264
|
+ SX1276_SPIWrite(module,
|
|
265
|
+ LR_RegModemConfig2,
|
|
266
|
+ ((SX1276_SpreadFactor[LoRa_Rate] << 4) + (SX1276_CRC << 2)
|
|
267
|
+ + 0x03)); //SFactor & LNA gain set by the internal AGC loop
|
|
268
|
+ }
|
|
269
|
+
|
|
270
|
+ SX1276_SPIWrite(module, LR_RegSymbTimeoutLsb, 0xFF); //RegSymbTimeoutLsb Timeout = 0x3FF(Max)
|
|
271
|
+ SX1276_SPIWrite(module, LR_RegPreambleMsb, 0x00); //RegPreambleMsb
|
|
272
|
+ SX1276_SPIWrite(module, LR_RegPreambleLsb, 12); //RegPreambleLsb 8+4=12byte Preamble
|
|
273
|
+ SX1276_SPIWrite(module, REG_LR_DIOMAPPING2, 0x01); //RegDioMapping2 DIO5=00, DIO4=01
|
|
274
|
+ module->readBytes = 0;
|
|
275
|
+ SX1276_standby(module); //Entry standby mode
|
|
276
|
+}
|
|
277
|
+
|
|
278
|
+void SX1276_standby(SX1276_t * module) {
|
|
279
|
+// SX1276_SPIWrite(module, LR_RegOpMode, 0x09);
|
|
280
|
+ SX1276_SPIWrite(module, LR_RegOpMode, 0x01);
|
|
281
|
+
|
|
282
|
+ module->status = STANDBY;
|
|
283
|
+}
|
|
284
|
+
|
|
285
|
+void SX1276_sleep(SX1276_t * module) {
|
|
286
|
+// SX1276_SPIWrite(module, LR_RegOpMode, 0x08);
|
|
287
|
+ SX1276_SPIWrite(module, LR_RegOpMode, 0x80);
|
|
288
|
+
|
|
289
|
+ module->status = SLEEP;
|
|
290
|
+}
|
|
291
|
+
|
|
292
|
+void SX1276_entryLoRa(SX1276_t * module) {
|
|
293
|
+// SX1276_SPIWrite(module, LR_RegOpMode, 0x88);
|
|
294
|
+ SX1276_SPIWrite(module, LR_RegOpMode, 0x80);
|
|
295
|
+}
|
|
296
|
+
|
|
297
|
+void SX1276_clearLoRaIrq(SX1276_t * module) {
|
|
298
|
+ SX1276_SPIWrite(module, LR_RegIrqFlags, 0xFF);
|
|
299
|
+}
|
|
300
|
+
|
|
301
|
+int SX1276_LoRaEntryRx(SX1276_t * module, uint8_t length, uint32_t timeout) {
|
|
302
|
+ uint8_t addr;
|
|
303
|
+
|
|
304
|
+ module->packetLength = length;
|
|
305
|
+
|
|
306
|
+ SX1276_defaultConfig(module); //Setting base parameter
|
|
307
|
+ SX1276_SPIWrite(module, REG_LR_PADAC, 0x84); //Normal and RX
|
|
308
|
+ SX1276_SPIWrite(module, LR_RegHopPeriod, 0xFF); //No FHSS
|
|
309
|
+ SX1276_SPIWrite(module, REG_LR_DIOMAPPING1, 0x01);//DIO=00,DIO1=00,DIO2=00, DIO3=01
|
|
310
|
+ SX1276_SPIWrite(module, LR_RegIrqFlagsMask, 0x3F);//Open RxDone interrupt & Timeout
|
|
311
|
+ SX1276_clearLoRaIrq(module);
|
|
312
|
+ SX1276_SPIWrite(module, LR_RegPayloadLength, length);//Payload Length 21byte(this register must difine when the data long of one byte in SF is 6)
|
|
313
|
+ addr = SX1276_SPIRead(module, LR_RegFifoRxBaseAddr); //Read RxBaseAddr
|
|
314
|
+ SX1276_SPIWrite(module, LR_RegFifoAddrPtr, addr); //RxBaseAddr->FiFoAddrPtr
|
|
315
|
+ SX1276_SPIWrite(module, LR_RegOpMode, 0x85); //Mode//Low Frequency Mode
|
|
316
|
+ //SX1276_SPIWrite(module, LR_RegOpMode,0x05); //Continuous Rx Mode //High Frequency Mode
|
|
317
|
+ module->readBytes = 0;
|
|
318
|
+
|
|
319
|
+ while (1) {
|
|
320
|
+ if ((SX1276_SPIRead(module, LR_RegModemStat) & 0x04) == 0x04) { //Rx-on going RegModemStat
|
|
321
|
+ module->status = RX;
|
|
322
|
+ return 1;
|
|
323
|
+ }
|
|
324
|
+ if (--timeout == 0) {
|
|
325
|
+ printf(
"Function : %s Line : %d \n",__func__,__LINE__);
|
|
326
|
+ NVIC_SystemReset();
|
|
327
|
+ SX1276_hw_Reset(module->hw);
|
|
328
|
+ SX1276_defaultConfig(module);
|
|
329
|
+ return 0;
|
|
330
|
+ }
|
|
331
|
+ SX1276_hw_DelayMs(1);
|
|
332
|
+ }
|
|
333
|
+}
|
|
334
|
+
|
|
335
|
+uint8_t SX1276_LoRaRxPacket(SX1276_t * module) {
|
|
336
|
+ unsigned char addr;
|
|
337
|
+ unsigned char packet_size;
|
|
338
|
+
|
|
339
|
+ if (SX1276_hw_GetDIO0(module->hw)) {
|
|
340
|
+ memset(module->rxBuffer, 0x00, SX1276_MAX_PACKET);
|
|
341
|
+
|
|
342
|
+ addr = SX1276_SPIRead(module, LR_RegFifoRxCurrentaddr); //last packet addr
|
|
343
|
+ SX1276_SPIWrite(module, LR_RegFifoAddrPtr, addr); //RxBaseAddr -> FiFoAddrPtr
|
|
344
|
+
|
|
345
|
+ if (module->LoRa_Rate == SX1276_LORA_SF_6) { //When SpreadFactor is six,will used Implicit Header mode(Excluding internal packet length)
|
|
346
|
+ packet_size = module->packetLength;
|
|
347
|
+ } else {
|
|
348
|
+ packet_size = SX1276_SPIRead(module, LR_RegRxNbBytes); //Number for received bytes
|
|
349
|
+ }
|
|
350
|
+
|
|
351
|
+ SX1276_SPIBurstRead(module, 0x00, module->rxBuffer, packet_size);
|
|
352
|
+ module->readBytes = packet_size;
|
|
353
|
+ SX1276_clearLoRaIrq(module);
|
|
354
|
+ }
|
|
355
|
+ return module->readBytes;
|
|
356
|
+}
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+int SX1276_LoRaEntryTx(SX1276_t * module, uint8_t length, uint32_t timeout) {
|
|
362
|
+ uint8_t addr;
|
|
363
|
+ uint8_t temp;
|
|
364
|
+
|
|
365
|
+ module->packetLength = length;
|
|
366
|
+
|
|
367
|
+ SX1276_defaultConfig(module); //setting base parameter
|
|
368
|
+ SX1276_SPIWrite(module, REG_LR_PADAC, 0x87); //Tx for 20dBm
|
|
369
|
+ SX1276_SPIWrite(module, LR_RegHopPeriod, 0x00); //RegHopPeriod NO FHSS
|
|
370
|
+ SX1276_SPIWrite(module, REG_LR_DIOMAPPING1, 0x41); //DIO0=01, DIO1=00,DIO2=00, DIO3=01
|
|
371
|
+ SX1276_clearLoRaIrq(module);
|
|
372
|
+ SX1276_SPIWrite(module, LR_RegIrqFlagsMask, 0xF7); //Open TxDone interrupt
|
|
373
|
+ SX1276_SPIWrite(module, LR_RegPayloadLength, length); //RegPayloadLength 21byte
|
|
374
|
+ addr = SX1276_SPIRead(module, LR_RegFifoTxBaseAddr); //RegFiFoTxBaseAddr
|
|
375
|
+ SX1276_SPIWrite(module, LR_RegFifoAddrPtr, addr); //RegFifoAddrPtr
|
|
376
|
+
|
|
377
|
+ while (1) {
|
|
378
|
+ temp = SX1276_SPIRead(module, LR_RegPayloadLength);
|
|
379
|
+ if (temp == length) {
|
|
380
|
+ module->status = TX;
|
|
381
|
+ return 1;
|
|
382
|
+ }
|
|
383
|
+
|
|
384
|
+ if (--timeout == 0) {
|
|
385
|
+ printf(
"Function : %s Line : %d \n",__func__,__LINE__);
|
|
386
|
+ NVIC_SystemReset();
|
|
387
|
+ SX1276_hw_Reset(module->hw);
|
|
388
|
+ SX1276_defaultConfig(module);
|
|
389
|
+ return 0;
|
|
390
|
+ }
|
|
391
|
+ }
|
|
392
|
+}
|
|
393
|
+
|
|
394
|
+int SX1276_LoRaTxPacket(SX1276_t * module, uint8_t* txBuffer, uint8_t length,
|
|
395
|
+ uint32_t timeout) {
|
|
396
|
+ SX1276_SPIBurstWrite(module, 0x00, txBuffer, length);
|
|
397
|
+// SX1276_SPIWrite(module, LR_RegOpMode, 0x8b); //Tx Mode
|
|
398
|
+ SX1276_SPIWrite(module, LR_RegOpMode, 0x83); //Tx Mode
|
|
399
|
+
|
|
400
|
+ while (1) {
|
|
401
|
+ if (SX1276_hw_GetDIO0(module->hw)) { //if(Get_NIRQ()) //Packet send over
|
|
402
|
+ SX1276_SPIRead(module, LR_RegIrqFlags);
|
|
403
|
+ SX1276_clearLoRaIrq(module); //Clear irq
|
|
404
|
+ SX1276_standby(module); //Entry Standby mode
|
|
405
|
+ return 1;
|
|
406
|
+ }
|
|
407
|
+
|
|
408
|
+ if (--timeout == 0) {
|
|
409
|
+ printf(
"Function : %s Line : %d \n",__func__,__LINE__);
|
|
410
|
+ NVIC_SystemReset();
|
|
411
|
+ SX1276_hw_Reset(module->hw);
|
|
412
|
+ SX1276_defaultConfig(module);
|
|
413
|
+ return 0;
|
|
414
|
+ }
|
|
415
|
+ SX1276_hw_DelayMs(1);
|
|
416
|
+ }
|
|
417
|
+}
|
|
418
|
+
|
|
419
|
+void SX1276_begin(SX1276_t * module, uint8_t frequency, uint8_t power,
|
|
420
|
+ uint8_t LoRa_Rate, uint8_t LoRa_BW, uint8_t packetLength,uint8_t LoRa_Lna,uint8_t LoRa_PaBoost) {
|
|
421
|
+ SX1276_hw_init(module->hw);
|
|
422
|
+ module->frequency = frequency;
|
|
423
|
+ module->power = power;
|
|
424
|
+ module->LoRa_Rate = LoRa_Rate;
|
|
425
|
+ module->LoRa_BW = LoRa_BW;
|
|
426
|
+ module->packetLength = packetLength;
|
|
427
|
+ module->LoRa_Lna = LoRa_Lna;
|
|
428
|
+ module->LoRa_Pa_boost = LoRa_PaBoost;
|
|
429
|
+ SX1276_defaultConfig(module);
|
|
430
|
+}
|
|
431
|
+
|
|
432
|
+int SX1276_transmit(SX1276_t * module, uint8_t* txBuf, uint8_t length,
|
|
433
|
+ uint32_t timeout) {
|
|
434
|
+ if (SX1276_LoRaEntryTx(module, length, timeout)) {
|
|
435
|
+ return SX1276_LoRaTxPacket(module, txBuf, length, timeout);
|
|
436
|
+ }
|
|
437
|
+ return 0;
|
|
438
|
+}
|
|
439
|
+
|
|
440
|
+int SX1276_receive(SX1276_t * module, uint8_t length, uint32_t timeout) {
|
|
441
|
+ return SX1276_LoRaEntryRx(module, length, timeout);
|
|
442
|
+}
|
|
443
|
+
|
|
444
|
+uint8_t SX1276_available(SX1276_t * module) {
|
|
445
|
+ return SX1276_LoRaRxPacket(module);
|
|
446
|
+}
|
|
447
|
+
|
|
448
|
+uint8_t SX1276_read(SX1276_t * module, uint8_t* rxBuf, uint8_t length) {
|
|
449
|
+ if (length != module->readBytes)
|
|
450
|
+ length = module->readBytes;
|
|
451
|
+ memcpy(rxBuf, module->rxBuffer, length);
|
|
452
|
+ rxBuf[length] = '\0';
|
|
453
|
+ module->readBytes = 0;
|
|
454
|
+ return length;
|
|
455
|
+}
|
|
456
|
+
|
|
457
|
+uint8_t SX1276_RSSI_LoRa(SX1276_t * module) {
|
|
458
|
+ uint32_t temp = 10;
|
|
459
|
+ temp = SX1276_SPIRead(module, LR_RegRssiValue); //Read RegRssiValue, Rssi value
|
|
460
|
+ temp = temp + 127 - 137; //127:Max RSSI, 137:RSSI offset
|
|
461
|
+ return (uint8_t) temp;
|
|
462
|
+}
|
|
463
|
+
|
|
464
|
+uint8_t SX1276_RSSI(SX1276_t * module) {
|
|
465
|
+ uint8_t temp = 0xff;
|
|
466
|
+ temp = SX1276_SPIRead(module, 0x11);
|
|
467
|
+ temp = 127 - (temp >> 1); //127:Max RSSI
|
|
468
|
+ return temp;
|
|
469
|
+}
|
|
470
|
+
|