+/**
+ * Get 1 byte from slave in master transmitter mode
+ * to the selected slave device through the TWI bus.
+ * If \a ack is true issue a ACK after getting the byte,
+ * otherwise a NACK is issued.
+ *
+ * \return the byte read if ok, EOF on errors.
+ */
+int twi_get(bool ack)
+{
+ TWCR = BV(TWINT) | BV(TWEN) | (ack ? BV(TWEA) : 0);
+ WAIT_TWI_READY;
+
+ if (ack)
+ {
+ if (TW_STATUS != TW_MR_DATA_ACK)
+ {
+ kprintf("!TW_MR_DATA_ACK: %x\n", TWSR);
+ return EOF;
+ }
+ }
+ else
+ {
+ if (TW_STATUS != TW_MR_DATA_NACK)
+ {
+ kprintf("!TW_MR_DATA_NACK: %x\n", TWSR);
+ return EOF;
+ }
+ }
+
+ /* avoid sign extension */
+ return (int)(uint8_t)TWDR;
+}
+