Subversion Repositories ScreenTimer

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32f0xx_ll_spi.c
  4.   * @author  MCD Application Team
  5.   * @brief   SPI LL module driver.
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. #if defined(USE_FULL_LL_DRIVER)
  20.  
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "stm32f0xx_ll_spi.h"
  23. #include "stm32f0xx_ll_bus.h"
  24. #include "stm32f0xx_ll_rcc.h"
  25.  
  26. #ifdef  USE_FULL_ASSERT
  27. #include "stm32_assert.h"
  28. #else
  29. #define assert_param(expr) ((void)0U)
  30. #endif /* USE_FULL_ASSERT */
  31.  
  32. /** @addtogroup STM32F0xx_LL_Driver
  33.   * @{
  34.   */
  35.  
  36. #if defined (SPI1) || defined (SPI2)
  37.  
  38. /** @addtogroup SPI_LL
  39.   * @{
  40.   */
  41.  
  42. /* Private types -------------------------------------------------------------*/
  43. /* Private variables ---------------------------------------------------------*/
  44.  
  45. /* Private constants ---------------------------------------------------------*/
  46. /** @defgroup SPI_LL_Private_Constants SPI Private Constants
  47.   * @{
  48.   */
  49. /* SPI registers Masks */
  50. #define SPI_CR1_CLEAR_MASK                 (SPI_CR1_CPHA    | SPI_CR1_CPOL     | SPI_CR1_MSTR   | \
  51.                                             SPI_CR1_BR      | SPI_CR1_LSBFIRST | SPI_CR1_SSI    | \
  52.                                             SPI_CR1_SSM     | SPI_CR1_RXONLY   | SPI_CR1_CRCL   | \
  53.                                             SPI_CR1_CRCNEXT | SPI_CR1_CRCEN    | SPI_CR1_BIDIOE | \
  54.                                             SPI_CR1_BIDIMODE)
  55. /**
  56.   * @}
  57.   */
  58.  
  59. /* Private macros ------------------------------------------------------------*/
  60. /** @defgroup SPI_LL_Private_Macros SPI Private Macros
  61.   * @{
  62.   */
  63. #define IS_LL_SPI_TRANSFER_DIRECTION(__VALUE__) (((__VALUE__) == LL_SPI_FULL_DUPLEX)       \
  64.                                                  || ((__VALUE__) == LL_SPI_SIMPLEX_RX)     \
  65.                                                  || ((__VALUE__) == LL_SPI_HALF_DUPLEX_RX) \
  66.                                                  || ((__VALUE__) == LL_SPI_HALF_DUPLEX_TX))
  67.  
  68. #define IS_LL_SPI_MODE(__VALUE__) (((__VALUE__) == LL_SPI_MODE_MASTER) \
  69.                                    || ((__VALUE__) == LL_SPI_MODE_SLAVE))
  70.  
  71. #define IS_LL_SPI_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_SPI_DATAWIDTH_4BIT)     \
  72.                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_5BIT)  \
  73.                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_6BIT)  \
  74.                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_7BIT)  \
  75.                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_8BIT)  \
  76.                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_9BIT)  \
  77.                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_10BIT) \
  78.                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_11BIT) \
  79.                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_12BIT) \
  80.                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_13BIT) \
  81.                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_14BIT) \
  82.                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_15BIT) \
  83.                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_16BIT))
  84.  
  85. #define IS_LL_SPI_POLARITY(__VALUE__) (((__VALUE__) == LL_SPI_POLARITY_LOW) \
  86.                                        || ((__VALUE__) == LL_SPI_POLARITY_HIGH))
  87.  
  88. #define IS_LL_SPI_PHASE(__VALUE__) (((__VALUE__) == LL_SPI_PHASE_1EDGE) \
  89.                                     || ((__VALUE__) == LL_SPI_PHASE_2EDGE))
  90.  
  91. #define IS_LL_SPI_NSS(__VALUE__) (((__VALUE__) == LL_SPI_NSS_SOFT)          \
  92.                                   || ((__VALUE__) == LL_SPI_NSS_HARD_INPUT) \
  93.                                   || ((__VALUE__) == LL_SPI_NSS_HARD_OUTPUT))
  94.  
  95. #define IS_LL_SPI_BAUDRATE(__VALUE__) (((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV2)      \
  96.                                        || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV4)   \
  97.                                        || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV8)   \
  98.                                        || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV16)  \
  99.                                        || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV32)  \
  100.                                        || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV64)  \
  101.                                        || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV128) \
  102.                                        || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV256))
  103.  
  104. #define IS_LL_SPI_BITORDER(__VALUE__) (((__VALUE__) == LL_SPI_LSB_FIRST) \
  105.                                        || ((__VALUE__) == LL_SPI_MSB_FIRST))
  106.  
  107. #define IS_LL_SPI_CRCCALCULATION(__VALUE__) (((__VALUE__) == LL_SPI_CRCCALCULATION_ENABLE) \
  108.                                              || ((__VALUE__) == LL_SPI_CRCCALCULATION_DISABLE))
  109.  
  110. #define IS_LL_SPI_CRC_POLYNOMIAL(__VALUE__) ((__VALUE__) >= 0x1U)
  111.  
  112. /**
  113.   * @}
  114.   */
  115.  
  116. /* Private function prototypes -----------------------------------------------*/
  117.  
  118. /* Exported functions --------------------------------------------------------*/
  119. /** @addtogroup SPI_LL_Exported_Functions
  120.   * @{
  121.   */
  122.  
  123. /** @addtogroup SPI_LL_EF_Init
  124.   * @{
  125.   */
  126.  
  127. /**
  128.   * @brief  De-initialize the SPI registers to their default reset values.
  129.   * @param  SPIx SPI Instance
  130.   * @retval An ErrorStatus enumeration value:
  131.   *          - SUCCESS: SPI registers are de-initialized
  132.   *          - ERROR: SPI registers are not de-initialized
  133.   */
  134. ErrorStatus LL_SPI_DeInit(SPI_TypeDef *SPIx)
  135. {
  136.   ErrorStatus status = ERROR;
  137.  
  138.   /* Check the parameters */
  139.   assert_param(IS_SPI_ALL_INSTANCE(SPIx));
  140.  
  141. #if defined(SPI1)
  142.   if (SPIx == SPI1)
  143.   {
  144.     /* Force reset of SPI clock */
  145.     LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_SPI1);
  146.  
  147.     /* Release reset of SPI clock */
  148.     LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_SPI1);
  149.  
  150.     status = SUCCESS;
  151.   }
  152. #endif /* SPI1 */
  153. #if defined(SPI2)
  154.   if (SPIx == SPI2)
  155.   {
  156.     /* Force reset of SPI clock */
  157.     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_SPI2);
  158.  
  159.     /* Release reset of SPI clock */
  160.     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_SPI2);
  161.  
  162.     status = SUCCESS;
  163.   }
  164. #endif /* SPI2 */
  165.  
  166.   return status;
  167. }
  168.  
  169. /**
  170.   * @brief  Initialize the SPI registers according to the specified parameters in SPI_InitStruct.
  171.   * @note   As some bits in SPI configuration registers can only be written when the SPI is disabled (SPI_CR1_SPE bit =0),
  172.   *         SPI peripheral should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
  173.   * @param  SPIx SPI Instance
  174.   * @param  SPI_InitStruct pointer to a @ref LL_SPI_InitTypeDef structure
  175.   * @retval An ErrorStatus enumeration value. (Return always SUCCESS)
  176.   */
  177. ErrorStatus LL_SPI_Init(SPI_TypeDef *SPIx, LL_SPI_InitTypeDef *SPI_InitStruct)
  178. {
  179.   ErrorStatus status = ERROR;
  180.  
  181.   /* Check the SPI Instance SPIx*/
  182.   assert_param(IS_SPI_ALL_INSTANCE(SPIx));
  183.  
  184.   /* Check the SPI parameters from SPI_InitStruct*/
  185.   assert_param(IS_LL_SPI_TRANSFER_DIRECTION(SPI_InitStruct->TransferDirection));
  186.   assert_param(IS_LL_SPI_MODE(SPI_InitStruct->Mode));
  187.   assert_param(IS_LL_SPI_DATAWIDTH(SPI_InitStruct->DataWidth));
  188.   assert_param(IS_LL_SPI_POLARITY(SPI_InitStruct->ClockPolarity));
  189.   assert_param(IS_LL_SPI_PHASE(SPI_InitStruct->ClockPhase));
  190.   assert_param(IS_LL_SPI_NSS(SPI_InitStruct->NSS));
  191.   assert_param(IS_LL_SPI_BAUDRATE(SPI_InitStruct->BaudRate));
  192.   assert_param(IS_LL_SPI_BITORDER(SPI_InitStruct->BitOrder));
  193.   assert_param(IS_LL_SPI_CRCCALCULATION(SPI_InitStruct->CRCCalculation));
  194.  
  195.   if (LL_SPI_IsEnabled(SPIx) == 0x00000000U)
  196.   {
  197.     /*---------------------------- SPIx CR1 Configuration ------------------------
  198.      * Configure SPIx CR1 with parameters:
  199.      * - TransferDirection:  SPI_CR1_BIDIMODE, SPI_CR1_BIDIOE and SPI_CR1_RXONLY bits
  200.      * - Master/Slave Mode:  SPI_CR1_MSTR bit
  201.      * - ClockPolarity:      SPI_CR1_CPOL bit
  202.      * - ClockPhase:         SPI_CR1_CPHA bit
  203.      * - NSS management:     SPI_CR1_SSM bit
  204.      * - BaudRate prescaler: SPI_CR1_BR[2:0] bits
  205.      * - BitOrder:           SPI_CR1_LSBFIRST bit
  206.      * - CRCCalculation:     SPI_CR1_CRCEN bit
  207.      */
  208.     MODIFY_REG(SPIx->CR1,
  209.                SPI_CR1_CLEAR_MASK,
  210.                SPI_InitStruct->TransferDirection | SPI_InitStruct->Mode |
  211.                SPI_InitStruct->ClockPolarity | SPI_InitStruct->ClockPhase |
  212.                SPI_InitStruct->NSS | SPI_InitStruct->BaudRate |
  213.                SPI_InitStruct->BitOrder | SPI_InitStruct->CRCCalculation);
  214.  
  215.     /*---------------------------- SPIx CR2 Configuration ------------------------
  216.      * Configure SPIx CR2 with parameters:
  217.      * - DataWidth:          DS[3:0] bits
  218.      * - NSS management:     SSOE bit
  219.      */
  220.     MODIFY_REG(SPIx->CR2,
  221.                SPI_CR2_DS | SPI_CR2_SSOE,
  222.                SPI_InitStruct->DataWidth | (SPI_InitStruct->NSS >> 16U));
  223.  
  224.     /* Set Rx FIFO to Quarter (1 Byte) in case of 8 Bits mode. No DataPacking by default */
  225.     if (SPI_InitStruct->DataWidth < LL_SPI_DATAWIDTH_9BIT)
  226.     {
  227.       LL_SPI_SetRxFIFOThreshold(SPIx, LL_SPI_RX_FIFO_TH_QUARTER);
  228.     }
  229.  
  230.     /*---------------------------- SPIx CRCPR Configuration ----------------------
  231.      * Configure SPIx CRCPR with parameters:
  232.      * - CRCPoly:            CRCPOLY[15:0] bits
  233.      */
  234.     if (SPI_InitStruct->CRCCalculation == LL_SPI_CRCCALCULATION_ENABLE)
  235.     {
  236.       assert_param(IS_LL_SPI_CRC_POLYNOMIAL(SPI_InitStruct->CRCPoly));
  237.       LL_SPI_SetCRCPolynomial(SPIx, SPI_InitStruct->CRCPoly);
  238.     }
  239.     status = SUCCESS;
  240.   }
  241.  
  242. #if defined (SPI_I2S_SUPPORT)
  243.   /* Activate the SPI mode (Reset I2SMOD bit in I2SCFGR register) */
  244.   CLEAR_BIT(SPIx->I2SCFGR, SPI_I2SCFGR_I2SMOD);
  245. #endif /* SPI_I2S_SUPPORT */
  246.   return status;
  247. }
  248.  
  249. /**
  250.   * @brief  Set each @ref LL_SPI_InitTypeDef field to default value.
  251.   * @param  SPI_InitStruct pointer to a @ref LL_SPI_InitTypeDef structure
  252.   * whose fields will be set to default values.
  253.   * @retval None
  254.   */
  255. void LL_SPI_StructInit(LL_SPI_InitTypeDef *SPI_InitStruct)
  256. {
  257.   /* Set SPI_InitStruct fields to default values */
  258.   SPI_InitStruct->TransferDirection = LL_SPI_FULL_DUPLEX;
  259.   SPI_InitStruct->Mode              = LL_SPI_MODE_SLAVE;
  260.   SPI_InitStruct->DataWidth         = LL_SPI_DATAWIDTH_8BIT;
  261.   SPI_InitStruct->ClockPolarity     = LL_SPI_POLARITY_LOW;
  262.   SPI_InitStruct->ClockPhase        = LL_SPI_PHASE_1EDGE;
  263.   SPI_InitStruct->NSS               = LL_SPI_NSS_HARD_INPUT;
  264.   SPI_InitStruct->BaudRate          = LL_SPI_BAUDRATEPRESCALER_DIV2;
  265.   SPI_InitStruct->BitOrder          = LL_SPI_MSB_FIRST;
  266.   SPI_InitStruct->CRCCalculation    = LL_SPI_CRCCALCULATION_DISABLE;
  267.   SPI_InitStruct->CRCPoly           = 7U;
  268. }
  269.  
  270. /**
  271.   * @}
  272.   */
  273.  
  274. /**
  275.   * @}
  276.   */
  277.  
  278. /**
  279.   * @}
  280.   */
  281.  
  282. #if defined(SPI_I2S_SUPPORT)
  283. /** @addtogroup I2S_LL
  284.   * @{
  285.   */
  286.  
  287. /* Private types -------------------------------------------------------------*/
  288. /* Private variables ---------------------------------------------------------*/
  289. /* Private constants ---------------------------------------------------------*/
  290. /** @defgroup I2S_LL_Private_Constants I2S Private Constants
  291.   * @{
  292.   */
  293. /* I2S registers Masks */
  294. #define I2S_I2SCFGR_CLEAR_MASK             (SPI_I2SCFGR_CHLEN   | SPI_I2SCFGR_DATLEN | \
  295.                                             SPI_I2SCFGR_CKPOL   | SPI_I2SCFGR_I2SSTD | \
  296.                                             SPI_I2SCFGR_I2SCFG  | SPI_I2SCFGR_I2SMOD )
  297.  
  298. #define I2S_I2SPR_CLEAR_MASK               0x0002U
  299. /**
  300.   * @}
  301.   */
  302. /* Private macros ------------------------------------------------------------*/
  303. /** @defgroup I2S_LL_Private_Macros I2S Private Macros
  304.   * @{
  305.   */
  306.  
  307. #define IS_LL_I2S_DATAFORMAT(__VALUE__)  (((__VALUE__) == LL_I2S_DATAFORMAT_16B)             \
  308.                                           || ((__VALUE__) == LL_I2S_DATAFORMAT_16B_EXTENDED) \
  309.                                           || ((__VALUE__) == LL_I2S_DATAFORMAT_24B)          \
  310.                                           || ((__VALUE__) == LL_I2S_DATAFORMAT_32B))
  311.  
  312. #define IS_LL_I2S_CPOL(__VALUE__)        (((__VALUE__) == LL_I2S_POLARITY_LOW)  \
  313.                                           || ((__VALUE__) == LL_I2S_POLARITY_HIGH))
  314.  
  315. #define IS_LL_I2S_STANDARD(__VALUE__)    (((__VALUE__) == LL_I2S_STANDARD_PHILIPS)      \
  316.                                           || ((__VALUE__) == LL_I2S_STANDARD_MSB)       \
  317.                                           || ((__VALUE__) == LL_I2S_STANDARD_LSB)       \
  318.                                           || ((__VALUE__) == LL_I2S_STANDARD_PCM_SHORT) \
  319.                                           || ((__VALUE__) == LL_I2S_STANDARD_PCM_LONG))
  320.  
  321. #define IS_LL_I2S_MODE(__VALUE__)        (((__VALUE__) == LL_I2S_MODE_SLAVE_TX)     \
  322.                                           || ((__VALUE__) == LL_I2S_MODE_SLAVE_RX)  \
  323.                                           || ((__VALUE__) == LL_I2S_MODE_MASTER_TX) \
  324.                                           || ((__VALUE__) == LL_I2S_MODE_MASTER_RX))
  325.  
  326. #define IS_LL_I2S_MCLK_OUTPUT(__VALUE__) (((__VALUE__) == LL_I2S_MCLK_OUTPUT_ENABLE) \
  327.                                           || ((__VALUE__) == LL_I2S_MCLK_OUTPUT_DISABLE))
  328.  
  329. #define IS_LL_I2S_AUDIO_FREQ(__VALUE__) ((((__VALUE__) >= LL_I2S_AUDIOFREQ_8K)       \
  330.                                           && ((__VALUE__) <= LL_I2S_AUDIOFREQ_192K)) \
  331.                                          || ((__VALUE__) == LL_I2S_AUDIOFREQ_DEFAULT))
  332.  
  333. #define IS_LL_I2S_PRESCALER_LINEAR(__VALUE__)  ((__VALUE__) >= 0x2U)
  334.  
  335. #define IS_LL_I2S_PRESCALER_PARITY(__VALUE__) (((__VALUE__) == LL_I2S_PRESCALER_PARITY_EVEN) \
  336.                                                || ((__VALUE__) == LL_I2S_PRESCALER_PARITY_ODD))
  337. /**
  338.   * @}
  339.   */
  340.  
  341. /* Private function prototypes -----------------------------------------------*/
  342.  
  343. /* Exported functions --------------------------------------------------------*/
  344. /** @addtogroup I2S_LL_Exported_Functions
  345.   * @{
  346.   */
  347.  
  348. /** @addtogroup I2S_LL_EF_Init
  349.   * @{
  350.   */
  351.  
  352. /**
  353.   * @brief  De-initialize the SPI/I2S registers to their default reset values.
  354.   * @param  SPIx SPI Instance
  355.   * @retval An ErrorStatus enumeration value:
  356.   *          - SUCCESS: SPI registers are de-initialized
  357.   *          - ERROR: SPI registers are not de-initialized
  358.   */
  359. ErrorStatus LL_I2S_DeInit(SPI_TypeDef *SPIx)
  360. {
  361.   return LL_SPI_DeInit(SPIx);
  362. }
  363.  
  364. /**
  365.   * @brief  Initializes the SPI/I2S registers according to the specified parameters in I2S_InitStruct.
  366.   * @note   As some bits in SPI configuration registers can only be written when the SPI is disabled (SPI_CR1_SPE bit =0),
  367.   *         SPI peripheral should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
  368.   * @param  SPIx SPI Instance
  369.   * @param  I2S_InitStruct pointer to a @ref LL_I2S_InitTypeDef structure
  370.   * @retval An ErrorStatus enumeration value:
  371.   *          - SUCCESS: SPI registers are Initialized
  372.   *          - ERROR: SPI registers are not Initialized
  373.   */
  374. ErrorStatus LL_I2S_Init(SPI_TypeDef *SPIx, LL_I2S_InitTypeDef *I2S_InitStruct)
  375. {
  376.   uint32_t i2sdiv = 2U;
  377.   uint32_t i2sodd = 0U;
  378.   uint32_t packetlength = 1U;
  379.   uint32_t tmp;
  380.   LL_RCC_ClocksTypeDef rcc_clocks;
  381.   uint32_t sourceclock;
  382.   ErrorStatus status = ERROR;
  383.  
  384.   /* Check the I2S parameters */
  385.   assert_param(IS_I2S_ALL_INSTANCE(SPIx));
  386.   assert_param(IS_LL_I2S_MODE(I2S_InitStruct->Mode));
  387.   assert_param(IS_LL_I2S_STANDARD(I2S_InitStruct->Standard));
  388.   assert_param(IS_LL_I2S_DATAFORMAT(I2S_InitStruct->DataFormat));
  389.   assert_param(IS_LL_I2S_MCLK_OUTPUT(I2S_InitStruct->MCLKOutput));
  390.   assert_param(IS_LL_I2S_AUDIO_FREQ(I2S_InitStruct->AudioFreq));
  391.   assert_param(IS_LL_I2S_CPOL(I2S_InitStruct->ClockPolarity));
  392.  
  393.   if (LL_I2S_IsEnabled(SPIx) == 0x00000000U)
  394.   {
  395.     /*---------------------------- SPIx I2SCFGR Configuration --------------------
  396.      * Configure SPIx I2SCFGR with parameters:
  397.      * - Mode:          SPI_I2SCFGR_I2SCFG[1:0] bit
  398.      * - Standard:      SPI_I2SCFGR_I2SSTD[1:0] and SPI_I2SCFGR_PCMSYNC bits
  399.      * - DataFormat:    SPI_I2SCFGR_CHLEN and SPI_I2SCFGR_DATLEN bits
  400.      * - ClockPolarity: SPI_I2SCFGR_CKPOL bit
  401.      */
  402.  
  403.     /* Write to SPIx I2SCFGR */
  404.     MODIFY_REG(SPIx->I2SCFGR,
  405.                I2S_I2SCFGR_CLEAR_MASK,
  406.                I2S_InitStruct->Mode | I2S_InitStruct->Standard |
  407.                I2S_InitStruct->DataFormat | I2S_InitStruct->ClockPolarity |
  408.                SPI_I2SCFGR_I2SMOD);
  409.  
  410.     /*---------------------------- SPIx I2SPR Configuration ----------------------
  411.      * Configure SPIx I2SPR with parameters:
  412.      * - MCLKOutput:    SPI_I2SPR_MCKOE bit
  413.      * - AudioFreq:     SPI_I2SPR_I2SDIV[7:0] and SPI_I2SPR_ODD bits
  414.      */
  415.  
  416.     /* If the requested audio frequency is not the default, compute the prescaler (i2sodd, i2sdiv)
  417.      * else, default values are used:  i2sodd = 0U, i2sdiv = 2U.
  418.      */
  419.     if (I2S_InitStruct->AudioFreq != LL_I2S_AUDIOFREQ_DEFAULT)
  420.     {
  421.       /* Check the frame length (For the Prescaler computing)
  422.        * Default value: LL_I2S_DATAFORMAT_16B (packetlength = 1U).
  423.        */
  424.       if (I2S_InitStruct->DataFormat != LL_I2S_DATAFORMAT_16B)
  425.       {
  426.         /* Packet length is 32 bits */
  427.         packetlength = 2U;
  428.       }
  429.  
  430.       /* I2S Clock source is System clock: Get System Clock frequency */
  431.       LL_RCC_GetSystemClocksFreq(&rcc_clocks);
  432.  
  433.       /* Get the source clock value: based on System Clock value */
  434.       sourceclock = rcc_clocks.SYSCLK_Frequency;
  435.  
  436.       /* Compute the Real divider depending on the MCLK output state with a floating point */
  437.       if (I2S_InitStruct->MCLKOutput == LL_I2S_MCLK_OUTPUT_ENABLE)
  438.       {
  439.         /* MCLK output is enabled */
  440.         tmp = (((((sourceclock / 256U) * 10U) / I2S_InitStruct->AudioFreq)) + 5U);
  441.       }
  442.       else
  443.       {
  444.         /* MCLK output is disabled */
  445.         tmp = (((((sourceclock / (32U * packetlength)) * 10U) / I2S_InitStruct->AudioFreq)) + 5U);
  446.       }
  447.  
  448.       /* Remove the floating point */
  449.       tmp = tmp / 10U;
  450.  
  451.       /* Check the parity of the divider */
  452.       i2sodd = (tmp & (uint16_t)0x0001U);
  453.  
  454.       /* Compute the i2sdiv prescaler */
  455.       i2sdiv = ((tmp - i2sodd) / 2U);
  456.  
  457.       /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */
  458.       i2sodd = (i2sodd << 8U);
  459.     }
  460.  
  461.     /* Test if the divider is 1 or 0 or greater than 0xFF */
  462.     if ((i2sdiv < 2U) || (i2sdiv > 0xFFU))
  463.     {
  464.       /* Set the default values */
  465.       i2sdiv = 2U;
  466.       i2sodd = 0U;
  467.     }
  468.  
  469.     /* Write to SPIx I2SPR register the computed value */
  470.     WRITE_REG(SPIx->I2SPR, i2sdiv | i2sodd | I2S_InitStruct->MCLKOutput);
  471.  
  472.     status = SUCCESS;
  473.   }
  474.   return status;
  475. }
  476.  
  477. /**
  478.   * @brief  Set each @ref LL_I2S_InitTypeDef field to default value.
  479.   * @param  I2S_InitStruct pointer to a @ref LL_I2S_InitTypeDef structure
  480.   *         whose fields will be set to default values.
  481.   * @retval None
  482.   */
  483. void LL_I2S_StructInit(LL_I2S_InitTypeDef *I2S_InitStruct)
  484. {
  485.   /*--------------- Reset I2S init structure parameters values -----------------*/
  486.   I2S_InitStruct->Mode              = LL_I2S_MODE_SLAVE_TX;
  487.   I2S_InitStruct->Standard          = LL_I2S_STANDARD_PHILIPS;
  488.   I2S_InitStruct->DataFormat        = LL_I2S_DATAFORMAT_16B;
  489.   I2S_InitStruct->MCLKOutput        = LL_I2S_MCLK_OUTPUT_DISABLE;
  490.   I2S_InitStruct->AudioFreq         = LL_I2S_AUDIOFREQ_DEFAULT;
  491.   I2S_InitStruct->ClockPolarity     = LL_I2S_POLARITY_LOW;
  492. }
  493.  
  494. /**
  495.   * @brief  Set linear and parity prescaler.
  496.   * @note   To calculate value of PrescalerLinear(I2SDIV[7:0] bits) and PrescalerParity(ODD bit)\n
  497.   *         Check Audio frequency table and formulas inside Reference Manual (SPI/I2S).
  498.   * @param  SPIx SPI Instance
  499.   * @param  PrescalerLinear value Min_Data=0x02 and Max_Data=0xFF.
  500.   * @param  PrescalerParity This parameter can be one of the following values:
  501.   *         @arg @ref LL_I2S_PRESCALER_PARITY_EVEN
  502.   *         @arg @ref LL_I2S_PRESCALER_PARITY_ODD
  503.   * @retval None
  504.   */
  505. void LL_I2S_ConfigPrescaler(SPI_TypeDef *SPIx, uint32_t PrescalerLinear, uint32_t PrescalerParity)
  506. {
  507.   /* Check the I2S parameters */
  508.   assert_param(IS_I2S_ALL_INSTANCE(SPIx));
  509.   assert_param(IS_LL_I2S_PRESCALER_LINEAR(PrescalerLinear));
  510.   assert_param(IS_LL_I2S_PRESCALER_PARITY(PrescalerParity));
  511.  
  512.   /* Write to SPIx I2SPR */
  513.   MODIFY_REG(SPIx->I2SPR, SPI_I2SPR_I2SDIV | SPI_I2SPR_ODD, PrescalerLinear | (PrescalerParity << 8U));
  514. }
  515.  
  516. /**
  517.   * @}
  518.   */
  519.  
  520. /**
  521.   * @}
  522.   */
  523.  
  524. /**
  525.   * @}
  526.   */
  527. #endif /* SPI_I2S_SUPPORT */
  528.  
  529. #endif /* defined (SPI1) || defined (SPI2) */
  530.  
  531. /**
  532.   * @}
  533.   */
  534.  
  535. #endif /* USE_FULL_LL_DRIVER */
  536.  
  537. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  538.