19 #ifndef SOFTWARE_TWI_H 20 #define SOFTWARE_TWI_H 31 template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
80 virtual int read(uint8_t addr,
void* buf,
size_t count)
88 if (!
write_byte(addr | 1, nack) || nack)
return (-1);
91 uint8_t* bp = (uint8_t*) buf;
94 bool ack = (size != 0);
109 virtual int write(uint8_t addr, iovec_t* vp)
117 if (!
write_byte(addr | 0, nack) || nack)
return (-1);
118 if (vp == NULL)
return (0);
122 for(; vp->buf != NULL; vp++) {
123 const uint8_t* bp = (
const uint8_t*) vp->buf;
124 size_t size = vp->size;
127 uint8_t data = *bp++;
128 if (!
write_byte(data, nack) || nack)
return (-1);
136 static const int T1 = 4;
139 static const int T2 = 5;
161 if (m_scl)
return (
true);
162 delayMicroseconds(T1);
175 if (m_sda == 0)
return (
false);
177 delayMicroseconds(T1);
189 delayMicroseconds(T1);
191 if (m_sda == 0)
return (
false);
193 delayMicroseconds(T2);
195 delayMicroseconds(T1);
207 delayMicroseconds(T1);
210 delayMicroseconds(T1);
224 if (value) m_sda.input();
else m_sda.output();
225 delayMicroseconds(T2);
227 delayMicroseconds(T1);
242 delayMicroseconds(T2);
244 delayMicroseconds(T1);
260 for (
int i = 0; i < 8; i++) {
261 if (!
write_bit(byte & 0x80))
return (
false);
279 for (
int i = 0; i < 8; i++) {
280 if (!
read_bit(value))
return (
false);
281 byte = (byte << 1) | value;
virtual int read(uint8_t addr, void *buf, size_t count)
bool repeated_start_condition()
static const int CLOCK_STRETCHING_RETRY_MAX
virtual int write(uint8_t addr, iovec_t *vp)
bool write_byte(uint8_t byte, bool &nack)
bool read_bit(bool &value)
bool read_byte(uint8_t &byte, bool ack)
bool write_bit(bool value)