如何在STM32F4中将备份SRAM用作EEPROM

Jol*_*lle 14 stm32 eeprom stm32f4discovery

在STM32F4上有两种模拟EEPROM的方法:

  1. 片上4 KB备份SRAM
  2. 片上Flash,具有特定的软件算法

第二个选项在这里描述:AN3969.

但遗憾的是,谷歌未能提供有关如何使用第一个选项的信息 - 使用4Kb备份SRAM作为EEPROM?

任何人都可以帮助这个话题吗?

小智 13

必须这样做:

  1. 使能PWR时钟

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
    
    Run Code Online (Sandbox Code Playgroud)
  2. 启用对备份域的访问

    PWR_BackupAccessCmd(ENABLE);
    
    Run Code Online (Sandbox Code Playgroud)
  3. 启用备份SRAM时钟

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE);
    
    Run Code Online (Sandbox Code Playgroud)
  4. 启用备份SRAM低功耗稳压器以在VBAT模式下保留其内容

    PWR_BackupRegulatorCmd(ENABLE);
    
    Run Code Online (Sandbox Code Playgroud)

你可以写入/读取数据到sram(这些代码来自STM32F4xx_DSP_StdPeriph_Lib中的BKP_Domain代码)(在我的mcu stm32f417 BKPSRAM_BASE = 0x40024000中)

   // Write to Backup SRAM with 32-Bit Data 
   for (i = 0x0; i < 0x100; i += 4) {
       *(__IO uint32_t *) (BKPSRAM_BASE + i) = i;
   }

   // Check the written Data 
   for (i = 0x0; i < 0x100; i += 4) {
          if ((*(__IO uint32_t *) (BKPSRAM_BASE + i)) != i){
              errorindex++;
          }
   }
Run Code Online (Sandbox Code Playgroud)

那么如果你想:

    // Wait until the Backup SRAM low power Regulator is ready
    while(PWR_GetFlagStatus(PWR_FLAG_BRR) == RESET)
    {}
Run Code Online (Sandbox Code Playgroud)

您可以在STM32F4xx_DSP_StdPeriph_Lib中找到这些功能.


swi*_*man 11

在阅读stm32f4参考手册和stm32f405xx/stm32f407xx数据表后,我同意不清楚如何实际使用备份sram(或它所在的位置).这是我发现的.只要您有电池电量,RTC寄存器和备份SRAM都包含一定量的存储空间.RTC包含20个寄存器(80个字节),备用sram(AHB1上的自身外设,位于寄存器地址区域内)包含0x1000(4096字节).默认情况下都不启用.

在DM00037051中(stm32f405xx/stm32f407xx数据表,第29页):

The 4-Kbyte backup SRAM is an EEPROM-like memory area. It can be used to store
data which need to be retained in VBAT and standby mode. This memory area is 
disabled by default to minimize power consumption (see Section 2.2.19: 
Low-power modes). It can be enabled by software.

The backup registers are 32-bit registers used to store 80 bytes of user 
application data when VDD power is not present. Backup registers are not reset
by a system, a power reset, or when the device wakes up from the Standby mode 
(see Section 2.2.19: Low-power modes).
Run Code Online (Sandbox Code Playgroud)

数据表第71页和参考手册的第65页

AHB1   |   0x4002 4000 - 0x4002 4FFF   |   BKPSRAM
Run Code Online (Sandbox Code Playgroud)

和数据表的第73页和参考手册的第67页

APB1   |   0x4000 2800 - 0x4000 2BFF   |   RTC & BKP Registers
Run Code Online (Sandbox Code Playgroud)

参考手册的第118-119页包含有关启用备份SRAM和RTC寄存器的信息.

注意:如果您已经在备份域中使用RTC并且只需要存储<= 80字节,那么最好使用RTC备份寄存器,因为启用备份SRAM基本上会使电流消耗翻倍(参见表25中的表格). stm32f405/7数据表).

这是我的备份SRAM和备份RTC寄存器的写入和读取功能

