перевела комментарии

This commit is contained in:
klassents 2024-10-15 15:18:23 +07:00
parent 878d419808
commit ca644b2276
2 changed files with 56 additions and 66 deletions

View File

@ -4,7 +4,6 @@
HAL_EEPROM_HandleTypeDef heeprom;
void EEPROMClass:: begin()
{
heeprom.Instance = EEPROM_REGS;
@ -18,7 +17,7 @@ void EEPROMClass:: begin()
uint8_t read_byte( int idx )
{
// Проверка адекватности адреса idx
// check if idx is valid
if (idx < 0)
{
idx = -idx;
@ -32,23 +31,23 @@ uint8_t read_byte( int idx )
uint32_t read_data_buf[EEPROM_PAGE_WORDS] = {};
// Вычисление адреса начала нужной страницы
// calc start address of the desired page
uint32_t addr = EEPROM_START_ADDR + (((uint32_t)idx) / EEPROM_PAGE_SIZE) * EEPROM_PAGE_SIZE;
// Чтение нужной страницы
// read desired page
HAL_EEPROM_Read(&heeprom, (uint16_t)addr, read_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT);
// Адрес искомого слова в eeprom: EEPROM_START_ADDR + (uint32_t)idx
// address of the searched word in eeprom: EEPROM_START_ADDR + (uint32_t)idx
uint32_t word_addr = EEPROM_START_ADDR + (((uint32_t)idx) / EEPROM_WORD_SIZE) * EEPROM_WORD_SIZE;
// Индекс искомого слова в массиве read_data_buf: адрес слова в eeprom за вычетом адреса начала страницы
// searched word index in the read_data_buf array: the word address in the eeprom minus the page beginning address
uint32_t word_idx = (word_addr - addr) / EEPROM_WORD_SIZE;
// Номер байта в составе слова
// byte number in a word
uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE;
// Вычленение нужного байта из страницы
// get desired byte from page data
return (uint8_t)(( read_data_buf[word_idx] & (((uint32_t)0xFF) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8)) ) >> ((EEPROM_WORD_SIZE - byte_idx - 1) * 8));
}
void write_byte( int idx, uint8_t val )
{
// Проверка адекватности адреса idx
// check if idx is valid
if (idx < 0)
{
idx = -idx;
@ -62,10 +61,9 @@ void write_byte( int idx, uint8_t val )
update_byte(idx, val);
}
// Делает то же, что и write, но записывает только если новый байт отличается от уже записанного
void update_byte( int idx, uint8_t val )
{
// Проверка адекватности адреса idx
// check if idx is valid
if (idx < 0)
{
idx = -idx;
@ -79,35 +77,31 @@ void update_byte( int idx, uint8_t val )
uint32_t write_data_buf[EEPROM_PAGE_WORDS] = {};
// Вычисление адреса начала нужной страницы
// calc start address of the desired page
uint32_t addr = EEPROM_START_ADDR + (((uint32_t)idx) / EEPROM_PAGE_SIZE) * EEPROM_PAGE_SIZE;
// Читаем нужную страницу
// read desired page
HAL_EEPROM_Read(&heeprom, (uint16_t)addr, write_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT);
// Адрес искомого слова в eeprom: EEPROM_START_ADDR + (uint32_t)idx
// address of the searched word in eeprom: EEPROM_START_ADDR + (uint32_t)idx
uint32_t word_addr = EEPROM_START_ADDR + (((uint32_t)idx) / EEPROM_WORD_SIZE) * EEPROM_WORD_SIZE;
// Индекс искомого слова в массиве write_data_buf: адрес слова в eeprom за вычетом адреса начала страницы
// searched word index in the write_data_buf array: the word address in the eeprom minus the page beginning address
uint32_t word_idx = (word_addr - addr) / EEPROM_WORD_SIZE;
// Номер байта в составе слова
// byte number in a word
uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE;
// Извлечение нужного байта
// get desired byte
uint32_t byte = ((uint32_t)val) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8);
uint8_t oldVal = (uint8_t)(*((uint8_t*)write_data_buf + word_idx * EEPROM_WORD_SIZE + (EEPROM_WORD_SIZE - byte_idx - 1)));
// Проверка того, что записанный байт отличается от нового
// checking if written byte is different from the new one
if(oldVal != val)
{
// Очистка нужной страницы
// clear page
HAL_EEPROM_Erase(&heeprom, (uint16_t)addr, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT);
// Вычленение и замена нужного байта из страницы
// get and replace the desired byte
write_data_buf[word_idx] = (write_data_buf[word_idx] & (~((uint32_t)(0xFF) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8)))) | byte;
HAL_EEPROM_Write(&heeprom, (uint16_t)addr, write_data_buf, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT);
}
}
// uint16_t EEPROMClass:: length( void )
// {
// return (uint16_t)EEPROM_LENGHT;
// }
void HAL_read(uint16_t addr, uint32_t * data)
{

View File

@ -1,15 +1,16 @@
#ifndef EEPROM_h
#define EEPROM_h
#include <Arduino.h>
#include <inttypes.h>
#include "string.h"
#define EEPROM_OP_TIMEOUT 100000
#define EEPROM_PAGE_WORDS 32 // Количество слов на странице
#define EEPROM_PAGE_COUNT 8 // КОличество страниц пользовательской EEPROM
#define EEPROM_START_ADDR 0x1C00 // Адрес начала пользовательской EEPROM
#define EEPROM_WORD_SIZE 4 // Слово занимает 4 байта
#define EEPROM_PAGE_SIZE ( EEPROM_PAGE_WORDS * EEPROM_WORD_SIZE ) // Страница занимает 32*4 = 128 байт
#define EEPROM_PAGE_WORDS 32 // words number per page
#define EEPROM_PAGE_COUNT 8 // user EEPROM pages number
#define EEPROM_START_ADDR 0x1C00 // user EEPROM start address
#define EEPROM_WORD_SIZE 4 // word takes 4 bytes
#define EEPROM_PAGE_SIZE ( EEPROM_PAGE_WORDS * EEPROM_WORD_SIZE ) // page takes 32*4 = 128 bytes
#define EEPROM_END 0x1FFF
#define EEPROM_LENGHT (EEPROM_PAGE_SIZE * EEPROM_PAGE_COUNT)
@ -20,8 +21,6 @@ uint8_t read_byte( int idx );
void write_byte( int idx, uint8_t val );
void update_byte( int idx, uint8_t val );
extern void ErrorMsgHandler(const char * msg);
struct EERef{
EERef( const int index )
@ -98,7 +97,7 @@ struct EEPROMClass{
T &put(int idx, T &data)
{
void* dataPointer = (void*)&data;
// Проверка адекватности адреса idx
// check if idx is valid
if (idx < 0)
{
idx = -idx;
@ -111,32 +110,30 @@ struct EEPROMClass{
}
uint32_t write_data_buf[EEPROM_PAGE_WORDS] = {};
// Размер данных под запись
uint32_t dataSize = sizeof(data);
// Сдвиг начала записи данных
uint32_t dataShift = 0;
uint32_t dataSize = sizeof(data); // writing data size
uint32_t dataShift = 0; // shift of the data writing start address
uint32_t writeSize = dataSize;
// Вычисление адреса начала нужной страницы
// calc start address of the desired page
uint32_t addr = EEPROM_START_ADDR + (((uint32_t)idx) / EEPROM_PAGE_SIZE) * EEPROM_PAGE_SIZE;
// Адрес искомого слова в eeprom: EEPROM_START_ADDR + (uint32_t)idx
// address of the searched word in eeprom: EEPROM_START_ADDR + (uint32_t)idx
uint32_t word_addr = EEPROM_START_ADDR + (((uint32_t)idx) / EEPROM_WORD_SIZE) * EEPROM_WORD_SIZE;
// Индекс искомого слова в массиве write_data_buf: адрес слова в eeprom за вычетом адреса начала страницы
// searched word index in the write_data_buf array: the word address in the eeprom minus the page beginning address
uint32_t word_idx = (word_addr - addr) / EEPROM_WORD_SIZE;
// Адрес начала данных на странице
// page data start address
uint32_t byte_addr = (uint32_t)idx % EEPROM_PAGE_SIZE;
// Номер байта в составе слова
// byte number in a word
uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE;
// Чтение первой страницы
// read first page
HAL_read((uint16_t)addr, write_data_buf);
// Если данные не влезут на первую страницу, то записываем только то, что влезло
// if data does not fit on the first page, then write down only what fits
if (EEPROM_PAGE_SIZE - byte_addr < dataSize)
writeSize = EEPROM_PAGE_SIZE - byte_addr;
uint32_t lastWord = (writeSize + byte_idx - 1) / EEPROM_WORD_SIZE + word_idx;
dataSize -= writeSize;
// Запись данных постранично, сначала записываем первую страницу - отдельно
// write data page by page, first separately write the first page
memcpy((void *)((uint8_t *)write_data_buf + byte_addr), (void*)dataPointer, writeSize);
// Разворачиваем слова
// prepare words for writing
for(uint8_t i = word_idx; i <= lastWord; i++)
{
uint32_t word = write_data_buf[i];
@ -145,11 +142,11 @@ struct EEPROMClass{
}
HAL_erase((uint16_t)addr);
HAL_write((uint16_t)addr, write_data_buf);
// Если данные остались после записи первой страницы, то пишем их постранично, пока не закончатся
// if there is data left after writing the first page, then write it page by page until it runs out
while (dataSize > 0)
{
addr += EEPROM_PAGE_SIZE;
// При достижении крайнего адреса eeprom возвращаемся к начальному адресу
// if reaching the eeprom end address, return to the initial address
if (addr == EEPROM_START_ADDR + EEPROM_LENGHT)
addr = EEPROM_START_ADDR;
HAL_read((uint16_t)addr, write_data_buf);
@ -159,14 +156,13 @@ struct EEPROMClass{
writeSize = EEPROM_PAGE_SIZE;
lastWord = (writeSize - 1) / EEPROM_WORD_SIZE;
memcpy((void *)(write_data_buf), (void*)((uint8_t *)dataPointer + dataShift), writeSize);
// Разворачиваем слова
// prepare words for writing
for(uint8_t i = 0; i <= lastWord; i++)
{
uint32_t word = write_data_buf[i];
write_data_buf[i] = 0;
write_data_buf[i] = ((word & 0xFF)<<24) | ((word & (0xFF<<8))<<8) | ((word & (0xFF<<16))>>8) | ((word & (0xFF<<24))>>24);
}
// write_data_buf[0] = writeSize;
HAL_erase((uint16_t)addr);
HAL_write((uint16_t)addr, write_data_buf);
dataSize -= writeSize;
@ -178,7 +174,7 @@ struct EEPROMClass{
T &get(int idx, T &data)
{
void* dataPointer = (void*)&data;
// Проверка адекватности адреса idx
// check if idx is valid
if (idx < 0)
{
idx = -idx;
@ -191,43 +187,41 @@ struct EEPROMClass{
}
uint32_t read_data_buf[EEPROM_PAGE_WORDS] = {};
// Размер данных
uint32_t dataSize = sizeof(data);
// Сдвиг начала чтения данных
uint32_t dataShift = 0;
uint32_t dataSize = sizeof(data); // reading data size
uint32_t dataShift = 0; // shift of the data reading start address
uint32_t readSize = dataSize;
// Вычисление адреса начала нужной страницы
// calc start address of the desired page
uint32_t addr = EEPROM_START_ADDR + (((uint32_t)idx) / EEPROM_PAGE_SIZE) * EEPROM_PAGE_SIZE;
// Адрес искомого слова в eeprom: EEPROM_START_ADDR + (uint32_t)idx
// address of the searched word in eeprom: EEPROM_START_ADDR + (uint32_t)idx
uint32_t word_addr = EEPROM_START_ADDR + (((uint32_t)idx) / EEPROM_WORD_SIZE) * EEPROM_WORD_SIZE;
// Индекс искомого слова в массиве write_data_buf: адрес слова в eeprom за вычетом адреса начала страницы
// searched word index in the read_data_buf array: the word address in the eeprom minus the page beginning address
uint32_t word_idx = (word_addr - addr) / EEPROM_WORD_SIZE;
// Адрес начала данных на странице
// page data start address
uint32_t byte_addr = (uint32_t)idx % EEPROM_PAGE_SIZE;
// Номер байта в составе слова
// byte number in a word
uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE;
// Чтение первой страницы
// read first page
HAL_read((uint16_t)addr, read_data_buf);
if (EEPROM_PAGE_SIZE - byte_addr < dataSize)
readSize = EEPROM_PAGE_SIZE - byte_addr;
uint32_t lastWord = (readSize + byte_idx - 1) / EEPROM_WORD_SIZE + word_idx;
dataSize -= readSize;
// Разворачиваем слова
// prepare words
for(uint8_t i = word_idx; i <= lastWord; i++)
{
uint32_t word = read_data_buf[i];
read_data_buf[i] = 0;
read_data_buf[i] = ((word & 0xFF)<<24) | ((word & (0xFF<<8))<<8) | ((word & (0xFF<<16))>>8) | ((word & (0xFF<<24))>>24);
}
// Чтение данных постранично, сначала читаем первую страницу - отдельно
// read data page by page, first separately read the first page
memcpy((void *)dataPointer, (void*)((uint8_t *)read_data_buf + byte_addr), readSize);
// Если данные остались после записи первой страницы, то пишем их постранично, пока не закончатся
// if there is data left after reading the first page, then read it page by page until it runs out
while (dataSize > 0)
{
addr += EEPROM_PAGE_SIZE;
// При достижении крайнего адреса eeprom возвращаемся к начальному адресу
// if reaching the eeprom end address, return to the initial address
if (addr == EEPROM_START_ADDR + EEPROM_LENGHT)
addr = EEPROM_START_ADDR;
HAL_read((uint16_t)addr, read_data_buf);
@ -236,7 +230,7 @@ struct EEPROMClass{
if (EEPROM_PAGE_SIZE < dataSize)
readSize = EEPROM_PAGE_SIZE;
lastWord = (dataSize - 1) / EEPROM_WORD_SIZE;
// Разворачиваем слова
// prepare words
for(uint8_t i = 0; i <= lastWord; i++)
{
uint32_t word = read_data_buf[i];
@ -246,9 +240,11 @@ struct EEPROMClass{
memcpy((void *)((uint8_t *)dataPointer + dataShift), (void*)(read_data_buf), readSize);
dataSize -= readSize;
}
return data; // Возвращаем ссылку на переданный объект с прочитанными данными
return data; // Return passed object pointer with the read data
}
};
#pragma GCC diagnostic ignored "-Wunused-variable" // for GCC and Clang
static EEPROMClass EEPROM;
#endif