STM8S/A Standard Peripherals Drivers: stm8s_beep.c Source File

STM8S/A

stm8s_beep.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm8s_beep.c
00004   * @author  MCD Application Team
00005   * @version V2.3.0
00006   * @date    16-June-2017
00007   * @brief   This file contains all the functions for the BEEP peripheral.
00008    ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
00012   *
00013   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
00014   * You may not use this file except in compliance with the License.
00015   * You may obtain a copy of the License at:
00016   *
00017   *        http://www.st.com/software_license_agreement_liberty_v2
00018   *
00019   * Unless required by applicable law or agreed to in writing, software 
00020   * distributed under the License is distributed on an "AS IS" BASIS, 
00021   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00022   * See the License for the specific language governing permissions and
00023   * limitations under the License.
00024   *
00025   ******************************************************************************
00026   */
00027 
00028 /* Includes ------------------------------------------------------------------*/
00029 #include "stm8s_beep.h"
00030 
00031 /** @addtogroup STM8S_StdPeriph_Driver
00032   * @{
00033   */
00034 /* Private typedef -----------------------------------------------------------*/
00035 /* Private define ------------------------------------------------------------*/
00036 /* Private macro -------------------------------------------------------------*/
00037 /* Private variables ---------------------------------------------------------*/
00038 /* Private function prototypes -----------------------------------------------*/
00039 /* Private functions ---------------------------------------------------------*/
00040 
00041 /* Public functions ----------------------------------------------------------*/
00042 
00043 /**
00044   * @addtogroup BEEP_Public_Functions
00045   * @{
00046   */
00047 
00048 /**
00049   * @brief  Deinitializes the BEEP peripheral registers to their default reset
00050   * values.
00051   * @param  None
00052   * @retval None
00053   */
00054 void BEEP_DeInit(void)
00055 {
00056   BEEP->CSR = BEEP_CSR_RESET_VALUE;
00057 }
00058 
00059 /**
00060   * @brief  Initializes the BEEP function according to the specified parameters.
00061   * @param   BEEP_Frequency Frequency selection.
00062   * can be one of the values of @ref BEEP_Frequency_TypeDef.
00063   * @retval None
00064   * @par Required preconditions:
00065   * The LS RC calibration must be performed before calling this function.
00066   */
00067 void BEEP_Init(BEEP_Frequency_TypeDef BEEP_Frequency)
00068 {
00069   /* Check parameter */
00070   assert_param(IS_BEEP_FREQUENCY_OK(BEEP_Frequency));
00071   
00072   /* Set a default calibration value if no calibration is done */
00073   if ((BEEP->CSR & BEEP_CSR_BEEPDIV) == BEEP_CSR_BEEPDIV)
00074   {
00075     BEEP->CSR &= (uint8_t)(~BEEP_CSR_BEEPDIV); /* Clear bits */
00076     BEEP->CSR |= BEEP_CALIBRATION_DEFAULT;
00077   }
00078   
00079   /* Select the output frequency */
00080   BEEP->CSR &= (uint8_t)(~BEEP_CSR_BEEPSEL);
00081   BEEP->CSR |= (uint8_t)(BEEP_Frequency);
00082 }
00083 
00084 /**
00085   * @brief  Enable or disable the BEEP function.
00086   * @param   NewState Indicates the new state of the BEEP function.
00087   * @retval None
00088   * @par Required preconditions:
00089   * Initialisation of BEEP and LS RC calibration must be done before.
00090   */
00091 void BEEP_Cmd(FunctionalState NewState)
00092 {
00093   if (NewState != DISABLE)
00094   {
00095     /* Enable the BEEP peripheral */
00096     BEEP->CSR |= BEEP_CSR_BEEPEN;
00097   }
00098   else
00099   {
00100     /* Disable the BEEP peripheral */
00101     BEEP->CSR &= (uint8_t)(~BEEP_CSR_BEEPEN);
00102   }
00103 }
00104 
00105 /**
00106   * @brief  Update CSR register with the measured LSI frequency.
00107   * @par Note on the APR calculation:
00108   * A is the integer part of LSIFreqkHz/4 and x the decimal part.
00109   * x <= A/(1+2A) is equivalent to A >= x(1+2A) and also to 4A >= 4x(1+2A) [F1]
00110   * but we know that A + x = LSIFreqkHz/4 ==> 4x = LSIFreqkHz-4A
00111   * so [F1] can be written :
00112   * 4A >= (LSIFreqkHz-4A)(1+2A)
00113   * @param   LSIFreqHz Low Speed RC frequency measured by timer (in Hz).
00114   * @retval None
00115   * @par Required preconditions:
00116   * - BEEP must be disabled to avoid unwanted interrupts.
00117   */
00118 void BEEP_LSICalibrationConfig(uint32_t LSIFreqHz)
00119 {
00120   uint16_t lsifreqkhz;
00121   uint16_t A;
00122   
00123   /* Check parameter */
00124   assert_param(IS_LSI_FREQUENCY_OK(LSIFreqHz));
00125   
00126   lsifreqkhz = (uint16_t)(LSIFreqHz / 1000); /* Converts value in kHz */
00127   
00128   /* Calculation of BEEPER calibration value */
00129   
00130   BEEP->CSR &= (uint8_t)(~BEEP_CSR_BEEPDIV); /* Clear bits */
00131   
00132   A = (uint16_t)(lsifreqkhz >> 3U); /* Division by 8, keep integer part only */
00133   
00134   if ((8U * A) >= ((lsifreqkhz - (8U * A)) * (1U + (2U * A))))
00135   {
00136     BEEP->CSR |= (uint8_t)(A - 2U);
00137   }
00138   else
00139   {
00140     BEEP->CSR |= (uint8_t)(A - 1U);
00141   }
00142 }
00143 
00144 /**
00145   * @}
00146   */
00147   
00148 /**
00149   * @}
00150   */
00151   
00152 
00153 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM8 Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM8 8-bit Microcontrollers platform visit www.st.com