JunMo Hong / EV-COG-AD3029LZ

Fork of stm-spirit1-rf-driver by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers radio_spi.cpp Source File

radio_spi.cpp

00001 /**
00002 ******************************************************************************
00003 * @file    radio_spi.c
00004 * @author  System Lab - NOIDA
00005 * @version V1.0.0
00006 * @date    15-May-2014
00007 * @brief   This file provides code for the configuration of the SPI instances.
00008 ******************************************************************************
00009 * @attention
00010 *
00011 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
00012 *
00013 * Redistribution and use in source and binary forms, with or without modification,
00014 * are permitted provided that the following conditions are met:
00015 *   1. Redistributions of source code must retain the above copyright notice,
00016 *      this list of conditions and the following disclaimer.
00017 *   2. Redistributions in binary form must reproduce the above copyright notice,
00018 *      this list of conditions and the following disclaimer in the documentation
00019 *      and/or other materials provided with the distribution.
00020 *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021 *      may be used to endorse or promote products derived from this software
00022 *     without specific prior written permission.
00023 *
00024 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 *
00035 ******************************************************************************
00036 */
00037 
00038 
00039 /* Includes ------------------------------------------------------------------*/
00040 #include "radio_spi.h"
00041 #include "SimpleSpirit1.h"
00042 
00043 
00044 /**
00045  * @addtogroup BSP
00046  * @{
00047  */
00048 
00049 
00050 /**
00051  * @addtogroup X-NUCLEO-IDS02Ax
00052  * @{
00053  */
00054 
00055 
00056 /**
00057  * @defgroup RADIO_SPI_Private_TypesDefinitions       RADIO_SPI Private Types Definitions
00058  * @{
00059  */
00060 
00061 /**
00062  * @}
00063  */
00064 
00065 
00066 /**
00067  * @defgroup RADIO_SPI_Private_Defines                RADIO_SPI Private Defines
00068  * @{
00069  */
00070 
00071 /**
00072  * @}
00073  */
00074 
00075 
00076 /**
00077  * @defgroup RADIO_SPI_Private_Macros                 RADIO_SPI Private Macros
00078  * @{
00079  */
00080 
00081 /**
00082  * @}
00083  */
00084 
00085 
00086 /**
00087  * @defgroup RADIO_SPI_Private_Variables              RADIO_SPI Private Variables
00088  * @{
00089  */
00090 
00091 /**
00092  * @}
00093  */
00094 
00095 
00096 /**
00097  * @defgroup RADIO_SPI_Private_FunctionPrototypes     RADIO_SPI Private Function Prototypes
00098  * @{
00099  */
00100 
00101 /**
00102  * @}
00103  */
00104 
00105 /**
00106  * @defgroup RADIO_SPI_Private_Functions              RADIO_SPI Private Functions
00107  * @{
00108  */
00109 
00110 /**
00111 * @}
00112 */
00113 
00114 /**
00115 * @brief  Write single or multiple RF Transceivers register
00116 * @param  cRegAddress: base register's address to be write
00117 * @param  cNbBytes: number of registers and bytes to be write
00118 * @param  pcBuffer: pointer to the buffer of values have to be written into registers
00119 * @retval StatusBytes
00120 */
00121 StatusBytes SdkEvalSpiWriteRegisters(uint8_t cRegAddress, uint8_t cNbBytes, uint8_t* pcBuffer)
00122 {
00123     return SimpleSpirit1::Instance().SdkEvalSpiWriteRegisters(cRegAddress, cNbBytes, pcBuffer);
00124 }
00125 
00126 StatusBytes SimpleSpirit1::SdkEvalSpiWriteRegisters(uint8_t cRegAddress, uint8_t cNbBytes, uint8_t* pcBuffer)
00127 {
00128   uint8_t aHeader[2] = {0};
00129   uint16_t tmpstatus = 0x0000;
00130   StatusBytes *pStatus=(StatusBytes *)&tmpstatus;
00131 
00132   /* Built the aHeader bytes */
00133   aHeader[0] = WRITE_HEADER;
00134   aHeader[1] = cRegAddress;
00135 
00136   /* Puts the SPI chip select low to start the transaction */
00137   chip_sync_select();
00138 
00139   /* Write the aHeader bytes and read the SPIRIT1 status bytes */
00140   tmpstatus = _spi.write(aHeader[0]);
00141   tmpstatus = tmpstatus << 8;
00142 
00143   /* Write the aHeader bytes and read the SPIRIT1 status bytes */
00144   tmpstatus |= _spi.write(aHeader[1]);
00145 
00146   /* Writes the registers according to the number of bytes */
00147   for (int index = 0; index < cNbBytes; index++)
00148   {
00149     _spi.write(pcBuffer[index]);
00150   }
00151 
00152   /* Puts the SPI chip select high to end the transaction */
00153   chip_sync_unselect();
00154 
00155   return *pStatus;
00156 }
00157 
00158 
00159 /**
00160 * @brief  Read single or multiple SPIRIT1 register
00161 * @param  cRegAddress: base register's address to be read
00162 * @param  cNbBytes: number of registers and bytes to be read
00163 * @param  pcBuffer: pointer to the buffer of registers' values read
00164 * @retval StatusBytes
00165 */
00166 StatusBytes SdkEvalSpiReadRegisters(uint8_t cRegAddress, uint8_t cNbBytes, uint8_t* pcBuffer)
00167 {
00168     return SimpleSpirit1::Instance().SdkEvalSpiReadRegisters(cRegAddress, cNbBytes, pcBuffer);
00169 }
00170 
00171 StatusBytes SimpleSpirit1::SdkEvalSpiReadRegisters(uint8_t cRegAddress, uint8_t cNbBytes, uint8_t* pcBuffer)
00172 {
00173   uint16_t tmpstatus = 0x00;
00174   StatusBytes *pStatus = (StatusBytes *)&tmpstatus;
00175 
00176   uint8_t aHeader[2] = {0};
00177 
00178   /* Built the aHeader bytes */
00179   aHeader[0] = READ_HEADER;
00180   aHeader[1] = cRegAddress;
00181 
00182   /* Put the SPI chip select low to start the transaction */
00183   chip_sync_select();
00184 
00185   /* Write the aHeader bytes and read the SPIRIT1 status bytes */
00186   tmpstatus = _spi.write(aHeader[0]);
00187   tmpstatus = tmpstatus << 8;
00188 
00189   /* Write the aHeader bytes and read the SPIRIT1 status bytes */
00190   tmpstatus |= _spi.write(aHeader[1]);
00191 
00192   for (int index = 0; index < cNbBytes; index++)
00193   {
00194       pcBuffer[index] = _spi.write(0xFF);
00195   }
00196 
00197   /* Put the SPI chip select high to end the transaction */
00198   chip_sync_unselect();
00199 
00200   return *pStatus;
00201 }
00202 
00203 
00204 /**
00205 * @brief  Send a command
00206 * @param  cCommandCode: command code to be sent
00207 * @retval StatusBytes
00208 */
00209 StatusBytes SdkEvalSpiCommandStrobes(uint8_t cCommandCode)
00210 {
00211     return SimpleSpirit1::Instance().SdkEvalSpiCommandStrobes(cCommandCode);
00212 }
00213 
00214 StatusBytes SimpleSpirit1::SdkEvalSpiCommandStrobes(uint8_t cCommandCode)
00215 {
00216   uint8_t aHeader[2] = {0};
00217   uint16_t tmpstatus = 0x0000;
00218 
00219   StatusBytes *pStatus = (StatusBytes *)&tmpstatus;
00220 
00221   /* Built the aHeader bytes */
00222   aHeader[0] = COMMAND_HEADER;
00223   aHeader[1] = cCommandCode;
00224 
00225   /* Puts the SPI chip select low to start the transaction */
00226   chip_sync_select();
00227 
00228   /* Write the aHeader bytes and read the SPIRIT1 status bytes */
00229   tmpstatus = _spi.write(aHeader[0]);
00230   tmpstatus = tmpstatus<<8;
00231 
00232   /* Write the aHeader bytes and read the SPIRIT1 status bytes */
00233   tmpstatus |= _spi.write(aHeader[1]);
00234 
00235   /* Puts the SPI chip select high to end the transaction */
00236   chip_sync_unselect();
00237 
00238   return *pStatus;
00239 }
00240 
00241 
00242 /**
00243 * @brief  Write data into TX FIFO
00244 * @param  cNbBytes: number of bytes to be written into TX FIFO
00245 * @param  pcBuffer: pointer to data to write
00246 * @retval StatusBytes
00247 */
00248 StatusBytes SdkEvalSpiWriteFifo(uint8_t cNbBytes, uint8_t* pcBuffer)
00249 {
00250     return SimpleSpirit1::Instance().SdkEvalSpiWriteFifo(cNbBytes, pcBuffer);
00251 }
00252 
00253 StatusBytes SimpleSpirit1::SdkEvalSpiWriteFifo(uint8_t cNbBytes, uint8_t* pcBuffer)
00254 {
00255   uint16_t tmpstatus = 0x0000;
00256   StatusBytes *pStatus = (StatusBytes *)&tmpstatus;
00257 
00258   uint8_t aHeader[2] = {0};
00259 
00260   /* Built the aHeader bytes */
00261   aHeader[0] = WRITE_HEADER;
00262   aHeader[1] = LINEAR_FIFO_ADDRESS;
00263 
00264   /* Put the SPI chip select low to start the transaction */
00265   chip_sync_select();
00266 
00267   /* Write the aHeader bytes and read the SPIRIT1 status bytes */
00268   tmpstatus = _spi.write(aHeader[0]);
00269   tmpstatus = tmpstatus<<8;
00270 
00271   /* Write the aHeader bytes and read the SPIRIT1 status bytes */
00272   tmpstatus |= _spi.write(aHeader[1]);
00273 
00274   /* Writes the registers according to the number of bytes */
00275   for (int index = 0; index < cNbBytes; index++)
00276   {
00277       _spi.write(pcBuffer[index]);
00278   }
00279 
00280   /* Put the SPI chip select high to end the transaction */
00281   chip_sync_unselect();
00282 
00283   return *pStatus;
00284 }
00285 
00286 /**
00287 * @brief  Read data from RX FIFO
00288 * @param  cNbBytes: number of bytes to read from RX FIFO
00289 * @param  pcBuffer: pointer to data read from RX FIFO
00290 * @retval StatusBytes
00291 */
00292 StatusBytes SdkEvalSpiReadFifo(uint8_t cNbBytes, uint8_t* pcBuffer)
00293 {
00294     return SimpleSpirit1::Instance().SdkEvalSpiReadFifo(cNbBytes, pcBuffer);
00295 }
00296 
00297 StatusBytes SimpleSpirit1::SdkEvalSpiReadFifo(uint8_t cNbBytes, uint8_t* pcBuffer)
00298 {
00299   uint16_t tmpstatus = 0x0000;
00300   StatusBytes *pStatus = (StatusBytes *)&tmpstatus;
00301 
00302   uint8_t aHeader[2];
00303 
00304   /* Built the aHeader bytes */
00305   aHeader[0]=READ_HEADER;
00306   aHeader[1]=LINEAR_FIFO_ADDRESS;
00307 
00308   /* Put the SPI chip select low to start the transaction */
00309   chip_sync_select();
00310 
00311   /* Write the aHeader bytes and read the SPIRIT1 status bytes */
00312   tmpstatus = _spi.write(aHeader[0]);
00313   tmpstatus = tmpstatus<<8;
00314 
00315   /* Write the aHeader bytes and read the SPIRIT1 status bytes */
00316   tmpstatus |= _spi.write(aHeader[1]);
00317 
00318   for (int index = 0; index < cNbBytes; index++)
00319   {
00320       pcBuffer[index] = _spi.write(0xFF);
00321   }
00322 
00323   /* Put the SPI chip select high to end the transaction */
00324   chip_sync_unselect();
00325 
00326   return *pStatus;
00327 }
00328 
00329 
00330 /**
00331 * @}
00332 */
00333 
00334 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/