-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2cm_drv.c
2256 lines (2073 loc) · 74.5 KB
/
i2cm_drv.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
//*****************************************************************************
//
// i2cm_drv.c - Interrupt-driven I2C master driver.
//
// Copyright (c) 2012-2015 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.2.111 of the Tiva Firmware Development Package.
//
//*****************************************************************************
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_i2c.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/i2c.h"
#include "driverlib/interrupt.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "sensorlib/i2cm_drv.h"
//*****************************************************************************
//
//! \addtogroup i2cm_drv_api
//! @{
//
//*****************************************************************************
//*****************************************************************************
//
// The states in the interrupt handler state machine.
//
//*****************************************************************************
#define STATE_IDLE 0
#define STATE_WRITE_NEXT 1
#define STATE_WRITE_FINAL 2
#define STATE_WRITE_PAUSE 3
#define STATE_READ_ONE 4
#define STATE_READ_FIRST 5
#define STATE_READ_NEXT 6
#define STATE_READ_FINAL 7
#define STATE_READ_PAUSE 8
#define STATE_READ_WAIT 9
#define STATE_CALLBACK 10
//*****************************************************************************
//
// The states in the I2C read-modify-write state machine.
//
//*****************************************************************************
#define I2CM_RMW_STATE_IDLE 0
#define I2CM_RMW_STATE_READ 1
#define I2CM_RMW_STATE_WRITE 2
//*****************************************************************************
//
//! Writes data to an I2C device.
//!
//! \param psInst is a pointer to the I2C master instance data.
//! \param ui8Addr is the address of the I2C device to access.
//! \param pui8Data is a pointer to the data buffer to be written.
//! \param ui16Count is the number of bytes to be written.
//! \param pfnCallback is the function to be called when the write has
//! completed (can be \b NULL if a callback is not required).
//! \param pvCallbackData is a pointer that is passed to the callback function.
//!
//! This function adds an I2C write to the queue of commands to be sent. If
//! successful, the I2C write is then performed in the background using the
//! interrupt handler. When the write is complete, the callback function, if
//! provided, is called in the context of the I2C master interrupt handler.
//!
//! The first byte of the data buffer contains the I2C address of the device to
//! access, and the remaining \e ui16Count bytes contain the data to be written
//! to the device. The \e ui16Count parameter can be zero if there are no
//! bytes to be written.
//!
//! \return Returns 1 if the command was successfully added to the queue and 0
//! if it was not.
//
// The extern here provides a non-inline definition for this function to handle
// the case where the compiler chooses not to inline the function (which is a
// valid choice for the compiler to make).
//
//*****************************************************************************
extern uint_fast8_t I2CMWrite(tI2CMInstance *psInst, uint_fast8_t ui8Addr,
const uint8_t *pui8Data, uint_fast16_t ui16Count,
tSensorCallback pfnCallback,
void *pvCallbackData);
//*****************************************************************************
//
//! Reads data from an I2C device.
//!
//! \param psInst is a pointer to the I2C master instance data.
//! \param ui8Addr is the address of the I2C device to access.
//! \param pui8WriteData is a pointer to the data buffer to be written.
//! \param ui16WriteCount is the number of bytes to be written.
//! \param pui8ReadData is a pointer to the buffer to be filled with the read
//! data.
//! \param ui16ReadCount is the number of bytes to be read.
//! \param pfnCallback is the function to be called when the transfer has
//! completed (can be \b NULL if a callback is not required).
//! \param pvCallbackData is a pointer that is passed to the callback function.
//!
//! This function adds an I2C read to the queue of commands to be sent. If
//! successful, the I2C read is then performed in the background using the
//! interrupt handler. When the read is complete, the callback function, if
//! provided, is called in the context of the I2C master interrupt handler.
//!
//! The first byte of \e pui8WriteData contains the I2C address of the device
//! to access, the next \e ui16WriteCount bytes contains the data to be written
//! to the device. The data read from the device is written into the first
//! \e ui16ReadCount bytes of \e pui8ReadData. The \e ui16WriteCount or
//! \e ui16ReadCount parameters can be zero if there are no bytes to be read or
//! written. The write bytes are sent to the device first, and then the read
//! bytes are read from the device afterward.
//!
//! \return Returns 1 if the command was successfully added to the queue and 0
//! if it was not.
//
// The extern here provides a non-inline definition for this function to handle
// the case where the compiler chooses not to inline the function (which is a
// valid choice for the compiler to make).
//
//*****************************************************************************
extern uint_fast8_t I2CMRead(tI2CMInstance *psInst, uint_fast8_t ui8Addr,
const uint8_t *pui8WriteData,
uint_fast16_t ui16WriteCount,
uint8_t *pui8ReadData,
uint_fast16_t ui16ReadCount,
tSensorCallback pfnCallback,
void *pvCallbackData);
//*****************************************************************************
//
//! Writes data in batches to an I2C device.
//!
//! \param psInst is a pointer to the I2C master instance data.
//! \param ui8Addr is the address of the I2C device to access.
//! \param pui8Data is a pointer to the data buffer to be written.
//! \param ui16Count is the number of bytes to be written.
//! \param ui16BatchSize is the number of bytes in each write batch.
//! \param pfnCallback is the function to be called when the transfer has
//! completed (can be \b NULL if a callback is not required).
//! \param pvCallbackData is a pointer that is passed to the callback function.
//!
//! This function adds an I2C write to the queue of commands to be sent. If
//! successful, the I2C write is then performed in the background using the
//! interrupt handler. When the write is complete, the callback function, if
//! provided, is called in the context of the I2C master interrupt handler.
//!
//! The first byte of the data buffer contains the I2C address of the device to
//! access, and the remaining \e ui16Count bytes contain the data to be written
//! to the device. The \e ui16Count parameter can be zero if there are no
//! bytes to be written.
//!
//! The data is written in batches of \e ui16WriteBatchSize. The callback
//! function is called after each batch is written, and I2CMTransferResume()
//! must be called when the next batch should be written.
//!
//! \return Returns 1 if the command was successfully added to the queue and 0
//! if it was not.
//
// The extern here provides a non-inline definition for this function to handle
// the case where the compiler chooses not to inline the function (which is a
// valid choice for the compiler to make).
//
//*****************************************************************************
extern uint_fast8_t I2CMWriteBatched(tI2CMInstance *psInst,
uint_fast8_t ui8Addr,
const uint8_t *pui8Data,
uint_fast16_t ui16Count,
uint_fast16_t ui16BatchSize,
tSensorCallback pfnCallback,
void *pvCallbackData);
//*****************************************************************************
//
//! Reads data in batches from an I2C device.
//!
//! \param psInst is a pointer to the I2C master instance data.
//! \param ui8Addr is the address of the I2C device to access.
//! \param pui8WriteData is a pointer to the data buffer to be written.
//! \param ui16WriteCount is the number of bytes to be written.
//! \param ui16WriteBatchSize is the number of bytes in each write batch.
//! \param pui8ReadData is a pointer to the buffer to be filled with the read
//! data.
//! \param ui16ReadCount is the number of bytes to be read.
//! \param ui16ReadBatchSize is the number of bytes in each read batch.
//! \param pfnCallback is the function to be called when the transfer has
//! completed (can be \b NULL if a callback is not required).
//! \param pvCallbackData is a pointer that is passed to the callback function.
//!
//! This function adds an I2C read to the queue of commands to be sent. If
//! successful, the I2C read is then performed in the background using the
//! interrupt handler. When the read is complete, the callback function, if
//! provided, is called in the context of the I2C master interrupt handler.
//!
//! The first byte of \e pui8WriteData contains the I2C address of the device
//! to access, the next \e ui16WriteCount bytes contains the data to be written
//! to the device. The data read from the device is written into the first
//! \e ui16ReadCount bytes of \e pui8ReadData. The \e ui16WriteCount or
//! \e ui16ReadCount parameters can be zero if there are no bytes to be read or
//! written. The write bytes are sent to the device first, and then the read
//! bytes are read from the device afterward.
//!
//! The data is written in batches of \e ui16WriteBatchSize. The callback
//! function is called after each batch is written, and I2CMTransferResume()
//! must be called when the next batch should be written.
//!
//! The data is read in batches of \e ui16ReadBatchSize. The callback function
//! is called after each batch is read, and I2CMTransferResume() must be called
//! when the next batch should be read.
//!
//! \return Returns 1 if the command was successfully added to the queue and 0
//! if it was not.
//
// The extern here provides a non-inline definition for this function to handle
// the case where the compiler chooses not to inline the function (which is a
// valid choice for the compiler to make).
//
//*****************************************************************************
extern uint_fast8_t I2CMReadBatched(tI2CMInstance *psInst,
uint_fast8_t ui8Addr,
const uint8_t *pui8WriteData,
uint_fast16_t ui16WriteCount,
uint_fast16_t ui16WriteBatchSize,
uint8_t *pui8ReadData,
uint_fast16_t ui16ReadCount,
uint_fast16_t ui16ReadBatchSize,
tSensorCallback pfnCallback,
void *pvCallbackData);
//*****************************************************************************
//
//! Performs a read-modify-write of 16 bits of big-endian data in an I2C
//! device.
//!
//! \param psInst is a pointer to the read-modify-write instance data.
//! \param psI2CInst is a pointer to the I2C master instance data.
//! \param ui8Addr is the address of the I2C device to access.
//! \param ui8Reg is the register in the I2C device to access.
//! \param ui16Mask is the mask indicating the register bits that should be
//! maintained.
//! \param ui16Value is the value indicating the new value for the register
//! bits that are not maintained.
//! \param pfnCallback is the function to be called when the write has
//! completed (can be \b NULL if a callback is not required).
//! \param pvCallbackData is a pointer that is passed to the callback function.
//!
//! This function initiates a read-modify-write transaction of 16 bits of
//! big-endian data in an I2C device. The modify portion of the operation is
//! performed by AND-ing the register value with \e ui16Mask and then OR-ing
//! the result with \e ui16Value. When the read-modify-write is complete, the
//! callback function, if provided, is called in the context of the I2C master
//! interrupt handler.
//!
//! If the mask (in \e ui16Mask) is zero, then none of the bits in the current
//! register value are maintained. In this case, the read portion of the
//! read-modify-write is bypassed, and the new register value (in \e ui16Value)
//! is directly written to the I2C device.
//!
//! \return Returns 1 if the command was successfully added to the queue and 0
//! if it was not.
//
// The extern here provides a non-inline definition for this function to handle
// the case where the compiler chooses not to inline the function (which is a
// valid choice for the compiler to make).
//
//*****************************************************************************
extern uint_fast8_t I2CMReadModifyWrite16BE(tI2CMReadModifyWrite16 *psInst,
tI2CMInstance *psI2CInst,
uint_fast8_t ui8Addr,
uint_fast8_t ui8Reg,
uint_fast16_t ui16Mask,
uint_fast16_t ui16Value,
tSensorCallback *pfnCallback,
void *pvCallbackData);
//*****************************************************************************
//
// This function handles the idle state of the I2C master state machine.
//
//*****************************************************************************
static void
I2CMStateIdle(tI2CMInstance *psInst, tI2CMCommand *pCommand)
{
//
// Do nothing if there is not another transfer in the queue.
//
if(psInst->ui8ReadPtr == psInst->ui8WritePtr)
{
return;
}
//
// See if there is any data to be written.
//
if(pCommand->ui16WriteCount != 0)
{
//
// Set the slave address and indicate a write.
//
MAP_I2CMasterSlaveAddrSet(psInst->ui32Base, pCommand->ui8Addr, false);
//
// Place the first data byte to be written in the data register.
//
MAP_I2CMasterDataPut(psInst->ui32Base, pCommand->pui8WriteData[0]);
//
// See if there is just a single byte to be written and no bytes to be
// read.
//
if((pCommand->ui16WriteCount == 1) && (pCommand->ui16ReadCount == 0))
{
//
// Perform a single byte send.
//
MAP_I2CMasterControl(psInst->ui32Base, I2C_MASTER_CMD_SINGLE_SEND);
//
// The next state is the callback state.
//
psInst->ui8State = STATE_CALLBACK;
}
//
// Otherwise, see if there is just a single byte to be written and at
// least one byte to be read.
//
else if(pCommand->ui16WriteCount == 1)
{
//
// Perform a single send, writing the first byte as the only byte.
//
MAP_I2CMasterControl(psInst->ui32Base,
I2C_MASTER_CMD_BURST_SEND_START);
//
// Set the next state of the interrupt state machine based on the
// number of bytes to read.
//
psInst->ui8State = ((pCommand->ui16ReadCount == 1) ?
STATE_READ_ONE : STATE_READ_FIRST);
}
//
// Otherwise, there is more than one byte to be written.
//
else
{
//
// Start the burst cycle, writing the first byte.
//
MAP_I2CMasterControl(psInst->ui32Base,
I2C_MASTER_CMD_BURST_SEND_START);
//
// Set the index to indicate that the first byte has been
// transmitted.
//
psInst->ui16Index = 1;
//
// Set the next state of the interrupt state machine based on the
// number of bytes to write.
//
psInst->ui8State = ((pCommand->ui16WriteCount != 2) ?
STATE_WRITE_NEXT : STATE_WRITE_FINAL);
}
}
else
{
//
// Set the slave address and indicate a read.
//
MAP_I2CMasterSlaveAddrSet(psInst->ui32Base, pCommand->ui8Addr, true);
//
// Set the index to indicate that the first byte is being read.
//
psInst->ui16Index = 0;
//
// See if there is just a single byte to be read.
//
if(pCommand->ui16ReadCount == 1)
{
//
// Perform a single byte read.
//
MAP_I2CMasterControl(psInst->ui32Base,
I2C_MASTER_CMD_SINGLE_RECEIVE);
//
// The next state is the wait for final read state.
//
psInst->ui8State = STATE_READ_WAIT;
}
else
{
//
// Start the burst receive.
//
MAP_I2CMasterControl(psInst->ui32Base,
I2C_MASTER_CMD_BURST_RECEIVE_START);
//
// Set the next state appropriately. If the read count is two, the
// next state must finish the transaction. If it is greater than
// two, the burst read must be continued.
//
psInst->ui8State = ((pCommand->ui16ReadCount == 2) ?
STATE_READ_FINAL : STATE_READ_NEXT);
}
}
}
//*****************************************************************************
//
// This function handles the write next state of the I2C master state machine.
//
//*****************************************************************************
static void
I2CMStateWriteNext(tI2CMInstance *psInst, tI2CMCommand *pCommand)
{
//
// See if the write batch has been sent.
//
if(psInst->ui16Index == pCommand->ui16WriteBatchSize)
{
//
// Move to the write pause state.
//
psInst->ui8State = STATE_WRITE_PAUSE;
//
// Call the callback function.
//
if(pCommand->pfnCallback)
{
pCommand->pfnCallback(pCommand->pvCallbackData,
I2CM_STATUS_BATCH_DONE);
}
}
else
{
//
// Write the next byte to the data register.
//
MAP_I2CMasterDataPut(psInst->ui32Base,
pCommand->pui8WriteData[psInst->ui16Index]);
psInst->ui16Index++;
//
// Continue the burst write.
//
MAP_I2CMasterControl(psInst->ui32Base, I2C_MASTER_CMD_BURST_SEND_CONT);
//
// If there is one byte left, set the next state to the final write
// state.
//
if((pCommand->ui16WriteCount - psInst->ui16Index) == 1)
{
psInst->ui8State = STATE_WRITE_FINAL;
}
}
}
//*****************************************************************************
//
// This function handles the write final state of the I2C master state machine.
//
//*****************************************************************************
static void
I2CMStateWriteFinal(tI2CMInstance *psInst, tI2CMCommand *pCommand)
{
//
// See if the write batch has been sent.
//
if(psInst->ui16Index == pCommand->ui16WriteBatchSize)
{
//
// Move to the write pause state.
//
psInst->ui8State = STATE_WRITE_PAUSE;
//
// Call the callback function.
//
if(pCommand->pfnCallback)
{
pCommand->pfnCallback(pCommand->pvCallbackData,
I2CM_STATUS_BATCH_DONE);
}
}
else
{
//
// Write the final byte to the data register.
//
MAP_I2CMasterDataPut(psInst->ui32Base,
pCommand->pui8WriteData[psInst->ui16Index]);
//
// See if there is data to be read after this byte is written.
//
if(pCommand->ui16ReadCount == 0)
{
//
// Finish the burst write.
//
MAP_I2CMasterControl(psInst->ui32Base,
I2C_MASTER_CMD_BURST_SEND_FINISH);
//
// The next state is the callback state.
//
psInst->ui8State = STATE_CALLBACK;
}
else
{
//
// Finish the burst write.
//
MAP_I2CMasterControl(psInst->ui32Base,
I2C_MASTER_CMD_BURST_SEND_CONT);
//
// Set the next state of the interrupt state machine based on the
// number of bytes to read.
//
psInst->ui8State = ((pCommand->ui16ReadCount == 1) ?
STATE_READ_ONE : STATE_READ_FIRST);
}
}
}
//*****************************************************************************
//
// This function handles the write pause state of the I2C master state machine.
//
//*****************************************************************************
static void
I2CMStateWritePause(tI2CMInstance *psInst, tI2CMCommand *pCommand)
{
//
// Decrement the write count by the batch size.
//
pCommand->ui16WriteCount -= pCommand->ui16WriteBatchSize;
//
// Write the next byte to the data register.
//
MAP_I2CMasterDataPut(psInst->ui32Base, pCommand->pui8WriteData[0]);
//
// Set the index to indicate that the first byte has been transmitted.
//
psInst->ui16Index = 1;
//
// See if there is more than one byte left to be written.
//
if((pCommand->ui16WriteCount - psInst->ui16Index) == 0)
{
//
// See if there is data to be read after this byte is written.
//
if(pCommand->ui16ReadCount == 0)
{
//
// Finish the burst write.
//
MAP_I2CMasterControl(psInst->ui32Base,
I2C_MASTER_CMD_BURST_SEND_FINISH);
//
// The next state is the callback state.
//
psInst->ui8State = STATE_CALLBACK;
}
else
{
//
// Finish the burst write.
//
MAP_I2CMasterControl(psInst->ui32Base,
I2C_MASTER_CMD_BURST_SEND_CONT);
//
// Set the next state of the interrupt state machine based on the
// number of bytes to read.
//
psInst->ui8State = ((pCommand->ui16ReadCount == 1) ?
STATE_READ_ONE : STATE_READ_FIRST);
}
}
else
{
//
// Continue the burst write.
//
MAP_I2CMasterControl(psInst->ui32Base, I2C_MASTER_CMD_BURST_SEND_CONT);
//
// The next state is the write next state.
//
if((pCommand->ui16WriteCount - psInst->ui16Index) == 1)
{
psInst->ui8State = STATE_WRITE_FINAL;
}
else
{
psInst->ui8State = STATE_WRITE_NEXT;
}
}
}
//*****************************************************************************
//
// This function handles the read one state of the I2C master state machine.
//
//*****************************************************************************
static void
I2CMStateReadOne(tI2CMInstance *psInst, tI2CMCommand *pCommand)
{
//
// Put the I2C master into receive mode.
//
MAP_I2CMasterSlaveAddrSet(psInst->ui32Base, pCommand->ui8Addr, true);
//
// Perform a single byte read.
//
MAP_I2CMasterControl(psInst->ui32Base, I2C_MASTER_CMD_SINGLE_RECEIVE);
//
// Set the index to indicate that the first byte is being read.
//
psInst->ui16Index = 0;
//
// The next state is the wait for final read state.
//
psInst->ui8State = STATE_READ_WAIT;
}
//*****************************************************************************
//
// This function handles the read first state of the I2C master state machine.
//
//*****************************************************************************
static void
I2CMStateReadFirst(tI2CMInstance *psInst, tI2CMCommand *pCommand)
{
//
// Put the I2C master into receive mode.
//
MAP_I2CMasterSlaveAddrSet(psInst->ui32Base, pCommand->ui8Addr, true);
//
// Start the burst receive.
//
MAP_I2CMasterControl(psInst->ui32Base, I2C_MASTER_CMD_BURST_RECEIVE_START);
//
// Set the index to indicate that the first byte is being read.
//
psInst->ui16Index = 0;
//
// Set the next state appropriately. If the count is greater than two it
// is the middle of the burst read. If exactly two, the next state must
// finish the transaction.
//
psInst->ui8State = ((pCommand->ui16ReadCount == 2) ?
STATE_READ_FINAL : STATE_READ_NEXT);
}
//*****************************************************************************
//
// This function handles the read next state of the I2C master state machine.
//
//*****************************************************************************
static void
I2CMStateReadNext(tI2CMInstance *psInst, tI2CMCommand *pCommand)
{
//
// Read the received character.
//
pCommand->pui8ReadData[psInst->ui16Index] =
MAP_I2CMasterDataGet(psInst->ui32Base);
psInst->ui16Index++;
//
// See if the read batch has been filled.
//
if(psInst->ui16Index == pCommand->ui16ReadBatchSize)
{
//
// Move to the read pause state.
//
psInst->ui8State = STATE_READ_PAUSE;
//
// Call the callback function.
//
if(pCommand->pfnCallback)
{
pCommand->pfnCallback(pCommand->pvCallbackData,
I2CM_STATUS_BATCH_READY);
}
}
else
{
//
// Continue the burst read.
//
MAP_I2CMasterControl(psInst->ui32Base,
I2C_MASTER_CMD_BURST_RECEIVE_CONT);
//
// If there are two characters left to be read, make the next state be
// the end of burst read state.
//
if((pCommand->ui16ReadCount - psInst->ui16Index) == 2)
{
psInst->ui8State = STATE_READ_FINAL;
}
}
}
//*****************************************************************************
//
// This function handles the read final state of the I2C master state machine.
//
//*****************************************************************************
static void
I2CMStateReadFinal(tI2CMInstance *psInst, tI2CMCommand *pCommand)
{
//
// Read the received character.
//
pCommand->pui8ReadData[psInst->ui16Index] =
MAP_I2CMasterDataGet(psInst->ui32Base);
psInst->ui16Index++;
//
// See if the read batch has been filled.
//
if(psInst->ui16Index == pCommand->ui16ReadBatchSize)
{
//
// Move to the read pause state.
//
psInst->ui8State = STATE_READ_PAUSE;
//
// Call the callback function.
//
if(pCommand->pfnCallback)
{
pCommand->pfnCallback(pCommand->pvCallbackData,
I2CM_STATUS_BATCH_READY);
}
}
else
{
//
// Finish the burst read.
//
MAP_I2CMasterControl(psInst->ui32Base,
I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
//
// The next state is the wait for final read state.
//
psInst->ui8State = STATE_READ_WAIT;
}
}
//*****************************************************************************
//
// This function handles the read pause state of the I2C master state machine.
//
//*****************************************************************************
static void
I2CMStateReadPause(tI2CMInstance *psInst, tI2CMCommand *pCommand)
{
//
// Decrement the read count by the batch size.
//
pCommand->ui16ReadCount -= pCommand->ui16ReadBatchSize;
//
// Reset the read index.
//
psInst->ui16Index = 0;
//
// See if there is more than one byte left to be read.
//
if((pCommand->ui16ReadCount - psInst->ui16Index) == 1)
{
//
// Finish the burst read.
//
MAP_I2CMasterControl(psInst->ui32Base,
I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
//
// The next state is the wait for final read state.
//
psInst->ui8State = STATE_READ_WAIT;
}
else
{
//
// Continue the burst read.
//
MAP_I2CMasterControl(psInst->ui32Base,
I2C_MASTER_CMD_BURST_RECEIVE_CONT);
//
// Determine the next state based on the number of bytes left to read.
//
if((pCommand->ui16ReadCount - psInst->ui16Index) == 2)
{
psInst->ui8State = STATE_READ_FINAL;
}
else
{
psInst->ui8State = STATE_READ_NEXT;
}
}
}
//*****************************************************************************
//
// This function handles the read wait state of the I2C master state machine.
//
//*****************************************************************************
static void
I2CMStateReadWait(tI2CMInstance *psInst, tI2CMCommand *pCommand)
{
//
// Read the received character.
//
pCommand->pui8ReadData[psInst->ui16Index] =
MAP_I2CMasterDataGet(psInst->ui32Base);
//
// The state machine is now in the callback state.
//
psInst->ui8State = STATE_CALLBACK;
}
//*****************************************************************************
//
// This function handles the callback state of the I2C master state machine.
//
//*****************************************************************************
static void
I2CMStateCallback(tI2CMInstance *psInst, tI2CMCommand *pCommand,
uint32_t ui32Status)
{
tSensorCallback *pfnCallback;
void *pvCallbackData;
//
// Save the callback information.
//
pfnCallback = pCommand->pfnCallback;
pvCallbackData = pCommand->pvCallbackData;
//
// This command has been completed, so increment the read pointer.
//
psInst->ui8ReadPtr++;
if(psInst->ui8ReadPtr == NUM_I2CM_COMMANDS)
{
psInst->ui8ReadPtr = 0;
}
//
// If there is a callback function then call it now.
//
if(pfnCallback)
{
//
// Convert the status from the I2C driver into the I2C master
// driver status.
//
if((ui32Status & (I2C_MCS_ARBLST | I2C_MCS_ERROR)) == 0)
{
ui32Status = I2CM_STATUS_SUCCESS;
}
else if(ui32Status & I2C_MCS_ARBLST)
{
ui32Status = I2CM_STATUS_ARB_LOST;
}
else if(ui32Status & I2C_MCS_ADRACK)
{
ui32Status = I2CM_STATUS_ADDR_NACK;
}
else if(ui32Status & I2C_MCS_DATACK)
{
ui32Status = I2CM_STATUS_DATA_NACK;
}
else
{
ui32Status = I2CM_STATUS_ERROR;
}
//
// Call the callback function.
//
pfnCallback(pvCallbackData, ui32Status);
}
//
// The state machine is now idle.
//
psInst->ui8State = STATE_IDLE;
}
//*****************************************************************************
//
//! Handles I2C master interrupts.
//!
//! \param psInst is a pointer to the I2C master instance data.
//!
//! This function performs the processing required in response to an I2C
//! interrupt. The application-supplied interrupt handler should call this
//! function with the correct instance data in response to the I2C interrupt.
//!
//! \return None.
//
//*****************************************************************************
void
I2CMIntHandler(tI2CMInstance *psInst)
{
tI2CMCommand *pCommand;
uint32_t ui32Status;
//
// Clear the I2C interrupt.
//
MAP_I2CMasterIntClear(psInst->ui32Base);
ui32Status = HWREG(psInst->ui32Base + I2C_O_MCS);
//
// Get a pointer to the current command.
//
pCommand = &(psInst->pCommands[psInst->ui8ReadPtr]);
//
// See if an error occurred during the last transaction.
//
if((ui32Status & (I2C_MCS_ERROR | I2C_MCS_ARBLST)) &&
(psInst->ui8State != STATE_IDLE))
{
//
// An error occurred, so halt the I2C transaction. The error stop
// command for send and receive is identical, so it does not matter
// which one is used here. Only issue the stop if the bus is busy.
//
if(ui32Status & I2C_MCS_BUSBSY)
{
MAP_I2CMasterControl(psInst->ui32Base,
I2C_MASTER_CMD_BURST_SEND_ERROR_STOP);
}
//
// Move to the callback state.
//
psInst->ui8State = STATE_CALLBACK;
}
//
// Loop forever. Most states will return when they have completed their
// action. However, a few states require multi-state processing, so those