25 #if defined(BOARD_ATTINY) 29 #include <avr/power.h> 57 static const uint32_t
MAX_FREQ = (F_CPU / (16 + 2*2));
142 void read_buf(
void* buf,
size_t size);
149 void write_buf(
void* buf,
size_t size);
177 virtual void on_request(
void* buf,
size_t size) = 0;
183 static const uint8_t WRITE_IX = 0;
184 static const uint8_t READ_IX = 1;
195 virtual void on_event(uint8_t type, uint16_t value);
214 m_freq(((F_CPU / DEFAULT_FREQ) - 16) / 2),
217 for (uint8_t ix = 0; ix < VEC_MAX; ix++) {
283 __attribute__((always_inline))
297 int write(uint8_t header,
void* buf = 0,
size_t size = 0)
298 __attribute__((always_inline))
312 int write(uint16_t header,
void* buf = 0,
size_t size = 0)
313 __attribute__((always_inline))
326 int read(
void* buf,
size_t size)
327 __attribute__((always_inline))
346 __attribute__((always_inline))
348 m_freq = (hz <
MAX_FREQ) ? (((F_CPU / hz) - 16) / 2) : 2;
378 } __attribute__((packed));
384 # define TWI_STATUS(x) ((x) >> 3) 429 } __attribute__((packed));
438 } __attribute__((packed));
441 static const uint8_t NEXT_IX = 255;
448 IDLE_CMD = _BV(TWEA) | _BV(TWEN) | _BV(TWIE),
449 START_CMD = _BV(TWINT) | _BV(TWEA) | _BV(TWSTA) | _BV(TWEN) | _BV(TWIE),
450 DATA_CMD = _BV(TWINT) | _BV(TWEN) | _BV(TWIE),
451 ACK_CMD = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE),
452 NACK_CMD = _BV(TWINT) | _BV(TWEN) | _BV(TWIE),
453 STOP_CMD = _BV(TWINT) | _BV(TWEA) | _BV(TWSTO) | _BV(TWEN) | _BV(TWIE)
454 } __attribute__((packed));
459 static const uint8_t HEADER_MAX = 4;
460 static const uint8_t VEC_MAX = 4;
461 uint8_t m_header[HEADER_MAX];
463 volatile State m_state;
464 volatile uint8_t m_status;
465 volatile uint8_t m_ix;
466 volatile uint8_t* m_next;
467 volatile uint8_t* m_last;
468 volatile int m_count;
472 volatile bool m_busy;
480 void isr_start(State state, uint8_t ix);
488 bool isr_write(Command cmd);
497 bool isr_read(Command cmd = NULL_CMD);
513 bool request(uint8_t op);
virtual void on_completion(uint8_t type, int count)
int write(uint16_t header, void *buf=0, size_t size=0)
void set_freq(uint32_t hz)
int write(uint8_t header, void *buf=0, size_t size=0)
static const uint32_t DEFAULT_FREQ
int read(void *buf, size_t size)
friend void TWI_vect(void)
static const uint32_t MAX_FREQ
int write(void *buf, size_t size)
void acquire(TWI::Driver *dev)
virtual void on_completion(uint8_t type, int count)
bool read_request(void *buf, size_t size)
static bool push(uint8_t type, Handler *target, uint16_t value=0)
bool write_request(void *buf, size_t size)