⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stm32l1xx_rtc.c

📁 STM32+Grlib
💻 C
📖 第 1 页 / 共 5 页
字号:
/**
  ******************************************************************************
  * @file    stm32l1xx_rtc.c
  * @author  MCD Application Team
  * @version V1.0.0
  * @date    31-December-2010
  * @brief   This file provides firmware functions to manage the following 
  *          functionalities of the Real-Time Clock (RTC) peripheral:           
  *           - Initialization
  *           - Calendar (Time and Date) configuration
  *           - Alarms (Alarm A and Alarm B) configuration
  *           - WakeUp Timer configuration
  *           - Daylight Saving configuration
  *           - Output pin Configuration
  *           - Digital Calibration configuration
  *           - TimeStamp configuration
  *           - Tampers configuration
  *           - Backup Data Registers configuration
  *           - Output Type Config configuration
  *           - Interrupts and flags management       
  *                     
  *  @verbatim
  *       
  *          ===================================================================      
  *                                    RTC Domain Reset
  *          ===================================================================          
  *          After power-on reset, the RTC domain (RTC clock source configuration,
  *          RTC registers and RTC Backup data registers) is reset. You can also
  *          reset this domain by software using the RCC_RTCResetCmd() function.        
  *         
  *          ===================================================================      
  *                                  RTC Operating Condition
  *          ===================================================================            
  *          As long as the supply voltage remains in the operating range, 
  *          the RTC never stops, regardless of the device status (Run mode, 
  *          low power modes or under reset).
  *              
  *          ===================================================================      
  *                                   RTC Domain Access
  *          ===================================================================          
  *          After reset, the RTC domain (RTC clock source configuration,
  *          RTC registers and RTC Backup data registers) are protected against 
  *          possible stray write accesses. 
  *          To enable access to the RTC Domain and RTC registers, proceed as follows:
  *            - Enable the Power Controller (PWR) APB1 interface clock using the
  *              RCC_APB1PeriphClockCmd() function.
  *            - Enable access to RTC domain using the PWR_RTCAccessCmd() function.
  *            - Select the RTC clock source using the RCC_RTCCLKConfig() function.
  *            - Enable RTC Clock using the RCC_RTCCLKCmd() function.
  *              
  *          ===================================================================      
  *                                   RTC Driver: how to use it
  *          ===================================================================          
  *            - Enable the RTC domain access (see description in the section above)
  *            - Configure the RTC Prescaler (Asynchronous and Synchronous) and
  *              RTC hour format using the RTC_Init() function.
  *                
  *          Time and Date configuration
  *          ===========================    
  *            - To configure the RTC Calendar (Time and Date) use the RTC_SetTime()
  *              and RTC_SetDate() functions.
  *            - To read the RTC Calendar, use the RTC_GetTime() and RTC_GetDate()
  *              functions.
  *            - Use the RTC_DayLightSavingConfig() function to add or sub one
  *              hour to the RTC Calendar.    
  *                
  *          Alarm configuration
  *          ===================    
  *            - To configure the RTC Alarm use the RTC_SetAlarm() function.
  *            - Enable the selected RTC Alarm using the RTC_AlarmCmd() function  
  *            - To read the RTC Alarm, use the RTC_GetAlarm() function.
  *              
  *          RTC Wakeup configuration
  *          ========================    
  *            - Configure the RTC Wakeup Clock source use the RTC_WakeUpClockConfig()
  *              function.
  *            - Configure the RTC WakeUp Counter using the RTC_SetWakeUpCounter() 
  *              function  
  *            - Enable the RTC WakeUp using the RTC_WakeUpCmd() function  
  *            - To read the RTC WakeUp Counter register, use the RTC_GetWakeUpCounter() 
  *              function.
  *                
  *          Outputs configuration
  *          =====================  
  *          The RTC has 2 different outputs:
  *            - AFO_ALARM: this output is used to manage the RTC Alarm A, Alarm B
  *              and WaKeUp signals.          
  *              To output the selected RTC signal on RTC_AF1 pin, use the 
  *              RTC_OutputConfig() function.                
  *            - AFO_CALIB: this output is used to manage the RTC Clock divided 
  *              by 64 (512Hz) signal.
  *              To output the RTC Clock on RTC_AF1 pin, use the RTC_CalibOutputCmd()
  *              function.                
  *                                                           
  *          Digital Calibration configuration
  *          =================================    
  *            - Configure the RTC Digital Calibration Value and the corresponding
  *              sign using the RTC_DigitalCalibConfig() function.
  *            - Enable the RTC Digital Calibration using the RTC_DigitalCalibCmd() 
  *              function  
  *                
  *          TimeStamp configuration
  *          =======================    
  *            - Configure the RTC_AF1 trigger and enables the RTC TimeStamp 
  *              using the RTC_TimeStampCmd() function.
  *            - To read the RTC TimeStamp Time and Date register, use the 
  *              RTC_GetTimeStamp() function.  
  *
  *          Tamper configuration
  *          ====================    
  *            - Configure the RTC Tamper trigger using the RTC_TamperConfig() 
  *              function.
  *            - Enable the RTC Tamper using the RTC_TamperCmd() function.
  *
  *          Backup Data Registers configuration
  *          ===================================    
  *            - To write to the RTC Backup Data registers, use the RTC_WriteBackupRegister()
  *              function.  
  *            - To read the RTC Backup Data registers, use the RTC_ReadBackupRegister()
  *              function.  
  *                                  
  *          ===================================================================      
  *                                  RTC and low power modes
  *          =================================================================== 
  *           The MCU can be woken up from a low power mode by an RTC alternate 
  *           function.
  *           The RTC alternate functions are the RTC alarms (Alarm A and Alarm B), 
  *           RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
  *           These RTC alternate functions can wake up the system from the Stop 
  *           and Standby lowpower modes.
  *           The system can also wake up from low power modes without depending 
  *           on an external interrupt (Auto-wakeup mode), by using the RTC alarm 
  *           or the RTC wakeup events.
  *           The RTC provides a programmable time base for waking up from the 
  *           Stop or Standby mode at regular intervals.
  *           Wakeup from STOP and Standby modes is possible only when the RTC 
  *           clock source is LSE or LSI.
  *           
  *          ===================================================================      
  *                            Selection of RTC_AF1 alternate functions
  *          ===================================================================     
  *          The RTC_AF1 pin (PC13) can be used for the following purposes:
  *            - Wakeup pin 2 (WKUP2) using the PWR_WakeUpPinCmd() function.
  *            - AFO_ALARM output       
  *            - AFO_CALIB output
  *            - AFI_TAMPER
  *            - AFI_TIMESTAMP
  *                         
  * +------------------------------------------------------------------------------------------+     
  * |     Pin         |AFO_ALARM |AFO_CALIB |AFI_TAMPER |AFI_TIMESTAMP | WKUP2  |ALARMOUTTYPE  |
  * |  configuration  | ENABLED  | ENABLED  |  ENABLED  |   ENABLED    |ENABLED |  AFO_ALARM   |
  * |  and function   |          |          |           |              |        |Configuration |
  * |-----------------|----------|----------|-----------|--------------|--------|--------------|             
  * |   Alarm out     |          |          |           |              | Don't  |              |
  * |   output OD     |     1    |    0     |Don't care | Don't care   | care   |      0       |
  * |-----------------|----------|----------|-----------|--------------|--------|--------------|  
  * |   Alarm out     |          |          |           |              | Don't  |              |
  * |   output PP     |     1    |    0     |Don't care | Don't care   | care   |      1       |
  * |-----------------|----------|----------|-----------|--------------|--------|--------------|  
  * | Calibration out |          |          |           |              | Don't  |              |
  * |   output PP     |     0    |    1     |Don't care | Don't care   | care   |  Don't care  |
  * |-----------------|----------|----------|-----------|--------------|--------|--------------|  
  * |  TAMPER input   |          |          |           |              | Don't  |              | 
  * |   floating      |     0    |    0     |     1     |      0       | care   |  Don't care  |
  * |-----------------|----------|----------|-----------|--------------|--------|--------------|  
  * |  TIMESTAMP and  |          |          |           |              | Don't  |              |
  * |  TAMPER input   |     0    |    0     |     1     |      1       | care   |  Don't care  |
  * |   floating      |          |          |           |              |        |              |                 
  * |-----------------|----------|----------|-----------|--------------|--------|--------------|  
  * | TIMESTAMP input |          |          |           |              | Don't  |              | 
  * |    floating     |     0    |    0     |     0     |      1       | care   |  Don't care  |                 
  * |-----------------|----------|----------|-----------|--------------|--------|--------------|  
  * |  Wakeup Pin 2   |     0    |    0     |     0     |      0       |   1    |  Don't care  |                 
  * |-----------------|----------|----------|-----------|--------------|--------|--------------|  
  * |  Standard GPIO  |     0    |    0     |     0     |      0       |   0    |  Don't care  |             
  * +------------------------------------------------------------------------------------------+  
  *
  *  @endverbatim
  *                      
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
  ******************************************************************************  
  */ 

