-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathS2LP_PktStack.c
More file actions
514 lines (407 loc) · 14.7 KB
/
S2LP_PktStack.c
File metadata and controls
514 lines (407 loc) · 14.7 KB
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/**
* @file S2LP_PktStack.c
* @author ST Microelectronics
* @version 1.3.0
* @date June, 2019
* @brief Configuration and management of S2-LP STack packets.
* @details
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS 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.
*
* THIS SOURCE CODE IS PROTECTED BY A LICENSE.
* FOR MORE INFORMATION PLEASE CAREFULLY READ THE LICENSE AGREEMENT FILE LOCATED
* IN THE ROOT DIRECTORY OF THIS FIRMWARE PACKAGE.
*
* <h2><center>© COPYRIGHT 2019 STMicroelectronics</center></h2>
*/
/* Includes ------------------------------------------------------------------*/
#include "S2LP_PktStack.h"
#include "S2LP_PktWMbus.h"
/**
* @addtogroup S2LP_Libraries
* @{
*/
/**
* @addtogroup S2LP_PktStack
* @{
*/
/**
* @defgroup PktStack_Private_Defines Pkt STack Private Defines
* @{
*/
#define PKT_FORMAT_STACK_CODE (uint8_t)3
/**
*@}
*/
/**
* @defgroup PktStack_Private_Macros Pkt STack Private Macros
* @{
*/
#define IS_STACK_PREAMBLE_LENGTH IS_PREAMBLE_LEN
#define IS_STACK_SYNC_LENGTH IS_SYNC_LEN
#define IS_STACK_PKT_LEN_FIELD_WID IS_PKT_LEN_FIELD_WID
#define IS_STACK_CRC_MODE IS_PKT_CRC_MODE
#define IS_STACK_NMAX_RETX(NRETX) (NRETX<=15)
#define IS_STACK_SEQNUM_RELOAD_VAL(VAL) (VAL<=3)
/**
*@}
*/
/**
* @defgroup PktStack_Private_Functions Pkt STack Private Functions
* @{
*/
/**
* @brief Initialize the S2LP STack packet according to the specified
* parameters in the PktStackInit.
* @param pxPktStackInit STack packet init structure.
* This parameter is a pointer to @ref PktStackInit.
* @retval None.
*/
void S2LPPktStackInit(PktStackInit* pxPktStackInit)
{
uint8_t tmpBuffer[6];
/* Check the parameters */
s_assert_param(IS_STACK_PREAMBLE_LENGTH(pxPktStackInit->xPreambleLength));
s_assert_param(IS_STACK_SYNC_LENGTH(pxPktStackInit->xSyncLength));
s_assert_param(IS_STACK_CRC_MODE(pxPktStackInit->xCrcMode));
s_assert_param(IS_SFUNCTIONAL_STATE(pxPktStackInit->cExtendedPktLenField));
s_assert_param(IS_SFUNCTIONAL_STATE(pxPktStackInit->xFixVarLength));
s_assert_param(IS_SFUNCTIONAL_STATE(pxPktStackInit->xFec));
s_assert_param(IS_SFUNCTIONAL_STATE(pxPktStackInit->xDataWhitening));
S2LPPktWMbusSetSubmode(WMBUS_SUBMODE_NOT_CONFIGURED);
/* Always set the automatic packet filtering */
S2LPSpiReadRegisters(PROTOCOL1_ADDR, 1, &tmpBuffer[0]);
tmpBuffer[0] |= AUTO_PCKT_FLT_REGMASK;
S2LPSpiWriteRegisters(PROTOCOL1_ADDR, 1, &tmpBuffer[0]);
tmpBuffer[0] = ((pxPktStackInit->xSyncLength)<<2) | (uint8_t)((pxPktStackInit->xPreambleLength)>>8);
tmpBuffer[1] = (uint8_t)(pxPktStackInit->xPreambleLength);
tmpBuffer[2] = ((uint8_t)pxPktStackInit->cExtendedPktLenField)<<7 | ADDRESS_LEN_REGMASK;
S2LPSpiReadRegisters(PCKTCTRL3_ADDR, 1, &tmpBuffer[3]);
tmpBuffer[3] &= ~(PCKT_FRMT_REGMASK | RX_MODE_REGMASK);
tmpBuffer[3] |= PKT_FORMAT_STACK_CODE<<6;
S2LPSpiReadRegisters(PCKTCTRL2_ADDR, 2, &tmpBuffer[4]);
if(pxPktStackInit->xFixVarLength == S_ENABLE) {
tmpBuffer[4] |= FIX_VAR_LEN_REGMASK;
}
else {
tmpBuffer[4] &= ~FIX_VAR_LEN_REGMASK;
}
tmpBuffer[4] &= ~(MANCHESTER_EN_REGMASK | MBUS_3OF6_EN_REGMASK);
tmpBuffer[5] &= ~(CRC_MODE_REGMASK | TXSOURCE_REGMASK);
tmpBuffer[5] |= (uint8_t)pxPktStackInit->xCrcMode;
if(pxPktStackInit->xDataWhitening == S_ENABLE) {
tmpBuffer[5] |= WHIT_EN_REGMASK;
}
else {
tmpBuffer[5] &= ~WHIT_EN_REGMASK;
}
if(pxPktStackInit->xFec == S_ENABLE) {
tmpBuffer[5] |= FEC_EN_REGMASK;
}
else {
tmpBuffer[5] &= ~FEC_EN_REGMASK;
}
S2LPSpiWriteRegisters(PCKTCTRL6_ADDR, 6, tmpBuffer);
/* SYNC word */
for(uint8_t i=0 ; i<4 ; i++) {
tmpBuffer[i] = (uint8_t)(pxPktStackInit->lSyncWords>>(8*i));
}
*(uint8_t*)&g_xStatus = S2LPSpiWriteRegisters(SYNC3_ADDR, 4, tmpBuffer);
/* Sets CRC check bit */
if(pxPktStackInit->xCrcMode == PKT_NO_CRC) {
S2LPPktStackFilterOnCrc(S_DISABLE);
}
else {
S2LPPktStackFilterOnCrc(S_ENABLE);
}
/* Constellation map setting */
S2LPSpiReadRegisters(MOD1_ADDR, 1, tmpBuffer);
tmpBuffer[0] &= ~G4FSK_CONST_MAP_REGMASK;
S2LPSpiWriteRegisters(MOD1_ADDR, 1, tmpBuffer);
}
/**
* @brief Return the S2LP STack packet structure according to the specified parameters in the registers.
* @param pxPktStackInit STack packet init structure.
* This parameter is a pointer to @ref PktStackInit.
* @retval None.
*/
void S2LPPktStackGetInfo(PktStackInit* pxPktStackInit)
{
uint8_t tmpBuffer[6];
S2LPSpiReadRegisters(PCKTCTRL6_ADDR, 6, tmpBuffer);
/* Sync length */
pxPktStackInit->xSyncLength = ((tmpBuffer[0] & SYNC_LEN_REGMASK)>>2);
/* Preamble length */
pxPktStackInit->xPreambleLength = (((uint16_t)(tmpBuffer[0] & PREAMBLE_LEN_9_8_REGMASK))<<8) | ((uint16_t)tmpBuffer[1]);
/* Length width */
pxPktStackInit->cExtendedPktLenField = (SFunctionalState)((tmpBuffer[2] & LEN_WID_REGMASK)>>7);
/* FIX or VAR bit */
pxPktStackInit->xFixVarLength = (SFunctionalState)(tmpBuffer[4] & FIX_VAR_LEN_REGMASK);
/* CRC mode */
pxPktStackInit->xCrcMode = (StackCrcMode)(tmpBuffer[5] & CRC_MODE_REGMASK);
/* Whitening */
pxPktStackInit->xDataWhitening = (SFunctionalState)((tmpBuffer[5] & WHIT_EN_REGMASK)>> 4);
/* FEC */
pxPktStackInit->xFec = (SFunctionalState)(tmpBuffer[5] & FEC_EN_REGMASK);
*(uint8_t*)&g_xStatus = S2LPSpiReadRegisters(SYNC3_ADDR, 4, tmpBuffer);
/* SYNC word */
pxPktStackInit->lSyncWords = 0;
for(uint8_t i=0 ; i<4 ; i++) {
pxPktStackInit->lSyncWords |= ((uint32_t)tmpBuffer[i])<<(8*i);
}
}
/**
* @brief Initialize the S2LP STack packet addresses according to the specified
* parameters in the PktStackAddresses struct.
* @param pxPktStackAddresses STack packet addresses init structure.
* This parameter is a pointer to @ref PktStackAddressesInit .
* @retval None.
*/
void S2LPPktStackAddressesInit(PktStackAddressesInit* pxPktStackAddresses)
{
uint8_t tmpBuffer[3];
s_assert_param(IS_SFUNCTIONAL_STATE(pxPktStackAddresses->xFilterOnMyAddress));
s_assert_param(IS_SFUNCTIONAL_STATE(pxPktStackAddresses->xFilterOnMulticastAddress));
s_assert_param(IS_SFUNCTIONAL_STATE(pxPktStackAddresses->xFilterOnBroadcastAddress));
/* Reads the PCKT_FLT_OPTIONS ragister */
S2LPSpiReadRegisters(PCKT_FLT_OPTIONS_ADDR, 1, &tmpBuffer[0]);
/* Enables or disables filtering on my address */
if(pxPktStackAddresses->xFilterOnMyAddress == S_ENABLE) {
tmpBuffer[0] |= DEST_VS_SOURCE_ADDR_REGMASK;
}
else {
tmpBuffer[0] &= ~DEST_VS_SOURCE_ADDR_REGMASK;
}
/* Enables or disables filtering on multicast address */
if(pxPktStackAddresses->xFilterOnMulticastAddress == S_ENABLE) {
tmpBuffer[0] |= DEST_VS_MULTICAST_ADDR_REGMASK;
}
else {
tmpBuffer[0] &= ~DEST_VS_MULTICAST_ADDR_REGMASK;
}
/* Enables or disables filtering on broadcast address */
if(pxPktStackAddresses->xFilterOnBroadcastAddress == S_ENABLE) {
tmpBuffer[0] |= DEST_VS_BROADCAST_ADDR_REGMASK;
}
else {
tmpBuffer[0] &= ~DEST_VS_BROADCAST_ADDR_REGMASK;
}
S2LPSpiWriteRegisters(PCKT_FLT_OPTIONS_ADDR, 1, &tmpBuffer[0]);
/* Fills the array with the addresses passed in the structure */
tmpBuffer[2] = pxPktStackAddresses->cMyAddress;
tmpBuffer[0] = pxPktStackAddresses->cBroadcastAddress;
tmpBuffer[1] = pxPktStackAddresses->cMulticastAddress;
*(uint8_t*)&g_xStatus = S2LPSpiWriteRegisters(PCKT_FLT_GOALS2_ADDR, 3, tmpBuffer);
}
/**
* @brief Return the S2LP STack packet addresses structure according to the specified
* parameters in the registers.
* @param pxPktStackAddresses STack packet addresses init structure.
* This parameter is a pointer to @ref PktStackAddresses.
* @retval None.
*/
void S2LPPktStackGetAddressesInfo(PktStackAddressesInit* pxPktStackAddresses)
{
uint8_t tmpBuffer[3];
S2LPSpiReadRegisters(PCKT_FLT_GOALS3_ADDR, 3, tmpBuffer);
pxPktStackAddresses->cMyAddress = tmpBuffer[0];
pxPktStackAddresses->cBroadcastAddress = tmpBuffer[1];
pxPktStackAddresses->cMulticastAddress = tmpBuffer[2];
*(uint8_t*)&g_xStatus = S2LPSpiReadRegisters(PCKT_FLT_OPTIONS_ADDR, 1, &tmpBuffer[0]);
pxPktStackAddresses->xFilterOnBroadcastAddress = (SFunctionalState)((tmpBuffer[0] & DEST_VS_BROADCAST_ADDR_REGMASK) >> 3);
pxPktStackAddresses->xFilterOnMulticastAddress = (SFunctionalState)((tmpBuffer[0] & DEST_VS_MULTICAST_ADDR_REGMASK) >> 2);
pxPktStackAddresses->xFilterOnMyAddress = (SFunctionalState)((tmpBuffer[0] & DEST_VS_SOURCE_ADDR_REGMASK) >> 1);
}
/**
* @brief Configure the STack packet format for S2LP.
* @param None.
* @retval None.
*/
void S2LPPktStackSetFormat(void)
{
uint8_t tmp;
S2LPSpiReadRegisters(PCKTCTRL3_ADDR, 1, &tmp);
/* Build the new value. Also set to 0 the direct RX mode bits */
tmp &= ~(PCKT_FRMT_REGMASK | RX_MODE_REGMASK);
tmp |= PKT_FORMAT_STACK_CODE;
S2LPSpiWriteRegisters(PCKTCTRL3_ADDR, 1, &tmp);
S2LPSpiReadRegisters(PCKTCTRL1_ADDR, 1, &tmp);
/* Set to 0 the direct TX mode bits */
tmp &= ~TXSOURCE_REGMASK;
*(uint8_t*)&g_xStatus = S2LPSpiWriteRegisters(PCKTCTRL1_ADDR, 1, &tmp);
S2LPPktWMbusSetSubmode(WMBUS_SUBMODE_NOT_CONFIGURED);
}
/**
* @brief Set the payload length for S2LP STack packets. Since the packet length
* depends from the address (always 2 for this packet format)
* and the control field size, this function reads the control length register
* content in order to determine the correct packet length to be written.
* @param nPayloadLength payload length in bytes.
* This parameter can be any value of uint16_t.
* @retval None.
*/
void S2LPPktStackSetPayloadLength(uint16_t nPayloadLength)
{
uint8_t tmpBuffer[2];
nPayloadLength+=2;
tmpBuffer[0] = (uint8_t)(nPayloadLength>>8);
tmpBuffer[1] = (uint8_t)nPayloadLength;
*(uint8_t*)&g_xStatus = S2LPSpiWriteRegisters(PCKTLEN1_ADDR, 2, tmpBuffer);
}
/**
* @brief Return the payload length for S2LP STack packets. Since the
* packet length depends from the address and the control
* field size, this function reads the correspondent
* registers in order to determine the correct payload length
* to be returned.
* @param None.
* @retval uint16_t Payload length.
*/
uint16_t S2LPPktStackGetPayloadLength(void)
{
uint8_t tmpBuffer[2];
uint16_t nPayloadLength;
*(uint8_t*)&g_xStatus = S2LPSpiReadRegisters(PCKTLEN1_ADDR, 2, tmpBuffer);
nPayloadLength = (((uint16_t)tmpBuffer[0])<<8) | ((uint16_t)tmpBuffer[1]);
nPayloadLength-=2;
return nPayloadLength;
}
/**
* @brief Return the packet length field of the received packet.
* @param None.
* @retval uint16_t Packet length.
*/
uint16_t S2LPPktStackGetReceivedPktLength(void)
{
uint8_t tmpBuffer[2];
uint16_t nPayloadLength;
*(uint8_t*)&g_xStatus = S2LPSpiReadRegisters(RX_PCKT_LEN1_ADDR, 2, tmpBuffer);
nPayloadLength = (((uint16_t)tmpBuffer[0])<<8) | ((uint16_t)tmpBuffer[1]);
nPayloadLength--;
return nPayloadLength;
}
/**
* @brief Se the AUTO_ACK bit on the receiver .
* @param xNewState if S_ENABLE, the receiver will check the NO_ACK bit to see if the ack should be sent (NO_ACK=0) or not (NO_ACK=1).
* @retval None.
*/
void S2LPPktStackAutoAck(SFunctionalState xNewState)
{
uint8_t tmp;
s_assert_param(IS_SFUNCTIONAL_STATE(xNewState));
S2LPSpiReadRegisters(PROTOCOL0_ADDR, 1, &tmp);
if(xNewState == S_ENABLE) {
tmp |= AUTO_ACK_REGMASK;
}
else {
tmp &= ~AUTO_ACK_REGMASK;
}
*(uint8_t*)&g_xStatus = S2LPSpiWriteRegisters(PROTOCOL0_ADDR, 1, &tmp);
}
/**
* @brief Set the number of retransmissions to be done in case of ACK loss.
* @param nRetx number of retransmissions.
* @retval None.
*/
void S2LPPktStackNRetx(uint8_t nRetx)
{
uint8_t tmp;
s_assert_param(IS_STACK_NMAX_RETX(nRetx));
S2LPSpiReadRegisters(PROTOCOL0_ADDR, 1, &tmp);
tmp &= ~NMAX_RETX_REGMASK;
tmp |= (nRetx<<4);
*(uint8_t*)&g_xStatus = S2LPSpiWriteRegisters(PROTOCOL0_ADDR, 1, &tmp);
}
/**
* @brief Get the NO_ACK bit.
* @param None.
* @retval SFlagStatus if it is S_SET, the ack will be not requested, otherwise it will be.
*/
SFlagStatus S2LPPktStackGetTXAckRequest(void)
{
uint8_t tmp;
S2LPSpiReadRegisters(RX_PCKT_INFO_ADDR, 1, &tmp);
tmp &= NACK_RX_REGMASK;
tmp >>= 2;
return (SFlagStatus)tmp;
}
/**
* @brief This function will set the NO_ACK bit or reset it.
* @param xNewState if it is S_DISABLE, the ack will be not request and thus the NO_ACK bit will be set to 1.
* if this parameter is S_ENABLE, the ack will be request and thus the NO_ACK bit will be set to 0.
* @retval None.
*/
void S2LPPktStackAckRequest(SFunctionalState xNewState)
{
uint8_t tmp;
s_assert_param(IS_SFUNCTIONAL_STATE(xNewState));
S2LPSpiReadRegisters(PROTOCOL0_ADDR, 1, &tmp);
if(xNewState == S_ENABLE) {
tmp &= ~NACK_TX_REGMASK;
}
else {
tmp |= NACK_TX_REGMASK;
}
*(uint8_t*)&g_xStatus = S2LPSpiWriteRegisters(PROTOCOL0_ADDR, 1, &tmp);
}
/**
* @brief Enable or Disable the piggybacking.
* @param xNewState enable or disable.
* @retval None.
*/
void S2LPPktStackPiggybacking(SFunctionalState xNewState)
{
uint8_t tmp;
s_assert_param(IS_SFUNCTIONAL_STATE(xNewState));
S2LPSpiReadRegisters(PROTOCOL1_ADDR, 1, &tmp);
if(xNewState == S_ENABLE) {
tmp |= PIGGYBACKING_REGMASK;
}
else {
tmp &= ~PIGGYBACKING_REGMASK;
}
*(uint8_t*)&g_xStatus = S2LPSpiWriteRegisters(PROTOCOL1_ADDR, 1, &tmp);
}
/**
* @brief Set the reload value of the sequence number.
* @note A SEQ_NUM_RELOAD command must be strobed to make this value available for the next packet.
* @param cReloadValue reload value.
* @retval None.
*/
void S2LPPktStackSeqNumForReload(uint8_t cReloadValue)
{
uint8_t tmp;
s_assert_param(IS_STACK_SEQNUM_RELOAD_VAL(cReloadValue));
S2LPSpiReadRegisters(PROTOCOL2_ADDR, 1, &tmp);
tmp &= ~TX_SEQ_NUM_RELOAD_REGMASK;
tmp |= (cReloadValue<<3);
*(uint8_t*)&g_xStatus = S2LPSpiWriteRegisters(PROTOCOL2_ADDR, 1, &tmp);
}
/**
* @brief Returns the number of retransmission done on the transmitted packet.
* @param None.
* @retval uint8_t Number of retransmissions done until now.
*/
uint8_t S2LPPktStackGetNReTx(void)
{
uint8_t tempRetValue;
/* Reads the TX_PCKT_INFO register value */
*(uint8_t*)&g_xStatus = S2LPSpiReadRegisters(TX_PCKT_INFO_ADDR, 1, &tempRetValue);
/* Obtains and returns the number of retransmission done */
return (tempRetValue & N_RETX_REGMASK);
}
/**
*@}
*/
/**
*@}
*/
/**
*@}
*/
/******************* (C) COPYRIGHT 2019 STMicroelectronics *****END OF FILE****/