summaryrefslogtreecommitdiff
path: root/utils/softi2c.c
blob: ab3e5a6faee51526e7941897c78dbafcf79262c7 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
//*****************************************************************************
//
// softi2c.c - Driver for the SoftI2C.
//
// Copyright (c) 2010-2014 Texas Instruments Incorporated.  All rights reserved.
// Software License Agreement
// 
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
// 
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
// 
// This is part of revision 2.1.0.12573 of the Tiva Utility Library.
//
//*****************************************************************************

//*****************************************************************************
//
//! \addtogroup softi2c_api
//! @{
//
//*****************************************************************************

#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "utils/softi2c.h"

//*****************************************************************************
//
// The states in the SoftI2C state machine.  The code depends upon the fact
// that the value of STATE_X1 is exactly one greater than STATE_X0 (for any
// value of X and for any following digit)...however there is no dependence on
// the values of STATE_Xn and STATE_Yn.
//
//*****************************************************************************
#define SOFTI2C_STATE_IDLE      0
#define SOFTI2C_STATE_START0    1
#define SOFTI2C_STATE_START1    2
#define SOFTI2C_STATE_START2    3
#define SOFTI2C_STATE_START3    4
#define SOFTI2C_STATE_START4    5
#define SOFTI2C_STATE_START5    6
#define SOFTI2C_STATE_START6    7
#define SOFTI2C_STATE_START7    8
#define SOFTI2C_STATE_ADDR0     9
#define SOFTI2C_STATE_ADDR1     10
#define SOFTI2C_STATE_ADDR2     11
#define SOFTI2C_STATE_ADDR3     12
#define SOFTI2C_STATE_SEND0     13
#define SOFTI2C_STATE_SEND1     14
#define SOFTI2C_STATE_SEND2     15
#define SOFTI2C_STATE_SEND3     16
#define SOFTI2C_STATE_RECV0     17
#define SOFTI2C_STATE_RECV1     18
#define SOFTI2C_STATE_RECV2     19
#define SOFTI2C_STATE_RECV3     20
#define SOFTI2C_STATE_STOP0     21
#define SOFTI2C_STATE_STOP1     22
#define SOFTI2C_STATE_STOP2     23
#define SOFTI2C_STATE_STOP3     24
#define SOFTI2C_STATE_STOP4     25

//*****************************************************************************
//
// The flags in the SoftI2C ui8Flags structure member.  The first four flags,
// RUN, START, STOP, and ACK, must match with the definitions of the
// SOFTI2C_CMD_* commands in softi2c.h.
//
//*****************************************************************************
#define SOFTI2C_FLAG_RUN        0
#define SOFTI2C_FLAG_START      1
#define SOFTI2C_FLAG_STOP       2
#define SOFTI2C_FLAG_ACK        3
#define SOFTI2C_FLAG_ADDR_ACK   5
#define SOFTI2C_FLAG_DATA_ACK   6
#define SOFTI2C_FLAG_RECEIVE    7

