Приведение примеров eeprom
This commit is contained in:
1 parent
c9f5563111
commit
52adac2b59
10 files changed
+88
-32
No files matched your search
@@ -12,18 +12,11 @@
|
|||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// initialize the LED pin as an output.
|
// initialize the LED pin as an output.
|
||||||
pinMode(13, OUTPUT);
|
pinMode(LED_BUILTIN, OUTPUT);
|
||||||
|
EEPROM.begin();
|
||||||
|
|
||||||
/***
|
/***
|
||||||
Iterate through each byte of the EEPROM storage.
|
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++) {
|
for (int i = 0 ; i < EEPROM.length() ; i++) {
|
||||||
@@ -31,7 +24,7 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// turn the LED on when we're done
|
// turn the LED on when we're done
|
||||||
digitalWrite(13, HIGH);
|
digitalWrite(LED_BUILTIN, HIGH);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|||||||
@@ -1,19 +1,18 @@
|
|||||||
/***
|
/***
|
||||||
Written by Christopher Andrews.
|
CRC algorithm generated by pycrc ( https://github.com/tpircher/pycrc ).
|
||||||
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.
|
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.
|
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.
|
The purpose of this example is to highlight how the EEPROM object can be used just like an array.
|
||||||
***/
|
***/
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
#include <EEPROM.h>
|
#include <EEPROM.h>
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
//Start serial
|
//Start serial
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
EEPROM.begin();
|
||||||
while (!Serial) {
|
while (!Serial) {
|
||||||
; // wait for serial port to connect. Needed for native USB port only
|
; // wait for serial port to connect. Needed for native USB port only
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,14 +10,12 @@
|
|||||||
This may cause the serial object to print out a large string
|
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
|
of garbage if there is no null character inside one of the strings
|
||||||
loaded.
|
loaded.
|
||||||
|
|
||||||
Written by Christopher Andrews 2015
|
|
||||||
Released under MIT licence.
|
|
||||||
***/
|
***/
|
||||||
|
|
||||||
#include <EEPROM.h>
|
#include <EEPROM.h>
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
EEPROM.begin();
|
||||||
|
|
||||||
float f = 0.00f; //Variable to store data read from EEPROM.
|
float f = 0.00f; //Variable to store data read from EEPROM.
|
||||||
int eeAddress = 0; //EEPROM address to start reading from
|
int eeAddress = 0; //EEPROM address to start reading from
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
#include <EEPROM.h>
|
#include <EEPROM.h>
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
EEPROM.begin();
|
||||||
|
|
||||||
/***
|
/***
|
||||||
Iterate the EEPROM using a for loop.
|
Iterate the EEPROM using a for loop.
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ struct MyObject {
|
|||||||
};
|
};
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
EEPROM.begin();
|
||||||
|
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
while (!Serial) {
|
while (!Serial) {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ int address = 0;
|
|||||||
byte value;
|
byte value;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
EEPROM.begin();
|
||||||
// initialize serial and wait for port to open:
|
// initialize serial and wait for port to open:
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
while (!Serial) {
|
while (!Serial) {
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
int address = 0;
|
int address = 0;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
/** Empty setup **/
|
EEPROM.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
int addr = 0;
|
int addr = 0;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
/** Empty setup. **/
|
EEPROM.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ void EEPROMClass:: begin()
|
|||||||
HAL_EEPROM_CalculateTimings(&heeprom, OSC_SYSTEM_VALUE);
|
HAL_EEPROM_CalculateTimings(&heeprom, OSC_SYSTEM_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t EEPROMClass:: read( int idx )
|
uint8_t read( int idx )
|
||||||
{
|
{
|
||||||
// Проверка адекватности адреса idx
|
// Проверка адекватности адреса idx
|
||||||
if (idx < 0)
|
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));
|
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
|
// Проверка адекватности адреса idx
|
||||||
if (idx < 0)
|
if (idx < 0)
|
||||||
@@ -72,7 +72,7 @@ void EEPROMClass:: write( int idx, uint8_t val )
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Делает то же, что и write, но записывает только если новый байт отличается от уже записанного
|
// Делает то же, что и write, но записывает только если новый байт отличается от уже записанного
|
||||||
void EEPROMClass:: update( int idx, uint8_t val )
|
void update( int idx, uint8_t val )
|
||||||
{
|
{
|
||||||
// Проверка адекватности адреса idx
|
// Проверка адекватности адреса idx
|
||||||
if (idx < 0)
|
if (idx < 0)
|
||||||
@@ -107,10 +107,10 @@ void EEPROMClass:: update( int idx, uint8_t val )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t EEPROMClass:: length( void )
|
// uint16_t EEPROMClass:: length( void )
|
||||||
{
|
// {
|
||||||
return (uint16_t)EEPROM_LENGHT;
|
// return (uint16_t)EEPROM_LENGHT;
|
||||||
}
|
// }
|
||||||
|
|
||||||
void HAL_read(uint16_t addr, uint32_t * data)
|
void HAL_read(uint16_t addr, uint32_t * data)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -16,19 +16,81 @@
|
|||||||
void HAL_read(uint16_t addr, uint32_t * data);
|
void HAL_read(uint16_t addr, uint32_t * data);
|
||||||
void HAL_write(uint16_t addr, uint32_t * data);
|
void HAL_write(uint16_t addr, uint32_t * data);
|
||||||
void HAL_erase(uint16_t addr);
|
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{
|
struct EEPROMClass{
|
||||||
|
|
||||||
//Basic user access methods.
|
//Basic user access methods.
|
||||||
// EERef operator[]( const int idx ) { return idx; }
|
EERef operator[]( const int idx ) { return idx; }
|
||||||
uint8_t read( int idx );
|
uint8_t read( int idx ) { return EERef( idx ); }
|
||||||
void write( int idx, uint8_t val );
|
void write( int idx, uint8_t val ) { (EERef( idx )) = val; }
|
||||||
void update( int idx, uint8_t val );
|
void update( int idx, uint8_t val ) { EERef( idx ).update( val ); }
|
||||||
|
|
||||||
//STL and C++11 iteration capability.
|
|
||||||
void begin();
|
void begin();
|
||||||
uint16_t 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();
|
uint16_t length() { return (uint16_t)EEPROM_LENGHT; }
|
||||||
|
|
||||||
template< typename T >
|
template< typename T >
|
||||||
T& put(int idx, T& dataPointer)
|
T& put(int idx, T& dataPointer)
|
||||||
@@ -95,6 +157,7 @@ struct EEPROMClass{
|
|||||||
write_data_buf[i] = 0;
|
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[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_erase((uint16_t)addr);
|
||||||
HAL_write((uint16_t)addr, write_data_buf);
|
HAL_write((uint16_t)addr, write_data_buf);
|
||||||
dataSize -= writeSize;
|
dataSize -= writeSize;
|
||||||
|
|||||||
Reference in new issue
Block a user