stm32f4xx_hal_sai_ex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_sai_ex.c
  4. * @author MCD Application Team
  5. * @brief SAI Extension HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of SAI extension peripheral:
  8. * + Extension features functions
  9. *
  10. @verbatim
  11. ==============================================================================
  12. ##### SAI peripheral extension features #####
  13. ==============================================================================
  14. [..] Comparing to other previous devices, the SAI interface for STM32F446xx
  15. devices contains the following additional features :
  16. (+) Possibility to be clocked from PLLR
  17. ##### How to use this driver #####
  18. ==============================================================================
  19. [..] This driver provides functions to manage several sources to clock SAI
  20. @endverbatim
  21. ******************************************************************************
  22. * @attention
  23. *
  24. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  25. *
  26. * Redistribution and use in source and binary forms, with or without modification,
  27. * are permitted provided that the following conditions are met:
  28. * 1. Redistributions of source code must retain the above copyright notice,
  29. * this list of conditions and the following disclaimer.
  30. * 2. Redistributions in binary form must reproduce the above copyright notice,
  31. * this list of conditions and the following disclaimer in the documentation
  32. * and/or other materials provided with the distribution.
  33. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  34. * may be used to endorse or promote products derived from this software
  35. * without specific prior written permission.
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  38. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  39. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  40. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  41. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  42. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  43. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  44. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  46. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47. *
  48. ******************************************************************************
  49. */
  50. /* Includes ------------------------------------------------------------------*/
  51. #include "stm32f4xx_hal.h"
  52. /** @addtogroup STM32F4xx_HAL_Driver
  53. * @{
  54. */
  55. /** @defgroup SAIEx SAIEx
  56. * @brief SAI Extension HAL module driver
  57. * @{
  58. */
  59. #ifdef HAL_SAI_MODULE_ENABLED
  60. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || \
  61. defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F413xx) || \
  62. defined(STM32F423xx)
  63. /* Private typedef -----------------------------------------------------------*/
  64. /* Private define ------------------------------------------------------------*/
  65. /* SAI registers Masks */
  66. /* Private macro -------------------------------------------------------------*/
  67. /* Private variables ---------------------------------------------------------*/
  68. /* Private function prototypes -----------------------------------------------*/
  69. /* Private functions ---------------------------------------------------------*/
  70. /** @defgroup SAI_Private_Functions SAI Private Functions
  71. * @{
  72. */
  73. /**
  74. * @}
  75. */
  76. /* Exported functions --------------------------------------------------------*/
  77. /** @defgroup SAIEx_Exported_Functions SAI Extended Exported Functions
  78. * @{
  79. */
  80. /** @defgroup SAIEx_Exported_Functions_Group1 Extension features functions
  81. * @brief Extension features functions
  82. *
  83. @verbatim
  84. ===============================================================================
  85. ##### Extension features Functions #####
  86. ===============================================================================
  87. [..]
  88. This subsection provides a set of functions allowing to manage the possible
  89. SAI clock sources.
  90. @endverbatim
  91. * @{
  92. */
  93. /**
  94. * @brief Configure SAI Block synchronization mode
  95. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  96. * the configuration information for SAI module.
  97. * @retval SAI Clock Input
  98. */
  99. void SAI_BlockSynchroConfig(SAI_HandleTypeDef *hsai)
  100. {
  101. uint32_t tmpregisterGCR = 0U;
  102. #if defined(STM32F446xx)
  103. /* This setting must be done with both audio block (A & B) disabled */
  104. switch(hsai->Init.SynchroExt)
  105. {
  106. case SAI_SYNCEXT_DISABLE :
  107. tmpregisterGCR = 0U;
  108. break;
  109. case SAI_SYNCEXT_OUTBLOCKA_ENABLE :
  110. tmpregisterGCR = SAI_GCR_SYNCOUT_0;
  111. break;
  112. case SAI_SYNCEXT_OUTBLOCKB_ENABLE :
  113. tmpregisterGCR = SAI_GCR_SYNCOUT_1;
  114. break;
  115. default:
  116. break;
  117. }
  118. if((hsai->Init.Synchro) == SAI_SYNCHRONOUS_EXT_SAI2)
  119. {
  120. tmpregisterGCR |= SAI_GCR_SYNCIN_0;
  121. }
  122. if((hsai->Instance == SAI1_Block_A) || (hsai->Instance == SAI1_Block_B))
  123. {
  124. SAI1->GCR = tmpregisterGCR;
  125. }
  126. else
  127. {
  128. SAI2->GCR = tmpregisterGCR;
  129. }
  130. #endif /* STM32F446xx */
  131. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || \
  132. defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F413xx) || defined(STM32F423xx)
  133. /* This setting must be done with both audio block (A & B) disabled */
  134. switch(hsai->Init.SynchroExt)
  135. {
  136. case SAI_SYNCEXT_DISABLE :
  137. tmpregisterGCR = 0U;
  138. break;
  139. case SAI_SYNCEXT_OUTBLOCKA_ENABLE :
  140. tmpregisterGCR = SAI_GCR_SYNCOUT_0;
  141. break;
  142. case SAI_SYNCEXT_OUTBLOCKB_ENABLE :
  143. tmpregisterGCR = SAI_GCR_SYNCOUT_1;
  144. break;
  145. default:
  146. break;
  147. }
  148. SAI1->GCR = tmpregisterGCR;
  149. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx || STM32F413xx || STM32F423xx */
  150. }
  151. /**
  152. * @brief Get SAI Input Clock based on SAI source clock selection
  153. * @param hsai pointer to a SAI_HandleTypeDef structure that contains
  154. * the configuration information for SAI module.
  155. * @retval SAI Clock Input
  156. */
  157. uint32_t SAI_GetInputClock(SAI_HandleTypeDef *hsai)
  158. {
  159. /* This variable used to store the SAI_CK_x (value in Hz) */
  160. uint32_t saiclocksource = 0U;
  161. #if defined(STM32F446xx)
  162. if ((hsai->Instance == SAI1_Block_A) || (hsai->Instance == SAI1_Block_B))
  163. {
  164. saiclocksource = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SAI1);
  165. }
  166. else /* SAI2_Block_A || SAI2_Block_B*/
  167. {
  168. saiclocksource = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SAI2);
  169. }
  170. #endif /* STM32F446xx */
  171. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || \
  172. defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F413xx) || defined(STM32F423xx)
  173. uint32_t vcoinput = 0U, tmpreg = 0U;
  174. /* Check the SAI Block parameters */
  175. assert_param(IS_SAI_CLK_SOURCE(hsai->Init.ClockSource));
  176. /* SAI Block clock source selection */
  177. if(hsai->Instance == SAI1_Block_A)
  178. {
  179. __HAL_RCC_SAI_BLOCKACLKSOURCE_CONFIG(hsai->Init.ClockSource);
  180. }
  181. else
  182. {
  183. __HAL_RCC_SAI_BLOCKBCLKSOURCE_CONFIG((uint32_t)(hsai->Init.ClockSource << 2U));
  184. }
  185. /* VCO Input Clock value calculation */
  186. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSI)
  187. {
  188. /* In Case the PLL Source is HSI (Internal Clock) */
  189. vcoinput = (HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  190. }
  191. else
  192. {
  193. /* In Case the PLL Source is HSE (External Clock) */
  194. vcoinput = ((HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM)));
  195. }
  196. #if defined(STM32F413xx) || defined(STM32F423xx)
  197. /* SAI_CLK_x : SAI Block Clock configuration for different clock sources selected */
  198. if(hsai->Init.ClockSource == SAI_CLKSOURCE_PLLR)
  199. {
  200. /* Configure the PLLI2S division factor */
  201. /* PLL_VCO Input = PLL_SOURCE/PLLM */
  202. /* PLL_VCO Output = PLL_VCO Input * PLLN */
  203. /* SAI_CLK(first level) = PLL_VCO Output/PLLR */
  204. tmpreg = (RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 28U;
  205. saiclocksource = (vcoinput * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6U))/(tmpreg);
  206. /* SAI_CLK_x = SAI_CLK(first level)/PLLDIVR */
  207. tmpreg = (((RCC->DCKCFGR & RCC_DCKCFGR_PLLDIVR) >> 8U) + 1U);
  208. saiclocksource = saiclocksource/(tmpreg);
  209. }
  210. else if(hsai->Init.ClockSource == SAI_CLKSOURCE_PLLI2S)
  211. {
  212. /* Configure the PLLI2S division factor */
  213. /* PLLI2S_VCO Input = PLL_SOURCE/PLLM */
  214. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  215. /* SAI_CLK(first level) = PLLI2S_VCO Output/PLLI2SR */
  216. tmpreg = (RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> 28U;
  217. saiclocksource = (vcoinput * ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6U))/(tmpreg);
  218. /* SAI_CLK_x = SAI_CLK(first level)/PLLI2SDIVR */
  219. tmpreg = ((RCC->DCKCFGR & RCC_DCKCFGR_PLLI2SDIVR) + 1U);
  220. saiclocksource = saiclocksource/(tmpreg);
  221. }
  222. else if(hsai->Init.ClockSource == SAI_CLKSOURCE_HS)
  223. {
  224. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSE)
  225. {
  226. /* Get the I2S source clock value */
  227. saiclocksource = (uint32_t)(HSE_VALUE);
  228. }
  229. else
  230. {
  231. /* Get the I2S source clock value */
  232. saiclocksource = (uint32_t)(HSI_VALUE);
  233. }
  234. }
  235. else /* sConfig->ClockSource == SAI_CLKSource_Ext */
  236. {
  237. saiclocksource = EXTERNAL_CLOCK_VALUE;
  238. }
  239. #else
  240. /* SAI_CLK_x : SAI Block Clock configuration for different clock sources selected */
  241. if(hsai->Init.ClockSource == SAI_CLKSOURCE_PLLSAI)
  242. {
  243. /* Configure the PLLI2S division factor */
  244. /* PLLSAI_VCO Input = PLL_SOURCE/PLLM */
  245. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  246. /* SAI_CLK(first level) = PLLSAI_VCO Output/PLLSAIQ */
  247. tmpreg = (RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> 24U;
  248. saiclocksource = (vcoinput * ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIN) >> 6U))/(tmpreg);
  249. /* SAI_CLK_x = SAI_CLK(first level)/PLLSAIDIVQ */
  250. tmpreg = (((RCC->DCKCFGR & RCC_DCKCFGR_PLLSAIDIVQ) >> 8U) + 1U);
  251. saiclocksource = saiclocksource/(tmpreg);
  252. }
  253. else if(hsai->Init.ClockSource == SAI_CLKSOURCE_PLLI2S)
  254. {
  255. /* Configure the PLLI2S division factor */
  256. /* PLLI2S_VCO Input = PLL_SOURCE/PLLM */
  257. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  258. /* SAI_CLK(first level) = PLLI2S_VCO Output/PLLI2SQ */
  259. tmpreg = (RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> 24U;
  260. saiclocksource = (vcoinput * ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6U))/(tmpreg);
  261. /* SAI_CLK_x = SAI_CLK(first level)/PLLI2SDIVQ */
  262. tmpreg = ((RCC->DCKCFGR & RCC_DCKCFGR_PLLI2SDIVQ) + 1U);
  263. saiclocksource = saiclocksource/(tmpreg);
  264. }
  265. else /* sConfig->ClockSource == SAI_CLKSource_Ext */
  266. {
  267. /* Enable the External Clock selection */
  268. __HAL_RCC_I2S_CONFIG(RCC_I2SCLKSOURCE_EXT);
  269. saiclocksource = EXTERNAL_CLOCK_VALUE;
  270. }
  271. #endif /* STM32F413xx || STM32F423xx */
  272. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx || STM32F413xx || STM32F423xx */
  273. /* the return result is the value of SAI clock */
  274. return saiclocksource;
  275. }
  276. /**
  277. * @}
  278. */
  279. /**
  280. * @}
  281. */
  282. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx || STM32F479xx || STM32F413xx || STM32F423xx */
  283. #endif /* HAL_SAI_MODULE_ENABLED */
  284. /**
  285. * @}
  286. */
  287. /**
  288. * @}
  289. */
  290. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/