Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of stm-spirit1-rf-driver by
SPIRIT_PktStack.c
00001 /** 00002 ****************************************************************************** 00003 * @file SPIRIT_PktStack.c 00004 * @author VMA division - AMS 00005 * @version 3.2.2 00006 * @date 08-July-2015 00007 * @brief Configuration and management of SPIRIT STack packets. 00008 * @details 00009 * 00010 * @attention 00011 * 00012 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> 00013 * 00014 * Redistribution and use in source and binary forms, with or without modification, 00015 * are permitted provided that the following conditions are met: 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 2. Redistributions in binary form must reproduce the above copyright notice, 00019 * this list of conditions and the following disclaimer in the documentation 00020 * and/or other materials provided with the distribution. 00021 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00022 * may be used to endorse or promote products derived from this software 00023 * without specific prior written permission. 00024 * 00025 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00026 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00028 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00029 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00030 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00031 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00032 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00033 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00034 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00035 * 00036 ****************************************************************************** 00037 */ 00038 00039 /* Includes ------------------------------------------------------------------*/ 00040 #include "SPIRIT_PktStack.h" 00041 #include "MCU_Interface.h" 00042 00043 00044 /** 00045 * @addtogroup SPIRIT_Libraries 00046 * @{ 00047 */ 00048 00049 00050 /** 00051 * @addtogroup SPIRIT_PktStack 00052 * @{ 00053 */ 00054 00055 00056 /** 00057 * @defgroup PktStack_Private_TypesDefinitions Pkt STack Private Types Definitions 00058 * @{ 00059 */ 00060 00061 /** 00062 *@} 00063 */ 00064 00065 00066 /** 00067 * @defgroup PktStack_Private_Defines Pkt STack Private Defines 00068 * @{ 00069 */ 00070 00071 /** 00072 *@} 00073 */ 00074 00075 00076 /** 00077 * @defgroup PktStack_Private_Macros Pkt STack Private Macros 00078 * @{ 00079 */ 00080 00081 /** 00082 *@} 00083 */ 00084 00085 00086 /** 00087 * @defgroup PktStack_Private_Variables Pkt STack Private Variables 00088 * @{ 00089 */ 00090 00091 /** 00092 *@} 00093 */ 00094 00095 00096 /** 00097 * @defgroup PktStack_Private_FunctionPrototypes Pkt STack Private Function Prototypes 00098 * @{ 00099 */ 00100 00101 /** 00102 *@} 00103 */ 00104 00105 00106 /** 00107 * @defgroup PktStack_Private_Functions Pkt STack Private Functions 00108 * @{ 00109 */ 00110 00111 00112 /** 00113 * @brief Initializes the SPIRIT STack packet according to the specified 00114 * parameters in the PktStackInit. 00115 * @param pxPktStackInit STack packet init structure. 00116 * This parameter is a pointer to @ref PktStackInit. 00117 * @retval None. 00118 */ 00119 void SpiritPktStackInit(PktStackInit* pxPktStackInit) 00120 { 00121 uint8_t tempRegValue[4], i; 00122 00123 /* Check the parameters */ 00124 s_assert_param(IS_STACK_PREAMBLE_LENGTH(pxPktStackInit->xPreambleLength)); 00125 s_assert_param(IS_STACK_SYNC_LENGTH(pxPktStackInit->xSyncLength)); 00126 s_assert_param(IS_STACK_CRC_MODE(pxPktStackInit->xCrcMode)); 00127 s_assert_param(IS_STACK_LENGTH_WIDTH_BITS(pxPktStackInit->cPktLengthWidth)); 00128 s_assert_param(IS_STACK_FIX_VAR_LENGTH(pxPktStackInit->xFixVarLength)); 00129 s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(pxPktStackInit->xFec)); 00130 s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(pxPktStackInit->xDataWhitening)); 00131 s_assert_param(IS_STACK_CONTROL_LENGTH(pxPktStackInit->xControlLength)); 00132 00133 00134 /* Reads the PROTOCOL1 register */ 00135 g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue[0]); 00136 00137 /* Mask a reserved bit */ 00138 tempRegValue[0] &= ~0x20; 00139 00140 /* Always (!) set the automatic packet filtering */ 00141 tempRegValue[0] |= PROTOCOL1_AUTO_PCKT_FLT_MASK; 00142 00143 /* Writes the value on register */ 00144 g_xStatus = SpiritSpiWriteRegisters(PROTOCOL1_BASE, 1, &tempRegValue[0]); 00145 00146 /* Reads the PCKT_FLT_OPTIONS register */ 00147 g_xStatus = SpiritSpiReadRegisters(PCKT_FLT_OPTIONS_BASE, 1, &tempRegValue[0]); 00148 00149 /* Always reset the control and source filtering */ 00150 tempRegValue[0] &= ~(PCKT_FLT_OPTIONS_SOURCE_FILTERING_MASK | PCKT_FLT_OPTIONS_CONTROL_FILTERING_MASK); 00151 00152 /* Writes the value on register */ 00153 g_xStatus = SpiritSpiWriteRegisters(PCKT_FLT_OPTIONS_BASE, 1, &tempRegValue[0]); 00154 00155 00156 /* Address and control length setting: source and destination address are always present so ADDRESS_LENGTH=2 */ 00157 tempRegValue[0] = 0x10 | ((uint8_t) pxPktStackInit->xControlLength); 00158 00159 00160 /* Packet format and width length setting */ 00161 pxPktStackInit->cPktLengthWidth == 0 ? pxPktStackInit->cPktLengthWidth=1 : pxPktStackInit->cPktLengthWidth; 00162 tempRegValue[1] = ((uint8_t) PCKTCTRL3_PCKT_FRMT_STACK) | ((uint8_t)(pxPktStackInit->cPktLengthWidth-1)); 00163 00164 /* Preamble, sync and fixed or variable length setting */ 00165 tempRegValue[2] = ((uint8_t) pxPktStackInit->xPreambleLength) | ((uint8_t) pxPktStackInit->xSyncLength) | 00166 ((uint8_t) pxPktStackInit->xFixVarLength); 00167 00168 /* CRC length, whitening and FEC setting */ 00169 tempRegValue[3] = (uint8_t) pxPktStackInit->xCrcMode; 00170 00171 if(pxPktStackInit->xDataWhitening == S_ENABLE) 00172 { 00173 tempRegValue[3] |= PCKTCTRL1_WHIT_MASK; 00174 } 00175 00176 if(pxPktStackInit->xFec == S_ENABLE) 00177 { 00178 tempRegValue[3] |= PCKTCTRL1_FEC_MASK; 00179 } 00180 00181 /* Writes registers */ 00182 SpiritSpiWriteRegisters(PCKTCTRL4_BASE, 4, tempRegValue); 00183 00184 /* Sync words setting */ 00185 for(i=0;i<4;i++) 00186 { 00187 if(i<3-(pxPktStackInit->xSyncLength >>1)) 00188 { 00189 tempRegValue[i]=0; 00190 } 00191 else 00192 { 00193 tempRegValue[i] = (uint8_t)(pxPktStackInit->lSyncWords>>(8*i)); 00194 } 00195 } 00196 00197 /* Enables or disables the CRC check */ 00198 if(pxPktStackInit->xCrcMode == PKT_NO_CRC) 00199 { 00200 SpiritPktStackFilterOnCrc(S_DISABLE); 00201 } 00202 else 00203 { 00204 SpiritPktStackFilterOnCrc(S_ENABLE); 00205 } 00206 00207 /* Writes registers */ 00208 g_xStatus = SpiritSpiWriteRegisters(SYNC4_BASE, 4, tempRegValue); 00209 00210 } 00211 00212 00213 /** 00214 * @brief Returns the SPIRIT STack packet structure according to the specified parameters in the registers. 00215 * @param pxPktStackInit STack packet init structure. 00216 * This parameter is a pointer to @ref PktStackInit. 00217 * @retval None. 00218 */ 00219 void SpiritPktStackGetInfo(PktStackInit* pxPktStackInit) 00220 { 00221 uint8_t tempRegValue[10]; 00222 00223 /* Reads registers */ 00224 g_xStatus = SpiritSpiReadRegisters(PCKTCTRL4_BASE, 10, tempRegValue); 00225 00226 /* Length width */ 00227 pxPktStackInit->cPktLengthWidth=(tempRegValue[1] & 0x0F)+1; 00228 00229 /* Control length */ 00230 pxPktStackInit->xControlLength=(StackControlLength)(tempRegValue[0] & 0x07); 00231 00232 /* CRC mode */ 00233 pxPktStackInit->xCrcMode=(StackCrcMode)(tempRegValue[3] & 0xE0); 00234 00235 /* Whitening */ 00236 pxPktStackInit->xDataWhitening=(SpiritFunctionalState)((tempRegValue[3] >> 4) & 0x01); 00237 00238 /* FEC */ 00239 pxPktStackInit->xFec=(SpiritFunctionalState)(tempRegValue[3] & 0x01); 00240 00241 /* FIX or VAR bit */ 00242 pxPktStackInit->xFixVarLength=(StackFixVarLength)(tempRegValue[2] & 0x01); 00243 00244 /* Preamble length */ 00245 pxPktStackInit->xPreambleLength=(StackPreambleLength)(tempRegValue[2] & 0xF8); 00246 00247 /* Sync length */ 00248 pxPktStackInit->xSyncLength=(StackSyncLength)(tempRegValue[2] & 0x06); 00249 00250 /* sync Words */ 00251 pxPktStackInit->lSyncWords=0; 00252 for(uint8_t i=0 ; i<4 ; i++) 00253 { 00254 if(i>2-(pxPktStackInit->xSyncLength >>1)) 00255 { 00256 pxPktStackInit->lSyncWords |= tempRegValue[i+6]<<(8*i); 00257 } 00258 } 00259 00260 } 00261 00262 00263 /** 00264 * @brief Initializes the SPIRIT STack packet addresses according to the specified 00265 * parameters in the PktStackAddresses struct. 00266 * @param pxPktStackAddresses STack packet addresses init structure. 00267 * This parameter is a pointer to @ref PktStackAddressesInit . 00268 * @retval None. 00269 */ 00270 void SpiritPktStackAddressesInit(PktStackAddressesInit* pxPktStackAddresses) 00271 { 00272 uint8_t tempRegValue[3]; 00273 00274 /* Check the parameters */ 00275 s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(pxPktStackAddresses->xFilterOnMyAddress)); 00276 s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(pxPktStackAddresses->xFilterOnMulticastAddress)); 00277 s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(pxPktStackAddresses->xFilterOnBroadcastAddress)); 00278 00279 /* Reads the filtering options ragister */ 00280 g_xStatus = SpiritSpiReadRegisters(PCKT_FLT_OPTIONS_BASE, 1, &tempRegValue[0]); 00281 00282 /* Enables or disables filtering on my address */ 00283 if(pxPktStackAddresses->xFilterOnMyAddress == S_ENABLE) 00284 { 00285 tempRegValue[0] |= PCKT_FLT_OPTIONS_DEST_VS_TX_ADDR_MASK; 00286 } 00287 else 00288 { 00289 tempRegValue[0] &= ~PCKT_FLT_OPTIONS_DEST_VS_TX_ADDR_MASK; 00290 } 00291 00292 /* Enables or disables filtering on multicast address */ 00293 if(pxPktStackAddresses->xFilterOnMulticastAddress == S_ENABLE) 00294 { 00295 tempRegValue[0] |= PCKT_FLT_OPTIONS_DEST_VS_MULTICAST_ADDR_MASK; 00296 } 00297 else 00298 { 00299 tempRegValue[0] &= ~PCKT_FLT_OPTIONS_DEST_VS_MULTICAST_ADDR_MASK; 00300 } 00301 00302 /* Enables or disables filtering on broadcast address */ 00303 if(pxPktStackAddresses->xFilterOnBroadcastAddress == S_ENABLE) 00304 { 00305 tempRegValue[0] |= PCKT_FLT_OPTIONS_DEST_VS_BROADCAST_ADDR_MASK; 00306 } 00307 else 00308 { 00309 tempRegValue[0] &= ~PCKT_FLT_OPTIONS_DEST_VS_BROADCAST_ADDR_MASK; 00310 } 00311 00312 /* Writes value on the register */ 00313 g_xStatus = SpiritSpiWriteRegisters(PCKT_FLT_OPTIONS_BASE, 1, &tempRegValue[0]); 00314 00315 /* Fills array with the addresses passed in the structure */ 00316 tempRegValue[0] = pxPktStackAddresses->cBroadcastAddress; 00317 tempRegValue[1] = pxPktStackAddresses->cMulticastAddress; 00318 tempRegValue[2] = pxPktStackAddresses->cMyAddress; 00319 00320 /* Writes them on the addresses registers */ 00321 g_xStatus = SpiritSpiWriteRegisters(PCKT_FLT_GOALS_BROADCAST_BASE, 3, tempRegValue); 00322 00323 } 00324 00325 00326 /** 00327 * @brief Returns the SPIRIT STack packet addresses structure according to the specified 00328 * parameters in the registers. 00329 * @param pxPktStackAddresses STack packet addresses init structure. 00330 * This parameter is a pointer to @ref PktStackAddresses. 00331 * @retval None. 00332 */ 00333 void SpiritPktStackGetAddressesInfo(PktStackAddressesInit* pxPktStackAddresses) 00334 { 00335 uint8_t tempRegValue[3]; 00336 00337 /* Reads values on the PCKT_FLT_GOALS registers */ 00338 g_xStatus = SpiritSpiReadRegisters(PCKT_FLT_GOALS_BROADCAST_BASE, 3, tempRegValue); 00339 00340 /* Fit the structure with the read addresses */ 00341 pxPktStackAddresses->cBroadcastAddress = tempRegValue[0]; 00342 pxPktStackAddresses->cMulticastAddress = tempRegValue[1]; 00343 pxPktStackAddresses->cMyAddress = tempRegValue[2]; 00344 00345 g_xStatus = SpiritSpiReadRegisters(PCKT_FLT_OPTIONS_BASE, 1, &tempRegValue[0]); 00346 00347 /* Fit the structure with the read filtering bits */ 00348 pxPktStackAddresses->xFilterOnBroadcastAddress = (SpiritFunctionalState)((tempRegValue[0] >> 1) & 0x01); 00349 pxPktStackAddresses->xFilterOnMulticastAddress = (SpiritFunctionalState)((tempRegValue[0] >> 2) & 0x01); 00350 pxPktStackAddresses->xFilterOnMyAddress = (SpiritFunctionalState)((tempRegValue[0] >> 3) & 0x01); 00351 00352 } 00353 00354 00355 /** 00356 * @brief Initializes the SPIRIT STack packet LLP options according to the specified 00357 * parameters in the PktStackLlpInit struct. 00358 * @param pxPktStackLlpInit STack packet LLP init structure. 00359 * This parameter is a pointer to @ref PktStackLlpInit. 00360 * @retval None. 00361 */ 00362 void SpiritPktStackLlpInit(PktStackLlpInit* pxPktStackLlpInit) 00363 { 00364 uint8_t tempRegValue[2]; 00365 00366 /* Check the parameters */ 00367 s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(pxPktStackLlpInit->xPiggybacking)); 00368 s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(pxPktStackLlpInit->xAutoAck)); 00369 s_assert_param(IS_STACK_NMAX_RETX(pxPktStackLlpInit->xNMaxRetx)); 00370 /* check if piggybacking is enabled and autoack is disabled */ 00371 s_assert_param(!(pxPktStackLlpInit->xPiggybacking==S_ENABLE && pxPktStackLlpInit->xAutoAck==S_DISABLE)); 00372 00373 /* Piggybacking mechanism setting on the PROTOCOL1 register */ 00374 g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 2, tempRegValue); 00375 if(pxPktStackLlpInit->xPiggybacking == S_ENABLE) 00376 { 00377 tempRegValue[0] |= PROTOCOL1_PIGGYBACKING_MASK; 00378 } 00379 else 00380 { 00381 tempRegValue[0] &= ~PROTOCOL1_PIGGYBACKING_MASK; 00382 } 00383 00384 /* RX and TX autoack mechanisms setting on the PROTOCOL0 register */ 00385 if(pxPktStackLlpInit->xAutoAck == S_ENABLE) 00386 { 00387 tempRegValue[1] |= PROTOCOL0_AUTO_ACK_MASK; 00388 } 00389 else 00390 { 00391 tempRegValue[1] &= ~PROTOCOL0_AUTO_ACK_MASK; 00392 } 00393 00394 /* Max number of retransmission setting */ 00395 tempRegValue[1] &= ~PROTOCOL0_NMAX_RETX_MASK; 00396 tempRegValue[1] |= pxPktStackLlpInit->xNMaxRetx; 00397 00398 /* Writes registers */ 00399 g_xStatus = SpiritSpiWriteRegisters(PROTOCOL1_BASE, 2, tempRegValue); 00400 00401 } 00402 00403 00404 /** 00405 * @brief Returns the SPIRIT STack packet LLP options according to the specified 00406 * values in the registers. 00407 * @param pxPktStackLlpInit STack packet LLP structure. 00408 * This parameter is a pointer to @ref PktStackLlpInit. 00409 * @retval None. 00410 */ 00411 void SpiritPktStackLlpGetInfo(PktStackLlpInit* pxPktStackLlpInit) 00412 { 00413 uint8_t tempRegValue[2]; 00414 00415 /* Piggybacking mechanism setting on the PROTOCOL1 register */ 00416 g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 2, tempRegValue); 00417 00418 /* Fit the structure with the read values */ 00419 pxPktStackLlpInit->xPiggybacking = (SpiritFunctionalState)((tempRegValue[0] >> 6) & 0x01); 00420 pxPktStackLlpInit->xAutoAck = (SpiritFunctionalState)((tempRegValue[1] >> 2) & 0x01); 00421 pxPktStackLlpInit->xNMaxRetx = (StackNMaxReTx)(tempRegValue[1] & PROTOCOL0_NMAX_RETX_MASK); 00422 00423 } 00424 00425 00426 /** 00427 * @brief Configures the STack packet format for SPIRIT. 00428 * @param None. 00429 * @retval None. 00430 */ 00431 void SpiritPktStackSetFormat(void) 00432 { 00433 uint8_t tempRegValue; 00434 00435 /* Reads the PCKTCTRL3 register value */ 00436 g_xStatus = SpiritSpiReadRegisters(PCKTCTRL3_BASE, 1, &tempRegValue); 00437 00438 /* Build value to be written. Also set to 0 the direct RX mode bits */ 00439 tempRegValue &= 0x0F; 00440 tempRegValue |= ((uint8_t)PCKTCTRL3_PCKT_FRMT_STACK); 00441 00442 /* Writes the value on the PCKTCTRL3 register. */ 00443 g_xStatus = SpiritSpiWriteRegisters(PCKTCTRL3_BASE, 1, &tempRegValue); 00444 00445 /* Reads the PCKTCTRL1 register value */ 00446 g_xStatus = SpiritSpiReadRegisters(PCKTCTRL1_BASE, 1, &tempRegValue); 00447 00448 /* Build the new value. Set to 0 the direct TX mode bits */ 00449 tempRegValue &= 0xF3; 00450 00451 /* Writes the PCKTCTRL1 value on register */ 00452 g_xStatus = SpiritSpiWriteRegisters(PCKTCTRL1_BASE, 1, &tempRegValue); 00453 00454 /* Reads the PROTOCOL1 register */ 00455 g_xStatus = SpiritSpiReadRegisters(PROTOCOL1_BASE, 1, &tempRegValue); 00456 00457 /* Mask a reserved bit */ 00458 tempRegValue &= ~0x20; 00459 00460 /* Writes the value on the PROTOCOL1 register */ 00461 g_xStatus = SpiritSpiWriteRegisters(PROTOCOL1_BASE, 1, &tempRegValue); 00462 00463 } 00464 00465 00466 /** 00467 * @brief Sets the address length for SPIRIT STack packets (always 2). 00468 * @param None. 00469 * @retval None. 00470 */ 00471 void SpiritPktStackSetAddressLength(void) 00472 { 00473 uint8_t tempRegValue; 00474 00475 /* Reads the PCKTCTRL4 register value */ 00476 g_xStatus = SpiritSpiReadRegisters(PCKTCTRL4_BASE, 1, &tempRegValue); 00477 00478 /* Build the new value */ 00479 tempRegValue &= ~PCKTCTRL4_ADDRESS_LEN_MASK; 00480 tempRegValue |= ((uint8_t)0x10); 00481 00482 /* Writes the value on the PCKTCTRL4 register */ 00483 g_xStatus = SpiritSpiWriteRegisters(PCKTCTRL4_BASE, 1, &tempRegValue); 00484 00485 } 00486 00487 00488 /** 00489 * @brief Sets the payload length for SPIRIT STack packets. Since the packet length 00490 * depends from the address (always 2 for this packet format) 00491 * and the control field size, this function reads the control length register 00492 * content in order to determine the correct packet length to be written. 00493 * @param nPayloadLength payload length in bytes. 00494 * This parameter can be any value of uint16_t. 00495 * @retval None. 00496 */ 00497 void SpiritPktStackSetPayloadLength(uint16_t nPayloadLength) 00498 { 00499 uint8_t tempRegValue[2]; 00500 00501 /* Computes the oversize (address + control) size */ 00502 uint16_t overSize = 2 + (uint16_t) SpiritPktStackGetControlLength(); 00503 00504 /* Computes PCKTLEN0 value from lPayloadLength */ 00505 tempRegValue[1]=STACK_BUILD_PCKTLEN0(nPayloadLength+overSize); 00506 /* Computes PCKTLEN1 value from lPayloadLength */ 00507 tempRegValue[0]=STACK_BUILD_PCKTLEN1(nPayloadLength+overSize); 00508 00509 /* Writes the value on the PCKTLENx registers */ 00510 g_xStatus = SpiritSpiWriteRegisters(PCKTLEN1_BASE, 2, tempRegValue); 00511 00512 } 00513 00514 00515 /** 00516 * @brief Returns the payload length for SPIRIT STack packets. Since the 00517 * packet length depends from the address and the control 00518 * field size, this function reads the correspondent 00519 * registers in order to determine the correct payload length 00520 * to be returned. 00521 * @param None. 00522 * @retval uint16_t Payload length. 00523 */ 00524 uint16_t SpiritPktStackGetPayloadLength(void) 00525 { 00526 uint8_t tempRegValue[2]; 00527 /* Computes the oversize (address + control) size */ 00528 uint16_t overSize = 2 + (uint16_t) SpiritPktStackGetControlLength(); 00529 00530 /* Reads the PCKTLEN1 registers value */ 00531 g_xStatus = SpiritSpiReadRegisters(PCKTLEN1_BASE, 2, tempRegValue); 00532 00533 /* Rebuild and return the payload length value */ 00534 return ((((uint16_t) tempRegValue[1])<<8) + (uint16_t) tempRegValue[0] - overSize); 00535 00536 } 00537 00538 00539 /** 00540 * @brief Computes and sets the variable payload length for SPIRIT STack packets. 00541 * @param nMaxPayloadLength payload length in bytes. 00542 * This parameter is an uint16_t. 00543 * @param xControlLength control length in bytes. 00544 * This parameter can be any value of @ref StackControlLength. 00545 * @retval None. 00546 */ 00547 void SpiritPktStackSetVarLengthWidth(uint16_t nMaxPayloadLength, StackControlLength xControlLength) 00548 { 00549 uint8_t tempRegValue, 00550 i; 00551 uint32_t packetLength; 00552 00553 00554 /* packet length = payload length + address length (2) + control length */ 00555 packetLength=nMaxPayloadLength+2+xControlLength; 00556 00557 /* Computes the number of bits */ 00558 for(i=0;i<16;i++) 00559 { 00560 if(packetLength == 0) 00561 { 00562 break; 00563 } 00564 packetLength >>= 1; 00565 } 00566 i==0 ? i=1 : i; 00567 00568 /* Reads the PCKTCTRL3 register value */ 00569 g_xStatus = SpiritSpiReadRegisters(PCKTCTRL3_BASE, 1, &tempRegValue); 00570 00571 /* Build the register value */ 00572 tempRegValue &= ~PCKTCTRL3_LEN_WID_MASK; 00573 tempRegValue |= ((uint8_t)(i-1)); 00574 00575 /* Writes the PCKTCTRL3 register value */ 00576 g_xStatus = SpiritSpiWriteRegisters(PCKTCTRL3_BASE, 1, &tempRegValue); 00577 00578 } 00579 00580 00581 /** 00582 * @brief Rx packet source mask. Used to mask the address of the accepted packets. If 0 -> no filtering. 00583 * @param cMask Rx source mask. 00584 * This parameter is an uint8_t. 00585 * @retval None. 00586 */ 00587 void SpiritPktStackSetRxSourceMask(uint8_t cMask) 00588 { 00589 /* Writes value on the register PCKT_FLT_GOALS_SOURCE_MASK */ 00590 g_xStatus = SpiritSpiWriteRegisters(PCKT_FLT_GOALS_SOURCE_MASK_BASE, 1, &cMask); 00591 00592 } 00593 00594 00595 /** 00596 * @brief Returns the Rx packet source mask. Used to mask the address of the accepted packets. If 0 -> no filtering. 00597 * @param None. 00598 * @retval uint8_t Rx source mask. 00599 */ 00600 uint8_t SpiritPktStackGetRxSourceMask(void) 00601 { 00602 uint8_t tempRegValue; 00603 00604 /* Writes value on the PCKT_FLT_GOALS_SOURCE_MASK register */ 00605 g_xStatus = SpiritSpiReadRegisters(PCKT_FLT_GOALS_SOURCE_MASK_BASE, 1, &tempRegValue); 00606 00607 /* Return the read value */ 00608 return tempRegValue; 00609 00610 } 00611 00612 /** 00613 * @brief Returns the packet length field of the received packet. 00614 * @param None. 00615 * @retval uint16_t Packet length. 00616 */ 00617 uint16_t SpiritPktStackGetReceivedPktLength(void) 00618 { 00619 uint8_t tempRegValue[2]; 00620 uint16_t tempLength; 00621 00622 /* Reads the RX_PCKT_LENx registers value */ 00623 g_xStatus = SpiritSpiReadRegisters(RX_PCKT_LEN1_BASE, 2, tempRegValue); 00624 00625 /* Rebuild and return the the length field */ 00626 tempLength = ((((uint16_t) tempRegValue[0]) << 8) + (uint16_t) tempRegValue[1]); 00627 00628 /* Computes the oversize (address + control) size */ 00629 tempLength -= 2 + (uint16_t) SpiritPktStackGetControlLength(); 00630 00631 return tempLength; 00632 00633 } 00634 00635 00636 /** 00637 * @brief If enabled RX packet is accepted only if the masked source address field matches the 00638 * masked source address field reference (SOURCE_MASK & SOURCE_FIELD_REF == SOURCE_MASK & RX_SOURCE_FIELD). 00639 * @param xNewState new state for Source address filtering enable bit. 00640 * This parameter can be S_ENABLE or S_DISABLE. 00641 * @retval None. 00642 * @note This filtering control is enabled by default but the source address mask is by default set to 0. 00643 * As a matter of fact the user has to enable the source filtering bit after the packet initialization 00644 * because the PktInit routine disables it. 00645 */ 00646 void SpiritPktStackFilterOnSourceAddress(SpiritFunctionalState xNewState) 00647 { 00648 uint8_t tempRegValue; 00649 00650 /* Check the parameters */ 00651 s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(xNewState)); 00652 00653 00654 /* Modify the register value: set or reset the source bit filtering */ 00655 g_xStatus = SpiritSpiReadRegisters(PCKT_FLT_OPTIONS_BASE, 1, &tempRegValue); 00656 00657 /* Set or reset the SOURCE ADDRESS filtering enabling bit */ 00658 if(xNewState == S_ENABLE) 00659 { 00660 tempRegValue |= PCKT_FLT_OPTIONS_SOURCE_FILTERING_MASK; 00661 } 00662 else 00663 { 00664 tempRegValue &= ~PCKT_FLT_OPTIONS_SOURCE_FILTERING_MASK; 00665 } 00666 00667 /* Writes the new value on the PCKT_FLT_OPTIONS register */ 00668 g_xStatus = SpiritSpiWriteRegisters(PCKT_FLT_OPTIONS_BASE, 1, &tempRegValue); 00669 00670 } 00671 00672 /** 00673 *@} 00674 */ 00675 00676 /** 00677 *@} 00678 */ 00679 00680 00681 /** 00682 *@} 00683 */ 00684 00685 00686 00687 /******************* (C) COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/
Generated on Wed Jul 13 2022 00:11:57 by
