GithubHelp home page GithubHelp logo

chayanforyou / wearleveling Goto Github PK

View Code? Open in Web Editor NEW
22.0 4.0 2.0 19 KB

This Wear Leveling library is for Microcontroller EEPROM to increase its life cycle

License: Apache License 2.0

C++ 100.00%
eeprom microcontroller arduino avr library wearlevel

wearleveling's Introduction

WearLeveling

The technique I am useing is to prefix the data with a 4-byte rolling sequence number where the largest number represents the lastest / current value. In the case of storing 2 bytes of actual data that would give 6 (4-for sequence & 2-for data) bytes total and then I form into a circular queue arrangement so for 1024 bytes of EEPROM (if your EEPROM size is small or max you have to change the value from library file) it would contain 170 entries and increase endurance 170 times if you use one Segment.

Then when booting the largest sequence number can be used to determine both the next sequence number to be used and the current tail of the queue. The following C-code demonstrates, this assumes that upon initial programming the EEPROM area has been erased to values of 0xFF so I ignore a sequence number of 0xFFFFFFFF (as 4-byte sequence no):

By default the MAX_EEPROM_SEGMENTS define is set to 2 this means you have two segments available, 0 or 1. This allows you to define an area of EEPROM for one structure and a secondary area of EEPROM for another. If you need more segments you have to change it.

HOW TO USE:

I designed this library to address a couple of issues when using EEPROM for data storage:

Data corruption is one of this big issues with EEPROM. From what I've read, the most common reason is a power issue where power drops out and/or brown out detection is not properly configured. The big problem with data corruption is that when the data is read back from the EEPROM, it may be corrupt and the uC will use corrupt data with can lead to all sorts of problems. The second issue is that the EEPROM design limit of 100,000 write cycles. If you are trying to save values periodically, this will be a concern if you are writing the data to the same spot of EEPROM each time. The Library solves both of these problems by using these techniques:

It does all this with three functions:

EEPROMwl.begin(const uint8_t amountOfIndexes);
// or
EEPROMwl.begin(const uint8_t amountOfIndexes, const uint16_t eepromLengthToUse);
EEPROMwl.read(const uint8_t idx)
EEPROMwl.write(const uint8_t idx, const uint16_t value);

The first IMPORTANT thing I want to mention is the ORDER these commands must be used.

You must call EEPROMwl.begin() first. After that you can call EEPROMwl.read() & EEPROMwl.write() as many times as you like. The reason for this order is that Save requires variables that Load set to work properly. This shouldn't be a problem because the general flow of usage for these commands should follow this order:

Arduino Example:

#include <EEPROMWearLevel.h>

#define AMOUNT_OF_INDEXES 2

#define INDEX_VAL1 0
#define INDEX_VAL2 1

void setup() {
  Serial.begin(9600);
  while (!Serial);

  EEPROMwl.begin(AMOUNT_OF_INDEXES);

  writeConfiguration();
  readConfiguration();
}

void loop() {
}

void writeConfiguration() {
  // write value
  EEPROMwl.write(INDEX_VAL1, 123);
  EEPROMwl.write(INDEX_VAL2, 32768);
}

void readConfiguration() {
  // read value
  Serial.print(F("INDEX 1: "));
  Serial.println(EEPROMwl.read(INDEX_VAL1));

  Serial.print(F("INDEX 2: "));
  Serial.println(EEPROMwl.read(INDEX_VAL2));
}

Make sure you don't just call EEPROMwl.write() to save the same data as this is wasteful on EEPROM design life. Keep a flag that indicates when the data has changed and also a flag indicating how long it has been since that last save. Only save if the data has changed AND it has been a long enough period of time.

Special Thanks

To Antor Ahmed

Contributions

Enhancements and improvements are welcome.

License

Arduino EEPROMWearLevel Library
Copyright (c) 2020 Chayan Mistry (https://github.com/chayanforyou).

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

wearleveling's People

Contributors

chayanforyou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

wearleveling's Issues

Will this wear leveling work on ESP8266 (Nodemcu) modules ?

I have tested this on arduino its working fine.
But when i used this for esp8266 its giving below error

fatal error: avr/io.h: No such file or directory

#include <avr/io.h>

                ^

compilation terminated.
exit status 1
Error compiling for board Generic ESP8266 Module.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.