root / branches / simulator / projects / libdragonfly / i2c.c @ 891
History | View | Annotate | Download (8.06 KB)
1 |
/**
|
---|---|
2 |
* Copyright (c) 2007 Colony Project
|
3 |
*
|
4 |
* Permission is hereby granted, free of charge, to any person
|
5 |
* obtaining a copy of this software and associated documentation
|
6 |
* files (the "Software"), to deal in the Software without
|
7 |
* restriction, including without limitation the rights to use,
|
8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
* copies of the Software, and to permit persons to whom the
|
10 |
* Software is furnished to do so, subject to the following
|
11 |
* conditions:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be
|
14 |
* included in all copies or substantial portions of the Software.
|
15 |
*
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
23 |
* OTHER DEALINGS IN THE SOFTWARE.
|
24 |
**/
|
25 |
|
26 |
|
27 |
/**
|
28 |
* @file i2c.c
|
29 |
* @brief Implemenation of I2C communications protocol
|
30 |
*
|
31 |
* In the case where you have master sends and then a master request to the same
|
32 |
* address, you will not give up control of the line because the send and
|
33 |
* request addresses are seen as different addresses. In between it will send a
|
34 |
* restart but will not give up the line.
|
35 |
*
|
36 |
* @author CMU Robotics Club, Kevin Woo, Sursh Nidhiry
|
37 |
* @bug Not tested.
|
38 |
*/
|
39 |
|
40 |
#include <avr/interrupt.h> |
41 |
#include <util/twi.h> |
42 |
|
43 |
#include "i2c.h" |
44 |
#include "ring_buffer.h" |
45 |
|
46 |
|
47 |
/**
|
48 |
* @brief Set bit rate 12 = 100kbit/s (max speed setting is 10 for an
|
49 |
* 8 MHz clock). It is a divider, so the lower the number the faster the speed.
|
50 |
*/
|
51 |
#define I2C_BIT_RATE_DIVIDER 0x0C |
52 |
|
53 |
static int start_flag; |
54 |
|
55 |
static fun_mrecv_t master_recv_function;
|
56 |
static fun_srecv_t slave_recv_function;
|
57 |
static fun_send_t slave_send_function;
|
58 |
|
59 |
RING_BUFFER_NEW(i2c_buffer, 128, char, i2c_write_buff, i2c_addr_buff); |
60 |
|
61 |
|
62 |
int i2c_init(char addr, fun_mrecv_t master_recv, fun_srecv_t slave_recv, fun_send_t slave_send) { |
63 |
master_recv_function = master_recv; |
64 |
slave_recv_function = slave_recv; |
65 |
slave_send_function = slave_send; |
66 |
|
67 |
RING_BUFFER_CLEAR(i2c_write_buff); |
68 |
RING_BUFFER_CLEAR(i2c_addr_buff); |
69 |
|
70 |
/* enables twi interrupt, automatic ack sending, and all twi hardware */
|
71 |
TWCR = (_BV(TWEA) | _BV(TWEN) | _BV(TWIE)); |
72 |
|
73 |
/* sets the bit rate of data transmission */
|
74 |
TWBR = I2C_BIT_RATE_DIVIDER; |
75 |
|
76 |
/* sets the address (it is stored in the 7 most significant bits) and allows
|
77 |
* global messages to be accepted */
|
78 |
TWAR = (addr << 1) | 1; |
79 |
|
80 |
return 0; |
81 |
} |
82 |
|
83 |
int i2c_send(char dest, char *data, size_t bytes) { |
84 |
int i;
|
85 |
|
86 |
/* adding data to be sent to ring buffers is not atomic,
|
87 |
* so disable interrupts */
|
88 |
cli(); |
89 |
for(i = 0; i < bytes; i++) { |
90 |
if(RING_BUFFER_FULL(i2c_write_buff)) {
|
91 |
sei(); |
92 |
return -1; |
93 |
} |
94 |
|
95 |
RING_BUFFER_ADD(i2c_write_buff, data[i]); |
96 |
RING_BUFFER_ADD(i2c_addr_buff, dest << 1);
|
97 |
} |
98 |
|
99 |
/* re-enable the interrupts */
|
100 |
sei(); |
101 |
|
102 |
/* send the start bit, only if this device is not currently master */
|
103 |
if(!start_flag) {
|
104 |
start_flag = 1;
|
105 |
TWCR |= _BV(TWSTA); |
106 |
TWCR |= _BV(TWINT); |
107 |
} |
108 |
|
109 |
return 0; |
110 |
} |
111 |
|
112 |
int i2c_request(char dest) { |
113 |
if(RING_BUFFER_FULL(i2c_write_buff))
|
114 |
return -1; |
115 |
|
116 |
RING_BUFFER_ADD(i2c_write_buff, 0);
|
117 |
RING_BUFFER_ADD(i2c_addr_buff, (dest << 1) | 1); |
118 |
|
119 |
if(!start_flag) {
|
120 |
start_flag = 1;
|
121 |
TWCR |= _BV(TWSTA); |
122 |
TWCR |= _BV(TWINT); |
123 |
} |
124 |
|
125 |
return 0; |
126 |
} |
127 |
|
128 |
/** @} **/
|
129 |
|
130 |
/**
|
131 |
* @brief Interrupt to handle I2C interrupts from the I2C hardware.
|
132 |
*
|
133 |
* Uses the status codes from the I2C register to handle the events
|
134 |
* needed to advance in I2C stages. For instance, you will get a bit for
|
135 |
* receiving a start ack, then a address ack, then a data ack, etc.
|
136 |
* The events are handled in each switch case. The status codes are defined
|
137 |
* by avr-gcc in /util/twi.h but are the same codes as the Atmel documentation.
|
138 |
*
|
139 |
* Bytes are sent by popping off the ring buffer. It also will keep track
|
140 |
* of what modes the send is in.
|
141 |
*
|
142 |
* Errors are handled here as well.
|
143 |
**/
|
144 |
ISR(TWI_vect) { |
145 |
static char data_to_send; |
146 |
static char addr_to_send = -1; |
147 |
char addr, statusCode;
|
148 |
|
149 |
//Get status code (only upper 5 bits)
|
150 |
statusCode = (TWSR & 0xF8);
|
151 |
|
152 |
switch (statusCode) {
|
153 |
//Start sent successfully
|
154 |
case TW_START:
|
155 |
case TW_REP_START:
|
156 |
/* Send address and write
|
157 |
* ring_buffer will not be empty */
|
158 |
RING_BUFFER_REMOVE(i2c_addr_buff, addr_to_send); |
159 |
RING_BUFFER_REMOVE(i2c_write_buff, data_to_send); |
160 |
|
161 |
/* first send the address */
|
162 |
TWDR = addr_to_send; |
163 |
|
164 |
//Turn off start bits
|
165 |
TWCR &= ~_BV(TWSTA); |
166 |
break;
|
167 |
|
168 |
//Master Transmit - Address sent succesfully
|
169 |
case TW_MT_SLA_ACK:
|
170 |
//Send byte
|
171 |
TWDR = data_to_send; |
172 |
PORTG &= ~_BV(PG2); |
173 |
break;
|
174 |
|
175 |
//Master Transmit - Data sent succesfully
|
176 |
case TW_MT_DATA_ACK:
|
177 |
//If there is still data to send
|
178 |
if(!RING_BUFFER_EMPTY(i2c_write_buff)) {
|
179 |
RING_BUFFER_PEEK(i2c_addr_buff, addr); |
180 |
|
181 |
//Still data for this address
|
182 |
if (addr == addr_to_send) {
|
183 |
RING_BUFFER_REMOVE(i2c_addr_buff, addr); |
184 |
RING_BUFFER_REMOVE(i2c_write_buff, TWDR); |
185 |
break;
|
186 |
//No more data for this address, data for another address -> resend start
|
187 |
} else {
|
188 |
TWCR |= _BV(TWSTA); |
189 |
break;
|
190 |
} |
191 |
} |
192 |
/* there are no bytes to send */
|
193 |
TWCR |= _BV(TWSTO); |
194 |
start_flag = 0;
|
195 |
break;
|
196 |
|
197 |
//Master Transmit - Slave sends a nack, transmit is done
|
198 |
case TW_MT_DATA_NACK:
|
199 |
PORTG |= _BV(PG2); |
200 |
TWCR |= _BV(TWSTO); |
201 |
start_flag = 0;
|
202 |
break;
|
203 |
|
204 |
//Master Receive - Address sent succesfully
|
205 |
case TW_MR_SLA_ACK:
|
206 |
PORTG |= _BV(PG2); |
207 |
break;
|
208 |
|
209 |
//Master Receive - Data received succesfully
|
210 |
case TW_MR_DATA_ACK:
|
211 |
if(master_recv_function) {
|
212 |
if(!master_recv_function(TWDR)) {
|
213 |
TWCR &= ~_BV(TWEA); |
214 |
} |
215 |
} |
216 |
break;
|
217 |
|
218 |
//Master Receive - Slave sends a nack, transmission is done
|
219 |
case TW_MR_DATA_NACK:
|
220 |
TWCR |= _BV(TWEA); |
221 |
|
222 |
//If there is still data to send
|
223 |
if(!RING_BUFFER_EMPTY(i2c_write_buff)) {
|
224 |
TWCR |= _BV(TWSTA); |
225 |
break;
|
226 |
} |
227 |
|
228 |
/* there are no bytes to send */
|
229 |
TWCR |= _BV(TWSTO); |
230 |
start_flag = 0;
|
231 |
break;
|
232 |
|
233 |
//Slave Transmit - Address received
|
234 |
case TW_ST_SLA_ACK:
|
235 |
break;
|
236 |
|
237 |
//Slave Transmit - Nack received, no data requsted
|
238 |
case TW_ST_DATA_NACK:
|
239 |
break;
|
240 |
|
241 |
//Slave Transmit - Data requested, ack received
|
242 |
case TW_ST_DATA_ACK:
|
243 |
if (slave_send_function) {
|
244 |
TWDR = slave_send_function(); |
245 |
} |
246 |
break;
|
247 |
|
248 |
//Slave Receive - Address received
|
249 |
case TW_SR_SLA_ACK:
|
250 |
break;
|
251 |
|
252 |
//Slave Receive - Data received, ack returned
|
253 |
case TW_SR_DATA_ACK:
|
254 |
if(slave_recv_function) {
|
255 |
slave_recv_function(TWDR); |
256 |
} |
257 |
|
258 |
break;
|
259 |
|
260 |
//Stop sent
|
261 |
case TW_SR_STOP:
|
262 |
break;
|
263 |
|
264 |
//Problem on the bus, reset everything
|
265 |
default:
|
266 |
TWCR |= _BV(TWSTO); |
267 |
start_flag = 0;
|
268 |
RING_BUFFER_CLEAR(i2c_write_buff); |
269 |
RING_BUFFER_CLEAR(i2c_addr_buff); |
270 |
} |
271 |
|
272 |
/* Toggle TWINT so that it resets and executes the commands */
|
273 |
TWCR |= _BV(TWINT); |
274 |
} |
275 |
|