23 #if !defined(BOARD_ATTINY) 29 #if defined(BOARD_ATMEGA32U4) 39 uint8_t key =
lock(m_busy);
75 TWI::request(uint8_t op)
78 m_state = ((op == READ_OP) ? MR_STATE : MT_STATE);
79 m_addr = (m_dev->
m_addr | op);
81 m_next = (uint8_t*) m_vec[0].buf;
82 m_last = m_next + m_vec[0].
size;
97 return (request(WRITE_OP));
104 m_header[0] = header;
108 return (request(WRITE_OP));
115 m_header[0] = (header >> 8);
116 m_header[1] = header;
120 return (request(WRITE_OP));
129 return (request(READ_OP));
135 while (m_state > IDLE_STATE)
yield();
140 TWI::isr_start(State state, uint8_t ix)
147 m_next = (uint8_t*) m_vec[ix].buf;
148 m_last = m_next + m_vec[ix].
size;
153 TWI::isr_stop(State state, uint8_t type)
155 TWCR = TWI::STOP_CMD;
156 loop_until_bit_is_clear(TWCR, TWSTO);
157 if (
UNLIKELY(state == TWI::ERROR_STATE)) m_count = -1;
161 if (m_dev->
is_async() || m_status == SR_STOP) {
170 TWI::isr_write(Command cmd)
172 if (
UNLIKELY(m_next == m_last))
return (
false);
180 TWI::isr_read(Command cmd)
182 if (
UNLIKELY(m_next == m_last))
return (
false);
185 if (cmd != 0) TWCR = cmd;
192 switch (
twi.m_status) {
200 TWCR = TWI::DATA_CMD;
204 TWCR = TWI::IDLE_CMD;
205 twi.m_state = TWI::ERROR_STATE;
212 case TWI::MT_SLA_ACK:
213 case TWI::MT_DATA_ACK:
214 if (
twi.m_next ==
twi.m_last)
twi.isr_start(TWI::MT_STATE, TWI::NEXT_IX);
215 if (
twi.isr_write(TWI::DATA_CMD))
break;
216 case TWI::MT_DATA_NACK:
219 case TWI::MT_SLA_NACK:
226 case TWI::MR_DATA_ACK:
228 case TWI::MR_SLA_ACK:
229 TWCR = (
twi.m_next < (
twi.m_last - 1)) ? TWI::ACK_CMD : TWI::NACK_CMD;
231 case TWI::MR_DATA_NACK:
235 case TWI::MR_SLA_NACK:
242 case TWI::ST_SLA_ACK:
243 case TWI::ST_ARB_LOST_SLA_ACK:
245 case TWI::ST_DATA_ACK:
246 if (
twi.isr_write(TWI::ACK_CMD))
break;
247 TWCR = TWI::NACK_CMD;
249 case TWI::ST_DATA_NACK:
250 case TWI::ST_LAST_DATA:
252 twi.m_state = TWI::IDLE_STATE;
258 case TWI::SR_SLA_ACK:
259 case TWI::SR_GCALL_ACK:
260 case TWI::SR_ARB_LOST_SLA_ACK:
261 case TWI::SR_ARB_LOST_GCALL_ACK:
265 case TWI::SR_DATA_ACK:
266 case TWI::SR_GCALL_DATA_ACK:
267 if (
twi.isr_read(TWI::ACK_CMD))
break;
268 case TWI::SR_DATA_NACK:
269 case TWI::SR_GCALL_DATA_NACK:
270 TWCR = TWI::NACK_CMD;
281 twi.isr_stop(TWI::ERROR_STATE);
285 TWCR = TWI::IDLE_CMD;
297 TWCR = TWI::IDLE_CMD;
305 void* buf =
twi.m_vec[WRITE_IX].
buf;
307 on_request(buf, size);
314 twi.m_vec[WRITE_IX].
buf = buf;
315 twi.m_vec[WRITE_IX].
size = size;
321 twi.m_vec[READ_IX].
buf = buf;
322 twi.m_vec[READ_IX].
size = size;
#define bit_mask_clear(p, m)
static const uint8_t READ_IX
virtual void on_event(uint8_t type, uint16_t value)
void write_buf(void *buf, size_t size)
size_t size
Size of buffer in bytes.
static const uint32_t DEFAULT_FREQ
void * buf
Buffer pointer.
friend void TWI_vect(void)
void read_buf(void *buf, size_t size)
void acquire(TWI::Driver *dev)
static const uint8_t WRITE_IX
#define bit_mask_set(p, m)
virtual void on_completion(uint8_t type, int count)
bool read_request(void *buf, size_t size)
bool write_request(void *buf, size_t size)
void iovec_arg(iovec_t *&vp, const void *buf, size_t size)
void iovec_end(iovec_t *&vp)