NetBurner 3.1
i2c.h
Go to the documentation of this file.
1 /*NB_REVISION*/
2 
3 /*NB_COPYRIGHT*/
4 
14 #ifndef __I2C_H
15 #define __I2C_H
16 
17 #include <predef.h>
18 #include <ctype.h>
19 #include <sim.h>
20 #include <nbrtos.h>
21 
22 
23 #define TWIHS_SR_IOS (TWIHS_SR_SCL | TWIHS_SR_SDA)
24 
25 struct I2CTxn_t {
26  uint8_t *buf;
27  uint32_t blen;
28  uint8_t regAddr[3];
29  uint8_t devAddr : 7;
30  bool RnW : 1;
31  uint8_t regAddrLen : 3;
32  bool bIssueStop : 1;
33 
34  void dump(uint32_t line);
35 };
36 
37 class I2C;
38 
51 class WireIntf {
52  I2C &i2c; // I2C instance of this wire interface
53  uint8_t txnBuf[128]; // Transaction buffer. Build up transaction before being flushed to the I2C bus
54  uint8_t txnLen; // Current transaction length
55  uint8_t devAddr; // I2C slave device adddress to be accessed
56  uint8_t bytesRead; // Number of bytes read, returned by requestFrom(). Provides number of bytes available to read()
57  uint8_t readIdx; // Read index. Location in the transaction buffer of next byte to be read
58  bool bTxnStarted; // Whether or not the wire interface is in an active transaction
59  bool bBusStarted; // Whether or not the bus is active (h/w signal state)
60 
61 public:
62  WireIntf(I2C &module);
63 
69  void begin();
70 
84  uint32_t requestFrom(uint8_t addr, uint32_t len, bool stop = true);
85 
93  void beginTransmission(uint8_t addr);
94 
105  void endTransmission(bool stop = true);
106 
117  uint32_t write(uint8_t dat);
118 
129  uint32_t write(char * str);
130 
141  uint32_t write(uint8_t * buf, uint32_t len);
142 
150  uint32_t available();
151 
158  uint8_t read();
159 
171  void flush(bool bIssueStop = false);
172 };
173 
174 
175 
176 /*
177  * I2C WireIntf objects instantiated by the system.
178  *
179  * Provides access to the all the I2C peripherals on the processor. The I2C peripherals available will vary by platform.
180  * For example, Wire.read(); will exectute a read opeeration on I2C module 0.
181  *
182  *
183  *
184  *
185  */
186 extern WireIntf Wire; // I2C module 0
187 extern WireIntf Wire1; // I2C module 1
188 extern WireIntf Wire2; // I2C module 2
189 
190 
203 class I2C {
204 public:
209  enum Result_t {
215  };
217 
218 private:
219  enum TxnStat {
220  TXN_RDY,
221  TXN_IN_PROGRESS,
222  TXN_WAITING
223  };
224 
225  Twihs &twi;
226  int modNum;
227  TxnStat txnStatus;
228  Result_t txnResult;
229  uint32_t sticky_sr;
230 
231  volatile I2CTxn_t *pTxn;
232  OS_SEM txnSem;
233 
234  void isr();
235  void isr_rx(uint32_t sr);
236  void isr_tx(uint32_t sr);
237  inline uint32_t getStatus()
238  { uint32_t sr = twi.TWIHS_SR; sticky_sr |= sr; return sr; }
239  inline uint32_t getStickyStatus()
240  { uint32_t sr = twi.TWIHS_SR; sticky_sr |= sr; return sticky_sr; }
241  inline void clrStickyStatus() { sticky_sr = 0; }
242  inline bool busBusy()
243  { return ((getStatus() & TWIHS_SR_IOS) != TWIHS_SR_IOS); }
244 
245  void start(uint8_t deviceAddr, bool rnw, uint8_t regAddrlen = 0, bool bIssueStop = false);
246  void restart(uint8_t deviceAddr, bool rnw, uint8_t regAddrlen = 0, bool bIssueStop = false);
247  void stop();
248  Result_t write8(uint8_t dat);
249  Result_t read8(uint8_t &dat);
250 
251 public:
258  I2C(int module);
259 
260  // Copy constructor
261  I2C(const I2C & rhs)
262  : twi(rhs.twi), modNum(rhs.modNum), txnStatus(rhs.txnStatus)
263  {}
264 
271  void setup(uint32_t busSpeed);
272 
277  void resetBus();
278 
290  Result_t DoTransaction(I2CTxn_t *pTransaction, bool bRepeatedStart = false);
291 
301  Result_t writeReg8(uint8_t devAddr, uint8_t reg, uint8_t dat);
302 
312  Result_t readReg8(uint8_t devAddr, uint8_t reg, uint8_t &dat);
313 
328  Result_t writeRegN(uint8_t devAddr, uint8_t reg, uint8_t *buf, uint32_t blen);
329 
340  Result_t readRegN(uint8_t devAddr, uint8_t reg, uint8_t *buf, uint32_t blen);
341 
342  // Static implementations of the C++ class that can be used if you prefer a C style interface
343  static Result_t writeReg8(int module, uint8_t devAddr, uint8_t reg, uint8_t dat);
344  static Result_t readReg8(int module, uint8_t devAddr, uint8_t reg, uint8_t &dat);
345  static Result_t writeRegN(int module, uint8_t devAddr, uint8_t reg, uint8_t *buf, uint32_t blen);
346  static Result_t readRegN(int module, uint8_t devAddr, uint8_t reg, uint8_t *buf, uint32_t blen);
347 
348  // These functions are for internal use only.
349  void dump(uint32_t line);
350  friend void TWIHS0_Handler();
351  friend void TWIHS1_Handler();
352  friend void TWIHS2_Handler();
353 
354  friend class WireIntf;
355 };
356 
357 // An I2C instantion exists for all I2C (TWIHS) on the processor. You can access they through this array
358 // i2c[0], i2c[1], i2c[2]
359 extern I2C i2c[];
360 
361 
362 inline I2C::Result_t I2C::writeReg8(int module, uint8_t devAddr, uint8_t reg, uint8_t dat)
363 {
364  if (module < 0) { return I2C_RES_NACK; }
365  if (module > 2) { return I2C_RES_NACK; }
366  return i2c[module].writeReg8(devAddr, reg, dat);
367 }
368 inline I2C::Result_t I2C::readReg8(int module, uint8_t devAddr, uint8_t reg, uint8_t &dat)
369 {
370  if (module < 0) { return I2C_RES_NACK; }
371  if (module > 2) { return I2C_RES_NACK; }
372  return i2c[module].readReg8(devAddr, reg, dat);
373 }
374 
375 inline I2C::Result_t I2C::writeRegN(int module, uint8_t devAddr, uint8_t reg, uint8_t *buf, uint32_t blen)
376 {
377  if (module < 0) { return I2C_RES_NACK; }
378  if (module > 2) { return I2C_RES_NACK; }
379  return i2c[module].writeRegN(devAddr, reg, buf, blen);
380 }
381 inline I2C::Result_t I2C::readRegN(int module, uint8_t devAddr, uint8_t reg, uint8_t *buf, uint32_t blen)
382 {
383  if (module < 0) { return I2C_RES_NACK; }
384  if (module > 2) { return I2C_RES_NACK; }
385  return i2c[module].readRegN(devAddr, reg, buf, blen);
386 }
387 
388 
389 #endif /* ----- #ifndef __I2C_H ----- */
390 
NetBurner Real-Time Operating System API.
uint32_t requestFrom(uint8_t addr, uint32_t len, bool stop=true)
Request a number of bytes from a slave device. The bytes can then be retrieved from the slave device ...
void begin()
Initialize the WireIntf driver object as a master with a bus speed of 100 kHz. This normally only nee...
Wire Interface Class for I2C.
Definition: i2c.h:51
Bad argument.
Definition: i2c.h:214
Semaphores are used to control access to shared resource critical section, or to communicate between ...
Definition: nbrtos.h:318
void setup(uint32_t busSpeed)
Initialize the I2C module.
void beginTransmission(uint8_t addr)
Begin a transmission to a I2C slave device at the provided address. Bytes can be queued for transmiss...
I2C Peripheral Class.
Definition: i2c.h:203
uint32_t available()
Get the number of bytes available to be read from the slave device with read(). This function can be ...
I2C(int module)
Constructor for the I2C peripheral module.
Result_t readReg8(uint8_t devAddr, uint8_t reg, uint8_t &dat)
Read an 8-bit value form an I2C slave device register.
Acknowledged.
Definition: i2c.h:210
uint32_t write(uint8_t dat)
Queues bytes of data to be transmitted to the slave device. This function can be called after a call ...
Not acknowledged.
Definition: i2c.h:211
void resetBus()
Reset the I2C bus.
Result_t writeReg8(uint8_t devAddr, uint8_t reg, uint8_t dat)
Write an 8-bit value to a I2C slave device register.
Result_t writeRegN(uint8_t devAddr, uint8_t reg, uint8_t *buf, uint32_t blen)
Write a number of 8-bit values to an I2C slave to the specified register address. ...
Result_t DoTransaction(I2CTxn_t *pTransaction, bool bRepeatedStart=false)
Start an I2C transaction.
Arbitration listening.
Definition: i2c.h:212
void flush(bool bIssueStop=false)
Force the data that were queued to be transmitted using the write() functions to be transmitted on th...
Result_t
Return result types.
Definition: i2c.h:209
Bus is busy.
Definition: i2c.h:213
Result_t readRegN(uint8_t devAddr, uint8_t reg, uint8_t *buf, uint32_t blen)
Read a number of 8-bit values from an I2C slave at the specified register address.
void endTransmission(bool stop=true)
Transmits the bytes of data queued by using the write() functions, and ends a transmission to a slave...
uint8_t read()
Reads a byte of data that was transmitted from a slave I2C device after a call to requestFrom()...