stm32f4xx_ll_spi.c 23 KB

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