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_Csma.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 CSMA.
vpcola 0:f1d3878b8dd9 8 * @details
vpcola 0:f1d3878b8dd9 9 * @attention
vpcola 0:f1d3878b8dd9 10 *
vpcola 0:f1d3878b8dd9 11 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
vpcola 0:f1d3878b8dd9 12 *
vpcola 0:f1d3878b8dd9 13 * Redistribution and use in source and binary forms, with or without modification,
vpcola 0:f1d3878b8dd9 14 * are permitted provided that the following conditions are met:
vpcola 0:f1d3878b8dd9 15 * 1. Redistributions of source code must retain the above copyright notice,
vpcola 0:f1d3878b8dd9 16 * this list of conditions and the following disclaimer.
vpcola 0:f1d3878b8dd9 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
vpcola 0:f1d3878b8dd9 18 * this list of conditions and the following disclaimer in the documentation
vpcola 0:f1d3878b8dd9 19 * and/or other materials provided with the distribution.
vpcola 0:f1d3878b8dd9 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
vpcola 0:f1d3878b8dd9 21 * may be used to endorse or promote products derived from this software
vpcola 0:f1d3878b8dd9 22 * without specific prior written permission.
vpcola 0:f1d3878b8dd9 23 *
vpcola 0:f1d3878b8dd9 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
vpcola 0:f1d3878b8dd9 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
vpcola 0:f1d3878b8dd9 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
vpcola 0:f1d3878b8dd9 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
vpcola 0:f1d3878b8dd9 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
vpcola 0:f1d3878b8dd9 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
vpcola 0:f1d3878b8dd9 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
vpcola 0:f1d3878b8dd9 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
vpcola 0:f1d3878b8dd9 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
vpcola 0:f1d3878b8dd9 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
vpcola 0:f1d3878b8dd9 34 *
vpcola 0:f1d3878b8dd9 35 ******************************************************************************
vpcola 0:f1d3878b8dd9 36 */
vpcola 0:f1d3878b8dd9 37
vpcola 0:f1d3878b8dd9 38 /* Includes ------------------------------------------------------------------*/
vpcola 0:f1d3878b8dd9 39 #include "SPIRIT_Csma.h"
vpcola 0:f1d3878b8dd9 40 #include "MCU_Interface.h"
vpcola 0:f1d3878b8dd9 41
vpcola 0:f1d3878b8dd9 42
vpcola 0:f1d3878b8dd9 43 /**
vpcola 0:f1d3878b8dd9 44 * @addtogroup SPIRIT_Libraries
vpcola 0:f1d3878b8dd9 45 * @{
vpcola 0:f1d3878b8dd9 46 */
vpcola 0:f1d3878b8dd9 47
vpcola 0:f1d3878b8dd9 48
vpcola 0:f1d3878b8dd9 49 /**
vpcola 0:f1d3878b8dd9 50 * @addtogroup SPIRIT_Csma
vpcola 0:f1d3878b8dd9 51 * @{
vpcola 0:f1d3878b8dd9 52 */
vpcola 0:f1d3878b8dd9 53
vpcola 0:f1d3878b8dd9 54
vpcola 0:f1d3878b8dd9 55 /**
vpcola 0:f1d3878b8dd9 56 * @defgroup Csma_Private_TypesDefinitions CSMA Private TypesDefinitions
vpcola 0:f1d3878b8dd9 57 * @{
vpcola 0:f1d3878b8dd9 58 */
vpcola 0:f1d3878b8dd9 59
vpcola 0:f1d3878b8dd9 60
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 * @defgroup Csma_Private_Defines CSMA Private Defines
vpcola 0:f1d3878b8dd9 68 * @{
vpcola 0:f1d3878b8dd9 69 */
vpcola 0:f1d3878b8dd9 70
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 * @defgroup Csma_Private_Macros CSMA Private Macros
vpcola 0:f1d3878b8dd9 78 * @{
vpcola 0:f1d3878b8dd9 79 */
vpcola 0:f1d3878b8dd9 80
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 * @defgroup Csma_Private_Variables CSMA Private Variables
vpcola 0:f1d3878b8dd9 88 * @{
vpcola 0:f1d3878b8dd9 89 */
vpcola 0:f1d3878b8dd9 90
vpcola 0:f1d3878b8dd9 91 /**
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 * @defgroup Csma_Private_FunctionPrototypes CSMA Private FunctionPrototypes
vpcola 0:f1d3878b8dd9 99 * @{
vpcola 0:f1d3878b8dd9 100 */
vpcola 0:f1d3878b8dd9 101
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 * @defgroup Csma_Private_Functions CSMA Private Functions
vpcola 0:f1d3878b8dd9 109 * @{
vpcola 0:f1d3878b8dd9 110 */
vpcola 0:f1d3878b8dd9 111
vpcola 0:f1d3878b8dd9 112
vpcola 0:f1d3878b8dd9 113 /**
vpcola 0:f1d3878b8dd9 114 * @brief Initializes the SPIRIT CSMA according to the specified parameters in the CsmaInit.
vpcola 0:f1d3878b8dd9 115 * @param pxCsmaInit Csma init structure.
vpcola 0:f1d3878b8dd9 116 * This parameter is a pointer to @ref CsmaInit.
vpcola 0:f1d3878b8dd9 117 * @retval None.
vpcola 0:f1d3878b8dd9 118 */
vpcola 0:f1d3878b8dd9 119 void SpiritCsmaInit(CsmaInit* pxCsmaInit)
vpcola 0:f1d3878b8dd9 120 {
vpcola 0:f1d3878b8dd9 121 uint8_t tempRegValue[5];
vpcola 0:f1d3878b8dd9 122
vpcola 0:f1d3878b8dd9 123 /* Check the parameters */
vpcola 0:f1d3878b8dd9 124 s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(pxCsmaInit->xCsmaPersistentMode));
vpcola 0:f1d3878b8dd9 125 s_assert_param(IS_CCA_PERIOD(pxCsmaInit->xMultiplierTbit));
vpcola 0:f1d3878b8dd9 126 s_assert_param(IS_CSMA_LENGTH(pxCsmaInit->xCcaLength));
vpcola 0:f1d3878b8dd9 127 s_assert_param(IS_BU_COUNTER_SEED(pxCsmaInit->nBuCounterSeed));
vpcola 0:f1d3878b8dd9 128 s_assert_param(IS_BU_PRESCALER(pxCsmaInit->cBuPrescaler));
vpcola 0:f1d3878b8dd9 129 s_assert_param(IS_CMAX_NB(pxCsmaInit->cMaxNb));
vpcola 0:f1d3878b8dd9 130
vpcola 0:f1d3878b8dd9 131 /* CSMA BU counter seed (MSB) config */
vpcola 0:f1d3878b8dd9 132 tempRegValue[0] = (uint8_t)(pxCsmaInit->nBuCounterSeed >> 8);
vpcola 0:f1d3878b8dd9 133
vpcola 0:f1d3878b8dd9 134 /* CSMA BU counter seed (LSB) config */
vpcola 0:f1d3878b8dd9 135 tempRegValue[1] = (uint8_t) pxCsmaInit->nBuCounterSeed;
vpcola 0:f1d3878b8dd9 136
vpcola 0:f1d3878b8dd9 137 /* CSMA BU prescaler config and CCA period config */
vpcola 0:f1d3878b8dd9 138 tempRegValue[2] = (pxCsmaInit->cBuPrescaler << 2) | pxCsmaInit->xMultiplierTbit;
vpcola 0:f1d3878b8dd9 139
vpcola 0:f1d3878b8dd9 140 /* CSMA CCA length config and max number of back-off */
vpcola 0:f1d3878b8dd9 141 tempRegValue[3] = (pxCsmaInit->xCcaLength | pxCsmaInit->cMaxNb);
vpcola 0:f1d3878b8dd9 142
vpcola 0:f1d3878b8dd9 143 /* Reads the PROTOCOL1_BASE register value, to write the SEED_RELOAD and CSMA_PERS_ON fields */
vpcola 0:f1d3878b8dd9 144 g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue[4]);
vpcola 0:f1d3878b8dd9 145
vpcola 0:f1d3878b8dd9 146 /* Writes the new value for persistent mode */
vpcola 0:f1d3878b8dd9 147 if(pxCsmaInit->xCsmaPersistentMode==S_ENABLE)
vpcola 0:f1d3878b8dd9 148 {
vpcola 0:f1d3878b8dd9 149 tempRegValue[4] |= PROTOCOL1_CSMA_PERS_ON_MASK;
vpcola 0:f1d3878b8dd9 150 }
vpcola 0:f1d3878b8dd9 151 else
vpcola 0:f1d3878b8dd9 152 {
vpcola 0:f1d3878b8dd9 153 tempRegValue[4] &= ~PROTOCOL1_CSMA_PERS_ON_MASK;
vpcola 0:f1d3878b8dd9 154 }
vpcola 0:f1d3878b8dd9 155
vpcola 0:f1d3878b8dd9 156 /* Writes PROTOCOL1_BASE register */
vpcola 0:f1d3878b8dd9 157 g_xStatus = SpiritSpiWriteRegisters(PROTOCOL1_BASE, 1, &tempRegValue[4]);
vpcola 0:f1d3878b8dd9 158
vpcola 0:f1d3878b8dd9 159 /* Writes CSMA_CONFIGx_BASE registers */
vpcola 0:f1d3878b8dd9 160 g_xStatus = SpiritSpiWriteRegisters(CSMA_CONFIG3_BASE, 4, tempRegValue);
vpcola 0:f1d3878b8dd9 161
vpcola 0:f1d3878b8dd9 162 }
vpcola 0:f1d3878b8dd9 163
vpcola 0:f1d3878b8dd9 164
vpcola 0:f1d3878b8dd9 165 /**
vpcola 0:f1d3878b8dd9 166 * @brief Returns the fitted structure CsmaInit starting from the registers values.
vpcola 0:f1d3878b8dd9 167 * @param pxCsmaInit Csma structure to be fitted.
vpcola 0:f1d3878b8dd9 168 * This parameter is a pointer to @ref CsmaInit.
vpcola 0:f1d3878b8dd9 169 * @retval None.
vpcola 0:f1d3878b8dd9 170 */
vpcola 0:f1d3878b8dd9 171 void SpiritCsmaGetInfo(CsmaInit* pxCsmaInit)
vpcola 0:f1d3878b8dd9 172 {
vpcola 0:f1d3878b8dd9 173 uint8_t tempRegValue[5];
vpcola 0:f1d3878b8dd9 174
vpcola 0:f1d3878b8dd9 175 /* Reads PROTOCOL1_BASE register */
vpcola 0:f1d3878b8dd9 176 g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue[4]);
vpcola 0:f1d3878b8dd9 177
vpcola 0:f1d3878b8dd9 178 /* Reads CSMA_CONFIGx_BASE registers */
vpcola 0:f1d3878b8dd9 179 g_xStatus = SpiritSpiReadRegisters(CSMA_CONFIG3_BASE, 4, tempRegValue);
vpcola 0:f1d3878b8dd9 180
vpcola 0:f1d3878b8dd9 181 /* Reads the bu counter seed */
vpcola 0:f1d3878b8dd9 182 pxCsmaInit->nBuCounterSeed = (uint16_t)tempRegValue[1] | ((uint16_t)(tempRegValue[0] << 8));
vpcola 0:f1d3878b8dd9 183
vpcola 0:f1d3878b8dd9 184 /* Reads the bu prescaler */
vpcola 0:f1d3878b8dd9 185 pxCsmaInit->cBuPrescaler = tempRegValue[2]>>2;
vpcola 0:f1d3878b8dd9 186
vpcola 0:f1d3878b8dd9 187 /* Reads the Cca period */
vpcola 0:f1d3878b8dd9 188 pxCsmaInit->xMultiplierTbit = (CcaPeriod)(tempRegValue[2] & 0x03);
vpcola 0:f1d3878b8dd9 189
vpcola 0:f1d3878b8dd9 190 /* Reads the Cca length */
vpcola 0:f1d3878b8dd9 191 pxCsmaInit->xCcaLength = (CsmaLength)(tempRegValue[3]&0xF0);
vpcola 0:f1d3878b8dd9 192
vpcola 0:f1d3878b8dd9 193 /* Reads the max number of back off */
vpcola 0:f1d3878b8dd9 194 pxCsmaInit->cMaxNb = tempRegValue[3] & 0x07;
vpcola 0:f1d3878b8dd9 195
vpcola 0:f1d3878b8dd9 196 /* Reads the persistent mode enable bit */
vpcola 0:f1d3878b8dd9 197 pxCsmaInit->xCsmaPersistentMode = (SpiritFunctionalState)((tempRegValue[4]>>1) & 0x01);
vpcola 0:f1d3878b8dd9 198
vpcola 0:f1d3878b8dd9 199 }
vpcola 0:f1d3878b8dd9 200
vpcola 0:f1d3878b8dd9 201
vpcola 0:f1d3878b8dd9 202 /**
vpcola 0:f1d3878b8dd9 203 * @brief Enables or Disables the CSMA.
vpcola 0:f1d3878b8dd9 204 * @param xNewState the state of the CSMA mode.
vpcola 0:f1d3878b8dd9 205 * This parameter can be: S_ENABLE or S_DISABLE.
vpcola 0:f1d3878b8dd9 206 * @retval None.
vpcola 0:f1d3878b8dd9 207 */
vpcola 0:f1d3878b8dd9 208 void SpiritCsma(SpiritFunctionalState xNewState)
vpcola 0:f1d3878b8dd9 209 {
vpcola 0:f1d3878b8dd9 210 uint8_t tempRegValue;
vpcola 0:f1d3878b8dd9 211
vpcola 0:f1d3878b8dd9 212 /* Check the parameters */
vpcola 0:f1d3878b8dd9 213 s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(xNewState));
vpcola 0:f1d3878b8dd9 214
vpcola 0:f1d3878b8dd9 215 /* Reads the PROTOCOL1 register value */
vpcola 0:f1d3878b8dd9 216 g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
vpcola 0:f1d3878b8dd9 217
vpcola 0:f1d3878b8dd9 218 /* Sets or resets the CSMA enable bit */
vpcola 0:f1d3878b8dd9 219 if(xNewState==S_ENABLE)
vpcola 0:f1d3878b8dd9 220 {
vpcola 0:f1d3878b8dd9 221 tempRegValue |= PROTOCOL1_CSMA_ON_MASK;
vpcola 0:f1d3878b8dd9 222 }
vpcola 0:f1d3878b8dd9 223 else
vpcola 0:f1d3878b8dd9 224 {
vpcola 0:f1d3878b8dd9 225 tempRegValue &= ~PROTOCOL1_CSMA_ON_MASK;
vpcola 0:f1d3878b8dd9 226 }
vpcola 0:f1d3878b8dd9 227
vpcola 0:f1d3878b8dd9 228 /* Writes the new value on the PROTOCOL1 register */
vpcola 0:f1d3878b8dd9 229 g_xStatus = SpiritSpiWriteRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
vpcola 0:f1d3878b8dd9 230
vpcola 0:f1d3878b8dd9 231 }
vpcola 0:f1d3878b8dd9 232
vpcola 0:f1d3878b8dd9 233 /**
vpcola 0:f1d3878b8dd9 234 * @brief Gets the CSMA mode. Says if it is enabled or disabled.
vpcola 0:f1d3878b8dd9 235 * @param None.
vpcola 0:f1d3878b8dd9 236 * @retval SpiritFunctionalState: CSMA mode.
vpcola 0:f1d3878b8dd9 237 */
vpcola 0:f1d3878b8dd9 238 SpiritFunctionalState SpiritCsmaGetCsma(void)
vpcola 0:f1d3878b8dd9 239 {
vpcola 0:f1d3878b8dd9 240 uint8_t tempRegValue;
vpcola 0:f1d3878b8dd9 241
vpcola 0:f1d3878b8dd9 242 /* Reads the PROTOCOL1 register value */
vpcola 0:f1d3878b8dd9 243 g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
vpcola 0:f1d3878b8dd9 244
vpcola 0:f1d3878b8dd9 245 /* Return if set or reset */
vpcola 0:f1d3878b8dd9 246 if(tempRegValue & PROTOCOL1_CSMA_ON_MASK)
vpcola 0:f1d3878b8dd9 247 {
vpcola 0:f1d3878b8dd9 248 return S_ENABLE;
vpcola 0:f1d3878b8dd9 249 }
vpcola 0:f1d3878b8dd9 250 else
vpcola 0:f1d3878b8dd9 251 {
vpcola 0:f1d3878b8dd9 252 return S_DISABLE;
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 Enables or Disables the persistent CSMA mode.
vpcola 0:f1d3878b8dd9 259 * @param xNewState the state of the persistent CSMA mode.
vpcola 0:f1d3878b8dd9 260 * This parameter can be: S_ENABLE or S_DISABLE.
vpcola 0:f1d3878b8dd9 261 * @retval None.
vpcola 0:f1d3878b8dd9 262 */
vpcola 0:f1d3878b8dd9 263 void SpiritCsmaPersistentMode(SpiritFunctionalState xNewState)
vpcola 0:f1d3878b8dd9 264 {
vpcola 0:f1d3878b8dd9 265 uint8_t tempRegValue;
vpcola 0:f1d3878b8dd9 266
vpcola 0:f1d3878b8dd9 267 /* Check the parameters */
vpcola 0:f1d3878b8dd9 268 s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(xNewState));
vpcola 0:f1d3878b8dd9 269
vpcola 0:f1d3878b8dd9 270 /* Reads the PROTOCOL1 register value */
vpcola 0:f1d3878b8dd9 271 g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
vpcola 0:f1d3878b8dd9 272
vpcola 0:f1d3878b8dd9 273 /* Enables/disables the CSMA persistent mode */
vpcola 0:f1d3878b8dd9 274 if(xNewState==S_ENABLE)
vpcola 0:f1d3878b8dd9 275 {
vpcola 0:f1d3878b8dd9 276 tempRegValue |= PROTOCOL1_CSMA_PERS_ON_MASK;
vpcola 0:f1d3878b8dd9 277 }
vpcola 0:f1d3878b8dd9 278 else
vpcola 0:f1d3878b8dd9 279 {
vpcola 0:f1d3878b8dd9 280 tempRegValue &= ~PROTOCOL1_CSMA_PERS_ON_MASK;
vpcola 0:f1d3878b8dd9 281 }
vpcola 0:f1d3878b8dd9 282
vpcola 0:f1d3878b8dd9 283 /* Writes the new vaue on the PROTOCOL1 register */
vpcola 0:f1d3878b8dd9 284 g_xStatus = SpiritSpiWriteRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
vpcola 0:f1d3878b8dd9 285
vpcola 0:f1d3878b8dd9 286 }
vpcola 0:f1d3878b8dd9 287
vpcola 0:f1d3878b8dd9 288
vpcola 0:f1d3878b8dd9 289 /**
vpcola 0:f1d3878b8dd9 290 * @brief Gets the persistent CSMA mode.
vpcola 0:f1d3878b8dd9 291 * @param None.
vpcola 0:f1d3878b8dd9 292 * @retval SpiritFunctionalState: CSMA persistent mode.
vpcola 0:f1d3878b8dd9 293 */
vpcola 0:f1d3878b8dd9 294 SpiritFunctionalState SpiritCsmaGetPersistentMode(void)
vpcola 0:f1d3878b8dd9 295 {
vpcola 0:f1d3878b8dd9 296 uint8_t tempRegValue;
vpcola 0:f1d3878b8dd9 297
vpcola 0:f1d3878b8dd9 298 /* Reads the PROTOCOL1 register value */
vpcola 0:f1d3878b8dd9 299 g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
vpcola 0:f1d3878b8dd9 300
vpcola 0:f1d3878b8dd9 301 /* Return if set or reset */
vpcola 0:f1d3878b8dd9 302 if(tempRegValue & PROTOCOL1_CSMA_PERS_ON_MASK)
vpcola 0:f1d3878b8dd9 303 {
vpcola 0:f1d3878b8dd9 304 return S_ENABLE;
vpcola 0:f1d3878b8dd9 305 }
vpcola 0:f1d3878b8dd9 306 else
vpcola 0:f1d3878b8dd9 307 {
vpcola 0:f1d3878b8dd9 308 return S_DISABLE;
vpcola 0:f1d3878b8dd9 309 }
vpcola 0:f1d3878b8dd9 310
vpcola 0:f1d3878b8dd9 311 }
vpcola 0:f1d3878b8dd9 312
vpcola 0:f1d3878b8dd9 313
vpcola 0:f1d3878b8dd9 314 /**
vpcola 0:f1d3878b8dd9 315 * @brief Enables or Disables the seed reload mode (if enabled it reloads the back-off generator seed using the value written in the BU_COUNTER_SEED register).
vpcola 0:f1d3878b8dd9 316 * @param xNewState the state of the seed reload mode.
vpcola 0:f1d3878b8dd9 317 * This parameter can be: S_ENABLE or S_DISABLE.
vpcola 0:f1d3878b8dd9 318 * @retval None.
vpcola 0:f1d3878b8dd9 319 */
vpcola 0:f1d3878b8dd9 320 void SpiritCsmaSeedReloadMode(SpiritFunctionalState xNewState)
vpcola 0:f1d3878b8dd9 321 {
vpcola 0:f1d3878b8dd9 322 uint8_t tempRegValue;
vpcola 0:f1d3878b8dd9 323
vpcola 0:f1d3878b8dd9 324 /* Check the parameters */
vpcola 0:f1d3878b8dd9 325 s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(xNewState));
vpcola 0:f1d3878b8dd9 326
vpcola 0:f1d3878b8dd9 327 /* Reads the PROTOCOL1 register value */
vpcola 0:f1d3878b8dd9 328 g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
vpcola 0:f1d3878b8dd9 329
vpcola 0:f1d3878b8dd9 330 /* Enables/disables the seed reload mode */
vpcola 0:f1d3878b8dd9 331 if(xNewState==S_ENABLE)
vpcola 0:f1d3878b8dd9 332 {
vpcola 0:f1d3878b8dd9 333 tempRegValue |= PROTOCOL1_SEED_RELOAD_MASK;
vpcola 0:f1d3878b8dd9 334 }
vpcola 0:f1d3878b8dd9 335 else
vpcola 0:f1d3878b8dd9 336 {
vpcola 0:f1d3878b8dd9 337 tempRegValue &= ~PROTOCOL1_SEED_RELOAD_MASK;
vpcola 0:f1d3878b8dd9 338 }
vpcola 0:f1d3878b8dd9 339
vpcola 0:f1d3878b8dd9 340 /* Writes the new value on the PROTOCOL1 register */
vpcola 0:f1d3878b8dd9 341 g_xStatus = SpiritSpiWriteRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
vpcola 0:f1d3878b8dd9 342
vpcola 0:f1d3878b8dd9 343 }
vpcola 0:f1d3878b8dd9 344
vpcola 0:f1d3878b8dd9 345
vpcola 0:f1d3878b8dd9 346 /**
vpcola 0:f1d3878b8dd9 347 * @brief Gets the seed reload mode.
vpcola 0:f1d3878b8dd9 348 * @param None.
vpcola 0:f1d3878b8dd9 349 * @retval SpiritFunctionalState: CSMA seed reload mode.
vpcola 0:f1d3878b8dd9 350 */
vpcola 0:f1d3878b8dd9 351 SpiritFunctionalState SpiritCsmaGetSeedReloadMode(void)
vpcola 0:f1d3878b8dd9 352 {
vpcola 0:f1d3878b8dd9 353 uint8_t tempRegValue;
vpcola 0:f1d3878b8dd9 354
vpcola 0:f1d3878b8dd9 355 /* Reads the PROTOCOL1 register value */
vpcola 0:f1d3878b8dd9 356 g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue);
vpcola 0:f1d3878b8dd9 357
vpcola 0:f1d3878b8dd9 358 /* Return if set or reset */
vpcola 0:f1d3878b8dd9 359 if(tempRegValue & PROTOCOL1_SEED_RELOAD_MASK)
vpcola 0:f1d3878b8dd9 360 {
vpcola 0:f1d3878b8dd9 361 return S_ENABLE;
vpcola 0:f1d3878b8dd9 362 }
vpcola 0:f1d3878b8dd9 363 else
vpcola 0:f1d3878b8dd9 364 {
vpcola 0:f1d3878b8dd9 365 return S_DISABLE;
vpcola 0:f1d3878b8dd9 366 }
vpcola 0:f1d3878b8dd9 367 }
vpcola 0:f1d3878b8dd9 368
vpcola 0:f1d3878b8dd9 369
vpcola 0:f1d3878b8dd9 370 /**
vpcola 0:f1d3878b8dd9 371 * @brief Sets the BU counter seed (BU_COUNTER_SEED register). The CSMA back off time is given by the formula: BO = rand(2^NB)*BU.
vpcola 0:f1d3878b8dd9 372 * @param nBuCounterSeed seed of the random number generator used to apply the BBE algorithm.
vpcola 0:f1d3878b8dd9 373 * This parameter is an uint16_t.
vpcola 0:f1d3878b8dd9 374 * @retval None.
vpcola 0:f1d3878b8dd9 375 */
vpcola 0:f1d3878b8dd9 376 void SpiritCsmaSetBuCounterSeed(uint16_t nBuCounterSeed)
vpcola 0:f1d3878b8dd9 377 {
vpcola 0:f1d3878b8dd9 378 uint8_t tempRegValue[2];
vpcola 0:f1d3878b8dd9 379
vpcola 0:f1d3878b8dd9 380 /* Check parameters */
vpcola 0:f1d3878b8dd9 381 s_assert_param(IS_BU_COUNTER_SEED(nBuCounterSeed));
vpcola 0:f1d3878b8dd9 382
vpcola 0:f1d3878b8dd9 383 /* Build value (MSB)*/
vpcola 0:f1d3878b8dd9 384 tempRegValue[0]=(uint8_t)(nBuCounterSeed>>8);
vpcola 0:f1d3878b8dd9 385 /* Build value (LSB) */
vpcola 0:f1d3878b8dd9 386 tempRegValue[1]=(uint8_t)nBuCounterSeed;
vpcola 0:f1d3878b8dd9 387
vpcola 0:f1d3878b8dd9 388 /* Writes the CSMA_CONFIG3 registers */
vpcola 0:f1d3878b8dd9 389 g_xStatus = SpiritSpiWriteRegisters(CSMA_CONFIG3_BASE, 2, tempRegValue);
vpcola 0:f1d3878b8dd9 390
vpcola 0:f1d3878b8dd9 391 }
vpcola 0:f1d3878b8dd9 392
vpcola 0:f1d3878b8dd9 393 /**
vpcola 0:f1d3878b8dd9 394 * @brief Returns the BU counter seed (BU_COUNTER_SEED register).
vpcola 0:f1d3878b8dd9 395 * @param None.
vpcola 0:f1d3878b8dd9 396 * @retval uint16_t Seed of the random number generator used to apply the BBE algorithm.
vpcola 0:f1d3878b8dd9 397 */
vpcola 0:f1d3878b8dd9 398 uint16_t SpiritCsmaGetBuCounterSeed(void)
vpcola 0:f1d3878b8dd9 399 {
vpcola 0:f1d3878b8dd9 400 uint8_t tempRegValue[2];
vpcola 0:f1d3878b8dd9 401
vpcola 0:f1d3878b8dd9 402 /* Reads the CSMA_CONFIGx registers value */
vpcola 0:f1d3878b8dd9 403 g_xStatus = SpiritSpiReadRegisters(CSMA_CONFIG3_BASE, 2, tempRegValue);
vpcola 0:f1d3878b8dd9 404
vpcola 0:f1d3878b8dd9 405 /* Build the counter seed and return it */
vpcola 0:f1d3878b8dd9 406 return ((uint16_t)tempRegValue[1] + (((uint16_t)tempRegValue[0])<<8));
vpcola 0:f1d3878b8dd9 407
vpcola 0:f1d3878b8dd9 408 }
vpcola 0:f1d3878b8dd9 409
vpcola 0:f1d3878b8dd9 410
vpcola 0:f1d3878b8dd9 411 /**
vpcola 0:f1d3878b8dd9 412 * @brief Sets the BU prescaler. The CSMA back off time is given by the formula: BO = rand(2^NB)*BU.
vpcola 0:f1d3878b8dd9 413 * @param cBuPrescaler used to program the back-off unit BU.
vpcola 0:f1d3878b8dd9 414 * This parameter is an uint8_t.
vpcola 0:f1d3878b8dd9 415 * @retval None.
vpcola 0:f1d3878b8dd9 416 */
vpcola 0:f1d3878b8dd9 417 void SpiritCsmaSetBuPrescaler(uint8_t cBuPrescaler)
vpcola 0:f1d3878b8dd9 418 {
vpcola 0:f1d3878b8dd9 419 uint8_t tempRegValue;
vpcola 0:f1d3878b8dd9 420
vpcola 0:f1d3878b8dd9 421 /* Check parameters */
vpcola 0:f1d3878b8dd9 422 s_assert_param(IS_BU_PRESCALER(cBuPrescaler));
vpcola 0:f1d3878b8dd9 423
vpcola 0:f1d3878b8dd9 424 /* Reads the CSMA_CONFIG1 register value */
vpcola 0:f1d3878b8dd9 425 g_xStatus = SpiritSpiReadRegisters(CSMA_CONFIG1_BASE, 1, &tempRegValue);
vpcola 0:f1d3878b8dd9 426
vpcola 0:f1d3878b8dd9 427 /* Build the new value for the BU prescaler */
vpcola 0:f1d3878b8dd9 428 tempRegValue &= 0x03;
vpcola 0:f1d3878b8dd9 429 tempRegValue |= (cBuPrescaler<<2);
vpcola 0:f1d3878b8dd9 430
vpcola 0:f1d3878b8dd9 431 /* Writes the new value on the CSMA_CONFIG1_BASE register */
vpcola 0:f1d3878b8dd9 432 g_xStatus = SpiritSpiWriteRegisters(CSMA_CONFIG1_BASE, 1, &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 Returns the BU prescaler.
vpcola 0:f1d3878b8dd9 439 * @param None.
vpcola 0:f1d3878b8dd9 440 * @retval uint8_t Value back-off unit (BU).
vpcola 0:f1d3878b8dd9 441 */
vpcola 0:f1d3878b8dd9 442 uint8_t SpiritCsmaGetBuPrescaler(void)
vpcola 0:f1d3878b8dd9 443 {
vpcola 0:f1d3878b8dd9 444 uint8_t tempRegValue;
vpcola 0:f1d3878b8dd9 445
vpcola 0:f1d3878b8dd9 446 /* Reads the CSMA_CONFIG1 register value */
vpcola 0:f1d3878b8dd9 447 g_xStatus = SpiritSpiReadRegisters(CSMA_CONFIG1_BASE, 1, &tempRegValue);
vpcola 0:f1d3878b8dd9 448
vpcola 0:f1d3878b8dd9 449 /* Build and return the BU prescaler value */
vpcola 0:f1d3878b8dd9 450 return (tempRegValue >> 2);
vpcola 0:f1d3878b8dd9 451
vpcola 0:f1d3878b8dd9 452 }
vpcola 0:f1d3878b8dd9 453
vpcola 0:f1d3878b8dd9 454
vpcola 0:f1d3878b8dd9 455 /**
vpcola 0:f1d3878b8dd9 456 * @brief Sets the CCA period.
vpcola 0:f1d3878b8dd9 457 * @param xMultiplierTbit value of CCA period to store.
vpcola 0:f1d3878b8dd9 458 * This parameter can be a value of @ref CcaPeriod.
vpcola 0:f1d3878b8dd9 459 * @retval None.
vpcola 0:f1d3878b8dd9 460 */
vpcola 0:f1d3878b8dd9 461 void SpiritCsmaSetCcaPeriod(CcaPeriod xMultiplierTbit)
vpcola 0:f1d3878b8dd9 462 {
vpcola 0:f1d3878b8dd9 463 uint8_t tempRegValue;
vpcola 0:f1d3878b8dd9 464
vpcola 0:f1d3878b8dd9 465 /* Check the parameters */
vpcola 0:f1d3878b8dd9 466 s_assert_param(IS_CCA_PERIOD(xMultiplierTbit));
vpcola 0:f1d3878b8dd9 467
vpcola 0:f1d3878b8dd9 468 /* Reads the CSMA_CONFIG1 register value */
vpcola 0:f1d3878b8dd9 469 g_xStatus = SpiritSpiReadRegisters(CSMA_CONFIG1_BASE, 1, &tempRegValue);
vpcola 0:f1d3878b8dd9 470
vpcola 0:f1d3878b8dd9 471 /* Build the new value setting the the CCA period */
vpcola 0:f1d3878b8dd9 472 tempRegValue &= 0xFC;
vpcola 0:f1d3878b8dd9 473 tempRegValue |= xMultiplierTbit;
vpcola 0:f1d3878b8dd9 474
vpcola 0:f1d3878b8dd9 475 /* Writes the new value on the CSMA_CONFIG1 register */
vpcola 0:f1d3878b8dd9 476 g_xStatus = SpiritSpiWriteRegisters(CSMA_CONFIG1_BASE, 1, &tempRegValue);
vpcola 0:f1d3878b8dd9 477
vpcola 0:f1d3878b8dd9 478 }
vpcola 0:f1d3878b8dd9 479
vpcola 0:f1d3878b8dd9 480
vpcola 0:f1d3878b8dd9 481 /**
vpcola 0:f1d3878b8dd9 482 * @brief Returns the CCA period.
vpcola 0:f1d3878b8dd9 483 * @param None.
vpcola 0:f1d3878b8dd9 484 * @retval CcaPeriod CCA period.
vpcola 0:f1d3878b8dd9 485 */
vpcola 0:f1d3878b8dd9 486 CcaPeriod SpiritCsmaGetCcaPeriod(void)
vpcola 0:f1d3878b8dd9 487 {
vpcola 0:f1d3878b8dd9 488 uint8_t tempRegValue;
vpcola 0:f1d3878b8dd9 489
vpcola 0:f1d3878b8dd9 490 /* Reads the CSMA_CONFIG1 register value */
vpcola 0:f1d3878b8dd9 491 g_xStatus = SpiritSpiReadRegisters(CSMA_CONFIG1_BASE, 1, &tempRegValue);
vpcola 0:f1d3878b8dd9 492
vpcola 0:f1d3878b8dd9 493 /* Build and return the CCA period value */
vpcola 0:f1d3878b8dd9 494 return (CcaPeriod)(tempRegValue & 0x03);
vpcola 0:f1d3878b8dd9 495
vpcola 0:f1d3878b8dd9 496 }
vpcola 0:f1d3878b8dd9 497
vpcola 0:f1d3878b8dd9 498
vpcola 0:f1d3878b8dd9 499 /**
vpcola 0:f1d3878b8dd9 500 * @brief Sets the CCA length.
vpcola 0:f1d3878b8dd9 501 * @param xCcaLength the CCA length (a value between 1 and 15 that multiplies the CCA period).
vpcola 0:f1d3878b8dd9 502 * This parameter can be any value of @ref CsmaLength.
vpcola 0:f1d3878b8dd9 503 * @retval None.
vpcola 0:f1d3878b8dd9 504 */
vpcola 0:f1d3878b8dd9 505 void SpiritCsmaSetCcaLength(CsmaLength xCcaLength)
vpcola 0:f1d3878b8dd9 506 {
vpcola 0:f1d3878b8dd9 507 uint8_t tempRegValue;
vpcola 0:f1d3878b8dd9 508
vpcola 0:f1d3878b8dd9 509 /* Check the parameters */
vpcola 0:f1d3878b8dd9 510 s_assert_param(IS_CSMA_LENGTH(xCcaLength));
vpcola 0:f1d3878b8dd9 511
vpcola 0:f1d3878b8dd9 512 /* Reads the CSMA_CONFIG0 register value */
vpcola 0:f1d3878b8dd9 513 g_xStatus = SpiritSpiReadRegisters(CSMA_CONFIG0_BASE, 1, &tempRegValue);
vpcola 0:f1d3878b8dd9 514
vpcola 0:f1d3878b8dd9 515 /* Build the value of CCA length to be set */
vpcola 0:f1d3878b8dd9 516 tempRegValue &= 0x0F;
vpcola 0:f1d3878b8dd9 517 tempRegValue |= xCcaLength;
vpcola 0:f1d3878b8dd9 518
vpcola 0:f1d3878b8dd9 519 /* Writes the new value on the CSMA_CONFIG0 register */
vpcola 0:f1d3878b8dd9 520 g_xStatus = SpiritSpiWriteRegisters(CSMA_CONFIG0_BASE, 1, &tempRegValue);
vpcola 0:f1d3878b8dd9 521
vpcola 0:f1d3878b8dd9 522 }
vpcola 0:f1d3878b8dd9 523
vpcola 0:f1d3878b8dd9 524
vpcola 0:f1d3878b8dd9 525 /**
vpcola 0:f1d3878b8dd9 526 * @brief Returns the CCA length.
vpcola 0:f1d3878b8dd9 527 * @param None.
vpcola 0:f1d3878b8dd9 528 * @retval uint8_t CCA length.
vpcola 0:f1d3878b8dd9 529 */
vpcola 0:f1d3878b8dd9 530 uint8_t SpiritCsmaGetCcaLength(void)
vpcola 0:f1d3878b8dd9 531 {
vpcola 0:f1d3878b8dd9 532 uint8_t tempRegValue;
vpcola 0:f1d3878b8dd9 533
vpcola 0:f1d3878b8dd9 534 /* Reads the CSMA_CONFIG0 register value */
vpcola 0:f1d3878b8dd9 535 g_xStatus = SpiritSpiReadRegisters(CSMA_CONFIG0_BASE, 1, &tempRegValue);
vpcola 0:f1d3878b8dd9 536
vpcola 0:f1d3878b8dd9 537 /* Build and return the CCA length */
vpcola 0:f1d3878b8dd9 538 return tempRegValue >> 4;
vpcola 0:f1d3878b8dd9 539
vpcola 0:f1d3878b8dd9 540 }
vpcola 0:f1d3878b8dd9 541
vpcola 0:f1d3878b8dd9 542
vpcola 0:f1d3878b8dd9 543 /**
vpcola 0:f1d3878b8dd9 544 * @brief Sets the max number of back-off. If reached Spirit stops the transmission.
vpcola 0:f1d3878b8dd9 545 * @param cMaxNb the max number of back-off.
vpcola 0:f1d3878b8dd9 546 * This parameter is an uint8_t.
vpcola 0:f1d3878b8dd9 547 * @retval None.
vpcola 0:f1d3878b8dd9 548 */
vpcola 0:f1d3878b8dd9 549 void SpiritCsmaSetMaxNumberBackoff(uint8_t cMaxNb)
vpcola 0:f1d3878b8dd9 550 {
vpcola 0:f1d3878b8dd9 551 uint8_t tempRegValue;
vpcola 0:f1d3878b8dd9 552
vpcola 0:f1d3878b8dd9 553 /* Check the parameters */
vpcola 0:f1d3878b8dd9 554 s_assert_param(IS_CMAX_NB(cMaxNb));
vpcola 0:f1d3878b8dd9 555
vpcola 0:f1d3878b8dd9 556 /* Reads the CSMA_CONFIG0 register value */
vpcola 0:f1d3878b8dd9 557 g_xStatus = SpiritSpiReadRegisters(CSMA_CONFIG0_BASE, 1, &tempRegValue);
vpcola 0:f1d3878b8dd9 558
vpcola 0:f1d3878b8dd9 559 /* Build the value of max back off to be set */
vpcola 0:f1d3878b8dd9 560 tempRegValue &= 0xF8;
vpcola 0:f1d3878b8dd9 561 tempRegValue |= cMaxNb;
vpcola 0:f1d3878b8dd9 562
vpcola 0:f1d3878b8dd9 563 /* Writes the new value on the CSMA_CONFIG0 register */
vpcola 0:f1d3878b8dd9 564 g_xStatus = SpiritSpiWriteRegisters(CSMA_CONFIG0_BASE, 1, &tempRegValue);
vpcola 0:f1d3878b8dd9 565 }
vpcola 0:f1d3878b8dd9 566
vpcola 0:f1d3878b8dd9 567 /**
vpcola 0:f1d3878b8dd9 568 * @brief Returns the max number of back-off.
vpcola 0:f1d3878b8dd9 569 * @param None.
vpcola 0:f1d3878b8dd9 570 * @retval uint8_t Max number of back-off.
vpcola 0:f1d3878b8dd9 571 */
vpcola 0:f1d3878b8dd9 572 uint8_t SpiritCsmaGetMaxNumberBackoff(void)
vpcola 0:f1d3878b8dd9 573 {
vpcola 0:f1d3878b8dd9 574 uint8_t tempRegValue;
vpcola 0:f1d3878b8dd9 575
vpcola 0:f1d3878b8dd9 576 /* Reads the CSMA_CONFIG0 register value */
vpcola 0:f1d3878b8dd9 577 g_xStatus = SpiritSpiReadRegisters(CSMA_CONFIG0_BASE, 1, &tempRegValue);
vpcola 0:f1d3878b8dd9 578
vpcola 0:f1d3878b8dd9 579 /* Build and return the max number of back-off */
vpcola 0:f1d3878b8dd9 580 return (tempRegValue & 0x07);
vpcola 0:f1d3878b8dd9 581
vpcola 0:f1d3878b8dd9 582 }
vpcola 0:f1d3878b8dd9 583
vpcola 0:f1d3878b8dd9 584
vpcola 0:f1d3878b8dd9 585 /**
vpcola 0:f1d3878b8dd9 586 *@}
vpcola 0:f1d3878b8dd9 587 */
vpcola 0:f1d3878b8dd9 588
vpcola 0:f1d3878b8dd9 589 /**
vpcola 0:f1d3878b8dd9 590 *@}
vpcola 0:f1d3878b8dd9 591 */
vpcola 0:f1d3878b8dd9 592
vpcola 0:f1d3878b8dd9 593
vpcola 0:f1d3878b8dd9 594 /**
vpcola 0:f1d3878b8dd9 595 *@}
vpcola 0:f1d3878b8dd9 596 */
vpcola 0:f1d3878b8dd9 597
vpcola 0:f1d3878b8dd9 598
vpcola 0:f1d3878b8dd9 599
vpcola 0:f1d3878b8dd9 600 /******************* (C) COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/