stm32f3xx_ll_spi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /**
  2. ******************************************************************************
  3. * @file stm32f3xx_ll_spi.c
  4. * @author MCD Application Team
  5. * @brief SPI LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2016 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. #if defined(USE_FULL_LL_DRIVER)
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "stm32f3xx_ll_spi.h"
  21. #include "stm32f3xx_ll_bus.h"
  22. #include "stm32f3xx_ll_rcc.h"
  23. #ifdef USE_FULL_ASSERT
  24. #include "stm32_assert.h"
  25. #else
  26. #define assert_param(expr) ((void)0U)
  27. #endif /* USE_FULL_ASSERT */
  28. /** @addtogroup STM32F3xx_LL_Driver
  29. * @{
  30. */
  31. #if defined (SPI1) || defined (SPI2) || defined (SPI3) || defined (SPI4)
  32. /** @addtogroup SPI_LL
  33. * @{
  34. */
  35. /* Private types -------------------------------------------------------------*/
  36. /* Private variables ---------------------------------------------------------*/
  37. /* Private constants ---------------------------------------------------------*/
  38. /** @defgroup SPI_LL_Private_Constants SPI Private Constants
  39. * @{
  40. */
  41. /* SPI registers Masks */
  42. #define SPI_CR1_CLEAR_MASK (SPI_CR1_CPHA | SPI_CR1_CPOL | SPI_CR1_MSTR | \
  43. SPI_CR1_BR | SPI_CR1_LSBFIRST | SPI_CR1_SSI | \
  44. SPI_CR1_SSM | SPI_CR1_RXONLY | SPI_CR1_CRCL | \
  45. SPI_CR1_CRCNEXT | SPI_CR1_CRCEN | SPI_CR1_BIDIOE | \
  46. SPI_CR1_BIDIMODE)
  47. /**
  48. * @}
  49. */
  50. /* Private macros ------------------------------------------------------------*/
  51. /** @defgroup SPI_LL_Private_Macros SPI Private Macros
  52. * @{
  53. */
  54. #define IS_LL_SPI_TRANSFER_DIRECTION(__VALUE__) (((__VALUE__) == LL_SPI_FULL_DUPLEX) \
  55. || ((__VALUE__) == LL_SPI_SIMPLEX_RX) \
  56. || ((__VALUE__) == LL_SPI_HALF_DUPLEX_RX) \
  57. || ((__VALUE__) == LL_SPI_HALF_DUPLEX_TX))
  58. #define IS_LL_SPI_MODE(__VALUE__) (((__VALUE__) == LL_SPI_MODE_MASTER) \
  59. || ((__VALUE__) == LL_SPI_MODE_SLAVE))
  60. #define IS_LL_SPI_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_SPI_DATAWIDTH_4BIT) \
  61. || ((__VALUE__) == LL_SPI_DATAWIDTH_5BIT) \
  62. || ((__VALUE__) == LL_SPI_DATAWIDTH_6BIT) \
  63. || ((__VALUE__) == LL_SPI_DATAWIDTH_7BIT) \
  64. || ((__VALUE__) == LL_SPI_DATAWIDTH_8BIT) \
  65. || ((__VALUE__) == LL_SPI_DATAWIDTH_9BIT) \
  66. || ((__VALUE__) == LL_SPI_DATAWIDTH_10BIT) \
  67. || ((__VALUE__) == LL_SPI_DATAWIDTH_11BIT) \
  68. || ((__VALUE__) == LL_SPI_DATAWIDTH_12BIT) \
  69. || ((__VALUE__) == LL_SPI_DATAWIDTH_13BIT) \
  70. || ((__VALUE__) == LL_SPI_DATAWIDTH_14BIT) \
  71. || ((__VALUE__) == LL_SPI_DATAWIDTH_15BIT) \
  72. || ((__VALUE__) == LL_SPI_DATAWIDTH_16BIT))
  73. #define IS_LL_SPI_POLARITY(__VALUE__) (((__VALUE__) == LL_SPI_POLARITY_LOW) \
  74. || ((__VALUE__) == LL_SPI_POLARITY_HIGH))
  75. #define IS_LL_SPI_PHASE(__VALUE__) (((__VALUE__) == LL_SPI_PHASE_1EDGE) \
  76. || ((__VALUE__) == LL_SPI_PHASE_2EDGE))
  77. #define IS_LL_SPI_NSS(__VALUE__) (((__VALUE__) == LL_SPI_NSS_SOFT) \
  78. || ((__VALUE__) == LL_SPI_NSS_HARD_INPUT) \
  79. || ((__VALUE__) == LL_SPI_NSS_HARD_OUTPUT))
  80. #define IS_LL_SPI_BAUDRATE(__VALUE__) (((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV2) \
  81. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV4) \
  82. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV8) \
  83. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV16) \
  84. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV32) \
  85. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV64) \
  86. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV128) \
  87. || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV256))
  88. #define IS_LL_SPI_BITORDER(__VALUE__) (((__VALUE__) == LL_SPI_LSB_FIRST) \
  89. || ((__VALUE__) == LL_SPI_MSB_FIRST))
  90. #define IS_LL_SPI_CRCCALCULATION(__VALUE__) (((__VALUE__) == LL_SPI_CRCCALCULATION_ENABLE) \
  91. || ((__VALUE__) == LL_SPI_CRCCALCULATION_DISABLE))
  92. #define IS_LL_SPI_CRC_POLYNOMIAL(__VALUE__) ((__VALUE__) >= 0x1U)
  93. /**
  94. * @}
  95. */
  96. /* Private function prototypes -----------------------------------------------*/
  97. /* Exported functions --------------------------------------------------------*/
  98. /** @addtogroup SPI_LL_Exported_Functions
  99. * @{
  100. */
  101. /** @addtogroup SPI_LL_EF_Init
  102. * @{
  103. */
  104. /**
  105. * @brief De-initialize the SPI registers to their default reset values.
  106. * @param SPIx SPI Instance
  107. * @retval An ErrorStatus enumeration value:
  108. * - SUCCESS: SPI registers are de-initialized
  109. * - ERROR: SPI registers are not de-initialized
  110. */
  111. ErrorStatus LL_SPI_DeInit(SPI_TypeDef *SPIx)
  112. {
  113. ErrorStatus status = ERROR;
  114. /* Check the parameters */
  115. assert_param(IS_SPI_ALL_INSTANCE(SPIx));
  116. #if defined(SPI1)
  117. if (SPIx == SPI1)
  118. {
  119. /* Force reset of SPI clock */
  120. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI1);
  121. /* Release reset of SPI clock */
  122. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_SPI1);
  123. status = SUCCESS;
  124. }
  125. #endif /* SPI1 */
  126. #if defined(SPI2)
  127. if (SPIx == SPI2)
  128. {
  129. /* Force reset of SPI clock */
  130. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_SPI2);
  131. /* Release reset of SPI clock */
  132. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_SPI2);
  133. status = SUCCESS;
  134. }
  135. #endif /* SPI2 */
  136. #if defined(SPI3)
  137. if (SPIx == SPI3)
  138. {
  139. /* Force reset of SPI clock */
  140. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_SPI3);
  141. /* Release reset of SPI clock */
  142. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_SPI3);
  143. status = SUCCESS;
  144. }
  145. #endif /* SPI3 */
  146. #if defined(SPI4)
  147. if (SPIx == SPI4)
  148. {
  149. /* Force reset of SPI clock */
  150. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI4);
  151. /* Release reset of SPI clock */
  152. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_SPI4);
  153. status = SUCCESS;
  154. }
  155. #endif /* SPI4 */
  156. return status;
  157. }
  158. /**
  159. * @brief Initialize the SPI registers according to the specified parameters in SPI_InitStruct.
  160. * @note As some bits in SPI configuration registers can only be written when the SPI is disabled (SPI_CR1_SPE bit =0),
  161. * SPI peripheral should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
  162. * @param SPIx SPI Instance
  163. * @param SPI_InitStruct pointer to a @ref LL_SPI_InitTypeDef structure
  164. * @retval An ErrorStatus enumeration value. (Return always SUCCESS)
  165. */
  166. ErrorStatus LL_SPI_Init(SPI_TypeDef *SPIx, LL_SPI_InitTypeDef *SPI_InitStruct)
  167. {
  168. ErrorStatus status = ERROR;
  169. /* Check the SPI Instance SPIx*/
  170. assert_param(IS_SPI_ALL_INSTANCE(SPIx));
  171. /* Check the SPI parameters from SPI_InitStruct*/
  172. assert_param(IS_LL_SPI_TRANSFER_DIRECTION(SPI_InitStruct->TransferDirection));
  173. assert_param(IS_LL_SPI_MODE(SPI_InitStruct->Mode));
  174. assert_param(IS_LL_SPI_DATAWIDTH(SPI_InitStruct->DataWidth));
  175. assert_param(IS_LL_SPI_POLARITY(SPI_InitStruct->ClockPolarity));
  176. assert_param(IS_LL_SPI_PHASE(SPI_InitStruct->ClockPhase));
  177. assert_param(IS_LL_SPI_NSS(SPI_InitStruct->NSS));
  178. assert_param(IS_LL_SPI_BAUDRATE(SPI_InitStruct->BaudRate));
  179. assert_param(IS_LL_SPI_BITORDER(SPI_InitStruct->BitOrder));
  180. assert_param(IS_LL_SPI_CRCCALCULATION(SPI_InitStruct->CRCCalculation));
  181. if (LL_SPI_IsEnabled(SPIx) == 0x00000000U)
  182. {
  183. /*---------------------------- SPIx CR1 Configuration ------------------------
  184. * Configure SPIx CR1 with parameters:
  185. * - TransferDirection: SPI_CR1_BIDIMODE, SPI_CR1_BIDIOE and SPI_CR1_RXONLY bits
  186. * - Master/Slave Mode: SPI_CR1_MSTR bit
  187. * - ClockPolarity: SPI_CR1_CPOL bit
  188. * - ClockPhase: SPI_CR1_CPHA bit
  189. * - NSS management: SPI_CR1_SSM bit
  190. * - BaudRate prescaler: SPI_CR1_BR[2:0] bits
  191. * - BitOrder: SPI_CR1_LSBFIRST bit
  192. * - CRCCalculation: SPI_CR1_CRCEN bit
  193. */
  194. MODIFY_REG(SPIx->CR1,
  195. SPI_CR1_CLEAR_MASK,
  196. SPI_InitStruct->TransferDirection | SPI_InitStruct->Mode |
  197. SPI_InitStruct->ClockPolarity | SPI_InitStruct->ClockPhase |
  198. SPI_InitStruct->NSS | SPI_InitStruct->BaudRate |
  199. SPI_InitStruct->BitOrder | SPI_InitStruct->CRCCalculation);
  200. /*---------------------------- SPIx CR2 Configuration ------------------------
  201. * Configure SPIx CR2 with parameters:
  202. * - DataWidth: DS[3:0] bits
  203. * - NSS management: SSOE bit
  204. */
  205. MODIFY_REG(SPIx->CR2,
  206. SPI_CR2_DS | SPI_CR2_SSOE,
  207. SPI_InitStruct->DataWidth | (SPI_InitStruct->NSS >> 16U));
  208. /* Set Rx FIFO to Quarter (1 Byte) in case of 8 Bits mode. No DataPacking by default */
  209. if (SPI_InitStruct->DataWidth < LL_SPI_DATAWIDTH_9BIT)
  210. {
  211. LL_SPI_SetRxFIFOThreshold(SPIx, LL_SPI_RX_FIFO_TH_QUARTER);
  212. }
  213. /*---------------------------- SPIx CRCPR Configuration ----------------------
  214. * Configure SPIx CRCPR with parameters:
  215. * - CRCPoly: CRCPOLY[15:0] bits
  216. */
  217. if (SPI_InitStruct->CRCCalculation == LL_SPI_CRCCALCULATION_ENABLE)
  218. {
  219. assert_param(IS_LL_SPI_CRC_POLYNOMIAL(SPI_InitStruct->CRCPoly));
  220. LL_SPI_SetCRCPolynomial(SPIx, SPI_InitStruct->CRCPoly);
  221. }
  222. status = SUCCESS;
  223. }
  224. #if defined (SPI_I2S_SUPPORT)
  225. /* Activate the SPI mode (Reset I2SMOD bit in I2SCFGR register) */
  226. CLEAR_BIT(SPIx->I2SCFGR, SPI_I2SCFGR_I2SMOD);
  227. #endif /* SPI_I2S_SUPPORT */
  228. return status;
  229. }
  230. /**
  231. * @brief Set each @ref LL_SPI_InitTypeDef field to default value.
  232. * @param SPI_InitStruct pointer to a @ref LL_SPI_InitTypeDef structure
  233. * whose fields will be set to default values.
  234. * @retval None
  235. */
  236. void LL_SPI_StructInit(LL_SPI_InitTypeDef *SPI_InitStruct)
  237. {
  238. /* Set SPI_InitStruct fields to default values */
  239. SPI_InitStruct->TransferDirection = LL_SPI_FULL_DUPLEX;
  240. SPI_InitStruct->Mode = LL_SPI_MODE_SLAVE;
  241. SPI_InitStruct->DataWidth = LL_SPI_DATAWIDTH_8BIT;
  242. SPI_InitStruct->ClockPolarity = LL_SPI_POLARITY_LOW;
  243. SPI_InitStruct->ClockPhase = LL_SPI_PHASE_1EDGE;
  244. SPI_InitStruct->NSS = LL_SPI_NSS_HARD_INPUT;
  245. SPI_InitStruct->BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV2;
  246. SPI_InitStruct->BitOrder = LL_SPI_MSB_FIRST;
  247. SPI_InitStruct->CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
  248. SPI_InitStruct->CRCPoly = 7U;
  249. }
  250. /**
  251. * @}
  252. */
  253. /**
  254. * @}
  255. */
  256. /**
  257. * @}
  258. */
  259. #if defined(SPI_I2S_SUPPORT)
  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. #if !defined (SPI_I2S_FULLDUPLEX_SUPPORT)
  343. LL_RCC_ClocksTypeDef rcc_clocks;
  344. #endif /* SPI_I2S_FULLDUPLEX_SUPPORT */
  345. uint32_t sourceclock;
  346. ErrorStatus status = ERROR;
  347. /* Check the I2S parameters */
  348. assert_param(IS_I2S_ALL_INSTANCE(SPIx));
  349. assert_param(IS_LL_I2S_MODE(I2S_InitStruct->Mode));
  350. assert_param(IS_LL_I2S_STANDARD(I2S_InitStruct->Standard));
  351. assert_param(IS_LL_I2S_DATAFORMAT(I2S_InitStruct->DataFormat));
  352. assert_param(IS_LL_I2S_MCLK_OUTPUT(I2S_InitStruct->MCLKOutput));
  353. assert_param(IS_LL_I2S_AUDIO_FREQ(I2S_InitStruct->AudioFreq));
  354. assert_param(IS_LL_I2S_CPOL(I2S_InitStruct->ClockPolarity));
  355. if (LL_I2S_IsEnabled(SPIx) == 0x00000000U)
  356. {
  357. /*---------------------------- SPIx I2SCFGR Configuration --------------------
  358. * Configure SPIx I2SCFGR with parameters:
  359. * - Mode: SPI_I2SCFGR_I2SCFG[1:0] bit
  360. * - Standard: SPI_I2SCFGR_I2SSTD[1:0] and SPI_I2SCFGR_PCMSYNC bits
  361. * - DataFormat: SPI_I2SCFGR_CHLEN and SPI_I2SCFGR_DATLEN bits
  362. * - ClockPolarity: SPI_I2SCFGR_CKPOL bit
  363. */
  364. /* Write to SPIx I2SCFGR */
  365. MODIFY_REG(SPIx->I2SCFGR,
  366. I2S_I2SCFGR_CLEAR_MASK,
  367. I2S_InitStruct->Mode | I2S_InitStruct->Standard |
  368. I2S_InitStruct->DataFormat | I2S_InitStruct->ClockPolarity |
  369. SPI_I2SCFGR_I2SMOD);
  370. /*---------------------------- SPIx I2SPR Configuration ----------------------
  371. * Configure SPIx I2SPR with parameters:
  372. * - MCLKOutput: SPI_I2SPR_MCKOE bit
  373. * - AudioFreq: SPI_I2SPR_I2SDIV[7:0] and SPI_I2SPR_ODD bits
  374. */
  375. /* If the requested audio frequency is not the default, compute the prescaler (i2sodd, i2sdiv)
  376. * else, default values are used: i2sodd = 0U, i2sdiv = 2U.
  377. */
  378. if (I2S_InitStruct->AudioFreq != LL_I2S_AUDIOFREQ_DEFAULT)
  379. {
  380. /* Check the frame length (For the Prescaler computing)
  381. * Default value: LL_I2S_DATAFORMAT_16B (packetlength = 1U).
  382. */
  383. if (I2S_InitStruct->DataFormat != LL_I2S_DATAFORMAT_16B)
  384. {
  385. /* Packet length is 32 bits */
  386. packetlength = 2U;
  387. }
  388. #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
  389. /* If an external I2S clock has to be used, the specific define should be set
  390. in the project configuration or in the stm32f3xx_ll_rcc.h file */
  391. /* Get the I2S source clock value */
  392. sourceclock = LL_RCC_GetI2SClockFreq(LL_RCC_I2S_CLKSOURCE);
  393. #else /* Case for STM32F373xC and STM32F378xx series */
  394. /* I2S Clock source is System clock: Get System Clock frequency */
  395. LL_RCC_GetSystemClocksFreq(&rcc_clocks);
  396. if (SPIx == SPI1)
  397. {
  398. sourceclock = rcc_clocks.PCLK2_Frequency;
  399. }
  400. else /* SPI2 or SPI3 */
  401. {
  402. sourceclock = rcc_clocks.PCLK1_Frequency;
  403. }
  404. #endif /* SPI_I2S_FULLDUPLEX_SUPPORT */
  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 = (((((sourceclock / 256U) * 10U) / I2S_InitStruct->AudioFreq)) + 5U);
  410. }
  411. else
  412. {
  413. /* MCLK output is disabled */
  414. tmp = (((((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 = (tmp & (uint16_t)0x0001U);
  420. /* Compute the i2sdiv prescaler */
  421. i2sdiv = ((tmp - i2sodd) / 2U);
  422. /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */
  423. i2sodd = (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. uint32_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 /* SPI_I2S_SUPPORT */
  543. #endif /* defined (SPI1) || defined (SPI2) || defined (SPI3) || defined (SPI4) */
  544. /**
  545. * @}
  546. */
  547. #endif /* USE_FULL_LL_DRIVER */