/* * Copyright © 2011 Keith Packard * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ #include /* * ATtiny USI as an I2C interface */ #define I2C_USICR ((0 << USISIE) | /* No start condition interrupt */ \ (0 << USIOIE) | /* No counter overflow interrupt */ \ (1 << USIWM1) | /* Two-wire mode */ \ (0 << USIWM0) | /* ... */ \ (1 << USICS1) | /* Software clock strobe */ \ (0 << USICS0) | /* ... */ \ (1 << USICLK)) /* ... */ \ #define I2C_USICR_TICK (I2C_USICR | (1 << USITC)) /* Toggle the clock on every write */ #define I2C_USISR_1BIT ((1<4.7μs */ #define T4_TWI 4 /* >4.0μs */ static inline void ao_i2c_transfer(uint8_t sr) { USISR = sr; for (;;) { ao_delay_us(T2_TWI); /* Clock high */ USICR = I2C_USICR_TICK; /* Wait for clock high (clock stretching) */ ao_delay_us(T4_TWI); while(!(I2C_PIN & (1<