//*****************************************************************************
//
//! Performs the periodic update of the SoftI2C module.
//!
//! \param psI2C specifies the SoftI2C data structure.
//!
//! This function performs the periodic, time-based updates to the SoftI2C
//! module.  The transmission and reception of data over the SoftI2C link is
//! performed by the state machine in this function.
//!
//! This function must be called at four times the desired SoftI2C clock rate.
//! For example, to run the SoftI2C clock at 10 KHz, this function must be
//! called at a 40 KHz rate.
//!
//! \return None.
//
//*****************************************************************************
void
SoftI2CTimerTick(tSoftI2C *psI2C)
{
    //
    // Determine the current state of the state machine.
    //
    switch(psI2C->ui8State)
    {
        //
        // The state machine is idle.
        //
        case SOFTI2C_STATE_IDLE:
        {
            //
            // See if the START flag is set, indicating that a start condition
            // should be generated.
            //
            if(HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_START) == 1)
            {
                //
                // Based on the current state of the SCL and SDA pins, pick the
                // appropriate place within the state machine to begin the
                // start/repeated-start signalling.
                //
                if(HWREG(psI2C->ui32SCLGPIO) != 0)
                {
                    psI2C->ui8State = SOFTI2C_STATE_START4;
                }
                else if(HWREG(psI2C->ui32SDAGPIO) == 0)
                {
                    psI2C->ui8State = SOFTI2C_STATE_START0;
                }
                else
                {
                    psI2C->ui8State = SOFTI2C_STATE_START2;
                }
            }

            //
            // Otherwise, see if the RUN flag is set, indicating that a data
            // byte should be transferred.
            //
            else if(HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_RUN) == 1)
            {
                //
                // Start the transfer from the first bit.
                //
                psI2C->ui8CurrentBit = 0;

                //
                // See if a byte should be sent or received.
                //
                if(HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_RECEIVE) == 0)
                {
                    //
                    // A byte should be sent.
                    //
                    psI2C->ui8State = SOFTI2C_STATE_SEND0;
                }
                else
                {
                    //
                    // A byte should be received.  Clear out the receive data
                    // buffer in preparation for receiving the new byte.
                    //
                    psI2C->ui8Data = 0;
                    psI2C->ui8State = SOFTI2C_STATE_RECV0;
                }
            }

            //
            // Otherwise, see if the STOP flag is set, indicating that a stop
            // condition should be generated.
            //
            else if(HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_STOP) == 1)
            {
                //
                // Generate a stop condition.
                //
                psI2C->ui8State = SOFTI2C_STATE_STOP0;
            }

            //
            // See if the SoftI2C state machine has left the idle state.
            //
            if(psI2C->ui8State != SOFTI2C_STATE_IDLE)
            {
                //
                // The address and data ACK error flags should be cleared; they
                // will be set if appropriate while the current command is
                // being executed.
                //
                HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_ADDR_ACK) = 0;
                HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_DATA_ACK) = 0;
            }

            //
            // This state has been handled.
            //
            break;
        }

        //
        // The beginning of the start condition sequence when SDA and SCL are
        // low.  SDA must be driven high prior to driving SCL high so that a
        // repeated-start is generated, instead of a stop then start.
        //
        case SOFTI2C_STATE_START0:
        {
            //
            // Set SDA high.
            //
            HWREG(psI2C->ui32SDAGPIO) = 255;

            //
            // Advance to the next state.
            //
            psI2C->ui8State = SOFTI2C_STATE_START1;

            //
            // This state has been handled.
            //
            break;
        }

        //
        // Each of these states exists only to provide some timing delay in
        // order to conform with the signalling requirements of I2C.  This
        // depends upon STATE_Xn and STATE_X(n+1) being consecutively numbered.
        //
        case SOFTI2C_STATE_START1:
        case SOFTI2C_STATE_START3:
        case SOFTI2C_STATE_START5:
        case SOFTI2C_STATE_STOP1:
        case SOFTI2C_STATE_STOP3:
        {
            //
            // Advance to the next state.
            //
            psI2C->ui8State++;

            //
            // This state has been handled.
            //
            break;
        }

        //
        // In each of these states, SCL must be driven high.  This depends upon
        // STATE_Xn and STATE_X(n+1) being consecutively numbered.
        //
        case SOFTI2C_STATE_START2:
        case SOFTI2C_STATE_ADDR1:
        case SOFTI2C_STATE_SEND1:
        case SOFTI2C_STATE_RECV1:
        case SOFTI2C_STATE_STOP2:
        {
            //
            // Set SCL high.
            //
            HWREG(psI2C->ui32SCLGPIO) = 255;

            //
            // Advance to the next state.
            //
            psI2C->ui8State++;

            //
            // This state has been handled.
            //
            break;
        }

        //
        // In each of these states, SDA must be driven low.  This depends upon
        // STATE_Xn and STATE_X(n+1) being consecutively numbered.
        //
        case SOFTI2C_STATE_START4:
        case SOFTI2C_STATE_STOP0:
        {
            //
            // Set SDA low.
            //
            HWREG(psI2C->ui32SDAGPIO) = 0;

            //
            // Advance to the next state.
            //
            psI2C->ui8State++;

            //
            // This state has been handled.
            //
            break;
        }

        //
        // In this state, SCL must be driven low.
        //
        case SOFTI2C_STATE_START6:
        {
            //
            // Set SCL low.
            //
            HWREG(psI2C->ui32SCLGPIO) = 0;

            //
            // Advance to the next state.
            //
            psI2C->ui8State = SOFTI2C_STATE_START7;

            //
            // This state has been handled.
            //
            break;
        }

        //
        // In this state, the start condition has been generated.
        //
        case SOFTI2C_STATE_START7:
        {
            //
            // Start with the first bit of the address.
            //
            psI2C->ui8CurrentBit = 0;

            //
            // Advance to the address output state.
            //
            psI2C->ui8State = SOFTI2C_STATE_ADDR0;

            //
            // This state has been handled.
            //
            break;
        }

        //
        // In this state, the next bit of the slave address must be sent.
        //
        case SOFTI2C_STATE_ADDR0:
        {
            //
            // See if this is one of the first seven bits of the address phase.
            //
            if(psI2C->ui8CurrentBit < 7)
            {
                //
                // Write the next bit of the slave address to SDA.
                //
                HWREG(psI2C->ui32SDAGPIO) =
                    ((psI2C->ui8SlaveAddr &
                      (1 << (6 - psI2C->ui8CurrentBit))) ? 255 : 0);
            }

            //
            // Otherwise, see if this is the eight bit of the address phase
            // (which is the read/not write bit).
            //
            else if(psI2C->ui8CurrentBit == 7)
            {
                //
                // Write the read/not write bit to SDA.
                //
                HWREG(psI2C->ui32SDAGPIO) =
                    (HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_RECEIVE) ?
                     255 : 0);
            }

            //
            // Otherwise, this is the ninth bit of the address phase (in other
            // words, the ACK bit).
            //
            else
            {
                //
                // Change the SDA GPIO into an input so that the ACK or NAK
                // provided by the slave can be read.
                //
                MAP_GPIODirModeSet(psI2C->ui32SDAGPIO & 0xfffff000,
                                   (psI2C->ui32SDAGPIO & 0x00000fff) >> 2,
                                   GPIO_DIR_MODE_IN);
            }

            //
            // Advance to the next state.
            //
            psI2C->ui8State = SOFTI2C_STATE_ADDR1;

            //
            // This state has been handled.
            //
            break;
        }

        //
        // In each of these states, wait until SCL has gone high (it has been
        // released by the SoftI2C master, but may be held low by the slave).
        // This depends upon STATE_Xn and STATE_X(n+1) being consecutively
        // numbered.
        //
        case SOFTI2C_STATE_ADDR2:
        case SOFTI2C_STATE_SEND2:
        case SOFTI2C_STATE_RECV2:
        {
            //
            // See if SCL has gone high.
            //
            if(HWREG(psI2C->ui32SCLGPIO) != 0)
            {
                //
                // Advance to the next state now that SCL has gone high.
                //
                psI2C->ui8State++;
            }

            //
            // This state has been handled.
            //
            break;
        }

        //
        // In this state, SCL must be driven low.  If on the ninth bit of the
        // address transfer, the ACK/NAK status is read from the slave.
        //
        case SOFTI2C_STATE_ADDR3:
        {
            //
            // See if this is the ninth bit of the address phase (in other
            // words, the ACK bit).
            //
            if(psI2C->ui8CurrentBit == 8)
            {
                //
                // See if the SDA line is high.
                //
                if(HWREG(psI2C->ui32SDAGPIO) != 0)
                {
                    //
                    // Since the SDA line is high, the address byte has not
                    // been ACKed by any slave.
                    //
                    HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_ADDR_ACK) = 1;
                }

                //
                // Change the SDA GPIO back into an output.
                //
                MAP_GPIODirModeSet(psI2C->ui32SDAGPIO & 0xfffff000,
                                   (psI2C->ui32SDAGPIO & 0x00000fff) >> 2,
                                   GPIO_DIR_MODE_OUT);

                //
                // The start phase (start or repeated-start, plus the address
                // byte) have completed, so clear the START flag.
                //
                HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_START) = 0;

                //
                // See if the RUN flag is set, indicating that a data byte
                // should be transferred as well.
                //
                if(HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_RUN) == 1)
                {
                    //
                    // Reset the current bit to zero for the start of the data
                    // phase.
                    //
                    psI2C->ui8CurrentBit = 0;

                    //
                    // See if the data byte is being sent or received.
                    //
                    if(HWREGBITB(&(psI2C->ui8Flags),
                                 SOFTI2C_FLAG_RECEIVE) == 0)
                    {
                        //
                        // The data byte is being sent, so advance to the data
                        // send state.
                        //
                        psI2C->ui8State = SOFTI2C_STATE_SEND0;
                    }
                    else
                    {
                        //
                        // The data byte is being received, so clear the data
                        // buffer and advance to the data receive state.
                        //
                        psI2C->ui8Data = 0;
                        psI2C->ui8State = SOFTI2C_STATE_RECV0;
                    }
                }

                //
                // Otherwise, see if the STOP flag is set, indicating that a
                // stop condition should be generated.
                //
                else if(HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_STOP) == 1)
                {
                    //
                    // Advance to the stop state.
                    //
                    psI2C->ui8State = SOFTI2C_STATE_STOP0;
                }

                //
                // Otherwise, go to the idle state.
                //
                else
                {
                    //
                    // Since the requested operations have completed, set the
                    // SoftI2C ``interrupt''.
                    //
                    psI2C->ui8IntStatus = 1;

                    //
                    // Advance to the idle state.
                    //
                    psI2C->ui8State = SOFTI2C_STATE_IDLE;
                }
            }

            //
            // Otherwise, the next bit of the address should be transferred.
            //
            else
            {
                //
                // Increment the bit count.
                //
                psI2C->ui8CurrentBit++;

                //
                // Advance to the address tranfer state.
                //
                psI2C->ui8State = SOFTI2C_STATE_ADDR0;
            }

            //
            // Set SCL low.
            //
            HWREG(psI2C->ui32SCLGPIO) = 0;

            //
            // This state has been handled.
            //
            break;
        }

        //
        // In this state, the next bit of the data byte must be sent.
        //
        case SOFTI2C_STATE_SEND0:
        {
            //
            // See if this is one of the first eight bits of the data phase.
            //
            if(psI2C->ui8CurrentBit < 8)
            {
                //
                // Write the next bit of the data byte to SDA.
                //
                HWREG(psI2C->ui32SDAGPIO) =
                    ((psI2C->ui8Data &
                      (1 << (7 - psI2C->ui8CurrentBit))) ? 255 : 0);
            }

            //
            // Otherwise, this is the ninth bit of the data phase (in other
            // words, the ACK bit).
            //
            else
            {
                //
                // Change the SDA GPIO into an input so that the ACK or NAK
                // provided by the slave can be read.
                //
                MAP_GPIODirModeSet(psI2C->ui32SDAGPIO & 0xfffff000,
                                   (psI2C->ui32SDAGPIO & 0x00000fff) >> 2,
                                   GPIO_DIR_MODE_IN);
            }

            //
            // Advance to the next state.
            //
            psI2C->ui8State = SOFTI2C_STATE_SEND1;

            //
            // This state has been handled.
            //
            break;
        }

        //
        // In this state, SCL must be driven low.  If on the ninth bit of the
        // data transfer, the ACK/NAK status is read from the slave.
        //
        case SOFTI2C_STATE_SEND3:
        {
            //
            // See if this is the ninth bit of the data phase (in other words,
            // the ACK bit).
            //
            if(psI2C->ui8CurrentBit == 8)
            {
                //
                // See if the SDA line is high.
                //
                if(HWREG(psI2C->ui32SDAGPIO) != 0)
                {
                    //
                    // Since the SDA line is high, the data byte has not been
                    // ACKed by the slave.
                    //
                    HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_DATA_ACK) = 1;
                }

                //
                // Change the SDA GPIO back into an output.
                //
                MAP_GPIODirModeSet(psI2C->ui32SDAGPIO & 0xfffff000,
                                   (psI2C->ui32SDAGPIO & 0x00000fff) >> 2,
                                   GPIO_DIR_MODE_OUT);

                //
                // The data phase has completed, so clear the RUN flag.
                //
                HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_RUN) = 0;

                //
                // See if the STOP flag is set, indicating that a stop
                // condition should be generated.
                //
                if(HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_STOP) == 1)
                {
                    //
                    // Advance to the stop state.
                    //
                    psI2C->ui8State = SOFTI2C_STATE_STOP0;
                }

                //
                // Otherwise, go to the idle state.
                //
                else
                {
                    //
                    // Since the requested operations have completed, set the
                    // SoftI2C ``interrupt''.
                    //
                    psI2C->ui8IntStatus = 1;

                    //
                    // Advance to the idle state.
                    //
                    psI2C->ui8State = SOFTI2C_STATE_IDLE;
                }
            }

            //
            // Otherwise, the next bit of the data should be transferred.
            //
            else
            {
                //
                // Increment the bit count.
                //
                psI2C->ui8CurrentBit++;

                //
                // Advance to the data transmit state.
                //
                psI2C->ui8State = SOFTI2C_STATE_SEND0;
            }

            //
            // Set SCL low.
            //
            HWREG(psI2C->ui32SCLGPIO) = 0;

            //
            // This state has been handled.
            //
            break;
        }

        //
        // In this state, the next bit of the data byte must be received.
        //
        case SOFTI2C_STATE_RECV0:
        {
            //
            // See if this is the first bit of the data phase.
            //
            if(psI2C->ui8CurrentBit == 0)
            {
                //
                // Change the SDA GPIO into an input so that the data provided
                // by the slave can be read.
                //
                MAP_GPIODirModeSet(psI2C->ui32SDAGPIO & 0xfffff000,
                                   (psI2C->ui32SDAGPIO & 0x00000fff) >> 2,
                                   GPIO_DIR_MODE_IN);
            }

            //
            // Otherwise, see if this is the ninth bit of the data phase (in
            // other words, the ACK bit).
            //
            else if(psI2C->ui8CurrentBit == 8)
            {
                //
                // Change the SDA GPIO into an output so that the ACK bit can
                // be driven to the slave.
                //
                MAP_GPIODirModeSet(psI2C->ui32SDAGPIO & 0xfffff000,
                                   (psI2C->ui32SDAGPIO & 0x00000fff) >> 2,
                                   GPIO_DIR_MODE_OUT);

                //
                // See if this byte should be ACKed or NAKed.
                //
                if(HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_ACK) == 1)
                {
                    //
                    // Drive SDA low to ACK the data byte.
                    //
                    HWREG(psI2C->ui32SDAGPIO) = 0;
                }
                else
                {
                    //
                    // Allow SDA to get pulled high to NAK the data byte.
                    //
                    HWREG(psI2C->ui32SDAGPIO) = 255;
                }
            }

            //
            // Advance to the next state.
            //
            psI2C->ui8State = SOFTI2C_STATE_RECV1;

            //
            // This state has been handled.
            //
            break;
        }

        //
        // In this state, SCL must be driven low.  For the first eight bits of
        // the data transfer, the data bits are read from the slave.
        //
        case SOFTI2C_STATE_RECV3:
        {
            //
            // See if this is the ninth bit of the data phase (in other words,
            // the ACK bit).
            //
            if(psI2C->ui8CurrentBit == 8)
            {
                //
                // The data phase has completed, so clear the RUN flag.
                //
                HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_RUN) = 0;

                //
                // See if the STOP flag is set, indicating that a stop
                // condition should be generated.
                //
                if(HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_STOP) == 1)
                {
                    //
                    // Advance to the stop state.
                    //
                    psI2C->ui8State = SOFTI2C_STATE_STOP0;
                }

                //
                // Otherwise, go to the idle state.
                //
                else
                {
                    //
                    // Since the requested operations have completed, set the
                    // SoftI2C ``interrupt''.
                    //
                    psI2C->ui8IntStatus = 1;

                    //
                    // Advance to the idle state.
                    //
                    psI2C->ui8State = SOFTI2C_STATE_IDLE;
                }
            }

            //
            // Otherwise, the next bit of the data should be transferred.
            //
            else
            {
                //
                // Read the next bit of data from the SDA line.
                //
                psI2C->ui8Data |= (HWREG(psI2C->ui32SDAGPIO) ?
                                   (1 << (7 - psI2C->ui8CurrentBit)) : 0);

                //
                // Increment the bit count.
                //
                psI2C->ui8CurrentBit++;

                //
                // Advance to the data receive state.
                //
                psI2C->ui8State = SOFTI2C_STATE_RECV0;
            }

            //
            // Set SCL low.
            //
            HWREG(psI2C->ui32SCLGPIO) = 0;

            //
            // This state has been handled.
            //
            break;
        }

        //
        // In this state, SDA must be driven high to create the stop condition.
        //
        case SOFTI2C_STATE_STOP4:
        {
            //
            // Set SDA high to create the stop condition.
            //
            HWREG(psI2C->ui32SDAGPIO) = 255;

            //
            // The stop condition has been generated, so clear the STOP flag.
            //
            HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_STOP) = 0;

            //
            // Since the requested operations have completed, set the SoftI2C
            // ``interrupt''.
            //
            psI2C->ui8IntStatus = 1;

            //
            // Advance to the idle state.
            //
            psI2C->ui8State = SOFTI2C_STATE_IDLE;

            //
            // This state has been handled.
            //
            break;
        }
    }

    //
    // Call the "interrupt" callback while there are enabled "interrupts"
    // asserted.  By calling in a loop until the "interrupts" are no longer
    // asserted, this mimics the behavior of a real hardware implementation of
    // the I2C peripheral.
    //
    while(((psI2C->ui8IntStatus & psI2C->ui8IntMask) != 0) &&
          (psI2C->pfnIntCallback != 0))
    {
        //
        // Call the callback function.
        //
        psI2C->pfnIntCallback();
    }
}

