STM32F479xx HAL User Manual
stm32f4xx_ll_spi.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_ll_spi.c
00004   * @author  MCD Application Team
00005   * @brief   SPI LL module driver.
00006   ******************************************************************************
00007   * @attention
00008   *
00009   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
00010   * All rights reserved.</center></h2>
00011   *
00012   * This software component is licensed by ST under BSD 3-Clause license,
00013   * the "License"; You may not use this file except in compliance with the
00014   * License. You may obtain a copy of the License at:
00015   *                        opensource.org/licenses/BSD-3-Clause
00016   *
00017   ******************************************************************************
00018   */
00019 #if defined(USE_FULL_LL_DRIVER)
00020 
00021 /* Includes ------------------------------------------------------------------*/
00022 #include "stm32f4xx_ll_spi.h"
00023 #include "stm32f4xx_ll_bus.h"
00024 #include "stm32f4xx_ll_rcc.h"
00025 
00026 #ifdef  USE_FULL_ASSERT
00027 #include "stm32_assert.h"
00028 #else
00029 #define assert_param(expr) ((void)0U)
00030 #endif /* USE_FULL_ASSERT */
00031 
00032 /** @addtogroup STM32F4xx_LL_Driver
00033   * @{
00034   */
00035 
00036 #if defined (SPI1) || defined (SPI2) || defined (SPI3) || defined (SPI4) || defined (SPI5) || defined(SPI6)
00037 
00038 /** @addtogroup SPI_LL
00039   * @{
00040   */
00041 
00042 /* Private types -------------------------------------------------------------*/
00043 /* Private variables ---------------------------------------------------------*/
00044 
00045 /* Private constants ---------------------------------------------------------*/
00046 /** @defgroup SPI_LL_Private_Constants SPI Private Constants
00047   * @{
00048   */
00049 /* SPI registers Masks */
00050 #define SPI_CR1_CLEAR_MASK                 (SPI_CR1_CPHA    | SPI_CR1_CPOL     | SPI_CR1_MSTR   | \
00051                                             SPI_CR1_BR      | SPI_CR1_LSBFIRST | SPI_CR1_SSI    | \
00052                                             SPI_CR1_SSM     | SPI_CR1_RXONLY   | SPI_CR1_DFF    | \
00053                                             SPI_CR1_CRCNEXT | SPI_CR1_CRCEN    | SPI_CR1_BIDIOE | \
00054                                             SPI_CR1_BIDIMODE)
00055 /**
00056   * @}
00057   */
00058 
00059 /* Private macros ------------------------------------------------------------*/
00060 /** @defgroup SPI_LL_Private_Macros SPI Private Macros
00061   * @{
00062   */
00063 #define IS_LL_SPI_TRANSFER_DIRECTION(__VALUE__) (((__VALUE__) == LL_SPI_FULL_DUPLEX)       \
00064                                                  || ((__VALUE__) == LL_SPI_SIMPLEX_RX)     \
00065                                                  || ((__VALUE__) == LL_SPI_HALF_DUPLEX_RX) \
00066                                                  || ((__VALUE__) == LL_SPI_HALF_DUPLEX_TX))
00067 
00068 #define IS_LL_SPI_MODE(__VALUE__) (((__VALUE__) == LL_SPI_MODE_MASTER) \
00069                                    || ((__VALUE__) == LL_SPI_MODE_SLAVE))
00070 
00071 #define IS_LL_SPI_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_SPI_DATAWIDTH_8BIT)  \
00072                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_16BIT))
00073 
00074 #define IS_LL_SPI_POLARITY(__VALUE__) (((__VALUE__) == LL_SPI_POLARITY_LOW) \
00075                                        || ((__VALUE__) == LL_SPI_POLARITY_HIGH))
00076 
00077 #define IS_LL_SPI_PHASE(__VALUE__) (((__VALUE__) == LL_SPI_PHASE_1EDGE) \
00078                                     || ((__VALUE__) == LL_SPI_PHASE_2EDGE))
00079 
00080 #define IS_LL_SPI_NSS(__VALUE__) (((__VALUE__) == LL_SPI_NSS_SOFT)          \
00081                                   || ((__VALUE__) == LL_SPI_NSS_HARD_INPUT) \
00082                                   || ((__VALUE__) == LL_SPI_NSS_HARD_OUTPUT))
00083 
00084 #define IS_LL_SPI_BAUDRATE(__VALUE__) (((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV2)      \
00085                                        || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV4)   \
00086                                        || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV8)   \
00087                                        || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV16)  \
00088                                        || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV32)  \
00089                                        || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV64)  \
00090                                        || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV128) \
00091                                        || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV256))
00092 
00093 #define IS_LL_SPI_BITORDER(__VALUE__) (((__VALUE__) == LL_SPI_LSB_FIRST) \
00094                                        || ((__VALUE__) == LL_SPI_MSB_FIRST))
00095 
00096 #define IS_LL_SPI_CRCCALCULATION(__VALUE__) (((__VALUE__) == LL_SPI_CRCCALCULATION_ENABLE) \
00097                                              || ((__VALUE__) == LL_SPI_CRCCALCULATION_DISABLE))
00098 
00099 #define IS_LL_SPI_CRC_POLYNOMIAL(__VALUE__) ((__VALUE__) >= 0x1U)
00100 
00101 /**
00102   * @}
00103   */
00104 
00105 /* Private function prototypes -----------------------------------------------*/
00106 
00107 /* Exported functions --------------------------------------------------------*/
00108 /** @addtogroup SPI_LL_Exported_Functions
00109   * @{
00110   */
00111 
00112 /** @addtogroup SPI_LL_EF_Init
00113   * @{
00114   */
00115 
00116 /**
00117   * @brief  De-initialize the SPI registers to their default reset values.
00118   * @param  SPIx SPI Instance
00119   * @retval An ErrorStatus enumeration value:
00120   *          - SUCCESS: SPI registers are de-initialized
00121   *          - ERROR: SPI registers are not de-initialized
00122   */
00123 ErrorStatus LL_SPI_DeInit(SPI_TypeDef *SPIx)
00124 {
00125   ErrorStatus status = ERROR;
00126 
00127   /* Check the parameters */
00128   assert_param(IS_SPI_ALL_INSTANCE(SPIx));
00129 
00130 #if defined(SPI1)
00131   if (SPIx == SPI1)
00132   {
00133     /* Force reset of SPI clock */
00134     LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI1);
00135 
00136     /* Release reset of SPI clock */
00137     LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_SPI1);
00138 
00139     status = SUCCESS;
00140   }
00141 #endif /* SPI1 */
00142 #if defined(SPI2)
00143   if (SPIx == SPI2)
00144   {
00145     /* Force reset of SPI clock */
00146     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_SPI2);
00147 
00148     /* Release reset of SPI clock */
00149     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_SPI2);
00150 
00151     status = SUCCESS;
00152   }
00153 #endif /* SPI2 */
00154 #if defined(SPI3)
00155   if (SPIx == SPI3)
00156   {
00157     /* Force reset of SPI clock */
00158     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_SPI3);
00159 
00160     /* Release reset of SPI clock */
00161     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_SPI3);
00162 
00163     status = SUCCESS;
00164   }
00165 #endif /* SPI3 */
00166 #if defined(SPI4)
00167   if (SPIx == SPI4)
00168   {
00169     /* Force reset of SPI clock */
00170     LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI4);
00171 
00172     /* Release reset of SPI clock */
00173     LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_SPI4);
00174 
00175     status = SUCCESS;
00176   }
00177 #endif /* SPI4 */
00178 #if defined(SPI5)
00179   if (SPIx == SPI5)
00180   {
00181     /* Force reset of SPI clock */
00182     LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI5);
00183 
00184     /* Release reset of SPI clock */
00185     LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_SPI5);
00186 
00187     status = SUCCESS;
00188   }
00189 #endif /* SPI5 */
00190 #if defined(SPI6)
00191   if (SPIx == SPI6)
00192   {
00193     /* Force reset of SPI clock */
00194     LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI6);
00195 
00196     /* Release reset of SPI clock */
00197     LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_SPI6);
00198 
00199     status = SUCCESS;
00200   }
00201 #endif /* SPI6 */
00202 
00203   return status;
00204 }
00205 
00206 /**
00207   * @brief  Initialize the SPI registers according to the specified parameters in SPI_InitStruct.
00208   * @note   As some bits in SPI configuration registers can only be written when the SPI is disabled (SPI_CR1_SPE bit =0),
00209   *         SPI peripheral should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
00210   * @param  SPIx SPI Instance
00211   * @param  SPI_InitStruct pointer to a @ref LL_SPI_InitTypeDef structure
00212   * @retval An ErrorStatus enumeration value. (Return always SUCCESS)
00213   */
00214 ErrorStatus LL_SPI_Init(SPI_TypeDef *SPIx, LL_SPI_InitTypeDef *SPI_InitStruct)
00215 {
00216   ErrorStatus status = ERROR;
00217 
00218   /* Check the SPI Instance SPIx*/
00219   assert_param(IS_SPI_ALL_INSTANCE(SPIx));
00220 
00221   /* Check the SPI parameters from SPI_InitStruct*/
00222   assert_param(IS_LL_SPI_TRANSFER_DIRECTION(SPI_InitStruct->TransferDirection));
00223   assert_param(IS_LL_SPI_MODE(SPI_InitStruct->Mode));
00224   assert_param(IS_LL_SPI_DATAWIDTH(SPI_InitStruct->DataWidth));
00225   assert_param(IS_LL_SPI_POLARITY(SPI_InitStruct->ClockPolarity));
00226   assert_param(IS_LL_SPI_PHASE(SPI_InitStruct->ClockPhase));
00227   assert_param(IS_LL_SPI_NSS(SPI_InitStruct->NSS));
00228   assert_param(IS_LL_SPI_BAUDRATE(SPI_InitStruct->BaudRate));
00229   assert_param(IS_LL_SPI_BITORDER(SPI_InitStruct->BitOrder));
00230   assert_param(IS_LL_SPI_CRCCALCULATION(SPI_InitStruct->CRCCalculation));
00231 
00232   if (LL_SPI_IsEnabled(SPIx) == 0x00000000U)
00233   {
00234     /*---------------------------- SPIx CR1 Configuration ------------------------
00235      * Configure SPIx CR1 with parameters:
00236      * - TransferDirection:  SPI_CR1_BIDIMODE, SPI_CR1_BIDIOE and SPI_CR1_RXONLY bits
00237      * - Master/Slave Mode:  SPI_CR1_MSTR bit
00238      * - DataWidth:          SPI_CR1_DFF bit
00239      * - ClockPolarity:      SPI_CR1_CPOL bit
00240      * - ClockPhase:         SPI_CR1_CPHA bit
00241      * - NSS management:     SPI_CR1_SSM bit
00242      * - BaudRate prescaler: SPI_CR1_BR[2:0] bits
00243      * - BitOrder:           SPI_CR1_LSBFIRST bit
00244      * - CRCCalculation:     SPI_CR1_CRCEN bit
00245      */
00246     MODIFY_REG(SPIx->CR1,
00247                SPI_CR1_CLEAR_MASK,
00248                SPI_InitStruct->TransferDirection | SPI_InitStruct->Mode | SPI_InitStruct->DataWidth |
00249                SPI_InitStruct->ClockPolarity | SPI_InitStruct->ClockPhase |
00250                SPI_InitStruct->NSS | SPI_InitStruct->BaudRate |
00251                SPI_InitStruct->BitOrder | SPI_InitStruct->CRCCalculation);
00252 
00253     /*---------------------------- SPIx CR2 Configuration ------------------------
00254      * Configure SPIx CR2 with parameters:
00255      * - NSS management:     SSOE bit
00256      */
00257     MODIFY_REG(SPIx->CR2, SPI_CR2_SSOE, (SPI_InitStruct->NSS >> 16U));
00258 
00259     /*---------------------------- SPIx CRCPR Configuration ----------------------
00260      * Configure SPIx CRCPR with parameters:
00261      * - CRCPoly:            CRCPOLY[15:0] bits
00262      */
00263     if (SPI_InitStruct->CRCCalculation == LL_SPI_CRCCALCULATION_ENABLE)
00264     {
00265       assert_param(IS_LL_SPI_CRC_POLYNOMIAL(SPI_InitStruct->CRCPoly));
00266       LL_SPI_SetCRCPolynomial(SPIx, SPI_InitStruct->CRCPoly);
00267     }
00268     status = SUCCESS;
00269   }
00270 
00271   /* Activate the SPI mode (Reset I2SMOD bit in I2SCFGR register) */
00272   CLEAR_BIT(SPIx->I2SCFGR, SPI_I2SCFGR_I2SMOD);
00273   return status;
00274 }
00275 
00276 /**
00277   * @brief  Set each @ref LL_SPI_InitTypeDef field to default value.
00278   * @param  SPI_InitStruct pointer to a @ref LL_SPI_InitTypeDef structure
00279   * whose fields will be set to default values.
00280   * @retval None
00281   */
00282 void LL_SPI_StructInit(LL_SPI_InitTypeDef *SPI_InitStruct)
00283 {
00284   /* Set SPI_InitStruct fields to default values */
00285   SPI_InitStruct->TransferDirection = LL_SPI_FULL_DUPLEX;
00286   SPI_InitStruct->Mode              = LL_SPI_MODE_SLAVE;
00287   SPI_InitStruct->DataWidth         = LL_SPI_DATAWIDTH_8BIT;
00288   SPI_InitStruct->ClockPolarity     = LL_SPI_POLARITY_LOW;
00289   SPI_InitStruct->ClockPhase        = LL_SPI_PHASE_1EDGE;
00290   SPI_InitStruct->NSS               = LL_SPI_NSS_HARD_INPUT;
00291   SPI_InitStruct->BaudRate          = LL_SPI_BAUDRATEPRESCALER_DIV2;
00292   SPI_InitStruct->BitOrder          = LL_SPI_MSB_FIRST;
00293   SPI_InitStruct->CRCCalculation    = LL_SPI_CRCCALCULATION_DISABLE;
00294   SPI_InitStruct->CRCPoly           = 7U;
00295 }
00296 
00297 /**
00298   * @}
00299   */
00300 
00301 /**
00302   * @}
00303   */
00304 
00305 /**
00306   * @}
00307   */
00308 
00309 /** @addtogroup I2S_LL
00310   * @{
00311   */
00312 
00313 /* Private types -------------------------------------------------------------*/
00314 /* Private variables ---------------------------------------------------------*/
00315 /* Private constants ---------------------------------------------------------*/
00316 /** @defgroup I2S_LL_Private_Constants I2S Private Constants
00317   * @{
00318   */
00319 /* I2S registers Masks */
00320 #define I2S_I2SCFGR_CLEAR_MASK             (SPI_I2SCFGR_CHLEN   | SPI_I2SCFGR_DATLEN | \
00321                                             SPI_I2SCFGR_CKPOL   | SPI_I2SCFGR_I2SSTD | \
00322                                             SPI_I2SCFGR_I2SCFG  | SPI_I2SCFGR_I2SMOD )
00323 
00324 #define I2S_I2SPR_CLEAR_MASK               0x0002U
00325 /**
00326   * @}
00327   */
00328 /* Private macros ------------------------------------------------------------*/
00329 /** @defgroup I2S_LL_Private_Macros I2S Private Macros
00330   * @{
00331   */
00332 
00333 #define IS_LL_I2S_DATAFORMAT(__VALUE__)  (((__VALUE__) == LL_I2S_DATAFORMAT_16B)             \
00334                                           || ((__VALUE__) == LL_I2S_DATAFORMAT_16B_EXTENDED) \
00335                                           || ((__VALUE__) == LL_I2S_DATAFORMAT_24B)          \
00336                                           || ((__VALUE__) == LL_I2S_DATAFORMAT_32B))
00337 
00338 #define IS_LL_I2S_CPOL(__VALUE__)        (((__VALUE__) == LL_I2S_POLARITY_LOW)  \
00339                                           || ((__VALUE__) == LL_I2S_POLARITY_HIGH))
00340 
00341 #define IS_LL_I2S_STANDARD(__VALUE__)    (((__VALUE__) == LL_I2S_STANDARD_PHILIPS)      \
00342                                           || ((__VALUE__) == LL_I2S_STANDARD_MSB)       \
00343                                           || ((__VALUE__) == LL_I2S_STANDARD_LSB)       \
00344                                           || ((__VALUE__) == LL_I2S_STANDARD_PCM_SHORT) \
00345                                           || ((__VALUE__) == LL_I2S_STANDARD_PCM_LONG))
00346 
00347 #define IS_LL_I2S_MODE(__VALUE__)        (((__VALUE__) == LL_I2S_MODE_SLAVE_TX)     \
00348                                           || ((__VALUE__) == LL_I2S_MODE_SLAVE_RX)  \
00349                                           || ((__VALUE__) == LL_I2S_MODE_MASTER_TX) \
00350                                           || ((__VALUE__) == LL_I2S_MODE_MASTER_RX))
00351 
00352 #define IS_LL_I2S_MCLK_OUTPUT(__VALUE__) (((__VALUE__) == LL_I2S_MCLK_OUTPUT_ENABLE) \
00353                                           || ((__VALUE__) == LL_I2S_MCLK_OUTPUT_DISABLE))
00354 
00355 #define IS_LL_I2S_AUDIO_FREQ(__VALUE__) ((((__VALUE__) >= LL_I2S_AUDIOFREQ_8K)       \
00356                                           && ((__VALUE__) <= LL_I2S_AUDIOFREQ_192K)) \
00357                                          || ((__VALUE__) == LL_I2S_AUDIOFREQ_DEFAULT))
00358 
00359 #define IS_LL_I2S_PRESCALER_LINEAR(__VALUE__)  ((__VALUE__) >= 0x2U)
00360 
00361 #define IS_LL_I2S_PRESCALER_PARITY(__VALUE__) (((__VALUE__) == LL_I2S_PRESCALER_PARITY_EVEN) \
00362                                                || ((__VALUE__) == LL_I2S_PRESCALER_PARITY_ODD))
00363 /**
00364   * @}
00365   */
00366 
00367 /* Private function prototypes -----------------------------------------------*/
00368 
00369 /* Exported functions --------------------------------------------------------*/
00370 /** @addtogroup I2S_LL_Exported_Functions
00371   * @{
00372   */
00373 
00374 /** @addtogroup I2S_LL_EF_Init
00375   * @{
00376   */
00377 
00378 /**
00379   * @brief  De-initialize the SPI/I2S registers to their default reset values.
00380   * @param  SPIx SPI Instance
00381   * @retval An ErrorStatus enumeration value:
00382   *          - SUCCESS: SPI registers are de-initialized
00383   *          - ERROR: SPI registers are not de-initialized
00384   */
00385 ErrorStatus LL_I2S_DeInit(SPI_TypeDef *SPIx)
00386 {
00387   return LL_SPI_DeInit(SPIx);
00388 }
00389 
00390 /**
00391   * @brief  Initializes the SPI/I2S registers according to the specified parameters in I2S_InitStruct.
00392   * @note   As some bits in SPI configuration registers can only be written when the SPI is disabled (SPI_CR1_SPE bit =0),
00393   *         SPI peripheral should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
00394   * @param  SPIx SPI Instance
00395   * @param  I2S_InitStruct pointer to a @ref LL_I2S_InitTypeDef structure
00396   * @retval An ErrorStatus enumeration value:
00397   *          - SUCCESS: SPI registers are Initialized
00398   *          - ERROR: SPI registers are not Initialized
00399   */
00400 ErrorStatus LL_I2S_Init(SPI_TypeDef *SPIx, LL_I2S_InitTypeDef *I2S_InitStruct)
00401 {
00402   uint32_t i2sdiv = 2U;
00403   uint32_t i2sodd = 0U;
00404   uint32_t packetlength = 1U;
00405   uint32_t tmp;
00406   uint32_t sourceclock;
00407   ErrorStatus status = ERROR;
00408 
00409   /* Check the I2S parameters */
00410   assert_param(IS_I2S_ALL_INSTANCE(SPIx));
00411   assert_param(IS_LL_I2S_MODE(I2S_InitStruct->Mode));
00412   assert_param(IS_LL_I2S_STANDARD(I2S_InitStruct->Standard));
00413   assert_param(IS_LL_I2S_DATAFORMAT(I2S_InitStruct->DataFormat));
00414   assert_param(IS_LL_I2S_MCLK_OUTPUT(I2S_InitStruct->MCLKOutput));
00415   assert_param(IS_LL_I2S_AUDIO_FREQ(I2S_InitStruct->AudioFreq));
00416   assert_param(IS_LL_I2S_CPOL(I2S_InitStruct->ClockPolarity));
00417 
00418   if (LL_I2S_IsEnabled(SPIx) == 0x00000000U)
00419   {
00420     /*---------------------------- SPIx I2SCFGR Configuration --------------------
00421      * Configure SPIx I2SCFGR with parameters:
00422      * - Mode:          SPI_I2SCFGR_I2SCFG[1:0] bit
00423      * - Standard:      SPI_I2SCFGR_I2SSTD[1:0] and SPI_I2SCFGR_PCMSYNC bits
00424      * - DataFormat:    SPI_I2SCFGR_CHLEN and SPI_I2SCFGR_DATLEN bits
00425      * - ClockPolarity: SPI_I2SCFGR_CKPOL bit
00426      */
00427 
00428     /* Write to SPIx I2SCFGR */
00429     MODIFY_REG(SPIx->I2SCFGR,
00430                I2S_I2SCFGR_CLEAR_MASK,
00431                I2S_InitStruct->Mode | I2S_InitStruct->Standard |
00432                I2S_InitStruct->DataFormat | I2S_InitStruct->ClockPolarity |
00433                SPI_I2SCFGR_I2SMOD);
00434 
00435     /*---------------------------- SPIx I2SPR Configuration ----------------------
00436      * Configure SPIx I2SPR with parameters:
00437      * - MCLKOutput:    SPI_I2SPR_MCKOE bit
00438      * - AudioFreq:     SPI_I2SPR_I2SDIV[7:0] and SPI_I2SPR_ODD bits
00439      */
00440 
00441     /* If the requested audio frequency is not the default, compute the prescaler (i2sodd, i2sdiv)
00442      * else, default values are used:  i2sodd = 0U, i2sdiv = 2U.
00443      */
00444     if (I2S_InitStruct->AudioFreq != LL_I2S_AUDIOFREQ_DEFAULT)
00445     {
00446       /* Check the frame length (For the Prescaler computing)
00447        * Default value: LL_I2S_DATAFORMAT_16B (packetlength = 1U).
00448        */
00449       if (I2S_InitStruct->DataFormat != LL_I2S_DATAFORMAT_16B)
00450       {
00451         /* Packet length is 32 bits */
00452         packetlength = 2U;
00453       }
00454 
00455       /* If an external I2S clock has to be used, the specific define should be set
00456       in the project configuration or in the stm32f4xx_ll_rcc.h file */
00457       /* Get the I2S source clock value */
00458       sourceclock = LL_RCC_GetI2SClockFreq(LL_RCC_I2S1_CLKSOURCE);
00459 
00460       /* Compute the Real divider depending on the MCLK output state with a floating point */
00461       if (I2S_InitStruct->MCLKOutput == LL_I2S_MCLK_OUTPUT_ENABLE)
00462       {
00463         /* MCLK output is enabled */
00464         tmp = (((((sourceclock / 256U) * 10U) / I2S_InitStruct->AudioFreq)) + 5U);
00465       }
00466       else
00467       {
00468         /* MCLK output is disabled */
00469         tmp = (((((sourceclock / (32U * packetlength)) * 10U) / I2S_InitStruct->AudioFreq)) + 5U);
00470       }
00471 
00472       /* Remove the floating point */
00473       tmp = tmp / 10U;
00474 
00475       /* Check the parity of the divider */
00476       i2sodd = (tmp & (uint16_t)0x0001U);
00477 
00478       /* Compute the i2sdiv prescaler */
00479       i2sdiv = ((tmp - i2sodd) / 2U);
00480 
00481       /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */
00482       i2sodd = (i2sodd << 8U);
00483     }
00484 
00485     /* Test if the divider is 1 or 0 or greater than 0xFF */
00486     if ((i2sdiv < 2U) || (i2sdiv > 0xFFU))
00487     {
00488       /* Set the default values */
00489       i2sdiv = 2U;
00490       i2sodd = 0U;
00491     }
00492 
00493     /* Write to SPIx I2SPR register the computed value */
00494     WRITE_REG(SPIx->I2SPR, i2sdiv | i2sodd | I2S_InitStruct->MCLKOutput);
00495 
00496     status = SUCCESS;
00497   }
00498   return status;
00499 }
00500 
00501 /**
00502   * @brief  Set each @ref LL_I2S_InitTypeDef field to default value.
00503   * @param  I2S_InitStruct pointer to a @ref LL_I2S_InitTypeDef structure
00504   *         whose fields will be set to default values.
00505   * @retval None
00506   */
00507 void LL_I2S_StructInit(LL_I2S_InitTypeDef *I2S_InitStruct)
00508 {
00509   /*--------------- Reset I2S init structure parameters values -----------------*/
00510   I2S_InitStruct->Mode              = LL_I2S_MODE_SLAVE_TX;
00511   I2S_InitStruct->Standard          = LL_I2S_STANDARD_PHILIPS;
00512   I2S_InitStruct->DataFormat        = LL_I2S_DATAFORMAT_16B;
00513   I2S_InitStruct->MCLKOutput        = LL_I2S_MCLK_OUTPUT_DISABLE;
00514   I2S_InitStruct->AudioFreq         = LL_I2S_AUDIOFREQ_DEFAULT;
00515   I2S_InitStruct->ClockPolarity     = LL_I2S_POLARITY_LOW;
00516 }
00517 
00518 /**
00519   * @brief  Set linear and parity prescaler.
00520   * @note   To calculate value of PrescalerLinear(I2SDIV[7:0] bits) and PrescalerParity(ODD bit)\n
00521   *         Check Audio frequency table and formulas inside Reference Manual (SPI/I2S).
00522   * @param  SPIx SPI Instance
00523   * @param  PrescalerLinear value Min_Data=0x02 and Max_Data=0xFF.
00524   * @param  PrescalerParity This parameter can be one of the following values:
00525   *         @arg @ref LL_I2S_PRESCALER_PARITY_EVEN
00526   *         @arg @ref LL_I2S_PRESCALER_PARITY_ODD
00527   * @retval None
00528   */
00529 void LL_I2S_ConfigPrescaler(SPI_TypeDef *SPIx, uint32_t PrescalerLinear, uint32_t PrescalerParity)
00530 {
00531   /* Check the I2S parameters */
00532   assert_param(IS_I2S_ALL_INSTANCE(SPIx));
00533   assert_param(IS_LL_I2S_PRESCALER_LINEAR(PrescalerLinear));
00534   assert_param(IS_LL_I2S_PRESCALER_PARITY(PrescalerParity));
00535 
00536   /* Write to SPIx I2SPR */
00537   MODIFY_REG(SPIx->I2SPR, SPI_I2SPR_I2SDIV | SPI_I2SPR_ODD, PrescalerLinear | (PrescalerParity << 8U));
00538 }
00539 
00540 #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
00541 /**
00542   * @brief  Configures the full duplex mode for the I2Sx peripheral using its extension
00543   *         I2Sxext according to the specified parameters in the I2S_InitStruct.
00544   * @note   The structure pointed by I2S_InitStruct parameter should be the same
00545   *         used for the master I2S peripheral. In this case, if the master is
00546   *         configured as transmitter, the slave will be receiver and vice versa.
00547   *         Or you can force a different mode by modifying the field I2S_Mode to the
00548   *         value I2S_SlaveRx or I2S_SlaveTx independently of the master configuration.
00549   * @param  I2Sxext SPI Instance
00550   * @param  I2S_InitStruct pointer to a @ref LL_I2S_InitTypeDef structure
00551   * @retval An ErrorStatus enumeration value:
00552   *          - SUCCESS: I2Sxext registers are Initialized
00553   *          - ERROR: I2Sxext registers are not Initialized
00554   */
00555 ErrorStatus  LL_I2S_InitFullDuplex(SPI_TypeDef *I2Sxext, LL_I2S_InitTypeDef *I2S_InitStruct)
00556 {
00557   uint32_t mode = 0U;
00558   ErrorStatus status = ERROR;
00559 
00560   /* Check the I2S parameters */
00561   assert_param(IS_I2S_EXT_ALL_INSTANCE(I2Sxext));
00562   assert_param(IS_LL_I2S_MODE(I2S_InitStruct->Mode));
00563   assert_param(IS_LL_I2S_STANDARD(I2S_InitStruct->Standard));
00564   assert_param(IS_LL_I2S_DATAFORMAT(I2S_InitStruct->DataFormat));
00565   assert_param(IS_LL_I2S_CPOL(I2S_InitStruct->ClockPolarity));
00566 
00567   if (LL_I2S_IsEnabled(I2Sxext) == 0x00000000U)
00568   {
00569     /*---------------------------- SPIx I2SCFGR Configuration --------------------
00570      * Configure SPIx I2SCFGR with parameters:
00571      * - Mode:          SPI_I2SCFGR_I2SCFG[1:0] bit
00572      * - Standard:      SPI_I2SCFGR_I2SSTD[1:0] and SPI_I2SCFGR_PCMSYNC bits
00573      * - DataFormat:    SPI_I2SCFGR_CHLEN and SPI_I2SCFGR_DATLEN bits
00574      * - ClockPolarity: SPI_I2SCFGR_CKPOL bit
00575      */
00576 
00577     /* Reset I2SPR registers */
00578     WRITE_REG(I2Sxext->I2SPR, I2S_I2SPR_CLEAR_MASK);
00579 
00580     /* Get the mode to be configured for the extended I2S */
00581     if ((I2S_InitStruct->Mode == LL_I2S_MODE_MASTER_TX) || (I2S_InitStruct->Mode == LL_I2S_MODE_SLAVE_TX))
00582     {
00583       mode = LL_I2S_MODE_SLAVE_RX;
00584     }
00585     else
00586     {
00587       if ((I2S_InitStruct->Mode == LL_I2S_MODE_MASTER_RX) || (I2S_InitStruct->Mode == LL_I2S_MODE_SLAVE_RX))
00588       {
00589         mode = LL_I2S_MODE_SLAVE_TX;
00590       }
00591     }
00592 
00593     /* Write to SPIx I2SCFGR */
00594     MODIFY_REG(I2Sxext->I2SCFGR,
00595                I2S_I2SCFGR_CLEAR_MASK,
00596                I2S_InitStruct->Standard |
00597                I2S_InitStruct->DataFormat | I2S_InitStruct->ClockPolarity |
00598                SPI_I2SCFGR_I2SMOD | mode);
00599 
00600     status = SUCCESS;
00601   }
00602   return status;
00603 }
00604 #endif /* SPI_I2S_FULLDUPLEX_SUPPORT */
00605 
00606 /**
00607   * @}
00608   */
00609 
00610 /**
00611   * @}
00612   */
00613 
00614 /**
00615   * @}
00616   */
00617 
00618 #endif /* defined (SPI1) || defined (SPI2) || defined (SPI3) || defined (SPI4) || defined (SPI5) || defined(SPI6) */
00619 
00620 /**
00621   * @}
00622   */
00623 
00624 #endif /* USE_FULL_LL_DRIVER */
00625 
00626 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/