source: asp3_wo_tecs/trunk/arch/arm_m_gcc/stm32f4xx_stm32cube/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sai_ex.c@ 303

Last change on this file since 303 was 303, checked in by ertl-honda, 7 years ago

nucleo_f401re依存部の追加

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