//*****************************************************************************
//
//! Initializes the SoftI2C module.
//!
//! \param psI2C specifies the SoftI2C data structure.
//!
//! This function initializes operation of the SoftI2C module.  After
//! successful initialization of the SoftI2C module, the software I2C bus is in
//! the idle state.
//!
//! \return None.
//
//*****************************************************************************
void
SoftI2CInit(tSoftI2C *psI2C)
{
    //
    // Configure the SCL pin.
    //
    MAP_GPIODirModeSet(psI2C->ui32SCLGPIO & 0xfffff000,
                       (psI2C->ui32SCLGPIO & 0x00000fff) >> 2,
                       GPIO_DIR_MODE_OUT);
    MAP_GPIOPadConfigSet(psI2C->ui32SCLGPIO & 0xfffff000,
                         (psI2C->ui32SCLGPIO & 0x00000fff) >> 2,
                         GPIO_STRENGTH_8MA, GPIO_PIN_TYPE_OD);

    //
    // Set the SCL pin high.
    //
    HWREG(psI2C->ui32SCLGPIO) = 255;

    //
    // Configure the SDA pin.
    //
    MAP_GPIODirModeSet(psI2C->ui32SDAGPIO & 0xfffff000,
                       (psI2C->ui32SDAGPIO & 0x00000fff) >> 2,
                       GPIO_DIR_MODE_OUT);
    MAP_GPIOPadConfigSet(psI2C->ui32SDAGPIO & 0xfffff000,
                         (psI2C->ui32SDAGPIO & 0x00000fff) >> 2,
                         GPIO_STRENGTH_8MA, GPIO_PIN_TYPE_OD);

    //
    // Set the SDA pin high.
    //
    HWREG(psI2C->ui32SDAGPIO) = 255;

    //
    // The ``interrupt'' is not asserted at the start.
    //
    psI2C->ui8IntStatus = 0;

    //
    // There are no flags at the start.
    //
    psI2C->ui8Flags = 0;

    //
    // Start the SoftI2C state machine in the idle state.
    //
    psI2C->ui8State = SOFTI2C_STATE_IDLE;
}

