如何使用备份SRAM的EEPROM中STM32F4
展开全部
1.必须做到如下: 启用压水堆时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
能够访问到备份域
PWR_BackupAccessCmd(ENABLE);
启用备份SRAM时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE);
启用备份SRAM的低功耗稳压器,以保持它在VBAT模式的内容
PWR_BackupRegulatorCmd(ENABLE);
你可以读/写数据,建立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++;
}
}
那么,如果你想 //等到备份SRAM的低功耗稳压器已准备就绪
while(PWR_GetFlagStatus(PWR_FLAG_BRR) == RESET)
{}
你可以在STM32F4xx_DSP_StdPeriph_Lib找到这些函数。
2. 通过参考手册STM32F4和stm32f405xx / stm32f407xx书看完后,我同意这是不清楚如何备份SRAM(或所在)。下面是我发现。无论是RTC寄存器和备份SRAM包含存储的,只要你有电池电量维持量。该RTC含有20寄存器(80字节)和备份SRAM(这是它自己的周缘上AHB1和位于寄存器地址区域内)包含为0x1000(4096字节)。无论是默认启用的。 在DM00037051(stm32f405xx / stm32f407xx数据,P29):
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).
数据表71页参考手册和p65在
AHB1 | 0x4002 4000 - 0x4002 4FFF | BKPSRAM
和参考手册的datatasheet和P67的第73页
APB1 | 0x4000 2800 - 0x4000 2BFF | RTC & BKP Registers
页面上启用备份SRAM和RTC寄存器参考手册118-119。 注意:如果你是RTC的备份域 CodeGo.net,只需要存储和LT=80个字节,那么你最好的支持RTC寄存器启用备份SRAM基本上会加倍消耗电流(参见stm32f405 / 7数据表25 )。 这里是我的写入和读取用于备份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;
}
3. 我是一个STM32F2xx微控制器。根据数据表: 4 KB的备份SRAM是EEPROM般的区域。 保留的RTC备份寄存器的内容......当VDD关闭时,VBAT引脚可以连接到由电池或由另一个源提供的可选的备份电压。 甲超级电容器,例如,将需要而在微控制器的电源关闭,以保持备份寄存器的内容。 此外,根据本 复位后,备份域(...备份SRAM)进行保护,防止可能有害的写访问。要允许访问备份域,请执行以下操作... 它为您提供了有关如何访问到备份域通过直接写入到某些外设寄存器指令。如果你有机会到STM32F4xx库,你可以这样调用(注意:我的STM32F2xx库):
PWR_BackupAccessCmd(ENABLE);
注:还有更多的是它不是简单地调用上面的函数,如启用备份SRAM接口时钟。咨询STM32F4系列 有很多嵌入的库源是无价的,如果它是可用的,应阅读。 在STM32F2系列微控制器,SRAM位于地址范围: 0x40024000-0x40024FFF 并且可以被写入到在位置,例如,如下所示:
#define VAR_LOC ((volatile uint8_t *)(0x40024000))
volatile uint8_t *pVar = VAR_LOC;
*pVar = 5;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
能够访问到备份域
PWR_BackupAccessCmd(ENABLE);
启用备份SRAM时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE);
启用备份SRAM的低功耗稳压器,以保持它在VBAT模式的内容
PWR_BackupRegulatorCmd(ENABLE);
你可以读/写数据,建立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++;
}
}
那么,如果你想 //等到备份SRAM的低功耗稳压器已准备就绪
while(PWR_GetFlagStatus(PWR_FLAG_BRR) == RESET)
{}
你可以在STM32F4xx_DSP_StdPeriph_Lib找到这些函数。
2. 通过参考手册STM32F4和stm32f405xx / stm32f407xx书看完后,我同意这是不清楚如何备份SRAM(或所在)。下面是我发现。无论是RTC寄存器和备份SRAM包含存储的,只要你有电池电量维持量。该RTC含有20寄存器(80字节)和备份SRAM(这是它自己的周缘上AHB1和位于寄存器地址区域内)包含为0x1000(4096字节)。无论是默认启用的。 在DM00037051(stm32f405xx / stm32f407xx数据,P29):
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).
数据表71页参考手册和p65在
AHB1 | 0x4002 4000 - 0x4002 4FFF | BKPSRAM
和参考手册的datatasheet和P67的第73页
APB1 | 0x4000 2800 - 0x4000 2BFF | RTC & BKP Registers
页面上启用备份SRAM和RTC寄存器参考手册118-119。 注意:如果你是RTC的备份域 CodeGo.net,只需要存储和LT=80个字节,那么你最好的支持RTC寄存器启用备份SRAM基本上会加倍消耗电流(参见stm32f405 / 7数据表25 )。 这里是我的写入和读取用于备份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;
}
3. 我是一个STM32F2xx微控制器。根据数据表: 4 KB的备份SRAM是EEPROM般的区域。 保留的RTC备份寄存器的内容......当VDD关闭时,VBAT引脚可以连接到由电池或由另一个源提供的可选的备份电压。 甲超级电容器,例如,将需要而在微控制器的电源关闭,以保持备份寄存器的内容。 此外,根据本 复位后,备份域(...备份SRAM)进行保护,防止可能有害的写访问。要允许访问备份域,请执行以下操作... 它为您提供了有关如何访问到备份域通过直接写入到某些外设寄存器指令。如果你有机会到STM32F4xx库,你可以这样调用(注意:我的STM32F2xx库):
PWR_BackupAccessCmd(ENABLE);
注:还有更多的是它不是简单地调用上面的函数,如启用备份SRAM接口时钟。咨询STM32F4系列 有很多嵌入的库源是无价的,如果它是可用的,应阅读。 在STM32F2系列微控制器,SRAM位于地址范围: 0x40024000-0x40024FFF 并且可以被写入到在位置,例如,如下所示:
#define VAR_LOC ((volatile uint8_t *)(0x40024000))
volatile uint8_t *pVar = VAR_LOC;
*pVar = 5;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询