MySensors Library & Examples  2.3.2
I2CEeprom.h
Go to the documentation of this file.
1 // Copyright (C) 2016 Krister W. <[email protected]>
2 //
3 // Original SPI flash driver this is based on:
4 // Copyright (c) 2013-2015 by Felix Rusu, LowPowerLab.com
5 //
6 // I2C EEPROM library for MySensors OTA. Based on SPI Flash memory library for
7 // arduino/moteino.
8 // This driver is made to look like the SPI flash driver so changes needed to
9 // MySensors OTA code is minimized.
10 // This works with 32 or 64kB I2C EEPROM like an 24(L)C256. AVR HW I2C is assumed
11 // and error handling is quite minimal. Uses extEEPROM as the underlying driver.
12 // DEPENDS ON: Arduino Wire library, extEEPROM
13 // **********************************************************************************
14 // License
15 // **********************************************************************************
16 // This program is free software; you can redistribute it
17 // and/or modify it under the terms of the GNU General
18 // Public License as published by the Free Software
19 // Foundation; either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 // This program is distributed in the hope that it will
23 // be useful, but WITHOUT ANY WARRANTY; without even the
24 // implied warranty of MERCHANTABILITY or FITNESS FOR A
25 // PARTICULAR PURPOSE. See the GNU General Public
26 // License for more details.
27 //
28 // You should have received a copy of the GNU General
29 // Public License along with this program.
30 // If not, see <http://www.gnu.org/licenses/>.
31 //
32 // Licence can be viewed at
33 // http://www.gnu.org/licenses/gpl-3.0.txt
34 //
35 // Please maintain this license information along with authorship
36 // and copyright notices in any redistribution of this code
37 
45 #ifndef _I2CEeprom_H_
46 #define _I2CEeprom_H_
47 
48 #include <Arduino.h>
49 #include <extEEPROM.h>
50 
52 // 400kHz clock as default. Use extEEPROM type
53 #ifndef I2CEEPROM_TWI_CLK
54 #define I2CEEPROM_TWI_CLK twiClock400kHz
55 #endif
56 
58 //Typically 64 (see data sheet for your EEPROM)
59 // Some 512kbit chips use 128 byte pages (e.g. Atmel AT24C512)
60 #ifndef I2CEEPROM_PAGE_SIZE
61 #define I2CEEPROM_PAGE_SIZE 64
62 #endif
63 
65 // 24C256 is 32kB, minimum that fits code for ATmega328
66 // Use extEEPROM type
67 #ifndef I2CEEPROM_CHIP_SIZE
68 #define I2CEEPROM_CHIP_SIZE kbits_256
69 #endif
70 
73 {
74 public:
75 
76  explicit I2CEeprom(uint8_t addr);
77  bool initialize();
78  uint8_t readByte(uint32_t addr);
79  void readBytes(uint32_t addr, void* buf, uint16_t len);
80  void writeByte(uint32_t addr, uint8_t byt);
81  void writeBytes(uint32_t addr, const void* buf,
82  uint16_t len);
83  bool busy();
84 
85  // the rest not needed for EEPROMs, but kept so SPI flash code compiles as is (functions are NOP)
86 
88  uint16_t readDeviceId()
89  {
90  return 0xDEAD;
91  };
93  void chipErase() {};
95  void blockErase4K(uint32_t address)
96  {
97  (void)address;
98  };
100  void blockErase32K(uint32_t address)
101  {
102  (void)address;
103  };
105  void sleep() {};
107  void wakeup() {};
109  void end() {};
110 
111 protected:
112 
113  uint8_t m_addr;
114 };
115 
116 #endif
I2CEeprom
Definition: I2CEeprom.h:72
I2CEeprom::end
void end()
dummy function for SPI flash compatibility
Definition: I2CEeprom.h:109
I2CEeprom::blockErase4K
void blockErase4K(uint32_t address)
dummy function for SPI flash compatibility
Definition: I2CEeprom.h:95
I2CEeprom::m_addr
uint8_t m_addr
I2C address for busy()
Definition: I2CEeprom.h:109
extEEPROM
Definition: extEEPROM.h:87
I2CEeprom::readByte
uint8_t readByte(uint32_t addr)
read 1 byte from flash memory
I2CEeprom::blockErase32K
void blockErase32K(uint32_t address)
dummy function for SPI flash compatibility
Definition: I2CEeprom.h:100
I2CEeprom::writeBytes
void writeBytes(uint32_t addr, const void *buf, uint16_t len)
write multiple bytes to flash memory (up to 64K), if define SPIFLASH_SST25TYPE is set AAI Word Progra...
I2CEeprom::writeByte
void writeByte(uint32_t addr, uint8_t byt)
Write 1 byte to flash memory.
I2CEeprom::I2CEeprom
I2CEeprom(uint8_t addr)
Constructor.
I2CEeprom::readDeviceId
uint16_t readDeviceId()
dummy function for SPI flash compatibility
Definition: I2CEeprom.h:88
I2CEeprom::busy
bool busy()
check if the chip is busy erasing/writing
I2CEeprom::wakeup
void wakeup()
dummy function for SPI flash compatibility
Definition: I2CEeprom.h:107
I2CEeprom::sleep
void sleep()
dummy function for SPI flash compatibility
Definition: I2CEeprom.h:105
I2CEeprom::chipErase
void chipErase()
dummy function for SPI flash compatibility
Definition: I2CEeprom.h:93
I2CEeprom::readBytes
void readBytes(uint32_t addr, void *buf, uint16_t len)
read multiple bytes
I2CEeprom::initialize
bool initialize()
setup