//*****************************************************************************
//
//! Sets the callback used by the SoftI2C module.
//!
//! \param psI2C specifies the SoftI2C data structure.
//! \param pfnCallback is a pointer to the callback function.
//!
//! This function sets the address of the callback function that is called when
//! there is an ``interrupt'' produced by the SoftI2C module.
//!
//! \return None.
//
//*****************************************************************************
void
SoftI2CCallbackSet(tSoftI2C *psI2C, void (*pfnCallback)(void))
{
    //
    // Save the callback function address.
    //
    psI2C->pfnIntCallback = pfnCallback;
}

//*****************************************************************************
//
//! Sets the GPIO pin to be used as the SoftI2C SCL signal.
//!
//! \param psI2C specifies the SoftI2C data structure.
//! \param ui32Base is the base address of the GPIO module.
//! \param ui8Pin is the bit-packed representation of the pin to use.
//!
//! This function sets the GPIO pin that is used for the SoftI2C SCL signal.
//!
//! The pin is specified using a bit-packed byte, where bit 0 of the byte
//! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
//!
//! \return None.
//
//*****************************************************************************
void
SoftI2CSCLGPIOSet(tSoftI2C *psI2C, uint32_t ui32Base, uint8_t ui8Pin)
{
    //
    // Save the base address and pin for the SCL signal.
    //
    psI2C->ui32SCLGPIO = ui32Base + (ui8Pin << 2);
}

