stm32g0xx_ll_spi.c 21 KB

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