stm32f3xx_ll_spi.c 25 KB

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