stm8s_beep.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. ******************************************************************************
  3. * @file stm8s_beep.c
  4. * @author MCD Application Team
  5. * @version V2.3.0
  6. * @date 16-June-2017
  7. * @brief This file contains all the functions for the BEEP peripheral.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  12. *
  13. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14. * You may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at:
  16. *
  17. * http://www.st.com/software_license_agreement_liberty_v2
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. ******************************************************************************
  26. */
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "stm8s_beep.h"
  29. /** @addtogroup STM8S_StdPeriph_Driver
  30. * @{
  31. */
  32. /* Private typedef -----------------------------------------------------------*/
  33. /* Private define ------------------------------------------------------------*/
  34. /* Private macro -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private function prototypes -----------------------------------------------*/
  37. /* Private functions ---------------------------------------------------------*/
  38. /* Public functions ----------------------------------------------------------*/
  39. /**
  40. * @addtogroup BEEP_Public_Functions
  41. * @{
  42. */
  43. /**
  44. * @brief Deinitializes the BEEP peripheral registers to their default reset
  45. * values.
  46. * @param None
  47. * @retval None
  48. */
  49. void BEEP_DeInit(void)
  50. {
  51. BEEP->CSR = BEEP_CSR_RESET_VALUE;
  52. }
  53. /**
  54. * @brief Initializes the BEEP function according to the specified parameters.
  55. * @param BEEP_Frequency Frequency selection.
  56. * can be one of the values of @ref BEEP_Frequency_TypeDef.
  57. * @retval None
  58. * @par Required preconditions:
  59. * The LS RC calibration must be performed before calling this function.
  60. */
  61. void BEEP_Init(BEEP_Frequency_TypeDef BEEP_Frequency)
  62. {
  63. /* Check parameter */
  64. assert_param(IS_BEEP_FREQUENCY_OK(BEEP_Frequency));
  65. /* Set a default calibration value if no calibration is done */
  66. if ((BEEP->CSR & BEEP_CSR_BEEPDIV) == BEEP_CSR_BEEPDIV)
  67. {
  68. BEEP->CSR &= (uint8_t)(~BEEP_CSR_BEEPDIV); /* Clear bits */
  69. BEEP->CSR |= BEEP_CALIBRATION_DEFAULT;
  70. }
  71. /* Select the output frequency */
  72. BEEP->CSR &= (uint8_t)(~BEEP_CSR_BEEPSEL);
  73. BEEP->CSR |= (uint8_t)(BEEP_Frequency);
  74. }
  75. /**
  76. * @brief Enable or disable the BEEP function.
  77. * @param NewState Indicates the new state of the BEEP function.
  78. * @retval None
  79. * @par Required preconditions:
  80. * Initialisation of BEEP and LS RC calibration must be done before.
  81. */
  82. void BEEP_Cmd(FunctionalState NewState)
  83. {
  84. if (NewState != DISABLE)
  85. {
  86. /* Enable the BEEP peripheral */
  87. BEEP->CSR |= BEEP_CSR_BEEPEN;
  88. }
  89. else
  90. {
  91. /* Disable the BEEP peripheral */
  92. BEEP->CSR &= (uint8_t)(~BEEP_CSR_BEEPEN);
  93. }
  94. }
  95. /**
  96. * @brief Update CSR register with the measured LSI frequency.
  97. * @par Note on the APR calculation:
  98. * A is the integer part of LSIFreqkHz/4 and x the decimal part.
  99. * x <= A/(1+2A) is equivalent to A >= x(1+2A) and also to 4A >= 4x(1+2A) [F1]
  100. * but we know that A + x = LSIFreqkHz/4 ==> 4x = LSIFreqkHz-4A
  101. * so [F1] can be written :
  102. * 4A >= (LSIFreqkHz-4A)(1+2A)
  103. * @param LSIFreqHz Low Speed RC frequency measured by timer (in Hz).
  104. * @retval None
  105. * @par Required preconditions:
  106. * - BEEP must be disabled to avoid unwanted interrupts.
  107. */
  108. void BEEP_LSICalibrationConfig(uint32_t LSIFreqHz)
  109. {
  110. uint16_t lsifreqkhz;
  111. uint16_t A;
  112. /* Check parameter */
  113. assert_param(IS_LSI_FREQUENCY_OK(LSIFreqHz));
  114. lsifreqkhz = (uint16_t)(LSIFreqHz / 1000); /* Converts value in kHz */
  115. /* Calculation of BEEPER calibration value */
  116. BEEP->CSR &= (uint8_t)(~BEEP_CSR_BEEPDIV); /* Clear bits */
  117. A = (uint16_t)(lsifreqkhz >> 3U); /* Division by 8, keep integer part only */
  118. if ((8U * A) >= ((lsifreqkhz - (8U * A)) * (1U + (2U * A))))
  119. {
  120. BEEP->CSR |= (uint8_t)(A - 2U);
  121. }
  122. else
  123. {
  124. BEEP->CSR |= (uint8_t)(A - 1U);
  125. }
  126. }
  127. /**
  128. * @}
  129. */
  130. /**
  131. * @}
  132. */
  133. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/