本帖最后由 tpgf 于 2021-10-10 12:27 编辑
备份域的使用初始化后对于备份域中各功能(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);
}
- [color=rgba(0, 0, 0, 0.75)]备份SRAM: 这部分的使用就更加灵活了,可以直接当内存去访问。推荐一种使用分散加载文件进行访问的方式。具体为定义自己的结构体,使用结构体定义变量BKP_SRAM myContent __attribute__((section("BKP_SRAM_SECTION")));,最后使用分散加载文件,将以上定义的变量直接映射到备份SRAM即可。
; *************************************************************
; *** Scatter-Loading Description File generated by uVision ***
; *************************************************************
LR_IROM1 0x08000000 0x0000C000 { ; load region size_region
ER_IROM1 0x08000000 0x0000C000 { ; load address = execution address
*.o (RESET, +First)
*(InRoot$$Sections)
.ANY (+RO)
}
RW_IRAM1 0x20000000 0x00020000 { ; RW data
.ANY (+RW +ZI)
}
RW_BkSRAM 0x40024000 0x1000 {
*.o (BKP_SRAM_SECTION, +First) ; 备份SRAM
}
}
|