Arduino-Storage
External Memory Storage library for Arduino
MC23LCXXX.h
Go to the documentation of this file.
1 
19 #ifndef MC23LCXXX_H
20 #define MC23LCXXX_H
21 
22 #include "SPI.h"
23 #include "Storage.h"
24 
31 template<uint16_t KBYTE, BOARD::pin_t SS_PIN, uint32_t FREQ = 16000000L>
32 class MC23LCXXX : public Storage,
33  protected SPI::Device<0, MSBFIRST, FREQ, SS_PIN>
34 {
35 public:
37  static const uint32_t MAX_FREQ = 16000000L;
38 
44  MC23LCXXX(SPI& spi) :
45  Storage(KBYTE * 1024UL),
46  SPI::Device<0, MSBFIRST, FREQ, SS_PIN>(spi)
47  {}
48 
59  virtual int read(void* dst, uint32_t src, size_t count)
60  {
61  uint8_t* sp = (uint8_t*) &src;
62  header_t header;
63  size_t size;
64  header.cmd = READ;
65  if (KBYTE > 64) {
66  header.addr[0] = sp[2];
67  header.addr[1] = sp[1];
68  header.addr[2] = sp[0];
69  size = sizeof(header);
70  }
71  else {
72  header.addr[0] = sp[1];
73  header.addr[1] = sp[0];
74  size = sizeof(header) - 1;
75  }
76  acquire();
77  write(&header, size);
78  read(dst, count);
79  release();
80  return (count);
81  }
82 
93  virtual int write(uint32_t dst, const void* src, size_t count)
94  {
95  uint8_t* dp = (uint8_t*) &dst;
96  header_t header;
97  size_t size;
98  header.cmd = WRITE;
99  if (KBYTE > 64) {
100  header.addr[0] = dp[2];
101  header.addr[1] = dp[1];
102  header.addr[2] = dp[0];
103  size = sizeof(header);
104  }
105  else {
106  header.addr[0] = dp[1];
107  header.addr[1] = dp[0];
108  size = sizeof(header) - 1;
109  }
110  acquire();
111  write(&header, size);
112  write(src, count);
113  release();
114  return (count);
115  }
116 
117 protected:
119  struct header_t {
120  uint8_t cmd;
121  uint8_t addr[3];
122  } __attribute__((packed));
123 
125  enum {
126  READ = 0x03,
127  WRITE = 0x02,
128  RDMR = 0x05,
129  WRMR = 0x01
130  };
131 
132  using SPI::Device<0,MSBFIRST,FREQ,SS_PIN>::acquire;
133  using SPI::Device<0,MSBFIRST,FREQ,SS_PIN>::transfer;
134  using SPI::Device<0,MSBFIRST,FREQ,SS_PIN>::read;
135  using SPI::Device<0,MSBFIRST,FREQ,SS_PIN>::write;
136  using SPI::Device<0,MSBFIRST,FREQ,SS_PIN>::release;
137 };
138 
155 template<BOARD::pin_t SS_PIN, uint32_t FREQ = 16000000L>
156 class MC23LC512 : public MC23LCXXX<64, SS_PIN, FREQ> {
157 public:
163  MC23LC512(SPI& spi) : MC23LCXXX<64, SS_PIN, FREQ>(spi) {}
164 };
165 
182 template<BOARD::pin_t SS_PIN, uint32_t FREQ = 16000000L>
183 class MC23LC1024 : public MC23LCXXX<128, SS_PIN, FREQ>
184 {
185 public:
191  MC23LC1024(SPI& spi) : MC23LCXXX<128, SS_PIN, FREQ>(spi) {}
192 };
193 
194 #endif
Write mode register.
Definition: MC23LCXXX.h:129
Software::SPI< BOARD::D11, BOARD::D12, BOARD::D13 > spi
Definition: MC23LCXXX.ino:11
uint8_t addr[3]
16/24-bit address in MSB order.
Definition: MC23LCXXX.h:121
Write data to memory.
Definition: MC23LCXXX.h:127
#define FREQ
Definition: AT24CXX.ino:58
MC23LC512(SPI &spi)
Definition: MC23LCXXX.h:163
Read data from memory.
Definition: MC23LCXXX.h:126
virtual int read(void *dst, uint32_t src, size_t count)
Definition: MC23LCXXX.h:59
virtual int write(uint32_t dst, const void *src, size_t count)
Definition: MC23LCXXX.h:93
MC23LCXXX(SPI &spi)
Definition: MC23LCXXX.h:44
Read mode register.
Definition: MC23LCXXX.h:128
static const uint32_t MAX_FREQ
Definition: MC23LCXXX.h:37
uint8_t cmd
Command code.
Definition: MC23LCXXX.h:120
MC23LC1024(SPI &spi)
Definition: MC23LCXXX.h:191
int count
Definition: Stream.ino:26