libpropeller
Making PropellerGCC Easier
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
eeprom.h
Go to the documentation of this file.
1 #ifndef LIBPROPELLER_EEPROM_H_
2 #define LIBPROPELLER_EEPROM_H_
3 
5 
15 class EEPROM {
16 public:
17 
27  void Init(const int scl = 28, const int sda = 29) {
28  base_.Init(scl, sda);
29  }
30 
37  bool Put(const unsigned short address, const char byte) {
38  char bytes[] = {byte};
39  return Put(address, bytes, 1);
40  }
41 
51  bool Put(unsigned short address, char bytes [], int size) {
52  //The lower seven bits define an EEPROM page, so we need a bit of magic to make
53  //sure that if we go over the boundary, we start a new page.
54 
55  int bytesWritten = 0;
56  while (bytesWritten < size) {
57  if (PollForAcknowledge() == false) {
58  return false;
59  }
60  base_.SendByte((address >> 8) & 0xFF); //High address byte
61  base_.SendByte(address & 0xFF); //Low address byte
62  do {
63  base_.SendByte(bytes[bytesWritten]); //Payload
64  bytesWritten++;
65  address++;
66  } while ((address & 0b1111111) != 0 && bytesWritten < size); //detect rollover
67 
68  base_.Stop();
69  }
70 
71  return true;
72  }
73 
83  bool PutNumber(const unsigned short address, const int bytes, const int length) {
84 
85  char temp[4];
86  //Even if length is < 4, do them all (easier than a loop). Only the used ones will be written.
87  temp[3] = (((unsigned) bytes) & 0xFF000000) >> 24;
88  temp[2] = (((unsigned) bytes) & 0xFF0000) >> 16;
89  temp[1] = (((unsigned) bytes) & 0xFF00) >> 8;
90  temp[0] = (((unsigned) bytes) & 0xFF) >> 0;
91  return Put(address, temp, length);
92  }
93 
99  int Get(unsigned short address) {
100  char byte[1];
101  int result = Get(address, byte, 1);
102  if (result < 0) {
103  return result;
104  } else {
105  return byte[0];
106  }
107  }
108 
118  int Get(unsigned short address, char bytes [], const int length) {
119  int bytesRead = 0;
120  while (bytesRead < length) {
121  if (PollForAcknowledge() == false) {
122  return -1;
123  }
124  base_.SendByte((address >> 8) & 0xFF); //High address byte
125  base_.SendByte(address & 0xFF); //Low address byte
126  base_.Start();
127  base_.SendByte(kI2CAddress | 0x01); //device EEPROM read (w/ read bit set)
128 
129 
130  while (((address + 1) & 0b1111111) != 0
131  && bytesRead + 1 < length) {
132  bytes[bytesRead] = base_.ReadByte(true);
133  bytesRead++;
134  address++;
135  }
136 
137  bytes[bytesRead] = base_.ReadByte(false);
138  bytesRead++;
139  address++;
140 
141  base_.Stop();
142  }
143 
144  return 0;
145 
146  }
147 
156  int GetNumber(unsigned short address, int length) {
157  char temp[4];
158  Get(address, temp, length);
159  int result = 0;
160 
161  /*
162  // Commented out because of the compiler loop bug.
163  for (int i = length - 1; i >= 0; --i) {
164  //debug->Put(" Get_C ");
165  result = (result << 8) | temp[i];
166  }*/
167 
168  if (length >= 4) {
169  result = (result << 8) | temp[3];
170  }
171  if (length >= 3) {
172  result = (result << 8) | temp[2];
173  }
174  if (length >= 2) {
175  result = (result << 8) | temp[1];
176  }
177  if (length >= 1) {
178  result = (result << 8) | temp[0];
179  }
180 
181  return result;
182  }
183 
184 private:
185 
186  bool PollForAcknowledge() {
187  base_.Start();
188  int counter = 0;
189  while (base_.SendByte(kI2CAddress) == false) { //device EEPROM write
190  if (++counter == 100) { //timeout
191  return false;
192  }
193  base_.Stop();
194  base_.Start();
195  }
196  return true;
197  }
198 
199  I2CBase base_;
200  static const unsigned char kI2CAddress = 0b10100000;
201 
202 };
203 
204 #endif // LIBPROPELLER_EEPROM_H_