Arduino-TWI
Two-Wire Interface (TWI) library for Arduino
TWI.h
Go to the documentation of this file.
1 
19 #ifndef HARDWARE_AVR_TWI_H
20 #define HARDWARE_AVR_TWI_H
21 
22 #include "TWI.h"
23 
27 namespace Hardware {
28 class TWI : public ::TWI {
29 public:
34  TWI(uint32_t freq = DEFAULT_FREQ)
35  {
36  // Initiate hardware registers: baudrate and control
37  TWBR = ((F_CPU / freq) - 16) / 2;
38  TWSR = 0;
39  TWCR = 0;
40  }
41 
48  virtual bool acquire()
49  {
50  // Acquire bus and issue start condition
51  lock();
52  m_start = true;
53  TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTA);
54  return iowait(START);
55  }
56 
63  virtual bool release()
64  {
65  // Mark bus manager as idle
66  m_start = false;
67  unlock();
68 
69  // Issue stop condition and release bus
70  TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTO);
71 
72  // Allow the command to complete
73  delayMicroseconds(10);
74  return (true);
75  }
76 
85  virtual int read(uint8_t addr, void* buf, size_t count)
86  {
87  // Check if repeated start condition should be generated
88  if (!m_start) {
89  TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTA);
90  if (!iowait(REP_START)) return (false);
91  }
92  m_start = false;
93 
94  // Address device with read request and check that it acknowledges
95  TWDR = addr | 0x01;
96  TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA);
97  if (!iowait(MR_SLA_ACK)) return (-1);
98 
99  // Read bytes and acknowledge until required size
100  uint8_t* bp = (uint8_t*) buf;
101  size_t size = count;
102  while (size--) {
103  if (size != 0) {
104  TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA);
105  if (!iowait(MR_DATA_ACK)) return (-1);
106  }
107  else {
108  TWCR = _BV(TWEN) | _BV(TWINT);
109  if (!iowait(MR_DATA_NACK)) return (-1);
110  }
111  *bp++ = TWDR;
112  }
113  return (count);
114  }
115 
123  virtual int write(uint8_t addr, iovec_t* vp)
124  {
125  // Check if repeated start condition should be generated
126  if (!m_start) {
127  TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTA);
128  if (!iowait(REP_START)) return (-1);
129  }
130  m_start = false;
131 
132  // Address device with write request and check that it acknowledges
133  TWDR = addr | 0x00;
134  TWCR = _BV(TWEN) | _BV(TWINT);
135  if (!iowait(MT_SLA_ACK)) return (-1);
136  if (vp == NULL) return (0);
137 
138  // Write given io vector buffers to device
139  int count = 0;
140  for(; vp->buf != NULL; vp++) {
141  const uint8_t* bp = (const uint8_t*) vp->buf;
142  size_t size = vp->size;
143  count += size;
144  while (size--) {
145  TWDR = *bp++;
146  TWCR = _BV(TWEN) | _BV(TWINT);
147  if (!iowait(MT_DATA_ACK)) return (-1);
148  }
149  }
150  return (count);
151  }
152 
153 protected:
155  enum {
156  START = 0x08,
157  REP_START = 0x10,
158  ARB_LOST = 0x38,
159  MT_SLA_ACK = 0x18,
160  MT_SLA_NACK = 0x20,
161  MT_DATA_ACK = 0x28,
162  MT_DATA_NACK = 0x30,
163  MR_SLA_ACK = 0x40,
164  MR_SLA_NACK = 0x48,
165  MR_DATA_ACK = 0x50,
166  MR_DATA_NACK = 0x58,
167  MASK = 0xF8,
168  BUS_ERROR = 0x00
169  } __attribute__((packed));
170 
171 
178  bool iowait(uint8_t status)
179  {
180  loop_until_bit_is_set(TWCR, TWINT);
181  return ((TWSR & MASK) == status);
182  }
183 
185  bool m_start;
186 };
187 };
188 
189 #endif
dito, NACK sent.
Definition: TWI.h:166
void lock()
Definition: TWI.h:176
Data received, ACK sent.
Definition: TWI.h:165
Definition: TWI.h:27
TWI(uint32_t freq=DEFAULT_FREQ)
Definition: TWI.h:34
virtual int write(uint8_t addr, iovec_t *vp)
Definition: TWI.h:123
dito, NACK received.
Definition: TWI.h:160
Data write sent, ACK received.
Definition: TWI.h:161
Arbitration lost.
Definition: TWI.h:158
virtual bool acquire()
Definition: TWI.h:48
virtual bool release()
Definition: TWI.h:63
Slave address/read sent, ACK received.
Definition: TWI.h:163
static const uint32_t DEFAULT_FREQ
Definition: TWI.h:108
bool m_start
Definition: TWI.h:185
Start condition transmitted.
Definition: TWI.h:156
virtual int read(uint8_t addr, void *buf, size_t count)
Definition: TWI.h:85
bool iowait(uint8_t status)
Definition: TWI.h:178
dito, NACK received.
Definition: TWI.h:164
dito, NACK received.
Definition: TWI.h:162
Slave address/write sent, ACK received.
Definition: TWI.h:159
Bus error state.
Definition: TWI.h:168
Mask status code.
Definition: TWI.h:167
Repeated start transmitted.
Definition: TWI.h:157
void unlock()
Definition: TWI.h:185