From c9f55631119ac9a25745e4eaf2c62849c6f6d91c Mon Sep 17 00:00:00 2001 From: EgorShin <98223248+EgorShin@users.noreply.github.com> Date: Mon, 14 Oct 2024 09:49:46 +0700 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=BF=D0=B8=D1=81=D0=B0=D0=BB=20?= =?UTF-8?q?=D1=86=D0=B8=D0=BA=D0=BB=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D1=83?= =?UTF-8?q?=D1=8E=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81=D1=8C=20=D0=B8=20=D1=87?= =?UTF-8?q?=D1=82=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libraries/EEPROM/src/EEPROM.cpp | 24 +++--- libraries/EEPROM/src/EEPROM.h | 135 ++++---------------------------- 2 files changed, 31 insertions(+), 128 deletions(-) diff --git a/libraries/EEPROM/src/EEPROM.cpp b/libraries/EEPROM/src/EEPROM.cpp index 981aa64..23b2e8e 100644 --- a/libraries/EEPROM/src/EEPROM.cpp +++ b/libraries/EEPROM/src/EEPROM.cpp @@ -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); diff --git a/libraries/EEPROM/src/EEPROM.h b/libraries/EEPROM/src/EEPROM.h index 369bbd7..53b0a0c 100644 --- a/libraries/EEPROM/src/EEPROM.h +++ b/libraries/EEPROM/src/EEPROM.h @@ -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;