libpropeller
Making PropellerGCC Easier
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
i2c.h
Go to the documentation of this file.
1 
2 
3 #ifndef LIBPROPELLER_I2C_H_
4 #define LIBPROPELLER_I2C_H_
5 
6 #include <stdint.h>
7 
9 
36 class I2C {
37 public:
38 
40 
47  void Init(const int scl = 28, const int sda = 29, const int frequency = 400000) {
48  base_.Init(scl, sda, frequency);
49  }
50 
62  bool Ping(const unsigned char device) {
63  base_.Start();
64  bool result = base_.SendByte(device);
65  base_.Stop();
66  return result;
67  }
68 
83  bool Put(const unsigned char device, const unsigned char address,
84  const char byte) {
85  bool result;
86 
87  base_.Start();
88  result = base_.SendByte(device);
89  result &= base_.SendByte(address);
90  result &= base_.SendByte(byte);
91  base_.Stop();
92 
93  return result;
94  }
95 
109  unsigned char Get(const unsigned char device, const unsigned char address) {
110  bool result;
111 
112  base_.Start();
113  result = base_.SendByte(device);
114  result &= base_.SendByte(address);
115 
116  base_.Start();
117  result &= base_.SendByte(device | 0x01); //Set read bit
118  unsigned char dataByte = base_.ReadByte(false);
119  base_.Stop();
120  return dataByte;
121  }
122 
140  bool Put(const unsigned char device, const unsigned char address,
141  const char * bytes, const int size) {
142  bool result;
143  base_.Start();
144  result = base_.SendByte(device);
145  result &= base_.SendByte(address);
146 
147  for (int i = 0; i < size; ++i) {
148  result &= base_.SendByte(bytes[i]);
149  }
150  base_.Stop();
151 
152  return result;
153  }
154 
172  bool Get(const unsigned char device, const unsigned char address,
173  char * bytes, const int size) {
174  bool result;
175  base_.Start();
176  result = base_.SendByte(device);
177  result &= base_.SendByte(address); //Assert the read multiple bytes bit (ref: L3GD20 datasheet)
178  base_.Start();
179  result &= base_.SendByte(device | 0x01);
180 
181  int i = 0;
182  for (; i < size - 1; ++i) {
183  bytes[i] = base_.ReadByte(true); //Send true to keep on reading bytes
184  }
185 
186  bytes[i] = base_.ReadByte(false); //Trailing NAK
187  base_.Stop();
188 
189  return result;
190  }
191 
207  bool Put(const unsigned char device, const char byte) {
208  //Warning: this method is not unit tested! (it's run, but the MS5611 does
209  //not have a register that can be written to and read from).
210 
211  base_.Start();
212  bool result = base_.SendByte(device);
213  result &= base_.SendByte(byte);
214  base_.Stop();
215 
216  return result;
217  }
218 
238  bool Get(const unsigned char device, char * bytes, const int size) {
239  base_.Start();
240  bool result = base_.SendByte(device | 0x01);
241  int i = 0;
242  for (; i < size - 1; ++i) {
243  bytes[i] = base_.ReadByte(true);
244  }
245  bytes[i] = base_.ReadByte(false);
246  base_.Stop();
247 
248  return result;
249  }
250 
251 
252  /*
253  Pass through methods to base:
254  */
255 
257  void Start() {
258  base_.Start();
259  }
260 
262  void Stop() {
263  base_.Stop();
264  }
265 
267  int SendByte(const unsigned char byte) {
268  return base_.SendByte(byte);
269  }
270 
272  unsigned char ReadByte(const int acknowledge) {
273  return base_.ReadByte(acknowledge);
274  }
275 
276 };
277 
278 #endif // LIBPROPELLER_I2C_H_