Revision 277
more cleanup
i2c.c | ||
---|---|---|
94 | 94 |
* are not set to null they will be called when that transmission mode is called. The address |
95 | 95 |
* is your address that you will listen to when you are not the master. |
96 | 96 |
* |
97 |
* @param addr Your address on the I2C bus.
|
|
98 |
* @param master_recv The address of the function to call when you receive a byte when you are a
|
|
99 |
* master.
|
|
100 |
* @param slave_recv The address of the function to call when you are a slave you receive data
|
|
101 |
* from the master
|
|
102 |
* @param slave_send The address of the function to call when you are a slave and the master
|
|
103 |
* requests data from you.
|
|
97 |
* @param addr Your address on the I2C bus.
|
|
98 |
* @param master_recv The address of the function to call when you receive a byte when you are a
|
|
99 |
* master.
|
|
100 |
* @param slave_recv The address of the function to call when you are a slave you receive data
|
|
101 |
* from the master
|
|
102 |
* @param slave_send The address of the function to call when you are a slave and the master
|
|
103 |
* requests data from you.
|
|
104 | 104 |
* |
105 | 105 |
* @return 0 for success, nonzero for failure |
106 | 106 |
**/ |
107 | 107 |
int i2c_init(char addr, fun_mrecv_t master_recv, fun_srecv_t slave_recv, fun_send_t slave_send) { |
108 |
master_recv_function = master_recv;
|
|
109 |
slave_recv_function = slave_recv;
|
|
110 |
slave_send_function = slave_send;
|
|
108 |
master_recv_function = master_recv; |
|
109 |
slave_recv_function = slave_recv; |
|
110 |
slave_send_function = slave_send; |
|
111 | 111 |
|
112 |
RING_BUFFER_CLEAR(i2c_write_buff);
|
|
113 |
RING_BUFFER_CLEAR(i2c_addr_buff);
|
|
112 |
RING_BUFFER_CLEAR(i2c_write_buff); |
|
113 |
RING_BUFFER_CLEAR(i2c_addr_buff); |
|
114 | 114 |
|
115 |
/* enables twi interrupt, automatic ack sending, and all twi hardware */
|
|
116 |
TWCR = (_BV(TWEA) | _BV(TWEN) | _BV(TWIE));
|
|
115 |
/* enables twi interrupt, automatic ack sending, and all twi hardware */ |
|
116 |
TWCR = (_BV(TWEA) | _BV(TWEN) | _BV(TWIE)); |
|
117 | 117 |
|
118 |
/* sets the bit rate of data transmission */
|
|
119 |
TWBR = I2C_BIT_RATE_DIVIDER;
|
|
118 |
/* sets the bit rate of data transmission */ |
|
119 |
TWBR = I2C_BIT_RATE_DIVIDER; |
|
120 | 120 |
|
121 |
/* sets the address (it is stored in the 7 most significant bits) and allows
|
|
122 |
* global messages to be accepted */
|
|
123 |
TWAR = (addr << 1) | 1;
|
|
121 |
/* sets the address (it is stored in the 7 most significant bits) and allows |
|
122 |
* global messages to be accepted */ |
|
123 |
TWAR = (addr << 1) | 1; |
|
124 | 124 |
|
125 |
return 0;
|
|
125 |
return 0; |
|
126 | 126 |
} |
127 | 127 |
|
128 | 128 |
/** |
... | ... | |
131 | 131 |
* Will perform a send over I2C to the destination from data for the ammount of |
132 | 132 |
* bytes that bytes is. |
133 | 133 |
* |
134 |
* @param dest Destination address of the data on the I2C bus.
|
|
135 |
* @param data The pointer to the byte array of data
|
|
136 |
* @param bytes The amount of bytes long that the byte array is. This is how
|
|
137 |
* many bytes from the array that the function will send.
|
|
134 |
* @param dest Destination address of the data on the I2C bus.
|
|
135 |
* @param data The pointer to the byte array of data
|
|
136 |
* @param bytes The amount of bytes long that the byte array is. This is how
|
|
137 |
* many bytes from the array that the function will send.
|
|
138 | 138 |
* |
139 | 139 |
* @return zero for success, nonzero for failure |
140 | 140 |
**/ |
141 | 141 |
int i2c_send(char dest, char *data, size_t bytes) { |
142 |
int i;
|
|
142 |
int i; |
|
143 | 143 |
|
144 |
/* adding data to be sent to ring buffers is not atomic, |
|
145 |
* so disable interrupts */ |
|
146 |
cli(); |
|
147 |
for(i = 0; i < bytes; i++) { |
|
148 |
if(RING_BUFFER_FULL(i2c_write_buff)) { |
|
149 |
sei(); |
|
150 |
return -1; |
|
151 |
} |
|
152 |
|
|
153 |
RING_BUFFER_ADD(i2c_write_buff, data[i]); |
|
154 |
RING_BUFFER_ADD(i2c_addr_buff, dest << 1); |
|
144 |
/* adding data to be sent to ring buffers is not atomic, |
|
145 |
* so disable interrupts */ |
|
146 |
cli(); |
|
147 |
for (i = 0; i < bytes; i++) { |
|
148 |
if (RING_BUFFER_FULL(i2c_write_buff)) { |
|
149 |
sei(); |
|
150 |
return -1; |
|
155 | 151 |
} |
156 | 152 |
|
157 |
/* re-enable the interrupts */ |
|
158 |
sei(); |
|
153 |
RING_BUFFER_ADD(i2c_write_buff, data[i]); |
|
154 |
RING_BUFFER_ADD(i2c_addr_buff, dest << 1); |
|
155 |
} |
|
159 | 156 |
|
160 |
/* send the start bit, only if this device is not currently master */ |
|
161 |
if(!start_flag) { |
|
162 |
start_flag = 1; |
|
163 |
TWCR |= _BV(TWSTA); |
|
164 |
TWCR |= _BV(TWINT); |
|
165 |
} |
|
157 |
/* re-enable the interrupts */ |
|
158 |
sei(); |
|
166 | 159 |
|
167 |
return 0; |
|
160 |
/* send the start bit, only if this device is not currently master */ |
|
161 |
if (!start_flag) { |
|
162 |
start_flag = 1; |
|
163 |
TWCR |= _BV(TWSTA); |
|
164 |
TWCR |= _BV(TWINT); |
|
165 |
} |
|
166 |
|
|
167 |
return 0; |
|
168 | 168 |
} |
169 | 169 |
|
170 | 170 |
/** |
... | ... | |
175 | 175 |
* not work if the slave has not informationt to send or has nothing implemented |
176 | 176 |
* to send it. |
177 | 177 |
* |
178 |
* @param dest The destination that we want to receive information from.
|
|
178 |
* @param dest The destination that we want to receive information from.
|
|
179 | 179 |
* |
180 | 180 |
* @return 0 for success, nonzero for failure |
181 | 181 |
**/ |
182 | 182 |
int i2c_request(char dest) { |
183 |
if(RING_BUFFER_FULL(i2c_write_buff)) |
|
184 |
return -1; |
|
183 |
if (RING_BUFFER_FULL(i2c_write_buff)) { |
|
184 |
return -1; |
|
185 |
} |
|
185 | 186 |
|
186 |
RING_BUFFER_ADD(i2c_write_buff, 0);
|
|
187 |
RING_BUFFER_ADD(i2c_addr_buff, (dest << 1) | 1);
|
|
187 |
RING_BUFFER_ADD(i2c_write_buff, 0); |
|
188 |
RING_BUFFER_ADD(i2c_addr_buff, (dest << 1) | 1); |
|
188 | 189 |
|
189 |
if(!start_flag) {
|
|
190 |
start_flag = 1;
|
|
191 |
TWCR |= _BV(TWSTA);
|
|
192 |
TWCR |= _BV(TWINT);
|
|
193 |
}
|
|
190 |
if (!start_flag) {
|
|
191 |
start_flag = 1; |
|
192 |
TWCR |= _BV(TWSTA); |
|
193 |
TWCR |= _BV(TWINT); |
|
194 |
} |
|
194 | 195 |
|
195 |
return 0;
|
|
196 |
return 0; |
|
196 | 197 |
} |
197 | 198 |
|
198 | 199 |
/** @} **/ |
... | ... | |
212 | 213 |
* Errors are handled here as well. |
213 | 214 |
**/ |
214 | 215 |
ISR(TWI_vect) { |
215 |
static char data_to_send;
|
|
216 |
static char addr_to_send = -1;
|
|
217 |
char addr, statusCode;
|
|
216 |
static char data_to_send;
|
|
217 |
static char addr_to_send = -1;
|
|
218 |
char addr, statusCode;
|
|
218 | 219 |
|
219 |
//Get status code (only upper 5 bits)
|
|
220 |
statusCode = (TWSR & 0xF8);
|
|
220 |
//Get status code (only upper 5 bits)
|
|
221 |
statusCode = (TWSR & 0xF8);
|
|
221 | 222 |
|
222 |
switch (statusCode) {
|
|
223 |
//Start sent successfully
|
|
224 |
case TW_START:
|
|
225 |
case TW_REP_START:
|
|
226 |
/* Send address and write
|
|
227 |
* ring_buffer will not be empty */
|
|
228 |
RING_BUFFER_REMOVE(i2c_addr_buff, addr_to_send);
|
|
229 |
RING_BUFFER_REMOVE(i2c_write_buff, data_to_send);
|
|
223 |
switch (statusCode) { |
|
224 |
//Start sent successfully |
|
225 |
case TW_START: |
|
226 |
case TW_REP_START: |
|
227 |
/* Send address and write |
|
228 |
* ring_buffer will not be empty */ |
|
229 |
RING_BUFFER_REMOVE(i2c_addr_buff, addr_to_send); |
|
230 |
RING_BUFFER_REMOVE(i2c_write_buff, data_to_send); |
|
230 | 231 |
|
231 |
/* first send the address */
|
|
232 |
TWDR = addr_to_send;
|
|
232 |
/* first send the address */ |
|
233 |
TWDR = addr_to_send; |
|
233 | 234 |
|
234 |
//Turn off start bits
|
|
235 |
TWCR &= ~_BV(TWSTA);
|
|
236 |
break;
|
|
235 |
//Turn off start bits |
|
236 |
TWCR &= ~_BV(TWSTA); |
|
237 |
break; |
|
237 | 238 |
|
238 |
//Master Transmit - Address sent succesfully
|
|
239 |
case TW_MT_SLA_ACK:
|
|
240 |
//Send byte
|
|
241 |
TWDR = data_to_send;
|
|
242 |
PORTG &= ~_BV(PG2);
|
|
243 |
break;
|
|
239 |
//Master Transmit - Address sent succesfully |
|
240 |
case TW_MT_SLA_ACK: |
|
241 |
//Send byte |
|
242 |
TWDR = data_to_send; |
|
243 |
PORTG &= ~_BV(PG2); |
|
244 |
break; |
|
244 | 245 |
|
245 |
//Master Transmit - Data sent succesfully
|
|
246 |
case TW_MT_DATA_ACK:
|
|
247 |
//If there is still data to send
|
|
248 |
if(!RING_BUFFER_EMPTY(i2c_write_buff)) {
|
|
249 |
RING_BUFFER_PEEK(i2c_addr_buff, addr);
|
|
246 |
//Master Transmit - Data sent succesfully |
|
247 |
case TW_MT_DATA_ACK: |
|
248 |
//If there is still data to send |
|
249 |
if (!RING_BUFFER_EMPTY(i2c_write_buff)) {
|
|
250 |
RING_BUFFER_PEEK(i2c_addr_buff, addr); |
|
250 | 251 |
|
251 |
//Still data for this address
|
|
252 |
if (addr == addr_to_send) {
|
|
253 |
RING_BUFFER_REMOVE(i2c_addr_buff, addr);
|
|
254 |
RING_BUFFER_REMOVE(i2c_write_buff, TWDR);
|
|
255 |
break;
|
|
256 |
//No more data for this address, data for another address -> resend start
|
|
257 |
} else {
|
|
258 |
TWCR |= _BV(TWSTA);
|
|
259 |
break;
|
|
260 |
}
|
|
261 |
}
|
|
262 |
/* there are no bytes to send */
|
|
263 |
TWCR |= _BV(TWSTO);
|
|
264 |
start_flag = 0;
|
|
265 |
break;
|
|
252 |
//Still data for this address |
|
253 |
if (addr == addr_to_send) { |
|
254 |
RING_BUFFER_REMOVE(i2c_addr_buff, addr); |
|
255 |
RING_BUFFER_REMOVE(i2c_write_buff, TWDR); |
|
256 |
break; |
|
257 |
//No more data for this address, data for another address -> resend start |
|
258 |
} else { |
|
259 |
TWCR |= _BV(TWSTA); |
|
260 |
break; |
|
261 |
} |
|
262 |
} |
|
263 |
/* there are no bytes to send */ |
|
264 |
TWCR |= _BV(TWSTO); |
|
265 |
start_flag = 0; |
|
266 |
break; |
|
266 | 267 |
|
267 |
//Master Transmit - Slave sends a nack, transmit is done
|
|
268 |
case TW_MT_DATA_NACK:
|
|
269 |
PORTG |= _BV(PG2);
|
|
270 |
TWCR |= _BV(TWSTO);
|
|
271 |
start_flag = 0;
|
|
272 |
break;
|
|
268 |
//Master Transmit - Slave sends a nack, transmit is done |
|
269 |
case TW_MT_DATA_NACK: |
|
270 |
PORTG |= _BV(PG2); |
|
271 |
TWCR |= _BV(TWSTO); |
|
272 |
start_flag = 0; |
|
273 |
break; |
|
273 | 274 |
|
274 |
//Master Receive - Address sent succesfully
|
|
275 |
case TW_MR_SLA_ACK:
|
|
276 |
PORTG |= _BV(PG2);
|
|
277 |
break;
|
|
275 |
//Master Receive - Address sent succesfully |
|
276 |
case TW_MR_SLA_ACK: |
|
277 |
PORTG |= _BV(PG2); |
|
278 |
break; |
|
278 | 279 |
|
279 |
//Master Receive - Data received succesfully
|
|
280 |
case TW_MR_DATA_ACK:
|
|
281 |
if(master_recv_function) {
|
|
282 |
if(!master_recv_function(TWDR)) {
|
|
283 |
TWCR &= ~_BV(TWEA);
|
|
284 |
}
|
|
285 |
}
|
|
286 |
break;
|
|
280 |
//Master Receive - Data received succesfully |
|
281 |
case TW_MR_DATA_ACK: |
|
282 |
if (master_recv_function) {
|
|
283 |
if (!master_recv_function(TWDR)) {
|
|
284 |
TWCR &= ~_BV(TWEA); |
|
285 |
} |
|
286 |
} |
|
287 |
break; |
|
287 | 288 |
|
288 |
//Master Receive - Slave sends a nack, transmission is done
|
|
289 |
case TW_MR_DATA_NACK:
|
|
290 |
TWCR |= _BV(TWEA);
|
|
289 |
//Master Receive - Slave sends a nack, transmission is done |
|
290 |
case TW_MR_DATA_NACK: |
|
291 |
TWCR |= _BV(TWEA); |
|
291 | 292 |
|
292 |
//If there is still data to send
|
|
293 |
if(!RING_BUFFER_EMPTY(i2c_write_buff)) {
|
|
294 |
TWCR |= _BV(TWSTA);
|
|
295 |
break;
|
|
296 |
}
|
|
293 |
//If there is still data to send |
|
294 |
if (!RING_BUFFER_EMPTY(i2c_write_buff)) {
|
|
295 |
TWCR |= _BV(TWSTA); |
|
296 |
break; |
|
297 |
} |
|
297 | 298 |
|
298 |
/* there are no bytes to send */
|
|
299 |
TWCR |= _BV(TWSTO);
|
|
300 |
start_flag = 0;
|
|
301 |
break;
|
|
299 |
/* there are no bytes to send */ |
|
300 |
TWCR |= _BV(TWSTO); |
|
301 |
start_flag = 0; |
|
302 |
break; |
|
302 | 303 |
|
303 |
//Slave Transmit - Address received
|
|
304 |
case TW_ST_SLA_ACK:
|
|
305 |
break;
|
|
304 |
//Slave Transmit - Address received |
|
305 |
case TW_ST_SLA_ACK: |
|
306 |
break; |
|
306 | 307 |
|
307 |
//Slave Transmit - Nack received, no data requsted
|
|
308 |
case TW_ST_DATA_NACK:
|
|
309 |
break;
|
|
308 |
//Slave Transmit - Nack received, no data requsted |
|
309 |
case TW_ST_DATA_NACK: |
|
310 |
break; |
|
310 | 311 |
|
311 |
//Slave Transmit - Data requested, ack received
|
|
312 |
case TW_ST_DATA_ACK:
|
|
313 |
if (slave_send_function) {
|
|
314 |
TWDR = slave_send_function();
|
|
315 |
}
|
|
316 |
break;
|
|
312 |
//Slave Transmit - Data requested, ack received |
|
313 |
case TW_ST_DATA_ACK: |
|
314 |
if (slave_send_function) { |
|
315 |
TWDR = slave_send_function(); |
|
316 |
} |
|
317 |
break; |
|
317 | 318 |
|
318 |
//Slave Receive - Address received
|
|
319 |
case TW_SR_SLA_ACK:
|
|
320 |
break;
|
|
319 |
//Slave Receive - Address received |
|
320 |
case TW_SR_SLA_ACK: |
|
321 |
break; |
|
321 | 322 |
|
322 |
//Slave Receive - Data received, ack returned
|
|
323 |
case TW_SR_DATA_ACK:
|
|
324 |
if(slave_recv_function) {
|
|
325 |
slave_recv_function(TWDR);
|
|
326 |
}
|
|
323 |
//Slave Receive - Data received, ack returned |
|
324 |
case TW_SR_DATA_ACK: |
|
325 |
if (slave_recv_function) {
|
|
326 |
slave_recv_function(TWDR); |
|
327 |
} |
|
327 | 328 |
|
328 |
break;
|
|
329 |
break; |
|
329 | 330 |
|
330 |
//Stop sent
|
|
331 |
case TW_SR_STOP:
|
|
332 |
break;
|
|
331 |
//Stop sent |
|
332 |
case TW_SR_STOP: |
|
333 |
break; |
|
333 | 334 |
|
334 |
//Problem on the bus, reset everything
|
|
335 |
default:
|
|
336 |
TWCR |= _BV(TWSTO);
|
|
337 |
start_flag = 0;
|
|
338 |
RING_BUFFER_CLEAR(i2c_write_buff);
|
|
339 |
RING_BUFFER_CLEAR(i2c_addr_buff);
|
|
340 |
}
|
|
335 |
//Problem on the bus, reset everything |
|
336 |
default: |
|
337 |
TWCR |= _BV(TWSTO); |
|
338 |
start_flag = 0; |
|
339 |
RING_BUFFER_CLEAR(i2c_write_buff); |
|
340 |
RING_BUFFER_CLEAR(i2c_addr_buff); |
|
341 |
} |
|
341 | 342 |
|
342 | 343 |
/* Toggle TWINT so that it resets and executes the commands */ |
343 | 344 |
TWCR |= _BV(TWINT); |
344 | 345 |
} |
345 |
|
Also available in: Unified diff