stm8l15x_beep.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. ******************************************************************************
  3. * @file stm8l15x_beep.c
  4. * @author MCD Application Team
  5. * @version V1.6.1
  6. * @date 30-September-2014
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the BEEPER (BEEP) peripheral:
  9. * - Initialization and Configuration
  10. * - Low Speed Internal Clock(LSI) Calibration
  11. *
  12. * @verbatim
  13. * ===================================================================
  14. * How to use this driver
  15. * ===================================================================
  16. * 1- Make sure that the LS RC clock calibration is performed by the following
  17. * steps:
  18. * - Connect internally the LS clock source to TIM2 channel 1 input
  19. * capture for measurement using BEEP_LSClockToTIMConnectCmd() function
  20. * - Update the BEEP_CSR register by the measured LSI frequency
  21. * --> During this phase the BEEPER must be disabled to avoid
  22. * unwanted interrupts
  23. *
  24. * 2- Configure the output beeper frequency using the BEEP_Init() function
  25. *
  26. * 3- Enable the beeper using the BEEP_Cmd() function
  27. *
  28. * @endverbatim
  29. ******************************************************************************
  30. * @attention
  31. *
  32. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  33. *
  34. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  35. * You may not use this file except in compliance with the License.
  36. * You may obtain a copy of the License at:
  37. *
  38. * http://www.st.com/software_license_agreement_liberty_v2
  39. *
  40. * Unless required by applicable law or agreed to in writing, software
  41. * distributed under the License is distributed on an "AS IS" BASIS,
  42. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  43. * See the License for the specific language governing permissions and
  44. * limitations under the License.
  45. *
  46. ******************************************************************************
  47. */
  48. /* Includes ------------------------------------------------------------------*/
  49. #include "stm8l15x_beep.h"
  50. /** @addtogroup STM8L15x_StdPeriph_Driver
  51. * @{
  52. */
  53. /** @defgroup BEEP
  54. * @brief BEEP driver modules
  55. * @{
  56. */
  57. /* Private typedef -----------------------------------------------------------*/
  58. /* Private define ------------------------------------------------------------*/
  59. /* Private macro -------------------------------------------------------------*/
  60. /* Private variables ---------------------------------------------------------*/
  61. /* Private function prototypes -----------------------------------------------*/
  62. /* Private functions ---------------------------------------------------------*/
  63. /** @defgroup BEEP_Private_Functions
  64. * @{
  65. */
  66. /** @defgroup BEEP_Group1 Initialization and Configuration functions
  67. * @brief Initialization and Configuration functions
  68. *
  69. @verbatim
  70. ===============================================================================
  71. Initialization and Configuration functions
  72. ===============================================================================
  73. This section provides functions allowing to:
  74. - Initialize and configure the Beeper frequency
  75. - Enable and Disable the Beeper output
  76. @endverbatim
  77. * @{
  78. */
  79. /**
  80. * @brief Deinitializes the BEEP peripheral registers to their default
  81. * reset values.
  82. * @param None
  83. * @retval None
  84. */
  85. void BEEP_DeInit(void)
  86. {
  87. BEEP->CSR1 = BEEP_CSR1_RESET_VALUE;
  88. BEEP->CSR2 = BEEP_CSR2_RESET_VALUE;
  89. }
  90. /**
  91. * @brief Initializes the BEEP function according to the specified parameters.
  92. * @note The LS RC calibration must be performed before calling this function.
  93. * @param BEEP_Frequency Frequency selection.
  94. * This parameter can be one of the values of @ref BEEP_Frequency_TypeDef.
  95. * @retval None
  96. */
  97. void BEEP_Init(BEEP_Frequency_TypeDef BEEP_Frequency)
  98. {
  99. /* Check parameter */
  100. assert_param(IS_BEEP_FREQUENCY(BEEP_Frequency));
  101. /* Set a default calibration value if no calibration is done */
  102. if ((BEEP->CSR2 & BEEP_CSR2_BEEPDIV) == BEEP_CSR2_BEEPDIV)
  103. {
  104. BEEP->CSR2 &= (uint8_t)(~BEEP_CSR2_BEEPDIV); /* Clear bits */
  105. BEEP->CSR2 |= BEEP_CALIBRATION_DEFAULT;
  106. }
  107. /* Select the output frequency */
  108. BEEP->CSR2 &= (uint8_t)(~BEEP_CSR2_BEEPSEL);
  109. BEEP->CSR2 |= (uint8_t)(BEEP_Frequency);
  110. }
  111. /**
  112. * @brief Enable or disable the BEEP function.
  113. * @note Initialisation of BEEP and LS RC calibration must be done before.
  114. * @param NewState Indicates the new state of the BEEP function.
  115. * @retval None
  116. */
  117. void BEEP_Cmd(FunctionalState NewState)
  118. {
  119. /* Check the parameters */
  120. assert_param(IS_FUNCTIONAL_STATE(NewState));
  121. if (NewState != DISABLE)
  122. {
  123. /* Enable the BEEP peripheral */
  124. BEEP->CSR2 |= BEEP_CSR2_BEEPEN;
  125. }
  126. else
  127. {
  128. /* Disable the BEEP peripheral */
  129. BEEP->CSR2 &= (uint8_t)(~BEEP_CSR2_BEEPEN);
  130. }
  131. }
  132. /**
  133. * @}
  134. */
  135. /** @defgroup BEEP_Group2 Low Speed Internal Clock(LSI) Calibration functions
  136. * @brief Low Speed Internal Clock(LSI) Calibration functions
  137. *
  138. @verbatim
  139. ===============================================================================
  140. Low Speed Internal Clock(LSI) Calibration functions
  141. ===============================================================================
  142. This section provides functions allowing to measure and calibrate the internal
  143. low speed clock source to ensure better BEEPER output frequency .
  144. A typical configuration for LSI calibration is done following these steps :
  145. 1. Disable the Beeper to avoid any unwanted interrupt using BEEP_Cmd() function
  146. 2. Measure the LSI clock frequency by connecting it internally to TIM2 input capture
  147. using BEEP_LSClockToTIMConnectCmd() function.
  148. 3. Calibrate the beeper frequency with the measured LSI clock frequency using
  149. BEEP_LSICalibrationConfig() function.
  150. @endverbatim
  151. * @{
  152. */
  153. /**
  154. * @brief Enable or disable the LS clock source connection to TIM for measurement.
  155. * @param NewState Indicates the new state of the LS clock to TIM connection
  156. * @retval None
  157. */
  158. void BEEP_LSClockToTIMConnectCmd(FunctionalState NewState)
  159. {
  160. /* Check the parameters */
  161. assert_param(IS_FUNCTIONAL_STATE(NewState));
  162. if (NewState != DISABLE)
  163. {
  164. /* Connect LS clock to TIM for measurement */
  165. BEEP->CSR1 |= BEEP_CSR1_MSR;
  166. }
  167. else
  168. {
  169. /* Disconnect LS clock to TIM */
  170. BEEP->CSR1 &= (uint8_t)(~BEEP_CSR1_MSR);
  171. }
  172. }
  173. /**
  174. * @brief Update CSR register with the measured LSI frequency.
  175. * @note BEEP must be disabled to avoid unwanted interrupts.
  176. * @note Prescaler calculation:
  177. * A is the integer part of LSIFreqkHz/4 and x the decimal part.
  178. * x <= A/(1+2A) is equivalent to A >= x(1+2A)
  179. * and also to 4A >= 4x(1+2A) [F1]
  180. * but we know that A + x = LSIFreqkHz/4 ==> 4x = LSIFreqkHz-4A
  181. * so [F1] can be written :
  182. * 4A >= (LSIFreqkHz-4A)(1+2A)
  183. * @param LSIFreqHz Low Speed RC frequency measured by timer (in Hz).
  184. * @retval None
  185. */
  186. void BEEP_LSICalibrationConfig(uint32_t LSIFreqHz)
  187. {
  188. uint16_t lsifreqkhz;
  189. uint16_t A;
  190. /* Check parameter */
  191. assert_param(IS_LSI_FREQUENCY(LSIFreqHz));
  192. lsifreqkhz = (uint16_t)(LSIFreqHz / 1000); /* Converts value in kHz */
  193. /* Calculation of BEEPER calibration value */
  194. BEEP->CSR2 &= (uint8_t)(~BEEP_CSR2_BEEPDIV); /* Clear bits */
  195. A = (uint16_t)(lsifreqkhz >> 3U); /* Division by 8, keep integer part only */
  196. if ((8U * A) >= ((lsifreqkhz - (8U * A)) * (1U + (2U * A))))
  197. {
  198. BEEP->CSR2 |= (uint8_t)(A - 2U);
  199. }
  200. else
  201. {
  202. BEEP->CSR2 |= (uint8_t)(A - 1U);
  203. }
  204. }
  205. /**
  206. * @}
  207. */
  208. /**
  209. * @}
  210. */
  211. /**
  212. * @}
  213. */
  214. /**
  215. * @}
  216. */
  217. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/