📄 stm32l1xx_rtc.c
字号:
[..] This section provide functions allowing to program and read the RTC WakeUp.
@endverbatim
* @{
*/
/**
* @brief Configures the RTC Wakeup clock source.
* @note The WakeUp Clock source can only be changed when the RTC WakeUp
* is disabled (Use the RTC_WakeUpCmd(DISABLE)).
* @param RTC_WakeUpClock: Wakeup Clock source.
* This parameter can be one of the following values:
* @arg RTC_WakeUpClock_RTCCLK_Div16: RTC Wakeup Counter Clock = RTCCLK/16.
* @arg RTC_WakeUpClock_RTCCLK_Div8: RTC Wakeup Counter Clock = RTCCLK/8.
* @arg RTC_WakeUpClock_RTCCLK_Div4: RTC Wakeup Counter Clock = RTCCLK/4.
* @arg RTC_WakeUpClock_RTCCLK_Div2: RTC Wakeup Counter Clock = RTCCLK/2.
* @arg RTC_WakeUpClock_CK_SPRE_16bits: RTC Wakeup Counter Clock = CK_SPRE.
* @arg RTC_WakeUpClock_CK_SPRE_17bits: RTC Wakeup Counter Clock = CK_SPRE.
* @retval None
*/
void RTC_WakeUpClockConfig(uint32_t RTC_WakeUpClock)
{
/* Check the parameters */
assert_param(IS_RTC_WAKEUP_CLOCK(RTC_WakeUpClock));
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
/* Clear the Wakeup Timer clock source bits in CR register */
RTC->CR &= (uint32_t)~RTC_CR_WUCKSEL;
/* Configure the clock source */
RTC->CR |= (uint32_t)RTC_WakeUpClock;
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
}
/**
* @brief Configures the RTC Wakeup counter.
* @note The RTC WakeUp counter can only be written when the RTC WakeUp.
* is disabled (Use the RTC_WakeUpCmd(DISABLE)).
* @param RTC_WakeUpCounter: specifies the WakeUp counter.
* This parameter can be a value from 0x0000 to 0xFFFF.
* @retval None
*/
void RTC_SetWakeUpCounter(uint32_t RTC_WakeUpCounter)
{
/* Check the parameters */
assert_param(IS_RTC_WAKEUP_COUNTER(RTC_WakeUpCounter));
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
/* Configure the Wakeup Timer counter */
RTC->WUTR = (uint32_t)RTC_WakeUpCounter;
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
}
/**
* @brief Returns the RTC WakeUp timer counter value.
* @param None
* @retval The RTC WakeUp Counter value.
*/
uint32_t RTC_GetWakeUpCounter(void)
{
/* Get the counter value */
return ((uint32_t)(RTC->WUTR & RTC_WUTR_WUT));
}
/**
* @brief Enables or Disables the RTC WakeUp timer.
* @param NewState: new state of the WakeUp timer.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
ErrorStatus RTC_WakeUpCmd(FunctionalState NewState)
{
__IO uint32_t wutcounter = 0x00;
uint32_t wutwfstatus = 0x00;
ErrorStatus status = ERROR;
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
if (NewState != DISABLE)
{
/* Enable the Wakeup Timer */
RTC->CR |= (uint32_t)RTC_CR_WUTE;
status = SUCCESS;
}
else
{
/* Disable the Wakeup Timer */
RTC->CR &= (uint32_t)~RTC_CR_WUTE;
/* 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
{
status = SUCCESS;
}
}
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
return status;
}
/**
* @}
*/
/** @defgroup RTC_Group5 Daylight Saving configuration functions
* @brief Daylight Saving configuration functions
*
@verbatim
===============================================================================
##### Daylight Saving configuration functions #####
===============================================================================
[..] This section provide functions allowing to configure the RTC DayLight Saving.
@endverbatim
* @{
*/
/**
* @brief Adds or substract one hour from the current time.
* @param RTC_DayLightSaveOperation: the value of hour adjustment.
* This parameter can be one of the following values:
* @arg RTC_DayLightSaving_SUB1H: Substract one hour (winter time).
* @arg RTC_DayLightSaving_ADD1H: Add one hour (summer time).
* @param RTC_StoreOperation: Specifies the value to be written in the BCK bit
* in CR register to store the operation.
* This parameter can be one of the following values:
* @arg RTC_StoreOperation_Reset: BCK Bit Reset.
* @arg RTC_StoreOperation_Set: BCK Bit Set.
* @retval None
*/
void RTC_DayLightSavingConfig(uint32_t RTC_DayLightSaving, uint32_t RTC_StoreOperation)
{
/* Check the parameters */
assert_param(IS_RTC_DAYLIGHT_SAVING(RTC_DayLightSaving));
assert_param(IS_RTC_STORE_OPERATION(RTC_StoreOperation));
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
/* Clear the bits to be configured */
RTC->CR &= (uint32_t)~(RTC_CR_BCK);
/* Configure the RTC_CR register */
RTC->CR |= (uint32_t)(RTC_DayLightSaving | RTC_StoreOperation);
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
}
/**
* @brief Returns the RTC Day Light Saving stored operation.
* @param None
* @retval RTC Day Light Saving stored operation.
* - RTC_StoreOperation_Reset
* - RTC_StoreOperation_Set
*/
uint32_t RTC_GetStoreOperation(void)
{
return (RTC->CR & RTC_CR_BCK);
}
/**
* @}
*/
/** @defgroup RTC_Group6 Output pin Configuration function
* @brief Output pin Configuration function
*
@verbatim
===============================================================================
##### Output pin Configuration function #####
===============================================================================
[..] This section provide functions allowing to configure the RTC Output source.
@endverbatim
* @{
*/
/**
* @brief Configures the RTC output source (AFO_ALARM).
* @param RTC_Output: Specifies which signal will be routed to the RTC output.
* This parameter can be one of the following values:
* @arg RTC_Output_Disable: No output selected
* @arg RTC_Output_AlarmA: signal of AlarmA mapped to output.
* @arg RTC_Output_AlarmB: signal of AlarmB mapped to output.
* @arg RTC_Output_WakeUp: signal of WakeUp mapped to output.
* @param RTC_OutputPolarity: Specifies the polarity of the output signal.
* This parameter can be one of the following:
* @arg RTC_OutputPolarity_High: The output pin is high when the
* ALRAF/ALRBF/WUTF is high (depending on OSEL).
* @arg RTC_OutputPolarity_Low: The output pin is low when the
* ALRAF/ALRBF/WUTF is high (depending on OSEL).
* @retval None
*/
void RTC_OutputConfig(uint32_t RTC_Output, uint32_t RTC_OutputPolarity)
{
/* Check the parameters */
assert_param(IS_RTC_OUTPUT(RTC_Output));
assert_param(IS_RTC_OUTPUT_POL(RTC_OutputPolarity));
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
/* Clear the bits to be configured */
RTC->CR &= (uint32_t)~(RTC_CR_OSEL | RTC_CR_POL);
/* Configure the output selection and polarity */
RTC->CR |= (uint32_t)(RTC_Output | RTC_OutputPolarity);
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
}
/**
* @}
*/
/** @defgroup RTC_Group7 Coarse and Smooth Calibrations configuration functions
* @brief Coarse and Smooth Calibrations configuration functions
*
@verbatim
===============================================================================
##### Coarse and Smooth Calibrations configuration functions #####
===============================================================================
@endverbatim
* @{
*/
/**
* @brief Configures the Coarse Calibration parameters.
* @param RTC_CalibSign: specifies the sign of the calibration value.
* This parameter can be one of the following values:
* @arg RTC_CalibSign_Positive: The value sign is positive.
* @arg RTC_CalibSign_Negative: The value sign is negative.
* @param Value: value of calibration expressed in ppm (coded on 5 bits)
* This value should be between 0 and 63 when using negative sign
* with a 2-ppm step.
* This value should be between 0 and 126 when using positive sign
* with a 4-ppm step.
* @retval An ErrorStatus enumeration value:
* - SUCCESS: RTC Coarse calibration are initialized
* - ERROR: RTC Coarse calibration are not initialized
*/
ErrorStatus RTC_CoarseCalibConfig(uint32_t RTC_CalibSign, uint32_t Value)
{
ErrorStatus status = ERROR;
/* Check the parameters */
assert_param(IS_RTC_CALIB_SIGN(RTC_CalibSign));
assert_param(IS_RTC_CALIB_VALUE(Value));
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
/* Set Initialization mode */
if (RTC_EnterInitMode() == ERROR)
{
status = ERROR;
}
else
{
/* Set the coarse calibration value */
RTC->CALIBR = (uint32_t)(RTC_CalibSign | Value);
/* Exit Initialization mode */
RTC_ExitInitMode();
status = SUCCESS;
}
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
return status;
}
/**
* @brief Enables or disables the Coarse calibration process.
* @param NewState: new state of the Coarse calibration.
* This parameter can be: ENABLE or DISABLE.
* @retval An ErrorStatus enumeration value:
* - SUCCESS: RTC Coarse calibration are enabled/disabled
* - ERROR: RTC Coarse calibration are not enabled/disabled
*/
ErrorStatus RTC_CoarseCalibCmd(FunctionalState NewState)
{
ErrorStatus status = ERROR;
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
/* Set Initialization mode */
if (RTC_EnterInitMode() == ERROR)
{
status = ERROR;
}
else
{
if (NewState != DISABLE)
{
/* Enable the Coarse Calibration */
RTC->CR |= (uint32_t)RTC_CR_DCE;
}
else
{
/* Disable the Coarse Calibration */
RTC->CR &= (uint32_t)~RTC_CR_DCE;
}
/* Exit Initialization mode */
RTC_ExitInitMode();
status = SUCCESS;
}
/* Enable the write protection for RTC registers */
RTC->WPR = 0xF
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -