-
Notifications
You must be signed in to change notification settings - Fork 0
/
sx126x_long_pkt.c
319 lines (265 loc) · 10.6 KB
/
sx126x_long_pkt.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*!
* \file sx126x_long_pkt.c
*
* \brief sx126x long packet library
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
* ______ _
* / _____) _ | |
* ( (____ _____ ____ _| |_ _____ ____| |__
* \____ \| ___ | (_ _) ___ |/ ___) _ \
* _____) ) ____| | | || |_| ____( (___| | | |
* (______/|_____)_|_|_| \__)_____)\____)_| |_|
* (C)2019-2019 Semtech
*
* \endcode
*
* \authors Semtech WSP Applications Team
*/
#include "sx126x_long_pkt.h"
#include "sx126x_regs.h"
/// @cond
#define SX126X_REG_RX_ADDR_POINTER 0x0803
#define SX126X_REG_RXTX_PAYLOAD_LEN 0x06BB
/// @endcond
static sx126x_status_t sx126x_long_pkt_check_gfsk_pkt_params( const sx126x_pkt_params_gfsk_t* params );
sx126x_status_t sx126x_long_pkt_rx_set_gfsk_pkt_params( const void* context, const sx126x_pkt_params_gfsk_t* params )
{
sx126x_status_t status = sx126x_long_pkt_check_gfsk_pkt_params( params );
if( status != SX126X_STATUS_OK )
{
return status;
}
sx126x_pkt_params_gfsk_t pkt_params = *params;
// Tell the receiver that it should receive as many packets as possible
pkt_params.pld_len_in_bytes = SX126X_SHORT_PACKET_MAX;
return sx126x_set_gfsk_pkt_params( context, &pkt_params );
}
sx126x_status_t sx126x_long_pkt_set_rx( const void* context, struct sx126x_long_pkt_rx_state* state, uint32_t timeout )
{
uint8_t tmp = SX126X_SHORT_PACKET_MAX;
state->index = 0;
sx126x_status_t status = sx126x_write_register( context, SX126X_REG_RXTX_PAYLOAD_LEN, &tmp, 1 );
if( status != SX126X_STATUS_OK ) return status;
return sx126x_set_rx( context, timeout );
}
sx126x_status_t sx126x_long_pkt_set_rx_with_timeout_in_rtc_step( const void* context, struct sx126x_long_pkt_rx_state* state, uint32_t timeout )
{
uint8_t tmp = SX126X_SHORT_PACKET_MAX;
state->index = 0;
sx126x_status_t status = sx126x_write_register( context, SX126X_REG_RXTX_PAYLOAD_LEN, &tmp, 1 );
if( status != SX126X_STATUS_OK ) return status;
return sx126x_set_rx_with_timeout_in_rtc_step( context, timeout );
}
sx126x_status_t sx126x_long_pkt_rx_get_partial_payload( const void* context, struct sx126x_long_pkt_rx_state* state,
uint8_t* dest, unsigned int max_len, uint8_t* num_bytes_read )
{
uint8_t num_bytes_available;
uint8_t current_index;
// Find how many bytes of data are available
sx126x_status_t status = sx126x_read_register( context, SX126X_REG_RX_ADDR_POINTER, ¤t_index, 1 );
if( status != SX126X_STATUS_OK ) return status;
num_bytes_available = current_index - state->index;
if( num_bytes_available > max_len )
{
num_bytes_available = max_len;
current_index = state->index + num_bytes_available;
}
// Fool the transceiver into keeping rx on for 255 more bytes.
uint8_t tmp = current_index - 1;
status = sx126x_write_register( context, SX126X_REG_RXTX_PAYLOAD_LEN, &tmp, 1 );
if( status != SX126X_STATUS_OK ) return status;
if( num_bytes_available > 0 )
{
status = sx126x_read_buffer( context, state->index, dest, num_bytes_available );
state->index = current_index;
}
*num_bytes_read = num_bytes_available;
return status;
}
uint8_t sx126x_long_pkt_rx_check_for_last( unsigned int num_remaining, unsigned int stop_margin )
{
if( stop_margin > SX126X_SHORT_PACKET_MAX )
{
stop_margin = SX126X_SHORT_PACKET_MAX;
}
if( num_remaining < stop_margin )
{
num_remaining = stop_margin;
}
if( num_remaining >= 255 )
{
return 0;
}
return num_remaining;
}
sx126x_status_t sx126x_long_pkt_rx_prepare_for_last( const void* context, struct sx126x_long_pkt_rx_state* state,
uint8_t stop_offset )
{
uint8_t end = state->index + stop_offset;
return sx126x_write_register( context, SX126X_REG_RXTX_PAYLOAD_LEN, &end, 1 );
}
sx126x_status_t sx126x_long_pkt_rx_complete( const void* context )
{
return sx126x_set_standby( context, SX126X_STANDBY_CFG_RC );
}
sx126x_status_t sx126x_long_pkt_tx_bitbang_activate( const void* context,
struct sx126x_long_pkt_tx_io_storage* storage )
{
uint8_t tmp;
sx126x_status_t status;
status = sx126x_read_register( context, 0x680, &tmp, 1 );
if( status != SX126X_STATUS_OK ) return status;
if( storage != 0 )
{
storage->state1 = tmp & 0x70;
}
tmp = ( tmp & ~0x70 ) | 0x10;
status = sx126x_write_register( context, 0x680, &tmp, 1 );
if( status != SX126X_STATUS_OK ) return status;
status = sx126x_read_register( context, 0x0587, &tmp, 1 );
if( status != SX126X_STATUS_OK ) return status;
if( storage != 0 )
{
storage->state1 |= tmp & 0x0F;
}
tmp = ( tmp & ~0x0F ) | 0x0C;
status = sx126x_write_register( context, 0x587, &tmp, 1 );
if( status != SX126X_STATUS_OK ) return status;
status = sx126x_read_register( context, 0x0580, &tmp, 1 );
if( status != SX126X_STATUS_OK ) return status;
if( storage != 0 )
{
storage->state2 |= tmp & ( 1 << 3 );
}
tmp = tmp | 1 << 3;
status = sx126x_write_register( context, 0x580, &tmp, 1 );
if( status != SX126X_STATUS_OK ) return status;
status = sx126x_read_register( context, 0x0583, &tmp, 1 );
if( status != SX126X_STATUS_OK ) return status;
if( storage != 0 )
{
storage->state2 |= ( tmp & ( 1 << 3 ) ) << 1;
}
tmp = tmp | 1 << 3;
status = sx126x_write_register( context, 0x583, &tmp, 1 );
return status;
}
sx126x_status_t sx126x_long_pkt_tx_bitbang_restore( const void* context,
const struct sx126x_long_pkt_tx_io_storage* storage )
{
uint8_t tmp;
sx126x_status_t status;
status = sx126x_read_register( context, 0x680, &tmp, 1 );
if( status != SX126X_STATUS_OK ) return status;
tmp = ( tmp & ~0x70 ) | ( storage->state1 & 0x70 );
status = sx126x_write_register( context, 0x680, &tmp, 1 );
if( status != SX126X_STATUS_OK ) return status;
status = sx126x_read_register( context, 0x0587, &tmp, 1 );
if( status != SX126X_STATUS_OK ) return status;
tmp = ( tmp & ~0x0F ) | ( storage->state1 & 0x0F );
status = sx126x_write_register( context, 0x587, &tmp, 1 );
if( status != SX126X_STATUS_OK ) return status;
status = sx126x_read_register( context, 0x0580, &tmp, 1 );
if( status != SX126X_STATUS_OK ) return status;
tmp = ( tmp & ~( 1 << 3 ) ) | ( storage->state2 & ( 1 << 3 ) );
status = sx126x_write_register( context, 0x580, &tmp, 1 );
if( status != SX126X_STATUS_OK ) return status;
status = sx126x_read_register( context, 0x0583, &tmp, 1 );
if( status != SX126X_STATUS_OK ) return status;
tmp = ( tmp & ~( 1 << 3 ) ) | ( ( storage->state2 & ( 1 << 4 ) ) >> 1 );
status = sx126x_write_register( context, 0x583, &tmp, 1 );
return status;
}
sx126x_status_t sx126x_long_pkt_tx_set_gfsk_pkt_params( const void* context,
const sx126x_long_pkt_pkt_params_gfsk_t* params )
{
sx126x_status_t status = sx126x_long_pkt_check_gfsk_pkt_params( params->pkt_params );
if( status != SX126X_STATUS_OK )
{
return status;
}
sx126x_pkt_params_gfsk_t mod_params = *params->pkt_params;
// Use payload and sync_word reservations for large packets.
if( params->long_pld_len_in_bytes >= 512 )
{
mod_params.pld_len_in_bytes = 255;
mod_params.sync_word_len_in_bits = 64;
}
else
{
mod_params.pld_len_in_bytes = 1;
mod_params.sync_word_len_in_bits = 0;
}
unsigned int pbl_len_in_bits = params->pkt_params->sync_word_len_in_bits + params->pkt_params->pld_len_in_bytes*8 +
( params->long_pld_len_in_bytes << 3 ) - ( mod_params.pld_len_in_bytes << 3 ) -
mod_params.sync_word_len_in_bits;
if( pbl_len_in_bits > 65535 )
{
return SX126X_STATUS_ERROR;
}
mod_params.pld_len_in_bytes = pbl_len_in_bits/8;
return sx126x_set_gfsk_pkt_params( context, &mod_params );
}
void sx126x_long_pkt_tx_bits_init( struct sx126x_long_pkt_tx_state* state,
const sx126x_long_pkt_pkt_params_gfsk_t* params, const uint8_t* sync_word,
const uint8_t* payload )
{
state->part = SX126X_LONG_PACKET_TX_PART_PREAMBLE;
state->offset = 0;
state->long_pkt_params = params;
state->sync_word = sync_word;
state->payload = payload;
}
bool sx126x_long_pkt_tx_bits_get( struct sx126x_long_pkt_tx_state* state, bool* bit_to_send )
{
if( state->part == SX126X_LONG_PACKET_TX_PART_PREAMBLE )
{
*bit_to_send = state->offset % 2;
if( state->offset++ < state->long_pkt_params->pkt_params->pld_len_in_bytes*8 )
{
return true;
}
state->part = SX126X_LONG_PACKET_TX_PART_SYNC_WORD;
state->offset = 0;
}
if( state->part == SX126X_LONG_PACKET_TX_PART_SYNC_WORD )
{
unsigned int byte_num = state->offset / 8;
unsigned int bit_num = state->offset % 8;
if( state->offset++ < state->long_pkt_params->pkt_params->sync_word_len_in_bits )
{
*bit_to_send = ( ( state->sync_word[byte_num] >> ( 7 - bit_num ) ) & 0x01 ) != 0;
return true;
}
state->part = SX126X_LONG_PACKET_TX_PART_PAYLOAD;
state->offset = 0;
}
if( state->part == SX126X_LONG_PACKET_TX_PART_PAYLOAD )
{
unsigned int byte_num = state->offset / 8;
unsigned int bit_num = state->offset % 8;
if( state->offset++ < ( state->long_pkt_params->long_pld_len_in_bytes << 3 ) )
{
*bit_to_send = ( ( state->payload[byte_num] >> ( 7 - bit_num ) ) & 0x01 ) != 0;
return true;
}
state->part = SX126X_LONG_PACKET_TX_PART_DONE;
state->offset = 0;
}
return false;
}
static sx126x_status_t sx126x_long_pkt_check_gfsk_pkt_params( const sx126x_pkt_params_gfsk_t* params )
{
if( params->crc_type != SX126X_GFSK_CRC_OFF )
{
return SX126X_STATUS_UNSUPPORTED_FEATURE;
}
if( params->address_filtering != SX126X_GFSK_ADDRESS_FILTERING_DISABLE )
{
return SX126X_STATUS_UNSUPPORTED_FEATURE;
}
return SX126X_STATUS_OK;
}