//*****************************************************************************
//
//! Sets the GPIO pin to be used as the SoftI2C SDA signal.
//!
//! \param psI2C specifies the SoftI2C data structure.
//! \param ui32Base is the base address of the GPIO module.
//! \param ui8Pin is the bit-packed representation of the pin to use.
//!
//! This function sets the GPIO pin that is used for the SoftI2C SDA signal.
//!
//! The pin is specified using a bit-packed byte, where bit 0 of the byte
//! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
//!
//! \return None.
//
//*****************************************************************************
void
SoftI2CSDAGPIOSet(tSoftI2C *psI2C, uint32_t ui32Base, uint8_t ui8Pin)
{
    //
    // Save the base address and pin for the SDA signal.
    //
    psI2C->ui32SDAGPIO = ui32Base + (ui8Pin << 2);
}

//*****************************************************************************
//
//! Enables the SoftI2C ``interrupt''.
//!
//! \param psI2C specifies the SoftI2C data structure.
//!
//! Enables the SoftI2C ``interrupt'' source.
//!
//! \return None.
//
//*****************************************************************************
void
SoftI2CIntEnable(tSoftI2C *psI2C)
{
    //
    // Enable the master interrupt.
    //
    psI2C->ui8IntMask = 1;
}

//*****************************************************************************
//
//! Disables the SoftI2C ``interrupt''.
//!
//! \param psI2C specifies the SoftI2C data structure.
//!
//! Disables the SoftI2C ``interrupt'' source.
//!
//! \return None.
//
//*****************************************************************************
void
SoftI2CIntDisable(tSoftI2C *psI2C)
{
    //
    // Disable the master interrupt.
    //
    psI2C->ui8IntMask = 0;
}

