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 | **************************************************************************/
|
9 | #include <inttypes.h>
|
10 | #include <compat/twi.h>
|
11 | #include <uart.h>
|
12 |
|
13 | #include <i2c.h>
|
14 |
|
15 |
|
16 | /* define CPU frequency in Mhz here if not defined in Makefile */
|
17 | #ifndef F_CPU
|
18 | #define F_CPU 16000000UL
|
19 | #endif
|
20 |
|
21 | /* I2C clock in Hz */
|
22 | #define SCL_CLOCK 100000L
|
23 |
|
24 |
|
25 | /*************************************************************************
|
26 | Initialization of the I2C bus interface. Need to be called only once
|
27 | *************************************************************************/
|
28 | void i2c_init(void)
|
29 | {
|
30 | /* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */
|
31 |
|
32 | TWSR = 0; /* no prescaler */
|
33 | TWBR = ((F_CPU/SCL_CLOCK)-16)/2; /* must be > 10 for stable operation */
|
34 | //TWSR = 1;
|
35 | //TWBR = 192;
|
36 |
|
37 | }/* i2c_init */
|
38 |
|
39 |
|
40 | /*************************************************************************
|
41 | Issues a start condition and sends address and transfer direction.
|
42 | return 0 = device accessible, 1= failed to access device
|
43 | *************************************************************************/
|
44 | unsigned char i2c_start(unsigned char address)
|
45 | {
|
46 | uint8_t twst;
|
47 |
|
48 | // send START condition
|
49 | TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
|
50 |
|
51 | // wait until transmission completed
|
52 | while(!(TWCR & (1<<TWINT)));
|
53 |
|
54 | // check value of TWI Status Register. Mask prescaler bits.
|
55 | twst = TW_STATUS & 0xF8;
|
56 | if ( (twst != TW_START) && (twst != TW_REP_START)) return 1;
|
57 |
|
58 | !HIERHER KOMM ICH NICHT!
|
59 |
|
60 | // send device address
|
61 | TWDR = address;
|
62 | TWCR = (1<<TWINT) | (1<<TWEN);
|
63 |
|
64 | // wail until transmission completed and ACK/NACK has been received
|
65 | while(!(TWCR & (1<<TWINT)));
|
66 |
|
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;
|
70 |
|
71 | return 0;
|
72 |
|
73 | }/* i2c_start */
|
74 |
|
75 |
|
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
|
79 |
|
80 | Input: address and transfer direction of I2C device
|
81 | *************************************************************************/
|
82 | void i2c_start_wait(unsigned char address)
|
83 | {
|
84 | uint8_t twst;
|
85 |
|
86 |
|
87 | while ( 1 )
|
88 | {
|
89 | // send START condition
|
90 | TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
|
91 |
|
92 | // wait until transmission completed
|
93 | while(!(TWCR & (1<<TWINT)));
|
94 |
|
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;
|
98 |
|
99 | // send device address
|
100 | TWDR = address;
|
101 | TWCR = (1<<TWINT) | (1<<TWEN);
|
102 |
|
103 | // wail until transmission completed
|
104 | while(!(TWCR & (1<<TWINT)));
|
105 |
|
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) )
|
109 | {
|
110 | /* device busy, send stop condition to terminate write operation */
|
111 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
|
112 |
|
113 | // wait until stop condition is executed and bus released
|
114 | while(TWCR & (1<<TWSTO));
|
115 |
|
116 | continue;
|
117 | }
|
118 | //if( twst != TW_MT_SLA_ACK) return 1;
|
119 | break;
|
120 | }
|
121 |
|
122 | }/* i2c_start_wait */
|
123 |
|
124 |
|
125 | /*************************************************************************
|
126 | Issues a repeated start condition and sends address and transfer direction
|
127 |
|
128 | Input: address and transfer direction of I2C device
|
129 |
|
130 | Return: 0 device accessible
|
131 | 1 failed to access device
|
132 | *************************************************************************/
|
133 | unsigned char i2c_rep_start(unsigned char address)
|
134 | {
|
135 | return i2c_start( address );
|
136 |
|
137 | }/* i2c_rep_start */
|
138 |
|
139 |
|
140 | /*************************************************************************
|
141 | Terminates the data transfer and releases the I2C bus
|
142 | *************************************************************************/
|
143 | void i2c_stop(void)
|
144 | {
|
145 | /* send stop condition */
|
146 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
|
147 |
|
148 | // wait until stop condition is executed and bus released
|
149 | while(TWCR & (1<<TWSTO));
|
150 |
|
151 | }/* i2c_stop */
|
152 |
|
153 |
|
154 | /*************************************************************************
|
155 | Send one byte to I2C device
|
156 |
|
157 | Input: byte to be transfered
|
158 | Return: 0 write successful
|
159 | 1 write failed
|
160 | *************************************************************************/
|
161 | unsigned char i2c_write( unsigned char data )
|
162 | {
|
163 | uint8_t twst;
|
164 |
|
165 | // send data to the previously addressed device
|
166 | TWDR = data;
|
167 | TWCR = (1<<TWINT) | (1<<TWEN);
|
168 |
|
169 | // wait until transmission completed
|
170 | while(!(TWCR & (1<<TWINT)));
|
171 |
|
172 | // check value of TWI Status Register. Mask prescaler bits
|
173 | twst = TW_STATUS & 0xF8;
|
174 | if( twst != TW_MT_DATA_ACK) return 1;
|
175 | return 0;
|
176 |
|
177 | }/* i2c_write */
|
178 |
|
179 |
|
180 | /*************************************************************************
|
181 | Read one byte from the I2C device, request more data from device
|
182 |
|
183 | Return: byte read from I2C device
|
184 | *************************************************************************/
|
185 | unsigned char i2c_readAck(void)
|
186 | {
|
187 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
|
188 | while(!(TWCR & (1<<TWINT)));
|
189 |
|
190 | return TWDR;
|
191 |
|
192 | }/* i2c_readAck */
|
193 |
|
194 |
|
195 | /*************************************************************************
|
196 | Read one byte from the I2C device, read is followed by a stop condition
|
197 |
|
198 | Return: byte read from I2C device
|
199 | *************************************************************************/
|
200 | unsigned char i2c_readNak(void)
|
201 | {
|
202 | TWCR = (1<<TWINT) | (1<<TWEN);
|
203 | while(!(TWCR & (1<<TWINT)));
|
204 |
|
205 | return TWDR;
|
206 |
|
207 | }/* i2c_readNak */
|