int8_t write_to_backup_sram( uint8_t *data, uint16_t bytes, uint16_t offset ) {
  const uint16_t backup_size = 0x1000;
  uint8_t* base_addr = (uint8_t *) BKPSRAM_BASE;
  uint16_t i;
  if( bytes + offset >= backup_size ) {
    /* ERROR : the last byte is outside the backup SRAM region */
    return -1;
  }
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE);
  /* disable backup domain write protection */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);   // set RCC->APB1ENR.pwren
  PWR_BackupAccessCmd(ENABLE);                          // set PWR->CR.dbp = 1;
  /** enable the backup regulator (used to maintain the backup SRAM content in
    * standby and Vbat modes).  NOTE : this bit is not reset when the device
    * wakes up from standby, system reset or power reset. You can check that
    * the backup regulator is ready on PWR->CSR.brr, see rm p144 */
  PWR_BackupRegulatorCmd(ENABLE);     // set PWR->CSR.bre = 1;
  for( i = 0; i < bytes; i++ ) {
    *(base_addr + offset + i) = *(data + i);
  }
  PWR_BackupAccessCmd(DISABLE);                     // reset PWR->CR.dbp = 0;
  return 0;
}

int8_t read_from_backup_sram( uint8_t *data, uint16_t bytes, uint16_t offset ) {
  const uint16_t backup_size = 0x1000;
  uint8_t* base_addr = (uint8_t *) BKPSRAM_BASE;
  uint16_t i;
  if( bytes + offset >= backup_size ) {
    /* ERROR : the last byte is outside the backup SRAM region */
    return -1;
  }
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE);
  for( i = 0; i < bytes; i++ ) {
    *(data + i) = *(base_addr + offset + i);
  }
  return 0;
}

int8_t write_to_backup_rtc( uint32_t *data, uint16_t bytes, uint16_t offset ) {
  const uint16_t backup_size = 80;
  volatile uint32_t* base_addr = &(RTC->BKP0R);
  uint16_t i;
  if( bytes + offset >= backup_size ) {
    /* ERROR : the last byte is outside the backup SRAM region */
    return -1;
  } else if( offset % 4 || bytes % 4 ) {
    /* ERROR: data start or num bytes are not word aligned */
    return -2;
  } else {
    bytes >>= 2;      /* divide by 4 because writing words */
  }
  /* disable backup domain write protection */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);   // set RCC->APB1ENR.pwren
  PWR_BackupAccessCmd(ENABLE);                          // set PWR->CR.dbp = 1;
  for( i = 0; i < bytes; i++ ) {
    *(base_addr + offset + i) = *(data + i);
  }
  PWR_BackupAccessCmd(DISABLE);                     // reset PWR->CR.dbp = 0;
  // consider also disabling the power peripherial?
  return 0;
}

int8_t read_from_backup_rtc( uint32_t *data, uint16_t bytes, uint16_t offset ) {
  const uint16_t backup_size = 80;
  volatile uint32_t* base_addr = &(RTC->BKP0R);
  uint16_t i;
  if( bytes + offset >= backup_size ) {
    /* ERROR : the last byte is outside the backup SRAM region */
    return -1;
  } else if( offset % 4 || bytes % 4 ) {
    /* ERROR: data start or num bytes are not word aligned */
    return -2;
  } else {
    bytes >>= 2;      /* divide by 4 because writing words */
  }
  /* read should be 32 bit aligned */
  for( i = 0; i < bytes; i++ ) {
    *(data + i) = *(base_addr + offset + i);
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)


mko*_*sar 5

我不得不根据用户请求从主程序跳转到引导加载程序。所以我在主程序的 BKPSRAM 中放入了一些“幻数”,做 CPU 软复位。引导加载程序始终首先启动。它检查“幻数”是否存在,它执行,否则启动主程序

使用 HAL 时,这是跳转到引导加载程序的方法:

__HAL_RCC_PWR_CLK_ENABLE();

HAL_PWR_EnableBkUpAccess();

__BKPSRAM_CLK_ENABLE();

*(__IO uint8_t *)0x40024000 = 42;//magic number

HAL_NVIC_SystemReset();
Run Code Online (Sandbox Code Playgroud)

在引导加载程序中读取幻数,仅启用备份 sram 时钟就足够了(引导加载程序使用 StdPeriphDriver)。

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE);

extRequest = *(__IO uint8_t *)0x40024000;

if(extRequest == 42)
    //run bootloader
Run Code Online (Sandbox Code Playgroud)

cpu是stm32f407