Float to EEPROM
EEPROM STM32 How to save float data – STM32 EEPROM is one of the important things that must be learned. STM32 does not have EEPROM by default, so we can use flash memory for EEPROM allocation.
This method is called STM32 EEPROM Emulation.
STM32 EEPROM Emulation
This emulation is done by considering the limitation of Flash memory and some requirements. This approach requires at least two pages of flash memory of the same size allocated for non-volatile data. For a more complete explanation of this EEPROM Emulation, please read the official ST document.
Officially, to program this chip using software from the ST itself named STM32CubeIDE. But because I’m not familiar with it, I just use Arduino IDE because there are already many libraries available.
Well, because the Arduino IDE is basically an IDE for programming AVR, someone developed a library board for this chip to be used in the Arduino IDE. This is very useful.
Library for STM32 Emulation
The library board to support this chip to be programmed using the Arduino IDE has several options. However, the library board that I use on this project is STM32 Arduino Core.
This Arduino Core STM32 tries to emulate this non-volatile storage access way as in Arduino. STM32 Arduino Core provides this library which I managed to try.
If we use arduino, to write data just use “EEPROM.write (address, data)” and read with “EEPROM.read (address)”.
Whereas what I use to write is “EEPROMwrite (address, data)” and reads with “EEPROMread (address, data_data)”.
The STM32 program is to use ST-LINK V2 Clone (China) and for serial monitor communication I connect FTDI on pins A9 and A10.
EEPROM STM32?
In the previous article, I wrote a tutorial on how to access reading and writing EEPROM at STM32F103 using Arduino IDE, which can store data strings.
Well, complex programs often use float values because they can store very detailed data.
For example, coordinating data to show accurate locations, require data behind coma float data.
The more numbers behind commas, the more detail the results of the object position on this coordinate.
I tried finding out on the internet how to save float to EEPROM and reread the data stored at STM32 Blue Pill. But I didn’t find the right program.
For that reason, I created a program that can write float data and save to EEPROM STM32 (in this case I tested using STM32F103 Blue Pill) and read it again
EEPROMWRITE commands to write data. The data used must be a string, so the float data must be converted into a string. The program below is a modification program from the STM32Projects website.
Reading and Write
The program below I find in Kini Saya Ngerti (Indonesian).
#include <EEPROM.h>
const int addressEEPROM_min = 0; //Tentukan batasan alamat yang ingin digunakan.
const int addressEEPROM_max = 4095; //Sebagai contoh maksimum adalah 4095, artinya kita alokasikan memori 4KB (4096 bytes) untuk EEPROM Virtual.
float latitude = 4.158919; //Ini adalah contoh data yang ingin disimpan ke EEPROM.
float longitude = 96.124843; //Data Koordinat yang terdiri dari Latitude dan Longitude.
int addressLat = 0; //Jumlah karakter dari nilai latitude adalah 8, jadi alamat dimulai dari 0 hingga 8.
int addressLon = 9; //Jumlah karakter dari nilai longitude adalah 9, jadi alamat dimulai dari 9 hingga 17.
void setup()
{
Serial.begin(9600);
delay(100);
String lat = String (latitude, 6); //Data yang akan disimpan ke EEPROM adalah String. Baris ini digunakan untuk konversi float ke String.
String lon = String (longitude, 6); //Nilai 6 menunjukkan berapa banyak angka dibelakang koma yang akan di konversi.
//------------------- Perintah Tulis Data -------------------
Serial.println(" -----------------------------------------");
Serial.println("| Menulis Latitude ke EEPROM | Tersimpan |");
EEPROMwrite(addressLat, lat);
Serial.println("| Menulis Longitude ke EEPROM | Tersimpan |");
EEPROMwrite(addressLon, lon);
Serial.println(" -----------------------------------------");
Serial.println("\n");
delay(1000);
//----------------------------------------------------------
//------------------- Perintah Baca Data -------------------
Serial.println("Baca data dari EEPROM.....");
Serial.print("Latitude : ");
Serial.println(EEPROMread(addressLat, 8)); //Ambil data dari alamat 0 dengan jumlah karakter sebanyak 8.
Serial.print("Longitude : ");
Serial.println(EEPROMread(addressLon, 9)); //Ambil data dari alamat 9 dengan jumlah karakter sebanyak 9.
Serial.println("\n");
delay(1000);
//----------------------------------------------------------
Serial.println("Sekarang, silahkan COMMENT *Perintah Tulis Data* untuk menonaktifkan perintah tulis data ke EEPROM.");
delay(5000);
Serial.println("Kemudian, silahkan tekan tombol reset atau cabut STM32 dari komputer, untuk melihat apakah data dari EEPROM tetap tersimpan atau tidak.");
}
void loop()
{
}
String EEPROMread(int StartAddr, int StringLength)
{
char buf[StringLength + 1];
eeprom_read_string(StartAddr, buf, StringLength + 1);
String dataStr = buf;
return dataStr;
}
void EEPROMwrite(int StartAddr, String DataString)
{
int val = DataString.length() + 1;
char StringChar[val];
char buff[val];
DataString.toCharArray(StringChar, val);
strcpy(buff, StringChar);
eeprom_write_string(StartAddr, buff);
}
boolean eeprom_is_addr_ok(int addr)
{
return ((addr >= addressEEPROM_min) && (addr <= addressEEPROM_max));
}
boolean eeprom_write_bytes(int startAddr, const byte* array, int numBytes)
{
int i;
if (!eeprom_is_addr_ok(startAddr) || !eeprom_is_addr_ok(startAddr + numBytes))
{
return false;
}
for (i = 0; i < numBytes; i++)
{
EEPROM.write(startAddr + i, array[i]);
}
return true;
}
boolean eeprom_write_string(int addr, const char* string)
{
int numBytes; // jumlah byte aktual yang akan ditulis
numBytes = strlen(string) + 1; // Tulis konten string ditambah byte terminator string (0x00)
return eeprom_write_bytes(addr, (const byte*)string, numBytes);
}
boolean eeprom_read_string(int addr, char* buffer, int bufSize)
{
byte ch; // byte baca dari eeprom
int bytesRead; // jumlah byte yang dibaca sejauh ini
if (!eeprom_is_addr_ok(addr)) // periksa alamat mulai
{
return false;
}
if (bufSize == 0) // bagaimana kita bisa menyimpan byte dalam buffer kosong?
{
return false;
}
if (bufSize == 1) // apakah ada ruang untuk terminator string saja, tidak ada alasan untuk melangkah lebih jauh
{
buffer[0] = 0;
return true;
}
bytesRead = 0; // inisialisasi penghitung byte
ch = EEPROM.read(addr + bytesRead); // baca byte berikutnya dari eeprom
buffer[bytesRead] = ch; // simpan ke dalam buffer pengguna
bytesRead++;
// jika tidak ada kondisi berhenti terpenuhi, baca byte berikutnya dari eeprom
while ( (ch != 0x00) && (bytesRead < bufSize) && ((addr + bytesRead) <= addressEEPROM_max) )
{
ch = EEPROM.read(addr + bytesRead);
buffer[bytesRead] = ch; // simpan ke dalam buffer pengguna
bytesRead++;
}
if ((ch != 0x00) && (bytesRead >= 1)) // pastikan buffer pengguna memiliki terminator string, (0x00) sebagai byte terakhirnya
{
buffer[bytesRead - 1] = 0;
}
return true;
}
Results:
This program still has shortcomings. As you can see from Latitude, the initial value written was 96,124843, when it was read again to 96,124840. I don’t know the code to store data with the amount of data above 8 characters correctly.
If you can finish this, please comment below to help me increase the EEPROM STM32 article.
May be this article is useful.
READ NEXT
> How to Program STM32 or CKS32 via USB Ubuntu
> How to Install STM32 Arduino IDE Library
> Download Fritzing Part, Library, PinOut Arduino STM32
> How to Use STM32 RTC Internal Arduino IDE
> SMT32 RTC Stop Reading Data Hang or Freeze
> SMT32 MAX7219 Dot Matrix Display Text