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

STM8S/A

stm8s_awu.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm8s_awu.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 AWU 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_awu.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 /* See also AWU_Timebase_TypeDef structure in stm8s_awu.h file :
00042                           N   2   5   1   2   4   8   1   3   6   1   2   5   1   2   1   3
00043                           O   5   0   m   m   m   m   6   2   4   2   5   1   s   s   2   0
00044                           I   0   0   s   s   s   s   m   m   m   8   6   2           s   s
00045                           T   u   u                   s   s   s   m   m   m
00046                               s   s                               s   s   s
00047 */
00048 /** Contains the different values to write in the APR register (used by AWU_Init function) */
00049 CONST uint8_t APR_Array[17] =
00050     {
00051         0, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 61, 23, 23, 62
00052     };
00053 
00054 /** Contains the different values to write in the TBR register (used by AWU_Init function) */
00055 CONST uint8_t TBR_Array[17] =
00056     {
00057         0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 12, 14, 15, 15
00058     };
00059 
00060 /* Public functions ----------------------------------------------------------*/
00061 
00062 /**
00063   * @addtogroup AWU_Public_Functions
00064   * @{
00065   */
00066 
00067 /**
00068   * @brief  Deinitializes the AWU peripheral registers to their default reset
00069   * values.
00070   * @param  None
00071   * @retval None
00072   */
00073 void AWU_DeInit(void)
00074 {
00075   AWU->CSR = AWU_CSR_RESET_VALUE;
00076   AWU->APR = AWU_APR_RESET_VALUE;
00077   AWU->TBR = AWU_TBR_RESET_VALUE;
00078 }
00079 
00080 /**
00081   * @brief  Initializes the AWU peripheral according to the specified parameters.
00082   * @param   AWU_TimeBase : Time base selection (interval between AWU interrupts).
00083   * can be one of the values of @ref AWU_Timebase_TypeDef.
00084   * @retval None
00085   * @par Required preconditions:
00086   * The LS RC calibration must be performed before calling this function.
00087   */
00088 void AWU_Init(AWU_Timebase_TypeDef AWU_TimeBase)
00089 {
00090   /* Check parameter */
00091   assert_param(IS_AWU_TIMEBASE_OK(AWU_TimeBase));
00092   
00093   /* Enable the AWU peripheral */
00094   AWU->CSR |= AWU_CSR_AWUEN;
00095   
00096   /* Set the TimeBase */
00097   AWU->TBR &= (uint8_t)(~AWU_TBR_AWUTB);
00098   AWU->TBR |= TBR_Array[(uint8_t)AWU_TimeBase];
00099   
00100   /* Set the APR divider */
00101   AWU->APR &= (uint8_t)(~AWU_APR_APR);
00102   AWU->APR |= APR_Array[(uint8_t)AWU_TimeBase];
00103 }
00104 
00105 /**
00106   * @brief  Enable or disable the AWU peripheral.
00107   * @param   NewState Indicates the new state of the AWU peripheral.
00108   * @retval None
00109   * @par Required preconditions:
00110   * Initialisation of AWU and LS RC calibration must be done before.
00111   */
00112 void AWU_Cmd(FunctionalState NewState)
00113 {
00114   if (NewState != DISABLE)
00115   {
00116     /* Enable the AWU peripheral */
00117     AWU->CSR |= AWU_CSR_AWUEN;
00118   }
00119   else
00120   {
00121     /* Disable the AWU peripheral */
00122     AWU->CSR &= (uint8_t)(~AWU_CSR_AWUEN);
00123   }
00124 }
00125 
00126 /**
00127   * @brief  Update APR register with the measured LSI frequency.
00128   * @par Note on the APR calculation:
00129   * A is the integer part of lsifreqkhz/4 and x the decimal part.
00130   * x <= A/(1+2A) is equivalent to A >= x(1+2A) and also to 4A >= 4x(1+2A) [F1]
00131   * but we know that A + x = lsifreqkhz/4 ==> 4x = lsifreqkhz-4A
00132   * so [F1] can be written :
00133   * 4A >= (lsifreqkhz-4A)(1+2A)
00134   * @param   LSIFreqHz Low Speed RC frequency measured by timer (in Hz).
00135   * @retval None
00136   * @par Required preconditions:
00137   * - AWU must be disabled to avoid unwanted interrupts.
00138   */
00139 void AWU_LSICalibrationConfig(uint32_t LSIFreqHz)
00140 {
00141   uint16_t lsifreqkhz = 0x0;
00142   uint16_t A = 0x0;
00143   
00144   /* Check parameter */
00145   assert_param(IS_LSI_FREQUENCY_OK(LSIFreqHz));
00146   
00147   lsifreqkhz = (uint16_t)(LSIFreqHz / 1000); /* Converts value in kHz */
00148   
00149   /* Calculation of AWU calibration value */
00150   
00151   A = (uint16_t)(lsifreqkhz >> 2U); /* Division by 4, keep integer part only */
00152   
00153   if ((4U * A) >= ((lsifreqkhz - (4U * A)) * (1U + (2U * A))))
00154   {
00155     AWU->APR = (uint8_t)(A - 2U);
00156   }
00157   else
00158   {
00159     AWU->APR = (uint8_t)(A - 1U);
00160   }
00161 }
00162 
00163 /**
00164   * @brief  Configures AWU in Idle mode to reduce power consumption.
00165   * @param  None
00166   * @retval None
00167   */
00168 void AWU_IdleModeEnable(void)
00169 {
00170   /* Disable AWU peripheral */
00171   AWU->CSR &= (uint8_t)(~AWU_CSR_AWUEN);
00172   
00173   /* No AWU timebase */
00174   AWU->TBR = (uint8_t)(~AWU_TBR_AWUTB);
00175 }
00176 
00177 /**
00178   * @brief  Returns status of the AWU peripheral flag.
00179   * @param  None
00180   * @retval FlagStatus : Status of the AWU flag.
00181   * This parameter can be any of the @ref FlagStatus enumeration.
00182   */
00183 FlagStatus AWU_GetFlagStatus(void)
00184 {
00185   return((FlagStatus)(((uint8_t)(AWU->CSR & AWU_CSR_AWUF) == (uint8_t)0x00) ? RESET : SET));
00186 }
00187 
00188 
00189 /**
00190   * @}
00191   */
00192   
00193 /**
00194   * @}
00195   */
00196   
00197 
00198 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM8 Standard Peripherals Library: Footer

 

 

 

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