/* Includes ------------------------------------------------------------------*/
#include "stm32l1xx_rtc.h"
#include "stm32l1xx_rcc.h"

/** @addtogroup STM32L1xx_StdPeriph_Driver
  * @{
  */

/** @defgroup RTC 
  * @brief RTC driver modules
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/

/* Masks Definition */
#define RTC_TR_RESERVED_MASK    ((uint32_t)0x007F7F7F)
#define RTC_DR_RESERVED_MASK    ((uint32_t)0x00FFFF3F) 
#define RTC_INIT_MASK           ((uint32_t)0xFFFFFFFF)  
#define RTC_RSF_MASK            ((uint32_t)0xFFFFFF5F)
#define RTC_FLAGS_MASK          ((uint32_t)(RTC_FLAG_TSOVF | RTC_FLAG_TSF | RTC_FLAG_WUTF | \
                                            RTC_FLAG_ALRBF | RTC_FLAG_ALRAF | RTC_FLAG_INITF | \
                                            RTC_FLAG_RSF | RTC_FLAG_INITS | RTC_FLAG_WUTWF | \
                                            RTC_FLAG_ALRBWF | RTC_FLAG_ALRAWF | RTC_FLAG_TAMP1F ))

#define INITMODE_TIMEOUT         ((uint32_t) 0x00002000)
#define SYNCHRO_TIMEOUT          ((uint32_t) 0x00001000)

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static uint8_t RTC_ByteToBcd2(uint8_t Value);
static uint8_t RTC_Bcd2ToByte(uint8_t Value);

