Appiko
S2LP_Csma.c
Go to the documentation of this file.
1 
24 /* Includes ------------------------------------------------------------------*/
25 #include "S2LP_Csma.h"
26 #include "MCU_Interface.h"
27 
28 
65 #define IS_CCA_PERIOD(PERIOD) (PERIOD == CSMA_PERIOD_64TBIT || \
66  PERIOD == CSMA_PERIOD_128TBIT || \
67  PERIOD == CSMA_PERIOD_256TBIT || \
68  PERIOD == CSMA_PERIOD_512TBIT)
69 
70 #define IS_CSMA_LENGTH(LENGTH) (LENGTH < 16)
71 
72 #define IS_BU_COUNTER_SEED(SEED) (SEED!=0)
73 #define IS_BU_PRESCALER(PRESCALER) (PRESCALER<64)
74 #define IS_CMAX_NB(NB) (NB<8)
75 
114 void S2LPCsmaInit(SCsmaInit* pxCsmaInit)
115 {
116  uint8_t tmpBuffer[4];
117 
118  /* Check the parameters */
119  s_assert_param(IS_SFUNCTIONAL_STATE(pxCsmaInit->xCsmaPersistentMode));
120  s_assert_param(IS_CCA_PERIOD(pxCsmaInit->xMultiplierTbit));
121  s_assert_param(IS_CSMA_LENGTH(pxCsmaInit->xCcaLength));
122  s_assert_param(IS_BU_COUNTER_SEED(pxCsmaInit->nBuCounterSeed));
123  s_assert_param(IS_BU_PRESCALER(pxCsmaInit->cBuPrescaler));
124  s_assert_param(IS_CMAX_NB(pxCsmaInit->cMaxNb));
125 
126  /* CSMA BU counter seed (MSB) config */
127  tmpBuffer[0] = (uint8_t)(pxCsmaInit->nBuCounterSeed >> 8);
128 
129  /* CSMA BU counter seed (LSB) config */
130  tmpBuffer[1] = (uint8_t) pxCsmaInit->nBuCounterSeed;
131 
132  /* CSMA BU prescaler config and CCA period config */
133  tmpBuffer[2] = (pxCsmaInit->cBuPrescaler << 2) | pxCsmaInit->xMultiplierTbit;
134 
135  /* CSMA CCA length config and max number of back-off */
136  tmpBuffer[3] = (pxCsmaInit->xCcaLength<<4) | pxCsmaInit->cMaxNb;
137 
138  /* Reads the PROTOCOL1_BASE register value, to write the SEED_RELOAD and CSMA_PERS_ON fields */
139  S2LPSpiWriteRegisters(CSMA_CONF3_ADDR, 4, tmpBuffer);
140 
141  S2LPSpiReadRegisters(PROTOCOL1_ADDR, 1, &tmpBuffer[0]);
142 
143  /* Writes the new value for persistent mode */
144  if(pxCsmaInit->xCsmaPersistentMode==S_ENABLE) {
145  tmpBuffer[0] |= CSMA_PERS_ON_REGMASK;
146  }
147  else {
148  tmpBuffer[0] &= ~CSMA_PERS_ON_REGMASK;
149  }
150 
151  /* Writes PROTOCOL1_BASE register */
152  g_xStatus = S2LPSpiWriteRegisters(PROTOCOL1_ADDR, 1, &tmpBuffer[0]);
153 
154 
155 }
156 
157 
164 void S2LPCsmaGetInfo(SCsmaInit* pxSCsmaInit)
165 {
166  uint8_t tmpBuffer[4];
167 
168  /* Reads PROTOCOL1_BASE register */
169  S2LPSpiReadRegisters(PROTOCOL1_ADDR, 1, &tmpBuffer[0]);
170 
171  /* Reads the persistent mode enable bit */
172  pxSCsmaInit->xCsmaPersistentMode = (SFunctionalState)((tmpBuffer[0]&CSMA_PERS_ON_REGMASK) >> 1);
173 
174  /* Reads CSMA_CONFIGx_BASE registers */
175  g_xStatus = S2LPSpiReadRegisters(CSMA_CONF3_ADDR, 4, tmpBuffer);
176 
177  /* Reads the bu counter seed */
178  pxSCsmaInit->nBuCounterSeed = (((uint16_t)tmpBuffer[0]) << 8) | (uint16_t)tmpBuffer[1];
179 
180  /* Reads the bu prescaler */
181  pxSCsmaInit->cBuPrescaler = tmpBuffer[2]>>2;
182 
183  /* Reads the Cca period */
184  pxSCsmaInit->xMultiplierTbit = (SCsmaPeriod)(tmpBuffer[2] & CCA_PERIOD_REGMASK);
185 
186  /* Reads the Cca length */
187  pxSCsmaInit->xCcaLength = (tmpBuffer[3]&CCA_LEN_REGMASK)>>4;
188 
189  /* Reads the max number of back off */
190  pxSCsmaInit->cMaxNb = tmpBuffer[3] & NBACKOFF_MAX_REGMASK;
191 
192 }
193 
194 
201 void S2LPCsma(SFunctionalState xNewState)
202 {
203  uint8_t tmp, tmp2;
204  s_assert_param(IS_SFUNCTIONAL_STATE(xNewState));
205 
206  S2LPSpiReadRegisters(PROTOCOL1_ADDR, 1, &tmp);
207 
208  /* Set or reSet the CSMA enable bit */
209  if(xNewState==S_ENABLE)
210  {
211  tmp |= CSMA_ON_REGMASK;
212 
213  S2LPSpiReadRegisters(PM_CONF0_ADDR, 1, &tmp2);
214  tmp2 |= SLEEP_MODE_SEL_REGMASK;
215  S2LPSpiWriteRegisters(PM_CONF0_ADDR, 1, &tmp2);
216  }
217  else
218  {
219  tmp &= ~CSMA_ON_REGMASK;
220  }
221 
222  g_xStatus = S2LPSpiWriteRegisters(PROTOCOL1_ADDR, 1, &tmp);
223 
224 }
225 
232 {
233  uint8_t tmp;
234 
235  /* Reads the PROTOCOL1 register value */
236  g_xStatus = S2LPSpiReadRegisters(PROTOCOL1_ADDR, 1, &tmp);
237 
238  /* Return if set or reset */
239  if(tmp & CSMA_ON_REGMASK) {
240  return S_ENABLE;
241  }
242  else {
243  return S_DISABLE;
244  }
245 
246 }
247 
255 {
256  uint8_t tmp;
257  s_assert_param(IS_SFUNCTIONAL_STATE(xNewState));
258 
259  S2LPSpiReadRegisters(PROTOCOL1_ADDR, 1, &tmp);
260 
261  /* Enables/disables the CSMA persistent mode */
262  if(xNewState==S_ENABLE) {
263  tmp |= CSMA_PERS_ON_REGMASK;
264  }
265  else {
266  tmp &= ~CSMA_PERS_ON_REGMASK;
267  }
268 
269  g_xStatus = S2LPSpiWriteRegisters(PROTOCOL1_ADDR, 1, &tmp);
270 
271 }
272 
273 
280 {
281  uint8_t tmp;
282 
283  g_xStatus = S2LPSpiReadRegisters(PROTOCOL1_ADDR, 1, &tmp);
284 
285  if(tmp & CSMA_PERS_ON_REGMASK) {
286  return S_ENABLE;
287  }
288  else {
289  return S_DISABLE;
290  }
291 
292 }
293 
294 
302 {
303  uint8_t tmp;
304  s_assert_param(IS_SFUNCTIONAL_STATE(xNewState));
305 
306  S2LPSpiReadRegisters(PROTOCOL1_ADDR, 1, &tmp);
307 
308  /* Enables/disables the seed reload mode */
309  if(xNewState==S_ENABLE) {
310  tmp |= SEED_RELOAD_REGMASK;
311  }
312  else {
313  tmp &= ~SEED_RELOAD_REGMASK;
314  }
315 
316  g_xStatus = S2LPSpiWriteRegisters(PROTOCOL1_ADDR, 1, &tmp);
317 
318 }
319 
320 
327 {
328  uint8_t tmp;
329 
330  g_xStatus = S2LPSpiReadRegisters(PROTOCOL1_ADDR, 1, &tmp);
331 
332  /* Return if set or reset */
333  if(tmp & SEED_RELOAD_REGMASK) {
334  return S_ENABLE;
335  }
336  else {
337  return S_DISABLE;
338  }
339 }
340 
341 
348 void S2LPCsmaSetBuCounterSeed(uint16_t nBuCounterSeed)
349 {
350  uint8_t tmpBuffer[2];
351  s_assert_param(IS_BU_COUNTER_SEED(nBuCounterSeed));
352 
353  tmpBuffer[0] = (uint8_t)(nBuCounterSeed>>8);
354  tmpBuffer[1] = (uint8_t)nBuCounterSeed;
355 
356  g_xStatus = S2LPSpiWriteRegisters(CSMA_CONF3_ADDR, 2, tmpBuffer);
357 
358 }
359 
366 {
367  uint8_t tmpBuffer[2];
368 
369  g_xStatus = S2LPSpiReadRegisters(CSMA_CONF3_ADDR, 2, tmpBuffer);
370 
371  return ((((uint16_t)tmpBuffer[0])<<8) + (uint16_t)tmpBuffer[1]);
372 }
373 
374 
381 void S2LPCsmaSetBuPrescaler(uint8_t cBuPrescaler)
382 {
383  uint8_t tmp;
384  s_assert_param(IS_BU_PRESCALER(cBuPrescaler));
385 
386  S2LPSpiReadRegisters(CSMA_CONF1_ADDR, 1, &tmp);
387 
388  tmp &= ~BU_PRSC_REGMASK;
389  tmp |= (cBuPrescaler<<2);
390 
391  g_xStatus = S2LPSpiWriteRegisters(CSMA_CONF1_ADDR, 1, &tmp);
392 }
393 
394 
401 {
402  uint8_t tmp;
403 
404  g_xStatus = S2LPSpiReadRegisters(CSMA_CONF1_ADDR, 1, &tmp);
405 
406  return (tmp >> 2);
407 }
408 
409 
416 void S2LPCsmaSetCcaPeriod(SCsmaPeriod xMultiplierTbit)
417 {
418  uint8_t tmp;
419  s_assert_param(IS_CCA_PERIOD(xMultiplierTbit));
420 
421  S2LPSpiReadRegisters(CSMA_CONF1_ADDR, 1, &tmp);
422 
423  tmp &= ~CCA_PERIOD_REGMASK;
424  tmp |= xMultiplierTbit;
425 
426  g_xStatus = S2LPSpiWriteRegisters(CSMA_CONF1_ADDR, 1, &tmp);
427 
428 }
429 
430 
436 uint8_t S2LPCsmaGetCcaPeriod(void)
437 {
438  uint8_t tmp;
439 
440  g_xStatus = S2LPSpiReadRegisters(CSMA_CONF1_ADDR, 1, &tmp);
441 
442  return (SCsmaPeriod)(tmp & CCA_PERIOD_REGMASK);
443 }
444 
445 
452 void S2LPCsmaSetCcaLength(uint8_t xCcaLength)
453 {
454  uint8_t tmp;
455  s_assert_param(IS_CSMA_LENGTH(xCcaLength));
456 
457  S2LPSpiReadRegisters(CSMA_CONF0_ADDR, 1, &tmp);
458 
459  tmp &= ~CCA_LEN_REGMASK;
460  tmp |= xCcaLength;
461 
462  g_xStatus = S2LPSpiWriteRegisters(CSMA_CONF0_ADDR, 1, &tmp);
463 
464 }
465 
466 
472 uint8_t S2LPCsmaGetCcaLength(void)
473 {
474  uint8_t tmp;
475 
476  g_xStatus = S2LPSpiReadRegisters(CSMA_CONF0_ADDR, 1, &tmp);
477 
478  return (tmp >> 4);
479 }
480 
481 
488 void S2LPCsmaSetMaxNumberBackoff(uint8_t cMaxNb)
489 {
490  uint8_t tmp;
491  s_assert_param(IS_CMAX_NB(cMaxNb));
492 
493  S2LPSpiReadRegisters(CSMA_CONF0_ADDR, 1, &tmp);
494 
495  tmp &= ~NBACKOFF_MAX_REGMASK;
496  tmp |= cMaxNb;
497 
498  g_xStatus = S2LPSpiWriteRegisters(CSMA_CONF0_ADDR, 1, &tmp);
499 }
500 
507 {
508  uint8_t tmp;
509 
510  g_xStatus = S2LPSpiReadRegisters(CSMA_CONF0_ADDR, 1, &tmp);
511 
512  return (tmp & NBACKOFF_MAX_REGMASK);
513 
514 }
515 
516 
532 /******************* (C) COPYRIGHT 2016 STMicroelectronics *****END OF FILE****/
#define CSMA_CONF0_ADDR
CSMA_CONF0 register.
Definition: S2LP_Regs.h:992
void S2LPCsmaSetBuPrescaler(uint8_t cBuPrescaler)
Set the BU prescaler. The CSMA back off time is given by the formula: BO = rand(2^NB)*BU.
Definition: S2LP_Csma.c:381
uint8_t cBuPrescaler
Definition: S2LP_Csma.h:107
Configuration and management of S2-LP CSMA.
uint8_t S2LPCsmaGetCcaPeriod(void)
Return the CCA period.
Definition: S2LP_Csma.c:436
SFunctionalState xCsmaPersistentMode
Definition: S2LP_Csma.h:102
uint8_t S2LPCsmaGetMaxNumberBackoff(void)
Return the max number of back-off.
Definition: S2LP_Csma.c:506
#define CSMA_CONF1_ADDR
CSMA_CONF1 register.
Definition: S2LP_Regs.h:976
uint8_t xCcaLength
Definition: S2LP_Csma.h:104
SFunctionalState
S2LP Functional state. Used to enable or disable a specific option.
Definition: S2LP_Types.h:67
uint16_t S2LPCsmaGetBuCounterSeed(void)
Return the BU counter seed (BU_COUNTER_SEED register).
Definition: S2LP_Csma.c:365
#define PROTOCOL1_ADDR
PROTOCOL1 register.
Definition: S2LP_Regs.h:685
void S2LPCsmaSetMaxNumberBackoff(uint8_t cMaxNb)
Set the max number of back-off. If reached S2LP stops the transmission.
Definition: S2LP_Csma.c:488
SCsmaPeriod xMultiplierTbit
Definition: S2LP_Csma.h:103
uint16_t nBuCounterSeed
Definition: S2LP_Csma.h:106
void S2LPCsmaSeedReloadMode(SFunctionalState xNewState)
Enables or Disables the seed reload mode (if enabled it reloads the back-off generator seed using the...
Definition: S2LP_Csma.c:301
void S2LPCsmaGetInfo(SCsmaInit *pxSCsmaInit)
Return the fitted structure SCsmaInit starting from the registers values.
Definition: S2LP_Csma.c:164
void S2LPCsma(SFunctionalState xNewState)
Enable or Disables the CSMA.
Definition: S2LP_Csma.c:201
void S2LPCsmaPersistentMode(SFunctionalState xNewState)
Enables or Disables the persistent CSMA mode.
Definition: S2LP_Csma.c:254
SCsmaPeriod
Multiplier for Tcca time enumeration (Tcca = Multiplier*Tbit).
Definition: S2LP_Csma.h:90
void S2LPCsmaInit(SCsmaInit *pxCsmaInit)
Initialize the S2LP CSMA according to the specified parameters in the SCsmaInit.
Definition: S2LP_Csma.c:114
SFunctionalState S2LPCsmaGetSeedReloadMode(void)
Gets the seed reload mode.
Definition: S2LP_Csma.c:326
uint8_t cMaxNb
Definition: S2LP_Csma.h:105
#define CSMA_CONF3_ADDR
CSMA_CONF3 register.
Definition: S2LP_Regs.h:949
#define PM_CONF0_ADDR
PM_CONF0 register.
Definition: S2LP_Regs.h:1447
SFunctionalState S2LPCsmaGetCsma(void)
Gets the CSMA mode. Says if it is enabled or disabled.
Definition: S2LP_Csma.c:231
uint8_t S2LPCsmaGetBuPrescaler(void)
Return the BU prescaler.
Definition: S2LP_Csma.c:400
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
SFunctionalState S2LPCsmaGetPersistentMode(void)
Gets the persistent CSMA mode.
Definition: S2LP_Csma.c:279
void S2LPCsmaSetCcaLength(uint8_t xCcaLength)
Set the CCA length.
Definition: S2LP_Csma.c:452
uint8_t S2LPCsmaGetCcaLength(void)
Return the CCA length.
Definition: S2LP_Csma.c:472
Header file for low level S2LP SPI driver.
void S2LPCsmaSetCcaPeriod(SCsmaPeriod xMultiplierTbit)
Set the CCA period.
Definition: S2LP_Csma.c:416
void S2LPCsmaSetBuCounterSeed(uint16_t nBuCounterSeed)
Set the BU counter seed (BU_COUNTER_SEED register). The CSMA back off time is given by the formula: B...
Definition: S2LP_Csma.c:348
S2LP CSMA Init structure definition.
Definition: S2LP_Csma.h:101