-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI2C_Stellaris_API.c
859 lines (751 loc) · 24 KB
/
I2C_Stellaris_API.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
//*****************************************************************************
//
// I2C_Stellaris_API.c - Stellaris I2C Master Driver.
//
// 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. THE AUTHOR SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
// AUTHOR: JOERG QUINTEN
// E2E-NICKNAME: aBUGSworstnightmare
//
//*****************************************************************************
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/i2c.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "utils/uartstdio.h"
#include "I2C_Stellaris_API.h"
//*****************************************************************************
//
//! \internal
//! Checks a I2C master base address.
//!
//! \param ulI2CBase is the base address of the I2C Master module.
//!
//! This function determines if a I2C master module base address is valid.
//! \e ulI2CBase parameter can be one of the following values:
//!
//! - \b I2C0_MASTER_BASE
//! - \b I2C1_MASTER_BASE
//! - \b I2C2_MASTER_BASE
//! - \b I2C3_MASTER_BASE
//!
//! \return Returns \b true if the base address is valid and \b false
//! otherwise.
//
//*****************************************************************************
#ifdef DEBUG
static tBoolean
I2CMasterBaseValid(unsigned long ulI2CBase)
{
return( (ulI2CBase == I2C0_MASTER_BASE) || (ulI2CBase == I2C1_MASTER_BASE) ||
(ulI2CBase == I2C2_MASTER_BASE) || (ulI2CBase == I2C3_MASTER_BASE));
}
#endif
//*****************************************************************************
//
//! Initializes and enables the specified I2C block.
//!
//! \param ulI2CBase is the I2C peripheral to be used.
//! \param ulI2CSpeed defines the normal (100kbps) or fast (400kbps) I2C mode.
//!
//! This function enables the specified I2C block and sets it up to run at
//! the either 100kbps or 400kbps. If the \e ulI2CSpeed is false, the I2C will
//! run at 100kbps and if true, then the I2C will run at 400kbps. The
//! \e ulI2CBase parameter can be one of the following values:
//!
//! - \b I2C0_MASTER_BASE
//! - \b I2C1_MASTER_BASE
//! - \b I2C2_MASTER_BASE
//! - \b I2C3_MASTER_BASE
//!
//! \return None.
//
//*****************************************************************************
void I2CSetup(unsigned long ulI2CBase, unsigned long ulI2CSpeed)
{
//
// Check the arguments.
//
ASSERT(I2CMasterBaseValid(ulI2CBase));
ASSERT((ulI2CSpeed == true) || (ulI2CSpeed == false));
switch (ulI2CBase)
{
// I2C_PERIPH_0
case I2C0_MASTER_BASE:
//
// I2C0 is used with PortB[3:2]. The actual port and
// pins used may be different on your part, consult the data sheet for
// more information. GPIO port B needs to be enabled so these pins can
// be used.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
//
// Select the I2C function for these pins. This function will also
// configure the GPIO pins for I2C operation, setting them to
// open-drain operation with weak pull-ups. Consult the data sheet
// to see which functions are allocated per pin.
//
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); // special I2CSCL treatment for M4F devices
ROM_GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
//
// Configure the pin muxing for I2C0 functions on port B2 and B3.
// This step is not necessary if your part does not support pin muxing.
//
ROM_GPIOPinConfigure(GPIO_PB2_I2C0SCL);
ROM_GPIOPinConfigure(GPIO_PB3_I2C0SDA);
//
// The I2C0 peripheral must be enabled before use.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
//
// Enable and initialize the I2C0 master module.
//
ROM_I2CMasterInitExpClk(I2C0_MASTER_BASE, SysCtlClockGet(), ulI2CSpeed);
break;
// I2C_PERIPH_1
case I2C1_MASTER_BASE:
//
// I2C1 is used with PortA[7:6]. The actual port and
// pins used may be different on your part, consult the data sheet for
// more information. GPIO port A needs to be enabled so these pins can
// be used.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Select the I2C function for these pins. This function will also
// configure the GPIO pins for I2C operation, setting them to
// open-drain operation with weak pull-ups. Consult the data sheet
// to see which functions are allocated per pin.
//
GPIOPinTypeI2CSCL(GPIO_PORTA_BASE, GPIO_PIN_6); // special I2CSCL treatment for M4F devices
ROM_GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_7);
//
// Configure the pin muxing for I2C1 functions on port A6 and A7.
// This step is not necessary if your part does not support pin muxing.
//
ROM_GPIOPinConfigure(GPIO_PA6_I2C1SCL);
ROM_GPIOPinConfigure(GPIO_PA7_I2C1SDA);
//
// The I2C1 peripheral must be enabled before use.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1);
//
// Enable and initialize the I2C1 master module.
//
ROM_I2CMasterInitExpClk(I2C1_MASTER_BASE, SysCtlClockGet(), ulI2CSpeed);
break;
// I2C_PERIPH_2
case I2C2_MASTER_BASE:
//
// I2C2 is used with PortE[5:4]. The actual port and
// pins used may be different on your part, consult the data sheet for
// more information. GPIO port E needs to be enabled so these pins can
// be used.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
//
// Select the I2C function for these pins. This function will also
// configure the GPIO pins for I2C operation, setting them to
// open-drain operation with weak pull-ups. Consult the data sheet
// to see which functions are allocated per pin.
//
GPIOPinTypeI2CSCL(GPIO_PORTE_BASE, GPIO_PIN_4); // special I2CSCL treatment for M4F devices
ROM_GPIOPinTypeI2C(GPIO_PORTE_BASE, GPIO_PIN_5);
//
// Configure the pin muxing for I2C2 functions on port E4 and E5.
// This step is not necessary if your part does not support pin muxing.
//
ROM_GPIOPinConfigure(GPIO_PE4_I2C2SCL);
ROM_GPIOPinConfigure(GPIO_PE5_I2C2SDA);
//
// The I2C2 peripheral must be enabled before use.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C2);
//
// Enable and initialize the I2C2 master module.
//
ROM_I2CMasterInitExpClk(I2C2_MASTER_BASE, SysCtlClockGet(), ulI2CSpeed);
break;
// I2C_PERIPH_3
case I2C3_MASTER_BASE:
//
// I2C3 is used with PortD[1:0]. The actual port and
// pins used may be different on your part, consult the data sheet for
// more information. GPIO port D needs to be enabled so these pins can
// be used.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
//
// Select the I2C function for these pins. This function will also
// configure the GPIO pins for I2C operation, setting them to
// open-drain operation with weak pull-ups. Consult the data sheet
// to see which functions are allocated per pin.
//
GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0); // special I2CSCL treatment for M4F devices
ROM_GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1);
//
// Configure the pin muxing for I2C2 functions on port D0 and D1.
// This step is not necessary if your part does not support pin muxing.
//
ROM_GPIOPinConfigure(GPIO_PD0_I2C3SCL);
ROM_GPIOPinConfigure(GPIO_PD1_I2C3SDA);
//
// The I2C3 peripheral must be enabled before use.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3);
//
// Enable and initialize the I2C3 master module.
//
ROM_I2CMasterInitExpClk(I2C3_MASTER_BASE, SysCtlClockGet(), ulI2CSpeed);
break;
}
}
//*****************************************************************************
//
//! Reads the I2C slave register.
//!
//! \param ulI2CBase is the base for the I2C module.
//! \param ucSlaveAdress is the 7-bit address of the slave to be addressed.
//! \param ucReg is the register to read from.
//!
//! This function initiates a read from the slave device.
//! The ulI2CBase parameter is the I2C modules master base address.
//! \e ulI2CBase parameter can be one of the following values:
//!
//! - \b I2C0_MASTER_BASE
//! - \b I2C1_MASTER_BASE
//! - \b I2C2_MASTER_BASE
//! - \b I2C3_MASTER_BASE
//!
//! \return Register value in an unsigned long format. Note that 0 will be
//! returned if there is ever an error, 1 if there was not.
//
//*****************************************************************************
unsigned long
I2CRegRead(unsigned long ulI2CBase, unsigned char ucSlaveAdress, unsigned char ucReg)
{
unsigned long ulRegValue = 0;
//
// Check the arguments.
//
ASSERT(I2CMasterBaseValid(ulI2CBase));
//
// Wait until master module is done transferring.
//
while(ROM_I2CMasterBusy(ulI2CBase))
{
};
//
// Tell the master module what address it will place on the bus when
// writing to the slave.
//
ROM_I2CMasterSlaveAddrSet(ulI2CBase, ucSlaveAdress, 0);
//
// Place the command to be sent in the data register.
//
ROM_I2CMasterDataPut(ulI2CBase, ucReg);
//
// Initiate send of data from the master.
//
ROM_I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_SINGLE_SEND);
//
// Wait until master module is done transferring.
//
while(ROM_I2CMasterBusy(ulI2CBase))
{
};
//
// Check for errors.
//
if(ROM_I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE)
{
return 0;
}
//
// Tell the master module what address it will place on the bus when
// reading from the slave.
//
ROM_I2CMasterSlaveAddrSet(ulI2CBase, ucSlaveAdress, 1);
//
// Tell the master to read data.
//
ROM_I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_SINGLE_RECEIVE);
//
// Wait until master module is done receiving.
//
while(ROM_I2CMasterBusy(ulI2CBase))
{
};
//
// Check for errors.
//
if(ROM_I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE)
{
return 0;
}
//
// Read the data from the master.
//
ulRegValue = ROM_I2CMasterDataGet(ulI2CBase);
//
// Return the register value.
//
return ulRegValue;
}
//*****************************************************************************
//
//! Writes to the specified I2C slave register.
//!
//! \param ulI2CBase is the base for the I2C module.
//! \param ucSlaveAdress is the 7-bit address of the slave to be addressed.
//! \param ucReg is the register to write data to.
//! \param ucValue is the 8-bit data to be written.
//!
//! This function initiates a read from the I2C slave device.
//! The ulI2CBase parameter is the I2C modules master base address.
//! \e ulI2CBase parameter can be one of the following values:
//!
//! - \b I2C0_MASTER_BASE
//! - \b I2C1_MASTER_BASE
//! - \b I2C2_MASTER_BASE
//! - \b I2C3_MASTER_BASE
//!
//! \return Register value in an unsigned long format. Note that 0 will be
//! returned if there is ever an error, 1 if there was not.
//
//*****************************************************************************
unsigned long
I2CRegWrite(unsigned long ulI2CBase, unsigned char ucSlaveAdress,
unsigned char ucReg, unsigned char ucValue)
{
//
// Check the arguments.
//
ASSERT(I2CMasterBaseValid(ulI2CBase));
//
// Wait until master module is done transferring.
//
while(ROM_I2CMasterBusy(ulI2CBase))
{
};
//
// Tell the master module what address it will place on the bus when
// writing to the slave.
//
ROM_I2CMasterSlaveAddrSet(ulI2CBase, ucSlaveAdress, 0);
//
// Place the command to be sent in the data register.
//
ROM_I2CMasterDataPut(ulI2CBase, ucReg);
//
// Initiate send of data from the master.
//
ROM_I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_BURST_SEND_START);
//
// Wait until master module is done transferring.
//
while(ROM_I2CMasterBusy(ulI2CBase))
{
};
//
// Check for errors.
//
if(ROM_I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE)
{
return 0;
}
//
// Place the value to be sent in the data register.
//
ROM_I2CMasterDataPut(ulI2CBase, ucValue);
//
// Initiate send of data from the master.
//
ROM_I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_BURST_SEND_CONT);
//
// Wait until master module is done transferring.
//
while(ROM_I2CMasterBusy(ulI2CBase))
{
};
//
// Check for errors.
//
if(ROM_I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE)
{
return 0;
}
//
// Initiate send of data from the master.
//
ROM_I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_BURST_SEND_FINISH);
//
// Wait until master module is done transferring.
//
while(ROM_I2CMasterBusy(ulI2CBase))
{
};
//
// Check for errors.
//
if(ROM_I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE)
{
return 0;
}
//
// Return 1 if there is no error.
//
return 1;
}
//*****************************************************************************
//
//! Reads one/multiple bytes of data from an I2C slave device.
//!
//! \param ulI2CBase is the base for the I2C module.
//! \param ucSlaveAdress is the 7-bit address of the slave to be addressed.
//! \param ucReg is the register to start reading from.
//! \param cReadData is a pointer to the array to store the data.
//! \param uiSize is the number of bytes to read from the slave.
//!
//! This function reads one/multiple bytes of data from an I2C slave device.
//! The ulI2CBase parameter is the I2C modules master base address.
//! \e ulI2CBase parameter can be one of the following values:
//!
//! - \b I2C0_MASTER_BASE
//! - \b I2C1_MASTER_BASE
//! - \b I2C2_MASTER_BASE
//! - \b I2C3_MASTER_BASE
//!
//! \return 0 if there was an error or 1 if there was not.
//
//*****************************************************************************
unsigned long
I2CReadData(unsigned long ulI2CBase, unsigned char ucSlaveAdress,
unsigned char ucReg, char* cReadData, unsigned int uiSize)
{
unsigned int uibytecount; // local variable used for byte counting/state determination
int MasterOptionCommand; // used to assign the commands for ROM_I2CMasterControl() function
//
// Check the arguments.
//
ASSERT(I2CMasterBaseValid(ulI2CBase));
//
// Wait until master module is done transferring.
//
while(ROM_I2CMasterBusy(ulI2CBase))
{
};
//
// Tell the master module what address it will place on the bus when
// writing to the slave.
//
ROM_I2CMasterSlaveAddrSet(ulI2CBase, ucSlaveAdress, 0);
//
// Place the command to be sent in the data register.
//
ROM_I2CMasterDataPut(ulI2CBase, ucReg);
//
// Initiate send of data from the master.
//
ROM_I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_SINGLE_SEND);
//
// Wait until master module is done transferring.
//
while(ROM_I2CMasterBusy(ulI2CBase))
{
};
//
// Check for errors.
//
if(ROM_I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE)
{
return 0;
}
//
// Tell the master module what address it will place on the bus when
// reading from the slave.
//
ROM_I2CMasterSlaveAddrSet(ulI2CBase, ucSlaveAdress, true);
//
// Start with BURST with more than one byte to write
//
MasterOptionCommand = I2C_MASTER_CMD_BURST_RECEIVE_START;
for(uibytecount = 0; uibytecount < uiSize; uibytecount++)
{
//
// The second and intermittent byte has to be read with CONTINUE control word
//
if(uibytecount == 1)
MasterOptionCommand = I2C_MASTER_CMD_BURST_RECEIVE_CONT;
//
// The last byte has to be send with FINISH control word
//
if(uibytecount == uiSize - 1)
MasterOptionCommand = I2C_MASTER_CMD_BURST_RECEIVE_FINISH;
//
// Re-configure to SINGLE if there is only one byte to read
//
if(uiSize == 1)
MasterOptionCommand = I2C_MASTER_CMD_SINGLE_RECEIVE;
//
// Initiate read of data from the slave.
//
ROM_I2CMasterControl(ulI2CBase, MasterOptionCommand);
//
// Wait until master module is done reading.
//
while(ROM_I2CMasterBusy(ulI2CBase))
{
};
//
// Check for errors.
//
if(ROM_I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE)
{
return 0;
}
//
// Move byte from register
//
cReadData[uibytecount] = I2CMasterDataGet(ulI2CBase);
}
// send number of received bytes
return uibytecount;
}
//*****************************************************************************
//
//! Writes one/multiple bytes of data to an I2C slave device.
//! Ensure to use auto-increment options on some devices
//! (Control Registers, refer to data sheet).
//! I.e. store related command in the first position of your data array.
//!
//! \param ulI2CBase is the base for the I2C module.
//! \param ucSlaveAdress is the 7-bit address of the slave to be addressed.
//! \param ucReg is the register to start writing to.
//! \param cSendData is a pointer to the array to be send.
//! \param uiSize is the number of bytes to send from array cSendData[].
//!
//! This function writes multiple bytes of data an I2C slave device.
//! The ulI2CBase parameter is the I2C modules master base address.
//! \e ulI2CBase parameter can be one of the following values:
//!
//! - \b I2C0_MASTER_BASE
//! - \b I2C1_MASTER_BASE
//! - \b I2C2_MASTER_BASE
//! - \b I2C3_MASTER_BASE
//!
//! \return 0 if there was an error or 1 if there was not.
//
//*****************************************************************************
unsigned long
I2CWriteData(unsigned long ulI2CBase, unsigned char ucSlaveAdress,
unsigned char ucReg, char* cSendData, unsigned int uiSize)
{
unsigned int uibytecount; // local variable used for byte counting/state determination
int MasterOptionCommand; // used to assign the commands for ROM_I2CMasterControl() function
//
// Check the arguments.
//
ASSERT(I2CMasterBaseValid(ulI2CBase));
//
// Wait until master module is done transferring.
//
while(ROM_I2CMasterBusy(ulI2CBase))
{
};
//
// Tell the master module what address it will place on the bus when
// writing to the slave.
//
ROM_I2CMasterSlaveAddrSet(ulI2CBase, ucSlaveAdress, false);
//
// Place the value to be sent in the data register.
//
ROM_I2CMasterDataPut(ulI2CBase, ucReg);
//
// Initiate send of data from the master.
//
ROM_I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_BURST_SEND_START);
//
// Wait until master module is done transferring.
//
while(ROM_I2CMasterBusy(ulI2CBase))
{
};
//
// Check for errors.
//
if(ROM_I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE)
{
return 0;
}
//
// Start with CONT for more than one byte to write
//
MasterOptionCommand = I2C_MASTER_CMD_BURST_SEND_CONT;
for(uibytecount = 0; uibytecount < uiSize; uibytecount++)
{
//
// The second and intermittent byte has to be send with CONTINUE control word
//
if(uibytecount == 1)
MasterOptionCommand = I2C_MASTER_CMD_BURST_SEND_CONT;
//
// The last byte has to be send with FINISH control word
//
if(uibytecount == uiSize - 1)
MasterOptionCommand = I2C_MASTER_CMD_BURST_SEND_FINISH;
//
// Re-configure to SINGLE if there is only one byte to write
//
if(uiSize == 1)
MasterOptionCommand = I2C_MASTER_CMD_SINGLE_SEND;
//
// Send data byte
//
ROM_I2CMasterDataPut(ulI2CBase, cSendData[uibytecount]);
//
// Initiate send of data from the master.
//
ROM_I2CMasterControl(ulI2CBase, MasterOptionCommand);
//
// Wait until master module is done transferring.
//
while(ROM_I2CMasterBusy(ulI2CBase))
{
};
//
// Check for errors.
//
if(ROM_I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE)
{
return 0;
}
}
//
// Return 1 if there is no error.
//
return 1;
}
//*****************************************************************************
//
//! Probes the selected I2C bus for available slave devices
//!
//! \param ulI2CBase is the base for the I2C module.
//!
//! This function scans the selected I2C bus for available I2C slave device.
//! The ulI2CBase parameter is the I2C modules master base address.
//! \e ulI2CBase parameter can be one of the following values:
//!
//! - \b I2C0_MASTER_BASE
//! - \b I2C1_MASTER_BASE
//! - \b I2C2_MASTER_BASE
//! - \b I2C3_MASTER_BASE
//!
//! \return 0 if there was an error or 1 if there was not.
//
//*****************************************************************************
unsigned long
I2CBusScan(unsigned long ulI2CBase)
{
unsigned char ucProbeAdress;
unsigned long ucerrorstate;
//
// Check the arguments.
//
ASSERT(I2CMasterBaseValid(ulI2CBase));
//
// Wait until master module is done transferring.
//
while(ROM_I2CMasterBusy(ulI2CBase))
{
};
//
// I2C Addresses are 7-bit values
// probe the address range of 0 to 127 to find I2C slave devices on the bus
//
for (ucProbeAdress = 0; ucProbeAdress < 127; ucProbeAdress++)
{
//
// Tell the master module what address it will place on the bus when
// writing to the slave.
//
ROM_I2CMasterSlaveAddrSet(ulI2CBase, ucProbeAdress, false);
ROM_SysCtlDelay(50000);
//
// Place the command to be sent in the data register.
//
ROM_I2CMasterDataPut(ulI2CBase, 0x00);
//
// Initiate send of data from the master.
//
ROM_I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_BURST_SEND_START);
//
// Make some delay
//
ROM_SysCtlDelay(500000);
//
// Read the I2C Master Control/Status (I2CMCS) Register to a local
// variable
//
ucerrorstate = ROM_I2CMasterErr(ulI2CBase);
//
// Examining the content I2C Master Control/Status (I2CMCS) Register
// to see if the ADRACK-Bit (Acknowledge Address) is TRUE (1)
// ( 1: The transmitted address was not acknowledged by the slave)
//
if(ucerrorstate & I2C_MASTER_ERR_ADDR_ACK)
{
//
// device at selected address did not acknowledge --> there's no device
// with this address present on the I2C bus
//
//
// Print a message to Stdio
//
//UARTprintf("Address not found: 0x%2x - %3d\n",ucProbeAdress,ucProbeAdress);
//
// Make some delay
//
//ROM_SysCtlDelay(1500000);
}
//
// ( 0: The transmitted address was acknowledged by the slave)
//
else
{
//
// device at selected address acknowledged --> there is a device
// with this address present on the I2C bus
//
//
// Print a message to Stdio
//
UARTprintf("Address found: 0x%2x - %3d\n",ucProbeAdress,ucProbeAdress);
//
// Make some delay
//
ROM_SysCtlDelay(1500000);
}
}
//
// End transfer of data from the master.
//
ROM_I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
//
// Print a message to Stdio
//
UARTprintf("I2C Bus-Scan done...\n");
//
// Return 1 if there is no error.
//
return 1;
}