Library 45- Interface backup SRAM on STM32F4
Almost all (with few exceptions) STM32F4xx MCUs have internal backup SRAM, which stores data, if power is available at least on Vbat rails.
These MCUs have internal 4-kBytes of SRAM. Backup SRAM can be also used as EEPROM, if your Vbat supply is always active, so you can store data which will stay there also if you reset your device. You just have to make sure that you don’t shut down your power supply or re-write them during startup sequence of MCU.
Difference between backup SRAM and normal SRAM is that backup SRAM stays the same if main voltage is off (but Vbat is still active), while classic SRAM will loose data stored inside.
Library
Features
- Interface 4-kBytes of backup SRAM
- Does not work on STM32F401/411 and some others, backup SRAM not-present devices
- Read/Write 8-bit value
- Read/Write 16-bit value
- Read/Write 32-bit value
- Read/Write float value
- Version 1.1 – December 22, 2014
- Support for read/write float numbers
Dependencies
- CMSIS
- STM32F4xx
- STM32F4xx RCC
- STM32F4xx PWR
- TM
- defines.h
- defines.h
Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
/** * Get memory size for internal backup SRAM * * Returns memory value in Bytes */ #define TM_BKPSRAM_GetMemorySize() (0x00001000) /** * Initialize backup SRAM peripheral * * This function initializes and enable backup SRAM domain. * With this settings you have access to save/get from locations where SRAM is. * * No return */ extern void TM_BKPSRAM_Init(void); /** * Write 8-bit value to backup SRAM at desired location * * Parameters: * - uint16_t address: * Address where to save data in SRAM. * Value between 0 and TM_BKPSRAM_GetMemorySize() - 1 is valid, if more, HardFault error can happen. * - uint8_t value: * 8-bit value which will be stored to backup SRAM * * No return * * Defined as macro */ #define TM_BKPSRAM_Write8(address, value) (*(__IO uint8_t *) (BKPSRAM_BASE + (address)) = (value)) /** * Read 8-bit value from backup SRAM at desired location * * Parameters: * - uint16_t address: * Address from where read data in SRAM. * Value between 0 and TM_BKPSRAM_GetMemorySize() - 1 is valid, if more, HardFault error can happen. * * 8-bit value at specific location is returned * * Defined as macro */ #define TM_BKPSRAM_Read8(address) (*(__IO uint8_t *) (BKPSRAM_BASE + address)) /** * Write 16-bit value to backup SRAM at desired location * * Parameters: * - uint16_t address: * Address where to save data in SRAM. * Value between 0 and TM_BKPSRAM_GetMemorySize() - 2 is valid, if more, HardFault error can happen. * - uint16_t value: * 16-bit value which will be stored to backup SRAM * * No return * * Defined as macro */ #define TM_BKPSRAM_Write16(address, value) (*(__IO uint16_t *) (BKPSRAM_BASE + (address)) = (value)) /** * Read 16-bit value from backup SRAM at desired location * * Parameters: * - uint16_t address: * Address from where read data in SRAM. * Value between 0 and TM_BKPSRAM_GetMemorySize() - 2 is valid, if more, HardFault error can happen. * * 16-bit value at specific location is returned * * Defined as macro */ #define TM_BKPSRAM_Read16(address) (*(__IO uint16_t *) (BKPSRAM_BASE + address)) /** * Write 32-bit value to backup SRAM at desired location * * Parameters: * - uint16_t address: * Address where to save data in SRAM. * Value between 0 and TM_BKPSRAM_GetMemorySize() - 4 is valid, if more, HardFault error can happen. * - uint32_t value: * 32-bit value which will be stored to backup SRAM * * No return * * Defined as macro */ #define TM_BKPSRAM_Write32(address, value) (*(__IO uint32_t *) (BKPSRAM_BASE + (address)) = (value)) /** * Read 32-bit value from backup SRAM at desired location * * Parameters: * - uint16_t address: * Address from where read data in SRAM. * Value between 0 and TM_BKPSRAM_GetMemorySize() - 4 is valid, if more, HardFault error can happen. * * 32-bit value at specific location is returned * * Defined as macro */ #define TM_BKPSRAM_Read32(address) (*(__IO uint32_t *) (BKPSRAM_BASE + address)) /** * Write 32-bit float value to backup SRAM at desired location * * Parameters: * - uint16_t address: * Address where to save data in SRAM. * Value between 0 and TM_BKPSRAM_GetMemorySize() - 4 is valid, if more, HardFault error can happen. * - float value: * 32-bit float value which will be stored to backup SRAM * * No return * * Defined as macro */ #define TM_BKPSRAM_WriteFloat(address, value) (*(__IO float *) (BKPSRAM_BASE + (address)) = (value)) /** * Read 32-bit float value from backup SRAM at desired location * * Parameters: * - uint16_t address: * Address from where read data in SRAM. * Value between 0 and TM_BKPSRAM_GetMemorySize() - 4 is valid, if more, HardFault error can happen. * * 32-bit float value at specific location is returned * * Defined as macro */ #define TM_BKPSRAM_ReadFloat(address) (*(__IO float *) (BKPSRAM_BASE + address)) |
Sketches
In this section, I will show you, how to “fill” entire backup SRAM correctly, so you will not get like “HardFault” error or any other.
- Fill entire backup SRAM with 8-bit values
123456uint16_t i;/* Write to locations */for (i = 0; i < TM_BKPSRAM_GetMemorySize(); i++) {/* Write 8-bit 0x12 value on all locations in backup SRAM */TM_BKPSRAM_Write8(i, 0x12);} - Fill entire backup SRAM with 16-bit values
123456uint16_t i;/* Write to locations */for (i = 0; i < TM_BKPSRAM_GetMemorySize() - 1; i += 2) {/* Write 16-bit 0x4712 value on all locations in backup SRAM */TM_BKPSRAM_Write16(i, 0x4712);} - Fill entire backup SRAM with 32-bit values
123456uint16_t i;/* Write to locations */for (i = 0; i < TM_BKPSRAM_GetMemorySize() - 3; i += 4) {/* Write 32-bit 0x4d321712 value on all locations in backup SRAM */TM_BKPSRAM_Write32(i, 0x4d321712);}
Example
Example below works really easy. When you upload your code, first release usb power and put back again, that you reset everyting first.
After power on, you will se one LED on:
- RED led on: device was first time configured and value was written to backup sram
- If you see this, then press reset button
- GREEN led on: device read data back from backup SRAM successfully
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
/** * Keil project example for backup SRAM on STM32F4 * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @packs STM32F4xx Keil packs version 2.2.0 or greater required * @stdperiph STM32F4xx Standard peripheral drivers version 1.4.0 or greater required * * This will not work in STM32F401/411, these devices don't have backup SRAM. * If you will try to use this library, HardFault error will happen. * * If you see RED led, then press reset button. * After that, you should see GREEN led, which indicates successful write */ /* Include core modules */ #include "stm32f4xx.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32f4_disco.h" #include "tm_stm32f4_bkpsram.h" int main(void) { /* Initialize system */ SystemInit(); /* Initialize leds on board */ TM_DISCO_LedInit(); /* Initialize backup SRAM */ TM_BKPSRAM_Init(); /* Read from backup SRAM location 0x00 and check for value */ /* if it is the same as one we wrote */ if (TM_BKPSRAM_Read8(0x00) == 0x15) { /* Indicator that value after reset is still the same in backup SRAM */ /* Turn on green LED */ TM_DISCO_LedOn(LED_GREEN); } else { /* Write 8-bit value at backup SRAM location 0x00 */ TM_BKPSRAM_Write8(0x00, 0x15); /* Turn on red LED */ TM_DISCO_LedOn(LED_RED); } while (1) { } } |
Project is available on Github, download library below.
Recent comments