Fork of my original MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:43:14 2017 +0000
Revision:
0:a1734fe1ec4b
Initial commit

Who changed what in which revision?

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