From 4710f730b3bfeefe7eb6a0f533fd6fc6a77761b6 Mon Sep 17 00:00:00 2001 From: EgorShin <98223248+EgorShin@users.noreply.github.com> Date: Wed, 25 Sep 2024 12:06:24 +0700 Subject: [PATCH 01/11] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D1=81=D1=8B=D1=80=D1=83=D1=8E=20EEPROM?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/EEPROM.cpp | 145 +++++++++++++++++ cores/arduino/EEPROM.h | 150 ++++++++++++++++++ libraries/EEPROM/README.md | 139 ++++++++++++++++ .../examples/eeprom_clear/eeprom_clear.ino | 39 +++++ .../EEPROM/examples/eeprom_crc/eeprom_crc.ino | 52 ++++++ .../EEPROM/examples/eeprom_get/eeprom_get.ino | 68 ++++++++ .../eeprom_iteration/eeprom_iteration.ino | 57 +++++++ .../EEPROM/examples/eeprom_put/eeprom_put.ino | 58 +++++++ .../examples/eeprom_read/eeprom_read.ino | 56 +++++++ .../examples/eeprom_update/eeprom_update.ino | 71 +++++++++ .../examples/eeprom_write/eeprom_write.ino | 60 +++++++ libraries/EEPROM/keywords.txt | 22 +++ libraries/EEPROM/library.properties | 10 ++ libraries/EEPROM/src/.vscode/settings.json | 5 + libraries/EEPROM/src/EEPROM.cpp | 145 +++++++++++++++++ libraries/EEPROM/src/EEPROM.h | 150 ++++++++++++++++++ 16 files changed, 1227 insertions(+) create mode 100644 cores/arduino/EEPROM.cpp create mode 100644 cores/arduino/EEPROM.h create mode 100644 libraries/EEPROM/README.md create mode 100644 libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino create mode 100644 libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino create mode 100644 libraries/EEPROM/examples/eeprom_get/eeprom_get.ino create mode 100644 libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino create mode 100644 libraries/EEPROM/examples/eeprom_put/eeprom_put.ino create mode 100644 libraries/EEPROM/examples/eeprom_read/eeprom_read.ino create mode 100644 libraries/EEPROM/examples/eeprom_update/eeprom_update.ino create mode 100644 libraries/EEPROM/examples/eeprom_write/eeprom_write.ino create mode 100644 libraries/EEPROM/keywords.txt create mode 100644 libraries/EEPROM/library.properties create mode 100644 libraries/EEPROM/src/.vscode/settings.json create mode 100644 libraries/EEPROM/src/EEPROM.cpp create mode 100644 libraries/EEPROM/src/EEPROM.h diff --git a/cores/arduino/EEPROM.cpp b/cores/arduino/EEPROM.cpp new file mode 100644 index 0000000..500296b --- /dev/null +++ b/cores/arduino/EEPROM.cpp @@ -0,0 +1,145 @@ +#include "EEPROM.h" +#include "mik32_hal_eeprom.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_PAGE_WORDS ) // Страница занимает 32*4 = 128 байт +#define EEPROM_END 0x1FFF +#define EEPROM_LENGHT (EEPROM_PAGE_SIZE * EEPROM_PAGE_COUNT) + +HAL_EEPROM_HandleTypeDef heeprom; + + +EEPtr EEPROMClass:: begin() +{ + heeprom.Instance = EEPROM_REGS; + heeprom.Mode = HAL_EEPROM_MODE_TWO_STAGE; + heeprom.ErrorCorrection = HAL_EEPROM_ECC_ENABLE; + heeprom.EnableInterrupt = HAL_EEPROM_SERR_DISABLE; + + HAL_EEPROM_Init(&heeprom); + HAL_EEPROM_CalculateTimings(&heeprom, OSC_SYSTEM_VALUE); + return 0x00; +} + +uint8_t EEPROMClass:: read( int idx ) +{ + if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) + return 0x00; + + 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, addr, read_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); + // Адрес искомого слова в 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 за вычетом адреса начала страницы + uint32_t word_idx = (word_addr - addr) / EEPROM_WORD_SIZE; + // Номер байта в составе слова + uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE; + // Вычленение нужного байта из страницы + 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 ) +{ + if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) + return; + + 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, addr, write_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); + // Адрес искомого слова в 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 за вычетом адреса начала страницы + uint32_t word_idx = (word_addr - addr) / EEPROM_WORD_SIZE; + // Номер байта в составе слова + uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE; + // Извлечение нужного байта + uint32_t byte = ((uint32_t)val) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8); + // Вычленение и замена нужного байта из страницы + 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, EEPROM_START_ADDR, write_data_buf, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT); +} + +// Делает то же, что и write, но записывает только если новый байт отличается от уже записанного +void EEPROMClass:: update( int idx, uint8_t val ) +{ + if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) + return; + + 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, addr, write_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); + // Адрес искомого слова в 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 за вычетом адреса начала страницы + uint32_t word_idx = (word_addr - addr) / EEPROM_WORD_SIZE; + // Номер байта в составе слова + uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE; + // Извлечение нужного байта + uint32_t byte = ((uint32_t)val) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8); + // Проверка того, что записанный байт отличается от нового + if((write_data_buf[word_idx] & ((uint32_t)(0xFF) << ((EEPROM_WORD_SIZE - byte_idx - 1)) * 8)) != 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, EEPROM_START_ADDR, write_data_buf, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT); + } +} + +uint16_t EEPROMClass:: length( void ) +{ + return EEPROM_LENGHT; +} + +template< typename T > +const T& EEPROMClass:: put(int idx, const T &t) { + // Создание объекта EEPtr, который указывает на начальный адрес в EEPROM. + EEPtr e = idx; + + // Указатель на данные, которые нужно записать в EEPROM. + const uint8_t *ptr = (const uint8_t*) &t; + + // Количество байт данных для записи. + int count = sizeof(T); + + // Цикл для записи данных побайтно в EEPROM. + for(; count ; --count, ++e) { + // Запись текущего байта данных в EEPROM. + (*e).update( *ptr++ ); + } + + // Возврат ссылки на записанный объект. + return t; +} + +T& EEPROMClass:: get(int idx, T &t) { + EEPtr e = idx; // Указатель на адрес в EEPROM + uint8_t *ptr = (uint8_t*) &t; // Преобразуем ссылку на объект данных в указатель на байт + for (int count = sizeof(T); count; --count, ++e) { + *ptr++ = *e; // Читаем данные из EEPROM и записываем в объект + } + return t; // Возвращаем ссылку на переданный объект с прочитанными данными +} + + + +// uint32_t* EEPROM_Read(uint16_t index) +// { +// uint32_t read_data_buf[]; +// HAL_EEPROM_Read(&heeprom, 0x1C00 + index, read_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); +// return read_data_buf; +// } \ No newline at end of file diff --git a/cores/arduino/EEPROM.h b/cores/arduino/EEPROM.h new file mode 100644 index 0000000..b8fb21a --- /dev/null +++ b/cores/arduino/EEPROM.h @@ -0,0 +1,150 @@ +/* + 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 + +#include + +/*** + 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. + 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 ); + + //STL and C++11 iteration capability. + EEPtr begin(); + EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid. + uint16_t length(); + + template + const T& put(int address, const T& data); + + template + T& get(int address, T& data); + + //Functionality to 'get' and 'put' objects to and from EEPROM. + // template< typename T > T &get( int idx, T &t ){ + // EEPtr e = idx; + // uint8_t *ptr = (uint8_t*) &t; + // for( int count = sizeof(T) ; count ; --count, ++e ) *ptr++ = *e; + // return t; + // } + + // template< typename T > const T &put( int idx, const T &t ){ + // EEPtr e = idx; + // const uint8_t *ptr = (const uint8_t*) &t; + // for( int count = sizeof(T) ; count ; --count, ++e ) (*e).update( *ptr++ ); + // return t; + // } +}; + +/*static*/ EEPROMClass EEPROM; +#endif diff --git a/libraries/EEPROM/README.md b/libraries/EEPROM/README.md new file mode 100644 index 0000000..9ca761d --- /dev/null +++ b/libraries/EEPROM/README.md @@ -0,0 +1,139 @@ +## **EEPROM Library V2.0** for Arduino + +**Written by:** _Christopher Andrews_. + +### **What is the EEPROM library.** + +Th EEPROM library provides an easy to use interface to interact with the internal non-volatile storage found in AVR based Arduino boards. This library will work on many AVR devices like ATtiny and ATmega chips. + +### **How to use it** +The EEPROM library is included in your IDE download. To add its functionality to your sketch you'll need to reference the library header file. You do this by adding an include directive to the top of your sketch. + +```Arduino +#include + +void setup(){ + +} + +void loop(){ + +} + +``` + +The library provides a global variable named `EEPROM`, you use this variable to access the library functions. The methods provided in the EEPROM class are listed below. + +You can view all the examples [here](examples/). + +### **Library functions** + +#### **`EEPROM.read( address )`** [[_example_]](examples/eeprom_read/eeprom_read.ino) + +This function allows you to read a single byte of data from the eeprom. +Its only parameter is an `int` which should be set to the address you wish to read. + +The function returns an `unsigned char` containing the value read. + +#### **`EEPROM.write( address, value )`** [[_example_]](examples/eeprom_write/eeprom_write.ino) + +The `write()` method allows you to write a single byte of data to the EEPROM. +Two parameters are needed. The first is an `int` containing the address that is to be written, and the second is a the data to be written (`unsigned char`). + +This function does not return any value. + +#### **`EEPROM.update( address, value )`** [[_example_]](examples/eeprom_update/eeprom_update.ino) + +This function is similar to `EEPROM.write()` however this method will only write data if the cell contents pointed to by `address` is different to `value`. This method can help prevent unnecessary wear on the EEPROM cells. + +This function does not return any value. + +#### **`EEPROM.get( address, object )`** [[_example_]](examples/eeprom_get/eeprom_get.ino) + +This function will retrieve any object from the EEPROM. +Two parameters are needed to call this function. The first is an `int` containing the address that is to be written, and the second is the object you would like to read. + +This function returns a reference to the `object` passed in. It does not need to be used and is only returned for convenience. + +#### **`EEPROM.put( address, object )`** [[_example_]](examples/eeprom_put/eeprom_put.ino) + +This function will write any object to the EEPROM. +Two parameters are needed to call this function. The first is an `int` containing the address that is to be written, and the second is the object you would like to write. + +This function uses the _update_ method to write its data, and therefore only rewrites changed cells. + +This function returns a reference to the `object` passed in. It does not need to be used and is only returned for convenience. + +#### **Subscript operator: `EEPROM[address]`** [[_example_]](examples/eeprom_crc/eeprom_crc.ino) + +This operator allows using the identifier `EEPROM` like an array. +EEPROM cells can be read _and_ **_written_** directly using this method. + +This operator returns a reference to the EEPROM cell. + +```c++ +unsigned char val; + +//Read first EEPROM cell. +val = EEPROM[ 0 ]; + +//Write first EEPROM cell. +EEPROM[ 0 ] = val; + +//Compare contents +if( val == EEPROM[ 0 ] ){ + //Do something... +} +``` + +#### **`EEPROM.length()`** + +This function returns an `unsigned int` containing the number of cells in the EEPROM. + +--- + +### **Advanced features** + +This library uses a component based approach to provide its functionality. This means you can also use these components to design a customized approach. Two background classes are available for use: `EERef` & `EEPtr`. + +#### **`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. + +```C++ +EERef ref = EEPROM[ 10 ]; //Create a reference to 11th cell. + +ref = 4; //write to EEPROM cell. + +unsigned char val = ref; //Read referenced cell. +``` + +#### **`EEPtr` class** + +This object is a bidirectional pointer to EEPROM cells represented by `EERef` objects. +Just like a normal pointer type, this type can be dereferenced and repositioned using +increment/decrement operators. + +```C++ +EEPtr ptr = 10; //Create a pointer to 11th cell. + +*ptr = 4; //dereference and write to EEPROM cell. + +unsigned char val = *ptr; //dereference and read. + +ptr++; //Move to next EEPROM cell. +``` + +#### **`EEPROM.begin()`** + +This function returns an `EEPtr` pointing to the first cell in the EEPROM. +This is useful for STL objects, custom iteration and C++11 style ranged for loops. + +#### **`EEPROM.end()`** + +This function returns an `EEPtr` pointing at the location after the last EEPROM cell. +Used with `begin()` to provide custom iteration. + +**Note:** The `EEPtr` returned is invalid as it is out of range. In fact the hardware causes wrapping of the address (overflow) and `EEPROM.end()` actually references the first EEPROM cell. diff --git a/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino b/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino new file mode 100644 index 0000000..3fed10f --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino @@ -0,0 +1,39 @@ +/* + * EEPROM Clear + * + * Sets all of the bytes of the EEPROM to 0. + * Please see eeprom_iteration for a more in depth + * look at how to traverse the EEPROM. + * + * This example code is in the public domain. + */ + +#include + +void setup() { + // initialize the LED pin as an output. + pinMode(13, OUTPUT); + + /*** + 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++) { + EEPROM.write(i, 0); + } + + // turn the LED on when we're done + digitalWrite(13, HIGH); +} + +void loop() { + /** Empty loop. **/ +} diff --git a/libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino b/libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino new file mode 100644 index 0000000..c6db85c --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino @@ -0,0 +1,52 @@ +/*** + Written by Christopher Andrews. + CRC algorithm generated by pycrc, MIT licence ( 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); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + //Print length of data to run CRC on. + Serial.print("EEPROM length: "); + Serial.println(EEPROM.length()); + + //Print the result of calling eeprom_crc() + Serial.print("CRC32 of EEPROM data: 0x"); + Serial.println(eeprom_crc(), HEX); + Serial.print("\n\nDone!"); +} + +void loop() { + /* Empty loop */ +} + +unsigned long eeprom_crc(void) { + + const unsigned long crc_table[16] = { + 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, + 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, + 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, + 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c + }; + + unsigned long crc = ~0L; + + for (int index = 0 ; index < EEPROM.length() ; ++index) { + crc = crc_table[(crc ^ EEPROM[index]) & 0x0f] ^ (crc >> 4); + crc = crc_table[(crc ^ (EEPROM[index] >> 4)) & 0x0f] ^ (crc >> 4); + crc = ~crc; + } + return crc; +} diff --git a/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino b/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino new file mode 100644 index 0000000..a07cee7 --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino @@ -0,0 +1,68 @@ +/*** + eeprom_get example. + + This shows how to use the EEPROM.get() method. + + To pre-set the EEPROM data, run the example sketch eeprom_put. + This sketch will run without it, however, the values shown + will be shown from what ever is already on the EEPROM. + + 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() { + + float f = 0.00f; //Variable to store data read from EEPROM. + int eeAddress = 0; //EEPROM address to start reading from + + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + Serial.print("Read float from EEPROM: "); + + //Get the float data from the EEPROM at position 'eeAddress' + EEPROM.get(eeAddress, f); + Serial.println(f, 3); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float. + + /*** + As get also returns a reference to 'f', you can use it inline. + E.g: Serial.print( EEPROM.get( eeAddress, f ) ); + ***/ + + /*** + Get can be used with custom structures too. + I have separated this into an extra function. + ***/ + + secondTest(); //Run the next test. +} + +struct MyObject { + float field1; + byte field2; + char name[10]; +}; + +void secondTest() { + int eeAddress = sizeof(float); //Move address to the next byte after float 'f'. + + MyObject customVar; //Variable to store custom object read from EEPROM. + EEPROM.get(eeAddress, customVar); + + Serial.println("Read custom object from EEPROM: "); + Serial.println(customVar.field1); + Serial.println(customVar.field2); + Serial.println(customVar.name); +} + +void loop() { + /* Empty loop */ +} diff --git a/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino b/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino new file mode 100644 index 0000000..b5d68be --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino @@ -0,0 +1,57 @@ +/*** + eeprom_iteration example. + + A set of example snippets highlighting the + simplest methods for traversing the EEPROM. + + Running this sketch is not necessary, this is + simply highlighting certain programming methods. + + Written by Christopher Andrews 2015 + Released under MIT licence. +***/ + +#include + +void setup() { + + /*** + Iterate the EEPROM using a for loop. + ***/ + + for (int index = 0 ; index < EEPROM.length() ; index++) { + + //Add one to each cell in the EEPROM + EEPROM[ index ] += 1; + } + + /*** + Iterate the EEPROM using a while loop. + ***/ + + int index = 0; + + while (index < EEPROM.length()) { + + //Add one to each cell in the EEPROM + EEPROM[ index ] += 1; + index++; + } + + /*** + Iterate the EEPROM using a do-while loop. + ***/ + + int idx = 0; //Used 'idx' to avoid name conflict with 'index' above. + + do { + + //Add one to each cell in the EEPROM + EEPROM[ idx ] += 1; + idx++; + } while (idx < EEPROM.length()); + + +} //End of setup function. + +void loop() {} diff --git a/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino b/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino new file mode 100644 index 0000000..c1ba0a5 --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino @@ -0,0 +1,58 @@ +/*** + eeprom_put example. + + This shows how to use the EEPROM.put() method. + Also, this sketch will pre-set the EEPROM data for the + example sketch eeprom_get. + + Note, unlike the single byte version EEPROM.write(), + the put method will use update semantics. As in a byte + will only be written to the EEPROM if the data is actually + different. + + Written by Christopher Andrews 2015 + Released under MIT licence. +***/ + +#include + +struct MyObject { + float field1; + byte field2; + char name[10]; +}; + +void setup() { + + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + float f = 123.456f; //Variable to store in EEPROM. + int eeAddress = 0; //Location we want the data to be put. + + + //One simple call, with the address first and the object second. + EEPROM.put(eeAddress, f); + + Serial.println("Written float data type!"); + + /** Put is designed for use with custom structures also. **/ + + //Data to store. + MyObject customVar = { + 3.14f, + 65, + "Working!" + }; + + eeAddress += sizeof(float); //Move address to the next byte after float 'f'. + + EEPROM.put(eeAddress, customVar); + Serial.print("Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!"); +} + +void loop() { + /* Empty loop */ +} diff --git a/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino b/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino new file mode 100644 index 0000000..d465035 --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino @@ -0,0 +1,56 @@ +/* + * EEPROM Read + * + * Reads the value of each byte of the EEPROM and prints it + * to the computer. + * This example code is in the public domain. + */ + +#include + +// start reading from the first byte (address 0) of the EEPROM +int address = 0; +byte value; + +void setup() { + // initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } +} + +void loop() { + // read a byte from the current address of the EEPROM + value = EEPROM.read(address); + + Serial.print(address); + Serial.print("\t"); + Serial.print(value, DEC); + Serial.println(); + + /*** + Advance to the next address, when at the end restart at the beginning. + + 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. + ***/ + address = address + 1; + if (address == EEPROM.length()) { + address = 0; + } + + /*** + As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an + EEPROM address is also doable by a bitwise and of the length - 1. + + ++address &= EEPROM.length() - 1; + ***/ + + delay(500); +} diff --git a/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino b/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino new file mode 100644 index 0000000..f5b0c0c --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino @@ -0,0 +1,71 @@ +/*** + EEPROM Update method + + Stores values read from analog input 0 into the EEPROM. + These values will stay in the EEPROM when the board is + turned off and may be retrieved later by another sketch. + + If a value has not changed in the EEPROM, it is not overwritten + which would reduce the life span of the EEPROM unnecessarily. + + Released using MIT licence. + ***/ + +#include + +/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/ +int address = 0; + +void setup() { + /** Empty setup **/ +} + +void loop() { + /*** + need to divide by 4 because analog inputs range from + 0 to 1023 and each byte of the EEPROM can only hold a + value from 0 to 255. + ***/ + int val = analogRead(0) / 4; + + /*** + Update the particular EEPROM cell. + these values will remain there when the board is + turned off. + ***/ + EEPROM.update(address, val); + + /*** + The function EEPROM.update(address, val) is equivalent to the following: + + if( EEPROM.read(address) != val ){ + EEPROM.write(address, val); + } + ***/ + + + /*** + Advance to the next address, when at the end restart at the beginning. + + 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. + ***/ + address = address + 1; + if (address == EEPROM.length()) { + address = 0; + } + + /*** + As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an + EEPROM address is also doable by a bitwise and of the length - 1. + + ++address &= EEPROM.length() - 1; + ***/ + + delay(100); +} diff --git a/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino b/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino new file mode 100644 index 0000000..64e835c --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino @@ -0,0 +1,60 @@ +/* + * EEPROM Write + * + * Stores values read from analog input 0 into the EEPROM. + * These values will stay in the EEPROM when the board is + * turned off and may be retrieved later by another sketch. + */ + +#include + +/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/ +int addr = 0; + +void setup() { + /** Empty setup. **/ +} + +void loop() { + /*** + Need to divide by 4 because analog inputs range from + 0 to 1023 and each byte of the EEPROM can only hold a + value from 0 to 255. + ***/ + + int val = analogRead(0) / 4; + + /*** + Write the value to the appropriate byte of the EEPROM. + these values will remain there when the board is + turned off. + ***/ + + EEPROM.write(addr, val); + + /*** + Advance to the next address, when at the end restart at the beginning. + + 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. + ***/ + addr = addr + 1; + if (addr == EEPROM.length()) { + addr = 0; + } + + /*** + As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an + EEPROM address is also doable by a bitwise and of the length - 1. + + ++addr &= EEPROM.length() - 1; + ***/ + + + delay(100); +} diff --git a/libraries/EEPROM/keywords.txt b/libraries/EEPROM/keywords.txt new file mode 100644 index 0000000..2cabc0b --- /dev/null +++ b/libraries/EEPROM/keywords.txt @@ -0,0 +1,22 @@ +####################################### +# Syntax Coloring Map For EEPROM +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +EEPROM KEYWORD1 +EERef KEYWORD1 +EEPtr KEYWORD2 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +update KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### + diff --git a/libraries/EEPROM/library.properties b/libraries/EEPROM/library.properties new file mode 100644 index 0000000..d04fe51 --- /dev/null +++ b/libraries/EEPROM/library.properties @@ -0,0 +1,10 @@ +name=EEPROM +version=0.0.0 +author=Arduino, Christopher Andrews +maintainer=Elron +sentence=Enables reading and writing to the permanent board storage. +paragraph=This library allows to read and write data in a memory type, the EEPROM, that keeps its content also when the board is powered off. The amount of EEPROM available depends on the microcontroller type. +category=Data Storage +url=http://www.arduino.cc/en/Reference/EEPROM +architectures=MIK32_Amur + diff --git a/libraries/EEPROM/src/.vscode/settings.json b/libraries/EEPROM/src/.vscode/settings.json new file mode 100644 index 0000000..fc590ca --- /dev/null +++ b/libraries/EEPROM/src/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "eeprom.h": "c" + } +} \ No newline at end of file diff --git a/libraries/EEPROM/src/EEPROM.cpp b/libraries/EEPROM/src/EEPROM.cpp new file mode 100644 index 0000000..500296b --- /dev/null +++ b/libraries/EEPROM/src/EEPROM.cpp @@ -0,0 +1,145 @@ +#include "EEPROM.h" +#include "mik32_hal_eeprom.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_PAGE_WORDS ) // Страница занимает 32*4 = 128 байт +#define EEPROM_END 0x1FFF +#define EEPROM_LENGHT (EEPROM_PAGE_SIZE * EEPROM_PAGE_COUNT) + +HAL_EEPROM_HandleTypeDef heeprom; + + +EEPtr EEPROMClass:: begin() +{ + heeprom.Instance = EEPROM_REGS; + heeprom.Mode = HAL_EEPROM_MODE_TWO_STAGE; + heeprom.ErrorCorrection = HAL_EEPROM_ECC_ENABLE; + heeprom.EnableInterrupt = HAL_EEPROM_SERR_DISABLE; + + HAL_EEPROM_Init(&heeprom); + HAL_EEPROM_CalculateTimings(&heeprom, OSC_SYSTEM_VALUE); + return 0x00; +} + +uint8_t EEPROMClass:: read( int idx ) +{ + if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) + return 0x00; + + 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, addr, read_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); + // Адрес искомого слова в 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 за вычетом адреса начала страницы + uint32_t word_idx = (word_addr - addr) / EEPROM_WORD_SIZE; + // Номер байта в составе слова + uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE; + // Вычленение нужного байта из страницы + 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 ) +{ + if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) + return; + + 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, addr, write_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); + // Адрес искомого слова в 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 за вычетом адреса начала страницы + uint32_t word_idx = (word_addr - addr) / EEPROM_WORD_SIZE; + // Номер байта в составе слова + uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE; + // Извлечение нужного байта + uint32_t byte = ((uint32_t)val) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8); + // Вычленение и замена нужного байта из страницы + 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, EEPROM_START_ADDR, write_data_buf, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT); +} + +// Делает то же, что и write, но записывает только если новый байт отличается от уже записанного +void EEPROMClass:: update( int idx, uint8_t val ) +{ + if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) + return; + + 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, addr, write_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); + // Адрес искомого слова в 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 за вычетом адреса начала страницы + uint32_t word_idx = (word_addr - addr) / EEPROM_WORD_SIZE; + // Номер байта в составе слова + uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE; + // Извлечение нужного байта + uint32_t byte = ((uint32_t)val) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8); + // Проверка того, что записанный байт отличается от нового + if((write_data_buf[word_idx] & ((uint32_t)(0xFF) << ((EEPROM_WORD_SIZE - byte_idx - 1)) * 8)) != 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, EEPROM_START_ADDR, write_data_buf, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT); + } +} + +uint16_t EEPROMClass:: length( void ) +{ + return EEPROM_LENGHT; +} + +template< typename T > +const T& EEPROMClass:: put(int idx, const T &t) { + // Создание объекта EEPtr, который указывает на начальный адрес в EEPROM. + EEPtr e = idx; + + // Указатель на данные, которые нужно записать в EEPROM. + const uint8_t *ptr = (const uint8_t*) &t; + + // Количество байт данных для записи. + int count = sizeof(T); + + // Цикл для записи данных побайтно в EEPROM. + for(; count ; --count, ++e) { + // Запись текущего байта данных в EEPROM. + (*e).update( *ptr++ ); + } + + // Возврат ссылки на записанный объект. + return t; +} + +T& EEPROMClass:: get(int idx, T &t) { + EEPtr e = idx; // Указатель на адрес в EEPROM + uint8_t *ptr = (uint8_t*) &t; // Преобразуем ссылку на объект данных в указатель на байт + for (int count = sizeof(T); count; --count, ++e) { + *ptr++ = *e; // Читаем данные из EEPROM и записываем в объект + } + return t; // Возвращаем ссылку на переданный объект с прочитанными данными +} + + + +// uint32_t* EEPROM_Read(uint16_t index) +// { +// uint32_t read_data_buf[]; +// HAL_EEPROM_Read(&heeprom, 0x1C00 + index, read_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); +// return read_data_buf; +// } \ No newline at end of file diff --git a/libraries/EEPROM/src/EEPROM.h b/libraries/EEPROM/src/EEPROM.h new file mode 100644 index 0000000..f770fe5 --- /dev/null +++ b/libraries/EEPROM/src/EEPROM.h @@ -0,0 +1,150 @@ +/* + 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 + +#include + +/*** + 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. + 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 ); + + //STL and C++11 iteration capability. + EEPtr begin(); + EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid. + uint16_t length(); + + template + const T& put(int address, const T& data); + + template + T& get(int address, T& data); + + //Functionality to 'get' and 'put' objects to and from EEPROM. + // template< typename T > T &get( int idx, T &t ){ + // EEPtr e = idx; + // uint8_t *ptr = (uint8_t*) &t; + // for( int count = sizeof(T) ; count ; --count, ++e ) *ptr++ = *e; + // return t; + // } + + // template< typename T > const T &put( int idx, const T &t ){ + // EEPtr e = idx; + // const uint8_t *ptr = (const uint8_t*) &t; + // for( int count = sizeof(T) ; count ; --count, ++e ) (*e).update( *ptr++ ); + // return t; + // } +}; + +static EEPROMClass EEPROM; +#endif -- 2.43.0 From f3b0e0bd8848fca03831bd79cef21ae96fba49d5 Mon Sep 17 00:00:00 2001 From: EgorShin <98223248+EgorShin@users.noreply.github.com> Date: Wed, 25 Sep 2024 17:31:36 +0700 Subject: [PATCH 02/11] =?UTF-8?q?=D0=A4=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D0=B8=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81=D0=B8=20=D0=B8=20=D1=87?= =?UTF-8?q?=D1=82=D0=B5=D0=BD=D0=B8=D1=8F=20eeprom=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=D1=8E=D1=82,=20=D0=BF=D0=B5=D1=80=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=D0=B7=D0=B2=D0=B0=D0=BB=20=D1=84=D0=B0=D0=B9=D0=BB?= =?UTF-8?q?=20eeprom.h=20=D0=B2=D0=BE=20=D0=B8=D1=81=D0=B1=D0=B5=D0=B6?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BE=D0=BD=D1=84=D0=BB=D0=B8?= =?UTF-8?q?=D0=BA=D1=82=D0=BE,=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20?= =?UTF-8?q?=D1=8D=D1=82=D0=BE=20eeprom=5Fdef.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/EEPROM.cpp | 145 ----------------- cores/arduino/EEPROM.h | 150 ------------------ .../peripherals/Include/mik32_hal_eeprom.h | 2 +- .../periphery/{eeprom.h => eeprom_def.h} | 0 libraries/EEPROM/src/EEPROM.cpp | 71 ++++----- libraries/EEPROM/src/EEPROM.h | 104 ++++++------ 6 files changed, 86 insertions(+), 386 deletions(-) delete mode 100644 cores/arduino/EEPROM.cpp delete mode 100644 cores/arduino/EEPROM.h rename cores/arduino/mik32/shared/periphery/{eeprom.h => eeprom_def.h} (100%) diff --git a/cores/arduino/EEPROM.cpp b/cores/arduino/EEPROM.cpp deleted file mode 100644 index 500296b..0000000 --- a/cores/arduino/EEPROM.cpp +++ /dev/null @@ -1,145 +0,0 @@ -#include "EEPROM.h" -#include "mik32_hal_eeprom.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_PAGE_WORDS ) // Страница занимает 32*4 = 128 байт -#define EEPROM_END 0x1FFF -#define EEPROM_LENGHT (EEPROM_PAGE_SIZE * EEPROM_PAGE_COUNT) - -HAL_EEPROM_HandleTypeDef heeprom; - - -EEPtr EEPROMClass:: begin() -{ - heeprom.Instance = EEPROM_REGS; - heeprom.Mode = HAL_EEPROM_MODE_TWO_STAGE; - heeprom.ErrorCorrection = HAL_EEPROM_ECC_ENABLE; - heeprom.EnableInterrupt = HAL_EEPROM_SERR_DISABLE; - - HAL_EEPROM_Init(&heeprom); - HAL_EEPROM_CalculateTimings(&heeprom, OSC_SYSTEM_VALUE); - return 0x00; -} - -uint8_t EEPROMClass:: read( int idx ) -{ - if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) - return 0x00; - - 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, addr, read_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); - // Адрес искомого слова в 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 за вычетом адреса начала страницы - uint32_t word_idx = (word_addr - addr) / EEPROM_WORD_SIZE; - // Номер байта в составе слова - uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE; - // Вычленение нужного байта из страницы - 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 ) -{ - if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) - return; - - 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, addr, write_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); - // Адрес искомого слова в 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 за вычетом адреса начала страницы - uint32_t word_idx = (word_addr - addr) / EEPROM_WORD_SIZE; - // Номер байта в составе слова - uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE; - // Извлечение нужного байта - uint32_t byte = ((uint32_t)val) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8); - // Вычленение и замена нужного байта из страницы - 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, EEPROM_START_ADDR, write_data_buf, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT); -} - -// Делает то же, что и write, но записывает только если новый байт отличается от уже записанного -void EEPROMClass:: update( int idx, uint8_t val ) -{ - if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) - return; - - 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, addr, write_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); - // Адрес искомого слова в 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 за вычетом адреса начала страницы - uint32_t word_idx = (word_addr - addr) / EEPROM_WORD_SIZE; - // Номер байта в составе слова - uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE; - // Извлечение нужного байта - uint32_t byte = ((uint32_t)val) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8); - // Проверка того, что записанный байт отличается от нового - if((write_data_buf[word_idx] & ((uint32_t)(0xFF) << ((EEPROM_WORD_SIZE - byte_idx - 1)) * 8)) != 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, EEPROM_START_ADDR, write_data_buf, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT); - } -} - -uint16_t EEPROMClass:: length( void ) -{ - return EEPROM_LENGHT; -} - -template< typename T > -const T& EEPROMClass:: put(int idx, const T &t) { - // Создание объекта EEPtr, который указывает на начальный адрес в EEPROM. - EEPtr e = idx; - - // Указатель на данные, которые нужно записать в EEPROM. - const uint8_t *ptr = (const uint8_t*) &t; - - // Количество байт данных для записи. - int count = sizeof(T); - - // Цикл для записи данных побайтно в EEPROM. - for(; count ; --count, ++e) { - // Запись текущего байта данных в EEPROM. - (*e).update( *ptr++ ); - } - - // Возврат ссылки на записанный объект. - return t; -} - -T& EEPROMClass:: get(int idx, T &t) { - EEPtr e = idx; // Указатель на адрес в EEPROM - uint8_t *ptr = (uint8_t*) &t; // Преобразуем ссылку на объект данных в указатель на байт - for (int count = sizeof(T); count; --count, ++e) { - *ptr++ = *e; // Читаем данные из EEPROM и записываем в объект - } - return t; // Возвращаем ссылку на переданный объект с прочитанными данными -} - - - -// uint32_t* EEPROM_Read(uint16_t index) -// { -// uint32_t read_data_buf[]; -// HAL_EEPROM_Read(&heeprom, 0x1C00 + index, read_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); -// return read_data_buf; -// } \ No newline at end of file diff --git a/cores/arduino/EEPROM.h b/cores/arduino/EEPROM.h deleted file mode 100644 index b8fb21a..0000000 --- a/cores/arduino/EEPROM.h +++ /dev/null @@ -1,150 +0,0 @@ -/* - 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 - -#include - -/*** - 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. - 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 ); - - //STL and C++11 iteration capability. - EEPtr begin(); - EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid. - uint16_t length(); - - template - const T& put(int address, const T& data); - - template - T& get(int address, T& data); - - //Functionality to 'get' and 'put' objects to and from EEPROM. - // template< typename T > T &get( int idx, T &t ){ - // EEPtr e = idx; - // uint8_t *ptr = (uint8_t*) &t; - // for( int count = sizeof(T) ; count ; --count, ++e ) *ptr++ = *e; - // return t; - // } - - // template< typename T > const T &put( int idx, const T &t ){ - // EEPtr e = idx; - // const uint8_t *ptr = (const uint8_t*) &t; - // for( int count = sizeof(T) ; count ; --count, ++e ) (*e).update( *ptr++ ); - // return t; - // } -}; - -/*static*/ EEPROMClass EEPROM; -#endif diff --git a/cores/arduino/mik32/hal/peripherals/Include/mik32_hal_eeprom.h b/cores/arduino/mik32/hal/peripherals/Include/mik32_hal_eeprom.h index 86f8836..4a9b584 100644 --- a/cores/arduino/mik32/hal/peripherals/Include/mik32_hal_eeprom.h +++ b/cores/arduino/mik32/hal/peripherals/Include/mik32_hal_eeprom.h @@ -7,7 +7,7 @@ extern "C" { #include "mik32_hal_def.h" #include -#include +#include #include #ifndef HAL_EEPROM_TIMEOUT diff --git a/cores/arduino/mik32/shared/periphery/eeprom.h b/cores/arduino/mik32/shared/periphery/eeprom_def.h similarity index 100% rename from cores/arduino/mik32/shared/periphery/eeprom.h rename to cores/arduino/mik32/shared/periphery/eeprom_def.h diff --git a/libraries/EEPROM/src/EEPROM.cpp b/libraries/EEPROM/src/EEPROM.cpp index 500296b..c07e7a0 100644 --- a/libraries/EEPROM/src/EEPROM.cpp +++ b/libraries/EEPROM/src/EEPROM.cpp @@ -1,19 +1,20 @@ #include "EEPROM.h" #include "mik32_hal_eeprom.h" +#include "mik32_hal.h" #define EEPROM_OP_TIMEOUT 100000 -#define EEPROM_PAGE_WORDS 32 +#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_PAGE_WORDS ) // Страница занимает 32*4 = 128 байт +#define EEPROM_PAGE_SIZE ( EEPROM_PAGE_WORDS * EEPROM_WORD_SIZE ) // Страница занимает 32*4 = 128 байт #define EEPROM_END 0x1FFF #define EEPROM_LENGHT (EEPROM_PAGE_SIZE * EEPROM_PAGE_COUNT) HAL_EEPROM_HandleTypeDef heeprom; -EEPtr EEPROMClass:: begin() +void EEPROMClass:: begin() { heeprom.Instance = EEPROM_REGS; heeprom.Mode = HAL_EEPROM_MODE_TWO_STAGE; @@ -22,20 +23,19 @@ EEPtr EEPROMClass:: begin() HAL_EEPROM_Init(&heeprom); HAL_EEPROM_CalculateTimings(&heeprom, OSC_SYSTEM_VALUE); - return 0x00; } uint8_t EEPROMClass:: read( int idx ) { - if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) - return 0x00; + if (((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) || (idx < 0)) + return 0x0; 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, addr, read_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); + HAL_EEPROM_Read(&heeprom, (uint16_t)addr, read_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); // Адрес искомого слова в 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 за вычетом адреса начала страницы @@ -43,12 +43,12 @@ uint8_t EEPROMClass:: read( int idx ) // Номер байта в составе слова uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE; // Вычленение нужного байта из страницы - 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)); + 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 ) { - if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) + if (((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) || (idx < 0)) return; uint32_t write_data_buf[EEPROM_PAGE_WORDS] = {}; @@ -56,7 +56,7 @@ void EEPROMClass:: write( int idx, uint8_t val ) // Вычисление адресы начала нужной страницы uint32_t addr = EEPROM_START_ADDR + (((uint32_t)idx) / EEPROM_PAGE_SIZE) * EEPROM_PAGE_SIZE; // Читаем нужную страницу - HAL_EEPROM_Read(&heeprom, addr, write_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); + HAL_EEPROM_Read(&heeprom, (uint16_t)addr, write_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); // Адрес искомого слова в 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 за вычетом адреса начала страницы @@ -64,17 +64,20 @@ void EEPROMClass:: write( int idx, uint8_t val ) // Номер байта в составе слова uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE; // Извлечение нужного байта - uint32_t byte = ((uint32_t)val) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8); + uint32_t byte = (((uint32_t)val) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8)); // Вычленение и замена нужного байта из страницы - write_data_buf[word_idx] = (write_data_buf[word_idx] & (~((uint32_t)(0xFF) << ((EEPROM_WORD_SIZE - byte_idx - 1)) * 8))) | byte; + write_data_buf[word_idx] = (write_data_buf[word_idx] & (~(((uint32_t)0xFF) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8)))) | byte; + + // Очистка нужной страницы + HAL_EEPROM_Erase(&heeprom, (uint16_t)addr, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT); - HAL_EEPROM_Write(&heeprom, EEPROM_START_ADDR, write_data_buf, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT); + HAL_EEPROM_Write(&heeprom, (uint16_t)addr, write_data_buf, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT); } // Делает то же, что и write, но записывает только если новый байт отличается от уже записанного void EEPROMClass:: update( int idx, uint8_t val ) { - if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) + if (((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) || (idx < 0)) return; uint32_t write_data_buf[EEPROM_PAGE_WORDS] = {}; @@ -92,23 +95,24 @@ void EEPROMClass:: update( int idx, uint8_t val ) // Извлечение нужного байта uint32_t byte = ((uint32_t)val) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8); // Проверка того, что записанный байт отличается от нового - if((write_data_buf[word_idx] & ((uint32_t)(0xFF) << ((EEPROM_WORD_SIZE - byte_idx - 1)) * 8)) != byte) + if((uint8_t)(( write_data_buf[word_idx] & (((uint32_t)0xFF) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8)) ) >> ((EEPROM_WORD_SIZE - byte_idx - 1) * 8)) != val) { + // Очистка нужной страницы + HAL_EEPROM_Erase(&heeprom, (uint16_t)addr, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT); // Вычленение и замена нужного байта из страницы - 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, EEPROM_START_ADDR, write_data_buf, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT); + 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 EEPROM_LENGHT; + return (uint16_t)EEPROM_LENGHT; } template< typename T > const T& EEPROMClass:: put(int idx, const T &t) { - // Создание объекта EEPtr, который указывает на начальный адрес в EEPROM. - EEPtr e = idx; + uint16_t ee_idx = (uint16_t)idx; // Указатель на данные, которые нужно записать в EEPROM. const uint8_t *ptr = (const uint8_t*) &t; @@ -117,29 +121,20 @@ const T& EEPROMClass:: put(int idx, const T &t) { int count = sizeof(T); // Цикл для записи данных побайтно в EEPROM. - for(; count ; --count, ++e) { + for(uint16_t i = 0; i < sizeof(T); i++) { // Запись текущего байта данных в EEPROM. - (*e).update( *ptr++ ); + EEPROM.update(ee_idx + i, *ptr++ ); } // Возврат ссылки на записанный объект. return t; } - +template< typename T > T& EEPROMClass:: get(int idx, T &t) { - EEPtr e = idx; // Указатель на адрес в EEPROM - uint8_t *ptr = (uint8_t*) &t; // Преобразуем ссылку на объект данных в указатель на байт - for (int count = sizeof(T); count; --count, ++e) { - *ptr++ = *e; // Читаем данные из EEPROM и записываем в объект - } + // EEPtr e = idx; // Указатель на адрес в EEPROM + // uint8_t *ptr = (uint8_t*) &t; // Преобразуем ссылку на объект данных в указатель на байт + // for (int count = sizeof(T); count; --count, ++e) { + // *ptr++ = *e; // Читаем данные из EEPROM и записываем в объект + // } return t; // Возвращаем ссылку на переданный объект с прочитанными данными -} - - - -// uint32_t* EEPROM_Read(uint16_t index) -// { -// uint32_t read_data_buf[]; -// HAL_EEPROM_Read(&heeprom, 0x1C00 + index, read_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); -// return read_data_buf; -// } \ No newline at end of file +} \ No newline at end of file diff --git a/libraries/EEPROM/src/EEPROM.h b/libraries/EEPROM/src/EEPROM.h index f770fe5..bca64a2 100644 --- a/libraries/EEPROM/src/EEPROM.h +++ b/libraries/EEPROM/src/EEPROM.h @@ -31,48 +31,48 @@ This class has an overhead of two bytes, similar to storing a pointer to an EEPROM cell. ***/ -struct EERef{ +// struct EERef{ - EERef( const int index ) - : index( index ) {} +// 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; } +// //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; } +// //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; } +// EERef &update( uint8_t in ) { return in != *this ? *this = in : *this; } - /** Prefix increment/decrement **/ - EERef& operator++() { return *this += 1; } - EERef& operator--() { return *this -= 1; } +// /** 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; - } +// /** 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; - } +// uint8_t operator-- (int){ +// uint8_t ret = **this; +// return --(*this), ret; +// } - int index; //Index of current EEPROM cell. -}; +// int index; //Index of current EEPROM cell. +// }; /*** EEPtr class. @@ -82,26 +82,26 @@ struct EERef{ increment/decrement operators. ***/ -struct EEPtr{ +// struct EEPtr{ - EEPtr( const int index ) - : index( index ) {} +// EEPtr( const int index ) +// : index( index ) {} - operator int() const { return index; } - EEPtr &operator=( int in ) { return index = in, *this; } +// 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; } +// //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--; } +// /** 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. -}; +// int index; //Index of current EEPROM cell. +// }; /*** EEPROMClass class. @@ -114,14 +114,14 @@ struct EEPtr{ struct EEPROMClass{ //Basic user access methods. - EERef operator[]( const int idx ) { return idx; } + // 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 ); //STL and C++11 iteration capability. - EEPtr begin(); - EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid. + 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(); template -- 2.43.0 From 57d35800294ff0c666353d8e7570077b46f639ae Mon Sep 17 00:00:00 2001 From: EgorShin <98223248+EgorShin@users.noreply.github.com> Date: Thu, 3 Oct 2024 18:08:50 +0700 Subject: [PATCH 03/11] =?UTF-8?q?=D0=9D=D0=B0=D0=BF=D0=B8=D1=81=D0=B0?= =?UTF-8?q?=D0=BB=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8E=20put,=20?= =?UTF-8?q?=D0=BD=D0=BE=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=BA=D0=B0=20=D0=BD=D0=B5=D0=BA=D0=BE=D1=80?= =?UTF-8?q?=D1=80=D0=B5=D0=BA=D1=82=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libraries/.vscode/settings.json | 10 ++++ libraries/EEPROM/src/EEPROM.cpp | 81 +++++++++++++++++++++----------- libraries/EEPROM/src/EEPROM.h | 83 ++++++++++++++++++++++++++++++++- 3 files changed, 144 insertions(+), 30 deletions(-) create mode 100644 libraries/.vscode/settings.json diff --git a/libraries/.vscode/settings.json b/libraries/.vscode/settings.json new file mode 100644 index 0000000..a6c7df7 --- /dev/null +++ b/libraries/.vscode/settings.json @@ -0,0 +1,10 @@ +{ + "files.associations": { + "cstdint": "cpp", + "compare": "cpp", + "array": "cpp", + "string": "cpp", + "string_view": "cpp", + "initializer_list": "cpp" + } +} \ No newline at end of file diff --git a/libraries/EEPROM/src/EEPROM.cpp b/libraries/EEPROM/src/EEPROM.cpp index c07e7a0..4ff50a0 100644 --- a/libraries/EEPROM/src/EEPROM.cpp +++ b/libraries/EEPROM/src/EEPROM.cpp @@ -2,15 +2,6 @@ #include "mik32_hal_eeprom.h" #include "mik32_hal.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_END 0x1FFF -#define EEPROM_LENGHT (EEPROM_PAGE_SIZE * EEPROM_PAGE_COUNT) - HAL_EEPROM_HandleTypeDef heeprom; @@ -27,8 +18,9 @@ void EEPROMClass:: begin() uint8_t EEPROMClass:: read( int idx ) { + // Проверка адекватности адреса idx if (((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) || (idx < 0)) - return 0x0; + return 0x0; // #TODO Добавить сообщение об ошибке uint32_t read_data_buf[EEPROM_PAGE_WORDS] = {}; @@ -48,8 +40,9 @@ 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; + return; // #TODO Добавить сообщение об ошибке uint32_t write_data_buf[EEPROM_PAGE_WORDS] = {}; @@ -77,15 +70,16 @@ void EEPROMClass:: write( int idx, uint8_t val ) // Делает то же, что и write, но записывает только если новый байт отличается от уже записанного void EEPROMClass:: update( int idx, uint8_t val ) { + // Проверка адекватности адреса idx if (((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) || (idx < 0)) - return; + return; // #TODO Добавить сообщение об ошибке 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, addr, write_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); + HAL_EEPROM_Read(&heeprom, (uint16_t)addr, write_data_buf, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); // Адрес искомого слова в 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 за вычетом адреса начала страницы @@ -110,25 +104,41 @@ uint16_t EEPROMClass:: length( void ) return (uint16_t)EEPROM_LENGHT; } -template< typename T > -const T& EEPROMClass:: put(int idx, const T &t) { - uint16_t ee_idx = (uint16_t)idx; +// // template< typename T > +// void* EEPROMClass:: put(int idx, void* 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 Добавить сообщение об ошибке + +// uint32_t write_data_buf[EEPROM_PAGE_WORDS] = {}; - // Указатель на данные, которые нужно записать в EEPROM. - const uint8_t *ptr = (const uint8_t*) &t; +// // Вычисление адресы начала нужной страницы +// 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); +// uint8_t size = sizeof(dataPointer); +// memcpy((void *)((uint8_t *)write_data_buf + idx), (void *)&size, 20); - // Количество байт данных для записи. - int count = sizeof(T); +// HAL_EEPROM_Erase(&heeprom, (uint16_t)addr, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT); +// HAL_EEPROM_Write(&heeprom, (uint16_t)addr, write_data_buf, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT); - // Цикл для записи данных побайтно в EEPROM. - for(uint16_t i = 0; i < sizeof(T); i++) { - // Запись текущего байта данных в EEPROM. - EEPROM.update(ee_idx + i, *ptr++ ); - } +// // Количество байт данных для записи. +// // int count = sizeof(T); - // Возврат ссылки на записанный объект. - return t; -} +// // Цикл для записи данных побайтно в EEPROM. +// // for(uint16_t i = 0; i < sizeof(T); i++) { +// // // Запись текущего байта данных в EEPROM. +// // EEPROM.update(ee_idx + i, *ptr++ ); +// // } + +// // Запись данных в eeprom +// // Возврат ссылки на записанный объект. +// return dataPointer; +// } template< typename T > T& EEPROMClass:: get(int idx, T &t) { // EEPtr e = idx; // Указатель на адрес в EEPROM @@ -137,4 +147,19 @@ T& EEPROMClass:: get(int idx, T &t) { // *ptr++ = *e; // Читаем данные из EEPROM и записываем в объект // } return t; // Возвращаем ссылку на переданный объект с прочитанными данными +} + +void HAL_read(uint16_t addr, uint32_t * data) +{ + HAL_EEPROM_Read(&heeprom, (uint16_t)addr, data, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); +} + +void HAL_write(uint16_t addr, uint32_t * data) +{ + HAL_EEPROM_Write(&heeprom, (uint16_t)addr, data, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT); +} + +void HAL_erase(uint16_t addr) +{ + HAL_EEPROM_Erase(&heeprom, (uint16_t)addr, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT); } \ No newline at end of file diff --git a/libraries/EEPROM/src/EEPROM.h b/libraries/EEPROM/src/EEPROM.h index bca64a2..ca1116d 100644 --- a/libraries/EEPROM/src/EEPROM.h +++ b/libraries/EEPROM/src/EEPROM.h @@ -22,6 +22,20 @@ #define EEPROM_h #include +#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_END 0x1FFF +#define EEPROM_LENGHT (EEPROM_PAGE_SIZE * EEPROM_PAGE_COUNT) + +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. @@ -124,8 +138,73 @@ struct EEPROMClass{ 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(); - template - const T& put(int address, const T& data); + template< typename T > + 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 Добавить сообщение об ошибке + + uint32_t write_data_buf[EEPROM_PAGE_WORDS] = {}; + // Размер данных под запись + uint32_t dataSize = sizeof(dataPointer); + // Сдвиг начала записи данных + uint32_t dataShift = 0; + uint32_t writeSize = dataSize; + + // Вычисление адресы начала нужной страницы + uint32_t addr = EEPROM_START_ADDR + (((uint32_t)idx) / EEPROM_PAGE_SIZE) * EEPROM_PAGE_SIZE; + // Адрес начала данных на странице + uint32_t byte_addr = (uint32_t)idx % EEPROM_PAGE_SIZE; + // Чтение первой страницы + HAL_read((uint16_t)addr, write_data_buf); + // Если данные не влезут на первую страницу, то записываем только то, что влезло + if (EEPROM_PAGE_SIZE - byte_addr < dataSize) + { + writeSize = EEPROM_PAGE_SIZE - byte_addr; + } + dataSize -= writeSize; + // Копирование данных в буфер для записи в три этапа: первый - копирование того, что идёт до первого целого слова, + // второй - копирование целых слов, третий - копирование того, что идёт после последнего целого слова + memcpy((void *)((uint8_t *)write_data_buf + byte_addr), (void*)dataPointer, writeSize); + + HAL_erase((uint16_t)addr); + HAL_write((uint16_t)addr, write_data_buf); + // Если данные остались после записи первой страницы, то пишем их постранично, пока не закончатся + while (dataSize > 0) + { + addr += EEPROM_PAGE_SIZE; + HAL_read((uint16_t)addr, write_data_buf); + dataShift += writeSize; + writeSize = dataSize; + if (EEPROM_PAGE_SIZE < dataSize) + { + writeSize = EEPROM_PAGE_SIZE; + dataShift += writeSize; + } + dataSize -= writeSize; + memcpy((void *)((uint8_t *)write_data_buf), (void*)(dataPointer + dataShift), writeSize); + + HAL_erase((uint16_t)addr); + HAL_write((uint16_t)addr, write_data_buf); + } + + // Количество байт данных для записи. + // int count = sizeof(T); + + // Цикл для записи данных побайтно в EEPROM. + // for(uint16_t i = 0; i < sizeof(T); i++) { + // // Запись текущего байта данных в EEPROM. + // EEPROM.update(ee_idx + i, *ptr++ ); + // } + + // Запись данных в eeprom + // Возврат ссылки на записанный объект. + return dataPointer; + } template T& get(int address, T& data); -- 2.43.0 From 55c17cf2b6bf923809699c4019f7c5816cd3b01c Mon Sep 17 00:00:00 2001 From: EgorShin <98223248+EgorShin@users.noreply.github.com> Date: Tue, 8 Oct 2024 10:34:05 +0700 Subject: [PATCH 04/11] =?UTF-8?q?=D0=94=D0=BE=D0=BF=D0=B8=D1=81=D0=B0?= =?UTF-8?q?=D0=BB=20get=20=D0=B8=20put,=20=D0=BF=D0=BE=D1=87=D0=B8=D0=BD?= =?UTF-8?q?=D0=B8=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libraries/.vscode/c_cpp_properties.json | 18 ++++ libraries/.vscode/launch.json | 24 +++++ libraries/.vscode/settings.json | 73 ++++++++++++-- libraries/EEPROM/src/EEPROM.cpp | 45 --------- libraries/EEPROM/src/EEPROM.h | 123 +++++++++++++++++++++--- 5 files changed, 219 insertions(+), 64 deletions(-) create mode 100644 libraries/.vscode/c_cpp_properties.json create mode 100644 libraries/.vscode/launch.json diff --git a/libraries/.vscode/c_cpp_properties.json b/libraries/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..72461c3 --- /dev/null +++ b/libraries/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "windows-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "D:/Progi/Strawberry/c/bin/gcc.exe", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "windows-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/libraries/.vscode/launch.json b/libraries/.vscode/launch.json new file mode 100644 index 0000000..abd5da5 --- /dev/null +++ b/libraries/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": true, + "cwd": "c:/Users/biska/AppData/Local/Arduino15/packages/Elron/hardware/MIK32_Amur/0.2.0/libraries/EEPROM/src", + "program": "c:/Users/biska/AppData/Local/Arduino15/packages/Elron/hardware/MIK32_Amur/0.2.0/libraries/EEPROM/src/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/libraries/.vscode/settings.json b/libraries/.vscode/settings.json index a6c7df7..b12517e 100644 --- a/libraries/.vscode/settings.json +++ b/libraries/.vscode/settings.json @@ -1,10 +1,67 @@ { - "files.associations": { - "cstdint": "cpp", - "compare": "cpp", - "array": "cpp", - "string": "cpp", - "string_view": "cpp", - "initializer_list": "cpp" - } + "files.associations": { + "cstdint": "cpp", + "compare": "cpp", + "array": "cpp", + "string": "cpp", + "string_view": "cpp", + "initializer_list": "cpp" + }, + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false } \ No newline at end of file diff --git a/libraries/EEPROM/src/EEPROM.cpp b/libraries/EEPROM/src/EEPROM.cpp index 4ff50a0..4ab2572 100644 --- a/libraries/EEPROM/src/EEPROM.cpp +++ b/libraries/EEPROM/src/EEPROM.cpp @@ -104,51 +104,6 @@ uint16_t EEPROMClass:: length( void ) return (uint16_t)EEPROM_LENGHT; } -// // template< typename T > -// void* EEPROMClass:: put(int idx, void* 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 Добавить сообщение об ошибке - -// 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); -// uint8_t size = sizeof(dataPointer); -// memcpy((void *)((uint8_t *)write_data_buf + idx), (void *)&size, 20); - -// HAL_EEPROM_Erase(&heeprom, (uint16_t)addr, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT); -// HAL_EEPROM_Write(&heeprom, (uint16_t)addr, write_data_buf, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT); - -// // Количество байт данных для записи. -// // int count = sizeof(T); - -// // Цикл для записи данных побайтно в EEPROM. -// // for(uint16_t i = 0; i < sizeof(T); i++) { -// // // Запись текущего байта данных в EEPROM. -// // EEPROM.update(ee_idx + i, *ptr++ ); -// // } - -// // Запись данных в eeprom -// // Возврат ссылки на записанный объект. -// return dataPointer; -// } -template< typename T > -T& EEPROMClass:: get(int idx, T &t) { - // EEPtr e = idx; // Указатель на адрес в EEPROM - // uint8_t *ptr = (uint8_t*) &t; // Преобразуем ссылку на объект данных в указатель на байт - // for (int count = sizeof(T); count; --count, ++e) { - // *ptr++ = *e; // Читаем данные из EEPROM и записываем в объект - // } - return t; // Возвращаем ссылку на переданный объект с прочитанными данными -} - void HAL_read(uint16_t addr, uint32_t * data) { HAL_EEPROM_Read(&heeprom, (uint16_t)addr, data, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); diff --git a/libraries/EEPROM/src/EEPROM.h b/libraries/EEPROM/src/EEPROM.h index ca1116d..40471d1 100644 --- a/libraries/EEPROM/src/EEPROM.h +++ b/libraries/EEPROM/src/EEPROM.h @@ -154,42 +154,70 @@ 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; + // Индекс искомого слова в массиве write_data_buf: адрес слова в eeprom за вычетом адреса начала страницы + uint32_t word_idx = (word_addr - addr) / EEPROM_WORD_SIZE; // Адрес начала данных на странице uint32_t byte_addr = (uint32_t)idx % EEPROM_PAGE_SIZE; + // Номер байта в составе слова + uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE; // Чтение первой страницы HAL_read((uint16_t)addr, write_data_buf); // Если данные не влезут на первую страницу, то записываем только то, что влезло if (EEPROM_PAGE_SIZE - byte_addr < dataSize) { writeSize = EEPROM_PAGE_SIZE - byte_addr; + // lastWord = EEPROM_PAGE_WORDS - 1; } + uint32_t lastWord = (writeSize + byte_idx - 1) / EEPROM_WORD_SIZE + word_idx; dataSize -= writeSize; - // Копирование данных в буфер для записи в три этапа: первый - копирование того, что идёт до первого целого слова, - // второй - копирование целых слов, третий - копирование того, что идёт после последнего целого слова + // Запись данных постранично, сначала записываем первую страницу - отдельно memcpy((void *)((uint8_t *)write_data_buf + byte_addr), (void*)dataPointer, writeSize); - + // Разворачиваем слова + for(uint8_t i = word_idx; 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); + } + // for (uint8_t i = 0; i < writeSize; i++, dataPointer_8++) + // *(writePointer_8 + byte_addr + i) = *(dataPointer_8); HAL_erase((uint16_t)addr); HAL_write((uint16_t)addr, write_data_buf); // Если данные остались после записи первой страницы, то пишем их постранично, пока не закончатся while (dataSize > 0) { - addr += EEPROM_PAGE_SIZE; - HAL_read((uint16_t)addr, write_data_buf); dataShift += writeSize; + // addr = EEPROM_START_ADDR + ((dataShift) / EEPROM_PAGE_SIZE) * EEPROM_PAGE_SIZE; + addr += EEPROM_PAGE_SIZE; //Почему-то не работает с uint32_t данными + // addr = EEPROM_START_ADDR; + HAL_read((uint16_t)addr, write_data_buf); writeSize = dataSize; if (EEPROM_PAGE_SIZE < dataSize) { writeSize = EEPROM_PAGE_SIZE; - dataShift += writeSize; + // dataShift += writeSize; } - dataSize -= writeSize; - memcpy((void *)((uint8_t *)write_data_buf), (void*)(dataPointer + dataShift), writeSize); - + lastWord = (writeSize - 1) / EEPROM_WORD_SIZE; + memcpy((void *)(write_data_buf), (void*)((uint8_t *)dataPointer + dataShift), writeSize); + // Разворачиваем слова + 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); + } + // for (uint8_t i = 0; i < writeSize; i++, dataPointer_8++) + // *(write_data_buf + byte_addr + i) = *dataPointer_8; HAL_erase((uint16_t)addr); HAL_write((uint16_t)addr, write_data_buf); + dataSize -= writeSize; } // Количество байт данных для записи. @@ -206,8 +234,81 @@ struct EEPROMClass{ return dataPointer; } - template - T& get(int address, T& data); + template< typename T > + T& get(int idx, T& dataPointer) { + // EEPtr e = idx; // Указатель на адрес в EEPROM + // uint8_t *ptr = (uint8_t*) &t; // Преобразуем ссылку на объект данных в указатель на байт + // for (int count = sizeof(T); count; --count, ++e) { + // *ptr++ = *e; // Читаем данные из EEPROM и записываем в объект + // } + + // Проверка адекватности адреса idx + if (((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) || (idx < 0)) + return dataPointer; // #TODO Добавить сообщение об ошибке + + uint32_t read_data_buf[EEPROM_PAGE_WORDS] = {}; + // Размер данных + uint32_t dataSize = sizeof(dataPointer); + // Сдвиг начала чтения данных + 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; + // Индекс искомого слова в массиве write_data_buf: адрес слова в eeprom за вычетом адреса начала страницы + uint32_t word_idx = (word_addr - addr) / EEPROM_WORD_SIZE; + // Адрес начала данных на странице + uint32_t byte_addr = (uint32_t)idx % EEPROM_PAGE_SIZE; + // Номер байта в составе слова + uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE; + // Чтение первой страницы + HAL_read((uint16_t)addr, read_data_buf); + uint32_t lastWord = (dataSize + byte_idx - 1) / EEPROM_WORD_SIZE + word_idx; + if (EEPROM_PAGE_SIZE - byte_addr < dataSize) + { + readSize = EEPROM_PAGE_SIZE - byte_addr; + lastWord = EEPROM_PAGE_WORDS - 1; + } + dataSize -= readSize; + // Разворачиваем слова + 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); + } + // Чтение данных постранично, сначала читаем первую страницу - отдельно + memcpy((void *)dataPointer, (void*)((uint8_t *)read_data_buf + byte_addr), readSize); + + // Если данные остались после записи первой страницы, то пишем их постранично, пока не закончатся + while (dataSize > 0) + { + addr += EEPROM_PAGE_SIZE; + HAL_read((uint16_t)addr, read_data_buf); + dataShift += readSize; + readSize = dataSize; + if (EEPROM_PAGE_SIZE < dataSize) + { + readSize = EEPROM_PAGE_SIZE; + // dataShift += readSize; + } + lastWord = (dataSize - 1) / EEPROM_WORD_SIZE; + dataSize -= readSize; + // Разворачиваем слова + 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); + } + memcpy((void *)(dataPointer + dataShift), (void*)((uint8_t *)read_data_buf), readSize); + // for (uint8_t i = 0; i < readSize; i++, dataPointer_8++) + // *(read_data_buf + byte_addr + i) = *dataPointer_8; + } + return dataPointer; // Возвращаем ссылку на переданный объект с прочитанными данными +} //Functionality to 'get' and 'put' objects to and from EEPROM. // template< typename T > T &get( int idx, T &t ){ -- 2.43.0 From 577171f58f9476bfb2f8cb1d564ade3eeaa47fd7 Mon Sep 17 00:00:00 2001 From: EgorShin <98223248+EgorShin@users.noreply.github.com> Date: Tue, 8 Oct 2024 12:31:18 +0700 Subject: [PATCH 05/11] =?UTF-8?q?=D0=94=D0=BE=D0=BF=D0=B8=D1=81=D0=B0?= =?UTF-8?q?=D0=BB=20get=20=D0=B8=20put,=20=D0=BF=D0=BE=D1=87=D0=B8=D0=BD?= =?UTF-8?q?=D0=B8=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libraries/EEPROM/src/EEPROM.cpp | 7 +++- libraries/EEPROM/src/EEPROM.h | 72 +++++---------------------------- 2 files changed, 15 insertions(+), 64 deletions(-) diff --git a/libraries/EEPROM/src/EEPROM.cpp b/libraries/EEPROM/src/EEPROM.cpp index 4ab2572..fd9bd3c 100644 --- a/libraries/EEPROM/src/EEPROM.cpp +++ b/libraries/EEPROM/src/EEPROM.cpp @@ -87,14 +87,17 @@ void EEPROMClass:: update( int idx, uint8_t val ) // Номер байта в составе слова uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE; // Извлечение нужного байта - uint32_t byte = ((uint32_t)val) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8); + uint32_t byte = ((uint32_t)val) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8); + // uint8_t oldVal = (uint8_t)(( write_data_buf[word_idx] & (((uint32_t)0xFF) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8)) ) >> ((EEPROM_WORD_SIZE - byte_idx - 1) * 8)); + uint8_t oldVal = (uint8_t)(*((uint8_t*)write_data_buf + word_idx + (EEPROM_WORD_SIZE - byte_idx - 1))); // Проверка того, что записанный байт отличается от нового - if((uint8_t)(( write_data_buf[word_idx] & (((uint32_t)0xFF) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8)) ) >> ((EEPROM_WORD_SIZE - byte_idx - 1) * 8)) != val) + if( oldVal != val ) { // Очистка нужной страницы HAL_EEPROM_Erase(&heeprom, (uint16_t)addr, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT); // Вычленение и замена нужного байта из страницы 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); } } diff --git a/libraries/EEPROM/src/EEPROM.h b/libraries/EEPROM/src/EEPROM.h index 40471d1..369bbd7 100644 --- a/libraries/EEPROM/src/EEPROM.h +++ b/libraries/EEPROM/src/EEPROM.h @@ -171,10 +171,7 @@ struct EEPROMClass{ HAL_read((uint16_t)addr, write_data_buf); // Если данные не влезут на первую страницу, то записываем только то, что влезло if (EEPROM_PAGE_SIZE - byte_addr < dataSize) - { writeSize = EEPROM_PAGE_SIZE - byte_addr; - // lastWord = EEPROM_PAGE_WORDS - 1; - } uint32_t lastWord = (writeSize + byte_idx - 1) / EEPROM_WORD_SIZE + word_idx; dataSize -= writeSize; // Запись данных постранично, сначала записываем первую страницу - отдельно @@ -186,24 +183,17 @@ 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); } - // for (uint8_t i = 0; i < writeSize; i++, dataPointer_8++) - // *(writePointer_8 + byte_addr + i) = *(dataPointer_8); HAL_erase((uint16_t)addr); HAL_write((uint16_t)addr, write_data_buf); // Если данные остались после записи первой страницы, то пишем их постранично, пока не закончатся while (dataSize > 0) { - dataShift += writeSize; - // addr = EEPROM_START_ADDR + ((dataShift) / EEPROM_PAGE_SIZE) * EEPROM_PAGE_SIZE; - addr += EEPROM_PAGE_SIZE; //Почему-то не работает с uint32_t данными - // addr = EEPROM_START_ADDR; + addr += EEPROM_PAGE_SIZE; HAL_read((uint16_t)addr, write_data_buf); + dataShift += writeSize; writeSize = dataSize; if (EEPROM_PAGE_SIZE < dataSize) - { writeSize = EEPROM_PAGE_SIZE; - // dataShift += writeSize; - } lastWord = (writeSize - 1) / EEPROM_WORD_SIZE; memcpy((void *)(write_data_buf), (void*)((uint8_t *)dataPointer + dataShift), writeSize); // Разворачиваем слова @@ -213,35 +203,16 @@ 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); } - // for (uint8_t i = 0; i < writeSize; i++, dataPointer_8++) - // *(write_data_buf + byte_addr + i) = *dataPointer_8; HAL_erase((uint16_t)addr); HAL_write((uint16_t)addr, write_data_buf); dataSize -= writeSize; } - - // Количество байт данных для записи. - // int count = sizeof(T); - - // Цикл для записи данных побайтно в EEPROM. - // for(uint16_t i = 0; i < sizeof(T); i++) { - // // Запись текущего байта данных в EEPROM. - // EEPROM.update(ee_idx + i, *ptr++ ); - // } - - // Запись данных в eeprom - // Возврат ссылки на записанный объект. return dataPointer; } template< typename T > - T& get(int idx, T& dataPointer) { - // EEPtr e = idx; // Указатель на адрес в EEPROM - // uint8_t *ptr = (uint8_t*) &t; // Преобразуем ссылку на объект данных в указатель на байт - // for (int count = sizeof(T); count; --count, ++e) { - // *ptr++ = *e; // Читаем данные из EEPROM и записываем в объект - // } - + T& get(int idx, T& dataPointer) + { // Проверка адекватности адреса idx if (((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) || (idx < 0)) return dataPointer; // #TODO Добавить сообщение об ошибке @@ -265,12 +236,9 @@ struct EEPROMClass{ uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE; // Чтение первой страницы HAL_read((uint16_t)addr, read_data_buf); - uint32_t lastWord = (dataSize + byte_idx - 1) / EEPROM_WORD_SIZE + word_idx; if (EEPROM_PAGE_SIZE - byte_addr < dataSize) - { readSize = EEPROM_PAGE_SIZE - byte_addr; - lastWord = EEPROM_PAGE_WORDS - 1; - } + uint32_t lastWord = (readSize + byte_idx - 1) / EEPROM_WORD_SIZE + word_idx; dataSize -= readSize; // Разворачиваем слова for(uint8_t i = word_idx; i <= lastWord; i++) @@ -290,40 +258,20 @@ struct EEPROMClass{ dataShift += readSize; readSize = dataSize; if (EEPROM_PAGE_SIZE < dataSize) - { readSize = EEPROM_PAGE_SIZE; - // dataShift += readSize; - } lastWord = (dataSize - 1) / EEPROM_WORD_SIZE; - dataSize -= readSize; // Разворачиваем слова - for(uint8_t i = word_idx; i <= lastWord; i++) + for(uint8_t i = 0; 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); } - memcpy((void *)(dataPointer + dataShift), (void*)((uint8_t *)read_data_buf), readSize); - // for (uint8_t i = 0; i < readSize; i++, dataPointer_8++) - // *(read_data_buf + byte_addr + i) = *dataPointer_8; + memcpy((void *)((uint8_t *)dataPointer + dataShift), (void*)(read_data_buf), readSize); + dataSize -= readSize; } - return dataPointer; // Возвращаем ссылку на переданный объект с прочитанными данными -} - - //Functionality to 'get' and 'put' objects to and from EEPROM. - // template< typename T > T &get( int idx, T &t ){ - // EEPtr e = idx; - // uint8_t *ptr = (uint8_t*) &t; - // for( int count = sizeof(T) ; count ; --count, ++e ) *ptr++ = *e; - // return t; - // } - - // template< typename T > const T &put( int idx, const T &t ){ - // EEPtr e = idx; - // const uint8_t *ptr = (const uint8_t*) &t; - // for( int count = sizeof(T) ; count ; --count, ++e ) (*e).update( *ptr++ ); - // return t; - // } + return dataPointer; // Возвращаем ссылку на переданный объект с прочитанными данными + } }; static EEPROMClass EEPROM; -- 2.43.0 From 08bca8b276f205c51702869c912e5aedba6296d7 Mon Sep 17 00:00:00 2001 From: EgorShin <98223248+EgorShin@users.noreply.github.com> Date: Tue, 8 Oct 2024 12:32:30 +0700 Subject: [PATCH 06/11] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F,=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=20up?= =?UTF-8?q?date=20=D1=82=D1=80=D0=B5=D0=B1=D1=83=D0=B5=D1=82=20=D0=BE?= =?UTF-8?q?=D1=82=D0=BB=D0=B0=D0=B4=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libraries/EEPROM/src/EEPROM.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/EEPROM/src/EEPROM.cpp b/libraries/EEPROM/src/EEPROM.cpp index fd9bd3c..bd15fdc 100644 --- a/libraries/EEPROM/src/EEPROM.cpp +++ b/libraries/EEPROM/src/EEPROM.cpp @@ -91,7 +91,7 @@ void EEPROMClass:: update( int idx, uint8_t val ) // uint8_t oldVal = (uint8_t)(( write_data_buf[word_idx] & (((uint32_t)0xFF) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8)) ) >> ((EEPROM_WORD_SIZE - byte_idx - 1) * 8)); uint8_t oldVal = (uint8_t)(*((uint8_t*)write_data_buf + word_idx + (EEPROM_WORD_SIZE - byte_idx - 1))); // Проверка того, что записанный байт отличается от нового - if( oldVal != val ) + if(oldVal != val) { // Очистка нужной страницы HAL_EEPROM_Erase(&heeprom, (uint16_t)addr, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT); -- 2.43.0 From 690acb404cf9fce4de61c8046d289d2b2075f6da Mon Sep 17 00:00:00 2001 From: EgorShin <98223248+EgorShin@users.noreply.github.com> Date: Wed, 9 Oct 2024 09:56:18 +0700 Subject: [PATCH 07/11] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BF=D1=80=D0=B8=20=D0=BE=D1=82=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=BA=D0=B5=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8?= =?UTF-8?q?=20update?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libraries/EEPROM/src/EEPROM.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libraries/EEPROM/src/EEPROM.cpp b/libraries/EEPROM/src/EEPROM.cpp index bd15fdc..981aa64 100644 --- a/libraries/EEPROM/src/EEPROM.cpp +++ b/libraries/EEPROM/src/EEPROM.cpp @@ -88,8 +88,7 @@ void EEPROMClass:: update( int idx, uint8_t val ) uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE; // Извлечение нужного байта uint32_t byte = ((uint32_t)val) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8); - // uint8_t oldVal = (uint8_t)(( write_data_buf[word_idx] & (((uint32_t)0xFF) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8)) ) >> ((EEPROM_WORD_SIZE - byte_idx - 1) * 8)); - uint8_t oldVal = (uint8_t)(*((uint8_t*)write_data_buf + word_idx + (EEPROM_WORD_SIZE - byte_idx - 1))); + uint8_t oldVal = (uint8_t)(*((uint8_t*)write_data_buf + word_idx * EEPROM_WORD_SIZE + (EEPROM_WORD_SIZE - byte_idx - 1))); // Проверка того, что записанный байт отличается от нового if(oldVal != val) { -- 2.43.0 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 08/11] =?UTF-8?q?=D0=94=D0=BE=D0=BF=D0=B8=D1=81=D0=B0?= =?UTF-8?q?=D0=BB=20=D1=86=D0=B8=D0=BA=D0=BB=D0=B8=D1=87=D0=B5=D1=81=D0=BA?= =?UTF-8?q?=D1=83=D1=8E=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81=D1=8C=20=D0=B8=20?= =?UTF-8?q?=D1=87=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; -- 2.43.0 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 09/11] =?UTF-8?q?=D0=9F=D1=80=D0=B8=D0=B2=D0=B5=D0=B4?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80=D0=B8=D0=BC=D0=B5=D1=80?= =?UTF-8?q?=D0=BE=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; -- 2.43.0 From f33566cb41868ac5d4da3c16d6eee3a7ab0008f7 Mon Sep 17 00:00:00 2001 From: EgorShin <98223248+EgorShin@users.noreply.github.com> Date: Tue, 15 Oct 2024 12:21:55 +0700 Subject: [PATCH 10/11] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=BE=D0=B1=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0=D1=85,?= =?UTF-8?q?=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=20write=20=D1=82?= =?UTF-8?q?=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=BF=D0=BE=D0=B2=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D1=8F=D0=B5=D1=82=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D1=8E=20update,=20put=20=D0=B8=20get=20=D1=82=D0=B5=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D1=8C=20=D0=BF=D1=80=D0=B8=D0=BD=D0=B8=D0=BC=D0=B0?= =?UTF-8?q?=D1=8E=D1=82=20=D0=BD=D0=B5=20=D1=83=D0=BA=D0=B0=D0=B7=D0=B0?= =?UTF-8?q?=D1=82=D0=B5=D0=BB=D0=B8,=20=D0=B0=20=D0=B4=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../eeprom_iteration/eeprom_iteration.ino | 3 -- .../EEPROM/examples/eeprom_put/eeprom_put.ino | 3 -- .../examples/eeprom_read/eeprom_read.ino | 21 +------- .../examples/eeprom_update/eeprom_update.ino | 23 +------- .../examples/eeprom_write/eeprom_write.ino | 23 +------- libraries/EEPROM/src/EEPROM.cpp | 53 +++++++++---------- libraries/EEPROM/src/EEPROM.h | 46 ++++++++++------ 7 files changed, 60 insertions(+), 112 deletions(-) diff --git a/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino b/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino index d4c3427..c01d241 100644 --- a/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino +++ b/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino @@ -6,9 +6,6 @@ Running this sketch is not necessary, this is simply highlighting certain programming methods. - - Written by Christopher Andrews 2015 - Released under MIT licence. ***/ #include diff --git a/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino b/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino index 29e1cca..9c452f9 100644 --- a/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino +++ b/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino @@ -9,9 +9,6 @@ the put method will use update semantics. As in a byte will only be written to the EEPROM if the data is actually different. - - Written by Christopher Andrews 2015 - Released under MIT licence. ***/ #include diff --git a/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino b/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino index a2980b7..ec6e0cf 100644 --- a/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino +++ b/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino @@ -3,7 +3,6 @@ * * Reads the value of each byte of the EEPROM and prints it * to the computer. - * This example code is in the public domain. */ #include @@ -29,29 +28,11 @@ void loop() { Serial.print("\t"); Serial.print(value, DEC); Serial.println(); - - /*** - Advance to the next address, when at the end restart at the beginning. - - 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. - ***/ + address = address + 1; if (address == EEPROM.length()) { address = 0; } - /*** - As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an - EEPROM address is also doable by a bitwise and of the length - 1. - - ++address &= EEPROM.length() - 1; - ***/ - delay(500); } diff --git a/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino b/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino index b80051e..93ea2b4 100644 --- a/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino +++ b/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino @@ -7,8 +7,6 @@ If a value has not changed in the EEPROM, it is not overwritten which would reduce the life span of the EEPROM unnecessarily. - - Released using MIT licence. ***/ #include @@ -26,7 +24,7 @@ void loop() { 0 to 1023 and each byte of the EEPROM can only hold a value from 0 to 255. ***/ - int val = analogRead(0) / 4; + int val = analogRead(0) / 16; /*** Update the particular EEPROM cell. @@ -43,29 +41,10 @@ void loop() { } ***/ - - /*** - Advance to the next address, when at the end restart at the beginning. - - 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. - ***/ address = address + 1; if (address == EEPROM.length()) { address = 0; } - /*** - As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an - EEPROM address is also doable by a bitwise and of the length - 1. - - ++address &= EEPROM.length() - 1; - ***/ - delay(100); } diff --git a/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino b/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino index b83fe59..2b23347 100644 --- a/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino +++ b/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino @@ -22,7 +22,7 @@ void loop() { value from 0 to 255. ***/ - int val = analogRead(0) / 4; + int val = analogRead(0) / 16; /*** Write the value to the appropriate byte of the EEPROM. @@ -32,29 +32,10 @@ void loop() { EEPROM.write(addr, val); - /*** - Advance to the next address, when at the end restart at the beginning. - - 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. - ***/ addr = addr + 1; if (addr == EEPROM.length()) { addr = 0; } - - /*** - As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an - EEPROM address is also doable by a bitwise and of the length - 1. - - ++addr &= EEPROM.length() - 1; - ***/ - - + delay(100); } diff --git a/libraries/EEPROM/src/EEPROM.cpp b/libraries/EEPROM/src/EEPROM.cpp index 12dc0a0..2c37209 100644 --- a/libraries/EEPROM/src/EEPROM.cpp +++ b/libraries/EEPROM/src/EEPROM.cpp @@ -16,13 +16,19 @@ void EEPROMClass:: begin() HAL_EEPROM_CalculateTimings(&heeprom, OSC_SYSTEM_VALUE); } -uint8_t read( int idx ) +uint8_t read_byte( int idx ) { // Проверка адекватности адреса idx if (idx < 0) + { idx = -idx; + ErrorMsgHandler("EEPROM.read(): The eeprom cell address must be non-negative"); + } if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) - idx = (int)((uint32_t)idx % EEPROM_LENGHT); + { + idx = (int)((uint32_t)idx % EEPROM_LENGHT); + ErrorMsgHandler("EEPROM.read(): The address of the eeprom cell goes beyond the eeprom"); + } uint32_t read_data_buf[EEPROM_PAGE_WORDS] = {}; @@ -40,45 +46,36 @@ uint8_t 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 write( int idx, uint8_t val ) +void write_byte( int idx, uint8_t val ) { // Проверка адекватности адреса idx if (idx < 0) + { idx = -idx; + ErrorMsgHandler("EEPROM.write(): The eeprom cell address must be non-negative"); + } 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); - // Адрес искомого слова в 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 за вычетом адреса начала страницы - uint32_t word_idx = (word_addr - addr) / EEPROM_WORD_SIZE; - // Номер байта в составе слова - uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE; - // Извлечение нужного байта - uint32_t byte = (((uint32_t)val) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8)); - // Вычленение и замена нужного байта из страницы - write_data_buf[word_idx] = (write_data_buf[word_idx] & (~(((uint32_t)0xFF) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8)))) | byte; - - // Очистка нужной страницы - HAL_EEPROM_Erase(&heeprom, (uint16_t)addr, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT); - - HAL_EEPROM_Write(&heeprom, (uint16_t)addr, write_data_buf, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT); + { + idx = (int)((uint32_t)idx % EEPROM_LENGHT); + ErrorMsgHandler("EEPROM.write(): The address of the eeprom cell goes beyond the eeprom"); + } + update_byte(idx, val); } // Делает то же, что и write, но записывает только если новый байт отличается от уже записанного -void update( int idx, uint8_t val ) +void update_byte( int idx, uint8_t val ) { // Проверка адекватности адреса idx if (idx < 0) + { idx = -idx; + ErrorMsgHandler("EEPROM.update(): The eeprom cell address must be non-negative"); + } if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) - idx = (int)((uint32_t)idx % EEPROM_LENGHT); + { + idx = (int)((uint32_t)idx % EEPROM_LENGHT); + ErrorMsgHandler("EEPROM.update(): The address of the eeprom cell goes beyond the eeprom"); + } uint32_t write_data_buf[EEPROM_PAGE_WORDS] = {}; diff --git a/libraries/EEPROM/src/EEPROM.h b/libraries/EEPROM/src/EEPROM.h index 1eeb4bb..fe79006 100644 --- a/libraries/EEPROM/src/EEPROM.h +++ b/libraries/EEPROM/src/EEPROM.h @@ -16,20 +16,22 @@ 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 ); +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 ) : index( index ) {} - uint8_t operator*() const { return read( index ); } + uint8_t operator*() const { return read_byte( 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 write_byte( 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; } @@ -86,24 +88,31 @@ struct EEPROMClass{ 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 ); } + void update( int idx, uint8_t val ) { update_byte(idx, val); } void begin(); - EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid. + 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) + T &put(int idx, T &data) { + void* dataPointer = (void*)&data; // Проверка адекватности адреса idx if (idx < 0) + { idx = -idx; + ErrorMsgHandler("EEPROM.put(): The eeprom cell address must be non-negative"); + } if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) - idx = (int)((uint32_t)idx % EEPROM_LENGHT); + { + idx = (int)((uint32_t)idx % EEPROM_LENGHT); + ErrorMsgHandler("EEPROM.put(): The address of the eeprom cell goes beyond the eeprom"); + } uint32_t write_data_buf[EEPROM_PAGE_WORDS] = {}; // Размер данных под запись - uint32_t dataSize = sizeof(dataPointer); + uint32_t dataSize = sizeof(data); // Сдвиг начала записи данных uint32_t dataShift = 0; uint32_t writeSize = dataSize; @@ -162,21 +171,28 @@ struct EEPROMClass{ HAL_write((uint16_t)addr, write_data_buf); dataSize -= writeSize; } - return dataPointer; + return data; } template< typename T > - T& get(int idx, T& dataPointer) + T &get(int idx, T &data) { + void* dataPointer = (void*)&data; // Проверка адекватности адреса idx if (idx < 0) + { idx = -idx; + ErrorMsgHandler("EEPROM.get(): The eeprom cell address must be non-negative"); + } if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT) - idx = (int)((uint32_t)idx % EEPROM_LENGHT); + { + idx = (int)((uint32_t)idx % EEPROM_LENGHT); + ErrorMsgHandler("EEPROM.get(): The address of the eeprom cell goes beyond the eeprom"); + } uint32_t read_data_buf[EEPROM_PAGE_WORDS] = {}; // Размер данных - uint32_t dataSize = sizeof(dataPointer); + uint32_t dataSize = sizeof(data); // Сдвиг начала чтения данных uint32_t dataShift = 0; uint32_t readSize = dataSize; @@ -230,7 +246,7 @@ struct EEPROMClass{ memcpy((void *)((uint8_t *)dataPointer + dataShift), (void*)(read_data_buf), readSize); dataSize -= readSize; } - return dataPointer; // Возвращаем ссылку на переданный объект с прочитанными данными + return data; // Возвращаем ссылку на переданный объект с прочитанными данными } }; -- 2.43.0 From b01c6ff655a996c1e7177e36b9cdc25b264cc470 Mon Sep 17 00:00:00 2001 From: EgorShin <98223248+EgorShin@users.noreply.github.com> Date: Tue, 15 Oct 2024 12:46:14 +0700 Subject: [PATCH 11/11] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BF=D0=B0=D0=BF=D0=BA=D0=B0=20.vscode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- libraries/.vscode/c_cpp_properties.json | 18 ------ libraries/.vscode/launch.json | 24 -------- libraries/.vscode/settings.json | 67 ---------------------- libraries/EEPROM/.gitignore | 2 + libraries/EEPROM/src/.vscode/settings.json | 5 -- 6 files changed, 4 insertions(+), 115 deletions(-) delete mode 100644 libraries/.vscode/c_cpp_properties.json delete mode 100644 libraries/.vscode/launch.json delete mode 100644 libraries/.vscode/settings.json create mode 100644 libraries/EEPROM/.gitignore delete mode 100644 libraries/EEPROM/src/.vscode/settings.json diff --git a/.gitignore b/.gitignore index 5d15477..5bce97a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -installed.json \ No newline at end of file +installed.json +.vscode/ \ No newline at end of file diff --git a/libraries/.vscode/c_cpp_properties.json b/libraries/.vscode/c_cpp_properties.json deleted file mode 100644 index 72461c3..0000000 --- a/libraries/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "configurations": [ - { - "name": "windows-gcc-x64", - "includePath": [ - "${workspaceFolder}/**" - ], - "compilerPath": "D:/Progi/Strawberry/c/bin/gcc.exe", - "cStandard": "${default}", - "cppStandard": "${default}", - "intelliSenseMode": "windows-gcc-x64", - "compilerArgs": [ - "" - ] - } - ], - "version": 4 -} \ No newline at end of file diff --git a/libraries/.vscode/launch.json b/libraries/.vscode/launch.json deleted file mode 100644 index abd5da5..0000000 --- a/libraries/.vscode/launch.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - "name": "C/C++ Runner: Debug Session", - "type": "cppdbg", - "request": "launch", - "args": [], - "stopAtEntry": false, - "externalConsole": true, - "cwd": "c:/Users/biska/AppData/Local/Arduino15/packages/Elron/hardware/MIK32_Amur/0.2.0/libraries/EEPROM/src", - "program": "c:/Users/biska/AppData/Local/Arduino15/packages/Elron/hardware/MIK32_Amur/0.2.0/libraries/EEPROM/src/build/Debug/outDebug", - "MIMode": "gdb", - "miDebuggerPath": "gdb", - "setupCommands": [ - { - "description": "Enable pretty-printing for gdb", - "text": "-enable-pretty-printing", - "ignoreFailures": true - } - ] - } - ] -} \ No newline at end of file diff --git a/libraries/.vscode/settings.json b/libraries/.vscode/settings.json deleted file mode 100644 index b12517e..0000000 --- a/libraries/.vscode/settings.json +++ /dev/null @@ -1,67 +0,0 @@ -{ - "files.associations": { - "cstdint": "cpp", - "compare": "cpp", - "array": "cpp", - "string": "cpp", - "string_view": "cpp", - "initializer_list": "cpp" - }, - "C_Cpp_Runner.cCompilerPath": "gcc", - "C_Cpp_Runner.cppCompilerPath": "g++", - "C_Cpp_Runner.debuggerPath": "gdb", - "C_Cpp_Runner.cStandard": "", - "C_Cpp_Runner.cppStandard": "", - "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat", - "C_Cpp_Runner.useMsvc": false, - "C_Cpp_Runner.warnings": [ - "-Wall", - "-Wextra", - "-Wpedantic", - "-Wshadow", - "-Wformat=2", - "-Wcast-align", - "-Wconversion", - "-Wsign-conversion", - "-Wnull-dereference" - ], - "C_Cpp_Runner.msvcWarnings": [ - "/W4", - "/permissive-", - "/w14242", - "/w14287", - "/w14296", - "/w14311", - "/w14826", - "/w44062", - "/w44242", - "/w14905", - "/w14906", - "/w14263", - "/w44265", - "/w14928" - ], - "C_Cpp_Runner.enableWarnings": true, - "C_Cpp_Runner.warningsAsError": false, - "C_Cpp_Runner.compilerArgs": [], - "C_Cpp_Runner.linkerArgs": [], - "C_Cpp_Runner.includePaths": [], - "C_Cpp_Runner.includeSearch": [ - "*", - "**/*" - ], - "C_Cpp_Runner.excludeSearch": [ - "**/build", - "**/build/**", - "**/.*", - "**/.*/**", - "**/.vscode", - "**/.vscode/**" - ], - "C_Cpp_Runner.useAddressSanitizer": false, - "C_Cpp_Runner.useUndefinedSanitizer": false, - "C_Cpp_Runner.useLeakSanitizer": false, - "C_Cpp_Runner.showCompilationTime": false, - "C_Cpp_Runner.useLinkTimeOptimization": false, - "C_Cpp_Runner.msvcSecureNoWarnings": false -} \ No newline at end of file diff --git a/libraries/EEPROM/.gitignore b/libraries/EEPROM/.gitignore new file mode 100644 index 0000000..5bce97a --- /dev/null +++ b/libraries/EEPROM/.gitignore @@ -0,0 +1,2 @@ +installed.json +.vscode/ \ No newline at end of file diff --git a/libraries/EEPROM/src/.vscode/settings.json b/libraries/EEPROM/src/.vscode/settings.json deleted file mode 100644 index fc590ca..0000000 --- a/libraries/EEPROM/src/.vscode/settings.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "files.associations": { - "eeprom.h": "c" - } -} \ No newline at end of file -- 2.43.0