Added support for the WNC M14A2A Cellular LTE Data Module.

Dependencies:   WNC14A2AInterface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SPIRIT_Csma.c Source File

SPIRIT_Csma.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    SPIRIT_Csma.c
00004   * @author  VMA division - AMS
00005   * @version 3.2.2
00006   * @date    08-July-2015
00007   * @brief   Configuration and management of SPIRIT CSMA.
00008   * @details
00009   * @attention
00010   *
00011   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00012   *
00013   * Redistribution and use in source and binary forms, with or without modification,
00014   * are permitted provided that the following conditions are met:
00015   *   1. Redistributions of source code must retain the above copyright notice,
00016   *      this list of conditions and the following disclaimer.
00017   *   2. Redistributions in binary form must reproduce the above copyright notice,
00018   *      this list of conditions and the following disclaimer in the documentation
00019   *      and/or other materials provided with the distribution.
00020   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021   *      may be used to endorse or promote products derived from this software
00022   *      without specific prior written permission.
00023   *
00024   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034   *
00035   ******************************************************************************
00036   */
00037 
00038 /* Includes ------------------------------------------------------------------*/
00039 #include "SPIRIT_Csma.h"
00040 #include "MCU_Interface.h"
00041 
00042 
00043 /**
00044  * @addtogroup SPIRIT_Libraries
00045  * @{
00046  */
00047 
00048 
00049 /**
00050  * @addtogroup SPIRIT_Csma
00051  * @{
00052  */
00053 
00054 
00055 /**
00056  * @defgroup Csma_Private_TypesDefinitions      CSMA Private TypesDefinitions
00057  * @{
00058  */
00059 
00060 
00061 /**
00062  *@}
00063  */
00064 
00065 
00066 /**
00067  * @defgroup Csma_Private_Defines               CSMA Private Defines
00068  * @{
00069  */
00070 
00071 /**
00072  *@}
00073  */
00074 
00075 
00076 /**
00077  * @defgroup Csma_Private_Macros               CSMA Private Macros
00078  * @{
00079  */
00080 
00081 /**
00082  *@}
00083  */
00084 
00085 
00086 /**
00087  * @defgroup Csma_Private_Variables             CSMA Private Variables
00088  * @{
00089  */
00090 
00091 /**
00092  *@}
00093  */
00094 
00095 
00096 
00097 /**
00098  * @defgroup Csma_Private_FunctionPrototypes    CSMA Private FunctionPrototypes
00099  * @{
00100  */
00101 
00102 /**
00103  *@}
00104  */
00105 
00106 
00107 /**
00108  * @defgroup Csma_Private_Functions             CSMA Private Functions
00109  * @{
00110  */
00111 
00112 
00113 /**
00114  * @brief  Initializes the SPIRIT CSMA according to the specified parameters in the CsmaInit.
00115  * @param  pxCsmaInit Csma init structure.
00116  *         This parameter is a pointer to @ref CsmaInit.
00117  * @retval None.
00118  */
00119 void SpiritCsmaInit(CsmaInit* pxCsmaInit)
00120 {
00121   uint8_t tempRegValue[5];
00122 
00123   /* Check the parameters */
00124   s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(pxCsmaInit->xCsmaPersistentMode ));
00125   s_assert_param(IS_CCA_PERIOD(pxCsmaInit->xMultiplierTbit ));
00126   s_assert_param(IS_CSMA_LENGTH(pxCsmaInit->xCcaLength ));
00127   s_assert_param(IS_BU_COUNTER_SEED(pxCsmaInit->nBuCounterSeed ));
00128   s_assert_param(IS_BU_PRESCALER(pxCsmaInit->cBuPrescaler ));
00129   s_assert_param(IS_CMAX_NB(pxCsmaInit->cMaxNb ));
00130 
00131   /* CSMA BU counter seed (MSB) config */
00132   tempRegValue[0] = (uint8_t)(pxCsmaInit->nBuCounterSeed  >> 8);
00133 
00134   /* CSMA BU counter seed (LSB) config */
00135   tempRegValue[1] = (uint8_t) pxCsmaInit->nBuCounterSeed ;
00136 
00137   /* CSMA BU prescaler config and CCA period config */
00138   tempRegValue[2] = (pxCsmaInit->cBuPrescaler  << 2) | pxCsmaInit->xMultiplierTbit ;
00139 
00140   /* CSMA CCA length config and max number of back-off */
00141   tempRegValue[3] = (pxCsmaInit->xCcaLength  | pxCsmaInit->cMaxNb );
00142 
00143   /* Reads the PROTOCOL1_BASE register value, to write the SEED_RELOAD and CSMA_PERS_ON fields */
00144   g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue[4]);
00145 
00146   /* Writes the new value for persistent mode */
00147   if(pxCsmaInit->xCsmaPersistentMode ==S_ENABLE)
00148   {
00149     tempRegValue[4] |= PROTOCOL1_CSMA_PERS_ON_MASK;
00150   }
00151   else
00152   {
00153     tempRegValue[4] &= ~PROTOCOL1_CSMA_PERS_ON_MASK;
00154   }
00155 
00156   /* Writes PROTOCOL1_BASE register */
00157   g_xStatus = SpiritSpiWriteRegisters(PROTOCOL1_BASE, 1, &tempRegValue[4]);
00158 
00159   /* Writes CSMA_CONFIGx_BASE registers */
00160   g_xStatus = SpiritSpiWriteRegisters(CSMA_CONFIG3_BASE, 4, tempRegValue);
00161 
00162 }
00163 
00164 
00165  /**
00166   * @brief  Returns the fitted structure CsmaInit starting from the registers values.
00167   * @param  pxCsmaInit Csma structure to be fitted.
00168   *         This parameter is a pointer to @ref CsmaInit.
00169   * @retval None.
00170   */
00171 void SpiritCsmaGetInfo(CsmaInit* pxCsmaInit)
00172 {
00173    uint8_t tempRegValue[5];
00174 
00175    /* Reads PROTOCOL1_BASE register */
00176    g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue[4]);
00177 
00178    /* Reads CSMA_CONFIGx_BASE registers */
00179    g_xStatus = SpiritSpiReadRegisters(CSMA_CONFIG3_BASE, 4, tempRegValue);
00180 
00181    /* Reads the bu counter seed */
00182    pxCsmaInit->nBuCounterSeed  = (uint16_t)tempRegValue[1] | ((uint16_t)(tempRegValue[0] << 8));
00183 
00184    /* Reads the bu prescaler */
00185    pxCsmaInit->cBuPrescaler  = tempRegValue[2]>>2;
00186 
00187    /* Reads the Cca period */
00188    pxCsmaInit->xMultiplierTbit  = (CcaPeriod)(tempRegValue[2] & 0x03);
00189 
00190    /* Reads the Cca length */
00191    pxCsmaInit->xCcaLength  = (CsmaLength)(tempRegValue[3]&0xF0);
00192 
00193    /* Reads the max number of back off */
00194    pxCsmaInit->cMaxNb  = tempRegValue[3] & 0x07;
00195 
00196    /* Reads the persistent mode enable bit */
00197    pxCsmaInit->xCsmaPersistentMode  = (SpiritFunctionalState)((tempRegValue[4]>>1) & 0x01);
00198 
00199 }
00200 
00201 
00202 /**
00203  * @brief  Enables or Disables the CSMA.
00204  * @param  xNewState the state of the CSMA mode.
00205  *         This parameter can be: S_ENABLE or S_DISABLE.
00206  * @retval None.
00207  */
00208 void SpiritCsma(SpiritFunctionalState xNewState)
00209 {
00210   uint8_t tempRegValue;
00211 
00212   /* Check the parameters */
00213   s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(xNewState));
00214 
00215   /* Reads the PROTOCOL1 register value */
00216   g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
00217 
00218   /* Sets or resets the CSMA enable bit */
00219   if(xNewState==S_ENABLE)
00220   {
00221     tempRegValue |= PROTOCOL1_CSMA_ON_MASK;
00222   }
00223   else
00224   {
00225     tempRegValue &= ~PROTOCOL1_CSMA_ON_MASK;
00226   }
00227 
00228   /* Writes the new value on the PROTOCOL1 register */
00229   g_xStatus = SpiritSpiWriteRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
00230 
00231 }
00232 
00233 /**
00234  * @brief  Gets the CSMA mode. Says if it is enabled or disabled.
00235  * @param  None.
00236  * @retval SpiritFunctionalState: CSMA mode.
00237  */
00238 SpiritFunctionalState SpiritCsmaGetCsma(void)
00239 {
00240   uint8_t tempRegValue;
00241 
00242   /* Reads the PROTOCOL1 register value */
00243   g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
00244 
00245   /* Return if set or reset */
00246   if(tempRegValue & PROTOCOL1_CSMA_ON_MASK)
00247   {
00248     return S_ENABLE;
00249   }
00250   else
00251   {
00252     return S_DISABLE;
00253   }
00254 
00255 }
00256 
00257 /**
00258  * @brief  Enables or Disables the persistent CSMA mode.
00259  * @param  xNewState the state of the persistent CSMA mode.
00260  *         This parameter can be: S_ENABLE or S_DISABLE.
00261  * @retval None.
00262  */
00263 void SpiritCsmaPersistentMode(SpiritFunctionalState xNewState)
00264 {
00265   uint8_t tempRegValue;
00266 
00267   /* Check the parameters */
00268   s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(xNewState));
00269 
00270   /* Reads the PROTOCOL1 register value */
00271   g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
00272 
00273   /* Enables/disables the CSMA persistent mode */
00274   if(xNewState==S_ENABLE)
00275   {
00276     tempRegValue |= PROTOCOL1_CSMA_PERS_ON_MASK;
00277   }
00278   else
00279   {
00280     tempRegValue &= ~PROTOCOL1_CSMA_PERS_ON_MASK;
00281   }
00282 
00283   /* Writes the new vaue on the PROTOCOL1 register */
00284   g_xStatus = SpiritSpiWriteRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
00285 
00286 }
00287 
00288 
00289 /**
00290  * @brief  Gets the persistent CSMA mode.
00291  * @param  None.
00292  * @retval SpiritFunctionalState: CSMA persistent mode.
00293  */
00294 SpiritFunctionalState SpiritCsmaGetPersistentMode(void)
00295 {
00296   uint8_t tempRegValue;
00297 
00298   /* Reads the PROTOCOL1 register value */
00299   g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
00300 
00301   /* Return if set or reset */
00302   if(tempRegValue & PROTOCOL1_CSMA_PERS_ON_MASK)
00303   {
00304     return S_ENABLE;
00305   }
00306   else
00307   {
00308     return S_DISABLE;
00309   }
00310 
00311 }
00312 
00313 
00314 /**
00315  * @brief  Enables or Disables the seed reload mode (if enabled it reloads the back-off generator seed using the value written in the BU_COUNTER_SEED register).
00316  * @param  xNewState the state of the seed reload mode.
00317  *     This parameter can be: S_ENABLE or S_DISABLE.
00318  * @retval None.
00319  */
00320 void SpiritCsmaSeedReloadMode(SpiritFunctionalState xNewState)
00321 {
00322   uint8_t tempRegValue;
00323 
00324   /* Check the parameters */
00325   s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(xNewState));
00326 
00327   /* Reads the PROTOCOL1 register value */
00328   g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
00329 
00330   /* Enables/disables the seed reload mode */
00331   if(xNewState==S_ENABLE)
00332   {
00333     tempRegValue |= PROTOCOL1_SEED_RELOAD_MASK;
00334   }
00335   else
00336   {
00337     tempRegValue &= ~PROTOCOL1_SEED_RELOAD_MASK;
00338   }
00339 
00340   /* Writes the new value on the PROTOCOL1 register */
00341   g_xStatus = SpiritSpiWriteRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
00342 
00343 }
00344 
00345 
00346 /**
00347  * @brief  Gets the seed reload mode.
00348  * @param  None.
00349  * @retval SpiritFunctionalState: CSMA seed reload mode.
00350  */
00351 SpiritFunctionalState SpiritCsmaGetSeedReloadMode(void)
00352 {
00353   uint8_t tempRegValue;
00354 
00355   /* Reads the PROTOCOL1 register value */
00356   g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
00357 
00358   /* Return if set or reset */
00359   if(tempRegValue & PROTOCOL1_SEED_RELOAD_MASK)
00360   {
00361     return S_ENABLE;
00362   }
00363   else
00364   {
00365     return S_DISABLE;
00366   }
00367 }
00368 
00369 
00370 /**
00371  * @brief  Sets the BU counter seed (BU_COUNTER_SEED register). The CSMA back off time is given by the formula: BO = rand(2^NB)*BU.
00372  * @param  nBuCounterSeed seed of the random number generator used to apply the BBE algorithm.
00373  *     This parameter is an uint16_t.
00374  * @retval None.
00375  */
00376 void SpiritCsmaSetBuCounterSeed(uint16_t nBuCounterSeed)
00377 {
00378   uint8_t tempRegValue[2];
00379 
00380   /* Check parameters */
00381   s_assert_param(IS_BU_COUNTER_SEED(nBuCounterSeed));
00382 
00383   /* Build value (MSB)*/
00384   tempRegValue[0]=(uint8_t)(nBuCounterSeed>>8);
00385   /* Build value (LSB) */
00386   tempRegValue[1]=(uint8_t)nBuCounterSeed;
00387 
00388   /* Writes the CSMA_CONFIG3 registers */
00389   g_xStatus = SpiritSpiWriteRegisters(CSMA_CONFIG3_BASE, 2, tempRegValue);
00390 
00391 }
00392 
00393 /**
00394  * @brief  Returns the BU counter seed (BU_COUNTER_SEED register).
00395  * @param  None.
00396  * @retval uint16_t Seed of the random number generator used to apply the BBE algorithm.
00397  */
00398 uint16_t SpiritCsmaGetBuCounterSeed(void)
00399 {
00400   uint8_t tempRegValue[2];
00401 
00402   /* Reads the CSMA_CONFIGx registers value */
00403   g_xStatus = SpiritSpiReadRegisters(CSMA_CONFIG3_BASE, 2, tempRegValue);
00404 
00405   /* Build the counter seed and return it */
00406   return ((uint16_t)tempRegValue[1] + (((uint16_t)tempRegValue[0])<<8));
00407 
00408 }
00409 
00410 
00411 /**
00412  * @brief  Sets the BU prescaler. The CSMA back off time is given by the formula: BO = rand(2^NB)*BU.
00413  * @param  cBuPrescaler used to program the back-off unit BU.
00414  *     This parameter is an uint8_t.
00415  * @retval None.
00416  */
00417 void SpiritCsmaSetBuPrescaler(uint8_t cBuPrescaler)
00418 {
00419   uint8_t tempRegValue;
00420 
00421   /* Check parameters */
00422   s_assert_param(IS_BU_PRESCALER(cBuPrescaler));
00423 
00424   /* Reads the CSMA_CONFIG1 register value */
00425   g_xStatus = SpiritSpiReadRegisters(CSMA_CONFIG1_BASE, 1, &tempRegValue);
00426 
00427   /* Build the new value for the BU prescaler */
00428   tempRegValue &= 0x03;
00429   tempRegValue |= (cBuPrescaler<<2);
00430 
00431   /* Writes the new value on the CSMA_CONFIG1_BASE register */
00432   g_xStatus = SpiritSpiWriteRegisters(CSMA_CONFIG1_BASE, 1, &tempRegValue);
00433 
00434 }
00435 
00436 
00437 /**
00438  * @brief  Returns the BU prescaler.
00439  * @param  None.
00440  * @retval uint8_t Value back-off unit (BU).
00441  */
00442 uint8_t SpiritCsmaGetBuPrescaler(void)
00443 {
00444   uint8_t tempRegValue;
00445 
00446   /* Reads the CSMA_CONFIG1 register value */
00447   g_xStatus = SpiritSpiReadRegisters(CSMA_CONFIG1_BASE, 1, &tempRegValue);
00448 
00449   /* Build and return the BU prescaler value */
00450   return (tempRegValue >> 2);
00451 
00452 }
00453 
00454 
00455 /**
00456  * @brief  Sets the CCA period.
00457  * @param  xMultiplierTbit value of CCA period to store.
00458  *     This parameter can be a value of @ref CcaPeriod.
00459  * @retval None.
00460  */
00461 void SpiritCsmaSetCcaPeriod(CcaPeriod xMultiplierTbit)
00462 {
00463   uint8_t tempRegValue;
00464 
00465   /* Check the parameters */
00466   s_assert_param(IS_CCA_PERIOD(xMultiplierTbit));
00467 
00468   /* Reads the CSMA_CONFIG1 register value */
00469   g_xStatus = SpiritSpiReadRegisters(CSMA_CONFIG1_BASE, 1, &tempRegValue);
00470 
00471   /* Build the new value setting the the CCA period */
00472   tempRegValue &= 0xFC;
00473   tempRegValue |= xMultiplierTbit;
00474 
00475   /* Writes the new value on the CSMA_CONFIG1 register */
00476   g_xStatus = SpiritSpiWriteRegisters(CSMA_CONFIG1_BASE, 1, &tempRegValue);
00477 
00478 }
00479 
00480 
00481 /**
00482  * @brief  Returns the CCA period.
00483  * @param  None.
00484  * @retval CcaPeriod CCA period.
00485  */
00486 CcaPeriod SpiritCsmaGetCcaPeriod(void)
00487 {
00488   uint8_t tempRegValue;
00489 
00490   /* Reads the CSMA_CONFIG1 register value */
00491   g_xStatus = SpiritSpiReadRegisters(CSMA_CONFIG1_BASE, 1, &tempRegValue);
00492 
00493   /* Build and return the CCA period value */
00494   return (CcaPeriod)(tempRegValue & 0x03);
00495 
00496 }
00497 
00498 
00499 /**
00500  * @brief  Sets the CCA length.
00501  * @param  xCcaLength the CCA length (a value between 1 and 15 that multiplies the CCA period).
00502  *     This parameter can be any value of @ref CsmaLength.
00503  * @retval None.
00504  */
00505 void SpiritCsmaSetCcaLength(CsmaLength xCcaLength)
00506 {
00507   uint8_t tempRegValue;
00508 
00509   /* Check the parameters */
00510   s_assert_param(IS_CSMA_LENGTH(xCcaLength));
00511 
00512   /* Reads the CSMA_CONFIG0 register value */
00513   g_xStatus = SpiritSpiReadRegisters(CSMA_CONFIG0_BASE, 1, &tempRegValue);
00514 
00515   /* Build the value of CCA length to be set */
00516   tempRegValue &= 0x0F;
00517   tempRegValue |= xCcaLength;
00518 
00519   /* Writes the new value on the CSMA_CONFIG0 register */
00520   g_xStatus = SpiritSpiWriteRegisters(CSMA_CONFIG0_BASE, 1, &tempRegValue);
00521 
00522 }
00523 
00524 
00525 /**
00526  * @brief  Returns the CCA length.
00527  * @param  None.
00528  * @retval uint8_t CCA length.
00529  */
00530 uint8_t SpiritCsmaGetCcaLength(void)
00531 {
00532   uint8_t tempRegValue;
00533 
00534   /* Reads the CSMA_CONFIG0 register value */
00535   g_xStatus = SpiritSpiReadRegisters(CSMA_CONFIG0_BASE, 1, &tempRegValue);
00536 
00537   /* Build and return the CCA length */
00538   return tempRegValue >> 4;
00539 
00540 }
00541 
00542 
00543 /**
00544  * @brief  Sets the max number of back-off. If reached Spirit stops the transmission.
00545  * @param  cMaxNb the max number of back-off.
00546  *         This parameter is an uint8_t.
00547  * @retval None.
00548  */
00549 void SpiritCsmaSetMaxNumberBackoff(uint8_t cMaxNb)
00550 {
00551   uint8_t tempRegValue;
00552 
00553   /* Check the parameters */
00554   s_assert_param(IS_CMAX_NB(cMaxNb));
00555 
00556   /* Reads the CSMA_CONFIG0 register value */
00557   g_xStatus = SpiritSpiReadRegisters(CSMA_CONFIG0_BASE, 1, &tempRegValue);
00558 
00559   /* Build the value of max back off to be set */
00560   tempRegValue &= 0xF8;
00561   tempRegValue |= cMaxNb;
00562 
00563   /* Writes the new value on the CSMA_CONFIG0 register */
00564   g_xStatus = SpiritSpiWriteRegisters(CSMA_CONFIG0_BASE, 1, &tempRegValue);
00565 }
00566 
00567 /**
00568  * @brief  Returns the max number of back-off.
00569  * @param  None.
00570  * @retval uint8_t Max number of back-off.
00571  */
00572 uint8_t SpiritCsmaGetMaxNumberBackoff(void)
00573 {
00574   uint8_t tempRegValue;
00575 
00576   /* Reads the CSMA_CONFIG0 register value */
00577   g_xStatus = SpiritSpiReadRegisters(CSMA_CONFIG0_BASE, 1, &tempRegValue);
00578 
00579   /* Build and return the max number of back-off */
00580   return (tempRegValue & 0x07);
00581 
00582 }
00583 
00584 
00585 /**
00586  *@}
00587  */
00588 
00589 /**
00590  *@}
00591  */
00592 
00593 
00594 /**
00595  *@}
00596  */
00597 
00598 
00599 
00600 /******************* (C) COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/