forked from Ho-Ro/ComponentTester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI2C.c
1012 lines (801 loc) · 21.3 KB
/
I2C.c
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
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ************************************************************************
*
* I2C (bit-bang & hardware TWI)
*
* (c) 2017-2021 by Markus Reschke
*
* ************************************************************************ */
/*
* hints:
* - port and pins for bit-bang I2C
* I2C_PORT port data register
* I2C_DDR port data direction register
* I2C_PIN port input pins register
* I2C_SDA pin for SDA
* I2C_SCL pin for SCL
* - For hardware I2C (TWI) the MCU specific pins are used:
* ATmega 328: SDA PC4 / SCL PC5
* ATmega 644: SDA PC1 / SCL PC0
* - bus speed modes
* I2C_STANDARD_MODE 100kHz
* I2C_FAST_MODE 400kHz
* - Don't forget the pull up resistors for SDA and SCL!
* Usually 2-10kOhms for 5V.
*/
/* local includes */
#include "config.h" /* global configuration */
#ifdef HW_I2C
/*
* local constants
*/
/* source management */
#define I2C_C
/*
* include header files
*/
/* local includes */
#include "common.h" /* common header file */
#include "variables.h" /* global variables */
#include "functions.h" /* external functions */
/*
* bus speed modes:
* - standard mode up to 100kbit/s
* - fast mode up to 400kbit/s
* - fast mode plus up to 1Mbit/s
* - high-speed mode
* 400pF load up to 1.7Mbit/s
* 100pF load up to 3.4Mbit/s
*
* address modes:
* - 7 bit + R/W -> address byte
* R/W (1/0) bit 0
* bit 0 (A0) bit 1
* bit 1 (A1) bit 2
* bit 2 (A2) bit 3
* bits 3-6 bits 4-7
* - 10 bit + R/W -> 2 address bytes
* R/W (1/0) MSB bit 0
* bits 0-7 LSB bits 0-7
* bits 8-9 MSB bits 1-2
* 11110 MSB bits 3-7
*/
/* ************************************************************************
* functions for bit-bang I2C
* ************************************************************************ */
#ifdef I2C_BITBANG
/*
* set up SDA and SCL lines
*
* returns:
* - I2C_ERROR on bus error
* - I2C_OK on success
*/
uint8_t I2C_Setup(void)
{
uint8_t Flag = I2C_ERROR; /* return value */
uint8_t Bits; /* register bits */
/* default is SDA and SCL pulled up by external resistors */
/* set both pins to HiZ input mode */
I2C_DDR &= ~((1 << I2C_SDA) | (1 << I2C_SCL));
/* preset pins to 0 */
I2C_PORT &= ~((1 << I2C_SDA) | (1 << I2C_SCL));
/* check if SDA and SCL are pulled up externally */
Bits = I2C_PIN; /* get state */
Bits &= (1 << I2C_SDA) | (1 << I2C_SCL); /* filter lines */
if (Bits == ((1 << I2C_SDA) | (1 << I2C_SCL))) /* lines are high */
{
Flag = I2C_OK; /* signal success */
}
/*
* current state:
* - SDA high
* - SCL high
*/
return Flag;
}
/*
* create start condition
* - Type
* I2C_START for start condition
* I2C_REPEATED_START for repeated start condition (not enabled yet)
*
* returns:
* - I2C_ERROR on bus error
* - I2C_OK on success
*/
uint8_t I2C_Start(uint8_t Type)
{
uint8_t Flag = I2C_OK; /* return value */
uint8_t Bits; /* register bits */
if (Type == I2C_START) /* start */
{
/*
* expected state:
* - SDA high
* - SCL high
*/
/*
* todo: clock hold
* - check if SCL is pulled down by slave
* - wait until slave releases SCL
*/
Bits = I2C_DDR;
/* pull down SDA (SDA low) */
Bits |= (1 << I2C_SDA); /* set output mode */
I2C_DDR = Bits; /* enable pull down */
/* SCL will follow after t_HD;STA */
#ifdef I2C_FAST_MODE
/* fast mode: min. 0.6µs */
wait1us();
#else
/* standard mode: min. 4µs */
wait4us();
#endif
/* pull down SCL (SCL low) */
Bits |= (1 << I2C_SCL); /* set output mode */
I2C_DDR = Bits; /* enable pull down */
/*
* current state:
* - SDA low
* - SCL low
*/
}
#if 0
else /* repeated start */
{
/*
* expected state:
* - SDA undefined
* - SCL low
*/
/* set SDA high */
I2C_DDR &= ~(1 << I2C_SDA);
/* SCL has to stay low for t_LOW */
#ifdef I2C_FAST_MODE
/* fast mode: min. 1.3µs */
wait2us();
#else
/* standard mode: min. 4.7µs */
wait5us();
#endif
/* set SCL high */
I2C_DDR &= ~(1 << I2C_SCL);
/* SDA has to stay high for t_SU;STA after SCL rises */
#ifdef I2C_FAST_MODE
/* fast mode: min. 0.6µs */
wait1us();
#else
/* standard mode: min. 4.7µs */
wait5us();
#endif
/* follow common start condition */
/* ... code from above (I2C_START) ... */
/*
* current state:
* - SDA low
* - SCL low
*/
}
#endif
/* current state: SDA low / SCL low */
return Flag;
}
/*
* write byte (master to slave)
* - send byte (address or data) to slave
* bit-bang 8 bits, MSB first, LSB last
* - get ACK/NACK from slave
* - byte to be sent is taken from global I2C.Byte
*
* requires:
* - Type
* I2C_DATA for data byte
* I2C_ADDRESS for address byte
*
* returns:
* - I2C_ERROR for bus error
* - I2C_ACK for ACK
* - I2C_NACK NACK
*/
uint8_t I2C_WriteByte(uint8_t Type)
{
uint8_t Flag = I2C_ERROR; /* return value */
uint8_t Byte; /* byte */
uint8_t n = 8; /* counter */
/*
* expected state:
* - SDA undefined
* - SCL low
*/
/*
* send byte
*/
Byte = I2C.Byte; /* get byte */
/* bit-bang 8 bits */
while (n > 0) /* 8 bits */
{
/*
* set SDA
*/
/* SDA has to be set t_SU;DAT before SCL rises */
/* fast mode: min. 100ns */
/* standard mode: min. 250ns */
/* get current MSB and set SDA */
if (Byte & 0b10000000) /* 1 */
{
/* set SDA high by releasing it */
I2C_DDR &= ~(1 << I2C_SDA);
}
else /* 0 */
{
/* set SDA low by pulling it down */
I2C_DDR |= (1 << I2C_SDA);
}
/*
* run SCL cycle
*/
/* SCL has to stay low for t_LOW */
#ifdef I2C_FAST_MODE
/* fast mode: min. 1.3µs */
wait2us();
#else
/* standard mode: min. 4.7µs */
wait5us();
#endif
/* set SCL high */
I2C_DDR &= ~(1 << I2C_SCL);
/*
* todo: clock stretching (inter-bit)
* - check if SCL is pulled down by slave
* - wait until slave releases SCL
*/
/* SCL has to stay high for t_HIGH */
#ifdef I2C_FAST_MODE
/* fast mode: min. 0.6µs */
wait1us();
#else
/* standard mode: min. 4µs */
wait4us();
#endif
/* set SCL low */
I2C_DDR |= (1 << I2C_SCL);
/* SDA has to stay valid for t_HD;DAT after SCL falls */
/* fast mode: min. 300ns */
/* standard mode: min. 300ns */
wait1us();
/* new SDA has to become valid in t_VD;DAT after SCL falls */
/* fast mode: max. 0.9µs */
/* standard mode: max. 3.45µs */
Byte <<= 1; /* shift bits one step left */
n--; /* next bit */
}
/*
* current state:
* - SDA undefined
* - SCL low
*/
/*
* get ACK/NACK
*/
/*
* expected state:
* - SDA undefined
* - SCL low
*/
/* release SDA (SDA high) for slave */
I2C_DDR &= ~(1 << I2C_SDA);
/*
* start SCL cycle for ACK/NACK
*/
/* SCL has to stay low for t_LOW */
#ifdef I2C_FAST_MODE
/* fast mode: min. 1.3µs */
wait2us();
#else
/* standard mode: min. 4.7µs */
wait5us();
#endif
/* set SCL high */
I2C_DDR &= ~(1 << I2C_SCL);
/*
* we expect slave to pull down SDA (SDA low) to ack
* - some slaves might need some time, like an EEPROM for writing
* or an ADC for the conversion
*/
Byte = I2C.Timeout; /* get timeout */
Byte *= 2; /* convert into 5µs steps */
if (Byte == 0) /* prevent zero timeout */
{
Byte++; /* set to 1 (5µs) */
}
while (Byte > 0) /* timeout loop */
{
Byte--; /* decrease timeout counter */
n = I2C_PIN; /* get current state */
n &= (1 << I2C_SDA); /* filter SDA pin */
if (n == 0) /* SDA low */
{
Flag = I2C_ACK; /* signal ACK */
Byte = 0; /* end loop */
}
else /* SDA high */
{
if (Byte > 0) /* no timeout yet */
{
wait5us(); /* wait */
}
}
}
if (Flag == I2C_ERROR) /* got no ACK (still default) */
{
Flag = I2C_NACK; /* signal NACK */
}
/* for an ACK the slave has to keep SDA during the clock pulse */
/*
* todo: clock stretching (inter-byte)
* - check if SCL is pulled down by slave
* - wait until slave releases SCL
*/
/* SCL has to stay high for t_HIGH */
#ifdef I2C_FAST_MODE
/* fast mode: min. 0.6µs */
wait1us();
#else
/* standard mode: min. 4µs */
wait4us();
#endif
/* todo: should we check if SDA is still low? */
/* set SCL low */
I2C_DDR |= (1 << I2C_SCL);
/* SDA has to stay valid for t_HD;DAT after SCL falls */
/* fast mode: min. 300ns */
/* standard mode: min. 300ns */
wait1us();
/* slave has to release SDA (SDA high) */
/* so we check this */
n = I2C_PIN; /* get current state */
n &= (1 << I2C_SDA); /* filter SDA pin */
if (n == 0) /* SDA low */
{
/* slave hasn't released SDA */
Flag = I2C_ERROR; /* signal error */
}
/*
* current state:
* - SDA high
* - SCL low
*/
return Flag;
}
#ifdef I2C_RW
/*
* read byte (master from slave)
* - read byte (data) from slave
* bit-bang 8 bits, MSB first, LSB last
* - ACK/NACK to slave
* - byte to be read is stored in global I2C.Byte
*
* requires:
* - Type
* I2C_ACK for sending acknowledge
* I2C_NACK for sending not-acknowledge
*
* returns:
* - I2C_ERROR on bus error
* - I2C_OK on success
*/
uint8_t I2C_ReadByte(uint8_t Type)
{
uint8_t Flag = I2C_OK; /* return value */
uint8_t Byte = 0; /* byte */
uint8_t n = 8; /* counter */
/*
* expected state:
* - SDA undefined
* - SCL low
*/
/*
* read byte
*/
/* release SDA for slave (SDA high) */
I2C_DDR &= ~(1 << I2C_SDA);
/* bit-bang 8 bits */
while (n > 0) /* 8 bits */
{
/*
* run SCL cycle
*/
/* SCL has to stay low for t_LOW */
#ifdef I2C_FAST_MODE
/* fast mode: min. 1.3µs */
wait2us();
#else
/* standard mode: min. 4.7µs */
wait5us();
#endif
/* set SCL high */
I2C_DDR &= ~(1 << I2C_SCL);
/* SCL has to stay high for t_HIGH */
#ifdef I2C_FAST_MODE
/* fast mode: min. 0.6µs */
wait1us();
#else
/* standard mode: min. 4µs */
wait4us();
#endif
/* set SCL low */
I2C_DDR |= (1 << I2C_SCL);
/* SDA has to stay valid for t_HD;DAT after SCL falls */
/* fast mode: min. 300ns */
/* standard mode: min. 300ns */
/* new SDA has to become valid in t_VD;DAT after SCL falls */
/* fast mode: max. 0.9µs */
/* standard mode: max. 3.45µs */
/* read SDA */
if (I2C_PIN & (1 << I2C_SDA)) /* SDA high */
{
Byte |= 0b00000001; /* set bit */
}
n--; /* next bit */
if (n > 0)
{
Byte <<= 1; /* shift 1 bit left */
}
}
I2C.Byte = Byte; /* save byte */
/*
* current state:
* - SDA undefined
* - SCL low
*/
/*
* send ACK/NACK
*/
/*
* expected state:
* - SDA undefined
* - SCL low
*/
/* check if slave has released SDA */
n = I2C_PIN; /* get current state */
n &= (1 << I2C_SDA); /* filter SDA pin */
if (n == 0) /* SDA low */
{
/* slave hasn't released SDA */
Flag = I2C_ERROR; /* signal error */
return Flag;
}
/* SDA has to become valid in t_VD;ACK after read's last SCL fall */
/* fast mode: max. 0.9µs */
/* standard mode: max. 3.45µs */
/* set SDA */
if (Type == I2C_ACK) /* ACK */
{
/* pull down SDA (SDA low) to signal ack to slave */
I2C_DDR |= (1 << I2C_SDA);
}
/* else: NACK */
/* we keep SDA high to signal nack to slave */
/*
* run SCL cycle for ACK
*/
/* SCL has to stay low for t_LOW */
#ifdef I2C_FAST_MODE
/* fast mode: min. 1.3µs */
wait2us();
#else
/* standard mode: min. 4.7µs */
wait5us();
#endif
/* set SCL high */
I2C_DDR &= ~(1 << I2C_SCL);
/* SCL has to stay high for t_HIGH */
#ifdef I2C_FAST_MODE
/* fast mode: min. 0.6µs */
wait1us();
#else
/* standard mode: min. 4µs */
wait4us();
#endif
/* set SCL low */
I2C_DDR |= (1 << I2C_SCL);
/* SDA has to stay valid for t_HD;DAT after SCL falls */
/* fast mode: min. 300ns */
/* standard mode: min. 300ns */
wait1us();
/*
* current state:
* - SDA undefined
* - SCL low
*/
return Flag;
}
#endif
/*
* create stop condition
*/
void I2C_Stop(void)
{
uint8_t Bits; /* register bits */
/*
* expected state:
* - SDA undefined
* - SCL low
*/
Bits = I2C_DDR; /* get current state */
/* pull down SDA (SDA low) */
Bits |= (1 << I2C_SDA); /* set output mode */
I2C_DDR = Bits; /* enable pull-down */
/* SCL has to stay low for t_LOW */
/* actually t_LOW - t_HD;DAT after ACK */
#ifdef I2C_FAST_MODE
/* fast mode: min. 1.3µs */
wait2us();
#else
/* standard mode: min. 4.7µs */
wait5us();
#endif
/* release SCL (SCL high) */
Bits &= ~(1 << I2C_SCL); /* set input mode */
I2C_DDR = Bits; /* enable HiZ */
/* SDA will follow after t_SU;STO */
#ifdef I2C_FAST_MODE
/* fast mode: min. 0.6µs */
wait1us();
#else
/* standard mode: min. 4µs */
wait4us();
#endif
/* release SDA (SDA high) */
Bits &= ~(1 << I2C_SDA); /* set input mode */
I2C_DDR = Bits; /* enable HiZ */
/*
* bus free time between Stop and next Start (T_BUF)
* - standard mode: min. 4.7µs
* - fast mode: min. 1.3µs
*/
/*
* current state:
* - SDA high
* - SCL high
*/
}
#endif
/* ************************************************************************
* functions for hardware TWI
* ************************************************************************ */
#ifdef I2C_HARDWARE
/*
* set up TWI
*
* returns:
* - I2C_ERROR on bus error
* - I2C_OK on success
*/
uint8_t I2C_Setup(void)
{
uint8_t Flag = I2C_OK; /* return value */
/*
* set bus speed:
* - SCL clock = MCU clock / (16 + 2*TWBR * prescaler)
* - available prescalers: 1, 4, 16 & 64
* - TWBR register: 1-255
*/
/* set prescaler to 1 */
TWSR = 0; /* TWPS1=0 / TWPS0=0 */
#ifdef I2C_FAST_MODE
/* 400kHz */
TWBR = CPU_FREQ / 400000;
#else
/* 100kHz */
TWBR = CPU_FREQ / 100000;
#endif
return Flag;
}
/*
* create start condition
*
* requires:
* - Type
* I2C_START for start condition
* I2C_REPEATED_START for repeated start condition
*
* returns:
* - I2C_ERROR on bus error
* - I2C_OK on success
*/
uint8_t I2C_Start(uint8_t Type)
{
uint8_t Flag = I2C_ERROR; /* return value */
uint8_t Bits; /* bits */
/*
* MCU's state maschine for TWI:
* - Start for new communication
* - Repeated Start after Start (no Stop yet)
*/
/* set expected status flag */
if (Type == I2C_START) /* start */
{
Type = (1 << TWS3); /* 0x08 */
}
else /* repeated start */
{
Type = (1 << TWS4); /* 0x10 */
}
/*
* (repeated) start condition:
* - set TWINT to clear interrupt flag
* - set TWEN to enable TWI
* - set TWSTA for "start"
*/
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTA);
/* wait for job done */
while (!(TWCR & (1 << TWINT))); /* wait for flag */
/*
* check result
*/
Bits = TWSR; /* get status */
/* filter status bits */
Bits &= (1 << TWS7) | (1 << TWS6) | (1 << TWS5) | (1 << TWS4) | (1 << TWS3);
if (Bits == Type) /* got expected flag */
{
Flag = I2C_OK; /* signal success */
}
return Flag;
}
/*
* write byte (master to slave)
* - send byte (address or data) to slave
* - get ACK/NACK from slave
* - byte to be sent is taken from global I2C.Byte
*
* requires:
* - Type
* I2C_DATA for data byte
* I2C_ADDRESS for address byte
*
* returns:
* - I2C_ERROR for bus error
* - I2C_ACK for ACK
* - I2C_NACK NACK
*/
uint8_t I2C_WriteByte(uint8_t Type)
{
uint8_t Flag = I2C_ERROR; /* return value */
uint8_t Bits; /* register bits */
uint8_t AckBits; /* register bits for ACK */
uint8_t NackBits; /* register bits for NACK */
/*
* MCU's state maschine for TWI:
* - after Start the slave has to be addressed
* - after addressing the slave (write) data can be sent
*/
Bits = I2C.Byte; /* get byte */
/* set status register bits */
if (Type == I2C_DATA) /* data byte */
{
AckBits = (1 << TWS5) | (1 << TWS3); /* 0x28 data & ACK */
NackBits = (1 << TWS5) | (1 << TWS4); /* 0x30 data & NACK */
}
else /* address byte */
{
if (Bits & 0b00000001) /* read address (bit #0 = 1) */
{
AckBits = (1 << TWS6); /* 0x40 SLA+R & ACK */
NackBits = (1 << TWS6) | (1 << TWS3); /* 0x48 SLA+R & NACK */
}
else /* write address (bit #0 = 0) */
{
AckBits = (1 << TWS4) | (1 << TWS3); /* 0x18 SLA+W & ACK */
NackBits = (1 << TWS5); /* 0x20 SLA+W & NACK */
}
}
/* load byte into data register */
TWDR = Bits;
/* send by clearing TWINT */
TWCR = (1 << TWINT) | (1 << TWEN);
/* wait for job done */
while (!(TWCR & (1 << TWINT))); /* wait for flag */
/* check result */
Bits = TWSR; /* get status */
/* filter status bits */
Bits &= (1 << TWS7) | (1 << TWS6) | (1 << TWS5) | (1 << TWS4) | (1 << TWS3);
if (Bits == AckBits) /* got expected bits for ACK */
{
Flag = I2C_ACK; /* signal ACK */
}
else if (Bits == NackBits) /* got expected bits for NACK */
{
Flag = I2C_NACK; /* signal NACK */
}
/* else 0x38: keep default "bus error" */
return Flag;
}
#ifdef I2C_RW
/*
* read byte (master from slave)
* - read byte (data) from slave
* - ACK/NACK to slave
* - byte to be read is stored in global I2C.Byte
*
* requires:
* - Type
* I2C_ACK for sending acknowledge
* I2C_NACK for sending not-acknowledge
*
* returns:
* - I2C_ERROR on bus error
* - I2C_OK on success
*/
uint8_t I2C_ReadByte(uint8_t Type)
{
uint8_t Flag = I2C_ERROR; /* return value */
uint8_t Bits; /* register bits */
uint8_t StatusBits; /* status register bits */
/*
* MCU's state maschine for TWI:
* - after addressing slave (read) data can be read
*/
/* receive by clearing TWINT */
if (Type == I2C_ACK) /* ACK */
{
/* read and respond with ACK */
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
StatusBits = (1 << TWS6) | (1 << TWS4); /* 0x50 data read & ACK */
}
else /* NACK */
{
/* read and respond with NACK */
TWCR = (1 << TWINT) | (1 << TWEN);
StatusBits = (1 << TWS6) | (1 << TWS4) | (1 << TWS3); /* 0x58 data read & NACK */
}
/* wait for job done */
while (!(TWCR & (1 << TWINT))); /* wait for flag */
/* check result */
Bits = TWSR; /* get status */
/* filter status bits */
Bits &= (1 << TWS7) | (1 << TWS6) | (1 << TWS5) | (1 << TWS4) | (1 << TWS3);
if (Bits == StatusBits) /* got expected bits */
{
Flag = I2C_OK; /* signal success */
}
/* else 0x38: keep default "bus error" */
return Flag;
}
#endif
/*
* create stop condition
*/
void I2C_Stop(void)
{
/*
* stop condition:
* - set TWINT to clear interrupt flag
* - set TWEN to enable TWI
* - set TWSTO for "stop"
*/
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
/* in this case TWINT won't be set after the job is done */
/*
* bus free time between Stop and next Start (T_BUF)
* - standard mode: min. 4.7µs
* - fast mode: min. 1.3µs
*/
#ifdef I2C_FAST_MODE
/* fast mode: min. 1.3µs */
wait5us();
#else
/* standard mode: min. 4.7µs */
wait20us();
#endif
}
#endif