Appiko
S2LP_PktBasic.c
Go to the documentation of this file.
1 
24 /* Includes ------------------------------------------------------------------*/
25 #include "S2LP_PktBasic.h"
26 #include "MCU_Interface.h"
27 #include "S2LP_PktWMbus.h"
28 
29 
48 #define PKT_FORMAT_BASIC_CODE 0x00
49 
60 #define IS_BASIC_PREAMBLE_LENGTH IS_PREAMBLE_LEN
61 #define IS_BASIC_SYNC_LENGTH IS_SYNC_LEN
62 #define IS_BASIC_PKT_LEN_FIELD_WID IS_PKT_LEN_FIELD_WID
63 #define IS_BASIC_CRC_MODE IS_PKT_CRC_MODE
64 
83 void S2LPPktBasicInit(PktBasicInit* pxPktBasicInit)
84 {
85  uint8_t tmpBuffer[6];
86 
87  /* Check the parameters */
88  s_assert_param(IS_BASIC_PREAMBLE_LENGTH(pxPktBasicInit->xPreambleLength));
89  s_assert_param(IS_BASIC_SYNC_LENGTH(pxPktBasicInit->xSyncLength));
90  s_assert_param(IS_BASIC_CRC_MODE(pxPktBasicInit->xCrcMode));
91  s_assert_param(IS_SFUNCTIONAL_STATE(pxPktBasicInit->cExtendedPktLenField));
92  s_assert_param(IS_SFUNCTIONAL_STATE(pxPktBasicInit->xFixVarLength));
93  s_assert_param(IS_SFUNCTIONAL_STATE(pxPktBasicInit->xAddressField));
94  s_assert_param(IS_SFUNCTIONAL_STATE(pxPktBasicInit->xFec));
95  s_assert_param(IS_SFUNCTIONAL_STATE(pxPktBasicInit->xDataWhitening));
96 
98 
99  /* Always set the automatic packet filtering */
100  S2LPSpiReadRegisters(PROTOCOL1_ADDR, 1, &tmpBuffer[0]);
101  tmpBuffer[0] |= AUTO_PCKT_FLT_REGMASK;
102  S2LPSpiWriteRegisters(PROTOCOL1_ADDR, 1, &tmpBuffer[0]);
103 
104  tmpBuffer[0] = ((pxPktBasicInit->xSyncLength)<<2) | (uint8_t)((pxPktBasicInit->xPreambleLength)>>8);
105  tmpBuffer[1] = (uint8_t)(pxPktBasicInit->xPreambleLength);
106  tmpBuffer[2] = (((uint8_t)pxPktBasicInit->xAddressField)<<3);
107 
108  if((pxPktBasicInit->cExtendedPktLenField)==S_ENABLE)
109  {
110  tmpBuffer[2]|=0x80;
111  }
112 
113  S2LPSpiReadRegisters(PCKTCTRL3_ADDR, 1, &tmpBuffer[3]);
114  tmpBuffer[3] &= ~(PCKT_FRMT_REGMASK | RX_MODE_REGMASK);
115  tmpBuffer[3] |= PKT_FORMAT_BASIC_CODE;
116 
117  S2LPSpiReadRegisters(PCKTCTRL2_ADDR, 2, &tmpBuffer[4]);
118 
119  if(pxPktBasicInit->xFixVarLength == S_ENABLE) {
120  tmpBuffer[4] |= FIX_VAR_LEN_REGMASK;
121  }
122  else {
123  tmpBuffer[4] &= ~FIX_VAR_LEN_REGMASK;
124  }
125  tmpBuffer[4] &= ~(MANCHESTER_EN_REGMASK | MBUS_3OF6_EN_REGMASK);
126 
127  tmpBuffer[5] &= ~(CRC_MODE_REGMASK | TXSOURCE_REGMASK);
128  tmpBuffer[5] |= (uint8_t)pxPktBasicInit->xCrcMode;
129 
130  if(pxPktBasicInit->xDataWhitening == S_ENABLE) {
131  tmpBuffer[5] |= WHIT_EN_REGMASK;
132  }
133  else {
134  tmpBuffer[5] &= ~WHIT_EN_REGMASK;
135  }
136 
137  if(pxPktBasicInit->xFec == S_ENABLE)
138  {
139  tmpBuffer[5] |= FEC_EN_REGMASK;
140  }
141  else {
142  tmpBuffer[5] &= ~FEC_EN_REGMASK;
143  }
144 
145  S2LPSpiWriteRegisters(PCKTCTRL6_ADDR, 6, tmpBuffer);
146 
147  /* SYNC word */
148  for(uint8_t i=0 ; i<4 ; i++) {
149  tmpBuffer[i] = (uint8_t)(pxPktBasicInit->lSyncWords>>(8*i));
150  }
151  g_xStatus = S2LPSpiWriteRegisters(SYNC3_ADDR, 4, tmpBuffer);
152 
153  /* Sets CRC check bit */
154  if(pxPktBasicInit->xCrcMode == PKT_NO_CRC) {
155  S2LPPktBasicFilterOnCrc(S_DISABLE);
156  }
157  else {
158  S2LPPktBasicFilterOnCrc(S_ENABLE);
159  }
160 
161  /* Constellation map setting */
162  S2LPSpiReadRegisters(MOD1_ADDR, 1, tmpBuffer);
163  tmpBuffer[0] &= ~G4FSK_CONST_MAP_REGMASK;
164  S2LPSpiWriteRegisters(MOD1_ADDR, 1, tmpBuffer);
165 }
166 
167 
174 void S2LPPktBasicGetInfo(PktBasicInit* pxPktBasicInit)
175 {
176  uint8_t tmpBuffer[6];
177 
178  S2LPSpiReadRegisters(PCKTCTRL6_ADDR, 6, tmpBuffer);
179 
180  /* Sync length */
181  pxPktBasicInit->xSyncLength = ((tmpBuffer[0] & SYNC_LEN_REGMASK)>>2);
182 
183  /* Preamble length */
184  pxPktBasicInit->xPreambleLength = (((uint16_t)(tmpBuffer[0] & PREAMBLE_LEN_9_8_REGMASK))<<8) | ((uint16_t)tmpBuffer[1]);
185 
186  /* Length width */
187  pxPktBasicInit->cExtendedPktLenField = (SFunctionalState)((tmpBuffer[2] & LEN_WID_REGMASK)>>7);
188 
189  /* Address field */
190  pxPktBasicInit->xAddressField = (SFunctionalState)((tmpBuffer[2] & ADDRESS_LEN_REGMASK)>>3);
191 
192  /* FIX or VAR bit */
193  pxPktBasicInit->xFixVarLength = (SFunctionalState)(tmpBuffer[4] & FIX_VAR_LEN_REGMASK);
194 
195  /* CRC mode */
196  pxPktBasicInit->xCrcMode = (BasicCrcMode)(tmpBuffer[5] & CRC_MODE_REGMASK);
197 
198  /* Whitening */
199  pxPktBasicInit->xDataWhitening = (SFunctionalState)((tmpBuffer[5] & WHIT_EN_REGMASK)>> 4);
200 
201  /* FEC */
202  pxPktBasicInit->xFec = (SFunctionalState)(tmpBuffer[5] & FEC_EN_REGMASK);
203 
204  g_xStatus = S2LPSpiReadRegisters(SYNC3_ADDR, 4, tmpBuffer);
205 
206  /* SYNC word */
207  pxPktBasicInit->lSyncWords = 0;
208  for(uint8_t i=0 ; i<4 ; i++) {
209  pxPktBasicInit->lSyncWords |= ((uint32_t)tmpBuffer[i])<<(8*i);
210  }
211 }
212 
213 
222 {
223  uint8_t tmpBuffer[3];
224  s_assert_param(IS_SFUNCTIONAL_STATE(pxPktBasicAddresses->xFilterOnMyAddress));
225  s_assert_param(IS_SFUNCTIONAL_STATE(pxPktBasicAddresses->xFilterOnMulticastAddress));
226  s_assert_param(IS_SFUNCTIONAL_STATE(pxPktBasicAddresses->xFilterOnBroadcastAddress));
227 
228  /* Reads the PCKT_FLT_OPTIONS ragister */
229  S2LPSpiReadRegisters(PCKT_FLT_OPTIONS_ADDR, 1, &tmpBuffer[0]);
230 
231  /* Enables or disables filtering on my address */
232  if(pxPktBasicAddresses->xFilterOnMyAddress == S_ENABLE) {
233  tmpBuffer[0] |= DEST_VS_SOURCE_ADDR_REGMASK;
234  }
235  else {
236  tmpBuffer[0] &= ~DEST_VS_SOURCE_ADDR_REGMASK;
237  }
238 
239  /* Enables or disables filtering on multicast address */
240  if(pxPktBasicAddresses->xFilterOnMulticastAddress == S_ENABLE) {
241  tmpBuffer[0] |= DEST_VS_MULTICAST_ADDR_REGMASK;
242  }
243  else {
244  tmpBuffer[0] &= ~DEST_VS_MULTICAST_ADDR_REGMASK;
245  }
246 
247  /* Enables or disables filtering on broadcast address */
248  if(pxPktBasicAddresses->xFilterOnBroadcastAddress == S_ENABLE) {
249  tmpBuffer[0] |= DEST_VS_BROADCAST_ADDR_REGMASK;
250  }
251  else {
252  tmpBuffer[0] &= ~DEST_VS_BROADCAST_ADDR_REGMASK;
253  }
254 
255  S2LPSpiWriteRegisters(PCKT_FLT_OPTIONS_ADDR, 1, &tmpBuffer[0]);
256 
257  /* Fills the array with the addresses passed in the structure */
258  tmpBuffer[2] = pxPktBasicAddresses->cMyAddress;
259  tmpBuffer[0] = pxPktBasicAddresses->cBroadcastAddress;
260  tmpBuffer[1] = pxPktBasicAddresses->cMulticastAddress;
261  g_xStatus = S2LPSpiWriteRegisters(PCKT_FLT_GOALS2_ADDR, 3, tmpBuffer);
262 }
263 
264 
273 {
274  uint8_t tmpBuffer[3];
275 
276  S2LPSpiReadRegisters(PCKT_FLT_GOALS3_ADDR, 3, tmpBuffer);
277  pxPktBasicAddresses->cMyAddress = tmpBuffer[0];
278  pxPktBasicAddresses->cBroadcastAddress = tmpBuffer[1];
279  pxPktBasicAddresses->cMulticastAddress = tmpBuffer[2];
280 
281  g_xStatus = S2LPSpiReadRegisters(PCKT_FLT_OPTIONS_ADDR, 1, &tmpBuffer[0]);
282  pxPktBasicAddresses->xFilterOnBroadcastAddress = (SFunctionalState)((tmpBuffer[0] & DEST_VS_BROADCAST_ADDR_REGMASK) >> 3);
283  pxPktBasicAddresses->xFilterOnMulticastAddress = (SFunctionalState)((tmpBuffer[0] & DEST_VS_MULTICAST_ADDR_REGMASK) >> 2);
284  pxPktBasicAddresses->xFilterOnMyAddress = (SFunctionalState)((tmpBuffer[0] & DEST_VS_SOURCE_ADDR_REGMASK) >> 1);
285 }
286 
287 
294 {
295  uint8_t tmp;
296 
297  S2LPSpiReadRegisters(PCKTCTRL3_ADDR, 1, &tmp);
298 
299  /* Build the new value. Also set to 0 the direct RX mode bits */
300  tmp &= ~(PCKT_FRMT_REGMASK | RX_MODE_REGMASK);
301  tmp |= PKT_FORMAT_BASIC_CODE;
302  S2LPSpiWriteRegisters(PCKTCTRL3_ADDR, 1, &tmp);
303 
304  S2LPSpiReadRegisters(PCKTCTRL1_ADDR, 1, &tmp);
305 
306  /* Set to 0 the direct TX mode bits */
307  tmp &= ~TXSOURCE_REGMASK;
308  g_xStatus = S2LPSpiWriteRegisters(PCKTCTRL1_ADDR, 1, &tmp);
309 
311 }
312 
313 
321 {
322  uint8_t tmp;
323  s_assert_param(IS_SFUNCTIONAL_STATE(xAddressField));
324 
325  S2LPSpiReadRegisters(PCKTCTRL4_ADDR, 1, &tmp);
326  if(xAddressField==S_ENABLE) {
327  tmp |= ADDRESS_LEN_REGMASK;
328  }
329  else {
330  tmp &= ADDRESS_LEN_REGMASK;
331  }
332  g_xStatus = S2LPSpiWriteRegisters(PCKTCTRL4_ADDR, 1, &tmp);
333 
334 }
335 
336 
343 {
344  uint8_t tmp;
345 
346  g_xStatus = S2LPSpiReadRegisters(PCKTCTRL4_ADDR, 1, &tmp);
347  if(tmp & ADDRESS_LEN_REGMASK) {
348  return S_ENABLE;
349  }
350  else {
351  return S_DISABLE;
352  }
353 
354 }
355 
356 
366 void S2LPPktBasicSetPayloadLength(uint16_t nPayloadLength)
367 {
368  uint8_t tmpBuffer[2];
369 
371  nPayloadLength++;
372  }
373  tmpBuffer[0] = (uint8_t)(nPayloadLength>>8);
374  tmpBuffer[1] = (uint8_t)nPayloadLength;
375  g_xStatus = S2LPSpiWriteRegisters(PCKTLEN1_ADDR, 2, tmpBuffer);
376 }
377 
378 
389 {
390  uint8_t tmpBuffer[2];
391  uint16_t nPayloadLength;
392 
393  g_xStatus = S2LPSpiReadRegisters(PCKTLEN1_ADDR, 2, tmpBuffer);
394  nPayloadLength = (((uint16_t)tmpBuffer[0])<<8) | ((uint16_t)tmpBuffer[1]);
395 
397  nPayloadLength--;
398  }
399  return nPayloadLength;
400 }
401 
408 {
409  uint8_t tmpBuffer[2];
410  uint16_t nPayloadLength;
411 
412  g_xStatus = S2LPSpiReadRegisters(RX_PCKT_LEN1_ADDR, 2, tmpBuffer);
413  nPayloadLength = (((uint16_t)tmpBuffer[0])<<8) | ((uint16_t)tmpBuffer[1]);
414 
416  nPayloadLength--;
417  }
418  return nPayloadLength;
419 }
420 
421 
437 /******************* (C) COPYRIGHT 2016 STMicroelectronics *****END OF FILE****/
SFunctionalState xAddressField
SFunctionalState cExtendedPktLenField
SFunctionalState xDataWhitening
SFunctionalState xFilterOnMyAddress
uint16_t S2LPPktBasicGetReceivedPktLength(void)
Return the packet length field of the received packet.
#define MOD1_ADDR
MOD1 register.
Definition: S2LP_Regs.h:263
SFunctionalState
S2LP Functional state. Used to enable or disable a specific option.
Definition: S2LP_Types.h:67
uint16_t xPreambleLength
#define PROTOCOL1_ADDR
PROTOCOL1 register.
Definition: S2LP_Regs.h:685
void S2LPPktBasicGetAddressesInfo(PktBasicAddressesInit *pxPktBasicAddresses)
Return the S2LP Basic packet addresses structure according to the specified parameters in the registe...
S2LP Basic Packet Init structure definition.
void S2LPPktBasicSetFormat(void)
Configure the Basic packet format as packet used by S2LP.
#define PCKTCTRL6_ADDR
PCKTCTRL6 register.
Definition: S2LP_Regs.h:437
SFunctionalState xFilterOnBroadcastAddress
#define PCKTCTRL4_ADDR
PCKTCTRL4 register.
Definition: S2LP_Regs.h:467
#define PCKTCTRL2_ADDR
PCKTCTRL2 register.
Definition: S2LP_Regs.h:508
SFunctionalState xFilterOnMulticastAddress
S2LP Basic packet address structure definition. This structure allows users to specify the node/multi...
#define SYNC3_ADDR
SYNC3 register.
Definition: S2LP_Regs.h:573
Configuration and management of S2-LP Basic packets.
void S2LPPktBasicGetInfo(PktBasicInit *pxPktBasicInit)
Return the S2LP Basic packet structure according to the specified parameters in the registers.
void S2LPPktBasicAddressField(SFunctionalState xAddressField)
Set the address length for S2LP Basic packets.
Configuration and management of S2-LP WMbus packets.
uint32_t lSyncWords
#define PCKTCTRL1_ADDR
PCKTCTRL1 register.
Definition: S2LP_Regs.h:530
SFunctionalState xFixVarLength
void S2LPPktBasicAddressesInit(PktBasicAddressesInit *pxPktBasicAddresses)
Initialize the S2LP Basic packet addresses according to the specified parameters in the PktBasicAddre...
#define PCKTLEN1_ADDR
PCKTLEN1 register.
Definition: S2LP_Regs.h:547
void S2LPPktWMbusSetSubmode(WMbusSubmode xWMbusSubmode)
Set the W-MBus submode.
uint8_t xSyncLength
#define RX_PCKT_LEN1_ADDR
RX_PCKT_LEN1 register.
Definition: S2LP_Regs.h:1669
PktCrcMode xCrcMode
volatile S2LPStatus g_xStatus
S2LP Status global variable. This global variable of S2LPStatus type is updated on every SPI transact...
Definition: S2LP_Types.c:82
#define PCKTCTRL3_ADDR
PCKTCTRL3 register.
Definition: S2LP_Regs.h:485
#define PCKT_FLT_GOALS3_ADDR
PCKT_FLT_GOALS3 register.
Definition: S2LP_Regs.h:819
#define PCKT_FLT_GOALS2_ADDR
PCKT_FLT_GOALS2 register.
Definition: S2LP_Regs.h:832
void S2LPPktBasicInit(PktBasicInit *pxPktBasicInit)
Initialize the S2LP Basic packet according to the specified parameters in the PktBasicInit struct....
Definition: S2LP_PktBasic.c:83
#define S2LPPktBasicFilterOnCrc(xNewState)
Enables or Disables the CRC filtering.
#define PCKT_FLT_OPTIONS_ADDR
PCKT_FLT_OPTIONS register.
Definition: S2LP_Regs.h:788
SFunctionalState xFec
Header file for low level S2LP SPI driver.
void S2LPPktBasicSetPayloadLength(uint16_t nPayloadLength)
Set the payload length for S2LP Basic packets. Since the packet length depends from the address and t...
SFunctionalState S2LPPktBasicGetAddressField(void)
Specify if the Address field for S2LP Basic packets is enabled or disabled.
uint16_t S2LPPktBasicGetPayloadLength(void)
Return the payload length for S2LP Basic packets. Since the packet length depends from the address an...
PktCrcMode BasicCrcMode
CRC length in bytes enumeration.