19 #ifndef HARDWARE_AVR_TWI_H 20 #define HARDWARE_AVR_TWI_H 37 TWBR = ((F_CPU / freq) - 16) / 2;
53 TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTA);
70 TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTO);
73 delayMicroseconds(10);
85 virtual int read(uint8_t addr,
void* buf,
size_t count)
89 TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTA);
96 TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA);
100 uint8_t* bp = (uint8_t*) buf;
104 TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA);
108 TWCR = _BV(TWEN) | _BV(TWINT);
123 virtual int write(uint8_t addr, iovec_t* vp)
127 TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTA);
134 TWCR = _BV(TWEN) | _BV(TWINT);
136 if (vp == NULL)
return (0);
140 for(; vp->buf != NULL; vp++) {
141 const uint8_t* bp = (
const uint8_t*) vp->buf;
142 size_t size = vp->size;
146 TWCR = _BV(TWEN) | _BV(TWINT);
169 } __attribute__((packed));
180 loop_until_bit_is_set(TWCR, TWINT);
181 return ((TWSR &
MASK) == status);
TWI(uint32_t freq=DEFAULT_FREQ)
virtual int write(uint8_t addr, iovec_t *vp)
Data write sent, ACK received.
Slave address/read sent, ACK received.
static const uint32_t DEFAULT_FREQ
Start condition transmitted.
virtual int read(uint8_t addr, void *buf, size_t count)
bool iowait(uint8_t status)
Slave address/write sent, ACK received.
Repeated start transmitted.