stm8s_awu.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. ******************************************************************************
  3. * @file stm8s_awu.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 AWU 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_awu.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. /* See also AWU_Timebase_TypeDef structure in stm8s_awu.h file :
  39. N 2 5 1 2 4 8 1 3 6 1 2 5 1 2 1 3
  40. O 5 0 m m m m 6 2 4 2 5 1 s s 2 0
  41. I 0 0 s s s s m m m 8 6 2 s s
  42. T u u s s s m m m
  43. s s s s s
  44. */
  45. /** Contains the different values to write in the APR register (used by AWU_Init function) */
  46. CONST uint8_t APR_Array[17] =
  47. {
  48. 0, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 61, 23, 23, 62
  49. };
  50. /** Contains the different values to write in the TBR register (used by AWU_Init function) */
  51. CONST uint8_t TBR_Array[17] =
  52. {
  53. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 14, 15, 15
  54. };
  55. /* Public functions ----------------------------------------------------------*/
  56. /**
  57. * @addtogroup AWU_Public_Functions
  58. * @{
  59. */
  60. /**
  61. * @brief Deinitializes the AWU peripheral registers to their default reset
  62. * values.
  63. * @param None
  64. * @retval None
  65. */
  66. void AWU_DeInit(void)
  67. {
  68. AWU->CSR = AWU_CSR_RESET_VALUE;
  69. AWU->APR = AWU_APR_RESET_VALUE;
  70. AWU->TBR = AWU_TBR_RESET_VALUE;
  71. }
  72. /**
  73. * @brief Initializes the AWU peripheral according to the specified parameters.
  74. * @param AWU_TimeBase : Time base selection (interval between AWU interrupts).
  75. * can be one of the values of @ref AWU_Timebase_TypeDef.
  76. * @retval None
  77. * @par Required preconditions:
  78. * The LS RC calibration must be performed before calling this function.
  79. */
  80. void AWU_Init(AWU_Timebase_TypeDef AWU_TimeBase)
  81. {
  82. /* Check parameter */
  83. assert_param(IS_AWU_TIMEBASE_OK(AWU_TimeBase));
  84. /* Enable the AWU peripheral */
  85. AWU->CSR |= AWU_CSR_AWUEN;
  86. /* Set the TimeBase */
  87. AWU->TBR &= (uint8_t)(~AWU_TBR_AWUTB);
  88. AWU->TBR |= TBR_Array[(uint8_t)AWU_TimeBase];
  89. /* Set the APR divider */
  90. AWU->APR &= (uint8_t)(~AWU_APR_APR);
  91. AWU->APR |= APR_Array[(uint8_t)AWU_TimeBase];
  92. }
  93. /**
  94. * @brief Enable or disable the AWU peripheral.
  95. * @param NewState Indicates the new state of the AWU peripheral.
  96. * @retval None
  97. * @par Required preconditions:
  98. * Initialisation of AWU and LS RC calibration must be done before.
  99. */
  100. void AWU_Cmd(FunctionalState NewState)
  101. {
  102. if (NewState != DISABLE)
  103. {
  104. /* Enable the AWU peripheral */
  105. AWU->CSR |= AWU_CSR_AWUEN;
  106. }
  107. else
  108. {
  109. /* Disable the AWU peripheral */
  110. AWU->CSR &= (uint8_t)(~AWU_CSR_AWUEN);
  111. }
  112. }
  113. /**
  114. * @brief Update APR register with the measured LSI frequency.
  115. * @par Note on the APR calculation:
  116. * A is the integer part of lsifreqkhz/4 and x the decimal part.
  117. * x <= A/(1+2A) is equivalent to A >= x(1+2A) and also to 4A >= 4x(1+2A) [F1]
  118. * but we know that A + x = lsifreqkhz/4 ==> 4x = lsifreqkhz-4A
  119. * so [F1] can be written :
  120. * 4A >= (lsifreqkhz-4A)(1+2A)
  121. * @param LSIFreqHz Low Speed RC frequency measured by timer (in Hz).
  122. * @retval None
  123. * @par Required preconditions:
  124. * - AWU must be disabled to avoid unwanted interrupts.
  125. */
  126. void AWU_LSICalibrationConfig(uint32_t LSIFreqHz)
  127. {
  128. uint16_t lsifreqkhz = 0x0;
  129. uint16_t A = 0x0;
  130. /* Check parameter */
  131. assert_param(IS_LSI_FREQUENCY_OK(LSIFreqHz));
  132. lsifreqkhz = (uint16_t)(LSIFreqHz / 1000); /* Converts value in kHz */
  133. /* Calculation of AWU calibration value */
  134. A = (uint16_t)(lsifreqkhz >> 2U); /* Division by 4, keep integer part only */
  135. if ((4U * A) >= ((lsifreqkhz - (4U * A)) * (1U + (2U * A))))
  136. {
  137. AWU->APR = (uint8_t)(A - 2U);
  138. }
  139. else
  140. {
  141. AWU->APR = (uint8_t)(A - 1U);
  142. }
  143. }
  144. /**
  145. * @brief Configures AWU in Idle mode to reduce power consumption.
  146. * @param None
  147. * @retval None
  148. */
  149. void AWU_IdleModeEnable(void)
  150. {
  151. /* Disable AWU peripheral */
  152. AWU->CSR &= (uint8_t)(~AWU_CSR_AWUEN);
  153. /* No AWU timebase */
  154. AWU->TBR = (uint8_t)(~AWU_TBR_AWUTB);
  155. }
  156. /**
  157. * @brief Returns status of the AWU peripheral flag.
  158. * @param None
  159. * @retval FlagStatus : Status of the AWU flag.
  160. * This parameter can be any of the @ref FlagStatus enumeration.
  161. */
  162. FlagStatus AWU_GetFlagStatus(void)
  163. {
  164. return((FlagStatus)(((uint8_t)(AWU->CSR & AWU_CSR_AWUF) == (uint8_t)0x00) ? RESET : SET));
  165. }
  166. /**
  167. * @}
  168. */
  169. /**
  170. * @}
  171. */
  172. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/