📄 stm32f0xx_rtc.c
字号:
/**
******************************************************************************
* @file stm32f0xx_rtc.c
* @author MCD Application Team
* @version V1.0.0
* @date 23-March-2012
* @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) configuration
* + Daylight Saving configuration
* + Output pin Configuration
* + Digital Calibration configuration
* + TimeStamp configuration
* + Tampers configuration
* + Backup Data Registers configuration
* + Output Type Config configuration
* + Shift control synchronisation
* + Interrupts and flags management
*
@verbatim
===============================================================================
##### Backup Domain Operating Condition #####
===============================================================================
[..] The real-time clock (RTC) and the RTC backup registers can be powered
from the VBAT voltage when the main VDD supply is powered off.
To retain the content of the RTC backup registers 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:
(#) The RTC
(#) The LSE oscillator
(#) PC13 to PC15 I/Os I/Os (when available)
[..] When the backup domain is supplied by VDD (analog switch connected
to VDD), the following functions are available:
(#) PC14 and PC15 can be used as either GPIO or LSE pins
(#) PC13 can be used as a GPIO or as the RTC_AF1 pin
[..] When the backup domain is supplied by VBAT (analog switch connected
to VBAT because VDD is not present), the following functions are available:
(#) PC14 and PC15 can be used as LSE pins only
(#) PC13 can be used as the RTC_AF1 pin
##### Backup Domain Reset #####
===============================================================================
[..] The backup domain reset sets all RTC registers and the RCC_BDCR
register to their reset values.
A backup domain reset is generated when one of the following events
occurs:
(#) Software reset, triggered by setting the BDRST bit in the
RCC Backup domain control register (RCC_BDCR). You can use the
RCC_BackupResetCmd().
(#) VDD or VBAT power on, if both supplies have previously been
powered off.
##### Backup Domain Access #####
===============================================================================
[..] After reset, the backup domain (RTC registers and RTC backup data
registers) is protected against possible unwanted write accesses.
[..] To enable access to the Backup Domain and RTC registers, proceed as follows:
(#) Enable the Power Controller (PWR) APB1 interface clock using the
RCC_APB1PeriphClockCmd() function.
(#) Enable access to Backup domain using the PWR_BackupAccessCmd() function.
(#) Select the RTC clock source using the RCC_RTCCLKConfig() function.
(#) Enable RTC Clock using the RCC_RTCCLKCmd() function.
##### How to use this driver #####
===============================================================================
[..]
(+) Enable the backup 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.
(+) To read the RTC subsecond, use the RTC_GetSubSecond() function.
(+) 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.
(+) To read the RTC alarm SubSecond, use the RTC_GetAlarmSubSecond() function.
***Outputs configuration ***
============================
[..] The RTC has 2 different outputs:
(+) AFO_ALARM: this output is used to manage the RTC Alarm A.
To output the selected RTC signal on RTC_AF1 pin, use the
RTC_OutputConfig() function.
(+) AFO_CALIB: this output is 512Hz signal or 1Hz .
To output the RTC Clock on RTC_AF1 pin, use the RTC_CalibOutputCmd()
function.
***Original Digital Calibration configuration ***
=================================
[..] Configure the RTC Original Digital Calibration Value and the corresponding
calibration cycle period (32s,16s and 8s) using the RTC_SmoothCalibConfig()
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.
(+) To read the RTC TimeStamp SubSecond register, use the
RTC_GetTimeStampSubSecond() function.
***Tamper configuration ***
===========================
[..]
(+) Configure the Tamper filter count using RTC_TamperFilterConfig()
function.
(+) Configure the RTC Tamper trigger Edge or Level according to the Tamper
filter (if equal to 0 Edge else Level) value using the RTC_TamperConfig() function
(+) Configure the Tamper sampling frequency using RTC_TamperSamplingFreqConfig()
function.
(+) Configure the Tamper precharge or discharge duration using
RTC_TamperPinsPrechargeDuration() function.
(+) Enable the Tamper Pull-UP using RTC_TamperPullUpDisableCmd() function.
(+) Enable the RTC Tamper using the RTC_TamperCmd() function.
(+) Enable the Time stamp on Tamper detection event using
RTC_TSOnTamperDetecCmd() 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 alarm (Alarm A), 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 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 | 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
*
* <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_rtc.h"
/** @addtogroup STM32F0xx_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_ALRAF | \
RTC_FLAG_RSF | RTC_FLAG_INITS |RTC_FLAG_INITF | \
RTC_FLAG_TAMP1F | RTC_FLAG_TAMP2F | RTC_FLAG_TAMP3F | \
RTC_FLAG_RECALPF | RTC_FLAG_SHPF))
#define INITMODE_TIMEOUT ((uint32_t) 0x00004000)
#define SYNCHRO_TIMEOUT ((uint32_t) 0x00008000)
#define RECALPF_TIMEOUT ((uint32_t) 0x00001000)
#define SHPF_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.
(#) 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.
(#) All RTC registers are Write protected. Writing to the RTC registers
is enabled by writing a key into the Write Protection register, RTC_WPR.
(#) 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.
(#) 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;
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;
RTC->CR &= (uint32_t)0x00000000;
RTC->PRER = (uint32_t)0x007F00FF;
RTC->ALRMAR = (uint32_t)0x00000000;
RTC->SHIFTR = (uint32_t)0x00000000;
RTC->CAL = (uint32_t)0x00000000;
RTC->ALRMASSR = (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;
/* Wait till the RTC RSF flag is set */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -