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

📄 stm32f2xx_rtc.c

📁 STM32F2xx 标准固件库
💻 C
📖 第 1 页 / 共 5 页
字号:
/**
  ******************************************************************************
  * @file    stm32f2xx_rtc.c
  * @author  MCD Application Team
  * @version V1.0.0
  * @date    18-April-2011
  * @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
  *           - Coarse Calibration configuration
  *           - TimeStamp configuration
  *           - Tampers configuration
  *           - Backup Data Registers configuration
  *           - RTC Tamper and TimeStamp Pins Selection and Output Type Config configuration
  *           - Interrupts and flags management
  *
  *  @verbatim
  *
  *          ===================================================================
  *                               Backup Domain Operating Condition
  *          ===================================================================
  *          The real-time clock (RTC), the RTC backup registers, and the backup 
  *          SRAM (BKP SRAM) can be powered from the VBAT voltage when the main 
  *          VDD supply is powered off.
  *          To retain the content of the RTC backup registers, backup SRAM, 
  *          and supply the RTC when VDD is turned off, VBAT pin can be connected 
  *          to an optional standby voltage supplied by a battery or by another 
  *          source.
  *
  *          To allow the RTC to operate even when the main digital supply (VDD) 
  *          is turned off, the VBAT pin powers the following blocks:
  *            1 - The RTC
  *            2 - The LSE oscillator
  *            3 - The backup SRAM when the low power backup regulator is enabled
  *            4 - PC13 to PC15 I/Os, plus PI8 I/O (when available)
  *
  *          When the backup domain is supplied by VDD (analog switch connected 
  *          to VDD), the following functions are available:
  *            1 - PC14 and PC15 can be used as either GPIO or LSE pins
  *            2 - PC13 can be used as a GPIO or as the RTC_AF1 pin
  *            3 - PI8 can be used as a GPIO or as the RTC_AF2 pin
  *
  *          When the backup domain is supplied by VBAT (analog switch connected 
  *          to VBAT because VDD is not present), the following functions are available:
  *            1 - PC14 and PC15 can be used as LSE pins only
  *            2 - PC13 can be used as the RTC_AF1 pin 
  *            3 - PI8 can be used as the RTC_AF2 pin
  *
  *          ===================================================================
  *                                    Backup Domain Reset
  *          ===================================================================
  *          The backup domain reset sets all RTC registers and the RCC_BDCR 
  *          register to their reset values. The BKPSRAM is not affected by this
  *          reset. The only way of resetting the BKPSRAM is through the Flash 
  *          interface by requesting a protection level change from 1 to 0.
  *          A backup domain reset is generated when one of the following events
  *          occurs:
  *            1 - Software reset, triggered by setting the BDRST bit in the 
  *                RCC Backup domain control register (RCC_BDCR). You can use the
  *                RCC_BackupResetCmd().
  *            2 - VDD or VBAT power on, if both supplies have previously been
  *                powered off.
  *
  *          ===================================================================
  *                                   Backup Domain Access
  *          ===================================================================
  *          After reset, the backup domain (RTC registers, RTC backup data 
  *          registers and backup SRAM) is protected against possible unwanted 
  *          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_BackupAccessCmd() 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.
  *
  *          Coarse Calibration configuration
  *          =================================
  *            - Configure the RTC Coarse Calibration Value and the corresponding
  *              sign using the RTC_CoarseCalibConfig() function.
  *            - Enable the RTC Coarse Calibration using the RTC_CoarseCalibCmd() 
  *              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.
  *            - The TAMPER1 alternate function can be mapped either to RTC_AF1(PC13)
  *              or RTC_AF2 (PI8) depending on the value of TAMP1INSEL bit in 
  *              RTC_TAFCR register. You can use the  RTC_TamperPinSelection()
  *              function to select the corresponding pin.     
  *
  *          Tamper configuration
  *          ====================
  *            - Configure the RTC Tamper trigger using the RTC_TamperConfig() 
  *              function.
  *            - Enable the RTC Tamper using the RTC_TamperCmd() function.
  *            - The TIMESTAMP alternate function can be mapped to either RTC_AF1 
  *              or RTC_AF2 depending on the value of the TSINSEL bit in the 
  *              RTC_TAFCR register. You can use the  RTC_TimeStampPinSelection()
  *              function to select the corresponding pin.   
  *
  *          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:
  *            - AFO_ALARM output
  *            - AFO_CALIB output
  *            - AFI_TAMPER
  *            - AFI_TIMESTAMP
  *
  * +-------------------------------------------------------------------------------------------------------------+
  * |     Pin         |AFO_ALARM |AFO_CALIB |AFI_TAMPER |AFI_TIMESTAMP | TAMP1INSEL |   TSINSEL    |ALARMOUTTYPE  |
  * |  configuration  | ENABLED  | ENABLED  |  ENABLED  |   ENABLED    |TAMPER1 pin |TIMESTAMP pin |  AFO_ALARM   |
  * |  and function   |          |          |           |              | selection  |  selection   |Configuration |
  * |-----------------|----------|----------|-----------|--------------|------------|--------------|--------------|
  * |   Alarm out     |          |          |           |              |    Don't   |     Don't    |              |
  * |   output OD     |     1    |Don't care|Don't care | Don't care   |    care    |     care     |      0       |
  * |-----------------|----------|----------|-----------|--------------|------------|--------------|--------------|
  * |   Alarm out     |          |          |           |              |    Don't   |     Don't    |              |
  * |   output PP     |     1    |Don't care|Don't care | Don't care   |    care    |     care     |      1       |
  * |-----------------|----------|----------|-----------|--------------|------------|--------------|--------------|
  * | Calibration out |          |          |           |              |    Don't   |     Don't    |              |
  * |   output PP     |     0    |    1     |Don't care | Don't care   |    care    |     care     |  Don't care  |
  * |-----------------|----------|----------|-----------|--------------|------------|--------------|--------------|
  * |  TAMPER input   |          |          |           |              |            |     Don't    |              |
  * |   floating      |     0    |    0     |     1     |      0       |      0     |     care     |  Don't care  |
  * |-----------------|----------|----------|-----------|--------------|------------|--------------|--------------|
  * |  TIMESTAMP and  |          |          |           |              |            |              |              |
  * |  TAMPER input   |     0    |    0     |     1     |      1       |      0     |      0       |  Don't care  |
  * |   floating      |          |          |           |              |            |              |              |
  * |-----------------|----------|----------|-----------|--------------|------------|--------------|--------------|
  * | TIMESTAMP input |          |          |           |              |    Don't   |              |              |
  * |    floating     |     0    |    0     |     0     |      1       |    care    |      0       |  Don't care  |
  * |-----------------|----------|----------|-----------|--------------|------------|--------------|--------------|
  * |  Standard GPIO  |     0    |    0     |     0     |      0       | Don't care |  Don't care  |  Don't care  |
  * +-------------------------------------------------------------------------------------------------------------+
  *
  *
  *          ===================================================================
  *                            Selection of RTC_AF2 alternate functions
  *          ===================================================================
  *          The RTC_AF2 pin (PI8) can be used for the following purposes:
  *            - AFI_TAMPER
  *            - AFI_TIMESTAMP
  *
  * +---------------------------------------------------------------------------------------+
  * |     Pin         |AFI_TAMPER |AFI_TIMESTAMP | TAMP1INSEL |   TSINSEL    |ALARMOUTTYPE  |
  * |  configuration  |  ENABLED  |   ENABLED    |TAMPER1 pin |TIMESTAMP pin |  AFO_ALARM   |
  * |  and function   |           |              | selection  |  selection   |Configuration |
  * |-----------------|-----------|--------------|------------|--------------|--------------|
  * |  TAMPER input   |           |              |            |     Don't    |              |
  * |   floating      |     1     |      0       |      1     |     care     |  Don't care  |
  * |-----------------|-----------|--------------|------------|--------------|--------------|
  * |  TIMESTAMP and  |           |              |            |              |              |
  * |  TAMPER input   |     1     |      1       |      1     |      1       |  Don't care  |
  * |   floating      |           |              |            |              |              |
  * |-----------------|-----------|--------------|------------|--------------|--------------|
  * | TIMESTAMP input |           |              |    Don't   |              |              |
  * |    floating     |     0     |      1       |    care    |      1       |  Don't care  |
  * |-----------------|-----------|--------------|------------|--------------|--------------|
  * |  Standard GPIO  |     0     |      0       | Don't care |  Don't care  |  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 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "stm32f2xx_rtc.h"
#include "stm32f2xx_rcc.h"

/** @addtogroup STM32F2xx_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) 0x00010000)
#define SYNCHRO_TIMEOUT          ((uint32_t) 0x00008000)

/* 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

⌨️ 快捷键说明

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