Added support for the WNC M14A2A Cellular LTE Data Module.

Dependencies:   WNC14A2AInterface

Easy Connect

Easily add all supported connectivity methods to your mbed OS project

This project is derived from https://developer.mbed.org/teams/sandbox/code/simple-mbed-client-example/file/dd6231df71bb/easy-connect.lib. It give user the ability to switch between connectivity methods and includes support for the WNC14A2A Data Module. The `NetworkInterface` API makes this easy, but you still need a mechanism for the user to select the connection method, The selection is made by modifying the `mbed_app.json` file and using `easy_connect()` from your application.

Specifying connectivity method

To add support for the WNC14A2A, add the following to your ``mbed_app.json`` file:

mbed_app.json

{
    "config": {
        "network-interface":{
            "help": "options are ETHERNET,WIFI_ESP8266,WIFI_ODIN,MESH_LOWPAN_ND,MESH_THREAD,WNC14A2A",
            "value": "WNC14A2A"
        }
    },
}

After you choose `WNC14A2A` you'll also need to indicate if you want debug output or not by Enabling (true) or Disabling (false) WNC_DEBUG.

If WNC_DEBUG is enabled, there are 3 different levels of debug output (selected via bit settings). These debug levels are set using the following values:

ValueDescription
1Basic WNC driver debug output
2Comprehensive WNC driver debug output
4Network Layer debug output

You can have any combination of these three bit values for a total value of 0 – 7.

WNC Debug Settings

    "config": {
        "WNC_DEBUG": {
            "value": false
        },
        "WNC_DEBUG_SETTING": {
            "value": 4
        },
    }

Using Easy Connect from your application

Easy Connect has just one function which will either return a `NetworkInterface`-pointer or `NULL`:

Sample Code

#include "easy-connect.h"

int main(int, char**) {
    NetworkInterface* network = easy_connect(true); /* has 1 argument, enable_logging (pass in true to log to serial port) */
    if (!network) {
        printf("Connecting to the network failed... See serial output.\r\n");
        return 1;
    }
 
    // Rest of your program
}

Tested on

  • K64F with Ethernet.
  • AT&T Cellular IoT Starter Kit with WNC M14A2A Cellular Data Module

The WNCInterface class currently supports the following version(s):

  • MPSS: M14A2A_v11.50.164451 APSS: M14A2A_v11.53.164451

License

This library is released under the Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License and may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Committer:
group-Avnet
Date:
Wed Apr 19 01:08:11 2017 +0000
Revision:
0:478cfd88041f
Initial commit

Who changed what in which revision?

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