|
| 1 | +/* |
| 2 | + EEEPROM.h - External (I2C) EEPROM libray. |
| 3 | + Copyright (c) 2009 Amadeusz Jasak. All right reserved. |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | +/****************************************************************************** |
| 21 | + * Includes |
| 22 | + ******************************************************************************/ |
| 23 | + |
| 24 | +#include "WConstants.h" |
| 25 | +#include "EEEPROM.h" |
| 26 | +#include "Wire.h" |
| 27 | + |
| 28 | +/****************************************************************************** |
| 29 | + * Definitions |
| 30 | + ******************************************************************************/ |
| 31 | + |
| 32 | +/****************************************************************************** |
| 33 | + * Constructors |
| 34 | + ******************************************************************************/ |
| 35 | +EEEPROM::EEEPROM(int address) |
| 36 | +{ |
| 37 | + this->address = address; |
| 38 | +} |
| 39 | + |
| 40 | +/****************************************************************************** |
| 41 | + * User API |
| 42 | + ******************************************************************************/ |
| 43 | + |
| 44 | +uint8_t EEEPROM::read(unsigned int addr) |
| 45 | +{ |
| 46 | + uint8_t rdata = 0x00; |
| 47 | + Wire.beginTransmission(this->address); |
| 48 | + Wire.send((int)(addr >> 8)); // MSB |
| 49 | + Wire.send((int)(addr & 0xFF)); // LSB |
| 50 | + Wire.endTransmission(); |
| 51 | + Wire.requestFrom(this->address, 1); |
| 52 | + if (Wire.available()) rdata = Wire.receive(); |
| 53 | + return rdata; |
| 54 | +} |
| 55 | + |
| 56 | +void EEEPROM::readPage(unsigned int addr, uint8_t *buffer, int length = 16) |
| 57 | +{ |
| 58 | + Wire.beginTransmission(this->address); |
| 59 | + Wire.send((int)(addr >> 8)); // MSB |
| 60 | + Wire.send((int)(addr & 0xFF)); // LSB |
| 61 | + Wire.endTransmission(); |
| 62 | + Wire.requestFrom(this->address,length); |
| 63 | + int c = 0; |
| 64 | + for ( c = 0; c < length; c++ ) |
| 65 | + if (Wire.available()) buffer[c] = Wire.receive(); |
| 66 | +} |
| 67 | + |
| 68 | +void EEEPROM::write(unsigned int addr, uint8_t value) |
| 69 | +{ |
| 70 | + Wire.beginTransmission(this->address); |
| 71 | + Wire.send((int)(addr >> 8)); // MSB |
| 72 | + Wire.send((int)(addr & 0xFF)); // LSB |
| 73 | + Wire.send((int)value); |
| 74 | + Wire.endTransmission(); |
| 75 | + delay(5); |
| 76 | +} |
| 77 | + |
| 78 | +void EEEPROM::writePage(unsigned int addr, uint8_t *data, int length = 16) |
| 79 | +{ |
| 80 | + Wire.beginTransmission(this->address); |
| 81 | + Wire.send((int)(addr >> 8)); // MSB |
| 82 | + Wire.send((int)(addr & 0xFF)); // LSB |
| 83 | + byte c; |
| 84 | + for ( c = 0; c < length; c++) |
| 85 | + Wire.send(data[c]); |
| 86 | + Wire.endTransmission(); |
| 87 | +} |
0 commit comments