fix LPC812 PWM

Dependents:   IR_LED_Send

Fork of mbed-dev by mbed official

Committer:
nameless129
Date:
Mon May 16 16:50:30 2016 +0000
Revision:
129:2e517c56bcfb
Parent:
19:112740acecfa
PWM Fix:Duty 0%??H???????????????

Who changed what in which revision?

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