stm32g0xx_ll_spi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_ll_spi.c
  4. * @author MCD Application Team
  5. * @brief SPI LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2018 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 "stm32g0xx_ll_spi.h"
  21. #include "stm32g0xx_ll_bus.h"
  22. #include "stm32g0xx_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 STM32G0xx_LL_Driver
  29. * @{
  30. */
  31. #if defined (SPI1) || defined (SPI2) || defined (SPI3)
  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. return status;
  147. }
  148. /**
  149. * @brief Initialize the SPI registers according to the specified parameters in SPI_InitStruct.
  150. * @note As some bits in SPI configuration registers can only be written when the SPI is disabled (SPI_CR1_SPE bit =0),
  151. * SPI peripheral should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
  152. * @param SPIx SPI Instance
  153. * @param SPI_InitStruct pointer to a @ref LL_SPI_InitTypeDef structure
  154. * @retval An ErrorStatus enumeration value. (Return always SUCCESS)
  155. */
  156. ErrorStatus LL_SPI_Init(SPI_TypeDef *SPIx, LL_SPI_InitTypeDef *SPI_InitStruct)
  157. {
  158. ErrorStatus status = ERROR;
  159. /* Check the SPI Instance SPIx*/
  160. assert_param(IS_SPI_ALL_INSTANCE(SPIx));
  161. /* Check the SPI parameters from SPI_InitStruct*/
  162. assert_param(IS_LL_SPI_TRANSFER_DIRECTION(SPI_InitStruct->TransferDirection));
  163. assert_param(IS_LL_SPI_MODE(SPI_InitStruct->Mode));
  164. assert_param(IS_LL_SPI_DATAWIDTH(SPI_InitStruct->DataWidth));
  165. assert_param(IS_LL_SPI_POLARITY(SPI_InitStruct->ClockPolarity));
  166. assert_param(IS_LL_SPI_PHASE(SPI_InitStruct->ClockPhase));
  167. assert_param(IS_LL_SPI_NSS(SPI_InitStruct->NSS));
  168. assert_param(IS_LL_SPI_BAUDRATE(SPI_InitStruct->BaudRate));
  169. assert_param(IS_LL_SPI_BITORDER(SPI_InitStruct->BitOrder));
  170. assert_param(IS_LL_SPI_CRCCALCULATION(SPI_InitStruct->CRCCalculation));
  171. if (LL_SPI_IsEnabled(SPIx) == 0x00000000U)
  172. {
  173. /*---------------------------- SPIx CR1 Configuration ------------------------
  174. * Configure SPIx CR1 with parameters:
  175. * - TransferDirection: SPI_CR1_BIDIMODE, SPI_CR1_BIDIOE and SPI_CR1_RXONLY bits
  176. * - Master/Slave Mode: SPI_CR1_MSTR bit
  177. * - ClockPolarity: SPI_CR1_CPOL bit
  178. * - ClockPhase: SPI_CR1_CPHA bit
  179. * - NSS management: SPI_CR1_SSM bit
  180. * - BaudRate prescaler: SPI_CR1_BR[2:0] bits
  181. * - BitOrder: SPI_CR1_LSBFIRST bit
  182. * - CRCCalculation: SPI_CR1_CRCEN bit
  183. */
  184. MODIFY_REG(SPIx->CR1,
  185. SPI_CR1_CLEAR_MASK,
  186. SPI_InitStruct->TransferDirection | SPI_InitStruct->Mode |
  187. SPI_InitStruct->ClockPolarity | SPI_InitStruct->ClockPhase |
  188. SPI_InitStruct->NSS | SPI_InitStruct->BaudRate |
  189. SPI_InitStruct->BitOrder | SPI_InitStruct->CRCCalculation);
  190. /*---------------------------- SPIx CR2 Configuration ------------------------
  191. * Configure SPIx CR2 with parameters:
  192. * - DataWidth: DS[3:0] bits
  193. * - NSS management: SSOE bit
  194. */
  195. MODIFY_REG(SPIx->CR2,
  196. SPI_CR2_DS | SPI_CR2_SSOE,
  197. SPI_InitStruct->DataWidth | (SPI_InitStruct->NSS >> 16U));
  198. /* Set Rx FIFO to Quarter (1 Byte) in case of 8 Bits mode. No DataPacking by default */
  199. if (SPI_InitStruct->DataWidth < LL_SPI_DATAWIDTH_9BIT)
  200. {
  201. LL_SPI_SetRxFIFOThreshold(SPIx, LL_SPI_RX_FIFO_TH_QUARTER);
  202. }
  203. /*---------------------------- SPIx CRCPR Configuration ----------------------
  204. * Configure SPIx CRCPR with parameters:
  205. * - CRCPoly: CRCPOLY[15:0] bits
  206. */
  207. if (SPI_InitStruct->CRCCalculation == LL_SPI_CRCCALCULATION_ENABLE)
  208. {
  209. assert_param(IS_LL_SPI_CRC_POLYNOMIAL(SPI_InitStruct->CRCPoly));
  210. LL_SPI_SetCRCPolynomial(SPIx, SPI_InitStruct->CRCPoly);
  211. }
  212. status = SUCCESS;
  213. }
  214. #if defined (SPI_I2S_SUPPORT)
  215. /* Activate the SPI mode (Reset I2SMOD bit in I2SCFGR register) */
  216. CLEAR_BIT(SPIx->I2SCFGR, SPI_I2SCFGR_I2SMOD);
  217. #endif /* SPI_I2S_SUPPORT */
  218. return status;
  219. }
  220. /**
  221. * @brief Set each @ref LL_SPI_InitTypeDef field to default value.
  222. * @param SPI_InitStruct pointer to a @ref LL_SPI_InitTypeDef structure
  223. * whose fields will be set to default values.
  224. * @retval None
  225. */
  226. void LL_SPI_StructInit(LL_SPI_InitTypeDef *SPI_InitStruct)
  227. {
  228. /* Set SPI_InitStruct fields to default values */
  229. SPI_InitStruct->TransferDirection = LL_SPI_FULL_DUPLEX;
  230. SPI_InitStruct->Mode = LL_SPI_MODE_SLAVE;
  231. SPI_InitStruct->DataWidth = LL_SPI_DATAWIDTH_8BIT;
  232. SPI_InitStruct->ClockPolarity = LL_SPI_POLARITY_LOW;
  233. SPI_InitStruct->ClockPhase = LL_SPI_PHASE_1EDGE;
  234. SPI_InitStruct->NSS = LL_SPI_NSS_HARD_INPUT;
  235. SPI_InitStruct->BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV2;
  236. SPI_InitStruct->BitOrder = LL_SPI_MSB_FIRST;
  237. SPI_InitStruct->CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
  238. SPI_InitStruct->CRCPoly = 7U;
  239. }
  240. /**
  241. * @}
  242. */
  243. /**
  244. * @}
  245. */
  246. /**
  247. * @}
  248. */
  249. #if defined(SPI_I2S_SUPPORT)
  250. /** @addtogroup I2S_LL
  251. * @{
  252. */
  253. /* Private types -------------------------------------------------------------*/
  254. /* Private variables ---------------------------------------------------------*/
  255. /* Private constants ---------------------------------------------------------*/
  256. /** @defgroup I2S_LL_Private_Constants I2S Private Constants
  257. * @{
  258. */
  259. /* I2S registers Masks */
  260. #define I2S_I2SCFGR_CLEAR_MASK (SPI_I2SCFGR_CHLEN | SPI_I2SCFGR_DATLEN | \
  261. SPI_I2SCFGR_CKPOL | SPI_I2SCFGR_I2SSTD | \
  262. SPI_I2SCFGR_I2SCFG | SPI_I2SCFGR_I2SMOD )
  263. #define I2S_I2SPR_CLEAR_MASK 0x0002U
  264. /**
  265. * @}
  266. */
  267. /* Private macros ------------------------------------------------------------*/
  268. /** @defgroup I2S_LL_Private_Macros I2S Private Macros
  269. * @{
  270. */
  271. #define IS_LL_I2S_DATAFORMAT(__VALUE__) (((__VALUE__) == LL_I2S_DATAFORMAT_16B) \
  272. || ((__VALUE__) == LL_I2S_DATAFORMAT_16B_EXTENDED) \
  273. || ((__VALUE__) == LL_I2S_DATAFORMAT_24B) \
  274. || ((__VALUE__) == LL_I2S_DATAFORMAT_32B))
  275. #define IS_LL_I2S_CPOL(__VALUE__) (((__VALUE__) == LL_I2S_POLARITY_LOW) \
  276. || ((__VALUE__) == LL_I2S_POLARITY_HIGH))
  277. #define IS_LL_I2S_STANDARD(__VALUE__) (((__VALUE__) == LL_I2S_STANDARD_PHILIPS) \
  278. || ((__VALUE__) == LL_I2S_STANDARD_MSB) \
  279. || ((__VALUE__) == LL_I2S_STANDARD_LSB) \
  280. || ((__VALUE__) == LL_I2S_STANDARD_PCM_SHORT) \
  281. || ((__VALUE__) == LL_I2S_STANDARD_PCM_LONG))
  282. #define IS_LL_I2S_MODE(__VALUE__) (((__VALUE__) == LL_I2S_MODE_SLAVE_TX) \
  283. || ((__VALUE__) == LL_I2S_MODE_SLAVE_RX) \
  284. || ((__VALUE__) == LL_I2S_MODE_MASTER_TX) \
  285. || ((__VALUE__) == LL_I2S_MODE_MASTER_RX))
  286. #define IS_LL_I2S_MCLK_OUTPUT(__VALUE__) (((__VALUE__) == LL_I2S_MCLK_OUTPUT_ENABLE) \
  287. || ((__VALUE__) == LL_I2S_MCLK_OUTPUT_DISABLE))
  288. #define IS_LL_I2S_AUDIO_FREQ(__VALUE__) ((((__VALUE__) >= LL_I2S_AUDIOFREQ_8K) \
  289. && ((__VALUE__) <= LL_I2S_AUDIOFREQ_192K)) \
  290. || ((__VALUE__) == LL_I2S_AUDIOFREQ_DEFAULT))
  291. #define IS_LL_I2S_PRESCALER_LINEAR(__VALUE__) ((__VALUE__) >= 0x2U)
  292. #define IS_LL_I2S_PRESCALER_PARITY(__VALUE__) (((__VALUE__) == LL_I2S_PRESCALER_PARITY_EVEN) \
  293. || ((__VALUE__) == LL_I2S_PRESCALER_PARITY_ODD))
  294. /**
  295. * @}
  296. */
  297. /* Private function prototypes -----------------------------------------------*/
  298. /* Exported functions --------------------------------------------------------*/
  299. /** @addtogroup I2S_LL_Exported_Functions
  300. * @{
  301. */
  302. /** @addtogroup I2S_LL_EF_Init
  303. * @{
  304. */
  305. /**
  306. * @brief De-initialize the SPI/I2S registers to their default reset values.
  307. * @param SPIx SPI Instance
  308. * @retval An ErrorStatus enumeration value:
  309. * - SUCCESS: SPI registers are de-initialized
  310. * - ERROR: SPI registers are not de-initialized
  311. */
  312. ErrorStatus LL_I2S_DeInit(SPI_TypeDef *SPIx)
  313. {
  314. return LL_SPI_DeInit(SPIx);
  315. }
  316. /**
  317. * @brief Initializes the SPI/I2S registers according to the specified parameters in I2S_InitStruct.
  318. * @note As some bits in SPI configuration registers can only be written when the SPI is disabled (SPI_CR1_SPE bit =0),
  319. * SPI peripheral should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
  320. * @param SPIx SPI Instance
  321. * @param I2S_InitStruct pointer to a @ref LL_I2S_InitTypeDef structure
  322. * @retval An ErrorStatus enumeration value:
  323. * - SUCCESS: SPI registers are Initialized
  324. * - ERROR: SPI registers are not Initialized
  325. */
  326. ErrorStatus LL_I2S_Init(SPI_TypeDef *SPIx, LL_I2S_InitTypeDef *I2S_InitStruct)
  327. {
  328. uint32_t i2sdiv = 2U;
  329. uint32_t i2sodd = 0U;
  330. uint32_t packetlength = 1U;
  331. uint32_t tmp;
  332. LL_RCC_ClocksTypeDef rcc_clocks;
  333. uint32_t sourceclock;
  334. ErrorStatus status = ERROR;
  335. /* Check the I2S parameters */
  336. assert_param(IS_I2S_ALL_INSTANCE(SPIx));
  337. assert_param(IS_LL_I2S_MODE(I2S_InitStruct->Mode));
  338. assert_param(IS_LL_I2S_STANDARD(I2S_InitStruct->Standard));
  339. assert_param(IS_LL_I2S_DATAFORMAT(I2S_InitStruct->DataFormat));
  340. assert_param(IS_LL_I2S_MCLK_OUTPUT(I2S_InitStruct->MCLKOutput));
  341. assert_param(IS_LL_I2S_AUDIO_FREQ(I2S_InitStruct->AudioFreq));
  342. assert_param(IS_LL_I2S_CPOL(I2S_InitStruct->ClockPolarity));
  343. if (LL_I2S_IsEnabled(SPIx) == 0x00000000U)
  344. {
  345. /*---------------------------- SPIx I2SCFGR Configuration --------------------
  346. * Configure SPIx I2SCFGR with parameters:
  347. * - Mode: SPI_I2SCFGR_I2SCFG[1:0] bit
  348. * - Standard: SPI_I2SCFGR_I2SSTD[1:0] and SPI_I2SCFGR_PCMSYNC bits
  349. * - DataFormat: SPI_I2SCFGR_CHLEN and SPI_I2SCFGR_DATLEN bits
  350. * - ClockPolarity: SPI_I2SCFGR_CKPOL bit
  351. */
  352. /* Write to SPIx I2SCFGR */
  353. MODIFY_REG(SPIx->I2SCFGR,
  354. I2S_I2SCFGR_CLEAR_MASK,
  355. I2S_InitStruct->Mode | I2S_InitStruct->Standard |
  356. I2S_InitStruct->DataFormat | I2S_InitStruct->ClockPolarity |
  357. SPI_I2SCFGR_I2SMOD);
  358. /*---------------------------- SPIx I2SPR Configuration ----------------------
  359. * Configure SPIx I2SPR with parameters:
  360. * - MCLKOutput: SPI_I2SPR_MCKOE bit
  361. * - AudioFreq: SPI_I2SPR_I2SDIV[7:0] and SPI_I2SPR_ODD bits
  362. */
  363. /* If the requested audio frequency is not the default, compute the prescaler (i2sodd, i2sdiv)
  364. * else, default values are used: i2sodd = 0U, i2sdiv = 2U.
  365. */
  366. if (I2S_InitStruct->AudioFreq != LL_I2S_AUDIOFREQ_DEFAULT)
  367. {
  368. /* Check the frame length (For the Prescaler computing)
  369. * Default value: LL_I2S_DATAFORMAT_16B (packetlength = 1U).
  370. */
  371. if (I2S_InitStruct->DataFormat != LL_I2S_DATAFORMAT_16B)
  372. {
  373. /* Packet length is 32 bits */
  374. packetlength = 2U;
  375. }
  376. /* I2S Clock source is System clock: Get System Clock frequency */
  377. LL_RCC_GetSystemClocksFreq(&rcc_clocks);
  378. /* Get the source clock value: based on System Clock value */
  379. sourceclock = rcc_clocks.SYSCLK_Frequency;
  380. /* Compute the Real divider depending on the MCLK output state with a floating point */
  381. if (I2S_InitStruct->MCLKOutput == LL_I2S_MCLK_OUTPUT_ENABLE)
  382. {
  383. /* MCLK output is enabled */
  384. tmp = (((((sourceclock / 256U) * 10U) / I2S_InitStruct->AudioFreq)) + 5U);
  385. }
  386. else
  387. {
  388. /* MCLK output is disabled */
  389. tmp = (((((sourceclock / (32U * packetlength)) * 10U) / I2S_InitStruct->AudioFreq)) + 5U);
  390. }
  391. /* Remove the floating point */
  392. tmp = tmp / 10U;
  393. /* Check the parity of the divider */
  394. i2sodd = (tmp & (uint16_t)0x0001U);
  395. /* Compute the i2sdiv prescaler */
  396. i2sdiv = ((tmp - i2sodd) / 2U);
  397. /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */
  398. i2sodd = (i2sodd << 8U);
  399. }
  400. /* Test if the divider is 1 or 0 or greater than 0xFF */
  401. if ((i2sdiv < 2U) || (i2sdiv > 0xFFU))
  402. {
  403. /* Set the default values */
  404. i2sdiv = 2U;
  405. i2sodd = 0U;
  406. }
  407. /* Write to SPIx I2SPR register the computed value */
  408. WRITE_REG(SPIx->I2SPR, i2sdiv | i2sodd | I2S_InitStruct->MCLKOutput);
  409. status = SUCCESS;
  410. }
  411. return status;
  412. }
  413. /**
  414. * @brief Set each @ref LL_I2S_InitTypeDef field to default value.
  415. * @param I2S_InitStruct pointer to a @ref LL_I2S_InitTypeDef structure
  416. * whose fields will be set to default values.
  417. * @retval None
  418. */
  419. void LL_I2S_StructInit(LL_I2S_InitTypeDef *I2S_InitStruct)
  420. {
  421. /*--------------- Reset I2S init structure parameters values -----------------*/
  422. I2S_InitStruct->Mode = LL_I2S_MODE_SLAVE_TX;
  423. I2S_InitStruct->Standard = LL_I2S_STANDARD_PHILIPS;
  424. I2S_InitStruct->DataFormat = LL_I2S_DATAFORMAT_16B;
  425. I2S_InitStruct->MCLKOutput = LL_I2S_MCLK_OUTPUT_DISABLE;
  426. I2S_InitStruct->AudioFreq = LL_I2S_AUDIOFREQ_DEFAULT;
  427. I2S_InitStruct->ClockPolarity = LL_I2S_POLARITY_LOW;
  428. }
  429. /**
  430. * @brief Set linear and parity prescaler.
  431. * @note To calculate value of PrescalerLinear(I2SDIV[7:0] bits) and PrescalerParity(ODD bit)\n
  432. * Check Audio frequency table and formulas inside Reference Manual (SPI/I2S).
  433. * @param SPIx SPI Instance
  434. * @param PrescalerLinear value Min_Data=0x02 and Max_Data=0xFF.
  435. * @param PrescalerParity This parameter can be one of the following values:
  436. * @arg @ref LL_I2S_PRESCALER_PARITY_EVEN
  437. * @arg @ref LL_I2S_PRESCALER_PARITY_ODD
  438. * @retval None
  439. */
  440. void LL_I2S_ConfigPrescaler(SPI_TypeDef *SPIx, uint32_t PrescalerLinear, uint32_t PrescalerParity)
  441. {
  442. /* Check the I2S parameters */
  443. assert_param(IS_I2S_ALL_INSTANCE(SPIx));
  444. assert_param(IS_LL_I2S_PRESCALER_LINEAR(PrescalerLinear));
  445. assert_param(IS_LL_I2S_PRESCALER_PARITY(PrescalerParity));
  446. /* Write to SPIx I2SPR */
  447. MODIFY_REG(SPIx->I2SPR, SPI_I2SPR_I2SDIV | SPI_I2SPR_ODD, PrescalerLinear | (PrescalerParity << 8U));
  448. }
  449. /**
  450. * @}
  451. */
  452. /**
  453. * @}
  454. */
  455. /**
  456. * @}
  457. */
  458. #endif /* SPI_I2S_SUPPORT */
  459. #endif /* defined (SPI1) || defined (SPI2) || defined (SPI3) */
  460. /**
  461. * @}
  462. */
  463. #endif /* USE_FULL_LL_DRIVER */