1
0

stm32f1xx_ll_spi.c 21 KB

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