You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

643 lines
24 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_ll_spi.c
  4. * @author MCD Application Team
  5. * @version V1.7.1
  6. * @date 14-April-2017
  7. * @brief SPI LL module driver.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  12. *
  13. * Redistribution and use in source and binary forms, with or without modification,
  14. * are permitted provided that the following conditions are met:
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ******************************************************************************
  36. */
  37. #if defined(USE_FULL_LL_DRIVER)
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "stm32f4xx_ll_spi.h"
  40. #include "stm32f4xx_ll_bus.h"
  41. #include "stm32f4xx_ll_rcc.h"
  42. #ifdef USE_FULL_ASSERT
  43. #include "stm32_assert.h"
  44. #else
  45. #define assert_param(expr) ((void)0U)
  46. #endif
  47. /** @addtogroup STM32F4xx_LL_Driver
  48. * @{
  49. */
  50. #if defined (SPI1) || defined (SPI2) || defined (SPI3) || defined (SPI4) || defined (SPI5) || defined(SPI6)
  51. /** @addtogroup SPI_LL
  52. * @{
  53. */
  54. /* Private types -------------------------------------------------------------*/
  55. /* Private variables ---------------------------------------------------------*/
  56. /* Private constants ---------------------------------------------------------*/
  57. /** @defgroup SPI_LL_Private_Constants SPI Private Constants
  58. * @{
  59. */
  60. /* SPI registers Masks */
  61. #define SPI_CR1_CLEAR_MASK (SPI_CR1_CPHA | SPI_CR1_CPOL | SPI_CR1_MSTR | \
  62. SPI_CR1_BR | SPI_CR1_LSBFIRST | SPI_CR1_SSI | \
  63. SPI_CR1_SSM | SPI_CR1_RXONLY | SPI_CR1_DFF | \
  64. SPI_CR1_CRCNEXT | SPI_CR1_CRCEN | SPI_CR1_BIDIOE | \
  65. SPI_CR1_BIDIMODE)
  66. /**
  67. * @}
  68. */
  69. /* Private macros ------------------------------------------------------------*/
  70. /** @defgroup SPI_LL_Private_Macros SPI Private Macros
  71. * @{
  72. */
  73. #define IS_LL_SPI_TRANSFER_DIRECTION(__VALUE__) (((__VALUE__) == LL_SPI_FULL_DUPLEX) \
  74. || ((__VALUE__) == LL_SPI_SIMPLEX_RX) \
  75. || ((__VALUE__) == LL_SPI_HALF_DUPLEX_RX) \
  76. || ((__VALUE__) == LL_SPI_HALF_DUPLEX_TX))
  77. #define IS_LL_SPI_MODE(__VALUE__) (((__VALUE__) == LL_SPI_MODE_MASTER) \
  78. || ((__VALUE__) == LL_SPI_MODE_SLAVE))
  79. #define IS_LL_SPI_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_SPI_DATAWIDTH_8BIT) \
  80. || ((__VALUE__) == LL_SPI_DATAWIDTH_16BIT))
  81. #define IS_LL_SPI_POLARITY(__VALUE__) (((__VALUE__) == LL_SPI_POLARITY_LOW) \
  82. || ((__VALUE__) == LL_SPI_POLARITY_HIGH))
  83. #define IS_LL_SPI_PHASE(__VALUE__) (((__VALUE__) == LL_SPI_PHASE_1EDGE) \
  84. || ((__VALUE__) == LL_SPI_PHASE_2EDGE))
  85. #define IS_LL_SPI_NSS(__VALUE__) (((__VALUE__) == LL_SPI_NSS_SOFT) \
  86. || ((__VALUE__) == LL_SPI_NSS_HARD_INPUT) \
  87. || ((__VALUE__) == LL_SPI_NSS_HARD_OUTPUT))
  88. #define IS_LL_SPI_BAUDRATE(__VALUE__) (((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV2) \
  89. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV4) \
  90. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV8) \
  91. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV16) \
  92. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV32) \
  93. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV64) \
  94. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV128) \
  95. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV256))
  96. #define IS_LL_SPI_BITORDER(__VALUE__) (((__VALUE__) == LL_SPI_LSB_FIRST) \
  97. || ((__VALUE__) == LL_SPI_MSB_FIRST))
  98. #define IS_LL_SPI_CRCCALCULATION(__VALUE__) (((__VALUE__) == LL_SPI_CRCCALCULATION_ENABLE) \
  99. || ((__VALUE__) == LL_SPI_CRCCALCULATION_DISABLE))
  100. #define IS_LL_SPI_CRC_POLYNOMIAL(__VALUE__) ((__VALUE__) >= 0x1U)
  101. /**
  102. * @}
  103. */
  104. /* Private function prototypes -----------------------------------------------*/
  105. /* Exported functions --------------------------------------------------------*/
  106. /** @addtogroup SPI_LL_Exported_Functions
  107. * @{
  108. */
  109. /** @addtogroup SPI_LL_EF_Init
  110. * @{
  111. */
  112. /**
  113. * @brief De-initialize the SPI registers to their default reset values.
  114. * @param SPIx SPI Instance
  115. * @retval An ErrorStatus enumeration value:
  116. * - SUCCESS: SPI registers are de-initialized
  117. * - ERROR: SPI registers are not de-initialized
  118. */
  119. ErrorStatus LL_SPI_DeInit(SPI_TypeDef *SPIx)
  120. {
  121. ErrorStatus status = ERROR;
  122. /* Check the parameters */
  123. assert_param(IS_SPI_ALL_INSTANCE(SPIx));
  124. #if defined(SPI1)
  125. if (SPIx == SPI1)
  126. {
  127. /* Force reset of SPI clock */
  128. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI1);
  129. /* Release reset of SPI clock */
  130. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_SPI1);
  131. status = SUCCESS;
  132. }
  133. #endif /* SPI1 */
  134. #if defined(SPI2)
  135. if (SPIx == SPI2)
  136. {
  137. /* Force reset of SPI clock */
  138. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_SPI2);
  139. /* Release reset of SPI clock */
  140. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_SPI2);
  141. status = SUCCESS;
  142. }
  143. #endif /* SPI2 */
  144. #if defined(SPI3)
  145. if (SPIx == SPI3)
  146. {
  147. /* Force reset of SPI clock */
  148. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_SPI3);
  149. /* Release reset of SPI clock */
  150. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_SPI3);
  151. status = SUCCESS;
  152. }
  153. #endif /* SPI3 */
  154. #if defined(SPI4)
  155. if (SPIx == SPI4)
  156. {
  157. /* Force reset of SPI clock */
  158. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI4);
  159. /* Release reset of SPI clock */
  160. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_SPI4);
  161. status = SUCCESS;
  162. }
  163. #endif /* SPI4 */
  164. #if defined(SPI5)
  165. if (SPIx == SPI5)
  166. {
  167. /* Force reset of SPI clock */
  168. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI5);
  169. /* Release reset of SPI clock */
  170. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_SPI5);
  171. status = SUCCESS;
  172. }
  173. #endif /* SPI5 */
  174. #if defined(SPI6)
  175. if (SPIx == SPI6)
  176. {
  177. /* Force reset of SPI clock */
  178. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI6);
  179. /* Release reset of SPI clock */
  180. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_SPI6);
  181. status = SUCCESS;
  182. }
  183. #endif /* SPI6 */
  184. return status;
  185. }
  186. /**
  187. * @brief Initialize the SPI registers according to the specified parameters in SPI_InitStruct.
  188. * @note As some bits in SPI configuration registers can only be written when the SPI is disabled (SPI_CR1_SPE bit =0),
  189. * SPI IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
  190. * @param SPIx SPI Instance
  191. * @param SPI_InitStruct pointer to a @ref LL_SPI_InitTypeDef structure
  192. * @retval An ErrorStatus enumeration value. (Return always SUCCESS)
  193. */
  194. ErrorStatus LL_SPI_Init(SPI_TypeDef *SPIx, LL_SPI_InitTypeDef *SPI_InitStruct)
  195. {
  196. ErrorStatus status = ERROR;
  197. /* Check the SPI Instance SPIx*/
  198. assert_param(IS_SPI_ALL_INSTANCE(SPIx));
  199. /* Check the SPI parameters from SPI_InitStruct*/
  200. assert_param(IS_LL_SPI_TRANSFER_DIRECTION(SPI_InitStruct->TransferDirection));
  201. assert_param(IS_LL_SPI_MODE(SPI_InitStruct->Mode));
  202. assert_param(IS_LL_SPI_DATAWIDTH(SPI_InitStruct->DataWidth));
  203. assert_param(IS_LL_SPI_POLARITY(SPI_InitStruct->ClockPolarity));
  204. assert_param(IS_LL_SPI_PHASE(SPI_InitStruct->ClockPhase));
  205. assert_param(IS_LL_SPI_NSS(SPI_InitStruct->NSS));
  206. assert_param(IS_LL_SPI_BAUDRATE(SPI_InitStruct->BaudRate));
  207. assert_param(IS_LL_SPI_BITORDER(SPI_InitStruct->BitOrder));
  208. assert_param(IS_LL_SPI_CRCCALCULATION(SPI_InitStruct->CRCCalculation));
  209. if (LL_SPI_IsEnabled(SPIx) == 0x00000000U)
  210. {
  211. /*---------------------------- SPIx CR1 Configuration ------------------------
  212. * Configure SPIx CR1 with parameters:
  213. * - TransferDirection: SPI_CR1_BIDIMODE, SPI_CR1_BIDIOE and SPI_CR1_RXONLY bits
  214. * - Master/Slave Mode: SPI_CR1_MSTR bit
  215. * - DataWidth: SPI_CR1_DFF bit
  216. * - ClockPolarity: SPI_CR1_CPOL bit
  217. * - ClockPhase: SPI_CR1_CPHA bit
  218. * - NSS management: SPI_CR1_SSM bit
  219. * - BaudRate prescaler: SPI_CR1_BR[2:0] bits
  220. * - BitOrder: SPI_CR1_LSBFIRST bit
  221. * - CRCCalculation: SPI_CR1_CRCEN bit
  222. */
  223. MODIFY_REG(SPIx->CR1,
  224. SPI_CR1_CLEAR_MASK,
  225. SPI_InitStruct->TransferDirection | SPI_InitStruct->Mode | SPI_InitStruct->DataWidth |
  226. SPI_InitStruct->ClockPolarity | SPI_InitStruct->ClockPhase |
  227. SPI_InitStruct->NSS | SPI_InitStruct->BaudRate |
  228. SPI_InitStruct->BitOrder | SPI_InitStruct->CRCCalculation);
  229. /*---------------------------- SPIx CR2 Configuration ------------------------
  230. * Configure SPIx CR2 with parameters:
  231. * - NSS management: SSOE bit
  232. */
  233. MODIFY_REG(SPIx->CR2, SPI_CR2_SSOE, (SPI_InitStruct->NSS >> 16U));
  234. /*---------------------------- SPIx CRCPR Configuration ----------------------
  235. * Configure SPIx CRCPR with parameters:
  236. * - CRCPoly: CRCPOLY[15:0] bits
  237. */
  238. if (SPI_InitStruct->CRCCalculation == LL_SPI_CRCCALCULATION_ENABLE)
  239. {
  240. assert_param(IS_LL_SPI_CRC_POLYNOMIAL(SPI_InitStruct->CRCPoly));
  241. LL_SPI_SetCRCPolynomial(SPIx, SPI_InitStruct->CRCPoly);
  242. }
  243. status = SUCCESS;
  244. }
  245. /* Activate the SPI mode (Reset I2SMOD bit in I2SCFGR register) */
  246. CLEAR_BIT(SPIx->I2SCFGR, SPI_I2SCFGR_I2SMOD);
  247. return status;
  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. /** @addtogroup I2S_LL
  279. * @{
  280. */
  281. /* Private types -------------------------------------------------------------*/
  282. /* Private variables ---------------------------------------------------------*/
  283. /* Private constants ---------------------------------------------------------*/
  284. /** @defgroup I2S_LL_Private_Constants I2S Private Constants
  285. * @{
  286. */
  287. /* I2S registers Masks */
  288. #define I2S_I2SCFGR_CLEAR_MASK (SPI_I2SCFGR_CHLEN | SPI_I2SCFGR_DATLEN | \
  289. SPI_I2SCFGR_CKPOL | SPI_I2SCFGR_I2SSTD | \
  290. SPI_I2SCFGR_I2SCFG | SPI_I2SCFGR_I2SMOD )
  291. #define I2S_I2SPR_CLEAR_MASK 0x0002U
  292. /**
  293. * @}
  294. */
  295. /* Private macros ------------------------------------------------------------*/
  296. /** @defgroup I2S_LL_Private_Macros I2S Private Macros
  297. * @{
  298. */
  299. #define IS_LL_I2S_DATAFORMAT(__VALUE__) (((__VALUE__) == LL_I2S_DATAFORMAT_16B) \
  300. || ((__VALUE__) == LL_I2S_DATAFORMAT_16B_EXTENDED) \
  301. || ((__VALUE__) == LL_I2S_DATAFORMAT_24B) \
  302. || ((__VALUE__) == LL_I2S_DATAFORMAT_32B))
  303. #define IS_LL_I2S_CPOL(__VALUE__) (((__VALUE__) == LL_I2S_POLARITY_LOW) \
  304. || ((__VALUE__) == LL_I2S_POLARITY_HIGH))
  305. #define IS_LL_I2S_STANDARD(__VALUE__) (((__VALUE__) == LL_I2S_STANDARD_PHILIPS) \
  306. || ((__VALUE__) == LL_I2S_STANDARD_MSB) \
  307. || ((__VALUE__) == LL_I2S_STANDARD_LSB) \
  308. || ((__VALUE__) == LL_I2S_STANDARD_PCM_SHORT) \
  309. || ((__VALUE__) == LL_I2S_STANDARD_PCM_LONG))
  310. #define IS_LL_I2S_MODE(__VALUE__) (((__VALUE__) == LL_I2S_MODE_SLAVE_TX) \
  311. || ((__VALUE__) == LL_I2S_MODE_SLAVE_RX) \
  312. || ((__VALUE__) == LL_I2S_MODE_MASTER_TX) \
  313. || ((__VALUE__) == LL_I2S_MODE_MASTER_RX))
  314. #define IS_LL_I2S_MCLK_OUTPUT(__VALUE__) (((__VALUE__) == LL_I2S_MCLK_OUTPUT_ENABLE) \
  315. || ((__VALUE__) == LL_I2S_MCLK_OUTPUT_DISABLE))
  316. #define IS_LL_I2S_AUDIO_FREQ(__VALUE__) ((((__VALUE__) >= LL_I2S_AUDIOFREQ_8K) \
  317. && ((__VALUE__) <= LL_I2S_AUDIOFREQ_192K)) \
  318. || ((__VALUE__) == LL_I2S_AUDIOFREQ_DEFAULT))
  319. #define IS_LL_I2S_PRESCALER_LINEAR(__VALUE__) ((__VALUE__) >= 0x2U)
  320. #define IS_LL_I2S_PRESCALER_PARITY(__VALUE__) (((__VALUE__) == LL_I2S_PRESCALER_PARITY_EVEN) \
  321. || ((__VALUE__) == LL_I2S_PRESCALER_PARITY_ODD))
  322. /**
  323. * @}
  324. */
  325. /* Private function prototypes -----------------------------------------------*/
  326. /* Exported functions --------------------------------------------------------*/
  327. /** @addtogroup I2S_LL_Exported_Functions
  328. * @{
  329. */
  330. /** @addtogroup I2S_LL_EF_Init
  331. * @{
  332. */
  333. /**
  334. * @brief De-initialize the SPI/I2S registers to their default reset values.
  335. * @param SPIx SPI Instance
  336. * @retval An ErrorStatus enumeration value:
  337. * - SUCCESS: SPI registers are de-initialized
  338. * - ERROR: SPI registers are not de-initialized
  339. */
  340. ErrorStatus LL_I2S_DeInit(SPI_TypeDef *SPIx)
  341. {
  342. return LL_SPI_DeInit(SPIx);
  343. }
  344. /**
  345. * @brief Initializes the SPI/I2S registers according to the specified parameters in I2S_InitStruct.
  346. * @note As some bits in SPI configuration registers can only be written when the SPI is disabled (SPI_CR1_SPE bit =0),
  347. * SPI IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
  348. * @param SPIx SPI Instance
  349. * @param I2S_InitStruct pointer to a @ref LL_I2S_InitTypeDef structure
  350. * @retval An ErrorStatus enumeration value:
  351. * - SUCCESS: SPI registers are Initialized
  352. * - ERROR: SPI registers are not Initialized
  353. */
  354. ErrorStatus LL_I2S_Init(SPI_TypeDef *SPIx, LL_I2S_InitTypeDef *I2S_InitStruct)
  355. {
  356. uint16_t i2sdiv = 2U, i2sodd = 0U, packetlength = 1U;
  357. uint32_t tmp = 0U;
  358. uint32_t sourceclock = 0U;
  359. ErrorStatus status = ERROR;
  360. /* Check the I2S parameters */
  361. assert_param(IS_I2S_ALL_INSTANCE(SPIx));
  362. assert_param(IS_LL_I2S_MODE(I2S_InitStruct->Mode));
  363. assert_param(IS_LL_I2S_STANDARD(I2S_InitStruct->Standard));
  364. assert_param(IS_LL_I2S_DATAFORMAT(I2S_InitStruct->DataFormat));
  365. assert_param(IS_LL_I2S_MCLK_OUTPUT(I2S_InitStruct->MCLKOutput));
  366. assert_param(IS_LL_I2S_AUDIO_FREQ(I2S_InitStruct->AudioFreq));
  367. assert_param(IS_LL_I2S_CPOL(I2S_InitStruct->ClockPolarity));
  368. if (LL_I2S_IsEnabled(SPIx) == 0x00000000U)
  369. {
  370. /*---------------------------- SPIx I2SCFGR Configuration --------------------
  371. * Configure SPIx I2SCFGR with parameters:
  372. * - Mode: SPI_I2SCFGR_I2SCFG[1:0] bit
  373. * - Standard: SPI_I2SCFGR_I2SSTD[1:0] and SPI_I2SCFGR_PCMSYNC bits
  374. * - DataFormat: SPI_I2SCFGR_CHLEN and SPI_I2SCFGR_DATLEN bits
  375. * - ClockPolarity: SPI_I2SCFGR_CKPOL bit
  376. */
  377. /* Write to SPIx I2SCFGR */
  378. MODIFY_REG(SPIx->I2SCFGR,
  379. I2S_I2SCFGR_CLEAR_MASK,
  380. I2S_InitStruct->Mode | I2S_InitStruct->Standard |
  381. I2S_InitStruct->DataFormat | I2S_InitStruct->ClockPolarity |
  382. SPI_I2SCFGR_I2SMOD);
  383. /*---------------------------- SPIx I2SPR Configuration ----------------------
  384. * Configure SPIx I2SPR with parameters:
  385. * - MCLKOutput: SPI_I2SPR_MCKOE bit
  386. * - AudioFreq: SPI_I2SPR_I2SDIV[7:0] and SPI_I2SPR_ODD bits
  387. */
  388. /* If the requested audio frequency is not the default, compute the prescaler (i2sodd, i2sdiv)
  389. * else, default values are used: i2sodd = 0U, i2sdiv = 2U.
  390. */
  391. if (I2S_InitStruct->AudioFreq != LL_I2S_AUDIOFREQ_DEFAULT)
  392. {
  393. /* Check the frame length (For the Prescaler computing)
  394. * Default value: LL_I2S_DATAFORMAT_16B (packetlength = 1U).
  395. */
  396. if (I2S_InitStruct->DataFormat != LL_I2S_DATAFORMAT_16B)
  397. {
  398. /* Packet length is 32 bits */
  399. packetlength = 2U;
  400. }
  401. /* If an external I2S clock has to be used, the specific define should be set
  402. in the project configuration or in the stm32f4xx_ll_rcc.h file */
  403. /* Get the I2S source clock value */
  404. sourceclock = LL_RCC_GetI2SClockFreq(LL_RCC_I2S1_CLKSOURCE);
  405. /* Compute the Real divider depending on the MCLK output state with a floating point */
  406. if (I2S_InitStruct->MCLKOutput == LL_I2S_MCLK_OUTPUT_ENABLE)
  407. {
  408. /* MCLK output is enabled */
  409. tmp = (uint16_t)(((((sourceclock / 256U) * 10U) / I2S_InitStruct->AudioFreq)) + 5U);
  410. }
  411. else
  412. {
  413. /* MCLK output is disabled */
  414. tmp = (uint16_t)(((((sourceclock / (32U * packetlength)) * 10U) / I2S_InitStruct->AudioFreq)) + 5U);
  415. }
  416. /* Remove the floating point */
  417. tmp = tmp / 10U;
  418. /* Check the parity of the divider */
  419. i2sodd = (uint16_t)(tmp & (uint16_t)0x0001U);
  420. /* Compute the i2sdiv prescaler */
  421. i2sdiv = (uint16_t)((tmp - i2sodd) / 2U);
  422. /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */
  423. i2sodd = (uint16_t)(i2sodd << 8U);
  424. }
  425. /* Test if the divider is 1 or 0 or greater than 0xFF */
  426. if ((i2sdiv < 2U) || (i2sdiv > 0xFFU))
  427. {
  428. /* Set the default values */
  429. i2sdiv = 2U;
  430. i2sodd = 0U;
  431. }
  432. /* Write to SPIx I2SPR register the computed value */
  433. WRITE_REG(SPIx->I2SPR, i2sdiv | i2sodd | I2S_InitStruct->MCLKOutput);
  434. status = SUCCESS;
  435. }
  436. return status;
  437. }
  438. /**
  439. * @brief Set each @ref LL_I2S_InitTypeDef field to default value.
  440. * @param I2S_InitStruct pointer to a @ref LL_I2S_InitTypeDef structure
  441. * whose fields will be set to default values.
  442. * @retval None
  443. */
  444. void LL_I2S_StructInit(LL_I2S_InitTypeDef *I2S_InitStruct)
  445. {
  446. /*--------------- Reset I2S init structure parameters values -----------------*/
  447. I2S_InitStruct->Mode = LL_I2S_MODE_SLAVE_TX;
  448. I2S_InitStruct->Standard = LL_I2S_STANDARD_PHILIPS;
  449. I2S_InitStruct->DataFormat = LL_I2S_DATAFORMAT_16B;
  450. I2S_InitStruct->MCLKOutput = LL_I2S_MCLK_OUTPUT_DISABLE;
  451. I2S_InitStruct->AudioFreq = LL_I2S_AUDIOFREQ_DEFAULT;
  452. I2S_InitStruct->ClockPolarity = LL_I2S_POLARITY_LOW;
  453. }
  454. /**
  455. * @brief Set linear and parity prescaler.
  456. * @note To calculate value of PrescalerLinear(I2SDIV[7:0] bits) and PrescalerParity(ODD bit)\n
  457. * Check Audio frequency table and formulas inside Reference Manual (SPI/I2S).
  458. * @param SPIx SPI Instance
  459. * @param PrescalerLinear value: Min_Data=0x02 and Max_Data=0xFF.
  460. * @param PrescalerParity This parameter can be one of the following values:
  461. * @arg @ref LL_I2S_PRESCALER_PARITY_EVEN
  462. * @arg @ref LL_I2S_PRESCALER_PARITY_ODD
  463. * @retval None
  464. */
  465. void LL_I2S_ConfigPrescaler(SPI_TypeDef *SPIx, uint32_t PrescalerLinear, uint32_t PrescalerParity)
  466. {
  467. /* Check the I2S parameters */
  468. assert_param(IS_I2S_ALL_INSTANCE(SPIx));
  469. assert_param(IS_LL_I2S_PRESCALER_LINEAR(PrescalerLinear));
  470. assert_param(IS_LL_I2S_PRESCALER_PARITY(PrescalerParity));
  471. /* Write to SPIx I2SPR */
  472. MODIFY_REG(SPIx->I2SPR, SPI_I2SPR_I2SDIV | SPI_I2SPR_ODD, PrescalerLinear | (PrescalerParity << 8U));
  473. }
  474. #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
  475. /**
  476. * @brief Configures the full duplex mode for the I2Sx peripheral using its extension
  477. * I2Sxext according to the specified parameters in the I2S_InitStruct.
  478. * @note The structure pointed by I2S_InitStruct parameter should be the same
  479. * used for the master I2S peripheral. In this case, if the master is
  480. * configured as transmitter, the slave will be receiver and vice versa.
  481. * Or you can force a different mode by modifying the field I2S_Mode to the
  482. * value I2S_SlaveRx or I2S_SlaveTx independently of the master configuration.
  483. * @param I2Sxext SPI Instance
  484. * @param I2S_InitStruct pointer to a @ref LL_I2S_InitTypeDef structure
  485. * @retval An ErrorStatus enumeration value:
  486. * - SUCCESS: I2Sxext registers are Initialized
  487. * - ERROR: I2Sxext registers are not Initialized
  488. */
  489. ErrorStatus LL_I2S_InitFullDuplex(SPI_TypeDef *I2Sxext, LL_I2S_InitTypeDef *I2S_InitStruct)
  490. {
  491. uint16_t mode = 0U;
  492. ErrorStatus status = ERROR;
  493. /* Check the I2S parameters */
  494. assert_param(IS_I2S_EXT_ALL_INSTANCE(I2Sxext));
  495. assert_param(IS_LL_I2S_MODE(I2S_InitStruct->Mode));
  496. assert_param(IS_LL_I2S_STANDARD(I2S_InitStruct->Standard));
  497. assert_param(IS_LL_I2S_DATAFORMAT(I2S_InitStruct->DataFormat));
  498. assert_param(IS_LL_I2S_CPOL(I2S_InitStruct->ClockPolarity));
  499. if (LL_I2S_IsEnabled(I2Sxext) == 0x00000000U)
  500. {
  501. /*---------------------------- SPIx I2SCFGR Configuration --------------------
  502. * Configure SPIx I2SCFGR with parameters:
  503. * - Mode: SPI_I2SCFGR_I2SCFG[1:0] bit
  504. * - Standard: SPI_I2SCFGR_I2SSTD[1:0] and SPI_I2SCFGR_PCMSYNC bits
  505. * - DataFormat: SPI_I2SCFGR_CHLEN and SPI_I2SCFGR_DATLEN bits
  506. * - ClockPolarity: SPI_I2SCFGR_CKPOL bit
  507. */
  508. /* Reset I2SPR registers */
  509. WRITE_REG(I2Sxext->I2SPR, I2S_I2SPR_CLEAR_MASK);
  510. /* Get the mode to be configured for the extended I2S */
  511. if ((I2S_InitStruct->Mode == LL_I2S_MODE_MASTER_TX) || (I2S_InitStruct->Mode == LL_I2S_MODE_SLAVE_TX))
  512. {
  513. mode = LL_I2S_MODE_SLAVE_RX;
  514. }
  515. else
  516. {
  517. if ((I2S_InitStruct->Mode == LL_I2S_MODE_MASTER_RX) || (I2S_InitStruct->Mode == LL_I2S_MODE_SLAVE_RX))
  518. {
  519. mode = LL_I2S_MODE_SLAVE_TX;
  520. }
  521. }
  522. /* Write to SPIx I2SCFGR */
  523. MODIFY_REG(I2Sxext->I2SCFGR,
  524. I2S_I2SCFGR_CLEAR_MASK,
  525. I2S_InitStruct->Standard |
  526. I2S_InitStruct->DataFormat | I2S_InitStruct->ClockPolarity |
  527. SPI_I2SCFGR_I2SMOD | mode);
  528. status = SUCCESS;
  529. }
  530. return status;
  531. }
  532. #endif /* SPI_I2S_FULLDUPLEX_SUPPORT */
  533. /**
  534. * @}
  535. */
  536. /**
  537. * @}
  538. */
  539. /**
  540. * @}
  541. */
  542. #endif /* defined (SPI1) || defined (SPI2) || defined (SPI3) || defined (SPI4) || defined (SPI5) || defined(SPI6) */
  543. /**
  544. * @}
  545. */
  546. #endif /* USE_FULL_LL_DRIVER */
  547. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/