Fork of my MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:45:51 2017 +0000
Revision:
0:f1d3878b8dd9
Initial commit

Who changed what in which revision?

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