Arduino-Storage
External Memory Storage library for Arduino
AT24CXX.h
Go to the documentation of this file.
1 
19 #ifndef AT24CXX_H
20 #define AT24CXX_H
21 
22 #include "TWI.h"
23 #include "Storage.h"
24 
25 #ifndef CHARBITS
26 #define CHARBITS 8
27 #endif
28 
34 class AT24CXX : public Storage, protected TWI::Device {
35 public:
44  AT24CXX(TWI& twi,
45  uint8_t subaddr = 0,
46  const size_t size = 32,
47  const uint16_t page_max = 32) :
48  Storage((size / CHARBITS) * 1024UL),
49  TWI::Device(twi, 0x50 | (subaddr & 0x07)),
50  PAGE_MAX(page_max),
51  PAGE_MASK(page_max - 1)
52  {}
53 
55  const uint16_t PAGE_MAX;
56 
62  bool is_ready()
63  {
64  if (!acquire()) return (false);
65  int res = write(NULL);
66  if (!release()) return (false);
67  return (res == 0);
68  }
69 
79  virtual int read(void* dst, uint32_t src, size_t count)
80  {
81  uint8_t retry = RETRY_MAX;
82  uint16_t addr = __builtin_bswap16(src);
83  do {
84  if (!acquire()) return (-1);
85  int res = write(&addr, sizeof(addr));
86  if (res == sizeof(addr)) {
87  res = read(dst, count);
88  if (!release()) break;
89  if (res == (int) count) return (res);
90  }
91  if (!release()) return (-1);
92  delay(RETRY_DELAY_MS);
93  } while (--retry);
94  return (-1);
95  }
96 
106  virtual int write(uint32_t dst, const void* src, size_t count)
107  {
108  uint8_t* p = (uint8_t*) src;
109  size_t s = count;
110  size_t n = PAGE_MAX - (dst & PAGE_MASK);
111  if (n > s) n = s;
112  while (1) {
113  uint8_t retry = RETRY_MAX;
114  uint16_t addr = __builtin_bswap16(dst);
115  iovec_t vec[3];
116  iovec_t* vp = vec;
117  int res = -1;
118  iovec_arg(vp, &addr, sizeof(addr));
119  iovec_arg(vp, p, n);
120  iovec_end(vp);
121  do {
122  if (!acquire()) continue;
123  res = write(vec);
124  if (!release()) continue;
125  if (res > 0) break;
126  delay(RETRY_DELAY_MS);
127  } while (--retry);
128  if (res < 0) return (-1);
129  s -= n;
130  if (s == 0) return (count);
131  dst += n;
132  p += n;
133  n = (s < PAGE_MAX ? s : PAGE_MAX);
134  }
135  }
136 
137 protected:
139  const uint16_t PAGE_MASK;
140 
142  static const uint8_t RETRY_MAX = 20;
143 
145  static const uint8_t RETRY_DELAY_MS = 1;
146 
147  using TWI::Device::read;
148  using TWI::Device::write;
149 };
150 
159 class AT24C32 : public AT24CXX {
160 public:
161  AT24C32(TWI& twi, uint8_t subaddr = 0) :
162  AT24CXX(twi, subaddr, 32, 32)
163  {}
164 };
165 
174 class AT24C64 : public AT24CXX {
175 public:
176  AT24C64(TWI& twi, uint8_t subaddr = 0) :
177  AT24CXX(twi, subaddr, 64, 32)
178  {}
179 };
180 
189 class AT24C128 : public AT24CXX {
190 public:
191  AT24C128(TWI& twi, uint8_t subaddr = 0) :
192  AT24CXX(twi, subaddr, 128, 64)
193  {}
194 };
195 
204 class AT24C256 : public AT24CXX {
205 public:
206  AT24C256(TWI& twi, uint8_t subaddr = 0) :
207  AT24CXX(twi, subaddr, 256, 64)
208  {}
209 };
210 
219 class AT24C512 : public AT24CXX {
220 public:
221  AT24C512(TWI& twi, uint8_t subaddr = 0) :
222  AT24CXX(twi, subaddr, 512, 128)
223  {}
224 };
225 #endif
AT24C32(TWI &twi, uint8_t subaddr=0)
Definition: AT24CXX.h:161
Software::TWI< BOARD::D18, BOARD::D19 > twi
Definition: Cache.ino:21
const uint16_t PAGE_MAX
Definition: AT24CXX.h:55
AT24CXX(TWI &twi, uint8_t subaddr=0, const size_t size=32, const uint16_t page_max=32)
Definition: AT24CXX.h:44
AT24C128(TWI &twi, uint8_t subaddr=0)
Definition: AT24CXX.h:191
bool is_ready()
Definition: AT24CXX.h:62
AT24C512(TWI &twi, uint8_t subaddr=0)
Definition: AT24CXX.h:221
virtual int read(void *dst, uint32_t src, size_t count)
Definition: AT24CXX.h:79
static const uint8_t RETRY_MAX
Definition: AT24CXX.h:142
virtual int write(uint32_t dst, const void *src, size_t count)
Definition: AT24CXX.h:106
static const uint8_t RETRY_DELAY_MS
Definition: AT24CXX.h:145
AT24C256(TWI &twi, uint8_t subaddr=0)
Definition: AT24CXX.h:206
const uint16_t PAGE_MASK
Definition: AT24CXX.h:139
AT24C64(TWI &twi, uint8_t subaddr=0)
Definition: AT24CXX.h:176
#define CHARBITS
Definition: AT24CXX.h:26
int count
Definition: Stream.ino:26