From 52adac2b59a838e0117f5e4cca93ff35f87a83f5 Mon Sep 17 00:00:00 2001 From: EgorShin <98223248+EgorShin@users.noreply.github.com> Date: Mon, 14 Oct 2024 17:33:58 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B8=D0=B2=D0=B5=D0=B4=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80=D0=B8=D0=BC=D0=B5=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=20eeprom?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../examples/eeprom_clear/eeprom_clear.ino | 13 +--- .../EEPROM/examples/eeprom_crc/eeprom_crc.ino | 5 +- .../EEPROM/examples/eeprom_get/eeprom_get.ino | 4 +- .../eeprom_iteration/eeprom_iteration.ino | 1 + .../EEPROM/examples/eeprom_put/eeprom_put.ino | 1 + .../examples/eeprom_read/eeprom_read.ino | 1 + .../examples/eeprom_update/eeprom_update.ino | 2 +- .../examples/eeprom_write/eeprom_write.ino | 2 +- libraries/EEPROM/src/EEPROM.cpp | 14 ++-- libraries/EEPROM/src/EEPROM.h | 77 +++++++++++++++++-- 10 files changed, 88 insertions(+), 32 deletions(-) diff --git a/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino b/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino index 3fed10f..834d14a 100644 --- a/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino +++ b/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino @@ -12,18 +12,11 @@ void setup() { // initialize the LED pin as an output. - pinMode(13, OUTPUT); + pinMode(LED_BUILTIN, OUTPUT); + EEPROM.begin(); /*** Iterate through each byte of the EEPROM storage. - - Larger AVR processors have larger EEPROM sizes, E.g: - - Arduino Duemilanove: 512 B EEPROM storage. - - Arduino Uno: 1 kB EEPROM storage. - - Arduino Mega: 4 kB EEPROM storage. - - Rather than hard-coding the length, you should use the pre-provided length function. - This will make your code portable to all AVR processors. ***/ for (int i = 0 ; i < EEPROM.length() ; i++) { @@ -31,7 +24,7 @@ void setup() { } // turn the LED on when we're done - digitalWrite(13, HIGH); + digitalWrite(LED_BUILTIN, HIGH); } void loop() { diff --git a/libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino b/libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino index c6db85c..504e1d6 100644 --- a/libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino +++ b/libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino @@ -1,19 +1,18 @@ /*** - Written by Christopher Andrews. - CRC algorithm generated by pycrc, MIT licence ( https://github.com/tpircher/pycrc ). + CRC algorithm generated by pycrc ( https://github.com/tpircher/pycrc ). A CRC is a simple way of checking whether data has changed or become corrupted. This example calculates a CRC value directly on the EEPROM values. The purpose of this example is to highlight how the EEPROM object can be used just like an array. ***/ -#include #include void setup() { //Start serial Serial.begin(9600); + EEPROM.begin(); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } diff --git a/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino b/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino index a07cee7..c49a31a 100644 --- a/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino +++ b/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino @@ -10,14 +10,12 @@ This may cause the serial object to print out a large string of garbage if there is no null character inside one of the strings loaded. - - Written by Christopher Andrews 2015 - Released under MIT licence. ***/ #include void setup() { + EEPROM.begin(); float f = 0.00f; //Variable to store data read from EEPROM. int eeAddress = 0; //EEPROM address to start reading from diff --git a/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino b/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino index b5d68be..d4c3427 100644 --- a/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino +++ b/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino @@ -14,6 +14,7 @@ #include void setup() { + EEPROM.begin(); /*** Iterate the EEPROM using a for loop. diff --git a/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino b/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino index c1ba0a5..29e1cca 100644 --- a/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino +++ b/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino @@ -23,6 +23,7 @@ struct MyObject { }; void setup() { + EEPROM.begin(); Serial.begin(9600); while (!Serial) { diff --git a/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino b/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino index d465035..a2980b7 100644 --- a/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino +++ b/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino @@ -13,6 +13,7 @@ int address = 0; byte value; void setup() { + EEPROM.begin(); // initialize serial and wait for port to open: Serial.begin(9600); while (!Serial) { diff --git a/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino b/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino index f5b0c0c..b80051e 100644 --- a/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino +++ b/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino @@ -17,7 +17,7 @@ int address = 0; void setup() { - /** Empty setup **/ + EEPROM.begin(); } void loop() { diff --git a/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino b/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino index 64e835c..b83fe59 100644 --- a/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino +++ b/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino @@ -12,7 +12,7 @@ int addr = 0; void setup() { - /** Empty setup. **/ + EEPROM.begin(); } void loop() { diff --git a/libraries/EEPROM/src/EEPROM.cpp b/libraries/EEPROM/src/EEPROM.cpp index 23b2e8e..12dc0a0 100644 --- a/libraries/EEPROM/src/EEPROM.cpp +++ b/libraries/EEPROM/src/EEPROM.cpp @@ -16,7 +16,7 @@ void EEPROMClass:: begin() HAL_EEPROM_CalculateTimings(&heeprom, OSC_SYSTEM_VALUE); } -uint8_t EEPROMClass:: read( int idx ) +uint8_t read( int idx ) { // Проверка адекватности адреса idx if (idx < 0) @@ -40,7 +40,7 @@ uint8_t EEPROMClass:: read( int idx ) 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 EEPROMClass:: write( int idx, uint8_t val ) +void write( int idx, uint8_t val ) { // Проверка адекватности адреса idx if (idx < 0) @@ -72,7 +72,7 @@ void EEPROMClass:: write( int idx, uint8_t val ) } // Делает то же, что и write, но записывает только если новый байт отличается от уже записанного -void EEPROMClass:: update( int idx, uint8_t val ) +void update( int idx, uint8_t val ) { // Проверка адекватности адреса idx if (idx < 0) @@ -107,10 +107,10 @@ void EEPROMClass:: update( int idx, uint8_t val ) } } -uint16_t EEPROMClass:: length( void ) -{ - return (uint16_t)EEPROM_LENGHT; -} +// uint16_t EEPROMClass:: length( void ) +// { +// return (uint16_t)EEPROM_LENGHT; +// } void HAL_read(uint16_t addr, uint32_t * data) { diff --git a/libraries/EEPROM/src/EEPROM.h b/libraries/EEPROM/src/EEPROM.h index 53b0a0c..1eeb4bb 100644 --- a/libraries/EEPROM/src/EEPROM.h +++ b/libraries/EEPROM/src/EEPROM.h @@ -16,19 +16,81 @@ void HAL_read(uint16_t addr, uint32_t * data); void HAL_write(uint16_t addr, uint32_t * data); void HAL_erase(uint16_t addr); +uint8_t read( int idx ); +void write( int idx, uint8_t val ); +void update( int idx, uint8_t val ); + +struct EERef{ + + EERef( const int index ) + : index( index ) {} + + uint8_t operator*() const { return read( index ); } + operator uint8_t() const { return **this; } + + EERef &operator=( const EERef &ref ) { return *this = *ref; } + EERef &operator=( uint8_t in ) { return write( 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; +}; + +struct EEPtr{ + + EEPtr( const int index ) + : index( index ) {} + + operator int() const { return index; } + EEPtr &operator=( int in ) { return index = in, *this; } + + bool operator!=( const EEPtr &ptr ) { return index != ptr.index; } + EERef operator*() { return index; } + + EEPtr& operator++() { return ++index, *this; } + EEPtr& operator--() { return --index, *this; } + EEPtr operator++ (int) { return index++; } + EEPtr operator-- (int) { return index--; } + + int index; +}; struct EEPROMClass{ //Basic user access methods. - // EERef operator[]( const int idx ) { return idx; } - uint8_t read( int idx ); - void write( int idx, uint8_t val ); - void update( int idx, uint8_t val ); + EERef operator[]( const int idx ) { return idx; } + uint8_t read( int idx ) { return EERef( idx ); } + void write( int idx, uint8_t val ) { (EERef( idx )) = val; } + void update( int idx, uint8_t val ) { EERef( idx ).update( val ); } - //STL and C++11 iteration capability. void begin(); - uint16_t end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid. - uint16_t length(); + EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid. + uint16_t length() { return (uint16_t)EEPROM_LENGHT; } template< typename T > T& put(int idx, T& dataPointer) @@ -95,6 +157,7 @@ struct EEPROMClass{ 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;