Дописал циклическую запись и чтение

This commit is contained in:
EgorShin committed 2024-10-14 09:49:46 +07:00
1 parent 690acb404c
commit c9f5563111
2 files changed
+31 -128

No files matched your search

+15 -9
View File
@@ -19,12 +19,14 @@ void EEPROMClass:: begin()
uint8_t EEPROMClass:: read( int idx )
{
// Проверка адекватности адреса idx
if (((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) || (idx < 0))
return 0x0; // #TODO Добавить сообщение об ошибке
if (idx < 0)
idx = -idx;
if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT)
idx = (int)((uint32_t)idx % EEPROM_LENGHT);
uint32_t read_data_buf[EEPROM_PAGE_WORDS] = {};
// Вычисление адресы начала нужной страницы
// Вычисление адреса начала нужной страницы
uint32_t addr = EEPROM_START_ADDR + (((uint32_t)idx) / EEPROM_PAGE_SIZE) * EEPROM_PAGE_SIZE;
// Чтение нужной страницы
HAL_EEPROM_Read(&heeprom, (uint16_t)addr, read_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT);
@@ -41,12 +43,14 @@ uint8_t EEPROMClass:: read( int idx )
void EEPROMClass:: write( int idx, uint8_t val )
{
// Проверка адекватности адреса idx
if (((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) || (idx < 0))
return; // #TODO Добавить сообщение об ошибке
if (idx < 0)
idx = -idx;
if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT)
idx = (int)((uint32_t)idx % EEPROM_LENGHT);
uint32_t write_data_buf[EEPROM_PAGE_WORDS] = {};
// Вычисление адресы начала нужной страницы
// Вычисление адреса начала нужной страницы
uint32_t addr = EEPROM_START_ADDR + (((uint32_t)idx) / EEPROM_PAGE_SIZE) * EEPROM_PAGE_SIZE;
// Читаем нужную страницу
HAL_EEPROM_Read(&heeprom, (uint16_t)addr, write_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT);
@@ -71,12 +75,14 @@ void EEPROMClass:: write( int idx, uint8_t val )
void EEPROMClass:: update( int idx, uint8_t val )
{
// Проверка адекватности адреса idx
if (((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) || (idx < 0))
return; // #TODO Добавить сообщение об ошибке
if (idx < 0)
idx = -idx;
if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT)
idx = (int)((uint32_t)idx % EEPROM_LENGHT);
uint32_t write_data_buf[EEPROM_PAGE_WORDS] = {};
// Вычисление адресы начала нужной страницы
// Вычисление адреса начала нужной страницы
uint32_t addr = EEPROM_START_ADDR + (((uint32_t)idx) / EEPROM_PAGE_SIZE) * EEPROM_PAGE_SIZE;
// Читаем нужную страницу
HAL_EEPROM_Read(&heeprom, (uint16_t)addr, write_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT);
+16 -119
View File
@@ -1,23 +1,3 @@
/*
EEPROM.h - EEPROM library
Original Copyright (c) 2006 David A. Mellis. All right reserved.
New version by Christopher Andrews 2015.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef EEPROM_h
#define EEPROM_h
@@ -37,94 +17,6 @@ void HAL_read(uint16_t addr, uint32_t * data);
void HAL_write(uint16_t addr, uint32_t * data);
void HAL_erase(uint16_t addr);
/***
EERef class.
This object references an EEPROM cell.
Its purpose is to mimic a typical byte of RAM, however its storage is the EEPROM.
This class has an overhead of two bytes, similar to storing a pointer to an EEPROM cell.
***/
// struct EERef{
// EERef( const int index )
// : index( index ) {}
// //Access/read members.
// uint8_t operator*() const { return eeprom_read_byte( (uint8_t*) index ); }
// operator uint8_t() const { return **this; }
// //Assignment/write members.
// EERef &operator=( const EERef &ref ) { return *this = *ref; }
// EERef &operator=( uint8_t in ) { return eeprom_write_byte( (uint8_t*) index, in ), *this; }
// EERef &operator +=( uint8_t in ) { return *this = **this + in; }
// EERef &operator -=( uint8_t in ) { return *this = **this - in; }
// EERef &operator *=( uint8_t in ) { return *this = **this * in; }
// EERef &operator /=( uint8_t in ) { return *this = **this / in; }
// EERef &operator ^=( uint8_t in ) { return *this = **this ^ in; }
// EERef &operator %=( uint8_t in ) { return *this = **this % in; }
// EERef &operator &=( uint8_t in ) { return *this = **this & in; }
// EERef &operator |=( uint8_t in ) { return *this = **this | in; }
// EERef &operator <<=( uint8_t in ) { return *this = **this << in; }
// EERef &operator >>=( uint8_t in ) { return *this = **this >> in; }
// EERef &update( uint8_t in ) { return in != *this ? *this = in : *this; }
// /** Prefix increment/decrement **/
// EERef& operator++() { return *this += 1; }
// EERef& operator--() { return *this -= 1; }
// /** Postfix increment/decrement **/
// uint8_t operator++ (int){
// uint8_t ret = **this;
// return ++(*this), ret;
// }
// uint8_t operator-- (int){
// uint8_t ret = **this;
// return --(*this), ret;
// }
// int index; //Index of current EEPROM cell.
// };
/***
EEPtr class.
This object is a bidirectional pointer to EEPROM cells represented by EERef objects.
Just like a normal pointer type, this can be dereferenced and repositioned using
increment/decrement operators.
***/
// struct EEPtr{
// EEPtr( const int index )
// : index( index ) {}
// operator int() const { return index; }
// EEPtr &operator=( int in ) { return index = in, *this; }
// //Iterator functionality.
// bool operator!=( const EEPtr &ptr ) { return index != ptr.index; }
// EERef operator*() { return index; }
// /** Prefix & Postfix increment/decrement **/
// EEPtr& operator++() { return ++index, *this; }
// EEPtr& operator--() { return --index, *this; }
// EEPtr operator++ (int) { return index++; }
// EEPtr operator-- (int) { return index--; }
// int index; //Index of current EEPROM cell.
// };
/***
EEPROMClass class.
This object represents the entire EEPROM space.
It wraps the functionality of EEPtr and EERef into a basic interface.
This class is also 100% backwards compatible with earlier Arduino core releases.
***/
struct EEPROMClass{
//Basic user access methods.
@@ -142,11 +34,10 @@ struct EEPROMClass{
T& put(int idx, T& dataPointer)
{
// Проверка адекватности адреса idx
if (((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) || (idx < 0))
return dataPointer; // #TODO Добавить сообщение об ошибке
// Проверка того, что данные влезут в eeprom, если начинать с текущего адреса
if (sizeof(dataPointer) > ((int)EEPROM_LENGHT - idx))
return dataPointer; // #TODO Добавить сообщение об ошибке
if (idx < 0)
idx = -idx;
if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT)
idx = (int)((uint32_t)idx % EEPROM_LENGHT);
uint32_t write_data_buf[EEPROM_PAGE_WORDS] = {};
// Размер данных под запись
@@ -154,10 +45,8 @@ struct EEPROMClass{
// Сдвиг начала записи данных
uint32_t dataShift = 0;
uint32_t writeSize = dataSize;
// uint8_t* dataPointer_8 = (uint8_t*)dataPointer;
// uint8_t* writePointer_8 = (uint8_t*)write_data_buf;
// Вычисление адресы начала нужной страницы
// Вычисление адреса начала нужной страницы
uint32_t addr = EEPROM_START_ADDR + (((uint32_t)idx) / EEPROM_PAGE_SIZE) * EEPROM_PAGE_SIZE;
// Адрес искомого слова в eeprom: EEPROM_START_ADDR + (uint32_t)idx
uint32_t word_addr = EEPROM_START_ADDR + (((uint32_t)idx) / EEPROM_WORD_SIZE) * EEPROM_WORD_SIZE;
@@ -189,6 +78,9 @@ struct EEPROMClass{
while (dataSize > 0)
{
addr += EEPROM_PAGE_SIZE;
// При достижении крайнего адреса eeprom возвращаемся к начальному адресу
if (addr == EEPROM_START_ADDR + EEPROM_LENGHT)
addr = EEPROM_START_ADDR;
HAL_read((uint16_t)addr, write_data_buf);
dataShift += writeSize;
writeSize = dataSize;
@@ -214,8 +106,10 @@ struct EEPROMClass{
T& get(int idx, T& dataPointer)
{
// Проверка адекватности адреса idx
if (((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) || (idx < 0))
return dataPointer; // #TODO Добавить сообщение об ошибке
if (idx < 0)
idx = -idx;
if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT)
idx = (int)((uint32_t)idx % EEPROM_LENGHT);
uint32_t read_data_buf[EEPROM_PAGE_WORDS] = {};
// Размер данных
@@ -224,7 +118,7 @@ struct EEPROMClass{
uint32_t dataShift = 0;
uint32_t readSize = dataSize;
// Вычисление адресы начала нужной страницы
// Вычисление адреса начала нужной страницы
uint32_t addr = EEPROM_START_ADDR + (((uint32_t)idx) / EEPROM_PAGE_SIZE) * EEPROM_PAGE_SIZE;
// Адрес искомого слова в eeprom: EEPROM_START_ADDR + (uint32_t)idx
uint32_t word_addr = EEPROM_START_ADDR + (((uint32_t)idx) / EEPROM_WORD_SIZE) * EEPROM_WORD_SIZE;
@@ -254,6 +148,9 @@ struct EEPROMClass{
while (dataSize > 0)
{
addr += EEPROM_PAGE_SIZE;
// При достижении крайнего адреса eeprom возвращаемся к начальному адресу
if (addr == EEPROM_START_ADDR + EEPROM_LENGHT)
addr = EEPROM_START_ADDR;
HAL_read((uint16_t)addr, read_data_buf);
dataShift += readSize;
readSize = dataSize;