STM8L15x Standard Peripherals Drivers: stm8l15x_beep.c Source File

STM8L15x/16x Standard Peripherals Drivers

STM8L15x Standard Peripherals Drivers

stm8l15x_beep.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm8l15x_beep.c
00004   * @author  MCD Application Team
00005   * @version V1.5.0
00006   * @date    13-May-2011
00007   * @brief   This file provides firmware functions to manage the following 
00008   *          functionalities of the BEEPER (BEEP) peripheral:           
00009   *           - Initialization and Configuration
00010   *           - Low Speed Internal Clock(LSI) Calibration
00011   *
00012   *  @verbatim  
00013   *          ===================================================================
00014   *                                 How to use this driver
00015   *          ===================================================================  
00016   *          1- Make sure that the LS RC clock calibration is performed by the following 
00017   *            steps:
00018   *               - Connect internally the LS clock source to TIM2 channel 1 input
00019   *                 capture for measurement using BEEP_LSClockToTIMConnectCmd() function
00020   *               - Update the BEEP_CSR register by the measured LSI frequency 
00021   *                  --> During this phase the BEEPER must be disabled to avoid 
00022   *                      unwanted interrupts  
00023   *
00024   *          2- Configure the output beeper frequency using the BEEP_Init() function
00025   *
00026   *          3- Enable the beeper using the BEEP_Cmd() function
00027   *
00028   *  @endverbatim   
00029   ******************************************************************************
00030   * @attention
00031   *
00032   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00033   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00034   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00035   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00036   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00037   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00038   *
00039   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
00040   ******************************************************************************  
00041   */
00042 
00043 /* Includes ------------------------------------------------------------------*/
00044 #include "stm8l15x_beep.h"
00045 
00046 /** @addtogroup STM8L15x_StdPeriph_Driver
00047   * @{
00048   */
00049 /** @defgroup BEEP 
00050   * @brief BEEP driver modules
00051   * @{
00052   */ 
00053 
00054 /* Private typedef -----------------------------------------------------------*/
00055 /* Private define ------------------------------------------------------------*/
00056 /* Private macro -------------------------------------------------------------*/
00057 /* Private variables ---------------------------------------------------------*/
00058 /* Private function prototypes -----------------------------------------------*/
00059 /* Private functions ---------------------------------------------------------*/
00060 
00061 /** @defgroup BEEP_Private_Functions
00062   * @{
00063   */
00064 
00065 /** @defgroup BEEP_Group1 Initialization and Configuration functions
00066  *  @brief    Initialization and Configuration functions 
00067  *
00068 @verbatim    
00069  ===============================================================================
00070                       Initialization and Configuration functions
00071  ===============================================================================  
00072   This section provides functions allowing to:
00073    - Initialize and configure the Beeper frequency
00074    - Enable and Disable the Beeper output
00075    
00076 @endverbatim
00077   * @{
00078   */
00079   
00080 /**
00081   * @brief  Deinitializes the BEEP peripheral registers to their default
00082   *         reset values.
00083   * @param  None
00084   * @retval None
00085   */
00086 void BEEP_DeInit(void)
00087 {
00088   BEEP->CSR1 = BEEP_CSR1_RESET_VALUE;
00089   BEEP->CSR2 = BEEP_CSR2_RESET_VALUE;
00090 }
00091 
00092 /**
00093   * @brief  Initializes the BEEP function according to the specified parameters.
00094   * @note   The LS RC calibration must be performed before calling this function.
00095   * @param  BEEP_Frequency Frequency selection.
00096   *         This parameter can be one of the values of @ref BEEP_Frequency_TypeDef.
00097   * @retval None
00098   */
00099 void BEEP_Init(BEEP_Frequency_TypeDef BEEP_Frequency)
00100 {
00101 
00102   /* Check parameter */
00103   assert_param(IS_BEEP_FREQUENCY(BEEP_Frequency));
00104 
00105   /* Set a default calibration value if no calibration is done */
00106   if ((BEEP->CSR2 & BEEP_CSR2_BEEPDIV) == BEEP_CSR2_BEEPDIV)
00107   {
00108     BEEP->CSR2 &= (uint8_t)(~BEEP_CSR2_BEEPDIV); /* Clear bits */
00109     BEEP->CSR2 |= BEEP_CALIBRATION_DEFAULT;
00110   }
00111 
00112   /* Select the output frequency */
00113   BEEP->CSR2 &= (uint8_t)(~BEEP_CSR2_BEEPSEL);
00114   BEEP->CSR2 |= (uint8_t)(BEEP_Frequency);
00115 
00116 }
00117 
00118 /**
00119   * @brief  Enable or disable the BEEP function.
00120   * @note   Initialisation of BEEP and LS RC calibration must be done before.
00121   * @param  NewState Indicates the new state of the BEEP function.
00122   * @retval None
00123   */
00124 void BEEP_Cmd(FunctionalState NewState)
00125 {
00126   /* Check the parameters */
00127   assert_param(IS_FUNCTIONAL_STATE(NewState));
00128 
00129   if (NewState != DISABLE)
00130   {
00131     /* Enable the BEEP peripheral */
00132     BEEP->CSR2 |= BEEP_CSR2_BEEPEN;
00133   }
00134   else
00135   {
00136     /* Disable the BEEP peripheral */
00137     BEEP->CSR2 &= (uint8_t)(~BEEP_CSR2_BEEPEN);
00138   }
00139 }
00140 
00141 /**
00142   * @}
00143   */
00144 
00145 /** @defgroup BEEP_Group2 Low Speed Internal Clock(LSI) Calibration functions
00146  *  @brief    Low Speed Internal Clock(LSI) Calibration functions 
00147  *
00148 @verbatim   
00149  ===============================================================================
00150               Low Speed Internal Clock(LSI) Calibration functions
00151  ===============================================================================  
00152 
00153   This section provides functions allowing to measure and calibrate the internal 
00154   low speed clock source to ensure better BEEPER output frequency .
00155   
00156   A typical configuration for LSI calibration is done following these steps :
00157    1. Disable the Beeper to avoid any unwanted interrupt using BEEP_Cmd() function 
00158    2. Measure the LSI clock frequency by connecting it internally to TIM2 input capture  
00159       using BEEP_LSClockToTIMConnectCmd() function.
00160    3. Calibrate the beeper frequency with the measured LSI clock frequency using 
00161       BEEP_LSICalibrationConfig() function.
00162    
00163 @endverbatim
00164   * @{
00165   */
00166   
00167 /**
00168   * @brief  Enable or disable the LS clock source connection to TIM for measurement.
00169   * @param  NewState Indicates the new state of the LS clock to TIM connection
00170   * @retval None
00171   */
00172 void BEEP_LSClockToTIMConnectCmd(FunctionalState NewState)
00173 {
00174   /* Check the parameters */
00175   assert_param(IS_FUNCTIONAL_STATE(NewState));
00176 
00177   if (NewState != DISABLE)
00178   {
00179     /* Connect LS clock to TIM for measurement */
00180     BEEP->CSR1 |= BEEP_CSR1_MSR;
00181   }
00182   else
00183   {
00184     /* Disconnect LS clock to TIM */
00185     BEEP->CSR1 &= (uint8_t)(~BEEP_CSR1_MSR);
00186   }
00187 }
00188 /**
00189   * @brief  Update CSR register with the measured LSI frequency.
00190   * @note   BEEP must be disabled to avoid unwanted interrupts.
00191   * @note   Prescaler calculation:
00192   *         A is the integer part of LSIFreqkHz/4 and x the decimal part.
00193   *         x <= A/(1+2A) is equivalent to A >= x(1+2A)
00194   *         and also to 4A >= 4x(1+2A) [F1]
00195   *         but we know that A + x = LSIFreqkHz/4 ==> 4x = LSIFreqkHz-4A
00196   *         so [F1] can be written :
00197   *         4A >= (LSIFreqkHz-4A)(1+2A)
00198   * @param  LSIFreqHz Low Speed RC frequency measured by timer (in Hz).
00199   * @retval None
00200   */
00201 void BEEP_LSICalibrationConfig(uint32_t LSIFreqHz)
00202 {
00203   uint16_t lsifreqkhz;
00204   uint16_t A;
00205 
00206   /* Check parameter */
00207   assert_param(IS_LSI_FREQUENCY(LSIFreqHz));
00208 
00209   lsifreqkhz = (uint16_t)(LSIFreqHz / 1000); /* Converts value in kHz */
00210 
00211   /* Calculation of BEEPER calibration value */
00212 
00213   BEEP->CSR2 &= (uint8_t)(~BEEP_CSR2_BEEPDIV); /* Clear bits */
00214 
00215   A = (uint16_t)(lsifreqkhz >> 3U); /* Division by 8, keep integer part only */
00216 
00217   if ((8U * A) >= ((lsifreqkhz - (8U * A)) * (1U + (2U * A))))
00218   {
00219     BEEP->CSR2 |= (uint8_t)(A - 2U);
00220   }
00221   else
00222   {
00223     BEEP->CSR2 |= (uint8_t)(A - 1U);
00224   }
00225 }
00226 /**
00227   * @}
00228   */
00229 
00230 /**
00231   * @}
00232   */
00233 /**
00234   * @}
00235   */
00236 
00237 /**
00238   * @}
00239   */
00240 
00241 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
STM8S Firmware Library: Overview

 

 

 

For complete documentation on STM8L15x 8-bit microcontrollers platform visit www.st.com