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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 * ATtiny USI as an I2C interface
24 #define I2C_USICR ((0 << USISIE) | /* No start condition interrupt */ \
25 (0 << USIOIE) | /* No counter overflow interrupt */ \
26 (1 << USIWM1) | /* Two-wire mode */ \
27 (0 << USIWM0) | /* ... */ \
28 (1 << USICS1) | /* Software clock strobe */ \
29 (0 << USICS0) | /* ... */ \
30 (1 << USICLK)) /* ... */ \
32 #define I2C_USICR_TICK (I2C_USICR | (1 << USITC)) /* Toggle the clock on every write */
34 #define I2C_USISR_1BIT ((1<<USISIF)| /* Clear start condition flag */ \
35 (1<<USIOIF)| /* Clear overflow flag */ \
36 (1<<USIPF)| /* Clear stop condition flag */ \
37 (1<<USIDC)| /* Clear data collision flag */ \
38 (0xE<<USICNT0)) /* Set counter value to 0xe */
40 #define I2C_USISR_8BIT ((1<<USISIF)| /* Clear start condition flag */ \
41 (1<<USIOIF)| /* Clear overflow flag */ \
42 (1<<USIPF)| /* Clear stop condition flag */ \
43 (1<<USIDC)| /* Clear data collision flag */ \
44 (0x0<<USICNT0)) /* Set counter value to 0 */
46 #define T2_TWI 5 /* >4.7μs */
47 #define T4_TWI 4 /* >4.0μs */
49 static inline void ao_i2c_transfer(uint8_t sr)
56 USICR = I2C_USICR_TICK;
58 /* Wait for clock high (clock stretching) */
60 while(!(I2C_PIN & (1<<I2C_PIN_SCL)))
64 USICR = I2C_USICR_TICK;
66 /* Check for transfer complete */
67 if (USISR & (1 << USIOIF))
73 static inline uint8_t ao_i2c_get_byte(uint8_t sr)
77 /* Set SDA to input */
78 I2C_DIR &= ~(1<<I2C_PIN_SDA);
85 /* Set SDA to output */
86 I2C_DIR |= (1<<I2C_PIN_SDA);
92 ao_i2c_write_byte(uint8_t byte)
95 I2C_PORT &= ~(1<<I2C_PIN_SCL);
100 /* Clock and verify (N)ACK from slave */
102 ao_i2c_transfer(I2C_USISR_8BIT);
104 if (ao_i2c_get_byte(I2C_USISR_1BIT) & 0x80)
111 ao_i2c_read_byte(uint8_t ack)
116 ret = ao_i2c_get_byte(I2C_USISR_8BIT);
120 ao_i2c_transfer(I2C_USISR_8BIT);
126 ao_i2c_start_bus(uint8_t address)
128 /* Release SCL to ensure that (repeated) Start can be performed */
130 I2C_PORT |= (1<<I2C_PIN_SCL);
132 while( !(I2C_PORT & (1<<I2C_PIN_SCL)) )
136 /* Generate Start Condition */
139 I2C_PORT &= ~(1<<I2C_PIN_SDA);
143 I2C_PORT &= ~(1<<I2C_PIN_SCL);
146 I2C_PORT |= (1<<I2C_PIN_SDA);
148 return ao_i2c_write_byte(address);
152 ao_i2c_stop_bus(void)
155 I2C_PORT &= ~(1<<I2C_PIN_SDA);
158 I2C_PORT |= (1<<I2C_PIN_SCL);
160 /* Wait for SCL to go high */
161 while( !(I2C_PIN & (1<<I2C_PIN_SCL)) );
165 I2C_PORT |= (1<<I2C_PIN_SDA);
169 /* Send bytes over SPI.
171 * This just polls; the SPI is set to go as fast as possible,
172 * so using interrupts would take way too long
175 ao_i2c_send_bus(void __xdata *block, uint16_t len, uint8_t stop)
180 if (!ao_i2c_write_byte (*d++))
187 /* Send bytes over SPI.
189 * This just polls; the SPI is set to go as fast as possible,
190 * so using interrupts would take way too long
193 ao_i2c_send_fixed_bus(uint8_t d, uint16_t len, uint8_t stop)
196 if (!ao_i2c_write_byte (d))
203 /* Receive bytes over SPI.
205 * Poll, sending zeros and reading data back
208 ao_i2c_recv_bus(void __xdata *block, uint16_t len, uint8_t stop)
213 *d++ = ao_i2c_read_byte (len ? 0x00 : 0xff);
222 * Chip select is the responsibility of the caller
228 /* Pull-ups on SDA and SCL */
229 I2C_PORT |= (1<<I2C_PIN_SDA);
230 I2C_PORT |= (1<<I2C_PIN_SCL);
232 /* SCL and SDA are outputs */
233 I2C_DIR |= (1<<I2C_PIN_SCL);
234 I2C_DIR |= (1<<I2C_PIN_SDA);