|
@@ -0,0 +1,527 @@
|
|
1
|
+/*
|
|
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
|
+ Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
|
|
20
|
+*/
|
|
21
|
+
|
|
22
|
+#include <math.h>
|
|
23
|
+#include <stdlib.h>
|
|
24
|
+#include <inttypes.h>
|
|
25
|
+#include <avr/io.h>
|
|
26
|
+#include <avr/interrupt.h>
|
|
27
|
+#include <compat/twi.h>
|
|
28
|
+#include "Arduino.h" // for digitalWrite
|
|
29
|
+
|
|
30
|
+#ifndef cbi
|
|
31
|
+#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
|
|
32
|
+#endif
|
|
33
|
+
|
|
34
|
+#ifndef sbi
|
|
35
|
+#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
|
|
36
|
+#endif
|
|
37
|
+
|
|
38
|
+#include "pins_arduino.h"
|
|
39
|
+#include "twi.h"
|
|
40
|
+
|
|
41
|
+static volatile uint8_t twi_state;
|
|
42
|
+static volatile uint8_t twi_slarw;
|
|
43
|
+static volatile uint8_t twi_sendStop; // should the transaction end with a stop
|
|
44
|
+static volatile uint8_t twi_inRepStart; // in the middle of a repeated start
|
|
45
|
+
|
|
46
|
+static void (*twi_onSlaveTransmit)(void);
|
|
47
|
+static void (*twi_onSlaveReceive)(uint8_t*, int);
|
|
48
|
+
|
|
49
|
+static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH];
|
|
50
|
+static volatile uint8_t twi_masterBufferIndex;
|
|
51
|
+static volatile uint8_t twi_masterBufferLength;
|
|
52
|
+
|
|
53
|
+static uint8_t twi_txBuffer[TWI_BUFFER_LENGTH];
|
|
54
|
+static volatile uint8_t twi_txBufferIndex;
|
|
55
|
+static volatile uint8_t twi_txBufferLength;
|
|
56
|
+
|
|
57
|
+static uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH];
|
|
58
|
+static volatile uint8_t twi_rxBufferIndex;
|
|
59
|
+
|
|
60
|
+static volatile uint8_t twi_error;
|
|
61
|
+
|
|
62
|
+/*
|
|
63
|
+ * Function twi_init
|
|
64
|
+ * Desc readys twi pins and sets twi bitrate
|
|
65
|
+ * Input none
|
|
66
|
+ * Output none
|
|
67
|
+ */
|
|
68
|
+void twi_init(void)
|
|
69
|
+{
|
|
70
|
+ // initialize state
|
|
71
|
+ twi_state = TWI_READY;
|
|
72
|
+ twi_sendStop = true; // default value
|
|
73
|
+ twi_inRepStart = false;
|
|
74
|
+
|
|
75
|
+ // activate internal pullups for twi.
|
|
76
|
+ digitalWrite(SDA, 1);
|
|
77
|
+ digitalWrite(SCL, 1);
|
|
78
|
+
|
|
79
|
+ // initialize twi prescaler and bit rate
|
|
80
|
+ cbi(TWSR, TWPS0);
|
|
81
|
+ cbi(TWSR, TWPS1);
|
|
82
|
+ TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;
|
|
83
|
+
|
|
84
|
+ /* twi bit rate formula from atmega128 manual pg 204
|
|
85
|
+ SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR))
|
|
86
|
+ note: TWBR should be 10 or higher for master mode
|
|
87
|
+ It is 72 for a 16mhz Wiring board with 100kHz TWI */
|
|
88
|
+
|
|
89
|
+ // enable twi module, acks, and twi interrupt
|
|
90
|
+ TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA);
|
|
91
|
+}
|
|
92
|
+
|
|
93
|
+/*
|
|
94
|
+ * Function twi_slaveInit
|
|
95
|
+ * Desc sets slave address and enables interrupt
|
|
96
|
+ * Input none
|
|
97
|
+ * Output none
|
|
98
|
+ */
|
|
99
|
+void twi_setAddress(uint8_t address)
|
|
100
|
+{
|
|
101
|
+ // set twi slave address (skip over TWGCE bit)
|
|
102
|
+ TWAR = address << 1;
|
|
103
|
+}
|
|
104
|
+
|
|
105
|
+/*
|
|
106
|
+ * Function twi_readFrom
|
|
107
|
+ * Desc attempts to become twi bus master and read a
|
|
108
|
+ * series of bytes from a device on the bus
|
|
109
|
+ * Input address: 7bit i2c device address
|
|
110
|
+ * data: pointer to byte array
|
|
111
|
+ * length: number of bytes to read into array
|
|
112
|
+ * sendStop: Boolean indicating whether to send a stop at the end
|
|
113
|
+ * Output number of bytes read
|
|
114
|
+ */
|
|
115
|
+uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sendStop)
|
|
116
|
+{
|
|
117
|
+ uint8_t i;
|
|
118
|
+
|
|
119
|
+ // ensure data will fit into buffer
|
|
120
|
+ if(TWI_BUFFER_LENGTH < length){
|
|
121
|
+ return 0;
|
|
122
|
+ }
|
|
123
|
+
|
|
124
|
+ // wait until twi is ready, become master receiver
|
|
125
|
+ while(TWI_READY != twi_state){
|
|
126
|
+ continue;
|
|
127
|
+ }
|
|
128
|
+ twi_state = TWI_MRX;
|
|
129
|
+ twi_sendStop = sendStop;
|
|
130
|
+ // reset error state (0xFF.. no error occured)
|
|
131
|
+ twi_error = 0xFF;
|
|
132
|
+
|
|
133
|
+ // initialize buffer iteration vars
|
|
134
|
+ twi_masterBufferIndex = 0;
|
|
135
|
+ twi_masterBufferLength = length-1; // This is not intuitive, read on...
|
|
136
|
+ // On receive, the previously configured ACK/NACK setting is transmitted in
|
|
137
|
+ // response to the received byte before the interrupt is signalled.
|
|
138
|
+ // Therefor we must actually set NACK when the _next_ to last byte is
|
|
139
|
+ // received, causing that NACK to be sent in response to receiving the last
|
|
140
|
+ // expected byte of data.
|
|
141
|
+
|
|
142
|
+ // build sla+w, slave device address + w bit
|
|
143
|
+ twi_slarw = TW_READ;
|
|
144
|
+ twi_slarw |= address << 1;
|
|
145
|
+
|
|
146
|
+ if (true == twi_inRepStart) {
|
|
147
|
+ // if we're in the repeated start state, then we've already sent the start,
|
|
148
|
+ // (@@@ we hope), and the TWI statemachine is just waiting for the address byte.
|
|
149
|
+ // We need to remove ourselves from the repeated start state before we enable interrupts,
|
|
150
|
+ // since the ISR is ASYNC, and we could get confused if we hit the ISR before cleaning
|
|
151
|
+ // up. Also, don't enable the START interrupt. There may be one pending from the
|
|
152
|
+ // repeated start that we sent outselves, and that would really confuse things.
|
|
153
|
+ twi_inRepStart = false; // remember, we're dealing with an ASYNC ISR
|
|
154
|
+ TWDR = twi_slarw;
|
|
155
|
+ TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE); // enable INTs, but not START
|
|
156
|
+ }
|
|
157
|
+ else
|
|
158
|
+ // send start condition
|
|
159
|
+ TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
|
|
160
|
+
|
|
161
|
+ // wait for read operation to complete
|
|
162
|
+ while(TWI_MRX == twi_state){
|
|
163
|
+ continue;
|
|
164
|
+ }
|
|
165
|
+
|
|
166
|
+ if (twi_masterBufferIndex < length)
|
|
167
|
+ length = twi_masterBufferIndex;
|
|
168
|
+
|
|
169
|
+ // copy twi buffer to data
|
|
170
|
+ for(i = 0; i < length; ++i){
|
|
171
|
+ data[i] = twi_masterBuffer[i];
|
|
172
|
+ }
|
|
173
|
+
|
|
174
|
+ return length;
|
|
175
|
+}
|
|
176
|
+
|
|
177
|
+/*
|
|
178
|
+ * Function twi_writeTo
|
|
179
|
+ * Desc attempts to become twi bus master and write a
|
|
180
|
+ * series of bytes to a device on the bus
|
|
181
|
+ * Input address: 7bit i2c device address
|
|
182
|
+ * data: pointer to byte array
|
|
183
|
+ * length: number of bytes in array
|
|
184
|
+ * wait: boolean indicating to wait for write or not
|
|
185
|
+ * sendStop: boolean indicating whether or not to send a stop at the end
|
|
186
|
+ * Output 0 .. success
|
|
187
|
+ * 1 .. length to long for buffer
|
|
188
|
+ * 2 .. address send, NACK received
|
|
189
|
+ * 3 .. data send, NACK received
|
|
190
|
+ * 4 .. other twi error (lost bus arbitration, bus error, ..)
|
|
191
|
+ */
|
|
192
|
+uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait, uint8_t sendStop)
|
|
193
|
+{
|
|
194
|
+ uint8_t i;
|
|
195
|
+
|
|
196
|
+ // ensure data will fit into buffer
|
|
197
|
+ if(TWI_BUFFER_LENGTH < length){
|
|
198
|
+ return 1;
|
|
199
|
+ }
|
|
200
|
+
|
|
201
|
+ // wait until twi is ready, become master transmitter
|
|
202
|
+ while(TWI_READY != twi_state){
|
|
203
|
+ continue;
|
|
204
|
+ }
|
|
205
|
+ twi_state = TWI_MTX;
|
|
206
|
+ twi_sendStop = sendStop;
|
|
207
|
+ // reset error state (0xFF.. no error occured)
|
|
208
|
+ twi_error = 0xFF;
|
|
209
|
+
|
|
210
|
+ // initialize buffer iteration vars
|
|
211
|
+ twi_masterBufferIndex = 0;
|
|
212
|
+ twi_masterBufferLength = length;
|
|
213
|
+
|
|
214
|
+ // copy data to twi buffer
|
|
215
|
+ for(i = 0; i < length; ++i){
|
|
216
|
+ twi_masterBuffer[i] = data[i];
|
|
217
|
+ }
|
|
218
|
+
|
|
219
|
+ // build sla+w, slave device address + w bit
|
|
220
|
+ twi_slarw = TW_WRITE;
|
|
221
|
+ twi_slarw |= address << 1;
|
|
222
|
+
|
|
223
|
+ // if we're in a repeated start, then we've already sent the START
|
|
224
|
+ // in the ISR. Don't do it again.
|
|
225
|
+ //
|
|
226
|
+ if (true == twi_inRepStart) {
|
|
227
|
+ // if we're in the repeated start state, then we've already sent the start,
|
|
228
|
+ // (@@@ we hope), and the TWI statemachine is just waiting for the address byte.
|
|
229
|
+ // We need to remove ourselves from the repeated start state before we enable interrupts,
|
|
230
|
+ // since the ISR is ASYNC, and we could get confused if we hit the ISR before cleaning
|
|
231
|
+ // up. Also, don't enable the START interrupt. There may be one pending from the
|
|
232
|
+ // repeated start that we sent outselves, and that would really confuse things.
|
|
233
|
+ twi_inRepStart = false; // remember, we're dealing with an ASYNC ISR
|
|
234
|
+ TWDR = twi_slarw;
|
|
235
|
+ TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE); // enable INTs, but not START
|
|
236
|
+ }
|
|
237
|
+ else
|
|
238
|
+ // send start condition
|
|
239
|
+ TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE) | _BV(TWSTA); // enable INTs
|
|
240
|
+
|
|
241
|
+ // wait for write operation to complete
|
|
242
|
+ while(wait && (TWI_MTX == twi_state)){
|
|
243
|
+ continue;
|
|
244
|
+ }
|
|
245
|
+
|
|
246
|
+ if (twi_error == 0xFF)
|
|
247
|
+ return 0; // success
|
|
248
|
+ else if (twi_error == TW_MT_SLA_NACK)
|
|
249
|
+ return 2; // error: address send, nack received
|
|
250
|
+ else if (twi_error == TW_MT_DATA_NACK)
|
|
251
|
+ return 3; // error: data send, nack received
|
|
252
|
+ else
|
|
253
|
+ return 4; // other twi error
|
|
254
|
+}
|
|
255
|
+
|
|
256
|
+/*
|
|
257
|
+ * Function twi_transmit
|
|
258
|
+ * Desc fills slave tx buffer with data
|
|
259
|
+ * must be called in slave tx event callback
|
|
260
|
+ * Input data: pointer to byte array
|
|
261
|
+ * length: number of bytes in array
|
|
262
|
+ * Output 1 length too long for buffer
|
|
263
|
+ * 2 not slave transmitter
|
|
264
|
+ * 0 ok
|
|
265
|
+ */
|
|
266
|
+uint8_t twi_transmit(const uint8_t* data, uint8_t length)
|
|
267
|
+{
|
|
268
|
+ uint8_t i;
|
|
269
|
+
|
|
270
|
+ // ensure data will fit into buffer
|
|
271
|
+ if(TWI_BUFFER_LENGTH < length){
|
|
272
|
+ return 1;
|
|
273
|
+ }
|
|
274
|
+
|
|
275
|
+ // ensure we are currently a slave transmitter
|
|
276
|
+ if(TWI_STX != twi_state){
|
|
277
|
+ return 2;
|
|
278
|
+ }
|
|
279
|
+
|
|
280
|
+ // set length and copy data into tx buffer
|
|
281
|
+ twi_txBufferLength = length;
|
|
282
|
+ for(i = 0; i < length; ++i){
|
|
283
|
+ twi_txBuffer[i] = data[i];
|
|
284
|
+ }
|
|
285
|
+
|
|
286
|
+ return 0;
|
|
287
|
+}
|
|
288
|
+
|
|
289
|
+/*
|
|
290
|
+ * Function twi_attachSlaveRxEvent
|
|
291
|
+ * Desc sets function called before a slave read operation
|
|
292
|
+ * Input function: callback function to use
|
|
293
|
+ * Output none
|
|
294
|
+ */
|
|
295
|
+void twi_attachSlaveRxEvent( void (*function)(uint8_t*, int) )
|
|
296
|
+{
|
|
297
|
+ twi_onSlaveReceive = function;
|
|
298
|
+}
|
|
299
|
+
|
|
300
|
+/*
|
|
301
|
+ * Function twi_attachSlaveTxEvent
|
|
302
|
+ * Desc sets function called before a slave write operation
|
|
303
|
+ * Input function: callback function to use
|
|
304
|
+ * Output none
|
|
305
|
+ */
|
|
306
|
+void twi_attachSlaveTxEvent( void (*function)(void) )
|
|
307
|
+{
|
|
308
|
+ twi_onSlaveTransmit = function;
|
|
309
|
+}
|
|
310
|
+
|
|
311
|
+/*
|
|
312
|
+ * Function twi_reply
|
|
313
|
+ * Desc sends byte or readys receive line
|
|
314
|
+ * Input ack: byte indicating to ack or to nack
|
|
315
|
+ * Output none
|
|
316
|
+ */
|
|
317
|
+void twi_reply(uint8_t ack)
|
|
318
|
+{
|
|
319
|
+ // transmit master read ready signal, with or without ack
|
|
320
|
+ if(ack){
|
|
321
|
+ TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA);
|
|
322
|
+ }else{
|
|
323
|
+ TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT);
|
|
324
|
+ }
|
|
325
|
+}
|
|
326
|
+
|
|
327
|
+/*
|
|
328
|
+ * Function twi_stop
|
|
329
|
+ * Desc relinquishes bus master status
|
|
330
|
+ * Input none
|
|
331
|
+ * Output none
|
|
332
|
+ */
|
|
333
|
+void twi_stop(void)
|
|
334
|
+{
|
|
335
|
+ // send stop condition
|
|
336
|
+ TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO);
|
|
337
|
+
|
|
338
|
+ // wait for stop condition to be exectued on bus
|
|
339
|
+ // TWINT is not set after a stop condition!
|
|
340
|
+ while(TWCR & _BV(TWSTO)){
|
|
341
|
+ continue;
|
|
342
|
+ }
|
|
343
|
+
|
|
344
|
+ // update twi state
|
|
345
|
+ twi_state = TWI_READY;
|
|
346
|
+}
|
|
347
|
+
|
|
348
|
+/*
|
|
349
|
+ * Function twi_releaseBus
|
|
350
|
+ * Desc releases bus control
|
|
351
|
+ * Input none
|
|
352
|
+ * Output none
|
|
353
|
+ */
|
|
354
|
+void twi_releaseBus(void)
|
|
355
|
+{
|
|
356
|
+ // release bus
|
|
357
|
+ TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT);
|
|
358
|
+
|
|
359
|
+ // update twi state
|
|
360
|
+ twi_state = TWI_READY;
|
|
361
|
+}
|
|
362
|
+
|
|
363
|
+ISR(TWI_vect)
|
|
364
|
+{
|
|
365
|
+ switch(TW_STATUS){
|
|
366
|
+ // All Master
|
|
367
|
+ case TW_START: // sent start condition
|
|
368
|
+ case TW_REP_START: // sent repeated start condition
|
|
369
|
+ // copy device address and r/w bit to output register and ack
|
|
370
|
+ TWDR = twi_slarw;
|
|
371
|
+ twi_reply(1);
|
|
372
|
+ break;
|
|
373
|
+
|
|
374
|
+ // Master Transmitter
|
|
375
|
+ case TW_MT_SLA_ACK: // slave receiver acked address
|
|
376
|
+ case TW_MT_DATA_ACK: // slave receiver acked data
|
|
377
|
+ // if there is data to send, send it, otherwise stop
|
|
378
|
+ if(twi_masterBufferIndex < twi_masterBufferLength){
|
|
379
|
+ // copy data to output register and ack
|
|
380
|
+ TWDR = twi_masterBuffer[twi_masterBufferIndex++];
|
|
381
|
+ twi_reply(1);
|
|
382
|
+ }else{
|
|
383
|
+ if (twi_sendStop)
|
|
384
|
+ twi_stop();
|
|
385
|
+ else {
|
|
386
|
+ twi_inRepStart = true; // we're gonna send the START
|
|
387
|
+ // don't enable the interrupt. We'll generate the start, but we
|
|
388
|
+ // avoid handling the interrupt until we're in the next transaction,
|
|
389
|
+ // at the point where we would normally issue the start.
|
|
390
|
+ TWCR = _BV(TWINT) | _BV(TWSTA)| _BV(TWEN) ;
|
|
391
|
+ twi_state = TWI_READY;
|
|
392
|
+ }
|
|
393
|
+ }
|
|
394
|
+ break;
|
|
395
|
+ case TW_MT_SLA_NACK: // address sent, nack received
|
|
396
|
+ twi_error = TW_MT_SLA_NACK;
|
|
397
|
+ twi_stop();
|
|
398
|
+ break;
|
|
399
|
+ case TW_MT_DATA_NACK: // data sent, nack received
|
|
400
|
+ twi_error = TW_MT_DATA_NACK;
|
|
401
|
+ twi_stop();
|
|
402
|
+ break;
|
|
403
|
+ case TW_MT_ARB_LOST: // lost bus arbitration
|
|
404
|
+ twi_error = TW_MT_ARB_LOST;
|
|
405
|
+ twi_releaseBus();
|
|
406
|
+ break;
|
|
407
|
+
|
|
408
|
+ // Master Receiver
|
|
409
|
+ case TW_MR_DATA_ACK: // data received, ack sent
|
|
410
|
+ // put byte into buffer
|
|
411
|
+ twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
|
|
412
|
+ case TW_MR_SLA_ACK: // address sent, ack received
|
|
413
|
+ // ack if more bytes are expected, otherwise nack
|
|
414
|
+ if(twi_masterBufferIndex < twi_masterBufferLength){
|
|
415
|
+ twi_reply(1);
|
|
416
|
+ }else{
|
|
417
|
+ twi_reply(0);
|
|
418
|
+ }
|
|
419
|
+ break;
|
|
420
|
+ case TW_MR_DATA_NACK: // data received, nack sent
|
|
421
|
+ // put final byte into buffer
|
|
422
|
+ twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
|
|
423
|
+ if (twi_sendStop)
|
|
424
|
+ twi_stop();
|
|
425
|
+ else {
|
|
426
|
+ twi_inRepStart = true; // we're gonna send the START
|
|
427
|
+ // don't enable the interrupt. We'll generate the start, but we
|
|
428
|
+ // avoid handling the interrupt until we're in the next transaction,
|
|
429
|
+ // at the point where we would normally issue the start.
|
|
430
|
+ TWCR = _BV(TWINT) | _BV(TWSTA)| _BV(TWEN) ;
|
|
431
|
+ twi_state = TWI_READY;
|
|
432
|
+ }
|
|
433
|
+ break;
|
|
434
|
+ case TW_MR_SLA_NACK: // address sent, nack received
|
|
435
|
+ twi_stop();
|
|
436
|
+ break;
|
|
437
|
+ // TW_MR_ARB_LOST handled by TW_MT_ARB_LOST case
|
|
438
|
+
|
|
439
|
+ // Slave Receiver
|
|
440
|
+ case TW_SR_SLA_ACK: // addressed, returned ack
|
|
441
|
+ case TW_SR_GCALL_ACK: // addressed generally, returned ack
|
|
442
|
+ case TW_SR_ARB_LOST_SLA_ACK: // lost arbitration, returned ack
|
|
443
|
+ case TW_SR_ARB_LOST_GCALL_ACK: // lost arbitration, returned ack
|
|
444
|
+ // enter slave receiver mode
|
|
445
|
+ twi_state = TWI_SRX;
|
|
446
|
+ // indicate that rx buffer can be overwritten and ack
|
|
447
|
+ twi_rxBufferIndex = 0;
|
|
448
|
+ twi_reply(1);
|
|
449
|
+ break;
|
|
450
|
+ case TW_SR_DATA_ACK: // data received, returned ack
|
|
451
|
+ case TW_SR_GCALL_DATA_ACK: // data received generally, returned ack
|
|
452
|
+ // if there is still room in the rx buffer
|
|
453
|
+ if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){
|
|
454
|
+ // put byte in buffer and ack
|
|
455
|
+ twi_rxBuffer[twi_rxBufferIndex++] = TWDR;
|
|
456
|
+ twi_reply(1);
|
|
457
|
+ }else{
|
|
458
|
+ // otherwise nack
|
|
459
|
+ twi_reply(0);
|
|
460
|
+ }
|
|
461
|
+ break;
|
|
462
|
+ case TW_SR_STOP: // stop or repeated start condition received
|
|
463
|
+ // put a null char after data if there's room
|
|
464
|
+ if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){
|
|
465
|
+ twi_rxBuffer[twi_rxBufferIndex] = '\0';
|
|
466
|
+ }
|
|
467
|
+ // sends ack and stops interface for clock stretching
|
|
468
|
+ twi_stop();
|
|
469
|
+ // callback to user defined callback
|
|
470
|
+ twi_onSlaveReceive(twi_rxBuffer, twi_rxBufferIndex);
|
|
471
|
+ // since we submit rx buffer to "wire" library, we can reset it
|
|
472
|
+ twi_rxBufferIndex = 0;
|
|
473
|
+ // ack future responses and leave slave receiver state
|
|
474
|
+ twi_releaseBus();
|
|
475
|
+ break;
|
|
476
|
+ case TW_SR_DATA_NACK: // data received, returned nack
|
|
477
|
+ case TW_SR_GCALL_DATA_NACK: // data received generally, returned nack
|
|
478
|
+ // nack back at master
|
|
479
|
+ twi_reply(0);
|
|
480
|
+ break;
|
|
481
|
+
|
|
482
|
+ // Slave Transmitter
|
|
483
|
+ case TW_ST_SLA_ACK: // addressed, returned ack
|
|
484
|
+ case TW_ST_ARB_LOST_SLA_ACK: // arbitration lost, returned ack
|
|
485
|
+ // enter slave transmitter mode
|
|
486
|
+ twi_state = TWI_STX;
|
|
487
|
+ // ready the tx buffer index for iteration
|
|
488
|
+ twi_txBufferIndex = 0;
|
|
489
|
+ // set tx buffer length to be zero, to verify if user changes it
|
|
490
|
+ twi_txBufferLength = 0;
|
|
491
|
+ // request for txBuffer to be filled and length to be set
|
|
492
|
+ // note: user must call twi_transmit(bytes, length) to do this
|
|
493
|
+ twi_onSlaveTransmit();
|
|
494
|
+ // if they didn't change buffer & length, initialize it
|
|
495
|
+ if(0 == twi_txBufferLength){
|
|
496
|
+ twi_txBufferLength = 1;
|
|
497
|
+ twi_txBuffer[0] = 0x00;
|
|
498
|
+ }
|
|
499
|
+ // transmit first byte from buffer, fall
|
|
500
|
+ case TW_ST_DATA_ACK: // byte sent, ack returned
|
|
501
|
+ // copy data to output register
|
|
502
|
+ TWDR = twi_txBuffer[twi_txBufferIndex++];
|
|
503
|
+ // if there is more to send, ack, otherwise nack
|
|
504
|
+ if(twi_txBufferIndex < twi_txBufferLength){
|
|
505
|
+ twi_reply(1);
|
|
506
|
+ }else{
|
|
507
|
+ twi_reply(0);
|
|
508
|
+ }
|
|
509
|
+ break;
|
|
510
|
+ case TW_ST_DATA_NACK: // received nack, we are done
|
|
511
|
+ case TW_ST_LAST_DATA: // received ack, but we are done already!
|
|
512
|
+ // ack future responses
|
|
513
|
+ twi_reply(1);
|
|
514
|
+ // leave slave receiver state
|
|
515
|
+ twi_state = TWI_READY;
|
|
516
|
+ break;
|
|
517
|
+
|
|
518
|
+ // All
|
|
519
|
+ case TW_NO_INFO: // no state information
|
|
520
|
+ break;
|
|
521
|
+ case TW_BUS_ERROR: // bus error, illegal stop/start
|
|
522
|
+ twi_error = TW_BUS_ERROR;
|
|
523
|
+ twi_stop();
|
|
524
|
+ break;
|
|
525
|
+ }
|
|
526
|
+}
|
|
527
|
+
|