root / trunk / code / projects / libdragonfly / purgatory / i2c.c @ 8
History | View | Annotate | Download (6.4 KB)
1 |
/* @file i2c.c
|
---|---|
2 |
* @brief KEVIN
|
3 |
* In the case where you have master sends and then a master request to the same
|
4 |
* address, you will not give up control of the line because the send and
|
5 |
* request addresses are seen as different addresses. Inbetween it will send a
|
6 |
* restart but will not give up the line.
|
7 |
*
|
8 |
* @author CMU Robotics Club
|
9 |
* @bug Not tested.
|
10 |
*/
|
11 |
|
12 |
#include <avr/interrupt.h> |
13 |
#include <util/twi.h> |
14 |
|
15 |
#include "i2c.h" |
16 |
#include "ring_buffer.h" |
17 |
#include "serial.h" |
18 |
|
19 |
/* Set bit rate 12 = 100kbit/s (max speed setting is 10 for an
|
20 |
* 8 MHz clock). It is a divider, so the lower the number the faster the speed.
|
21 |
*/
|
22 |
#define I2C_BIT_RATE_DIVIDER 0x0C |
23 |
|
24 |
static int start_flag; |
25 |
|
26 |
static fun_mrecv_t master_recv_function;
|
27 |
static fun_srecv_t slave_recv_function;
|
28 |
static fun_send_t slave_send_function;
|
29 |
|
30 |
RING_BUFFER_NEW(i2c_buffer, 64, char, i2c_write_buff, i2c_addr_buff); |
31 |
|
32 |
/**
|
33 |
* KEVIN - description
|
34 |
*
|
35 |
* @param addr KEVIN
|
36 |
* @param master_recv KEVIN
|
37 |
* @param slave_recv KEVIN
|
38 |
* @param slave_send KEVIN
|
39 |
*
|
40 |
* @return KEVIN
|
41 |
*
|
42 |
**/
|
43 |
int i2c_init(char addr, fun_mrecv_t master_recv, fun_srecv_t slave_recv, fun_send_t slave_send) { |
44 |
master_recv_function = master_recv; |
45 |
slave_recv_function = slave_recv; |
46 |
slave_send_function = slave_send; |
47 |
|
48 |
RING_BUFFER_CLEAR(i2c_write_buff); |
49 |
RING_BUFFER_CLEAR(i2c_addr_buff); |
50 |
|
51 |
/* enables twi interrupt, automatic ack sending, and all twi hardware */
|
52 |
TWCR = (_BV(TWEA) | _BV(TWEN) | _BV(TWIE)); |
53 |
|
54 |
/* sets the bit rate of data transmission */
|
55 |
TWBR = I2C_BIT_RATE_DIVIDER; |
56 |
|
57 |
/* sets the address (it is stored in the 7 most significant bits) and allows
|
58 |
* global messages to be accepted */
|
59 |
TWAR = (addr << 1) | 1; |
60 |
|
61 |
|
62 |
i2c_homing_sensor_data_dirty = I2C_DATA_DIRTY; |
63 |
|
64 |
return 0; |
65 |
} |
66 |
|
67 |
/**
|
68 |
* KEVIN - description
|
69 |
**/
|
70 |
void i2c_clear(void) |
71 |
{ |
72 |
RING_BUFFER_CLEAR(i2c_write_buff); |
73 |
RING_BUFFER_CLEAR(i2c_addr_buff); |
74 |
i2c_register = I2C_REG_DEFAULT; |
75 |
} |
76 |
|
77 |
/**
|
78 |
* KEVIN - description
|
79 |
*
|
80 |
* @param dest KEVIN
|
81 |
* @param data KEVIN
|
82 |
* @param bytes KEVIN
|
83 |
*
|
84 |
* @return KEVIN
|
85 |
**/
|
86 |
int i2c_send(char dest, char *data, size_t bytes) { |
87 |
int i;
|
88 |
|
89 |
/* adding data to be sent to ring buffers is not atomic,
|
90 |
* so disable interrupts */
|
91 |
cli(); |
92 |
for(i = 0; i < bytes; i++) { |
93 |
if(RING_BUFFER_FULL(i2c_write_buff)) {
|
94 |
sei(); |
95 |
return -1; |
96 |
} |
97 |
|
98 |
RING_BUFFER_ADD(i2c_write_buff, data[i]); |
99 |
RING_BUFFER_ADD(i2c_addr_buff, dest << 1);
|
100 |
} |
101 |
sei(); |
102 |
|
103 |
/* send the start bit, only if this device is not currently master */
|
104 |
if(!start_flag) {
|
105 |
start_flag = 1;
|
106 |
TWCR |= _BV(TWSTA); |
107 |
TWCR |= _BV(TWINT); |
108 |
} |
109 |
|
110 |
return 0; |
111 |
} |
112 |
|
113 |
/**
|
114 |
* KEVIN - description
|
115 |
*
|
116 |
* @param dest KEVIN
|
117 |
*
|
118 |
* @return KEVIN
|
119 |
**/
|
120 |
int i2c_request(char dest) { |
121 |
if(RING_BUFFER_FULL(i2c_write_buff))
|
122 |
return -1; |
123 |
|
124 |
RING_BUFFER_ADD(i2c_write_buff, 0);
|
125 |
RING_BUFFER_ADD(i2c_addr_buff, (dest << 1) | 1); |
126 |
|
127 |
if(!start_flag) {
|
128 |
start_flag = 1;
|
129 |
TWCR |= _BV(TWSTA); |
130 |
TWCR |= _BV(TWINT); |
131 |
} |
132 |
|
133 |
return 0; |
134 |
} |
135 |
|
136 |
ISR(TWI_vect) { |
137 |
static char data_to_send; |
138 |
static char addr_to_send = -1; |
139 |
char addr, statusCode;
|
140 |
|
141 |
//Get status code (only upper 5 bits)
|
142 |
statusCode = (TWSR & 0xF8);
|
143 |
|
144 |
switch (statusCode) {
|
145 |
//Start sent successfully
|
146 |
case TW_START:
|
147 |
case TW_REP_START:
|
148 |
/* Send address and write
|
149 |
* ring_buffer will not be empty */
|
150 |
RING_BUFFER_REMOVE(i2c_addr_buff, addr_to_send); |
151 |
RING_BUFFER_REMOVE(i2c_write_buff, data_to_send); |
152 |
|
153 |
/* first send the address */
|
154 |
TWDR = addr_to_send; |
155 |
|
156 |
//Turn off start bits
|
157 |
TWCR &= ~_BV(TWSTA); |
158 |
break;
|
159 |
|
160 |
//Master Transmit - Address sent succesfully
|
161 |
case TW_MT_SLA_ACK:
|
162 |
//Send byte
|
163 |
TWDR = data_to_send; |
164 |
break;
|
165 |
//Master Transmit - Data sent succesfully
|
166 |
case TW_MT_DATA_ACK:
|
167 |
//If there is still data to send
|
168 |
if(!RING_BUFFER_EMPTY(i2c_write_buff)) {
|
169 |
RING_BUFFER_PEEK(i2c_addr_buff, addr); |
170 |
|
171 |
//Still data for this address
|
172 |
if(addr == addr_to_send) {
|
173 |
RING_BUFFER_REMOVE(i2c_addr_buff, addr); |
174 |
RING_BUFFER_REMOVE(i2c_write_buff, TWDR); |
175 |
break;
|
176 |
} |
177 |
//No more data for this address, data for another address -> resend start
|
178 |
else {
|
179 |
TWCR |= _BV(TWSTA); |
180 |
break;
|
181 |
} |
182 |
} |
183 |
|
184 |
/* there are no bytes to send */
|
185 |
TWCR |= _BV(TWSTO); |
186 |
start_flag = 0;
|
187 |
break;
|
188 |
|
189 |
//Master Receive - Address sent succesfully
|
190 |
case TW_MR_SLA_ACK:
|
191 |
break;
|
192 |
//Master Receive - Data received succesfully
|
193 |
case TW_MR_DATA_ACK:
|
194 |
if(master_recv_function) {
|
195 |
if(!master_recv_function(TWDR)) {
|
196 |
TWCR &= ~_BV(TWEA); |
197 |
} |
198 |
} |
199 |
break;
|
200 |
case TW_MR_DATA_NACK:
|
201 |
TWCR |= _BV(TWEA); |
202 |
|
203 |
//If there is still data to send
|
204 |
if(!RING_BUFFER_EMPTY(i2c_write_buff)) {
|
205 |
TWCR |= _BV(TWSTA); |
206 |
break;
|
207 |
} |
208 |
|
209 |
/* there are no bytes to send */
|
210 |
TWCR |= _BV(TWSTO); |
211 |
start_flag = 0;
|
212 |
break;
|
213 |
|
214 |
//Slave Transmit - Address received
|
215 |
case TW_ST_SLA_ACK:
|
216 |
break;
|
217 |
//Slave Transmit - Nack received, no data requsted
|
218 |
case TW_ST_DATA_NACK:
|
219 |
break;
|
220 |
//Slave Transmit - Data requested, ack received
|
221 |
case TW_ST_DATA_ACK:
|
222 |
if (slave_send_function) {
|
223 |
TWDR = slave_send_function(); |
224 |
} |
225 |
break;
|
226 |
|
227 |
//Slave Receive - Address received
|
228 |
case TW_SR_SLA_ACK:
|
229 |
break;
|
230 |
//Slave Receive - Data received, ack returned
|
231 |
case TW_SR_DATA_ACK:
|
232 |
if(slave_recv_function)
|
233 |
slave_recv_function(TWDR); |
234 |
break;
|
235 |
|
236 |
//Stop sent
|
237 |
case TW_SR_STOP:
|
238 |
break;
|
239 |
} |
240 |
|
241 |
/* Toggle TWINT so that it resets and executes the commands */
|
242 |
TWCR |= _BV(TWINT); |
243 |
} |
244 |
|
245 |
/**
|
246 |
* KEVIN - description
|
247 |
* @param data KEVIN
|
248 |
**/
|
249 |
void i2c_packet_sniff(char data) { |
250 |
usb_putc(data); |
251 |
usb_putc(' ');
|
252 |
} |
253 |
|
254 |
/**
|
255 |
* KEVIN - description
|
256 |
*
|
257 |
* @param i2c_byte KEVIN
|
258 |
**/
|
259 |
void i2c_packet_rec (char i2c_byte) |
260 |
{ |
261 |
if(i2c_prev_byte == 'H') { |
262 |
usb_puti(i2c_byte); |
263 |
i2c_homing_sensor_data = i2c_byte; |
264 |
i2c_homing_sensor_data_dirty = I2C_DATA_FRESH; |
265 |
} |
266 |
else
|
267 |
usb_putc(i2c_byte); |
268 |
usb_putc(' ');
|
269 |
i2c_prev_byte = i2c_byte; |
270 |
switch(i2c_byte)
|
271 |
{ |
272 |
case I2C_MSG_BATTERY_CHARGING:
|
273 |
i2c_register = I2C_REG_CHARGING; |
274 |
break;
|
275 |
case I2C_MSG_CONTACT_ERROR:
|
276 |
i2c_register = I2C_REG_CONTACT_ERROR; |
277 |
break;
|
278 |
case I2C_MSG_BATTERY_FULL:
|
279 |
i2c_register = I2C_REG_BATTERY_FULL; |
280 |
break;
|
281 |
case I2C_MSG_NO_CONTACT:
|
282 |
i2c_register = I2C_REG_NO_CONTACT; |
283 |
break;
|
284 |
default:
|
285 |
i2c_register = I2C_REG_DEFAULT; |
286 |
break;
|
287 |
} |
288 |
} |
289 |
|