-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
sx1278.c
304 lines (261 loc) · 6.01 KB
/
sx1278.c
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
/*
* THE FOLLOWING FIRMWARE IS PROVIDED: (1) "AS IS" WITH NO WARRANTY; AND
* (2)TO ENABLE ACCESS TO CODING INFORMATION TO GUIDE AND FACILITATE CUSTOMER.
* CONSEQUENTLY, SEMTECH SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
* CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
* OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
* CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* Copyright (C) SEMTECH S.A.
*/
/*!
* \file sx1278.c
* \brief SX1278 RF chip driver
*
* \version 2.0.0
* \date May 6 2013
* \author Gregory Cristian
*
* Last modified by Marc0 Xu on Jun 07 2018
*/
#include "platform.h"
#include "radio.h"
#if defined( USE_SX1278_RADIO )
#include "sx1278.h"
#include "sx1278-Hal.h"
#include "sx1278-Fsk.h"
#include "sx1278-LoRa.h"
/*!
* SX1278 registers variable
*/
uint8_t SX1278Regs[0x70];
static bool LoRaOn = false;
static bool LoRaOnState = false;
void SX1278Init( void )
{
// Initialize FSK and LoRa registers structure
SX1278 = ( tSX1278* )SX1278Regs;
SX1278LR = ( tSX1278LR* )SX1278Regs;
SX1278InitIo( );
SX1278Reset( );
// REMARK: After radio reset the default modem is FSK
#if ( LORA == 0 )
LoRaOn = false;
SX1278SetLoRaOn( LoRaOn );
// Initialize FSK modem
SX1278FskInit( );
#else
LoRaOn = true;
SX1278SetLoRaOn( LoRaOn );
// Initialize LoRa modem
SX1278LoRaInit( );
#endif
}
void SX1278Reset( void )
{
//SX1278SetReset( RADIO_RESET_ON );
// Wait 1ms
//uint32_t startTick = GET_TICK_COUNT( );
//while( ( GET_TICK_COUNT( ) - startTick ) < TICK_RATE_MS( 1 ) );
//SX1278SetReset( RADIO_RESET_OFF );
// Wait 6ms
//startTick = GET_TICK_COUNT( );
//while( ( GET_TICK_COUNT( ) - startTick ) < TICK_RATE_MS( 6 ) );
}
void SX1278SetLoRaOn( bool enable )
{
if( LoRaOnState == enable )
{
return;
}
LoRaOnState = enable;
LoRaOn = enable;
if( LoRaOn == true )
{
SX1278LoRaSetOpMode( RFLR_OPMODE_SLEEP );
SX1278LR->RegOpMode = ( SX1278LR->RegOpMode & RFLR_OPMODE_LONGRANGEMODE_MASK ) | RFLR_OPMODE_LONGRANGEMODE_ON;
SX1278Write( REG_LR_OPMODE, SX1278LR->RegOpMode );
SX1278LoRaSetOpMode( RFLR_OPMODE_STANDBY );
// RxDone RxTimeout FhssChangeChannel CadDone
SX1278LR->RegDioMapping1 = RFLR_DIOMAPPING1_DIO0_00 | RFLR_DIOMAPPING1_DIO1_00 | RFLR_DIOMAPPING1_DIO2_00 | RFLR_DIOMAPPING1_DIO3_00;
// CadDetected ModeReady
SX1278LR->RegDioMapping2 = RFLR_DIOMAPPING2_DIO4_00 | RFLR_DIOMAPPING2_DIO5_00;
SX1278WriteBuffer( REG_LR_DIOMAPPING1, &SX1278LR->RegDioMapping1, 2 );
SX1278ReadBuffer( REG_LR_OPMODE, SX1278Regs + 1, 0x70 - 1 );
}
else
{
SX1278LoRaSetOpMode( RFLR_OPMODE_SLEEP );
SX1278LR->RegOpMode = ( SX1278LR->RegOpMode & RFLR_OPMODE_LONGRANGEMODE_MASK ) | RFLR_OPMODE_LONGRANGEMODE_OFF;
SX1278Write( REG_LR_OPMODE, SX1278LR->RegOpMode );
SX1278LoRaSetOpMode( RFLR_OPMODE_STANDBY );
SX1278ReadBuffer( REG_OPMODE, SX1278Regs + 1, 0x70 - 1 );
}
}
bool SX1278GetLoRaOn( void )
{
return LoRaOn;
}
void SX1278SetOpMode( uint8_t opMode )
{
if( LoRaOn == false )
{
SX1278FskSetOpMode( opMode );
}
else
{
SX1278LoRaSetOpMode( opMode );
}
}
uint8_t SX1278GetOpMode( void )
{
if( LoRaOn == false )
{
return SX1278FskGetOpMode( );
}
else
{
return SX1278LoRaGetOpMode( );
}
}
double SX1278ReadRssi( void )
{
if( LoRaOn == false )
{
return SX1278FskReadRssi( );
}
else
{
return SX1278LoRaReadRssi( );
}
}
uint8_t SX1278ReadRxGain( void )
{
if( LoRaOn == false )
{
return SX1278FskReadRxGain( );
}
else
{
return SX1278LoRaReadRxGain( );
}
}
uint8_t SX1278GetPacketRxGain( void )
{
if( LoRaOn == false )
{
return SX1278FskGetPacketRxGain( );
}
else
{
return SX1278LoRaGetPacketRxGain( );
}
}
int8_t SX1278GetPacketSnr( void )
{
if( LoRaOn == false )
{
while( 1 )
{
// Useless in FSK mode
// Block program here
}
}
else
{
return SX1278LoRaGetPacketSnr( );
}
}
double SX1278GetPacketRssi( void )
{
if( LoRaOn == false )
{
return SX1278FskGetPacketRssi( );
}
else
{
return SX1278LoRaGetPacketRssi( );
}
}
uint32_t SX1278GetPacketAfc( void )
{
if( LoRaOn == false )
{
return SX1278FskGetPacketAfc( );
}
else
{
while( 1 )
{
// Useless in LoRa mode
// Block program here
}
}
}
void SX1278StartRx( void )
{
if( LoRaOn == false )
{
SX1278FskSetRFState( RF_STATE_RX_INIT );
}
else
{
SX1278LoRaSetRFState( RFLR_STATE_RX_INIT );
}
}
void SX1278GetRxPacket( void *buffer, uint16_t *size )
{
if( LoRaOn == false )
{
SX1278FskGetRxPacket( buffer, size );
}
else
{
SX1278LoRaGetRxPacket( buffer, size );
}
}
void SX1278SetTxPacket( const void *buffer, uint16_t size )
{
if( LoRaOn == false )
{
SX1278FskSetTxPacket( buffer, size );
}
else
{
SX1278LoRaSetTxPacket( buffer, size );
}
}
uint8_t SX1278GetRFState( void )
{
if( LoRaOn == false )
{
return SX1278FskGetRFState( );
}
else
{
return SX1278LoRaGetRFState( );
}
}
void SX1278SetRFState( uint8_t state )
{
if( LoRaOn == false )
{
SX1278FskSetRFState( state );
}
else
{
SX1278LoRaSetRFState( state );
}
}
uint32_t SX1278Process( void )
{
if( LoRaOn == false )
{
return SX1278FskProcess( );
}
else
{
return SX1278LoRaProcess( );
}
}
#endif // USE_SX1278_RADIO