-
Notifications
You must be signed in to change notification settings - Fork 26
/
twi.cpp
838 lines (715 loc) · 26.7 KB
/
twi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
/*****************************************************************************
*
*
* File twi.cpp
* Date Saturday, 10/29/17
* Composed by lucullus
*
* Modified by Benoit3 on 18/5/2019 to add multibyte send
*
* **** See twi.h for Credits and Usage information ****
*
*
* This library is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2.1 of the License, or any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
******************************************************************************/
#ifndef __USI_TWI_CPP__
#define __USI_TWI_CPP__
/*--------------------------------------------------------------
includes
----------------------------------------------------------------*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "twi.h"
/*--------------------------------------------------------------
local variables
----------------------------------------------------------------*/
// stores state of twi slave communication
typedef enum
{
USI_SLAVE_CHECK_ADDRESS = 0x00,
USI_SLAVE_SEND_DATA = 0x01,
USI_SLAVE_REQUEST_REPLY_FROM_SEND_DATA = 0x02,
USI_SLAVE_CHECK_REPLY_FROM_SEND_DATA = 0x03,
USI_SLAVE_REQUEST_DATA = 0x04,
USI_SLAVE_GET_DATA_AND_SEND_ACK = 0x05
} overflowState_t;
// stores twi master configuration or it's error state
union USI_TWI_state
{
unsigned char errorState; // Can reuse the TWI_state for error states since it will not be needed if there is an error.
struct
{
unsigned char addressMode : 1;
unsigned char masterWriteDataMode : 1;
unsigned char memReadMode : 1;
unsigned char unused : 5;
};
} USI_TWI_state;
struct USI_TWI_MASTER_TRANSFER_RESULT
{
unsigned char result;
unsigned char error_code;
};
static uint8_t slaveAddress;
static volatile overflowState_t overflowState;
static volatile bool twi_bus_busy = false;
static volatile bool twi_master_mode = false;
static volatile bool currently_receiving = false;
// Receive ringbuffer with extra bytecounter, to give out the number of bytes without iterating through it
static uint8_t rxBuf[ TWI_RX_BUFFER_SIZE ];
static volatile uint8_t rxHead;
static volatile uint8_t rxTail;
static volatile uint8_t rxByteNum;
// Transmit ringbuffer
static uint8_t txBuf[ TWI_TX_BUFFER_SIZE ];
static volatile uint8_t txHead;
static volatile uint8_t txTail;
// Event function variables: called in case of a slave request or a slave receive
static void (*twi_onSlaveTransmit)(void);
static void (*twi_onSlaveReceive)(int);
/*--------------------------------------------------------------
local functions
----------------------------------------------------------------*/
// flushes the TWI buffers
static void flushTwiBuffers(void)
{
rxTail = 0;
rxHead = 0;
txTail = 0;
txHead = 0;
rxByteNum = 0;
}
static void SET_USI_TO_SEND_ACK( )
{
/* prepare ACK */
USIDR = 0;
/* set SDA as output */
DDR_USI |= ( 1 << PORT_USI_SDA );
/* clear all interrupt flags, except Start Cond */
USISR =
( 0 << USI_START_COND_INT ) |
( 1 << USIOIF ) | ( 1 << USIPF ) |
( 1 << USIDC )|
/* set USI counter to shift 1 bit */
( 0x0E << USICNT0 );
}
static void SET_USI_TO_READ_ACK( )
{
/* set SDA as input */
DDR_USI &= ~( 1 << PORT_USI_SDA );
/* prepare ACK */
USIDR = 0;
/* clear all interrupt flags, except Start Cond */
USISR =
( 0 << USI_START_COND_INT ) |
( 1 << USIOIF ) |
( 1 << USIPF ) |
( 1 << USIDC ) |
/* set USI counter to shift 1 bit */
( 0x0E << USICNT0 );
}
static void SET_USI_TO_TWI_START_CONDITION_MODE( )
{
USICR =
/* enable Start Condition Interrupt, disable Overflow Interrupt */
( 1 << USISIE ) | ( 0 << USIOIE ) |
/* set USI in Two-wire mode, no USI Counter overflow hold */
( 1 << USIWM1 ) | ( 0 << USIWM0 ) |
/* Shift Register Clock Source = External, positive edge */
/* 4-Bit Counter Source = external, both edges */
( 1 << USICS1 ) | ( 0 << USICS0 ) | ( 0 << USICLK ) |
/* no toggle clock-port pin */
( 0 << USITC );
USISR =
/* clear all interrupt flags, except Start Cond */
( 0 << USI_START_COND_INT ) | ( 1 << USIOIF ) | ( 1 << USIPF ) |
( 1 << USIDC ) | ( 0x0 << USICNT0 );
}
static void SET_USI_TO_SEND_DATA( )
{
/* set SDA as output */
DDR_USI |= ( 1 << PORT_USI_SDA );
/* clear all interrupt flags, except Start Cond */
USISR =
( 0 << USI_START_COND_INT ) | ( 1 << USIOIF ) | ( 1 << USIPF ) |
( 1 << USIDC) |
/* set USI to shift out 8 bits */
( 0x0 << USICNT0 );
}
static void SET_USI_TO_READ_DATA( )
{
/* set SDA as input */
DDR_USI &= ~( 1 << PORT_USI_SDA );
/* clear all interrupt flags, except Start Cond */
USISR =
( 0 << USI_START_COND_INT ) | ( 1 << USIOIF ) |
( 1 << USIPF ) | ( 1 << USIDC ) |
/* set USI to shift out 8 bits */
( 0x0 << USICNT0 );
}
/*---------------------------------------------------------------
Core function of Master for shifting data in and out from the USI.
Data to be sent has to be placed into the USIDR prior to calling
this function. Data read, will be return'ed from the function.
---------------------------------------------------------------*/
USI_TWI_MASTER_TRANSFER_RESULT USI_TWI_Master_Transfer( unsigned char temp, bool ack)
{
USI_TWI_MASTER_TRANSFER_RESULT result;
result.error_code = 0;
USISR = temp; // Set USISR according to temp.
// Prepare clocking.
temp = (0<<USISIE)|(0<<USIOIE)| // Interrupts disabled
(1<<USIWM1)|(0<<USIWM0)| // Set USI in Two-wire mode.
(1<<USICS1)|(0<<USICS0)|(1<<USICLK)| // Software clock strobe as source.
(1<<USITC); // Toggle Clock Port.
do
{
_delay_us(T2_TWI);
USICR = temp; // Generate positve SCL edge.
while( !(PIN_USI & (1<<PIN_USI_SCL)) );// Wait for SCL to go high.
_delay_us(T4_TWI);
USICR = temp; // Generate negative SCL edge.
#ifdef BUS_ARBITRATION
if(!ack && (USISR & (1<<USIDC))) { // Check data collision bit
Twi_slave_init(slaveAddress);
result.result = 0;
result.error_code = USI_TWI_ARBITRATION_LOST;
return result;
}
#endif
}while( !(USISR & (1<<USIOIF)) ); // Check for transfer complete.
_delay_us(T2_TWI);
temp = USIDR; // Read out data.
USIDR = 0xFF; // Release SDA.
DDR_USI |= (1<<PIN_USI_SDA); // Enable SDA as output.
result.result = temp;
return result; // Return the data from the USIDR
}
/*---------------------------------------------------------------
Function for generating a TWI Start Condition.
---------------------------------------------------------------*/
unsigned char USI_TWI_Master_Start( void )
{
/* Release SCL to ensure that (repeated) Start can be performed */
PORT_USI |= (1<<PIN_USI_SCL); // Release SCL.
while( !(PORT_USI & (1<<PIN_USI_SCL)) ); // Verify that SCL becomes high.
_delay_us(T2_TWI);
/* Generate Start Condition */
PORT_USI &= ~(1<<PIN_USI_SDA); // Force SDA LOW.
_delay_us(T4_TWI);
PORT_USI &= ~(1<<PIN_USI_SCL); // Pull SCL LOW.
PORT_USI |= (1<<PIN_USI_SDA); // Release SDA.
#ifdef SIGNAL_VERIFY
if( !(USISR & (1<<USISIF)) )
{
USI_TWI_state.errorState = USI_TWI_MISSING_START_CON;
return (FALSE);
}
#endif
return (TRUE);
}
/*---------------------------------------------------------------
Function for generating a TWI Stop Condition. Used to release
the TWI bus.
---------------------------------------------------------------*/
unsigned char USI_TWI_Master_Stop( void )
{
PORT_USI &= ~(1<<PIN_USI_SDA); // Pull SDA low.
PORT_USI |= (1<<PIN_USI_SCL); // Release SCL.
while( !(PIN_USI & (1<<PIN_USI_SCL)) ); // Wait for SCL to go high.
_delay_us(T4_TWI);
PORT_USI |= (1<<PIN_USI_SDA); // Release SDA.
_delay_us(T2_TWI);
#ifdef SIGNAL_VERIFY
if( !(USISR & (1<<USIPF)) )
{
USI_TWI_state.errorState = USI_TWI_MISSING_STOP_CON;
return (FALSE);
}
#endif
return (TRUE);
}
/*---------------------------------------------------------------
Master Twi function. First it sends a Start-Condition. The first
byte in parameter msg is always the
slave address with the r/w-bit. Corresponding to the r/w-bit the
function sends all bytes with index < msgSize, or transmits only
the slave address and then reads msgSize-1 bytes from the slave.
Finally it sends a Stop-Condition
---------------------------------------------------------------*/
bool USI_TWI_Start_Read_Write( unsigned char *msg, unsigned char msgSize)
{
unsigned char const tempUSISR_8bit = (1<<USISIF)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| // Prepare register value to: Clear flags, and
(0x0<<USICNT0); // set USI to shift 8 bits i.e. count 16 clock edges.
unsigned char const tempUSISR_1bit = (1<<USISIF)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| // Prepare register value to: Clear flags, and
(0xE<<USICNT0); // set USI to shift 1 bit i.e. count 2 clock edges.
unsigned char *savedMsg;
unsigned char savedMsgSize;
USI_TWI_state.errorState = 0; // Clear error and mode bits
USI_TWI_state.addressMode = TRUE; // Always true for first byte
#ifdef PARAM_VERIFICATION
if(msg > (unsigned char*)RAMEND) // Test if address is outside SRAM space
{
USI_TWI_state.errorState = USI_TWI_DATA_OUT_OF_BOUND;
return (FALSE);
}
if(msgSize <= 1) // Test if the transmission buffer is empty
{
USI_TWI_state.errorState = USI_TWI_NO_DATA;
return (FALSE);
}
#endif
#ifdef NOISE_TESTING // Test if any unexpected conditions have arrived prior to this execution.
/*if( USISR & (1<<USISIF) ) // seems to always occur. Even, if nothing bad happened. Currently disabled
{
USI_TWI_state.errorState = USI_TWI_UE_START_CON;
return (FALSE);
}*/
if( USISR & (1<<USIPF) )
{
USI_TWI_state.errorState = USI_TWI_UE_STOP_CON;
return (FALSE);
}
if( USISR & (1<<USIDC) )
{
USI_TWI_state.errorState = USI_TWI_UE_DATA_COL;
return (FALSE);
}
#endif
if ( !(*msg & (1<<TWI_READ_BIT)) ) // The LSB in the address byte determines if is a masterRead or masterWrite operation.
{
USI_TWI_state.masterWriteDataMode = TRUE;
}
// if (USI_TWI_state.memReadMode)
// {
savedMsg = msg;
savedMsgSize = msgSize;
// }
if ( !USI_TWI_Master_Start( ))
{
return (FALSE); // Send a START condition on the TWI bus.
}
/*Write address and Read/Write data */
do
{
/* If masterWrite cycle (or inital address tranmission)*/
if (USI_TWI_state.addressMode || USI_TWI_state.masterWriteDataMode)
{
/* Write a byte */
PORT_USI &= ~(1<<PIN_USI_SCL); // Pull SCL LOW.
USIDR = *(msg++); // Setup data.
unsigned char error_code = USI_TWI_Master_Transfer( tempUSISR_8bit, false ).error_code; // Send 8 bits on bus.
if(error_code!=0){ // Check if arbitration is lost
USI_TWI_state.errorState = error_code;
return (FALSE);
}
/* Clock and verify (N)ACK from slave */
DDR_USI &= ~(1<<PIN_USI_SDA); // Enable SDA as input.
USI_TWI_MASTER_TRANSFER_RESULT temp_result = USI_TWI_Master_Transfer( tempUSISR_1bit, true );
// No Check for Arbitration Lost, since the arbitration cannot be lost on (N)ACK
if( temp_result.result & (1<<TWI_NACK_BIT) )
{
if ( USI_TWI_state.addressMode )
USI_TWI_state.errorState = USI_TWI_NO_ACK_ON_ADDRESS;
else
USI_TWI_state.errorState = USI_TWI_NO_ACK_ON_DATA;
return (FALSE);
}
if ((!USI_TWI_state.addressMode) && USI_TWI_state.memReadMode)// means memory start address has been written
{
msg = savedMsg; // start at slave address again
*(msg) |= (TRUE<<TWI_READ_BIT); // set the Read Bit on Slave address
USI_TWI_state.errorState = 0;
USI_TWI_state.addressMode = TRUE; // Now set up for the Read cycle
msgSize = savedMsgSize; // Set byte count correctly
// NOte that the length should be Slave adrs byte + # bytes to read + 1 (gets decremented below)
if ( !USI_TWI_Master_Start( ))
{
USI_TWI_state.errorState = USI_TWI_BAD_MEM_READ;
return (FALSE); // Send a START condition on the TWI bus.
}
}
else
{
USI_TWI_state.addressMode = FALSE; // Only perform address transmission once.
}
}
/* Else masterRead cycle*/
else
{
/* Read a data byte */
DDR_USI &= ~(1<<PIN_USI_SDA); // Enable SDA as input.
USI_TWI_MASTER_TRANSFER_RESULT temp_result = USI_TWI_Master_Transfer( tempUSISR_8bit,false );
if( temp_result.error_code != 0){ // Check for Arbitration Lost
USI_TWI_state.errorState = temp_result.error_code;
return (FALSE);
}
*(msg++) = temp_result.result;
/* Prepare to generate ACK (or NACK in case of End Of Transmission) */
if( msgSize == 1) // If transmission of last byte was performed.
{
USIDR = 0xFF; // Load NACK to confirm End Of Transmission.
}
else
{
USIDR = 0x00; // Load ACK. Set data register bit 7 (output for SDA) low.
}
USI_TWI_Master_Transfer( tempUSISR_1bit,true ); // Generate ACK/NACK.
// No Check for Arbitration Lost, since the arbitration cannot be lost on (N)ACK
}
}while( --msgSize) ; // Until all data sent/received.
if (!USI_TWI_Master_Stop())
{
return (FALSE); // Send a STOP condition on the TWI bus.
}
/* Transmission successfully completed*/
return (TRUE);
}
/*--------------------------------------------------------------
public functions
----------------------------------------------------------------*/
/*
Disable USI module
*/
void Twi_end(){
USICR &= 0b11001111; // disable USI
DDR_USI &= ~(( 1 << PORT_USI_SCL ) | ( 1 << PORT_USI_SDA )); // set SDA & SCL as input
twi_master_mode = false;
twi_bus_busy = false;
}
/*
* Function twi_attachSlaveRxEvent
* Desc sets function called after a slave read operation
* Input function: callback function to use
* Output none
*/
void Twi_attachSlaveRxEvent( void (*function)(int) )
{
twi_onSlaveReceive = function;
}
/*
* Function twi_attachSlaveTxEvent
* Desc sets function called before a slave write operation
* Input function: callback function to use
* Output none
*/
void Twi_attachSlaveTxEvent( void (*function)(void) )
{
twi_onSlaveTransmit = function;
}
void Twi_slave_init(uint8_t slave_addr)
{
flushTwiBuffers( );
twi_master_mode = false;
slaveAddress = slave_addr;
USIDR = 0xFF; // Preload dataregister with "released level" data.
// In Two Wire mode (USIWM1, USIWM0 = 1X), the slave USI will pull SCL
// low when a start condition is detected or a counter overflow (only
// for USIWM1, USIWM0 = 11). This inserts a wait state. SCL is released
// by the ISRs (USI_START_vect and USI_OVERFLOW_vect).
// Set SCL and SDA as output
DDR_USI |= ( 1 << PORT_USI_SCL ) | ( 1 << PORT_USI_SDA );
// set SCL high
PORT_USI |= ( 1 << PORT_USI_SCL );
// set SDA high
PORT_USI |= ( 1 << PORT_USI_SDA );
// Set SDA as input
DDR_USI &= ~( 1 << PORT_USI_SDA );
USICR =
// enable Start Condition Interrupt
( 1 << USISIE ) |
// disable Overflow Interrupt
( 0 << USIOIE ) |
// set USI in Two-wire mode, no USI Counter overflow hold
( 1 << USIWM1 ) | ( 0 << USIWM0 ) |
// Shift Register Clock Source = external, positive edge
// 4-Bit Counter Source = external, both edges
( 1 << USICS1 ) | ( 0 << USICS0 ) | ( 0 << USICLK ) |
// no toggle clock-port pin
( 0 << USITC );
// clear all interrupt flags and reset overflow counter
USISR = ( 1 << USI_START_COND_INT ) |
( 1 << USIOIF ) |
( 1 << USIPF ) |
( 1 << USIDC ) |
(0x0<< USICNT0);
GENERAL_INTERRUPT_MASK |= 1 << PIN_CHANGE_INTERRUPT_ENABLE; // enable Pin Change Interrupt
GENERAL_INTERRUPT_FLAGS |= 1 << PIN_CHANGE_FLAG; // clear interrupt flag, by writing 1 to it
PIN_CHANGE_MASK |= 1 << PIN_USI_SDA; // enable pin change interrupt on PCINT0 (SDA line)
}
uint8_t Twi_slave_send(uint8_t data)
{
uint8_t tmphead;
// calculate buffer index
tmphead = ( txHead + 1 ) & TWI_TX_BUFFER_MASK;
// wait for free space in buffer
if(tmphead == txTail) return 0;
// store data in buffer
txBuf[ tmphead ] = data;
// store new index
txHead = tmphead;
return 1;
}
uint8_t Twi_slave_send(uint8_t *data, uint8_t length)
{
uint8_t count=0;
//send byte one by one as long as there's free space in the buffer
while ( (count < length) && (Twi_slave_send(data[count])==1)) count++;
//return count of transmitted bytes
return count;
}
uint8_t Twi_receive(void)
{
// wait for Rx data
while ( rxHead == rxTail );
// calculate buffer index
rxTail = ( rxTail + 1 ) & TWI_RX_BUFFER_MASK;
rxByteNum--;
// return data from the buffer.
return rxBuf[ rxTail ];
}
/*
Returns the number of bytes in rx buffer
*/
uint8_t Twi_available(void)
{
return rxByteNum;
}
//Use this function to get hold of the error message from the last transmission
unsigned char USI_TWI_Get_State_Info( void )
{
return ( USI_TWI_state.errorState ); // Return error state.
}
void Twi_master_init(void)
{
twi_master_mode = true;
GIMSK &= ~(1 << PIN_CHANGE_INTERRUPT_ENABLE);
slaveAddress = 0;
USIDR = 0xFF; // Preload dataregister with "released level" data.
USICR = (0<<USISIE)|(0<<USIOIE)| // Disable Interrupts.
(1<<USIWM1)|(0<<USIWM0)| // Set USI in Two-wire mode.
(1<<USICS1)|(0<<USICS0)|(1<<USICLK)| // Software stobe as counter clock source
(0<<USITC);
USISR = (1<<USISIF)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| // Clear flags,
(0x0<<USICNT0); // and reset counter.
PORT_USI |= (1<<PIN_USI_SDA); // Enable pullup on SDA, to set high as released state.
PORT_USI |= (1<<PORT_USI_SCL); // Enable pullup on SCL, to set high as released state.
DDR_USI |= ( 1 << PORT_USI_SDA );
DDR_USI |= ( 1 << PORT_USI_SCL );
}
void Twi_master_beginTransmission(uint8_t slave_addr)
{
flushTwiBuffers( );
// calculate buffer index
txHead = ( txHead + 1 ) & TWI_TX_BUFFER_MASK;
// store slave address in buffer
txBuf[ txHead ] = (slave_addr<<TWI_ADR_BITS) | USI_SEND;
}
uint8_t Twi_master_send(uint8_t data)
{
uint8_t temphead = (txHead + 1) & TWI_TX_BUFFER_MASK;
if( temphead == txTail ) return 0; // return 0 if buffer is full
txBuf[temphead] = data;
txHead = temphead;
return 1;
}
uint8_t Twi_master_send(uint8_t *data, uint8_t length)
{
uint8_t count=0;
//send byte one by one as long as there's free space in the buffer
while ( (count < length) && (Twi_master_send(data[count])==1)) count++;
//return count of transmitted bytes
return count;
}
uint8_t Twi_master_endTransmission()
{
uint8_t tempbuf[TWI_TX_BUFFER_SIZE];
uint8_t j=0;
bool ok=false;
// copy tx buffer to tempory buffer
while( txHead != txTail ){
txTail = ( txTail +1 ) & TWI_TX_BUFFER_MASK;
tempbuf[j] = txBuf[ txTail ];
j++;
}
if(twi_bus_busy) {USI_TWI_state.errorState = USI_TWI_BUS_BUSY; return USI_TWI_state.errorState;}
if(USI_TWI_Start_Read_Write(tempbuf,j)) return 0;
else return USI_TWI_Get_State_Info();
}
uint8_t Twi_master_requestFrom(uint8_t slave_addr, uint8_t numBytes)
{
uint8_t tempbuf[TWI_RX_BUFFER_SIZE];
bool transferOK= false;
numBytes++; // add extra byte to transmit header
tempbuf[0] = (slave_addr<<TWI_ADR_BITS) | USI_RCVE; // setup address & Rcve bit
if(twi_bus_busy) {USI_TWI_state.errorState = USI_TWI_BUS_BUSY; return USI_TWI_state.errorState;}
transferOK = USI_TWI_Start_Read_Write(tempbuf,numBytes);
for(uint8_t i=0;i < TWI_RX_BUFFER_SIZE;i++){ // swap data from tempbuffer to rxBuffer
uint8_t temphead = (rxHead + 1) & TWI_RX_BUFFER_MASK;
rxBuf[temphead] = tempbuf[i];
rxByteNum++;
rxHead = temphead;
}
if(transferOK) return 0;
else return USI_TWI_Get_State_Info();
}
/*--------------------------------------------------------------
Interrupt Routines, implement slave functions
----------------------------------------------------------------*/
// Interrupt Routine, triggered when master sends start condition
ISR( USI_START_VECTOR )
{
twi_bus_busy = true;
// set SDA as input
DDR_USI &= ~( 1 << PORT_USI_SDA );
// wait for SCL to go low to ensure the Start Condition has completed (the
// start detector will hold SCL low ) - if a Stop Condition arises then leave
// the interrupt to prevent waiting forever - don't use USISR to test for Stop
// Condition as in Application Note AVR312 because the Stop Condition Flag is
// going to be set from the last TWI sequence
while (
// SCL his high
( PIN_USI & ( 1 << PIN_USI_SCL ) ) &&
// and SDA is low
!( ( PIN_USI & ( 1 << PIN_USI_SDA ) ) )
);
if ( !( PIN_USI & ( 1 << PIN_USI_SDA ) ) )
{
// a Stop Condition did not occur
USICR =
// keep Start Condition Interrupt enabled to detect RESTART
( 1 << USISIE ) |
// enable Overflow Interrupt
( 1 << USIOIE ) |
// set USI in Two-wire mode, hold SCL low on USI Counter overflow
( 1 << USIWM1 ) | ( 1 << USIWM0 ) |
// Shift Register Clock Source = External, positive edge
// 4-Bit Counter Source = external, both edges
( 1 << USICS1 ) | ( 0 << USICS0 ) | ( 0 << USICLK ) |
// no toggle clock-port pin
( 0 << USITC );
if(currently_receiving){ // if we had received something, this is a RESTART condition and we have
// to call the users receive callback
twi_onSlaveReceive(rxByteNum);
currently_receiving = false;
}
}
USISR =
// clear interrupt flags - resetting the Start Condition Flag will
// release SCL
( 1 << USI_START_COND_INT ) | ( 1 << USIOIF ) |
( 1 << USIPF ) |( 1 << USIDC ) |
// set USI to sample 8 bits (count 16 external SCL pin toggles)
( 0x0 << USICNT0);
// set default starting conditions for new TWI package
overflowState = USI_SLAVE_CHECK_ADDRESS;
} // end ISR( USI_START_VECTOR )
// Interrupt Routine, triggered when one byte was transmitted
ISR( USI_OVERFLOW_VECTOR )
{
switch ( overflowState )
{
// Address mode: check address and send ACK (and next USI_SLAVE_SEND_DATA) if OK,
// else reset USI
case USI_SLAVE_CHECK_ADDRESS:
if ( ( USIDR == 0 ) || ( ( USIDR >> 1 ) == slaveAddress) )
{
if ( USIDR & 0x01 )
{
overflowState = USI_SLAVE_SEND_DATA;
twi_onSlaveTransmit(); // call user event function for a request from master
}
else
{
currently_receiving = true;
overflowState = USI_SLAVE_REQUEST_DATA;
} // end if
SET_USI_TO_SEND_ACK( );
}
else
{
SET_USI_TO_TWI_START_CONDITION_MODE( );
}
break;
// Master write data mode: check reply and goto USI_SLAVE_SEND_DATA if OK,
// else reset USI
case USI_SLAVE_CHECK_REPLY_FROM_SEND_DATA:
if ( USIDR )
{
// if NACK, the master does not want more data
SET_USI_TO_TWI_START_CONDITION_MODE( );
// clear TX buffer, because the rest of data in it is unwanted by the master
txHead = txTail = 0;
return;
}
// from here we just drop straight into USI_SLAVE_SEND_DATA if the
// master sent an ACK
// copy data from buffer to USIDR and set USI to shift byte
// next USI_SLAVE_REQUEST_REPLY_FROM_SEND_DATA
case USI_SLAVE_SEND_DATA:
// Get data from Buffer
if ( txHead != txTail )
{
txTail = ( txTail + 1 ) & TWI_TX_BUFFER_MASK;
USIDR = txBuf[ txTail ];
}
else
{
// the buffer is empty
SET_USI_TO_TWI_START_CONDITION_MODE( );
return;
} // end if
overflowState = USI_SLAVE_REQUEST_REPLY_FROM_SEND_DATA;
SET_USI_TO_SEND_DATA( );
break;
// set USI to sample reply from master
// next USI_SLAVE_CHECK_REPLY_FROM_SEND_DATA
case USI_SLAVE_REQUEST_REPLY_FROM_SEND_DATA:
overflowState = USI_SLAVE_CHECK_REPLY_FROM_SEND_DATA;
SET_USI_TO_READ_ACK( );
break;
// Master read data mode: set USI to sample data from master, next
// USI_SLAVE_GET_DATA_AND_SEND_ACK
case USI_SLAVE_REQUEST_DATA:
overflowState = USI_SLAVE_GET_DATA_AND_SEND_ACK;
SET_USI_TO_READ_DATA( );
break;
// copy data from USIDR and send ACK
// next USI_SLAVE_REQUEST_DATA
case USI_SLAVE_GET_DATA_AND_SEND_ACK:
// put data into buffer
// Not necessary, but prevents warnings
rxHead = ( rxHead + 1 ) & TWI_RX_BUFFER_MASK;
rxBuf[ rxHead ] = USIDR;
rxByteNum++;
// next USI_SLAVE_REQUEST_DATA
overflowState = USI_SLAVE_REQUEST_DATA;
SET_USI_TO_SEND_ACK( );
break;
} // end switch
} // end ISR( USI_OVERFLOW_VECTOR )
// Interrupt Routine, triggered when SDA pin changes; for detecting Stop condition
ISR( PCINT0_vect )
{
if((PIN_USI & (1 << PIN_USI_SDA)) && (PIN_USI & ( 1 << PIN_USI_SCL ))){
// stop condition occured
_delay_us(7);
if((PIN_USI & (1 << PIN_USI_SDA)) && (PIN_USI & ( 1 << PIN_USI_SCL ))){
twi_bus_busy = false;
if(currently_receiving){ // If we are receiving bytes from a master, call user callback
if(rxByteNum>0) twi_onSlaveReceive(rxByteNum); // check, if anything is in the rx buffer, because it is possible,
// that the communication was interrupted by a stop condition
currently_receiving = false;
}
USISR |= 1<<USIPF; // resetting stop condition flag
}
}
}
#endif