备份域的使用
初始化后对于备份域中各功能(RTC、RTC备份寄存器、备份SRAM)的使用就比较灵活了。
RTC: 使用相对来说比较复杂,后面独立介绍
RTC备份寄存器: 读写非常简单,标准外设库和HAL库都提供了函数直接进行读写。
/*----------------------------标准外设库----------------------------*/
/**
* @brief Writes a data in a specified RTC Backup data register.
* @param RTC_BKP_DR: RTC Backup data Register number.
* This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
* specify the register.
* @param Data: Data to be written in the specified RTC Backup data register.
* @retval None
*/
void RTC_WriteBackupRegister(uint32_t RTC_BKP_DR, uint32_t Data)
{
__IO uint32_t tmp = 0;
/* Check the parameters */
assert_param(IS_RTC_BKP(RTC_BKP_DR));
tmp = RTC_BASE + 0x50;
tmp += (RTC_BKP_DR * 4);
/* Write the specified register */
*(__IO uint32_t *)tmp = (uint32_t)Data;
}
/**
* @brief Reads data from the specified RTC Backup data Register.
* @param RTC_BKP_DR: RTC Backup data Register number.
* This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
* specify the register.
* @retval None
*/
uint32_t RTC_ReadBackupRegister(uint32_t RTC_BKP_DR)
{
__IO uint32_t tmp = 0;
/* Check the parameters */
assert_param(IS_RTC_BKP(RTC_BKP_DR));
tmp = RTC_BASE + 0x50;
tmp += (RTC_BKP_DR * 4);
/* Read the specified register */
return (*(__IO uint32_t *)tmp);
}
/*----------------------------HAL库----------------------------*/
/**
* @brief Writes a data in a specified RTC Backup data register.
* @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
* the configuration information for RTC.
* @param BackupRegister: RTC Backup data Register number.
* This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
* specify the register.
* @param Data: Data to be written in the specified RTC Backup data register.
* @retval None
*/
void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
{
uint32_t tmp = 0U;
/* Check the parameters */
assert_param(IS_RTC_BKP(BackupRegister));
tmp = (uint32_t)&(hrtc->Instance->BKP0R);
tmp += (BackupRegister * 4U);
/* Write the specified register */
*(__IO uint32_t *)tmp = (uint32_t)Data;
}
/**
* @brief Reads data from the specified RTC Backup data Register.
* @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
* the configuration information for RTC.
* @param BackupRegister: RTC Backup data Register number.
* This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
* specify the register.
* @retval Read value
*/
uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
{
uint32_t tmp = 0U;
/* Check the parameters */
assert_param(IS_RTC_BKP(BackupRegister));
tmp = (uint32_t)&(hrtc->Instance->BKP0R);
tmp += (BackupRegister * 4U);
/* Read the specified register */
return (*(__IO uint32_t *)tmp);
}
|