1 /*************************************************************************
2 * Title: I2C master library using hardware TWI interface
3 * Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury
4 * File: $Id: twimaster.c,v 1.3 2005/07/02 11:14:21 Peter Exp $
5 * Software: AVR-GCC 3.4.3 / avr-libc 1.2.3
6 * Target: any AVR device with hardware TWI
7 * Usage: API compatible with I2C Software Library i2cmaster.h
8 **************************************************************************/
10 #include <compat/twi.h>
12 #include <i2cmaster.h>
14 /* define CPU frequency in Hz here if not defined in Makefile */
16 #define F_CPU 16000000UL
20 #define SCL_CLOCK 400000L
23 /*************************************************************************
24 Initialization of the I2C bus interface. Need to be called only once
25 *************************************************************************/
28 /* initialize TWI clock
29 * minimal values in Bit Rate Register (TWBR) and minimal Prescaler
30 * bits in the TWI Status Register should give us maximal possible
31 * I2C bus speed - about 444 kHz
33 * for more details, see 20.5.2 in ATmega16/32 secification
36 TWSR = 0; /* no prescaler */
37 TWBR = 10; /* must be >= 10 for stable operation */
42 /*************************************************************************
43 Issues a start condition and sends address and transfer direction.
44 return 0 = device accessible, 1= failed to access device
45 *************************************************************************/
46 unsigned char i2c_start(unsigned char address)
50 /* send START condition */
51 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
53 /* wait until transmission completed */
54 while(!(TWCR & (1<<TWINT)));
56 /* check value of TWI Status Register. Mask prescaler bits. */
57 twst = TW_STATUS & 0xF8;
58 if ( (twst != TW_START) && (twst != TW_REP_START)) return 1;
60 /* send device address */
62 TWCR = (1<<TWINT) | (1<<TWEN);
64 /* wail until transmission completed and ACK/NACK has been received */
65 while(!(TWCR & (1<<TWINT)));
67 /* check value of TWI Status Register. Mask prescaler bits. */
68 twst = TW_STATUS & 0xF8;
69 if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;
76 /*************************************************************************
77 Issues a start condition and sends address and transfer direction.
78 If device is busy, use ack polling to wait until device is ready
80 Input: address and transfer direction of I2C device
81 *************************************************************************/
82 void i2c_start_wait(unsigned char address)
89 /* send START condition */
90 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
92 /* wait until transmission completed */
93 while(!(TWCR & (1<<TWINT)));
95 /* check value of TWI Status Register. Mask prescaler bits. */
96 twst = TW_STATUS & 0xF8;
97 if ( (twst != TW_START) && (twst != TW_REP_START)) continue;
99 /* send device address */
101 TWCR = (1<<TWINT) | (1<<TWEN);
103 /* wail until transmission completed */
104 while(!(TWCR & (1<<TWINT)));
106 /* check value of TWI Status Register. Mask prescaler bits. */
107 twst = TW_STATUS & 0xF8;
108 if ( (twst == TW_MT_SLA_NACK )||(twst ==TW_MR_DATA_NACK) )
110 /* device busy, send stop condition to terminate write operation */
111 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
113 /* wait until stop condition is executed and bus released */
114 while(TWCR & (1<<TWSTO));
122 }/* i2c_start_wait */
125 /*************************************************************************
126 Issues a repeated start condition and sends address and transfer direction
128 Input: address and transfer direction of I2C device
130 Return: 0 device accessible
131 1 failed to access device
132 *************************************************************************/
133 unsigned char i2c_rep_start(unsigned char address)
135 return i2c_start( address );
140 /*************************************************************************
141 Terminates the data transfer and releases the I2C bus
142 *************************************************************************/
145 /* send stop condition */
146 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
148 /* wait until stop condition is executed and bus released */
149 while(TWCR & (1<<TWSTO));
154 /*************************************************************************
155 Send one byte to I2C device
157 Input: byte to be transfered
158 Return: 0 write successful
160 *************************************************************************/
161 unsigned char i2c_write( unsigned char data )
165 /* send data to the previously addressed device */
167 TWCR = (1<<TWINT) | (1<<TWEN);
169 /* wait until transmission completed */
170 while(!(TWCR & (1<<TWINT)));
172 /* check value of TWI Status Register. Mask prescaler bits */
173 twst = TW_STATUS & 0xF8;
174 if( twst != TW_MT_DATA_ACK) return 1;
180 /*************************************************************************
181 Read one byte from the I2C device, request more data from device
183 Return: byte read from I2C device
184 *************************************************************************/
185 unsigned char i2c_readAck(void)
187 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
188 while(!(TWCR & (1<<TWINT)));
195 /*************************************************************************
196 Read one byte from the I2C device, read is followed by a stop condition
198 Return: byte read from I2C device
199 *************************************************************************/
200 unsigned char i2c_readNak(void)
202 TWCR = (1<<TWINT) | (1<<TWEN);
203 while(!(TWCR & (1<<TWINT)));