//*****************************************************************************
//
//! Gets the current SoftI2C ``interrupt'' status.
//!
//! \param psI2C specifies the SoftI2C data structure.
//! \param bMasked is \b false if the raw ``interrupt'' status is requested and
//! \b true if the masked ``interrupt'' status is requested.
//!
//! This returns the ``interrupt'' status for the SoftI2C module.  Either the
//! raw ``interrupt'' status or the status of ``interrupts'' that are allowed
//! to reflect to the processor can be returned.
//!
//! \return The current interrupt status, returned as \b true if active
//! or \b false if not active.
//
//*****************************************************************************
bool
SoftI2CIntStatus(tSoftI2C *psI2C, bool bMasked)
{
    //
    // Return either the interrupt status or the raw interrupt status as
    // requested.
    //
    if(bMasked)
    {
        return((psI2C->ui8IntStatus & psI2C->ui8IntMask) ? true : false);
    }
    else
    {
        return(psI2C->ui8IntStatus ? true : false);
    }
}

//*****************************************************************************
//
//! Clears the SoftI2C ``interrupt''.
//!
//! \param psI2C specifies the SoftI2C data structure.
//!
//! The SoftI2C ``interrupt'' source is cleared, so that it no longer asserts.
//! This function must be called in the ``interrupt'' handler to keep it from
//! being called again immediately on exit.
//!
//! \return None.
//
//*****************************************************************************
void
SoftI2CIntClear(tSoftI2C *psI2C)
{
    //
    // Clear the SoftI2C interrupt source.
    //
    psI2C->ui8IntStatus = 0;
}