/* Private functions ---------------------------------------------------------*/

/** @defgroup RTC_Private_Functions
  * @{
  */ 

/** @defgroup RTC_Group1 Initialization and Configuration functions
 *  @brief   Initialization and Configuration functions 
 *
@verbatim   
 ===============================================================================
                 Initialization and Configuration functions
 ===============================================================================  

  This section provide functions allowing to initialize and configure the RTC
  Prescaler (Synchronous and Asynchronous), RTC Hour format, disable RTC registers
  Write protection, enter and exit the RTC initialization mode, RTC registers
  synchronization check and reference clock detection enable.
  
  1. The RTC Prescaler is programmed to generate the RTC 1Hz time base. It is
     split into 2 programmable prescalers to minimize power consumption.
     - A 7-bit asynchronous prescaler and A 13-bit synchronous prescaler.
     - When both prescalers are used, it is recommended to configure the asynchronous
       prescaler to a high value to minimize consumption.

  2. All RTC registers are Write protected. Writing to the RTC registers
     is enabled by writing a key into the Write Protection register, RTC_WPR.

  3. To Configure the RTC Calendar, user application should enter initialization
     mode. In this mode, the calendar counter is stopped and its value can be 
     updated. When the initialization sequence is complete, the calendar restarts 
     counting after 4 RTCCLK cycles.

  4. To read the calendar through the shadow registers after Calendar initialization,
     calendar update or after wakeup from low power modes the software must first 
     clear the RSF flag. The software must then wait until it is set again before 
     reading the calendar, which means that the calendar registers have been 
     correctly copied into the RTC_TR and RTC_DR shadow registers.
     The RTC_WaitForSynchro() function implements the above software sequence 
     (RSF clear and RSF check).

@endverbatim
  * @{
  */

/**
  * @brief  Deinitializes the RTC registers to their default reset values.
  * @note   This function doesn't reset the RTC Clock source and RTC Backup Data
  *         registers.       
  * @param  None
  * @retval An ErrorStatus enumeration value:
  *          - SUCCESS: RTC registers are deinitialized
  *          - ERROR: RTC registers are not deinitialized
  */
ErrorStatus RTC_DeInit(void)
{
  __IO uint32_t wutcounter = 0x00;
  uint32_t wutwfstatus = 0x00;
  ErrorStatus status = ERROR;
  
  /* Disable the write protection for RTC registers */
  RTC->WPR = 0xCA;
  RTC->WPR = 0x53;

  /* Set Initialization mode */
  if (RTC_EnterInitMode() == ERROR)
  {
    status = ERROR;
  }  
  else
  {
    /* Reset TR, DR and CR registers */
    RTC->TR = (uint32_t)0x00000000;
    RTC->DR = (uint32_t)0x00002101;
    /* Reset All CR bits except CR[2:0] */
    RTC->CR &= (uint32_t)0x00000007;
  
    /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
    do
    {
      wutwfstatus = RTC->ISR & RTC_ISR_WUTWF;
      wutcounter++;  
    } while((wutcounter != INITMODE_TIMEOUT) && (wutwfstatus == 0x00));
    
    if ((RTC->ISR & RTC_ISR_WUTWF) == RESET)
    {
      status = ERROR;
    }
    else
    {
      /* Reset all RTC CR register bits */
      RTC->CR &= (uint32_t)0x00000000;
      RTC->WUTR = (uint32_t)0x0000FFFF;
      RTC->PRER = (uint32_t)0x007F00FF;
      RTC->CALIBR = (uint32_t)0x00000000;
      RTC->ALRMAR = (uint32_t)0x00000000;        
      RTC->ALRMBR = (uint32_t)0x00000000;
      
      /* Reset ISR register and exit initialization mode */
      RTC->ISR = (uint32_t)0x00000000;
      
      /* Reset Tamper and alternate functions configuration register */
      RTC->TAFCR = 0x00000000;
  
      if(RTC_WaitForSynchro() == ERROR)
      {
        status = ERROR;
      }
      else
      {
        status = SUCCESS;      
      }
    }
  }
  
  /* Enable the write protection for RTC registers */
  RTC->WPR = 0xFF;  
  
  return status;
}

/**
  * @brief  Initializes the RTC registers according to the specified parameters 
  *         in RTC_InitStruct.
  * @param  RTC_InitStruct: pointer to a RTC_InitTypeDef structure that contains 
  *         the configuration information for the RTC peripheral.
  * @note   The RTC Prescaler register is write protected and can be written in 
  *         initialization mode only.  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -