Added support for the WNC M14A2A Cellular LTE Data Module.

Dependencies:   WNC14A2AInterface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SPIRIT_Qi.c Source File

SPIRIT_Qi.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    SPIRIT_Qi.c
00004   * @author  VMA division - AMS
00005   * @version 3.2.2
00006   * @date    08-July-2015
00007   * @brief   Configuration and management of SPIRIT QI.
00008   * @details
00009   *
00010   * @attention
00011   *
00012   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00013   *
00014   * Redistribution and use in source and binary forms, with or without modification,
00015   * are permitted provided that the following conditions are met:
00016   *   1. Redistributions of source code must retain the above copyright notice,
00017   *      this list of conditions and the following disclaimer.
00018   *   2. Redistributions in binary form must reproduce the above copyright notice,
00019   *      this list of conditions and the following disclaimer in the documentation
00020   *      and/or other materials provided with the distribution.
00021   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022   *      may be used to endorse or promote products derived from this software
00023   *      without specific prior written permission.
00024   *
00025   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035   *
00036   ******************************************************************************
00037   */
00038 
00039 /* Includes ------------------------------------------------------------------*/
00040 #include "SPIRIT_Qi.h"
00041 #include "MCU_Interface.h"
00042 
00043 
00044 
00045 /**
00046  * @addtogroup SPIRIT_Libraries
00047  * @{
00048  */
00049 
00050 
00051 /**
00052  * @addtogroup SPIRIT_Qi
00053  * @{
00054  */
00055 
00056 
00057 /**
00058  * @defgroup Qi_Private_TypesDefinitions        QI Private Types Definitions
00059  * @{
00060  */
00061 
00062 /**
00063  *@}
00064  */
00065 
00066 
00067 /**
00068  * @defgroup Qi_Private_Defines                 QI Private Defines
00069  * @{
00070  */
00071 
00072 /**
00073  *@}
00074  */
00075 
00076 
00077 /**
00078  * @defgroup Qi_Private_Macros                  QI Private Macros
00079  * @{
00080  */
00081 
00082 /**
00083  *@}
00084  */
00085 
00086 
00087 /**
00088  * @defgroup Qi_Private_Variables               QI Private Variables
00089  * @{
00090  */
00091 
00092 /**
00093  *@}
00094  */
00095 
00096 
00097 /**
00098  * @defgroup Qi_Private_FunctionPrototypes      QI Private Function Prototypes
00099  * @{
00100  */
00101 
00102 /**
00103  *@}
00104  */
00105 
00106 
00107 /**
00108  * @defgroup Qi_Private_Functions               QI Private Functions
00109  * @{
00110  */
00111 
00112 /**
00113  * @brief  Enables/Disables the PQI Preamble Quality Indicator check. The running peak PQI is
00114  *         compared to a threshold value and the preamble valid IRQ is asserted as soon as the threshold is passed.
00115  * @param  xNewState new state for PQI check.
00116  *         This parameter can be: S_ENABLE or S_DISABLE.
00117  * @retval None.
00118  */
00119 void SpiritQiPqiCheck(SpiritFunctionalState xNewState)
00120 {
00121   uint8_t tempRegValue;
00122 
00123   /* Check the parameters */
00124   s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(xNewState));
00125 
00126   /* Reads the QI register value */
00127   g_xStatus = SpiritSpiReadRegisters(QI_BASE, 1, &tempRegValue);
00128 
00129   /* Enables or disables the PQI Check bit on the QI_BASE register */
00130   if(xNewState == S_ENABLE)
00131   {
00132     tempRegValue |= QI_PQI_MASK;
00133   }
00134   else
00135   {
00136     tempRegValue &= ~QI_PQI_MASK;
00137   }
00138 
00139   /* Writes value on the QI register */
00140   g_xStatus = SpiritSpiWriteRegisters(QI_BASE, 1, &tempRegValue);
00141 
00142 }
00143 
00144 
00145 /**
00146  * @brief  Enables/Disables the Synchronization Quality Indicator check. The running peak SQI is
00147  *         compared to a threshold value and the sync valid IRQ is asserted as soon as the threshold is passed.
00148  * @param  xNewState new state for SQI check.
00149  *         This parameter can be: S_ENABLE or S_DISABLE.
00150  * @retval None.
00151  */
00152 void SpiritQiSqiCheck(SpiritFunctionalState xNewState)
00153 {
00154   uint8_t tempRegValue;
00155 
00156   /* Check the parameters */
00157   s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(xNewState));
00158 
00159   /* Reads the QI register value */
00160   g_xStatus = SpiritSpiReadRegisters(QI_BASE, 1, &tempRegValue);
00161 
00162   /* Enables or disables the SQI Check bit on the QI_BASE register */
00163   if(xNewState == S_ENABLE)
00164   {
00165     tempRegValue |= QI_SQI_MASK;
00166   }
00167   else
00168   {
00169     tempRegValue &= ~QI_SQI_MASK;
00170   }
00171 
00172   /* Writes value on the QI register */
00173   g_xStatus = SpiritSpiWriteRegisters(QI_BASE, 1, &tempRegValue);
00174 
00175 }
00176 
00177 
00178 /**
00179  * @brief  Sets the PQI threshold. The preamble quality threshold is 4*PQI_TH (PQI_TH = 0..15).
00180  * @param  xPqiThr parameter of the formula above.
00181  *     This variable is a @ref PqiThreshold.
00182  * @retval None.
00183  */
00184 void SpiritQiSetPqiThreshold(PqiThreshold xPqiThr)
00185 {
00186   uint8_t tempRegValue;
00187 
00188   /* Check the parameters */
00189   s_assert_param(IS_PQI_THR(xPqiThr));
00190 
00191   /* Reads the QI register value */
00192   g_xStatus = SpiritSpiReadRegisters(QI_BASE, 1, &tempRegValue);
00193 
00194   /* Build the PQI threshold value to be written */
00195   tempRegValue &= 0xC3;
00196   tempRegValue |= ((uint8_t)xPqiThr);
00197 
00198   /* Writes value on the QI register */
00199   g_xStatus = SpiritSpiWriteRegisters(QI_BASE, 1, &tempRegValue);
00200 
00201 }
00202 
00203 
00204 /**
00205  * @brief  Returns the PQI threshold. The preamble quality threshold is 4*PQI_TH (PQI_TH = 0..15).
00206  * @param  None.
00207  * @retval PqiThreshold PQI threshold (PQI_TH of the formula above).
00208  */
00209 PqiThreshold SpiritQiGetPqiThreshold(void)
00210 {
00211   uint8_t tempRegValue;
00212 
00213   /* Reads the QI register value */
00214   g_xStatus = SpiritSpiReadRegisters(QI_BASE, 1, &tempRegValue);
00215 
00216   /* Rebuild and return the PQI threshold value */
00217   return (PqiThreshold)(tempRegValue & 0x3C);
00218 
00219 }
00220 
00221 
00222 /**
00223  * @brief  Sets the SQI threshold. The synchronization quality
00224  *         threshold is equal to 8 * SYNC_LEN - 2 * SQI_TH with SQI_TH = 0..3. When SQI_TH is 0 perfect match is required; when
00225  *         SQI_TH = 1, 2, 3 then 1, 2, or 3 bit errors are respectively accepted. It is recommended that the SQI check is always
00226  *         enabled.
00227  * @param  xSqiThr parameter of the formula above.
00228  *     This parameter is a @ref SqiThreshold.
00229  * @retval None.
00230  */
00231 void SpiritQiSetSqiThreshold(SqiThreshold xSqiThr)
00232 {
00233   uint8_t tempRegValue;
00234 
00235   /* Check the parameters */
00236   s_assert_param(IS_SQI_THR(xSqiThr));
00237 
00238   /* Reads the QI register value */
00239   g_xStatus = SpiritSpiReadRegisters(QI_BASE, 1, &tempRegValue);
00240 
00241   /* Build the SQI threshold value to be written */
00242   tempRegValue &= 0x3F;
00243   tempRegValue |= ((uint8_t)xSqiThr);
00244 
00245   /* Writes the new value on the QI register */
00246   g_xStatus = SpiritSpiWriteRegisters(QI_BASE, 1, &tempRegValue);
00247 
00248 }
00249 
00250 
00251 /**
00252  * @brief  Returns the SQI threshold. The synchronization quality threshold is equal to 8 * SYNC_LEN - 2 * SQI_TH with SQI_TH = 0..3.
00253  * @param  None.
00254  * @retval SqiThreshold SQI threshold (SQI_TH of the formula above).
00255  */
00256 SqiThreshold SpiritQiGetSqiThreshold(void)
00257 {
00258   uint8_t tempRegValue;
00259 
00260   /* Reads the QI register value */
00261   g_xStatus = SpiritSpiReadRegisters(QI_BASE, 1, &tempRegValue);
00262 
00263   /* Rebuild and return the SQI threshold value */
00264   return (SqiThreshold)(tempRegValue & 0xC0);
00265 
00266 }
00267 
00268 
00269 /**
00270  * @brief  Returns the PQI value.
00271  * @param  None.
00272  * @retval uint8_t PQI value.
00273  */
00274 uint8_t SpiritQiGetPqi(void)
00275 {
00276   uint8_t tempRegValue;
00277 
00278   /* Reads the LINK_QUALIF2 register value */
00279   g_xStatus = SpiritSpiReadRegisters(LINK_QUALIF2_BASE, 1, &tempRegValue);
00280 
00281   /* Returns the PQI value */
00282   return tempRegValue;
00283 
00284 }
00285 
00286 
00287 /**
00288  * @brief  Returns the SQI value.
00289  * @param  None.
00290  * @retval uint8_t SQI value.
00291  */
00292 uint8_t SpiritQiGetSqi(void)
00293 {
00294   uint8_t tempRegValue;
00295 
00296   /* Reads the register LINK_QUALIF1 value */
00297   g_xStatus = SpiritSpiReadRegisters(LINK_QUALIF1_BASE, 1, &tempRegValue);
00298 
00299   /* Rebuild and return the SQI value */
00300   return (tempRegValue & 0x7F);
00301 
00302 }
00303 
00304 
00305 /**
00306  * @brief  Returns the LQI value.
00307  * @param  None.
00308  * @retval uint8_t LQI value.
00309  */
00310 uint8_t SpiritQiGetLqi(void)
00311 {
00312   uint8_t tempRegValue;
00313 
00314   /* Reads the LINK_QUALIF0 register value */
00315   g_xStatus = SpiritSpiReadRegisters(LINK_QUALIF0_BASE, 1, &tempRegValue);
00316 
00317   /* Rebuild and return the LQI value */
00318   return ((tempRegValue & 0xF0)>> 4);
00319 
00320 }
00321 
00322 
00323 /**
00324  * @brief  Returns the CS status.
00325  * @param  None.
00326  * @retval SpiritFlagStatus CS value (S_SET or S_RESET).
00327  */
00328 SpiritFlagStatus SpiritQiGetCs(void)
00329 {
00330   uint8_t tempRegValue;
00331 
00332   /* Reads the LINK_QUALIF1 register value */
00333   g_xStatus = SpiritSpiReadRegisters(LINK_QUALIF1_BASE, 1, &tempRegValue);
00334 
00335   /* Rebuild and returns the CS status value */
00336   if((tempRegValue & 0x80) == 0)
00337   {
00338     return S_RESET;
00339   }
00340   else
00341   {
00342     return S_SET;
00343   }
00344 
00345 }
00346 
00347 
00348 /**
00349  * @brief  Returns the RSSI value. The measured power is reported in steps of half a dB from 0 to 255 and is offset in such a way that -120 dBm corresponds
00350  *         to 20.
00351  * @param  None.
00352  * @retval uint8_t RSSI value.
00353  */
00354 uint8_t SpiritQiGetRssi(void)
00355 {
00356   uint8_t tempRegValue;
00357 
00358   /* Reads the RSSI_LEVEL register value */
00359   g_xStatus = SpiritSpiReadRegisters(RSSI_LEVEL_BASE, 1, &tempRegValue);
00360 
00361   /* Returns the RSSI value */
00362   return tempRegValue;
00363 
00364 }
00365 
00366 
00367 /**
00368  * @brief  Sets the RSSI threshold.
00369  * @param  cRssiThr RSSI threshold reported in steps of half a dBm with a -130 dBm offset.
00370  *         This parameter must be a uint8_t.
00371  * @retval None.
00372  */
00373 void SpiritQiSetRssiThreshold(uint8_t cRssiThr)
00374 {
00375   /* Writes the new value on the RSSI_TH register */
00376   g_xStatus = SpiritSpiWriteRegisters(RSSI_TH_BASE, 1, &cRssiThr);
00377 
00378 }
00379 
00380 
00381 /**
00382  * @brief  Returns the RSSI threshold.
00383  * @param  None.
00384  * @retval uint8_t RSSI threshold.
00385  */
00386 uint8_t SpiritQiGetRssiThreshold(void)
00387 {
00388   uint8_t tempRegValue;
00389 
00390   /* Reads the RSSI_TH register value */
00391   g_xStatus = SpiritSpiReadRegisters(RSSI_TH_BASE, 1, &tempRegValue);
00392 
00393   /* Returns RSSI threshold */
00394   return tempRegValue;
00395 
00396 }
00397 
00398 
00399 /**
00400  * @brief  Computes the RSSI threshold from its dBm value according to the formula: (RSSI[Dbm] + 130)/0.5
00401  * @param  nDbmValue RSSI threshold reported in dBm.
00402  *         This parameter must be a sint16_t.
00403  * @retval uint8_t RSSI threshold corresponding to dBm value.
00404  */
00405 uint8_t SpiritQiComputeRssiThreshold(int nDbmValue)
00406 {
00407   /* Check the parameters */
00408   s_assert_param(IS_RSSI_THR_DBM(nDbmValue));
00409 
00410   /* Computes the RSSI threshold for register */
00411   return 2*(nDbmValue+130);
00412 
00413 }
00414 
00415 /**
00416  * @brief  Sets the RSSI threshold from its dBm value according to the formula: (RSSI[Dbm] + 130)/0.5.
00417  * @param  nDbmValue RSSI threshold reported in dBm.
00418  *         This parameter must be a sint16_t.
00419  * @retval None.
00420  */
00421 void SpiritQiSetRssiThresholddBm(int nDbmValue)
00422 {
00423   uint8_t tempRegValue=2*(nDbmValue+130);
00424 
00425   /* Check the parameters */
00426   s_assert_param(IS_RSSI_THR_DBM(nDbmValue));
00427 
00428   /* Writes the new value on the RSSI_TH register */
00429   g_xStatus = SpiritSpiWriteRegisters(RSSI_TH_BASE, 1, &tempRegValue);
00430 
00431 }
00432 
00433 /**
00434  * @brief  Sets the RSSI filter gain. This parameter sets the bandwidth of a low pass IIR filter (RSSI_FLT register, allowed values 0..15), a
00435  *         lower values gives a faster settling of the measurements but lower precision. The recommended value for such parameter is 14.
00436  * @param  xRssiFg RSSI filter gain value.
00437  *         This parameter can be any value of @ref RssiFilterGain.
00438  * @retval None.
00439  */
00440 void SpiritQiSetRssiFilterGain(RssiFilterGain xRssiFg)
00441 {
00442   uint8_t tempRegValue;
00443 
00444    /* Check the parameters */
00445   s_assert_param(IS_RSSI_FILTER_GAIN(xRssiFg));
00446 
00447   /* Reads the RSSI_FLT register */
00448   g_xStatus = SpiritSpiReadRegisters(RSSI_FLT_BASE, 1, &tempRegValue);
00449 
00450   /* Sets the specified filter gain */
00451   tempRegValue &= 0x0F;
00452   tempRegValue |= ((uint8_t)xRssiFg);
00453 
00454   /* Writes the new value on the RSSI_FLT register */
00455   g_xStatus = SpiritSpiWriteRegisters(RSSI_FLT_BASE, 1, &tempRegValue);
00456 
00457 }
00458 
00459 
00460 /**
00461  * @brief  Returns the RSSI filter gain.
00462  * @param  None.
00463  * @retval RssiFilterGain RSSI filter gain.
00464  */
00465 RssiFilterGain SpiritQiGetRssiFilterGain(void)
00466 {
00467   uint8_t tempRegValue;
00468 
00469   /* Reads the RSSI_FLT register */
00470   g_xStatus = SpiritSpiReadRegisters(RSSI_FLT_BASE, 1, &tempRegValue);
00471 
00472   /* Rebuild and returns the filter gain value */
00473   return (RssiFilterGain)(tempRegValue & 0xF0);
00474 
00475 }
00476 
00477 
00478 /**
00479  * @brief  Sets the CS Mode. When static carrier sensing is used (cs_mode = 0), the carrier sense signal is asserted when the measured RSSI is above the
00480  *         value specified in the RSSI_TH register and is deasserted when the RSSI falls 3 dB below the same threshold.
00481  *         When dynamic carrier sense is used (cs_mode = 1, 2, 3), the carrier sense signal is asserted if the signal is above the
00482  *         threshold and a fast power increase of 6, 12 or 18 dB is detected; it is deasserted if a power fall of the same amplitude is
00483  *         detected.
00484  * @param  xCsMode CS mode selector.
00485  *         This parameter can be any value of @ref CSMode.
00486  * @retval None.
00487  */
00488 void SpiritQiSetCsMode(CSMode xCsMode)
00489 {
00490   uint8_t tempRegValue;
00491 
00492    /* Check the parameters */
00493   s_assert_param(IS_CS_MODE(xCsMode));
00494 
00495   /* Reads the RSSI_FLT register */
00496   g_xStatus = SpiritSpiReadRegisters(RSSI_FLT_BASE, 1, &tempRegValue);
00497 
00498   /* Sets bit to select the CS mode */
00499   tempRegValue &= ~0x0C;
00500   tempRegValue |= ((uint8_t)xCsMode);
00501 
00502   /* Writes the new value on the RSSI_FLT register */
00503   g_xStatus = SpiritSpiWriteRegisters(RSSI_FLT_BASE, 1, &tempRegValue);
00504 
00505 }
00506 
00507 
00508 /**
00509  * @brief  Returns the CS Mode.
00510  * @param  None.
00511  * @retval CSMode CS mode.
00512  */
00513 CSMode SpiritQiGetCsMode(void)
00514 {
00515   uint8_t tempRegValue;
00516 
00517   /* Reads the RSSI_FLT register */
00518   g_xStatus = SpiritSpiReadRegisters(RSSI_FLT_BASE, 1, &tempRegValue);
00519 
00520   /* Rebuild and returns the CS mode value */
00521   return (CSMode)(tempRegValue & 0x0C);
00522 
00523 }
00524 
00525 /**
00526  * @brief  Enables/Disables the CS Timeout Mask. If enabled CS value contributes to timeout disabling.
00527  * @param  xNewState new state for CS Timeout Mask.
00528  *         This parameter can be S_ENABLE or S_DISABLE.
00529  * @retval None.
00530  */
00531 void SpiritQiCsTimeoutMask(SpiritFunctionalState xNewState)
00532 {
00533   uint8_t tempRegValue;
00534 
00535   /* Check the parameters */
00536   s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(xNewState));
00537 
00538   /* Reads the PROTOCOL2 register value */
00539   g_xStatus = SpiritSpiReadRegisters(PROTOCOL2_BASE, 1, &tempRegValue);
00540 
00541   /* Enables or disables the CS timeout mask */
00542   if(xNewState == S_ENABLE)
00543   {
00544     tempRegValue |= PROTOCOL2_CS_TIMEOUT_MASK;
00545   }
00546   else
00547   {
00548     tempRegValue &= ~PROTOCOL2_CS_TIMEOUT_MASK;
00549   }
00550 
00551   /* Writes the new value on the PROTOCOL2 register */
00552   g_xStatus = SpiritSpiWriteRegisters(PROTOCOL2_BASE, 1, &tempRegValue);
00553 
00554 }
00555 
00556 
00557 /**
00558  * @brief  Enables/Disables the PQI Timeout Mask. If enabled PQI value contributes to timeout disabling.
00559  * @param  xNewState new state for PQI Timeout Mask.
00560  *         This parameter can be S_ENABLE or S_DISABLE.
00561  * @retval None.
00562  */
00563 void SpiritQiPqiTimeoutMask(SpiritFunctionalState xNewState)
00564 {
00565   uint8_t tempRegValue;
00566 
00567   /* Check the parameters */
00568   s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(xNewState));
00569 
00570   /* Reads the PROTOCOL2 register */
00571   g_xStatus = SpiritSpiReadRegisters(PROTOCOL2_BASE, 1, &tempRegValue);
00572 
00573   /* Enables or disables the PQI timeout mask */
00574   if(xNewState == S_ENABLE)
00575   {
00576     tempRegValue |= PROTOCOL2_PQI_TIMEOUT_MASK;
00577   }
00578   else
00579   {
00580     tempRegValue &= ~PROTOCOL2_PQI_TIMEOUT_MASK;
00581   }
00582 
00583   /* Writes the new value on the PROTOCOL2 register */
00584   g_xStatus = SpiritSpiWriteRegisters(PROTOCOL2_BASE, 1, &tempRegValue);
00585 
00586 }
00587 
00588 
00589 /**
00590  * @brief  Enables/Disables the SQI Timeout Mask. If enabled SQI value contributes to timeout disabling.
00591  * @param  xNewState new state for SQI Timeout Mask.
00592  *         This parameter can be S_ENABLE or S_DISABLE.
00593  * @retval None.
00594  */
00595 void SpiritQiSqiTimeoutMask(SpiritFunctionalState xNewState)
00596 {
00597   uint8_t tempRegValue;
00598 
00599   /* Check the parameters */
00600   s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(xNewState));
00601 
00602   /* Reads the PROTOCOL2 register */
00603   g_xStatus = SpiritSpiReadRegisters(PROTOCOL2_BASE, 1, &tempRegValue);
00604 
00605   /* Enables or disables the SQI timeout mask */
00606   if(xNewState == S_ENABLE)
00607   {
00608     tempRegValue |= PROTOCOL2_SQI_TIMEOUT_MASK;
00609   }
00610   else
00611   {
00612     tempRegValue &= ~PROTOCOL2_SQI_TIMEOUT_MASK;
00613   }
00614 
00615   /* Writes the new value on the PROTOCOL2 register */
00616   g_xStatus = SpiritSpiWriteRegisters(PROTOCOL2_BASE, 1, &tempRegValue);
00617 
00618 }
00619 
00620 
00621 /**
00622  *@}
00623  */
00624 
00625 /**
00626  *@}
00627  */
00628 
00629 
00630 /**
00631  *@}
00632  */
00633 
00634 
00635 
00636 /******************* (C) COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/