📄 stm8l15x_rtc.c
字号:
/**
******************************************************************************
* @file stm8l15x_rtc.c
* @author MCD Application Team
* @version V1.5.0
* @date 13-May-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
* - Alarm configuration
* - WakeUp Timer configuration
* - Daylight Saving configuration
* - Output pin Configuration
* - Smooth Calibration configuration
* - Tampers configuration
* - Output Type configuration
* - Interrupts and flags management
*
* @verbatim
*
* ===================================================================
* RTC Domain Reset
* ===================================================================
* After power-on reset, the RTC domain (RTC registers) is reset.
*
* ===================================================================
* 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 registers) is protected against
* possible stray write accesses.
* To enable access to the RTC registers, proceed as follows:
* - Select the RTC clock source using the CLK_RTCClockConfig()
* function : HSE, HSI, LSE or LSI.
* CLK_RTCClockConfig(CLK_RTCCLKSource_HSI, CLK_RTCCLKDiv_1)
* - Enable RTC Clock using the CLK_PeripheralClockConfig() function
* : CLK_PeripheralClockConfig(CLK_Peripheral_RTC, ENABLE).
*
* ===================================================================
* 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 1hour
* 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 and
* WaKeUp signals.
* To output the selected RTC signal on RTC_ALARM pin, use the
* RTC_OutputConfig() function.
* - AFO_CALIB: this output is used to manage the RTC Clock divided
* by 64 (512Hz) signal or divided by 32768 (1Hz) which can be
* configured using RTC_CalibOutputConfig() function.
* To output the RTC Clock on RTC_CALIB pin, use the
* RTC_CalibOutputCmd() function.
*
* Smooth Calibration configuration
* =================================
* - Configure the RTC Smooth Calibration Value and the corresponding
* sign using the RTC_SmoothCalibConfig() function.
*
*
* Tamper configuration
* ====================
* - Configure the RTC Tamper Level using the RTC_TamperConfig()
* function.
* - Enable the RTC Tamper using the RTC_TamperCmd() 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,
* RTC wakeup and RTC tamper event detection.
* These RTC alternate functions can wake up the system from the
* 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
* halt or wfe or wfi modes at regular intervals.
*
*
* @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>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm8l15x_rtc.h"
/** @addtogroup STM8L15x_StdPeriph_Driver
* @{
*/
/** @defgroup RTC
* @brief RTC driver modules
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define INIT_TIMEOUT ((uint16_t)0xFFFF)
#define RSF_TIMEOUT ((uint16_t)0xFFFF)
#define INITF_TIMEOUT ((uint16_t)0xFFFF)
#define WUTWF_TIMEOUT ((uint16_t)0xFFFF)
#define ALRAWF_TIMEOUT ((uint16_t)0xFFFF)
#define RECALPF_TIMEOUT ((uint16_t)0xFFFF)
#define SHPF_TIMEOUT ((uint16_t)0xFFFF)
#define TEN_VALUE_BCD ((uint8_t)0x10)
#define TEN_VALUE_BIN ((uint8_t)0x0A)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/** @defgroup RTC_Private_Functions_Prototype
* @{
*/
static uint8_t ByteToBcd2(uint8_t Value);
static uint8_t Bcd2ToByte(uint8_t Value);
/**
* @}
*/
/** @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 prescaler to minimize power consumption.
- A 7-bit asynchronous prescaler and A 13-bit or 15-bit synchronous
prescaler depending to density of the mcu package.
- When both prescaler 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.
* @param None
* @retval An ErrorStatus enumeration value:
* - SUCCESS: RTC registers are deinitialized
* - ERROR: RTC registers are not deinitialized
*/
ErrorStatus RTC_DeInit(void)
{
ErrorStatus status = ERROR;
uint16_t wutwfcount = 0;
uint16_t recalpfcount = 0;
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
/* Set Initialization mode */
if (RTC_EnterInitMode() == ERROR)
{
status = ERROR;
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
}
else
{
/* Reset TR registers */
RTC->TR1 = RTC_TR1_RESET_VALUE;
RTC->TR2 = RTC_TR2_RESET_VALUE;
RTC->TR3 = RTC_TR3_RESET_VALUE;
/* Reset DR registers */
RTC->DR1 = RTC_DR1_RESET_VALUE;
RTC->DR2 = RTC_DR2_RESET_VALUE;
RTC->DR3 = RTC_DR3_RESET_VALUE;
/* Reset SPER & ARPER registers */
RTC->SPRERH = RTC_SPRERH_RESET_VALUE;
RTC->SPRERL = RTC_SPRERL_RESET_VALUE;
RTC->APRER = RTC_APRER_RESET_VALUE;
RTC->TCR1 = RTC_TCR1_RESET_VALUE;
RTC->TCR2 = RTC_TCR2_RESET_VALUE;
/* Reset All CR1 bits except CR1[2:0] */
RTC->CR1 = RTC_CR1_RESET_VALUE;
RTC->CR2 = RTC_CR2_RESET_VALUE;
RTC->CR3 = RTC_CR3_RESET_VALUE;
/* Wait till RTC WUTWF flag is set or if Time out is reached exit */
while (((RTC->ISR1 & RTC_ISR1_WUTWF) == RESET) && ( wutwfcount != WUTWF_TIMEOUT))
{
wutwfcount++;
RTC->ISR1 = 0;
}
if ((RTC->ISR1 & RTC_ISR1_WUTWF) == RESET)
{
status = ERROR;
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
}
else
{
/* Reset All CR1 bits */
RTC->CR1 = RTC_CR1_RESET_VALUE;
/* Reset WUTR registers */
RTC->WUTRH = RTC_WUTRH_RESET_VALUE;
RTC->WUTRL = RTC_WUTRL_RESET_VALUE;
/* Reset ALARM registers */
RTC->ALRMAR1 = RTC_ALRMAR1_RESET_VALUE;
RTC->ALRMAR2 = RTC_ALRMAR2_RESET_VALUE;
RTC->ALRMAR3 = RTC_ALRMAR3_RESET_VALUE;
RTC->ALRMAR4 = RTC_ALRMAR4_RESET_VALUE;
RTC->ALRMASSRH = RTC_ALRMASSRH_RESET_VALUE;
RTC->ALRMASSRL = RTC_ALRMASSRL_RESET_VALUE;
RTC->ALRMASSMSKR = RTC_ALRMASSMSKR_RESET_VALUE;
/* Reset ISR register and exit initialization mode */
RTC->ISR1 = (uint8_t)0x00;
RTC->ISR2 = RTC_ISR2_RESET_VALUE;
if ((RTC->ISR1 & RTC_ISR1_RECALPF) != RESET)
{
while (((RTC->ISR1 & RTC_ISR1_RECALPF) != RESET) && (recalpfcount != RECALPF_TIMEOUT))
{
recalpfcount++;
}
}
if ((RTC->ISR1 & RTC_ISR1_RECALPF) == RESET)
{
RTC->CALRH = RTC_CALRH_RESET_VALUE;
RTC->CALRL = RTC_CALRL_RESET_VALUE;
if (RTC_WaitForSynchro() == ERROR)
{
status = ERROR;
}
else
{
status = SUCCESS;
}
}
else
{
status = ERROR;
}
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
}
}
/* return Deinitialize RTC registers status*/
return (ErrorStatus)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.
* @retval An ErrorStatus enumeration value:
* - SUCCESS: RTC registers are initialized
* - ERROR: RTC registers are not initialized
*/
ErrorStatus RTC_Init(RTC_InitTypeDef* RTC_InitStruct)
{
ErrorStatus status = ERROR;
/* Check the parameters */
assert_param(IS_RTC_HOUR_FORMAT(RTC_InitStruct->RTC_HourFormat));
assert_param(IS_RTC_ASYNCH_PREDIV(RTC_InitStruct->RTC_AsynchPrediv));
assert_param(IS_RTC_SYNCH_PREDIV(RTC_InitStruct->RTC_SynchPrediv));
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
/* Set Initialization mode */
if (RTC_EnterInitMode() == ERROR)
{
status = ERROR;
}
else
{
/* Clear the bits to be configured first */
RTC->CR1 &= ((uint8_t)~( RTC_CR1_FMT ));
/* Set RTC_CR1 register */
RTC->CR1 |= ((uint8_t)(RTC_InitStruct->RTC_HourFormat));
/* Set Prescalers registers */
RTC->SPRERH = (uint8_t)(RTC_InitStruct->RTC_SynchPrediv >> 8);
RTC->SPRERL = (uint8_t)(RTC_InitStruct->RTC_SynchPrediv);
RTC->APRER = (uint8_t)(RTC_InitStruct->RTC_AsynchPrediv);
/* Exit Initialization mode */
RTC->ISR1 &= (uint8_t)~RTC_ISR1_INIT;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -