2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 * ATtiny USI as an I2C interface
25 #define I2C_USICR ((0 << USISIE) | /* No start condition interrupt */ \
26 (0 << USIOIE) | /* No counter overflow interrupt */ \
27 (1 << USIWM1) | /* Two-wire mode */ \
28 (0 << USIWM0) | /* ... */ \
29 (1 << USICS1) | /* Software clock strobe */ \
30 (0 << USICS0) | /* ... */ \
31 (1 << USICLK)) /* ... */ \
33 #define I2C_USICR_TICK (I2C_USICR | (1 << USITC)) /* Toggle the clock on every write */
35 #define I2C_USISR_1BIT ((1<<USISIF)| /* Clear start condition flag */ \
36 (1<<USIOIF)| /* Clear overflow flag */ \
37 (1<<USIPF)| /* Clear stop condition flag */ \
38 (1<<USIDC)| /* Clear data collision flag */ \
39 (0xE<<USICNT0)) /* Set counter value to 0xe */
41 #define I2C_USISR_8BIT ((1<<USISIF)| /* Clear start condition flag */ \
42 (1<<USIOIF)| /* Clear overflow flag */ \
43 (1<<USIPF)| /* Clear stop condition flag */ \
44 (1<<USIDC)| /* Clear data collision flag */ \
45 (0x0<<USICNT0)) /* Set counter value to 0 */
47 #define T2_TWI 5 /* >4.7μs */
48 #define T4_TWI 4 /* >4.0μs */
50 static inline void ao_i2c_transfer(uint8_t sr)
57 USICR = I2C_USICR_TICK;
59 /* Wait for clock high (clock stretching) */
61 while(!(I2C_PIN & (1<<I2C_PIN_SCL)))
65 USICR = I2C_USICR_TICK;
67 /* Check for transfer complete */
68 if (USISR & (1 << USIOIF))
74 static inline uint8_t ao_i2c_get_byte(uint8_t sr)
78 /* Set SDA to input */
79 I2C_DIR &= ~(1<<I2C_PIN_SDA);
86 /* Set SDA to output */
87 I2C_DIR |= (1<<I2C_PIN_SDA);
93 ao_i2c_write_byte(uint8_t byte)
96 I2C_PORT &= ~(1<<I2C_PIN_SCL);
101 /* Clock and verify (N)ACK from slave */
103 ao_i2c_transfer(I2C_USISR_8BIT);
105 if (ao_i2c_get_byte(I2C_USISR_1BIT) & 0x80)
112 ao_i2c_read_byte(uint8_t ack)
117 ret = ao_i2c_get_byte(I2C_USISR_8BIT);
121 ao_i2c_transfer(I2C_USISR_8BIT);
127 ao_i2c_start_bus(uint8_t address)
129 /* Release SCL to ensure that (repeated) Start can be performed */
131 I2C_PORT |= (1<<I2C_PIN_SCL);
133 while( !(I2C_PORT & (1<<I2C_PIN_SCL)) )
137 /* Generate Start Condition */
140 I2C_PORT &= ~(1<<I2C_PIN_SDA);
144 I2C_PORT &= ~(1<<I2C_PIN_SCL);
147 I2C_PORT |= (1<<I2C_PIN_SDA);
149 return ao_i2c_write_byte(address);
153 ao_i2c_stop_bus(void)
156 I2C_PORT &= ~(1<<I2C_PIN_SDA);
159 I2C_PORT |= (1<<I2C_PIN_SCL);
161 /* Wait for SCL to go high */
162 while( !(I2C_PIN & (1<<I2C_PIN_SCL)) );
166 I2C_PORT |= (1<<I2C_PIN_SDA);
170 /* Send bytes over SPI.
172 * This just polls; the SPI is set to go as fast as possible,
173 * so using interrupts would take way too long
176 ao_i2c_send_bus(void __xdata *block, uint16_t len, uint8_t stop)
181 if (!ao_i2c_write_byte (*d++))
188 /* Send bytes over SPI.
190 * This just polls; the SPI is set to go as fast as possible,
191 * so using interrupts would take way too long
194 ao_i2c_send_fixed_bus(uint8_t d, uint16_t len, uint8_t stop)
197 if (!ao_i2c_write_byte (d))
204 /* Receive bytes over SPI.
206 * Poll, sending zeros and reading data back
209 ao_i2c_recv_bus(void __xdata *block, uint16_t len, uint8_t stop)
214 *d++ = ao_i2c_read_byte (len ? 0x00 : 0xff);
223 * Chip select is the responsibility of the caller
229 /* Pull-ups on SDA and SCL */
230 I2C_PORT |= (1<<I2C_PIN_SDA);
231 I2C_PORT |= (1<<I2C_PIN_SCL);
233 /* SCL and SDA are outputs */
234 I2C_DIR |= (1<<I2C_PIN_SCL);
235 I2C_DIR |= (1<<I2C_PIN_SDA);