//*****************************************************************************
//
//! Sets the address that the SoftI2C module places on the bus.
//!
//! \param psI2C specifies the SoftI2C data structure.
//! \param ui8SlaveAddr 7-bit slave address
//! \param bReceive flag indicating the type of communication with the slave.
//!
//! This function sets the address that the SoftI2C module places on the bus
//! when initiating a transaction.  When the \e bReceive parameter is set to
//! \b true, the address indicates that the SoftI2C moudle is initiating a read
//! from the slave; otherwise the address indicates that the SoftI2C module is
//! initiating a write to the slave.
//!
//! \return None.
//
//*****************************************************************************
void
SoftI2CSlaveAddrSet(tSoftI2C *psI2C, uint8_t ui8SlaveAddr,
                    bool bReceive)
{
    //
    // Check the arguments.
    //
    ASSERT(!(ui8SlaveAddr & 0x80));

    //
    // Set the address of the slave with which the master will communicate.
    //
    psI2C->ui8SlaveAddr = ui8SlaveAddr;

    //
    // Set a flag to indicate if this is a transmit or receive.
    //
    if(bReceive)
    {
        HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_RECEIVE) = 1;
    }
    else
    {
        HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_RECEIVE) = 0;
    }
}

//*****************************************************************************
//
//! Indicates whether or not the SoftI2C module is busy.
//!
//! \param psI2C specifies the SoftI2C data structure.
//!
//! This function returns an indication of whether or not the SoftI2C module is
//! busy transmitting or receiving data.
//!
//! \return Returns \b true if the SoftI2C module is busy; otherwise, returns
//! \b false.
//
//*****************************************************************************
bool
SoftI2CBusy(tSoftI2C *psI2C)
{
    //
    // Return the busy status.
    //
    if(psI2C->ui8State != SOFTI2C_STATE_IDLE)
    {
        return(true);
    }
    else
    {
        return(false);
    }
}

