21 #ifndef COSA_USI_TWI_HH 22 #define COSA_USI_TWI_HH 25 #if defined(BOARD_ATTINY) 123 void read_buf(
void* buf,
size_t size);
131 void write_buf(
void* buf,
size_t size);
160 virtual void on_request(
void* buf,
size_t size) = 0;
171 virtual void on_event(uint8_t type, uint16_t value);
203 int write(
void* buf,
size_t size);
213 int write(uint8_t header,
void* buf = 0,
size_t size = 0);
223 int write(uint16_t header,
void* buf = 0,
size_t size = 0);
232 int read(
void* buf,
size_t size);
277 } __attribute__((packed));
286 } __attribute__((packed));
293 SR_CLEAR_ALL = _BV(USISIF) | _BV(USIOIF) | _BV(USIPF) | _BV(USIDC),
295 SR_CLEAR = _BV(USIOIF) | _BV(USIPF) | _BV(USIDC),
297 SR_CLEAR_ACK = SR_CLEAR | (0x0E << USICNT0),
299 SR_CLEAR_DATA = SR_CLEAR | (0x0 << USICNT0),
301 CR_SERVICE_MODE = _BV(USIWM1) | _BV(USICS1),
303 CR_START_MODE = _BV(USISIE) | _BV(USIWM1) | _BV(USICS1),
305 CR_TRANSFER_MODE = _BV(USISIE) | _BV(USIOIE) | _BV(USIWM1) | _BV(USIWM0)
308 CR_INIT_MODE = _BV(USIWM1) | _BV(USICS1) | _BV(USICLK),
310 CR_DATA_MODE = _BV(USIWM1) | _BV(USICS1) | _BV(USICLK) | _BV(USITC)
311 } __attribute__((packed));
316 static const uint8_t HEADER_MAX = 4;
317 static const uint8_t VEC_MAX = 4;
318 static const uint8_t WRITE_IX = 0;
319 static const uint8_t READ_IX = 1;
320 uint8_t m_header[HEADER_MAX];
324 volatile State m_state;
325 volatile uint8_t* m_next;
326 volatile uint8_t* m_last;
327 volatile int m_count;
329 volatile bool m_busy;
344 void state(State state)
365 if (ix > VEC_MAX)
return;
366 m_next = (uint8_t*) m_vec[ix].buf;
367 m_last = m_next + m_vec[ix].
size;
377 return (m_last - m_next);
386 bool put(uint8_t data)
390 return (m_next < m_last);
399 bool get(uint8_t& data)
401 if (m_next == m_last)
return (
false);
420 uint8_t transfer(uint8_t data, uint8_t bits =
CHARBITS);
436 int request(uint8_t op);
void USI_START_vect(void)
void set_freq(uint32_t hz)
size_t size
Size of buffer in bytes.
int read(void *buf, size_t size)
int write(void *buf, size_t size)
void acquire(TWI::Driver *dev)
virtual void on_completion(uint8_t type, int count)
static bool push(uint8_t type, Handler *target, uint16_t value=0)