Version of easy-connect with the u-blox cellular platforms C027 and C030 added.

Dependents:   HelloMQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SPIRIT_Timer.c Source File

SPIRIT_Timer.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    SPIRIT_Timer.c
00004   * @author  VMA division - AMS
00005   * @version 3.2.2
00006   * @date    08-July-2015
00007   * @brief   Configuration and management of SPIRIT timers.
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_Timer.h"
00041 #include "SPIRIT_Radio.h"
00042 #include "MCU_Interface.h"
00043 
00044 
00045 
00046 
00047 /**
00048  * @addtogroup SPIRIT_Libraries
00049  * @{
00050  */
00051 
00052 
00053 /**
00054  * @addtogroup SPIRIT_Timer
00055  * @{
00056  */
00057 
00058 
00059 /**
00060  * @defgroup Timer_Private_TypesDefinitions             Timer Private Types Definitions
00061  * @{
00062  */
00063 
00064 /**
00065  *@}
00066  */
00067 
00068 
00069 /**
00070  * @defgroup Timer_Private_Defines                      Timer Private Defines
00071  * @{
00072  */
00073 
00074 /**
00075  *@}
00076  */
00077 
00078 
00079 /**
00080  * @defgroup Timer_Private_Macros                       Timer Private Macros
00081  * @{
00082  */
00083 
00084 
00085 /**
00086  *@}
00087  */
00088 
00089 
00090 /**
00091  * @defgroup Timer_Private_Variables                    Timer Private Variables
00092  * @{
00093  */
00094 
00095 /**
00096  *@}
00097  */
00098 
00099 
00100 /**
00101  * @defgroup Timer_Private_FunctionPrototypes            Timer Private Function Prototypes
00102  * @{
00103  */
00104 
00105 /**
00106  *@}
00107  */
00108 
00109 
00110 /**
00111  * @defgroup Timer_Private_Functions                    Timer Private Functions
00112  * @{
00113  */
00114 
00115 /**
00116  * @brief  Enables or Disables the LDCR mode.
00117  * @param  xNewState new state for LDCR mode.
00118  *         This parameter can be: S_ENABLE or S_DISABLE.
00119  * @retval None.
00120  */
00121 void SpiritTimerLdcrMode(SpiritFunctionalState xNewState)
00122 {
00123   uint8_t tempRegValue;
00124 
00125   /* Reads the register value */
00126   g_xStatus = SpiritSpiReadRegisters(PROTOCOL2_BASE, 1, &tempRegValue);
00127 
00128   /* Mask the read value to enable or disable the LDC mode */
00129   if(xNewState==S_ENABLE)
00130   {
00131     tempRegValue |= PROTOCOL2_LDC_MODE_MASK;
00132   }
00133   else
00134   {
00135     tempRegValue &= ~PROTOCOL2_LDC_MODE_MASK;
00136   }
00137 
00138   /* Writes the register to Enable or Disable the LDCR mode */
00139   g_xStatus = SpiritSpiWriteRegisters(PROTOCOL2_BASE, 1, &tempRegValue);
00140 
00141 }
00142 
00143 
00144 /**
00145  * @brief  Enables or Disables the LDCR timer reloading with the value stored in the LDCR_RELOAD registers.
00146  * @param  xNewState new state for LDCR reloading.
00147  *         This parameter can be: S_ENABLE or S_DISABLE.
00148  * @retval None.
00149  */
00150 void SpiritTimerLdcrAutoReload(SpiritFunctionalState xNewState)
00151 {
00152   uint8_t tempRegValue;
00153 
00154   /* Reads the register value */
00155   g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
00156 
00157   /* Mask te read value to enable or disable the reload on sync mode */
00158   if(xNewState==S_ENABLE)
00159   {
00160     tempRegValue |= PROTOCOL1_LDC_RELOAD_ON_SYNC_MASK;
00161   }
00162   else
00163   {
00164     tempRegValue &= ~PROTOCOL1_LDC_RELOAD_ON_SYNC_MASK;
00165   }
00166 
00167   /* Writes the register to Enable or Disable the Auto Reload */
00168   g_xStatus = SpiritSpiWriteRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
00169 
00170 }
00171 
00172 
00173 /**
00174  * @brief  Returns the LDCR timer reload bit.
00175  * @param  None.
00176  * @retval SpiritFunctionalState: value of the reload bit.
00177  */
00178 SpiritFunctionalState SpiritTimerLdcrGetAutoReload(void)
00179 {
00180   uint8_t tempRegValue;
00181 
00182   /* Reads the register value */
00183   g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
00184 
00185   return (SpiritFunctionalState)(tempRegValue & 0x80);
00186 
00187 }
00188 
00189 /**
00190  * @brief  Sets the RX timeout timer initialization registers with the values of COUNTER and PRESCALER according to the formula: Trx=PRESCALER*COUNTER*Tck.
00191  *         Remember that it is possible to have infinite RX_Timeout writing 0 in the RX_Timeout_Counter and/or RX_Timeout_Prescaler registers.
00192  * @param  cCounter value for the timer counter.
00193  *         This parameter must be an uint8_t.
00194  * @param  cPrescaler value for the timer prescaler.
00195  *         This parameter must be an uint8_t.
00196  * @retval None.
00197  */
00198 void SpiritTimerSetRxTimeout(uint8_t cCounter , uint8_t cPrescaler)
00199 {
00200   uint8_t tempRegValue[2]={cPrescaler,cCounter};
00201 
00202   /* Writes the prescaler and counter value for RX timeout in the corresponding register */
00203   g_xStatus = SpiritSpiWriteRegisters(TIMERS5_RX_TIMEOUT_PRESCALER_BASE, 2, tempRegValue);
00204 
00205 }
00206 
00207 
00208 /**
00209  * @brief  Sets the RX timeout timer counter and prescaler from the desired value in ms. it is possible to fix the RX_Timeout to
00210  *         a minimum value of 50.417us to a maximum value of about 3.28 s.
00211  * @param  fDesiredMsec desired timer value.
00212  *         This parameter must be a float.
00213  * @retval None
00214  */
00215 
00216 void SpiritTimerSetRxTimeoutMs(float fDesiredMsec)
00217 {
00218   uint8_t tempRegValue[2];
00219 
00220   /* Computes the counter and prescaler value */
00221   SpiritTimerComputeRxTimeoutValues(fDesiredMsec , &tempRegValue[1] , &tempRegValue[0]);
00222 
00223   /* Writes the prescaler and counter value for RX timeout in the corresponding register */
00224   g_xStatus = SpiritSpiWriteRegisters(TIMERS5_RX_TIMEOUT_PRESCALER_BASE, 2, tempRegValue);
00225 
00226 }
00227 
00228 
00229 /**
00230  * @brief  Sets the RX timeout timer counter. If it is equal to 0 the timeout is infinite.
00231  * @param  cCounter value for the timer counter.
00232  *         This parameter must be an uint8_t.
00233  * @retval None.
00234  */
00235 void SpiritTimerSetRxTimeoutCounter(uint8_t cCounter)
00236 {
00237   /* Writes the counter value for RX timeout in the corresponding register */
00238   g_xStatus = SpiritSpiWriteRegisters(TIMERS4_RX_TIMEOUT_COUNTER_BASE, 1, &cCounter);
00239 
00240 }
00241 
00242 
00243 /**
00244  * @brief  Sets the RX timeout timer prescaler. If it is equal to 0 the timeout is infinite.
00245  * @param  cPrescaler value for the timer prescaler.
00246  *         This parameter must be an uint8_t.
00247  * @retval None
00248  */
00249 void SpiritTimerSetRxTimeoutPrescaler(uint8_t cPrescaler)
00250 {
00251   /* Writes the prescaler value for RX timeout in the corresponding register */
00252   g_xStatus = SpiritSpiWriteRegisters(TIMERS5_RX_TIMEOUT_PRESCALER_BASE, 1, &cPrescaler);
00253 
00254 }
00255 
00256 
00257 /**
00258  * @brief  Returns the RX timeout timer.
00259  * @param  pfTimeoutMsec pointer to the variable in which the timeout expressed in milliseconds has to be stored.
00260  *         If the returned value is 0, it means that the RX_Timeout is infinite.
00261  *         This parameter must be a float*.
00262  * @param  pcCounter pointer to the variable in which the timer counter has to be stored.
00263  *         This parameter must be an uint8_t*.
00264  * @param  pcPrescaler pointer to the variable in which the timer prescaler has to be stored.
00265  *         This parameter must be an uint8_t*.
00266  * @retval None.
00267  */
00268 void SpiritTimerGetRxTimeout(float* pfTimeoutMsec, uint8_t* pcCounter , uint8_t* pcPrescaler)
00269 {
00270   uint8_t tempRegValue[2];
00271 
00272   /* Reads the RX timeout registers value */
00273   g_xStatus = SpiritSpiReadRegisters(TIMERS5_RX_TIMEOUT_PRESCALER_BASE, 2, tempRegValue);
00274 
00275   /* Returns values */
00276   (*pcPrescaler) = tempRegValue[0];
00277   (*pcCounter) = tempRegValue[1];
00278     
00279   float nXtalFrequency = (float)SpiritRadioGetXtalFrequency();
00280   if(nXtalFrequency>DOUBLE_XTAL_THR) {
00281     nXtalFrequency /= 2.0;
00282   }
00283   nXtalFrequency /= 1000.0;
00284   *pfTimeoutMsec = (float)((tempRegValue[0]+1)*tempRegValue[1]*(1210.0/nXtalFrequency));
00285   
00286 
00287 }
00288 
00289 
00290 /**
00291  * @brief  Sets the LDCR wake up timer initialization registers with the values of
00292  *         COUNTER and PRESCALER according to the formula: Twu=(PRESCALER +1)*(COUNTER+1)*Tck, where
00293  *         Tck = 28.818 us. The minimum vale of the wakeup timeout is 28.818us (PRESCALER and
00294  *         COUNTER equals to 0) and the maximum value is about 1.89 s (PRESCALER anc COUNTER equals
00295  *         to 255).
00296  * @param  cCounter value for the timer counter.
00297  *         This parameter must be an uint8_t.
00298  * @param  cPrescaler value for the timer prescaler.
00299  *         This parameter must be an uint8_t.
00300  * @retval None.
00301  */
00302 void SpiritTimerSetWakeUpTimer(uint8_t cCounter , uint8_t cPrescaler)
00303 {
00304   uint8_t tempRegValue[2]={cPrescaler,cCounter};
00305 
00306   /* Writes the counter and prescaler value of wake-up timer in the corresponding register */
00307   g_xStatus = SpiritSpiWriteRegisters(TIMERS3_LDC_PRESCALER_BASE, 2, tempRegValue);
00308 
00309 }
00310 
00311 
00312 /**
00313  * @brief  Sets the LDCR wake up timer counter and prescaler from the desired value in ms,
00314  *         according to the formula: Twu=(PRESCALER +1)*(COUNTER+1)*Tck, where Tck = 28.818 us.
00315  *         The minimum vale of the wakeup timeout is 28.818us (PRESCALER and COUNTER equals to 0)
00316  *         and the maximum value is about 1.89 s (PRESCALER anc COUNTER equals to 255).
00317  * @param  fDesiredMsec desired timer value.
00318  *         This parameter must be a float.
00319  * @retval None.
00320  */
00321 void SpiritTimerSetWakeUpTimerMs(float fDesiredMsec)
00322 {
00323   uint8_t tempRegValue[2];
00324 
00325   /* Computes counter and prescaler */
00326   SpiritTimerComputeWakeUpValues(fDesiredMsec , &tempRegValue[1] , &tempRegValue[0]);
00327 
00328   /* Writes the counter and prescaler value of wake-up timer in the corresponding register */
00329   g_xStatus = SpiritSpiWriteRegisters(TIMERS3_LDC_PRESCALER_BASE, 2, tempRegValue);
00330 
00331 }
00332 
00333 
00334 /**
00335  * @brief  Sets the LDCR wake up timer counter. Remember that this value is incresead by one in the Twu calculation.
00336  *         Twu=(PRESCALER +1)*(COUNTER+1)*Tck, where Tck = 28.818 us
00337  * @param  cCounter value for the timer counter.
00338  *         This parameter must be an uint8_t.
00339  * @retval None.
00340  */
00341 void SpiritTimerSetWakeUpTimerCounter(uint8_t cCounter)
00342 {
00343   /* Writes the counter value for Wake_Up timer in the corresponding register */
00344   g_xStatus = SpiritSpiWriteRegisters(TIMERS2_LDC_COUNTER_BASE, 1, &cCounter);
00345 
00346 }
00347 
00348 
00349 /**
00350  * @brief  Sets the LDCR wake up timer prescaler. Remember that this value is incresead by one in the Twu calculation.
00351  *         Twu=(PRESCALER +1)*(COUNTER+1)*Tck, where Tck = 28.818 us
00352  * @param  cPrescaler value for the timer prescaler.
00353  *         This parameter must be an uint8_t.
00354  * @retval None.
00355  */
00356 void SpiritTimerSetWakeUpTimerPrescaler(uint8_t cPrescaler)
00357 {
00358   /* Writes the prescaler value for Wake_Up timer in the corresponding register */
00359   g_xStatus = SpiritSpiWriteRegisters(TIMERS3_LDC_PRESCALER_BASE, 1, &cPrescaler);
00360 
00361 }
00362 
00363 
00364 /**
00365  * @brief  Returns the LDCR wake up timer, according to the formula: Twu=(PRESCALER +1)*(COUNTER+1)*Tck, where Tck = 28.818 us.
00366  * @param  pfWakeUpMsec pointer to the variable in which the wake-up time expressed in milliseconds has to be stored.
00367  *         This parameter must be a float*.
00368  * @param  pcCounter pointer to the variable in which the timer counter has to be stored.
00369  *         This parameter must be an uint8_t*.
00370  * @param  pcPrescaler pointer to the variable in which the timer prescaler has to be stored.
00371  *         This parameter must be an uint8_t*.
00372  * @retval None.
00373  */
00374 void SpiritTimerGetWakeUpTimer(float* pfWakeUpMsec, uint8_t* pcCounter , uint8_t* pcPrescaler)
00375 {
00376   uint8_t tempRegValue[2];
00377   //uint32_t xtal=SpiritRadioGetXtalFrequency();
00378   float rco_freq;
00379   
00380   rco_freq=(float)SpiritTimerGetRcoFrequency();
00381   
00382   /* Reads the Wake_Up timer registers value */
00383   g_xStatus = SpiritSpiReadRegisters(TIMERS3_LDC_PRESCALER_BASE, 2, tempRegValue);
00384 
00385   /* Returns values */
00386   (*pcPrescaler)=tempRegValue[0];
00387   (*pcCounter)=tempRegValue[1];
00388   *pfWakeUpMsec = (float)((((*pcPrescaler)+1)*((*pcCounter)+1)*(1000.0/rco_freq)));
00389 
00390 }
00391 
00392 
00393 /**
00394  * @brief  Sets the LDCR wake up timer reloading registers with the values of
00395  *         COUNTER and PRESCALER according to the formula: Twu=(PRESCALER +1)*(COUNTER+1)*Tck, where
00396  *         Tck = 28.818 us. The minimum vale of the wakeup timeout is 28.818us (PRESCALER and
00397  *         COUNTER equals to 0) and the maximum value is about 1.89 s (PRESCALER anc COUNTER equals
00398  *         to 255).
00399  * @param  cCounter reload value for the timer counter.
00400  *         This parameter must be an uint8_t.
00401  * @param  cPrescaler reload value for the timer prescaler.
00402  *         This parameter must be an uint8_t.
00403  * @retval None.
00404  */
00405 void SpiritTimerSetWakeUpTimerReload(uint8_t cCounter , uint8_t cPrescaler)
00406 {
00407   uint8_t tempRegValue[2]={cPrescaler,cCounter};
00408 
00409   /* Writes the counter and prescaler value of reload wake-up timer in the corresponding register */
00410   g_xStatus = SpiritSpiWriteRegisters(TIMERS1_LDC_RELOAD_PRESCALER_BASE, 2, tempRegValue);
00411 
00412 }
00413 
00414 
00415 /**
00416  * @brief  Sets the LDCR wake up reload timer counter and prescaler from the desired value in ms,
00417  *         according to the formula: Twu=(PRESCALER +1)*(COUNTER+1)*Tck, where Tck = 28.818 us.
00418  *         The minimum vale of the wakeup timeout is 28.818us (PRESCALER and COUNTER equals to 0)
00419  *         and the maximum value is about 1.89 s (PRESCALER anc COUNTER equals to 255).
00420  * @param  fDesiredMsec desired timer value.
00421  *         This parameter must be a float.
00422  * @retval None.
00423  */
00424 void SpiritTimerSetWakeUpTimerReloadMs(float fDesiredMsec)
00425 {
00426   uint8_t tempRegValue[2];
00427 
00428   /* Computes counter and prescaler */
00429   SpiritTimerComputeWakeUpValues(fDesiredMsec , &tempRegValue[1] , &tempRegValue[0]);
00430 
00431   /* Writes the counter and prescaler value of reload wake-up timer in the corresponding register */
00432   g_xStatus = SpiritSpiWriteRegisters(TIMERS1_LDC_RELOAD_PRESCALER_BASE, 2, tempRegValue);
00433 
00434 }
00435 
00436 
00437 /**
00438  * @brief  Sets the LDCR wake up timer reload counter. Remember that this value is incresead by one in the Twu calculation.
00439  *         Twu=(PRESCALER +1)*(COUNTER+1)*Tck, where Tck = 28.818 us
00440  * @param  cCounter value for the timer counter.
00441  *         This parameter must be an uint8_t.
00442  * @retval None
00443  */
00444 void SpiritTimerSetWakeUpTimerReloadCounter(uint8_t cCounter)
00445 {
00446   /* Writes the counter value for reload Wake_Up timer in the corresponding register */
00447   g_xStatus = SpiritSpiWriteRegisters(TIMERS0_LDC_RELOAD_COUNTER_BASE, 1, &cCounter);
00448 
00449 }
00450 
00451 
00452 /**
00453  * @brief  Sets the LDCR wake up timer reload prescaler. Remember that this value is incresead by one in the Twu calculation.
00454  *         Twu=(PRESCALER +1)*(COUNTER+1)*Tck, where Tck = 28.818 us
00455  * @param  cPrescaler value for the timer prescaler.
00456  *         This parameter must be an uint8_t.
00457  * @retval None
00458  */
00459 void SpiritTimerSetWakeUpTimerReloadPrescaler(uint8_t cPrescaler)
00460 {
00461   /* Writes the prescaler value for reload Wake_Up timer in the corresponding register */
00462   g_xStatus = SpiritSpiWriteRegisters(TIMERS1_LDC_RELOAD_PRESCALER_BASE, 1, &cPrescaler);
00463 
00464 }
00465 
00466 
00467 /**
00468  * @brief  Returns the LDCR wake up reload timer, according to the formula: Twu=(PRESCALER +1)*(COUNTER+1)*Tck, where Tck = 28.818 us.
00469  * @param  pfWakeUpReloadMsec pointer to the variable in which the wake-up reload time expressed in milliseconds has to be stored.
00470  *         This parameter must be a float*.
00471  * @param  pcCounter pointer to the variable in which the timer counter has to be stored.
00472  *         This parameter must be an uint8_t*.
00473  * @param  pcPrescaler pointer to the variable in which the timer prescaler has to be stored.
00474  *         This parameter must be an uint8_t*.
00475  * @retval None.
00476  */
00477 void SpiritTimerGetWakeUpTimerReload(float* pfWakeUpReloadMsec, uint8_t* pcCounter , uint8_t* pcPrescaler)
00478 {
00479   uint8_t tempRegValue[2];
00480   //uint32_t xtal=SpiritRadioGetXtalFrequency();
00481   float rco_freq;
00482   
00483   rco_freq=(float)SpiritTimerGetRcoFrequency();
00484   
00485   /* Reads the reload Wake_Up timer registers value */
00486   g_xStatus = SpiritSpiReadRegisters(TIMERS1_LDC_RELOAD_PRESCALER_BASE, 2, tempRegValue);
00487 
00488   /* Returns values */
00489   (*pcPrescaler)=tempRegValue[0];
00490   (*pcCounter)=tempRegValue[1];
00491   *pfWakeUpReloadMsec = (float)((((*pcPrescaler)+1)*((*pcCounter)+1)*(1000.0/rco_freq)));
00492 
00493 }
00494 
00495 /**
00496  * @brief  Computes and returns the RCO frequency. 
00497  *         This frequency depends on the xtal frequency and the XTAL bit in register 0x01.
00498  * @retval RCO frequency in Hz as an uint16_t.
00499  */
00500 uint16_t SpiritTimerGetRcoFrequency(void)
00501 {
00502   uint16_t rco_freq=34700;
00503   uint32_t xtal=SpiritRadioGetXtalFrequency();
00504   
00505   if(xtal>30000000) xtal/=2;
00506   
00507   if(xtal==25000000)
00508   {
00509     uint8_t xtal_flag;
00510     SpiritSpiReadRegisters(0x01, 1, &xtal_flag);
00511     xtal_flag=(xtal_flag&0x40);
00512     
00513     if(xtal_flag==0)
00514     {
00515       rco_freq=36100;
00516     }
00517     else
00518     {
00519       rco_freq=33300;
00520     }
00521   }
00522   
00523   return rco_freq;
00524 }
00525 
00526 /**
00527  * @brief  Computes the values of the wakeup timer counter and prescaler from the user time expressed in millisecond.
00528  *         The prescaler and the counter values are computed maintaining the prescaler value as
00529  *         small as possible in order to obtain the best resolution, and in the meantime minimizing the error.
00530  * @param  fDesiredMsec desired wakeup timeout in millisecs.
00531  *         This parameter must be a float. Since the counter and prescaler are 8 bit registers the maximum
00532  *         reachable value is maxTime = fTclk x 256 x 256.
00533  * @param  pcCounter pointer to the variable in which the value for the wakeup timer counter has to be stored.
00534  *         This parameter must be a uint8_t*.
00535  * @param  pcPrescaler pointer to the variable in which the value for the wakeup timer prescaler has to be stored.
00536  *         This parameter must be an uint8_t*.
00537  * @retval None
00538  */
00539 void SpiritTimerComputeWakeUpValues(float fDesiredMsec , uint8_t* pcCounter , uint8_t* pcPrescaler)
00540 {
00541   float rco_freq,err;
00542   uint32_t n;
00543   
00544   rco_freq=((float)SpiritTimerGetRcoFrequency())/1000;
00545   
00546   /* N cycles in the time base of the timer: 
00547      - clock of the timer is RCO frequency
00548      - divide times 1000 more because we have an input in ms (variable rco_freq is already this frequency divided by 1000)
00549   */
00550   n=(uint32_t)(fDesiredMsec*rco_freq);
00551     
00552   /* check if it is possible to reach that target with prescaler and counter of spirit1 */
00553   if(n/0xFF>0xFD)
00554   {
00555     /* if not return the maximum possible value */
00556     (*pcCounter) = 0xFF;
00557     (*pcPrescaler) = 0xFF;
00558     return;
00559   }
00560   
00561   /* prescaler is really 2 as min value */
00562   (*pcPrescaler)=(n/0xFF)+2;
00563   (*pcCounter) = n / (*pcPrescaler);
00564   
00565   /* check if the error is minimum */
00566   err=S_ABS((float)(*pcCounter)*(*pcPrescaler)/rco_freq-fDesiredMsec);
00567   
00568   if((*pcCounter)<=254)
00569   {
00570     if(S_ABS((float)((*pcCounter)+1)*(*pcPrescaler)/rco_freq-fDesiredMsec)<err)
00571       (*pcCounter)=(*pcCounter)+1;
00572   }
00573     
00574   /* decrement prescaler and counter according to the logic of this timer in spirit1 */
00575   (*pcPrescaler)--;
00576   if((*pcCounter)>1)
00577     (*pcCounter)--;
00578   else
00579     (*pcCounter)=1;
00580 }
00581 
00582 
00583 /**
00584  * @brief  Computes the values of the rx_timeout timer counter and prescaler from the user time expressed in millisecond.
00585  *         The prescaler and the counter values are computed maintaining the prescaler value as
00586  *         small as possible in order to obtain the best resolution, and in the meantime minimizing the error.
00587  * @param  fDesiredMsec desired rx_timeout in millisecs.
00588  *         This parameter must be a float. Since the counter and prescaler are 8 bit registers the maximum
00589  *         reachable value is maxTime = fTclk x 255 x 255.
00590  * @param  pcCounter pointer to the variable in which the value for the rx_timeout counter has to be stored.
00591  *         This parameter must be a uint8_t*.
00592  * @param  pcPrescaler pointer to the variable in which the value for the rx_timeout prescaler has to be stored.
00593  *         This parameter must be an uint8_t*.
00594  * @retval None
00595  */
00596 void SpiritTimerComputeRxTimeoutValues(float fDesiredMsec , uint8_t* pcCounter , uint8_t* pcPrescaler)
00597 {
00598   uint32_t nXtalFrequency = SpiritRadioGetXtalFrequency();
00599   uint32_t n;
00600   float err;
00601   
00602   /* if xtal is doubled divide it by 2 */
00603   if(nXtalFrequency>DOUBLE_XTAL_THR) {
00604     nXtalFrequency >>= 1;
00605   }
00606   
00607   /* N cycles in the time base of the timer: 
00608      - clock of the timer is xtal/1210
00609      - divide times 1000 more because we have an input in ms
00610   */
00611   n=(uint32_t)(fDesiredMsec*nXtalFrequency/1210000);
00612   
00613   /* check if it is possible to reach that target with prescaler and counter of spirit1 */
00614   if(n/0xFF>0xFD)
00615   {
00616     /* if not return the maximum possible value */
00617     (*pcCounter) = 0xFF;
00618     (*pcPrescaler) = 0xFF;
00619     return;
00620   }
00621   
00622   /* prescaler is really 2 as min value */
00623   (*pcPrescaler)=(n/0xFF)+2;
00624   (*pcCounter) = n / (*pcPrescaler);
00625   
00626   /* check if the error is minimum */
00627   err=S_ABS((float)(*pcCounter)*(*pcPrescaler)*1210000/nXtalFrequency-fDesiredMsec);
00628   
00629   if((*pcCounter)<=254)
00630   {
00631     if(S_ABS((float)((*pcCounter)+1)*(*pcPrescaler)*1210000/nXtalFrequency-fDesiredMsec)<err)
00632       (*pcCounter)=(*pcCounter)+1;
00633   }
00634     
00635   /* decrement prescaler and counter according to the logic of this timer in spirit1 */
00636   (*pcPrescaler)--;
00637   if((*pcCounter)>1)
00638     (*pcCounter)--;
00639   else
00640     (*pcCounter)=1;
00641 }
00642 
00643 
00644 /**
00645  * @brief  Sets the RX timeout stop conditions.
00646  * @param  xStopCondition new stop condition.
00647  *         This parameter can be any value of @ref RxTimeoutStopCondition.
00648  * @retval None
00649  */
00650 void SpiritTimerSetRxTimeoutStopCondition(RxTimeoutStopCondition xStopCondition)
00651 {
00652   uint8_t tempRegValue[2];
00653 
00654   /* Check the parameters */
00655   s_assert_param(IS_RX_TIMEOUT_STOP_CONDITION(xStopCondition));
00656 
00657   /* Reads value on the PKT_FLT_OPTIONS and PROTOCOL2 register */
00658   g_xStatus = SpiritSpiReadRegisters(PCKT_FLT_OPTIONS_BASE, 2, tempRegValue);
00659 
00660   tempRegValue[0] &= 0xBF;
00661   tempRegValue[0] |= ((xStopCondition & 0x08)  << 3);
00662 
00663   tempRegValue[1] &= 0x1F;
00664   tempRegValue[1] |= (xStopCondition << 5);
00665 
00666   /* Writes value on the PKT_FLT_OPTIONS and PROTOCOL2 register */
00667   g_xStatus = SpiritSpiWriteRegisters(PCKT_FLT_OPTIONS_BASE, 2, tempRegValue);
00668 
00669 }
00670 
00671 /**
00672  * @brief  Sends the LDC_RELOAD command to SPIRIT. Reload the LDC timer with the value stored in the LDC_PRESCALER / COUNTER registers.
00673  * @param  None.
00674  * @retval None
00675  */
00676 void SpiritTimerReloadStrobe(void)
00677 {
00678   /* Sends the CMD_LDC_RELOAD command */
00679   g_xStatus = SpiritSpiCommandStrobes(COMMAND_LDC_RELOAD);
00680 
00681 }
00682 
00683 
00684 /**
00685  *@}
00686  */
00687 
00688 
00689 /**
00690  *@}
00691  */
00692 
00693 
00694 /**
00695  *@}
00696  */
00697 
00698 
00699 
00700 /******************* (C) COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/