root / forklift / code / twi.c @ 902fa82f
History | View | Annotate | Download (13 KB)
1 | a52b0259 | Tom Mullins | /*
|
---|---|---|---|
2 | twi.c - TWI/I2C library for Wiring & Arduino
|
||
3 | Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
||
4 | |||
5 | This library is free software; you can redistribute it and/or
|
||
6 | modify it under the terms of the GNU Lesser General Public
|
||
7 | License as published by the Free Software Foundation; either
|
||
8 | version 2.1 of the License, or (at your option) any later version.
|
||
9 | |||
10 | This library is distributed in the hope that it will be useful,
|
||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
13 | Lesser General Public License for more details.
|
||
14 | |||
15 | You should have received a copy of the GNU Lesser General Public
|
||
16 | License along with this library; if not, write to the Free Software
|
||
17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||
18 | */
|
||
19 | |||
20 | #include <math.h> |
||
21 | #include <stdlib.h> |
||
22 | #include <inttypes.h> |
||
23 | #include <avr/io.h> |
||
24 | #include <avr/interrupt.h> |
||
25 | #include <compat/twi.h> |
||
26 | |||
27 | #ifndef cbi
|
||
28 | #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
|
||
29 | #endif
|
||
30 | |||
31 | #ifndef sbi
|
||
32 | #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
|
||
33 | #endif
|
||
34 | |||
35 | #include "twi.h" |
||
36 | |||
37 | static volatile uint8_t twi_state; |
||
38 | static uint8_t twi_slarw;
|
||
39 | |||
40 | static void (*twi_onSlaveTransmit)(void); |
||
41 | static void (*twi_onSlaveReceive)(uint8_t*, int); |
||
42 | |||
43 | static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH];
|
||
44 | static volatile uint8_t twi_masterBufferIndex; |
||
45 | static uint8_t twi_masterBufferLength;
|
||
46 | |||
47 | static uint8_t twi_txBuffer[TWI_BUFFER_LENGTH];
|
||
48 | static volatile uint8_t twi_txBufferIndex; |
||
49 | static volatile uint8_t twi_txBufferLength; |
||
50 | |||
51 | static uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH];
|
||
52 | static volatile uint8_t twi_rxBufferIndex; |
||
53 | |||
54 | static volatile uint8_t twi_error; |
||
55 | |||
56 | /*
|
||
57 | * Function twi_init
|
||
58 | * Desc readys twi pins and sets twi bitrate
|
||
59 | * Input none
|
||
60 | * Output none
|
||
61 | */
|
||
62 | void twi_init(void) |
||
63 | { |
||
64 | // initialize state
|
||
65 | twi_state = TWI_READY; |
||
66 | |||
67 | // activate internal pullups for twi.
|
||
68 | //digitalWrite(SDA, 1);
|
||
69 | //digitalWrite(SCL, 1);
|
||
70 | |||
71 | // initialize twi prescaler and bit rate
|
||
72 | cbi(TWSR, TWPS0); |
||
73 | cbi(TWSR, TWPS1); |
||
74 | TWBR = ((F_CPU / TWI_FREQ) - 16) / 2; |
||
75 | |||
76 | /* twi bit rate formula from atmega128 manual pg 204
|
||
77 | SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR))
|
||
78 | note: TWBR should be 10 or higher for master mode
|
||
79 | It is 72 for a 16mhz Wiring board with 100kHz TWI */
|
||
80 | |||
81 | // enable twi module, acks, and twi interrupt
|
||
82 | TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA); |
||
83 | } |
||
84 | |||
85 | /*
|
||
86 | * Function twi_slaveInit
|
||
87 | * Desc sets slave address and enables interrupt
|
||
88 | * Input none
|
||
89 | * Output none
|
||
90 | */
|
||
91 | void twi_setAddress(uint8_t address)
|
||
92 | { |
||
93 | // set twi slave address (skip over TWGCE bit)
|
||
94 | TWAR = address << 1;
|
||
95 | } |
||
96 | |||
97 | /*
|
||
98 | * Function twi_readFrom
|
||
99 | * Desc attempts to become twi bus master and read a
|
||
100 | * series of bytes from a device on the bus
|
||
101 | * Input address: 7bit i2c device address
|
||
102 | * data: pointer to byte array
|
||
103 | * length: number of bytes to read into array
|
||
104 | * Output number of bytes read
|
||
105 | */
|
||
106 | uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length) |
||
107 | { |
||
108 | uint8_t i; |
||
109 | |||
110 | // ensure data will fit into buffer
|
||
111 | if(TWI_BUFFER_LENGTH < length){
|
||
112 | return 0; |
||
113 | } |
||
114 | |||
115 | // wait until twi is ready, become master receiver
|
||
116 | while(TWI_READY != twi_state){
|
||
117 | continue;
|
||
118 | } |
||
119 | twi_state = TWI_MRX; |
||
120 | // reset error state (0xFF.. no error occured)
|
||
121 | twi_error = 0xFF;
|
||
122 | |||
123 | // initialize buffer iteration vars
|
||
124 | twi_masterBufferIndex = 0;
|
||
125 | twi_masterBufferLength = length-1; // This is not intuitive, read on... |
||
126 | // On receive, the previously configured ACK/NACK setting is transmitted in
|
||
127 | // response to the received byte before the interrupt is signalled.
|
||
128 | // Therefor we must actually set NACK when the _next_ to last byte is
|
||
129 | // received, causing that NACK to be sent in response to receiving the last
|
||
130 | // expected byte of data.
|
||
131 | |||
132 | // build sla+w, slave device address + w bit
|
||
133 | twi_slarw = TW_READ; |
||
134 | twi_slarw |= address << 1;
|
||
135 | |||
136 | // send start condition
|
||
137 | TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); |
||
138 | |||
139 | // wait for read operation to complete
|
||
140 | while(TWI_MRX == twi_state){
|
||
141 | continue;
|
||
142 | } |
||
143 | |||
144 | if (twi_masterBufferIndex < length)
|
||
145 | length = twi_masterBufferIndex; |
||
146 | |||
147 | // copy twi buffer to data
|
||
148 | for(i = 0; i < length; ++i){ |
||
149 | data[i] = twi_masterBuffer[i]; |
||
150 | } |
||
151 | |||
152 | return length;
|
||
153 | } |
||
154 | |||
155 | /*
|
||
156 | * Function twi_writeTo
|
||
157 | * Desc attempts to become twi bus master and write a
|
||
158 | * series of bytes to a device on the bus
|
||
159 | * Input address: 7bit i2c device address
|
||
160 | * data: pointer to byte array
|
||
161 | * length: number of bytes in array
|
||
162 | * wait: boolean indicating to wait for write or not
|
||
163 | * Output 0 .. success
|
||
164 | * 1 .. length to long for buffer
|
||
165 | * 2 .. address send, NACK received
|
||
166 | * 3 .. data send, NACK received
|
||
167 | * 4 .. other twi error (lost bus arbitration, bus error, ..)
|
||
168 | */
|
||
169 | uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait) |
||
170 | { |
||
171 | uint8_t i; |
||
172 | |||
173 | // ensure data will fit into buffer
|
||
174 | if(TWI_BUFFER_LENGTH < length){
|
||
175 | return 1; |
||
176 | } |
||
177 | |||
178 | // wait until twi is ready, become master transmitter
|
||
179 | while(TWI_READY != twi_state){
|
||
180 | continue;
|
||
181 | } |
||
182 | twi_state = TWI_MTX; |
||
183 | // reset error state (0xFF.. no error occured)
|
||
184 | twi_error = 0xFF;
|
||
185 | |||
186 | // initialize buffer iteration vars
|
||
187 | twi_masterBufferIndex = 0;
|
||
188 | twi_masterBufferLength = length; |
||
189 | |||
190 | // copy data to twi buffer
|
||
191 | for(i = 0; i < length; ++i){ |
||
192 | twi_masterBuffer[i] = data[i]; |
||
193 | } |
||
194 | |||
195 | // build sla+w, slave device address + w bit
|
||
196 | twi_slarw = TW_WRITE; |
||
197 | twi_slarw |= address << 1;
|
||
198 | |||
199 | // send start condition
|
||
200 | TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); |
||
201 | |||
202 | // wait for write operation to complete
|
||
203 | while(wait && (TWI_MTX == twi_state)){
|
||
204 | continue;
|
||
205 | } |
||
206 | |||
207 | if (twi_error == 0xFF) |
||
208 | return 0; // success |
||
209 | else if (twi_error == TW_MT_SLA_NACK) |
||
210 | return 2; // error: address send, nack received |
||
211 | else if (twi_error == TW_MT_DATA_NACK) |
||
212 | return 3; // error: data send, nack received |
||
213 | else
|
||
214 | return 4; // other twi error |
||
215 | } |
||
216 | |||
217 | /*
|
||
218 | * Function twi_transmit
|
||
219 | * Desc fills slave tx buffer with data
|
||
220 | * must be called in slave tx event callback
|
||
221 | * Input data: pointer to byte array
|
||
222 | * length: number of bytes in array
|
||
223 | * Output 1 length too long for buffer
|
||
224 | * 2 not slave transmitter
|
||
225 | * 0 ok
|
||
226 | */
|
||
227 | uint8_t twi_transmit(const uint8_t* data, uint8_t length)
|
||
228 | { |
||
229 | uint8_t i; |
||
230 | |||
231 | // ensure data will fit into buffer
|
||
232 | if(TWI_BUFFER_LENGTH < length){
|
||
233 | return 1; |
||
234 | } |
||
235 | |||
236 | // ensure we are currently a slave transmitter
|
||
237 | if(TWI_STX != twi_state){
|
||
238 | return 2; |
||
239 | } |
||
240 | |||
241 | // set length and copy data into tx buffer
|
||
242 | twi_txBufferLength = length; |
||
243 | for(i = 0; i < length; ++i){ |
||
244 | twi_txBuffer[i] = data[i]; |
||
245 | } |
||
246 | |||
247 | return 0; |
||
248 | } |
||
249 | |||
250 | /*
|
||
251 | * Function twi_attachSlaveRxEvent
|
||
252 | * Desc sets function called before a slave read operation
|
||
253 | * Input function: callback function to use
|
||
254 | * Output none
|
||
255 | */
|
||
256 | void twi_attachSlaveRxEvent( void (*function)(uint8_t*, int) ) |
||
257 | { |
||
258 | twi_onSlaveReceive = function; |
||
259 | } |
||
260 | |||
261 | /*
|
||
262 | * Function twi_attachSlaveTxEvent
|
||
263 | * Desc sets function called before a slave write operation
|
||
264 | * Input function: callback function to use
|
||
265 | * Output none
|
||
266 | */
|
||
267 | void twi_attachSlaveTxEvent( void (*function)(void) ) |
||
268 | { |
||
269 | twi_onSlaveTransmit = function; |
||
270 | } |
||
271 | |||
272 | /*
|
||
273 | * Function twi_reply
|
||
274 | * Desc sends byte or readys receive line
|
||
275 | * Input ack: byte indicating to ack or to nack
|
||
276 | * Output none
|
||
277 | */
|
||
278 | void twi_reply(uint8_t ack)
|
||
279 | { |
||
280 | // transmit master read ready signal, with or without ack
|
||
281 | if(ack){
|
||
282 | TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA); |
||
283 | }else{
|
||
284 | TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT); |
||
285 | } |
||
286 | } |
||
287 | |||
288 | /*
|
||
289 | * Function twi_stop
|
||
290 | * Desc relinquishes bus master status
|
||
291 | * Input none
|
||
292 | * Output none
|
||
293 | */
|
||
294 | void twi_stop(void) |
||
295 | { |
||
296 | // send stop condition
|
||
297 | TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO); |
||
298 | |||
299 | // wait for stop condition to be exectued on bus
|
||
300 | // TWINT is not set after a stop condition!
|
||
301 | while(TWCR & _BV(TWSTO)){
|
||
302 | continue;
|
||
303 | } |
||
304 | |||
305 | // update twi state
|
||
306 | twi_state = TWI_READY; |
||
307 | } |
||
308 | |||
309 | /*
|
||
310 | * Function twi_releaseBus
|
||
311 | * Desc releases bus control
|
||
312 | * Input none
|
||
313 | * Output none
|
||
314 | */
|
||
315 | void twi_releaseBus(void) |
||
316 | { |
||
317 | // release bus
|
||
318 | TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT); |
||
319 | |||
320 | // update twi state
|
||
321 | twi_state = TWI_READY; |
||
322 | } |
||
323 | |||
324 | SIGNAL(TWI_vect) |
||
325 | { |
||
326 | switch(TW_STATUS){
|
||
327 | // All Master
|
||
328 | case TW_START: // sent start condition |
||
329 | case TW_REP_START: // sent repeated start condition |
||
330 | // copy device address and r/w bit to output register and ack
|
||
331 | TWDR = twi_slarw; |
||
332 | twi_reply(1);
|
||
333 | break;
|
||
334 | |||
335 | // Master Transmitter
|
||
336 | case TW_MT_SLA_ACK: // slave receiver acked address |
||
337 | case TW_MT_DATA_ACK: // slave receiver acked data |
||
338 | // if there is data to send, send it, otherwise stop
|
||
339 | if(twi_masterBufferIndex < twi_masterBufferLength){
|
||
340 | // copy data to output register and ack
|
||
341 | TWDR = twi_masterBuffer[twi_masterBufferIndex++]; |
||
342 | twi_reply(1);
|
||
343 | }else{
|
||
344 | twi_stop(); |
||
345 | } |
||
346 | break;
|
||
347 | case TW_MT_SLA_NACK: // address sent, nack received |
||
348 | twi_error = TW_MT_SLA_NACK; |
||
349 | twi_stop(); |
||
350 | break;
|
||
351 | case TW_MT_DATA_NACK: // data sent, nack received |
||
352 | twi_error = TW_MT_DATA_NACK; |
||
353 | twi_stop(); |
||
354 | break;
|
||
355 | case TW_MT_ARB_LOST: // lost bus arbitration |
||
356 | twi_error = TW_MT_ARB_LOST; |
||
357 | twi_releaseBus(); |
||
358 | break;
|
||
359 | |||
360 | // Master Receiver
|
||
361 | case TW_MR_DATA_ACK: // data received, ack sent |
||
362 | // put byte into buffer
|
||
363 | twi_masterBuffer[twi_masterBufferIndex++] = TWDR; |
||
364 | case TW_MR_SLA_ACK: // address sent, ack received |
||
365 | // ack if more bytes are expected, otherwise nack
|
||
366 | if(twi_masterBufferIndex < twi_masterBufferLength){
|
||
367 | twi_reply(1);
|
||
368 | }else{
|
||
369 | twi_reply(0);
|
||
370 | } |
||
371 | break;
|
||
372 | case TW_MR_DATA_NACK: // data received, nack sent |
||
373 | // put final byte into buffer
|
||
374 | twi_masterBuffer[twi_masterBufferIndex++] = TWDR; |
||
375 | case TW_MR_SLA_NACK: // address sent, nack received |
||
376 | twi_stop(); |
||
377 | break;
|
||
378 | // TW_MR_ARB_LOST handled by TW_MT_ARB_LOST case
|
||
379 | |||
380 | // Slave Receiver
|
||
381 | case TW_SR_SLA_ACK: // addressed, returned ack |
||
382 | case TW_SR_GCALL_ACK: // addressed generally, returned ack |
||
383 | case TW_SR_ARB_LOST_SLA_ACK: // lost arbitration, returned ack |
||
384 | case TW_SR_ARB_LOST_GCALL_ACK: // lost arbitration, returned ack |
||
385 | // enter slave receiver mode
|
||
386 | twi_state = TWI_SRX; |
||
387 | // indicate that rx buffer can be overwritten and ack
|
||
388 | twi_rxBufferIndex = 0;
|
||
389 | twi_reply(1);
|
||
390 | break;
|
||
391 | case TW_SR_DATA_ACK: // data received, returned ack |
||
392 | case TW_SR_GCALL_DATA_ACK: // data received generally, returned ack |
||
393 | // if there is still room in the rx buffer
|
||
394 | if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){
|
||
395 | // put byte in buffer and ack
|
||
396 | twi_rxBuffer[twi_rxBufferIndex++] = TWDR; |
||
397 | twi_reply(1);
|
||
398 | }else{
|
||
399 | // otherwise nack
|
||
400 | twi_reply(0);
|
||
401 | } |
||
402 | break;
|
||
403 | case TW_SR_STOP: // stop or repeated start condition received |
||
404 | // put a null char after data if there's room
|
||
405 | if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){
|
||
406 | twi_rxBuffer[twi_rxBufferIndex] = '\0';
|
||
407 | } |
||
408 | // sends ack and stops interface for clock stretching
|
||
409 | twi_stop(); |
||
410 | // callback to user defined callback
|
||
411 | twi_onSlaveReceive(twi_rxBuffer, twi_rxBufferIndex); |
||
412 | // since we submit rx buffer to "wire" library, we can reset it
|
||
413 | twi_rxBufferIndex = 0;
|
||
414 | // ack future responses and leave slave receiver state
|
||
415 | twi_releaseBus(); |
||
416 | break;
|
||
417 | case TW_SR_DATA_NACK: // data received, returned nack |
||
418 | case TW_SR_GCALL_DATA_NACK: // data received generally, returned nack |
||
419 | // nack back at master
|
||
420 | twi_reply(0);
|
||
421 | break;
|
||
422 | |||
423 | // Slave Transmitter
|
||
424 | case TW_ST_SLA_ACK: // addressed, returned ack |
||
425 | case TW_ST_ARB_LOST_SLA_ACK: // arbitration lost, returned ack |
||
426 | // enter slave transmitter mode
|
||
427 | twi_state = TWI_STX; |
||
428 | // ready the tx buffer index for iteration
|
||
429 | twi_txBufferIndex = 0;
|
||
430 | // set tx buffer length to be zero, to verify if user changes it
|
||
431 | twi_txBufferLength = 0;
|
||
432 | // request for txBuffer to be filled and length to be set
|
||
433 | // note: user must call twi_transmit(bytes, length) to do this
|
||
434 | twi_onSlaveTransmit(); |
||
435 | // if they didn't change buffer & length, initialize it
|
||
436 | if(0 == twi_txBufferLength){ |
||
437 | twi_txBufferLength = 1;
|
||
438 | twi_txBuffer[0] = 0x00; |
||
439 | } |
||
440 | // transmit first byte from buffer, fall
|
||
441 | case TW_ST_DATA_ACK: // byte sent, ack returned |
||
442 | // copy data to output register
|
||
443 | TWDR = twi_txBuffer[twi_txBufferIndex++]; |
||
444 | // if there is more to send, ack, otherwise nack
|
||
445 | if(twi_txBufferIndex < twi_txBufferLength){
|
||
446 | twi_reply(1);
|
||
447 | }else{
|
||
448 | twi_reply(0);
|
||
449 | } |
||
450 | break;
|
||
451 | case TW_ST_DATA_NACK: // received nack, we are done |
||
452 | case TW_ST_LAST_DATA: // received ack, but we are done already! |
||
453 | // ack future responses
|
||
454 | twi_reply(1);
|
||
455 | // leave slave receiver state
|
||
456 | twi_state = TWI_READY; |
||
457 | break;
|
||
458 | |||
459 | // All
|
||
460 | case TW_NO_INFO: // no state information |
||
461 | break;
|
||
462 | case TW_BUS_ERROR: // bus error, illegal stop/start |
||
463 | twi_error = TW_BUS_ERROR; |
||
464 | twi_stop(); |
||
465 | break;
|
||
466 | } |
||
467 | beea48aa | Tom Mullins | } |