//*****************************************************************************
//
//! Controls the state of the SoftI2C module.
//!
//! \param psI2C specifies the SoftI2C data structure.
//! \param ui32Cmd command to be issued to the SoftI2C module.
//!
//! This function is used to control the state of the SoftI2C module send and
//! receive operations.  The \e ui8Cmd parameter can be one of the following
//! values:
//!
//! - \b SOFTI2C_CMD_SINGLE_SEND
//! - \b SOFTI2C_CMD_SINGLE_RECEIVE
//! - \b SOFTI2C_CMD_BURST_SEND_START
//! - \b SOFTI2C_CMD_BURST_SEND_CONT
//! - \b SOFTI2C_CMD_BURST_SEND_FINISH
//! - \b SOFTI2C_CMD_BURST_SEND_ERROR_STOP
//! - \b SOFTI2C_CMD_BURST_RECEIVE_START
//! - \b SOFTI2C_CMD_BURST_RECEIVE_CONT
//! - \b SOFTI2C_CMD_BURST_RECEIVE_FINISH
//! - \b SOFTI2C_CMD_BURST_RECEIVE_ERROR_STOP
//!
//! \return None.
//
//*****************************************************************************
void
SoftI2CControl(tSoftI2C *psI2C, uint32_t ui32Cmd)
{
    //
    // Check the arguments.
    //
    ASSERT((ui32Cmd == SOFTI2C_CMD_SINGLE_SEND) ||
           (ui32Cmd == SOFTI2C_CMD_SINGLE_RECEIVE) ||
           (ui32Cmd == SOFTI2C_CMD_BURST_SEND_START) ||
           (ui32Cmd == SOFTI2C_CMD_BURST_SEND_CONT) ||
           (ui32Cmd == SOFTI2C_CMD_BURST_SEND_FINISH) ||
           (ui32Cmd == SOFTI2C_CMD_BURST_SEND_ERROR_STOP) ||
           (ui32Cmd == SOFTI2C_CMD_BURST_RECEIVE_START) ||
           (ui32Cmd == SOFTI2C_CMD_BURST_RECEIVE_CONT) ||
           (ui32Cmd == SOFTI2C_CMD_BURST_RECEIVE_FINISH) ||
           (ui32Cmd == SOFTI2C_CMD_BURST_RECEIVE_ERROR_STOP));

    //
    // Send the command.
    //
    psI2C->ui8Flags = (psI2C->ui8Flags & 0xf0) | ui32Cmd;
}

//*****************************************************************************
//
//! Gets the error status of the SoftI2C module.
//!
//! \param psI2C specifies the SoftI2C data structure.
//!
//! This function is used to obtain the error status of the SoftI2C module send
//! and receive operations.
//!
//! \return Returns the error status, as one of \b SOFTI2C_ERR_NONE,
//! \b SOFTI2C_ERR_ADDR_ACK, or \b SOFTI2C_ERR_DATA_ACK.
//
//*****************************************************************************
uint32_t
SoftI2CErr(tSoftI2C *psI2C)
{
    //
    // If the SoftI2C is busy, there is no error to report.
    //
    if(psI2C->ui8State != SOFTI2C_STATE_IDLE)
    {
        return(SOFTI2C_ERR_NONE);
    }

    //
    // Return any errors that may have occurred.
    //
    return((HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_ADDR_ACK) ?
            SOFTI2C_ERR_ADDR_ACK : 0) |
           (HWREGBITB(&(psI2C->ui8Flags), SOFTI2C_FLAG_DATA_ACK) ?
            SOFTI2C_ERR_DATA_ACK : 0));
}

//*****************************************************************************
//
//! Transmits a byte from the SoftI2C module.
//!
//! \param psI2C specifies the SoftI2C data structure.
//! \param ui8Data data to be transmitted from the SoftI2C module.
//!
//! This function places the supplied data into SoftI2C module in preparation
//! for being transmitted via an appropriate call to SoftI2CControl().
//!
//! \return None.
//
//*****************************************************************************
void
SoftI2CDataPut(tSoftI2C *psI2C, uint8_t ui8Data)
{
    //
    // Write the byte.
    //
    psI2C->ui8Data = ui8Data;
}

//*****************************************************************************
//
//! Receives a byte that has been sent to the SoftI2C module.
//!
//! \param psI2C specifies the SoftI2C data structure.
//!
//! This function reads a byte of data from the SoftI2C module that was
//! received as a result of an appropriate call to SoftI2CControl().
//!
//! \return Returns the byte received by the SoftI2C module, cast as an
//! uint32_t.
//
//*****************************************************************************
uint32_t
SoftI2CDataGet(tSoftI2C *psI2C)
{
    //
    // Read a byte.
    //
    return(psI2C->ui8Data);
}

//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************