📄 stm32l1xx_rtc.c
字号:
* 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 Digital Calibration configuration functions
* @brief Digital Calibration configuration functions
*
@verbatim
===============================================================================
Digital Calibration configuration functions
===============================================================================
@endverbatim
* @{
*/
/**
* @brief Configures the digital 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 digital calibration are initialized
* - ERROR: RTC digital calibration are not initialized
*/
ErrorStatus RTC_DigitalCalibConfig(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 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 digital calibration process.
* @param NewState: new state of the digital calibration.
* This parameter can be: ENABLE or DISABLE.
* @retval An ErrorStatus enumeration value:
* - SUCCESS: RTC digital calibration are enabled/disabled
* - ERROR: RTC digital calibration are not enabled/disabled
*/
ErrorStatus RTC_DigitalCalibCmd(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 Digital Calibration */
RTC->CR |= (uint32_t)RTC_CR_DCE;
}
else
{
/* Disable the Digital Calibration */
RTC->CR &= (uint32_t)~RTC_CR_DCE;
}
/* Exit Initialization mode */
RTC_ExitInitMode();
status = SUCCESS;
}
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
return status;
}
/**
* @brief Enables or disables the RTC clock to be output through the relative
* pin.
* @param NewState: new state of the digital calibration Output.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void RTC_CalibOutputCmd(FunctionalState NewState)
{
/* 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 RTC clock output */
RTC->CR |= (uint32_t)RTC_CR_COE;
}
else
{
/* Disable the RTC clock output */
RTC->CR &= (uint32_t)~RTC_CR_COE;
}
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
}
/**
* @}
*/
/** @defgroup RTC_Group8 TimeStamp configuration functions
* @brief TimeStamp configuration functions
*
@verbatim
===============================================================================
TimeStamp configuration functions
===============================================================================
@endverbatim
* @{
*/
/**
* @brief Enables or Disables the RTC TimeStamp functionality with the
* specified time stamp pin stimulating edge.
* @param RTC_TimeStampEdge: Specifies the pin edge on which the TimeStamp is
* activated.
* This parameter can be one of the following:
* @arg RTC_TimeStampEdge_Rising: the Time stamp event occurs on the rising
* edge of the related pin.
* @arg RTC_TimeStampEdge_Falling: the Time stamp event occurs on the
* falling edge of the related pin.
* @param NewState: new state of the TimeStamp.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void RTC_TimeStampCmd(uint32_t RTC_TimeStampEdge, FunctionalState NewState)
{
uint32_t tmpreg = 0;
/* Check the parameters */
assert_param(IS_RTC_TIMESTAMP_EDGE(RTC_TimeStampEdge));
assert_param(IS_FUNCTIONAL_STATE(NewState));
/* Get the RTC_CR register and clear the bits to be configured */
tmpreg = (uint32_t)(RTC->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
/* Get the new configuration */
if (NewState != DISABLE)
{
tmpreg |= (uint32_t)(RTC_TimeStampEdge | RTC_CR_TSE);
}
else
{
tmpreg |= (uint32_t)(RTC_TimeStampEdge);
}
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
/* Configure the Time Stamp TSEDGE and Enable bits */
RTC->CR = (uint32_t)tmpreg;
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
}
/**
* @brief Get the RTC TimeStamp value and masks.
* @param RTC_Format: specifies the format of the output parameters.
* This parameter can be one of the following values:
* @arg RTC_Format_BIN: Binary data format
* @arg RTC_Format_BCD: BCD data format
* @param RTC_StampTimeStruct: pointer to a RTC_TimeTypeDef structure that will
* contains the TimeStamp time values.
* @param RTC_StampDateStruct: pointer to a RTC_DateTypeDef structure that will
* contains the TimeStamp date values.
* @retval None
*/
void RTC_GetTimeStamp(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_StampTimeStruct,
RTC_DateTypeDef* RTC_StampDateStruct)
{
uint32_t tmptime = 0, tmpdate = 0;
/* Check the parameters */
assert_param(IS_RTC_FORMAT(RTC_Format));
/* Get the TimeStamp time and date registers values */
tmptime = (uint32_t)(RTC->TSTR & RTC_TR_RESERVED_MASK);
tmpdate = (uint32_t)(RTC->TSDR & RTC_DR_RESERVED_MASK);
/* Fill the Time structure fields with the read parameters */
RTC_StampTimeStruct->RTC_Hours = (uint8_t)((tmptime & (RTC_TR_HT | RTC_TR_HU)) >> 16);
RTC_StampTimeStruct->RTC_Minutes = (uint8_t)((tmptime & (RTC_TR_MNT | RTC_TR_MNU)) >> 8);
RTC_StampTimeStruct->RTC_Seconds = (uint8_t)(tmptime & (RTC_TR_ST | RTC_TR_SU));
RTC_StampTimeStruct->RTC_H12 = (uint8_t)((tmptime & (RTC_TR_PM)) >> 16);
/* Fill the Date structure fields with the read parameters */
RTC_StampDateStruct->RTC_Year = 0;
RTC_StampDateStruct->RTC_Month = (uint8_t)((tmpdate & (RTC_DR_MT | RTC_DR_MU)) >> 8);
RTC_StampDateStruct->RTC_Date = (uint8_t)(tmpdate & (RTC_DR_DT | RTC_DR_DU));
RTC_StampDateStruct->RTC_WeekDay = (uint8_t)((tmpdate & (RTC_DR_WDU)) >> 13);
/* Check the input parameters format */
if (RTC_Format == RTC_Format_BIN)
{
/* Convert the Time structure parameters to Binary format */
RTC_StampTimeStruct->RTC_Hours = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Hours);
RTC_StampTimeStruct->RTC_Minutes = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Minutes);
RTC_StampTimeStruct->RTC_Seconds = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Seconds);
/* Convert the Date structure parameters to Binary format */
RTC_StampDateStruct->RTC_Month = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_Month);
RTC_StampDateStruct->RTC_Date = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_Date);
RTC_StampDateStruct->RTC_WeekDay = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_WeekDay);
}
}
/**
* @}
*/
/** @defgroup RTC_Group9 Tampers configuration functions
* @brief Tampers configuration functions
*
@verbatim
===============================================================================
Tampers configuration functions
===============================================================================
@endverbatim
* @{
*/
/**
* @brief Configures the select Tamper pin edge.
* @param RTC_Tamper: Selected tamper pin.
* This parameter can be RTC_Tamper_1.
* @param RTC_TamperTrigger: Specifies the trigger on the tamper pin that
* stimulates tamper event.
* This parameter can be one of the following values:
* @arg RTC_TamperTrigger_RisingEdge: Rising Edge of the tamper pin causes tamper event.
* @arg RTC_TamperTrigger_FallingEdge: Falling Edge of the tamper pin causes tamper event.
* @retval None
*/
void RTC_TamperTriggerConfig(uint32_t RTC_Tamper, uint32_t RTC_TamperTrigger)
{
/* Check the parameters */
assert_param(IS_RTC_TAMPER(RTC_Tamper));
assert_param(IS_RTC_TAMPER_TRIGGER(RTC_TamperTrigger));
if (RTC_TamperTrigger == RTC_TamperTrigger_RisingEdge)
{
/* Configure the RTC_TAFCR register */
RTC->TAFCR &= (uint32_t)((uint32_t)~(RTC_Tamper << 1));
}
else
{
/* Configure the RTC_TAFCR register */
RTC->TAFCR |= (uint32_t)(RTC_Tamper << 1);
}
}
/**
* @brief Enables or Disables the Tamper detection.
* @param RTC_Tamper: Selected tamper pin.
* This parameter can be RTC_Tamper_1.
* @param NewState: new state of the tamper pin.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void RTC_TamperCmd(uint32_t RTC_Tamper, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_RTC_TAMPER(RTC_Tamper));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the selected Tamper pin */
RTC->TAFCR |= (uint32_t)RTC_Tamper;
}
else
{
/* Disable the selected Tamper pin */
RTC->TA
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -