stm32f4xx_hal_sai_ex.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_sai_ex.c
  4. * @author MCD Application Team
  5. * @version V1.3.0
  6. * @date 09-March-2015
  7. * @brief SAI Extension HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of SAI extension peripheral:
  10. * + Extension features functions
  11. *
  12. @verbatim
  13. ==============================================================================
  14. ##### SAI peripheral extension features #####
  15. ==============================================================================
  16. [..] Comparing to other previous devices, the SAI interface for STM32F446xx
  17. devices contains the following additional features :
  18. (+) Possibility to be clocked from PLLR
  19. ##### How to use this driver #####
  20. ==============================================================================
  21. [..] This driver provides functions to manage several sources to clock SAI
  22. @endverbatim
  23. ******************************************************************************
  24. * @attention
  25. *
  26. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  27. *
  28. * Redistribution and use in source and binary forms, with or without modification,
  29. * are permitted provided that the following conditions are met:
  30. * 1. Redistributions of source code must retain the above copyright notice,
  31. * this list of conditions and the following disclaimer.
  32. * 2. Redistributions in binary form must reproduce the above copyright notice,
  33. * this list of conditions and the following disclaimer in the documentation
  34. * and/or other materials provided with the distribution.
  35. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  36. * may be used to endorse or promote products derived from this software
  37. * without specific prior written permission.
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  40. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  42. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  43. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  44. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  45. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  46. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  47. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  48. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49. *
  50. ******************************************************************************
  51. */
  52. /* Includes ------------------------------------------------------------------*/
  53. #include "stm32f4xx_hal.h"
  54. /** @addtogroup STM32F4xx_HAL_Driver
  55. * @{
  56. */
  57. /** @defgroup SAIEx SAIEx
  58. * @brief SAI Extension HAL module driver
  59. * @{
  60. */
  61. #ifdef HAL_SAI_MODULE_ENABLED
  62. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F446xx)
  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 = 0;
  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 = 0;
  108. break;
  109. case SAI_SYNCEXT_IN_ENABLE :
  110. tmpregisterGCR = SAI_GCR_SYNCIN_0;
  111. break;
  112. case SAI_SYNCEXT_OUTBLOCKA_ENABLE :
  113. tmpregisterGCR = SAI_GCR_SYNCOUT_0;
  114. break;
  115. case SAI_SYNCEXT_OUTBLOCKB_ENABLE :
  116. tmpregisterGCR = SAI_GCR_SYNCOUT_1;
  117. break;
  118. }
  119. if((hsai->Instance == SAI1_Block_A) || (hsai->Instance == SAI1_Block_B))
  120. {
  121. SAI1->GCR = tmpregisterGCR;
  122. }
  123. else
  124. {
  125. SAI2->GCR = tmpregisterGCR;
  126. }
  127. #endif /* STM32F446xx */
  128. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
  129. /* This setting must be done with both audio block (A & B) disabled */
  130. switch(hsai->Init.SynchroExt)
  131. {
  132. case SAI_SYNCEXT_DISABLE :
  133. tmpregisterGCR = 0;
  134. break;
  135. case SAI_SYNCEXT_OUTBLOCKA_ENABLE :
  136. tmpregisterGCR = SAI_GCR_SYNCOUT_0;
  137. break;
  138. case SAI_SYNCEXT_OUTBLOCKB_ENABLE :
  139. tmpregisterGCR = SAI_GCR_SYNCOUT_1;
  140. break;
  141. }
  142. SAI1->GCR = tmpregisterGCR;
  143. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
  144. }
  145. /**
  146. * @brief Get SAI Input Clock based on SAI source clock selection
  147. * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
  148. * the configuration information for SAI module.
  149. * @retval SAI Clock Input
  150. */
  151. uint32_t SAI_GetInputClock(SAI_HandleTypeDef *hsai)
  152. {
  153. /* This variable used to store the SAI_CK_x (value in Hz) */
  154. uint32_t saiclocksource = 0;
  155. #if defined(STM32F446xx)
  156. if ((hsai->Instance == SAI1_Block_A) || (hsai->Instance == SAI1_Block_B))
  157. {
  158. saiclocksource = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SAI1);
  159. }
  160. else /* SAI2_Block_A || SAI2_Block_B*/
  161. {
  162. saiclocksource = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SAI2);
  163. }
  164. #endif /* STM32F446xx */
  165. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
  166. uint32_t vcoinput = 0, tmpreg = 0;
  167. /* Check the SAI Block parameters */
  168. assert_param(IS_SAI_CLK_SOURCE(hsai->Init.ClockSource));
  169. /* SAI Block clock source selection */
  170. if(hsai->Instance == SAI1_Block_A)
  171. {
  172. __HAL_RCC_SAI_BLOCKACLKSOURCE_CONFIG(hsai->Init.ClockSource);
  173. }
  174. else
  175. {
  176. __HAL_RCC_SAI_BLOCKBCLKSOURCE_CONFIG((uint32_t)(hsai->Init.ClockSource << 2));
  177. }
  178. /* VCO Input Clock value calculation */
  179. if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSI)
  180. {
  181. /* In Case the PLL Source is HSI (Internal Clock) */
  182. vcoinput = (HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
  183. }
  184. else
  185. {
  186. /* In Case the PLL Source is HSE (External Clock) */
  187. vcoinput = ((HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM)));
  188. }
  189. /* SAI_CLK_x : SAI Block Clock configuration for different clock sources selected */
  190. if(hsai->Init.ClockSource == SAI_CLKSOURCE_PLLSAI)
  191. {
  192. /* Configure the PLLI2S division factor */
  193. /* PLLSAI_VCO Input = PLL_SOURCE/PLLM */
  194. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN */
  195. /* SAI_CLK(first level) = PLLSAI_VCO Output/PLLSAIQ */
  196. tmpreg = (RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIQ) >> 24;
  197. saiclocksource = (vcoinput * ((RCC->PLLSAICFGR & RCC_PLLSAICFGR_PLLSAIN) >> 6))/(tmpreg);
  198. /* SAI_CLK_x = SAI_CLK(first level)/PLLSAIDIVQ */
  199. tmpreg = (((RCC->DCKCFGR & RCC_DCKCFGR_PLLSAIDIVQ) >> 8) + 1);
  200. saiclocksource = saiclocksource/(tmpreg);
  201. }
  202. else if(hsai->Init.ClockSource == SAI_CLKSOURCE_PLLI2S)
  203. {
  204. /* Configure the PLLI2S division factor */
  205. /* PLLI2S_VCO Input = PLL_SOURCE/PLLM */
  206. /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
  207. /* SAI_CLK(first level) = PLLI2S_VCO Output/PLLI2SQ */
  208. tmpreg = (RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SQ) >> 24;
  209. saiclocksource = (vcoinput * ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6))/(tmpreg);
  210. /* SAI_CLK_x = SAI_CLK(first level)/PLLI2SDIVQ */
  211. tmpreg = ((RCC->DCKCFGR & RCC_DCKCFGR_PLLI2SDIVQ) + 1);
  212. saiclocksource = saiclocksource/(tmpreg);
  213. }
  214. else /* sConfig->ClockSource == SAI_CLKSource_Ext */
  215. {
  216. /* Enable the External Clock selection */
  217. __HAL_RCC_I2S_CONFIG(RCC_I2SCLKSOURCE_EXT);
  218. saiclocksource = EXTERNAL_CLOCK_VALUE;
  219. }
  220. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
  221. /* the return result is the value of SAI clock */
  222. return saiclocksource;
  223. }
  224. /**
  225. * @}
  226. */
  227. /**
  228. * @}
  229. */
  230. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F446xx */
  231. #endif /* HAL_SAI_MODULE_ENABLED */
  232. /**
  233. * @}
  234. */
  235. /**
  236. * @}
  237. */
  238. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/