wifi test

Dependencies:   X_NUCLEO_IKS01A2 mbed-http

Committer:
JMF
Date:
Wed Sep 05 14:28:24 2018 +0000
Revision:
0:24d3eb812fd4
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 0:24d3eb812fd4 1 /**
JMF 0:24d3eb812fd4 2 ******************************************************************************
JMF 0:24d3eb812fd4 3 * @file SPIRIT_Timer.c
JMF 0:24d3eb812fd4 4 * @author VMA division - AMS
JMF 0:24d3eb812fd4 5 * @version 3.2.2
JMF 0:24d3eb812fd4 6 * @date 08-July-2015
JMF 0:24d3eb812fd4 7 * @brief Configuration and management of SPIRIT timers.
JMF 0:24d3eb812fd4 8 * @details
JMF 0:24d3eb812fd4 9 *
JMF 0:24d3eb812fd4 10 * @attention
JMF 0:24d3eb812fd4 11 *
JMF 0:24d3eb812fd4 12 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
JMF 0:24d3eb812fd4 13 *
JMF 0:24d3eb812fd4 14 * Redistribution and use in source and binary forms, with or without modification,
JMF 0:24d3eb812fd4 15 * are permitted provided that the following conditions are met:
JMF 0:24d3eb812fd4 16 * 1. Redistributions of source code must retain the above copyright notice,
JMF 0:24d3eb812fd4 17 * this list of conditions and the following disclaimer.
JMF 0:24d3eb812fd4 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
JMF 0:24d3eb812fd4 19 * this list of conditions and the following disclaimer in the documentation
JMF 0:24d3eb812fd4 20 * and/or other materials provided with the distribution.
JMF 0:24d3eb812fd4 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
JMF 0:24d3eb812fd4 22 * may be used to endorse or promote products derived from this software
JMF 0:24d3eb812fd4 23 * without specific prior written permission.
JMF 0:24d3eb812fd4 24 *
JMF 0:24d3eb812fd4 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
JMF 0:24d3eb812fd4 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
JMF 0:24d3eb812fd4 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
JMF 0:24d3eb812fd4 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
JMF 0:24d3eb812fd4 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
JMF 0:24d3eb812fd4 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
JMF 0:24d3eb812fd4 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
JMF 0:24d3eb812fd4 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
JMF 0:24d3eb812fd4 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
JMF 0:24d3eb812fd4 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
JMF 0:24d3eb812fd4 35 *
JMF 0:24d3eb812fd4 36 ******************************************************************************
JMF 0:24d3eb812fd4 37 */
JMF 0:24d3eb812fd4 38
JMF 0:24d3eb812fd4 39 /* Includes ------------------------------------------------------------------*/
JMF 0:24d3eb812fd4 40 #include "SPIRIT_Timer.h"
JMF 0:24d3eb812fd4 41 #include "SPIRIT_Radio.h"
JMF 0:24d3eb812fd4 42 #include "MCU_Interface.h"
JMF 0:24d3eb812fd4 43
JMF 0:24d3eb812fd4 44
JMF 0:24d3eb812fd4 45
JMF 0:24d3eb812fd4 46
JMF 0:24d3eb812fd4 47 /**
JMF 0:24d3eb812fd4 48 * @addtogroup SPIRIT_Libraries
JMF 0:24d3eb812fd4 49 * @{
JMF 0:24d3eb812fd4 50 */
JMF 0:24d3eb812fd4 51
JMF 0:24d3eb812fd4 52
JMF 0:24d3eb812fd4 53 /**
JMF 0:24d3eb812fd4 54 * @addtogroup SPIRIT_Timer
JMF 0:24d3eb812fd4 55 * @{
JMF 0:24d3eb812fd4 56 */
JMF 0:24d3eb812fd4 57
JMF 0:24d3eb812fd4 58
JMF 0:24d3eb812fd4 59 /**
JMF 0:24d3eb812fd4 60 * @defgroup Timer_Private_TypesDefinitions Timer Private Types Definitions
JMF 0:24d3eb812fd4 61 * @{
JMF 0:24d3eb812fd4 62 */
JMF 0:24d3eb812fd4 63
JMF 0:24d3eb812fd4 64 /**
JMF 0:24d3eb812fd4 65 *@}
JMF 0:24d3eb812fd4 66 */
JMF 0:24d3eb812fd4 67
JMF 0:24d3eb812fd4 68
JMF 0:24d3eb812fd4 69 /**
JMF 0:24d3eb812fd4 70 * @defgroup Timer_Private_Defines Timer Private Defines
JMF 0:24d3eb812fd4 71 * @{
JMF 0:24d3eb812fd4 72 */
JMF 0:24d3eb812fd4 73
JMF 0:24d3eb812fd4 74 /**
JMF 0:24d3eb812fd4 75 *@}
JMF 0:24d3eb812fd4 76 */
JMF 0:24d3eb812fd4 77
JMF 0:24d3eb812fd4 78
JMF 0:24d3eb812fd4 79 /**
JMF 0:24d3eb812fd4 80 * @defgroup Timer_Private_Macros Timer Private Macros
JMF 0:24d3eb812fd4 81 * @{
JMF 0:24d3eb812fd4 82 */
JMF 0:24d3eb812fd4 83
JMF 0:24d3eb812fd4 84
JMF 0:24d3eb812fd4 85 /**
JMF 0:24d3eb812fd4 86 *@}
JMF 0:24d3eb812fd4 87 */
JMF 0:24d3eb812fd4 88
JMF 0:24d3eb812fd4 89
JMF 0:24d3eb812fd4 90 /**
JMF 0:24d3eb812fd4 91 * @defgroup Timer_Private_Variables Timer Private Variables
JMF 0:24d3eb812fd4 92 * @{
JMF 0:24d3eb812fd4 93 */
JMF 0:24d3eb812fd4 94
JMF 0:24d3eb812fd4 95 /**
JMF 0:24d3eb812fd4 96 *@}
JMF 0:24d3eb812fd4 97 */
JMF 0:24d3eb812fd4 98
JMF 0:24d3eb812fd4 99
JMF 0:24d3eb812fd4 100 /**
JMF 0:24d3eb812fd4 101 * @defgroup Timer_Private_FunctionPrototypes Timer Private Function Prototypes
JMF 0:24d3eb812fd4 102 * @{
JMF 0:24d3eb812fd4 103 */
JMF 0:24d3eb812fd4 104
JMF 0:24d3eb812fd4 105 /**
JMF 0:24d3eb812fd4 106 *@}
JMF 0:24d3eb812fd4 107 */
JMF 0:24d3eb812fd4 108
JMF 0:24d3eb812fd4 109
JMF 0:24d3eb812fd4 110 /**
JMF 0:24d3eb812fd4 111 * @defgroup Timer_Private_Functions Timer Private Functions
JMF 0:24d3eb812fd4 112 * @{
JMF 0:24d3eb812fd4 113 */
JMF 0:24d3eb812fd4 114
JMF 0:24d3eb812fd4 115 /**
JMF 0:24d3eb812fd4 116 * @brief Enables or Disables the LDCR mode.
JMF 0:24d3eb812fd4 117 * @param xNewState new state for LDCR mode.
JMF 0:24d3eb812fd4 118 * This parameter can be: S_ENABLE or S_DISABLE.
JMF 0:24d3eb812fd4 119 * @retval None.
JMF 0:24d3eb812fd4 120 */
JMF 0:24d3eb812fd4 121 void SpiritTimerLdcrMode(SpiritFunctionalState xNewState)
JMF 0:24d3eb812fd4 122 {
JMF 0:24d3eb812fd4 123 uint8_t tempRegValue;
JMF 0:24d3eb812fd4 124
JMF 0:24d3eb812fd4 125 /* Reads the register value */
JMF 0:24d3eb812fd4 126 g_xStatus = SpiritSpiReadRegisters(PROTOCOL2_BASE, 1, &tempRegValue);
JMF 0:24d3eb812fd4 127
JMF 0:24d3eb812fd4 128 /* Mask the read value to enable or disable the LDC mode */
JMF 0:24d3eb812fd4 129 if(xNewState==S_ENABLE)
JMF 0:24d3eb812fd4 130 {
JMF 0:24d3eb812fd4 131 tempRegValue |= PROTOCOL2_LDC_MODE_MASK;
JMF 0:24d3eb812fd4 132 }
JMF 0:24d3eb812fd4 133 else
JMF 0:24d3eb812fd4 134 {
JMF 0:24d3eb812fd4 135 tempRegValue &= ~PROTOCOL2_LDC_MODE_MASK;
JMF 0:24d3eb812fd4 136 }
JMF 0:24d3eb812fd4 137
JMF 0:24d3eb812fd4 138 /* Writes the register to Enable or Disable the LDCR mode */
JMF 0:24d3eb812fd4 139 g_xStatus = SpiritSpiWriteRegisters(PROTOCOL2_BASE, 1, &tempRegValue);
JMF 0:24d3eb812fd4 140
JMF 0:24d3eb812fd4 141 }
JMF 0:24d3eb812fd4 142
JMF 0:24d3eb812fd4 143
JMF 0:24d3eb812fd4 144 /**
JMF 0:24d3eb812fd4 145 * @brief Enables or Disables the LDCR timer reloading with the value stored in the LDCR_RELOAD registers.
JMF 0:24d3eb812fd4 146 * @param xNewState new state for LDCR reloading.
JMF 0:24d3eb812fd4 147 * This parameter can be: S_ENABLE or S_DISABLE.
JMF 0:24d3eb812fd4 148 * @retval None.
JMF 0:24d3eb812fd4 149 */
JMF 0:24d3eb812fd4 150 void SpiritTimerLdcrAutoReload(SpiritFunctionalState xNewState)
JMF 0:24d3eb812fd4 151 {
JMF 0:24d3eb812fd4 152 uint8_t tempRegValue;
JMF 0:24d3eb812fd4 153
JMF 0:24d3eb812fd4 154 /* Reads the register value */
JMF 0:24d3eb812fd4 155 g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
JMF 0:24d3eb812fd4 156
JMF 0:24d3eb812fd4 157 /* Mask te read value to enable or disable the reload on sync mode */
JMF 0:24d3eb812fd4 158 if(xNewState==S_ENABLE)
JMF 0:24d3eb812fd4 159 {
JMF 0:24d3eb812fd4 160 tempRegValue |= PROTOCOL1_LDC_RELOAD_ON_SYNC_MASK;
JMF 0:24d3eb812fd4 161 }
JMF 0:24d3eb812fd4 162 else
JMF 0:24d3eb812fd4 163 {
JMF 0:24d3eb812fd4 164 tempRegValue &= ~PROTOCOL1_LDC_RELOAD_ON_SYNC_MASK;
JMF 0:24d3eb812fd4 165 }
JMF 0:24d3eb812fd4 166
JMF 0:24d3eb812fd4 167 /* Writes the register to Enable or Disable the Auto Reload */
JMF 0:24d3eb812fd4 168 g_xStatus = SpiritSpiWriteRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
JMF 0:24d3eb812fd4 169
JMF 0:24d3eb812fd4 170 }
JMF 0:24d3eb812fd4 171
JMF 0:24d3eb812fd4 172
JMF 0:24d3eb812fd4 173 /**
JMF 0:24d3eb812fd4 174 * @brief Returns the LDCR timer reload bit.
JMF 0:24d3eb812fd4 175 * @param None.
JMF 0:24d3eb812fd4 176 * @retval SpiritFunctionalState: value of the reload bit.
JMF 0:24d3eb812fd4 177 */
JMF 0:24d3eb812fd4 178 SpiritFunctionalState SpiritTimerLdcrGetAutoReload(void)
JMF 0:24d3eb812fd4 179 {
JMF 0:24d3eb812fd4 180 uint8_t tempRegValue;
JMF 0:24d3eb812fd4 181
JMF 0:24d3eb812fd4 182 /* Reads the register value */
JMF 0:24d3eb812fd4 183 g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
JMF 0:24d3eb812fd4 184
JMF 0:24d3eb812fd4 185 return (SpiritFunctionalState)(tempRegValue & 0x80);
JMF 0:24d3eb812fd4 186
JMF 0:24d3eb812fd4 187 }
JMF 0:24d3eb812fd4 188
JMF 0:24d3eb812fd4 189 /**
JMF 0:24d3eb812fd4 190 * @brief Sets the RX timeout timer initialization registers with the values of COUNTER and PRESCALER according to the formula: Trx=PRESCALER*COUNTER*Tck.
JMF 0:24d3eb812fd4 191 * Remember that it is possible to have infinite RX_Timeout writing 0 in the RX_Timeout_Counter and/or RX_Timeout_Prescaler registers.
JMF 0:24d3eb812fd4 192 * @param cCounter value for the timer counter.
JMF 0:24d3eb812fd4 193 * This parameter must be an uint8_t.
JMF 0:24d3eb812fd4 194 * @param cPrescaler value for the timer prescaler.
JMF 0:24d3eb812fd4 195 * This parameter must be an uint8_t.
JMF 0:24d3eb812fd4 196 * @retval None.
JMF 0:24d3eb812fd4 197 */
JMF 0:24d3eb812fd4 198 void SpiritTimerSetRxTimeout(uint8_t cCounter , uint8_t cPrescaler)
JMF 0:24d3eb812fd4 199 {
JMF 0:24d3eb812fd4 200 uint8_t tempRegValue[2]={cPrescaler,cCounter};
JMF 0:24d3eb812fd4 201
JMF 0:24d3eb812fd4 202 /* Writes the prescaler and counter value for RX timeout in the corresponding register */
JMF 0:24d3eb812fd4 203 g_xStatus = SpiritSpiWriteRegisters(TIMERS5_RX_TIMEOUT_PRESCALER_BASE, 2, tempRegValue);
JMF 0:24d3eb812fd4 204
JMF 0:24d3eb812fd4 205 }
JMF 0:24d3eb812fd4 206
JMF 0:24d3eb812fd4 207
JMF 0:24d3eb812fd4 208 /**
JMF 0:24d3eb812fd4 209 * @brief Sets the RX timeout timer counter and prescaler from the desired value in ms. it is possible to fix the RX_Timeout to
JMF 0:24d3eb812fd4 210 * a minimum value of 50.417us to a maximum value of about 3.28 s.
JMF 0:24d3eb812fd4 211 * @param fDesiredMsec desired timer value.
JMF 0:24d3eb812fd4 212 * This parameter must be a float.
JMF 0:24d3eb812fd4 213 * @retval None
JMF 0:24d3eb812fd4 214 */
JMF 0:24d3eb812fd4 215
JMF 0:24d3eb812fd4 216 void SpiritTimerSetRxTimeoutMs(float fDesiredMsec)
JMF 0:24d3eb812fd4 217 {
JMF 0:24d3eb812fd4 218 uint8_t tempRegValue[2];
JMF 0:24d3eb812fd4 219
JMF 0:24d3eb812fd4 220 /* Computes the counter and prescaler value */
JMF 0:24d3eb812fd4 221 SpiritTimerComputeRxTimeoutValues(fDesiredMsec , &tempRegValue[1] , &tempRegValue[0]);
JMF 0:24d3eb812fd4 222
JMF 0:24d3eb812fd4 223 /* Writes the prescaler and counter value for RX timeout in the corresponding register */
JMF 0:24d3eb812fd4 224 g_xStatus = SpiritSpiWriteRegisters(TIMERS5_RX_TIMEOUT_PRESCALER_BASE, 2, tempRegValue);
JMF 0:24d3eb812fd4 225
JMF 0:24d3eb812fd4 226 }
JMF 0:24d3eb812fd4 227
JMF 0:24d3eb812fd4 228
JMF 0:24d3eb812fd4 229 /**
JMF 0:24d3eb812fd4 230 * @brief Sets the RX timeout timer counter. If it is equal to 0 the timeout is infinite.
JMF 0:24d3eb812fd4 231 * @param cCounter value for the timer counter.
JMF 0:24d3eb812fd4 232 * This parameter must be an uint8_t.
JMF 0:24d3eb812fd4 233 * @retval None.
JMF 0:24d3eb812fd4 234 */
JMF 0:24d3eb812fd4 235 void SpiritTimerSetRxTimeoutCounter(uint8_t cCounter)
JMF 0:24d3eb812fd4 236 {
JMF 0:24d3eb812fd4 237 /* Writes the counter value for RX timeout in the corresponding register */
JMF 0:24d3eb812fd4 238 g_xStatus = SpiritSpiWriteRegisters(TIMERS4_RX_TIMEOUT_COUNTER_BASE, 1, &cCounter);
JMF 0:24d3eb812fd4 239
JMF 0:24d3eb812fd4 240 }
JMF 0:24d3eb812fd4 241
JMF 0:24d3eb812fd4 242
JMF 0:24d3eb812fd4 243 /**
JMF 0:24d3eb812fd4 244 * @brief Sets the RX timeout timer prescaler. If it is equal to 0 the timeout is infinite.
JMF 0:24d3eb812fd4 245 * @param cPrescaler value for the timer prescaler.
JMF 0:24d3eb812fd4 246 * This parameter must be an uint8_t.
JMF 0:24d3eb812fd4 247 * @retval None
JMF 0:24d3eb812fd4 248 */
JMF 0:24d3eb812fd4 249 void SpiritTimerSetRxTimeoutPrescaler(uint8_t cPrescaler)
JMF 0:24d3eb812fd4 250 {
JMF 0:24d3eb812fd4 251 /* Writes the prescaler value for RX timeout in the corresponding register */
JMF 0:24d3eb812fd4 252 g_xStatus = SpiritSpiWriteRegisters(TIMERS5_RX_TIMEOUT_PRESCALER_BASE, 1, &cPrescaler);
JMF 0:24d3eb812fd4 253
JMF 0:24d3eb812fd4 254 }
JMF 0:24d3eb812fd4 255
JMF 0:24d3eb812fd4 256
JMF 0:24d3eb812fd4 257 /**
JMF 0:24d3eb812fd4 258 * @brief Returns the RX timeout timer.
JMF 0:24d3eb812fd4 259 * @param pfTimeoutMsec pointer to the variable in which the timeout expressed in milliseconds has to be stored.
JMF 0:24d3eb812fd4 260 * If the returned value is 0, it means that the RX_Timeout is infinite.
JMF 0:24d3eb812fd4 261 * This parameter must be a float*.
JMF 0:24d3eb812fd4 262 * @param pcCounter pointer to the variable in which the timer counter has to be stored.
JMF 0:24d3eb812fd4 263 * This parameter must be an uint8_t*.
JMF 0:24d3eb812fd4 264 * @param pcPrescaler pointer to the variable in which the timer prescaler has to be stored.
JMF 0:24d3eb812fd4 265 * This parameter must be an uint8_t*.
JMF 0:24d3eb812fd4 266 * @retval None.
JMF 0:24d3eb812fd4 267 */
JMF 0:24d3eb812fd4 268 void SpiritTimerGetRxTimeout(float* pfTimeoutMsec, uint8_t* pcCounter , uint8_t* pcPrescaler)
JMF 0:24d3eb812fd4 269 {
JMF 0:24d3eb812fd4 270 uint8_t tempRegValue[2];
JMF 0:24d3eb812fd4 271
JMF 0:24d3eb812fd4 272 /* Reads the RX timeout registers value */
JMF 0:24d3eb812fd4 273 g_xStatus = SpiritSpiReadRegisters(TIMERS5_RX_TIMEOUT_PRESCALER_BASE, 2, tempRegValue);
JMF 0:24d3eb812fd4 274
JMF 0:24d3eb812fd4 275 /* Returns values */
JMF 0:24d3eb812fd4 276 (*pcPrescaler) = tempRegValue[0];
JMF 0:24d3eb812fd4 277 (*pcCounter) = tempRegValue[1];
JMF 0:24d3eb812fd4 278
JMF 0:24d3eb812fd4 279 float nXtalFrequency = (float)SpiritRadioGetXtalFrequency();
JMF 0:24d3eb812fd4 280 if(nXtalFrequency>DOUBLE_XTAL_THR) {
JMF 0:24d3eb812fd4 281 nXtalFrequency /= 2.0;
JMF 0:24d3eb812fd4 282 }
JMF 0:24d3eb812fd4 283 nXtalFrequency /= 1000.0;
JMF 0:24d3eb812fd4 284 *pfTimeoutMsec = (float)((tempRegValue[0]+1)*tempRegValue[1]*(1210.0/nXtalFrequency));
JMF 0:24d3eb812fd4 285
JMF 0:24d3eb812fd4 286
JMF 0:24d3eb812fd4 287 }
JMF 0:24d3eb812fd4 288
JMF 0:24d3eb812fd4 289
JMF 0:24d3eb812fd4 290 /**
JMF 0:24d3eb812fd4 291 * @brief Sets the LDCR wake up timer initialization registers with the values of
JMF 0:24d3eb812fd4 292 * COUNTER and PRESCALER according to the formula: Twu=(PRESCALER +1)*(COUNTER+1)*Tck, where
JMF 0:24d3eb812fd4 293 * Tck = 28.818 us. The minimum vale of the wakeup timeout is 28.818us (PRESCALER and
JMF 0:24d3eb812fd4 294 * COUNTER equals to 0) and the maximum value is about 1.89 s (PRESCALER anc COUNTER equals
JMF 0:24d3eb812fd4 295 * to 255).
JMF 0:24d3eb812fd4 296 * @param cCounter value for the timer counter.
JMF 0:24d3eb812fd4 297 * This parameter must be an uint8_t.
JMF 0:24d3eb812fd4 298 * @param cPrescaler value for the timer prescaler.
JMF 0:24d3eb812fd4 299 * This parameter must be an uint8_t.
JMF 0:24d3eb812fd4 300 * @retval None.
JMF 0:24d3eb812fd4 301 */
JMF 0:24d3eb812fd4 302 void SpiritTimerSetWakeUpTimer(uint8_t cCounter , uint8_t cPrescaler)
JMF 0:24d3eb812fd4 303 {
JMF 0:24d3eb812fd4 304 uint8_t tempRegValue[2]={cPrescaler,cCounter};
JMF 0:24d3eb812fd4 305
JMF 0:24d3eb812fd4 306 /* Writes the counter and prescaler value of wake-up timer in the corresponding register */
JMF 0:24d3eb812fd4 307 g_xStatus = SpiritSpiWriteRegisters(TIMERS3_LDC_PRESCALER_BASE, 2, tempRegValue);
JMF 0:24d3eb812fd4 308
JMF 0:24d3eb812fd4 309 }
JMF 0:24d3eb812fd4 310
JMF 0:24d3eb812fd4 311
JMF 0:24d3eb812fd4 312 /**
JMF 0:24d3eb812fd4 313 * @brief Sets the LDCR wake up timer counter and prescaler from the desired value in ms,
JMF 0:24d3eb812fd4 314 * according to the formula: Twu=(PRESCALER +1)*(COUNTER+1)*Tck, where Tck = 28.818 us.
JMF 0:24d3eb812fd4 315 * The minimum vale of the wakeup timeout is 28.818us (PRESCALER and COUNTER equals to 0)
JMF 0:24d3eb812fd4 316 * and the maximum value is about 1.89 s (PRESCALER anc COUNTER equals to 255).
JMF 0:24d3eb812fd4 317 * @param fDesiredMsec desired timer value.
JMF 0:24d3eb812fd4 318 * This parameter must be a float.
JMF 0:24d3eb812fd4 319 * @retval None.
JMF 0:24d3eb812fd4 320 */
JMF 0:24d3eb812fd4 321 void SpiritTimerSetWakeUpTimerMs(float fDesiredMsec)
JMF 0:24d3eb812fd4 322 {
JMF 0:24d3eb812fd4 323 uint8_t tempRegValue[2];
JMF 0:24d3eb812fd4 324
JMF 0:24d3eb812fd4 325 /* Computes counter and prescaler */
JMF 0:24d3eb812fd4 326 SpiritTimerComputeWakeUpValues(fDesiredMsec , &tempRegValue[1] , &tempRegValue[0]);
JMF 0:24d3eb812fd4 327
JMF 0:24d3eb812fd4 328 /* Writes the counter and prescaler value of wake-up timer in the corresponding register */
JMF 0:24d3eb812fd4 329 g_xStatus = SpiritSpiWriteRegisters(TIMERS3_LDC_PRESCALER_BASE, 2, tempRegValue);
JMF 0:24d3eb812fd4 330
JMF 0:24d3eb812fd4 331 }
JMF 0:24d3eb812fd4 332
JMF 0:24d3eb812fd4 333
JMF 0:24d3eb812fd4 334 /**
JMF 0:24d3eb812fd4 335 * @brief Sets the LDCR wake up timer counter. Remember that this value is incresead by one in the Twu calculation.
JMF 0:24d3eb812fd4 336 * Twu=(PRESCALER +1)*(COUNTER+1)*Tck, where Tck = 28.818 us
JMF 0:24d3eb812fd4 337 * @param cCounter value for the timer counter.
JMF 0:24d3eb812fd4 338 * This parameter must be an uint8_t.
JMF 0:24d3eb812fd4 339 * @retval None.
JMF 0:24d3eb812fd4 340 */
JMF 0:24d3eb812fd4 341 void SpiritTimerSetWakeUpTimerCounter(uint8_t cCounter)
JMF 0:24d3eb812fd4 342 {
JMF 0:24d3eb812fd4 343 /* Writes the counter value for Wake_Up timer in the corresponding register */
JMF 0:24d3eb812fd4 344 g_xStatus = SpiritSpiWriteRegisters(TIMERS2_LDC_COUNTER_BASE, 1, &cCounter);
JMF 0:24d3eb812fd4 345
JMF 0:24d3eb812fd4 346 }
JMF 0:24d3eb812fd4 347
JMF 0:24d3eb812fd4 348
JMF 0:24d3eb812fd4 349 /**
JMF 0:24d3eb812fd4 350 * @brief Sets the LDCR wake up timer prescaler. Remember that this value is incresead by one in the Twu calculation.
JMF 0:24d3eb812fd4 351 * Twu=(PRESCALER +1)*(COUNTER+1)*Tck, where Tck = 28.818 us
JMF 0:24d3eb812fd4 352 * @param cPrescaler value for the timer prescaler.
JMF 0:24d3eb812fd4 353 * This parameter must be an uint8_t.
JMF 0:24d3eb812fd4 354 * @retval None.
JMF 0:24d3eb812fd4 355 */
JMF 0:24d3eb812fd4 356 void SpiritTimerSetWakeUpTimerPrescaler(uint8_t cPrescaler)
JMF 0:24d3eb812fd4 357 {
JMF 0:24d3eb812fd4 358 /* Writes the prescaler value for Wake_Up timer in the corresponding register */
JMF 0:24d3eb812fd4 359 g_xStatus = SpiritSpiWriteRegisters(TIMERS3_LDC_PRESCALER_BASE, 1, &cPrescaler);
JMF 0:24d3eb812fd4 360
JMF 0:24d3eb812fd4 361 }
JMF 0:24d3eb812fd4 362
JMF 0:24d3eb812fd4 363
JMF 0:24d3eb812fd4 364 /**
JMF 0:24d3eb812fd4 365 * @brief Returns the LDCR wake up timer, according to the formula: Twu=(PRESCALER +1)*(COUNTER+1)*Tck, where Tck = 28.818 us.
JMF 0:24d3eb812fd4 366 * @param pfWakeUpMsec pointer to the variable in which the wake-up time expressed in milliseconds has to be stored.
JMF 0:24d3eb812fd4 367 * This parameter must be a float*.
JMF 0:24d3eb812fd4 368 * @param pcCounter pointer to the variable in which the timer counter has to be stored.
JMF 0:24d3eb812fd4 369 * This parameter must be an uint8_t*.
JMF 0:24d3eb812fd4 370 * @param pcPrescaler pointer to the variable in which the timer prescaler has to be stored.
JMF 0:24d3eb812fd4 371 * This parameter must be an uint8_t*.
JMF 0:24d3eb812fd4 372 * @retval None.
JMF 0:24d3eb812fd4 373 */
JMF 0:24d3eb812fd4 374 void SpiritTimerGetWakeUpTimer(float* pfWakeUpMsec, uint8_t* pcCounter , uint8_t* pcPrescaler)
JMF 0:24d3eb812fd4 375 {
JMF 0:24d3eb812fd4 376 uint8_t tempRegValue[2];
JMF 0:24d3eb812fd4 377 //uint32_t xtal=SpiritRadioGetXtalFrequency();
JMF 0:24d3eb812fd4 378 float rco_freq;
JMF 0:24d3eb812fd4 379
JMF 0:24d3eb812fd4 380 rco_freq=(float)SpiritTimerGetRcoFrequency();
JMF 0:24d3eb812fd4 381
JMF 0:24d3eb812fd4 382 /* Reads the Wake_Up timer registers value */
JMF 0:24d3eb812fd4 383 g_xStatus = SpiritSpiReadRegisters(TIMERS3_LDC_PRESCALER_BASE, 2, tempRegValue);
JMF 0:24d3eb812fd4 384
JMF 0:24d3eb812fd4 385 /* Returns values */
JMF 0:24d3eb812fd4 386 (*pcPrescaler)=tempRegValue[0];
JMF 0:24d3eb812fd4 387 (*pcCounter)=tempRegValue[1];
JMF 0:24d3eb812fd4 388 *pfWakeUpMsec = (float)((((*pcPrescaler)+1)*((*pcCounter)+1)*(1000.0/rco_freq)));
JMF 0:24d3eb812fd4 389
JMF 0:24d3eb812fd4 390 }
JMF 0:24d3eb812fd4 391
JMF 0:24d3eb812fd4 392
JMF 0:24d3eb812fd4 393 /**
JMF 0:24d3eb812fd4 394 * @brief Sets the LDCR wake up timer reloading registers with the values of
JMF 0:24d3eb812fd4 395 * COUNTER and PRESCALER according to the formula: Twu=(PRESCALER +1)*(COUNTER+1)*Tck, where
JMF 0:24d3eb812fd4 396 * Tck = 28.818 us. The minimum vale of the wakeup timeout is 28.818us (PRESCALER and
JMF 0:24d3eb812fd4 397 * COUNTER equals to 0) and the maximum value is about 1.89 s (PRESCALER anc COUNTER equals
JMF 0:24d3eb812fd4 398 * to 255).
JMF 0:24d3eb812fd4 399 * @param cCounter reload value for the timer counter.
JMF 0:24d3eb812fd4 400 * This parameter must be an uint8_t.
JMF 0:24d3eb812fd4 401 * @param cPrescaler reload value for the timer prescaler.
JMF 0:24d3eb812fd4 402 * This parameter must be an uint8_t.
JMF 0:24d3eb812fd4 403 * @retval None.
JMF 0:24d3eb812fd4 404 */
JMF 0:24d3eb812fd4 405 void SpiritTimerSetWakeUpTimerReload(uint8_t cCounter , uint8_t cPrescaler)
JMF 0:24d3eb812fd4 406 {
JMF 0:24d3eb812fd4 407 uint8_t tempRegValue[2]={cPrescaler,cCounter};
JMF 0:24d3eb812fd4 408
JMF 0:24d3eb812fd4 409 /* Writes the counter and prescaler value of reload wake-up timer in the corresponding register */
JMF 0:24d3eb812fd4 410 g_xStatus = SpiritSpiWriteRegisters(TIMERS1_LDC_RELOAD_PRESCALER_BASE, 2, tempRegValue);
JMF 0:24d3eb812fd4 411
JMF 0:24d3eb812fd4 412 }
JMF 0:24d3eb812fd4 413
JMF 0:24d3eb812fd4 414
JMF 0:24d3eb812fd4 415 /**
JMF 0:24d3eb812fd4 416 * @brief Sets the LDCR wake up reload timer counter and prescaler from the desired value in ms,
JMF 0:24d3eb812fd4 417 * according to the formula: Twu=(PRESCALER +1)*(COUNTER+1)*Tck, where Tck = 28.818 us.
JMF 0:24d3eb812fd4 418 * The minimum vale of the wakeup timeout is 28.818us (PRESCALER and COUNTER equals to 0)
JMF 0:24d3eb812fd4 419 * and the maximum value is about 1.89 s (PRESCALER anc COUNTER equals to 255).
JMF 0:24d3eb812fd4 420 * @param fDesiredMsec desired timer value.
JMF 0:24d3eb812fd4 421 * This parameter must be a float.
JMF 0:24d3eb812fd4 422 * @retval None.
JMF 0:24d3eb812fd4 423 */
JMF 0:24d3eb812fd4 424 void SpiritTimerSetWakeUpTimerReloadMs(float fDesiredMsec)
JMF 0:24d3eb812fd4 425 {
JMF 0:24d3eb812fd4 426 uint8_t tempRegValue[2];
JMF 0:24d3eb812fd4 427
JMF 0:24d3eb812fd4 428 /* Computes counter and prescaler */
JMF 0:24d3eb812fd4 429 SpiritTimerComputeWakeUpValues(fDesiredMsec , &tempRegValue[1] , &tempRegValue[0]);
JMF 0:24d3eb812fd4 430
JMF 0:24d3eb812fd4 431 /* Writes the counter and prescaler value of reload wake-up timer in the corresponding register */
JMF 0:24d3eb812fd4 432 g_xStatus = SpiritSpiWriteRegisters(TIMERS1_LDC_RELOAD_PRESCALER_BASE, 2, tempRegValue);
JMF 0:24d3eb812fd4 433
JMF 0:24d3eb812fd4 434 }
JMF 0:24d3eb812fd4 435
JMF 0:24d3eb812fd4 436
JMF 0:24d3eb812fd4 437 /**
JMF 0:24d3eb812fd4 438 * @brief Sets the LDCR wake up timer reload counter. Remember that this value is incresead by one in the Twu calculation.
JMF 0:24d3eb812fd4 439 * Twu=(PRESCALER +1)*(COUNTER+1)*Tck, where Tck = 28.818 us
JMF 0:24d3eb812fd4 440 * @param cCounter value for the timer counter.
JMF 0:24d3eb812fd4 441 * This parameter must be an uint8_t.
JMF 0:24d3eb812fd4 442 * @retval None
JMF 0:24d3eb812fd4 443 */
JMF 0:24d3eb812fd4 444 void SpiritTimerSetWakeUpTimerReloadCounter(uint8_t cCounter)
JMF 0:24d3eb812fd4 445 {
JMF 0:24d3eb812fd4 446 /* Writes the counter value for reload Wake_Up timer in the corresponding register */
JMF 0:24d3eb812fd4 447 g_xStatus = SpiritSpiWriteRegisters(TIMERS0_LDC_RELOAD_COUNTER_BASE, 1, &cCounter);
JMF 0:24d3eb812fd4 448
JMF 0:24d3eb812fd4 449 }
JMF 0:24d3eb812fd4 450
JMF 0:24d3eb812fd4 451
JMF 0:24d3eb812fd4 452 /**
JMF 0:24d3eb812fd4 453 * @brief Sets the LDCR wake up timer reload prescaler. Remember that this value is incresead by one in the Twu calculation.
JMF 0:24d3eb812fd4 454 * Twu=(PRESCALER +1)*(COUNTER+1)*Tck, where Tck = 28.818 us
JMF 0:24d3eb812fd4 455 * @param cPrescaler value for the timer prescaler.
JMF 0:24d3eb812fd4 456 * This parameter must be an uint8_t.
JMF 0:24d3eb812fd4 457 * @retval None
JMF 0:24d3eb812fd4 458 */
JMF 0:24d3eb812fd4 459 void SpiritTimerSetWakeUpTimerReloadPrescaler(uint8_t cPrescaler)
JMF 0:24d3eb812fd4 460 {
JMF 0:24d3eb812fd4 461 /* Writes the prescaler value for reload Wake_Up timer in the corresponding register */
JMF 0:24d3eb812fd4 462 g_xStatus = SpiritSpiWriteRegisters(TIMERS1_LDC_RELOAD_PRESCALER_BASE, 1, &cPrescaler);
JMF 0:24d3eb812fd4 463
JMF 0:24d3eb812fd4 464 }
JMF 0:24d3eb812fd4 465
JMF 0:24d3eb812fd4 466
JMF 0:24d3eb812fd4 467 /**
JMF 0:24d3eb812fd4 468 * @brief Returns the LDCR wake up reload timer, according to the formula: Twu=(PRESCALER +1)*(COUNTER+1)*Tck, where Tck = 28.818 us.
JMF 0:24d3eb812fd4 469 * @param pfWakeUpReloadMsec pointer to the variable in which the wake-up reload time expressed in milliseconds has to be stored.
JMF 0:24d3eb812fd4 470 * This parameter must be a float*.
JMF 0:24d3eb812fd4 471 * @param pcCounter pointer to the variable in which the timer counter has to be stored.
JMF 0:24d3eb812fd4 472 * This parameter must be an uint8_t*.
JMF 0:24d3eb812fd4 473 * @param pcPrescaler pointer to the variable in which the timer prescaler has to be stored.
JMF 0:24d3eb812fd4 474 * This parameter must be an uint8_t*.
JMF 0:24d3eb812fd4 475 * @retval None.
JMF 0:24d3eb812fd4 476 */
JMF 0:24d3eb812fd4 477 void SpiritTimerGetWakeUpTimerReload(float* pfWakeUpReloadMsec, uint8_t* pcCounter , uint8_t* pcPrescaler)
JMF 0:24d3eb812fd4 478 {
JMF 0:24d3eb812fd4 479 uint8_t tempRegValue[2];
JMF 0:24d3eb812fd4 480 //uint32_t xtal=SpiritRadioGetXtalFrequency();
JMF 0:24d3eb812fd4 481 float rco_freq;
JMF 0:24d3eb812fd4 482
JMF 0:24d3eb812fd4 483 rco_freq=(float)SpiritTimerGetRcoFrequency();
JMF 0:24d3eb812fd4 484
JMF 0:24d3eb812fd4 485 /* Reads the reload Wake_Up timer registers value */
JMF 0:24d3eb812fd4 486 g_xStatus = SpiritSpiReadRegisters(TIMERS1_LDC_RELOAD_PRESCALER_BASE, 2, tempRegValue);
JMF 0:24d3eb812fd4 487
JMF 0:24d3eb812fd4 488 /* Returns values */
JMF 0:24d3eb812fd4 489 (*pcPrescaler)=tempRegValue[0];
JMF 0:24d3eb812fd4 490 (*pcCounter)=tempRegValue[1];
JMF 0:24d3eb812fd4 491 *pfWakeUpReloadMsec = (float)((((*pcPrescaler)+1)*((*pcCounter)+1)*(1000.0/rco_freq)));
JMF 0:24d3eb812fd4 492
JMF 0:24d3eb812fd4 493 }
JMF 0:24d3eb812fd4 494
JMF 0:24d3eb812fd4 495 /**
JMF 0:24d3eb812fd4 496 * @brief Computes and returns the RCO frequency.
JMF 0:24d3eb812fd4 497 * This frequency depends on the xtal frequency and the XTAL bit in register 0x01.
JMF 0:24d3eb812fd4 498 * @retval RCO frequency in Hz as an uint16_t.
JMF 0:24d3eb812fd4 499 */
JMF 0:24d3eb812fd4 500 uint16_t SpiritTimerGetRcoFrequency(void)
JMF 0:24d3eb812fd4 501 {
JMF 0:24d3eb812fd4 502 uint16_t rco_freq=34700;
JMF 0:24d3eb812fd4 503 uint32_t xtal=SpiritRadioGetXtalFrequency();
JMF 0:24d3eb812fd4 504
JMF 0:24d3eb812fd4 505 if(xtal>30000000) xtal/=2;
JMF 0:24d3eb812fd4 506
JMF 0:24d3eb812fd4 507 if(xtal==25000000)
JMF 0:24d3eb812fd4 508 {
JMF 0:24d3eb812fd4 509 uint8_t xtal_flag;
JMF 0:24d3eb812fd4 510 SpiritSpiReadRegisters(0x01, 1, &xtal_flag);
JMF 0:24d3eb812fd4 511 xtal_flag=(xtal_flag&0x40);
JMF 0:24d3eb812fd4 512
JMF 0:24d3eb812fd4 513 if(xtal_flag==0)
JMF 0:24d3eb812fd4 514 {
JMF 0:24d3eb812fd4 515 rco_freq=36100;
JMF 0:24d3eb812fd4 516 }
JMF 0:24d3eb812fd4 517 else
JMF 0:24d3eb812fd4 518 {
JMF 0:24d3eb812fd4 519 rco_freq=33300;
JMF 0:24d3eb812fd4 520 }
JMF 0:24d3eb812fd4 521 }
JMF 0:24d3eb812fd4 522
JMF 0:24d3eb812fd4 523 return rco_freq;
JMF 0:24d3eb812fd4 524 }
JMF 0:24d3eb812fd4 525
JMF 0:24d3eb812fd4 526 /**
JMF 0:24d3eb812fd4 527 * @brief Computes the values of the wakeup timer counter and prescaler from the user time expressed in millisecond.
JMF 0:24d3eb812fd4 528 * The prescaler and the counter values are computed maintaining the prescaler value as
JMF 0:24d3eb812fd4 529 * small as possible in order to obtain the best resolution, and in the meantime minimizing the error.
JMF 0:24d3eb812fd4 530 * @param fDesiredMsec desired wakeup timeout in millisecs.
JMF 0:24d3eb812fd4 531 * This parameter must be a float. Since the counter and prescaler are 8 bit registers the maximum
JMF 0:24d3eb812fd4 532 * reachable value is maxTime = fTclk x 256 x 256.
JMF 0:24d3eb812fd4 533 * @param pcCounter pointer to the variable in which the value for the wakeup timer counter has to be stored.
JMF 0:24d3eb812fd4 534 * This parameter must be a uint8_t*.
JMF 0:24d3eb812fd4 535 * @param pcPrescaler pointer to the variable in which the value for the wakeup timer prescaler has to be stored.
JMF 0:24d3eb812fd4 536 * This parameter must be an uint8_t*.
JMF 0:24d3eb812fd4 537 * @retval None
JMF 0:24d3eb812fd4 538 */
JMF 0:24d3eb812fd4 539 void SpiritTimerComputeWakeUpValues(float fDesiredMsec , uint8_t* pcCounter , uint8_t* pcPrescaler)
JMF 0:24d3eb812fd4 540 {
JMF 0:24d3eb812fd4 541 float rco_freq,err;
JMF 0:24d3eb812fd4 542 uint32_t n;
JMF 0:24d3eb812fd4 543
JMF 0:24d3eb812fd4 544 rco_freq=((float)SpiritTimerGetRcoFrequency())/1000;
JMF 0:24d3eb812fd4 545
JMF 0:24d3eb812fd4 546 /* N cycles in the time base of the timer:
JMF 0:24d3eb812fd4 547 - clock of the timer is RCO frequency
JMF 0:24d3eb812fd4 548 - divide times 1000 more because we have an input in ms (variable rco_freq is already this frequency divided by 1000)
JMF 0:24d3eb812fd4 549 */
JMF 0:24d3eb812fd4 550 n=(uint32_t)(fDesiredMsec*rco_freq);
JMF 0:24d3eb812fd4 551
JMF 0:24d3eb812fd4 552 /* check if it is possible to reach that target with prescaler and counter of spirit1 */
JMF 0:24d3eb812fd4 553 if(n/0xFF>0xFD)
JMF 0:24d3eb812fd4 554 {
JMF 0:24d3eb812fd4 555 /* if not return the maximum possible value */
JMF 0:24d3eb812fd4 556 (*pcCounter) = 0xFF;
JMF 0:24d3eb812fd4 557 (*pcPrescaler) = 0xFF;
JMF 0:24d3eb812fd4 558 return;
JMF 0:24d3eb812fd4 559 }
JMF 0:24d3eb812fd4 560
JMF 0:24d3eb812fd4 561 /* prescaler is really 2 as min value */
JMF 0:24d3eb812fd4 562 (*pcPrescaler)=(n/0xFF)+2;
JMF 0:24d3eb812fd4 563 (*pcCounter) = n / (*pcPrescaler);
JMF 0:24d3eb812fd4 564
JMF 0:24d3eb812fd4 565 /* check if the error is minimum */
JMF 0:24d3eb812fd4 566 err=S_ABS((float)(*pcCounter)*(*pcPrescaler)/rco_freq-fDesiredMsec);
JMF 0:24d3eb812fd4 567
JMF 0:24d3eb812fd4 568 if((*pcCounter)<=254)
JMF 0:24d3eb812fd4 569 {
JMF 0:24d3eb812fd4 570 if(S_ABS((float)((*pcCounter)+1)*(*pcPrescaler)/rco_freq-fDesiredMsec)<err)
JMF 0:24d3eb812fd4 571 (*pcCounter)=(*pcCounter)+1;
JMF 0:24d3eb812fd4 572 }
JMF 0:24d3eb812fd4 573
JMF 0:24d3eb812fd4 574 /* decrement prescaler and counter according to the logic of this timer in spirit1 */
JMF 0:24d3eb812fd4 575 (*pcPrescaler)--;
JMF 0:24d3eb812fd4 576 if((*pcCounter)>1)
JMF 0:24d3eb812fd4 577 (*pcCounter)--;
JMF 0:24d3eb812fd4 578 else
JMF 0:24d3eb812fd4 579 (*pcCounter)=1;
JMF 0:24d3eb812fd4 580 }
JMF 0:24d3eb812fd4 581
JMF 0:24d3eb812fd4 582
JMF 0:24d3eb812fd4 583 /**
JMF 0:24d3eb812fd4 584 * @brief Computes the values of the rx_timeout timer counter and prescaler from the user time expressed in millisecond.
JMF 0:24d3eb812fd4 585 * The prescaler and the counter values are computed maintaining the prescaler value as
JMF 0:24d3eb812fd4 586 * small as possible in order to obtain the best resolution, and in the meantime minimizing the error.
JMF 0:24d3eb812fd4 587 * @param fDesiredMsec desired rx_timeout in millisecs.
JMF 0:24d3eb812fd4 588 * This parameter must be a float. Since the counter and prescaler are 8 bit registers the maximum
JMF 0:24d3eb812fd4 589 * reachable value is maxTime = fTclk x 255 x 255.
JMF 0:24d3eb812fd4 590 * @param pcCounter pointer to the variable in which the value for the rx_timeout counter has to be stored.
JMF 0:24d3eb812fd4 591 * This parameter must be a uint8_t*.
JMF 0:24d3eb812fd4 592 * @param pcPrescaler pointer to the variable in which the value for the rx_timeout prescaler has to be stored.
JMF 0:24d3eb812fd4 593 * This parameter must be an uint8_t*.
JMF 0:24d3eb812fd4 594 * @retval None
JMF 0:24d3eb812fd4 595 */
JMF 0:24d3eb812fd4 596 void SpiritTimerComputeRxTimeoutValues(float fDesiredMsec , uint8_t* pcCounter , uint8_t* pcPrescaler)
JMF 0:24d3eb812fd4 597 {
JMF 0:24d3eb812fd4 598 uint32_t nXtalFrequency = SpiritRadioGetXtalFrequency();
JMF 0:24d3eb812fd4 599 uint32_t n;
JMF 0:24d3eb812fd4 600 float err;
JMF 0:24d3eb812fd4 601
JMF 0:24d3eb812fd4 602 /* if xtal is doubled divide it by 2 */
JMF 0:24d3eb812fd4 603 if(nXtalFrequency>DOUBLE_XTAL_THR) {
JMF 0:24d3eb812fd4 604 nXtalFrequency >>= 1;
JMF 0:24d3eb812fd4 605 }
JMF 0:24d3eb812fd4 606
JMF 0:24d3eb812fd4 607 /* N cycles in the time base of the timer:
JMF 0:24d3eb812fd4 608 - clock of the timer is xtal/1210
JMF 0:24d3eb812fd4 609 - divide times 1000 more because we have an input in ms
JMF 0:24d3eb812fd4 610 */
JMF 0:24d3eb812fd4 611 n=(uint32_t)(fDesiredMsec*nXtalFrequency/1210000);
JMF 0:24d3eb812fd4 612
JMF 0:24d3eb812fd4 613 /* check if it is possible to reach that target with prescaler and counter of spirit1 */
JMF 0:24d3eb812fd4 614 if(n/0xFF>0xFD)
JMF 0:24d3eb812fd4 615 {
JMF 0:24d3eb812fd4 616 /* if not return the maximum possible value */
JMF 0:24d3eb812fd4 617 (*pcCounter) = 0xFF;
JMF 0:24d3eb812fd4 618 (*pcPrescaler) = 0xFF;
JMF 0:24d3eb812fd4 619 return;
JMF 0:24d3eb812fd4 620 }
JMF 0:24d3eb812fd4 621
JMF 0:24d3eb812fd4 622 /* prescaler is really 2 as min value */
JMF 0:24d3eb812fd4 623 (*pcPrescaler)=(n/0xFF)+2;
JMF 0:24d3eb812fd4 624 (*pcCounter) = n / (*pcPrescaler);
JMF 0:24d3eb812fd4 625
JMF 0:24d3eb812fd4 626 /* check if the error is minimum */
JMF 0:24d3eb812fd4 627 err=S_ABS((float)(*pcCounter)*(*pcPrescaler)*1210000/nXtalFrequency-fDesiredMsec);
JMF 0:24d3eb812fd4 628
JMF 0:24d3eb812fd4 629 if((*pcCounter)<=254)
JMF 0:24d3eb812fd4 630 {
JMF 0:24d3eb812fd4 631 if(S_ABS((float)((*pcCounter)+1)*(*pcPrescaler)*1210000/nXtalFrequency-fDesiredMsec)<err)
JMF 0:24d3eb812fd4 632 (*pcCounter)=(*pcCounter)+1;
JMF 0:24d3eb812fd4 633 }
JMF 0:24d3eb812fd4 634
JMF 0:24d3eb812fd4 635 /* decrement prescaler and counter according to the logic of this timer in spirit1 */
JMF 0:24d3eb812fd4 636 (*pcPrescaler)--;
JMF 0:24d3eb812fd4 637 if((*pcCounter)>1)
JMF 0:24d3eb812fd4 638 (*pcCounter)--;
JMF 0:24d3eb812fd4 639 else
JMF 0:24d3eb812fd4 640 (*pcCounter)=1;
JMF 0:24d3eb812fd4 641 }
JMF 0:24d3eb812fd4 642
JMF 0:24d3eb812fd4 643
JMF 0:24d3eb812fd4 644 /**
JMF 0:24d3eb812fd4 645 * @brief Sets the RX timeout stop conditions.
JMF 0:24d3eb812fd4 646 * @param xStopCondition new stop condition.
JMF 0:24d3eb812fd4 647 * This parameter can be any value of @ref RxTimeoutStopCondition.
JMF 0:24d3eb812fd4 648 * @retval None
JMF 0:24d3eb812fd4 649 */
JMF 0:24d3eb812fd4 650 void SpiritTimerSetRxTimeoutStopCondition(RxTimeoutStopCondition xStopCondition)
JMF 0:24d3eb812fd4 651 {
JMF 0:24d3eb812fd4 652 uint8_t tempRegValue[2];
JMF 0:24d3eb812fd4 653
JMF 0:24d3eb812fd4 654 /* Check the parameters */
JMF 0:24d3eb812fd4 655 s_assert_param(IS_RX_TIMEOUT_STOP_CONDITION(xStopCondition));
JMF 0:24d3eb812fd4 656
JMF 0:24d3eb812fd4 657 /* Reads value on the PKT_FLT_OPTIONS and PROTOCOL2 register */
JMF 0:24d3eb812fd4 658 g_xStatus = SpiritSpiReadRegisters(PCKT_FLT_OPTIONS_BASE, 2, tempRegValue);
JMF 0:24d3eb812fd4 659
JMF 0:24d3eb812fd4 660 tempRegValue[0] &= 0xBF;
JMF 0:24d3eb812fd4 661 tempRegValue[0] |= ((xStopCondition & 0x08) << 3);
JMF 0:24d3eb812fd4 662
JMF 0:24d3eb812fd4 663 tempRegValue[1] &= 0x1F;
JMF 0:24d3eb812fd4 664 tempRegValue[1] |= (xStopCondition << 5);
JMF 0:24d3eb812fd4 665
JMF 0:24d3eb812fd4 666 /* Writes value on the PKT_FLT_OPTIONS and PROTOCOL2 register */
JMF 0:24d3eb812fd4 667 g_xStatus = SpiritSpiWriteRegisters(PCKT_FLT_OPTIONS_BASE, 2, tempRegValue);
JMF 0:24d3eb812fd4 668
JMF 0:24d3eb812fd4 669 }
JMF 0:24d3eb812fd4 670
JMF 0:24d3eb812fd4 671 /**
JMF 0:24d3eb812fd4 672 * @brief Sends the LDC_RELOAD command to SPIRIT. Reload the LDC timer with the value stored in the LDC_PRESCALER / COUNTER registers.
JMF 0:24d3eb812fd4 673 * @param None.
JMF 0:24d3eb812fd4 674 * @retval None
JMF 0:24d3eb812fd4 675 */
JMF 0:24d3eb812fd4 676 void SpiritTimerReloadStrobe(void)
JMF 0:24d3eb812fd4 677 {
JMF 0:24d3eb812fd4 678 /* Sends the CMD_LDC_RELOAD command */
JMF 0:24d3eb812fd4 679 g_xStatus = SpiritSpiCommandStrobes(COMMAND_LDC_RELOAD);
JMF 0:24d3eb812fd4 680
JMF 0:24d3eb812fd4 681 }
JMF 0:24d3eb812fd4 682
JMF 0:24d3eb812fd4 683
JMF 0:24d3eb812fd4 684 /**
JMF 0:24d3eb812fd4 685 *@}
JMF 0:24d3eb812fd4 686 */
JMF 0:24d3eb812fd4 687
JMF 0:24d3eb812fd4 688
JMF 0:24d3eb812fd4 689 /**
JMF 0:24d3eb812fd4 690 *@}
JMF 0:24d3eb812fd4 691 */
JMF 0:24d3eb812fd4 692
JMF 0:24d3eb812fd4 693
JMF 0:24d3eb812fd4 694 /**
JMF 0:24d3eb812fd4 695 *@}
JMF 0:24d3eb812fd4 696 */
JMF 0:24d3eb812fd4 697
JMF 0:24d3eb812fd4 698
JMF 0:24d3eb812fd4 699
JMF 0:24d3eb812fd4 700 /******************* (C) COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/