|
| 1 | +#include "Arduino.h" |
| 2 | +#include <stdint.h> |
| 3 | + |
| 4 | +/** |
| 5 | + * Most STM32 devices don't have an integrated EEPROM. |
| 6 | + * To emulate a EEPROM, the STM32 Arduino core emulated |
| 7 | + * the operation of an EEPROM with the help of the embedded |
| 8 | + * flash. |
| 9 | + * |
| 10 | + * Writing to a flash is very expensive operation, since a |
| 11 | + * whole flash page needs to be written, even if you only |
| 12 | + * want to access the flash byte-wise. |
| 13 | + * |
| 14 | + * The STM32 Arduino core provides a buffered access API |
| 15 | + * to the emulated EEPROM. The library has allocated the |
| 16 | + * buffer even if you don't use the buffered API, so |
| 17 | + * it's strongly suggested to use the buffered API anyhow. |
| 18 | + */ |
| 19 | + |
| 20 | +#define DATA_LENGTH E2END |
| 21 | + |
| 22 | +void setup() { |
| 23 | + Serial.begin(115200); |
| 24 | +} |
| 25 | + |
| 26 | +void loop() { |
| 27 | + // Fill the EEPROM buffer in memory with data |
| 28 | + for (uint16_t i = 0; i < DATA_LENGTH; i++) { |
| 29 | + eeprom_buffered_write_byte(i, i % 256); |
| 30 | + } |
| 31 | + |
| 32 | + // Copy the data from the buffer to the flash |
| 33 | + eeprom_buffer_flush(); |
| 34 | + |
| 35 | + // Clear the buffer for demonstration purpose |
| 36 | + for (uint16_t i = 0; i < DATA_LENGTH; i++) { |
| 37 | + eeprom_buffered_write_byte(i, 0); |
| 38 | + } |
| 39 | + |
| 40 | + // Print the 254th byte of the current buffer (should be 0) |
| 41 | + Serial.println(eeprom_buffered_read_byte(254)); |
| 42 | + |
| 43 | + // Copy the data from the flash to the buffer |
| 44 | + eeprom_buffer_fill(); |
| 45 | + |
| 46 | + // Print the 254th byte of the current buffer (should be 254) |
| 47 | + Serial.println(eeprom_buffered_read_byte(254)); |
| 48 | +} |
0 commit comments