stm32 hal库 rtc 备份 寄存器 备份sram 调试笔记

rtc 是可以单独供电的,通常设计rtc是带有一个纽扣电池用来给rtc供电。本次是调试rtc 的备份寄存器和备份sram,这两个都是可以在rtc不断电情况下一直保存数据。
步骤

  • 配置rtc参数,用cube mx 生成初始化代码
  • 使能备份寄存器时钟和备份sram时钟
  • 写入数据,不断电重启等
  • 读取数据
    使用的hal库函数
*** Main and Backup Regulators configuration ***
================================================
[..] 
  (+) The backup domain includes 4 Kbytes of backup SRAM accessible only from 
      the CPU, and address in 32-bit, 16-bit or 8-bit mode. Its content is 
      retained even in Standby or VBAT mode when the low power backup regulator
      is enabled. It can be considered as an internal EEPROM when VBAT is 
      always present. You can use the HAL_PWREx_EnableBkUpReg() function to 
      enable the low power backup regulator. 

  (+) When the backup domain is supplied by VDD (analog switch connected to VDD) 
      the backup SRAM is powered from VDD which replaces the VBAT power supply to 
      save battery life.

  (+) The backup SRAM is not mass erased by a tamper event. It is read 
      protected to prevent confidential data, such as cryptographic private 
      key, from being accessed. The backup SRAM can be erased only through 
      the Flash interface when a protection level change from level 1 to 
      level 0 is requested. 
  -@- Refer to the description of Read protection (RDP) in the Flash 
      programming manual.

    Refer to the product datasheets for more details.

翻译

主稳压器和备用稳压器配置 =======================================
[…](+)备份域包括只能从CPU访问的4 KB备份SRAM,其地址为32位,16位或8位模式。启用低功耗备用稳压器后,即使在待机或VBAT模式下,其内容也会保留。当始终存在VBAT时,可以将其视为内部EEPROM。您可以使用HAL_PWREx_EnableBkUpReg()函数来启用低功耗备用稳压器。
(+)当备份域由VDD提供(模拟开关连接到VDD)时,备份SRAM由VDD供电,该VDD替代了VBAT电源以节省电池寿命。
(+)篡改事件不会大量擦除备用SRAM。对其进行了读取保护,以防止访问机密数据(例如加密私钥)。当保护级别从1级更改为0级时,只能通过Flash接口擦除备份SRAM。
-@-请参阅Flash编程手册中的读保护(RDP)说明。有关更多详细信息,请参考产品数据表。

  __HAL_RCC_PWR_CLK_ENABLE();/* 电源管理时钟使能 */
  HAL_PWR_EnableBkUpAccess();/* 使能允许访问备份域(备份寄存器和备份SRAM)*/
  HAL_PWREx_EnableBkUpReg();/* 使能备份寄存器 这里最新版是这个函数*/
  __HAL_RCC_BKP_CLK_ENABLE();/* 使能备份域时钟 ,虽然有保留在h文件中,但是没具体实现,不用这个*/

备份寄存器

备份寄存器一般都数量比较少,只能存一点点东西。
数量需要查阅手册或者直接从生成的代码中找到数量,比如,本次生成的代码在函数中说明的0-19个寄存器,数据可以看见从下面实现是uint32_t大小的数据

/**
  * @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;
}

只需要在使能之后写入 再读取。

备份SRAM

上面提到了是4KB,这里查阅手册确实是4KB
在这里插入图片描述
这里直接使用就可以

uint32_t addr  = 0x40024000;
*(__IO uint32_t *)addr  = 0x01;//写入
uint32_t data = *(__IO uint32_t *)addr;//读取

当保护级别从1级更改为0级时,只能通过Flash接口擦除备份SRAM。

HAL_FLASH_Unlock();
HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError)

就需要使用flash接口擦除了(未验证)

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值