@@ -87,7 +87,7 @@ static volatile uint8_t twi_rxBufferIndex;
87
87
88
88
static volatile uint8_t twi_error ;
89
89
90
- /*
90
+ /*
91
91
* Function twi_init
92
92
* Desc readys twi pins and sets twi bitrate
93
93
* Input none
@@ -99,7 +99,7 @@ void twi_init(void)
99
99
twi_state = TWI_READY ;
100
100
twi_sendStop = true; // default value
101
101
twi_inRepStart = false;
102
-
102
+
103
103
// activate internal pullups for twi.
104
104
digitalWrite (SDA , 1 );
105
105
digitalWrite (SCL , 1 );
@@ -118,7 +118,7 @@ void twi_init(void)
118
118
TWCR = _BV (TWEN ) | _BV (TWIE ) | _BV (TWEA );
119
119
}
120
120
121
- /*
121
+ /*
122
122
* Function twi_disable
123
123
* Desc disables twi pins
124
124
* Input none
@@ -134,7 +134,7 @@ void twi_disable(void)
134
134
digitalWrite (SCL , 0 );
135
135
}
136
136
137
- /*
137
+ /*
138
138
* Function twi_slaveInit
139
139
* Desc sets slave address and enables interrupt
140
140
* Input none
@@ -146,7 +146,7 @@ void twi_setAddress(uint8_t address)
146
146
TWAR = address << 1 ;
147
147
}
148
148
149
- /*
149
+ /*
150
150
* Function twi_setClock
151
151
* Desc sets twi bit rate
152
152
* Input Clock Frequency
@@ -155,14 +155,14 @@ void twi_setAddress(uint8_t address)
155
155
void twi_setFrequency (uint32_t frequency )
156
156
{
157
157
TWBR = ((F_CPU / frequency ) - 16 ) / 2 ;
158
-
158
+
159
159
/* twi bit rate formula from atmega128 manual pg 204
160
160
SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR))
161
161
note: TWBR should be 10 or higher for master mode
162
162
It is 72 for a 16mhz Wiring board with 100kHz TWI */
163
163
}
164
164
165
- /*
165
+ /*
166
166
* Function twi_readFrom
167
167
* Desc attempts to become twi bus master and read a
168
168
* series of bytes from a device on the bus
@@ -193,7 +193,7 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen
193
193
twi_masterBufferIndex = 0 ;
194
194
twi_masterBufferLength = length - 1 ; // This is not intuitive, read on...
195
195
// On receive, the previously configured ACK/NACK setting is transmitted in
196
- // response to the received byte before the interrupt is signalled.
196
+ // response to the received byte before the interrupt is signalled.
197
197
// Therefor we must actually set NACK when the _next_ to last byte is
198
198
// received, causing that NACK to be sent in response to receiving the last
199
199
// expected byte of data.
@@ -207,7 +207,7 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen
207
207
// (@@@ we hope), and the TWI statemachine is just waiting for the address byte.
208
208
// We need to remove ourselves from the repeated start state before we enable interrupts,
209
209
// since the ISR is ASYNC, and we could get confused if we hit the ISR before cleaning
210
- // up. Also, don't enable the START interrupt. There may be one pending from the
210
+ // up. Also, don't enable the START interrupt. There may be one pending from the
211
211
// repeated start that we sent ourselves, and that would really confuse things.
212
212
twi_inRepStart = false; // remember, we're dealing with an ASYNC ISR
213
213
do {
@@ -229,7 +229,7 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen
229
229
for (i = 0 ; i < length ; ++ i ){
230
230
data [i ] = twi_masterBuffer [i ];
231
231
}
232
-
232
+
233
233
return length ;
234
234
235
235
waiting_timedout :
@@ -238,7 +238,7 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen
238
238
return 0 ;
239
239
}
240
240
241
- /*
241
+ /*
242
242
* Function twi_writeTo
243
243
* Desc attempts to become twi bus master and write a
244
244
* series of bytes to a device on the bus
@@ -273,16 +273,16 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait
273
273
// initialize buffer iteration vars
274
274
twi_masterBufferIndex = 0 ;
275
275
twi_masterBufferLength = length ;
276
-
276
+
277
277
// copy data to twi buffer
278
278
for (i = 0 ; i < length ; ++ i ){
279
279
twi_masterBuffer [i ] = data [i ];
280
280
}
281
-
281
+
282
282
// build sla+w, slave device address + w bit
283
283
twi_slarw = TW_WRITE ;
284
284
twi_slarw |= address << 1 ;
285
-
285
+
286
286
// if we're in a repeated start, then we've already sent the START
287
287
// in the ISR. Don't do it again.
288
288
//
@@ -291,11 +291,11 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait
291
291
// (@@@ we hope), and the TWI statemachine is just waiting for the address byte.
292
292
// We need to remove ourselves from the repeated start state before we enable interrupts,
293
293
// since the ISR is ASYNC, and we could get confused if we hit the ISR before cleaning
294
- // up. Also, don't enable the START interrupt. There may be one pending from the
294
+ // up. Also, don't enable the START interrupt. There may be one pending from the
295
295
// repeated start that we sent outselves, and that would really confuse things.
296
296
twi_inRepStart = false; // remember, we're dealing with an ASYNC ISR
297
297
do {
298
- TWDR = twi_slarw ;
298
+ TWDR = twi_slarw ;
299
299
} while (TWCR & _BV (TWWC ));
300
300
TWCR = _BV (TWINT ) | _BV (TWEA ) | _BV (TWEN ) | _BV (TWIE ); // enable INTs, but not START
301
301
}
@@ -307,7 +307,7 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait
307
307
if (wait ) {
308
308
BUSYWAIT_WITH_TIMEOUT_UNTIL (twi_state != TWI_MTX , waiting_timedout );
309
309
}
310
-
310
+
311
311
if (twi_error == 0xFF )
312
312
return 0 ; // success
313
313
else if (twi_error == TW_MT_SLA_NACK )
@@ -323,7 +323,7 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait
323
323
return 4 ;
324
324
}
325
325
326
- /*
326
+ /*
327
327
* Function twi_transmit
328
328
* Desc fills slave tx buffer with data
329
329
* must be called in slave tx event callback
@@ -341,22 +341,22 @@ uint8_t twi_transmit(const uint8_t* data, uint8_t length)
341
341
if (TWI_BUFFER_LENGTH < (twi_txBufferLength + length )){
342
342
return 1 ;
343
343
}
344
-
344
+
345
345
// ensure we are currently a slave transmitter
346
346
if (TWI_STX != twi_state ){
347
347
return 2 ;
348
348
}
349
-
349
+
350
350
// set length and copy data into tx buffer
351
351
for (i = 0 ; i < length ; ++ i ){
352
352
twi_txBuffer [twi_txBufferLength + i ] = data [i ];
353
353
}
354
354
twi_txBufferLength += length ;
355
-
355
+
356
356
return 0 ;
357
357
}
358
358
359
- /*
359
+ /*
360
360
* Function twi_attachSlaveRxEvent
361
361
* Desc sets function called before a slave read operation
362
362
* Input function: callback function to use
@@ -367,7 +367,7 @@ void twi_attachSlaveRxEvent( void (*function)(uint8_t*, int) )
367
367
twi_onSlaveReceive = function ;
368
368
}
369
369
370
- /*
370
+ /*
371
371
* Function twi_attachSlaveTxEvent
372
372
* Desc sets function called before a slave write operation
373
373
* Input function: callback function to use
@@ -378,7 +378,7 @@ void twi_attachSlaveTxEvent( void (*function)(void) )
378
378
twi_onSlaveTransmit = function ;
379
379
}
380
380
381
- /*
381
+ /*
382
382
* Function twi_reply
383
383
* Desc sends byte or readys receive line
384
384
* Input ack: byte indicating to ack or to nack
@@ -394,7 +394,7 @@ void twi_reply(uint8_t ack)
394
394
}
395
395
}
396
396
397
- /*
397
+ /*
398
398
* Function twi_stop
399
399
* Desc relinquishes bus master status
400
400
* Input none
@@ -417,7 +417,7 @@ void twi_stop(void)
417
417
twi_init ();
418
418
}
419
419
420
- /*
420
+ /*
421
421
* Function twi_releaseBus
422
422
* Desc releases bus control
423
423
* Input none
@@ -451,7 +451,7 @@ ISR(TWI_vect)
451
451
// Master Transmitter
452
452
case TW_MT_SLA_ACK : // slave receiver acked address
453
453
case TW_MT_DATA_ACK : // slave receiver acked data
454
- // if there is data to send, send it, otherwise stop
454
+ // if there is data to send, send it, otherwise stop
455
455
if (twi_masterBufferIndex < twi_masterBufferLength ){
456
456
// copy data to output register and ack
457
457
TWDR = twi_masterBuffer [twi_masterBufferIndex ++ ];
@@ -461,7 +461,7 @@ ISR(TWI_vect)
461
461
twi_stop ();
462
462
else {
463
463
twi_inRepStart = true; // we're gonna send the START
464
- // don't enable the interrupt. We'll generate the start, but we
464
+ // don't enable the interrupt. We'll generate the start, but we
465
465
// avoid handling the interrupt until we're in the next transaction,
466
466
// at the point where we would normally issue the start.
467
467
TWCR = _BV (TWINT ) | _BV (TWSTA )| _BV (TWEN ) ;
@@ -501,12 +501,12 @@ ISR(TWI_vect)
501
501
twi_stop ();
502
502
else {
503
503
twi_inRepStart = true; // we're gonna send the START
504
- // don't enable the interrupt. We'll generate the start, but we
504
+ // don't enable the interrupt. We'll generate the start, but we
505
505
// avoid handling the interrupt until we're in the next transaction,
506
506
// at the point where we would normally issue the start.
507
507
TWCR = _BV (TWINT ) | _BV (TWSTA )| _BV (TWEN ) ;
508
508
twi_state = TWI_READY ;
509
- }
509
+ }
510
510
break ;
511
511
case TW_MR_SLA_NACK : // address sent, nack received
512
512
twi_stop ();
@@ -553,7 +553,7 @@ ISR(TWI_vect)
553
553
// nack back at master
554
554
twi_reply (0 );
555
555
break ;
556
-
556
+
557
557
// Slave Transmitter
558
558
case TW_ST_SLA_ACK : // addressed, returned ack
559
559
case TW_ST_ARB_LOST_SLA_ACK : // arbitration lost, returned ack
@@ -582,7 +582,7 @@ ISR(TWI_vect)
582
582
twi_reply (0 );
583
583
}
584
584
break ;
585
- case TW_ST_DATA_NACK : // received nack, we are done
585
+ case TW_ST_DATA_NACK : // received nack, we are done
586
586
case TW_ST_LAST_DATA : // received ack, but we are done already!
587
587
// ack future responses
588
588
twi_reply (1 );
0 commit comments